1 static char operator_doc
[] = "\
4 This module exports a set of functions implemented in C corresponding\n\
5 to the intrinsic operators of Python. For example, operator.add(x, y)\n\
6 is equivalent to the expression x+y. The function names are those\n\
7 used for special class methods; variants without leading and trailing\n\
8 '__' are also provided for convenience.\n\
15 Copyright 1996 Digital Creations, L.C., 910 Princess Anne
16 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
17 rights reserved. Copyright in this software is owned by DCLC,
18 unless otherwise indicated. Permission to use, copy and
19 distribute this software is hereby granted, provided that the
20 above copyright notice appear in all copies and that both that
21 copyright notice and this permission notice appear. Note that
22 any product, process or technology described in this software
23 may be the subject of other Intellectual Property rights
24 reserved by Digital Creations, L.C. and are not licensed
29 Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
30 All other trademarks are owned by their respective companies.
34 The software is provided "as is" without warranty of any kind,
35 either express or implied, including, but not limited to, the
36 implied warranties of merchantability, fitness for a particular
37 purpose, or non-infringement. This software could include
38 technical inaccuracies or typographical errors. Changes are
39 periodically made to the software; these changes will be
40 incorporated in new editions of the software. DCLC may make
41 improvements and/or changes in this software at any time
44 Limitation Of Liability
46 In no event will DCLC be liable for direct, indirect, special,
47 incidental, economic, cover, or consequential damages arising
48 out of the use of or inability to use this software even if
49 advised of the possibility of such damages. Some states do not
50 allow the exclusion or limitation of implied warranties or
51 limitation of liability for incidental or consequential
52 damages, so the above limitation or exclusion may not apply to
56 If you have questions regarding this software,
59 Jim Fulton, jim@digicool.com
60 Digital Creations L.C.
66 Renamed and slightly rearranged by Guido van Rossum
72 #define spam1(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
74 if(! PyArg_ParseTuple(a,"O",&a1)) return NULL; \
77 #define spam2(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
79 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
82 #define spamoi(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
83 PyObject *a1; int a2; \
84 if(! PyArg_ParseTuple(a,"Oi",&a1,&a2)) return NULL; \
87 #define spam2n(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
89 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
90 if(-1 == AOP(a1,a2)) return NULL; \
94 #define spam3n(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
95 PyObject *a1, *a2, *a3; \
96 if(! PyArg_ParseTuple(a,"OOO",&a1,&a2,&a3)) return NULL; \
97 if(-1 == AOP(a1,a2,a3)) return NULL; \
101 #define spami(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
102 PyObject *a1; long r; \
103 if(! PyArg_ParseTuple(a,"O",&a1)) return NULL; \
104 if(-1 == (r=AOP(a1))) return NULL; \
105 return PyInt_FromLong(r); }
107 #define spami2(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
108 PyObject *a1, *a2; long r; \
109 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
110 if(-1 == (r=AOP(a1,a2))) return NULL; \
111 return PyInt_FromLong(r); }
113 spami(isCallable
, PyCallable_Check
)
114 spami(isNumberType
, PyNumber_Check
)
115 spami(truth
, PyObject_IsTrue
)
116 spam2(op_add
, PyNumber_Add
)
117 spam2(op_sub
, PyNumber_Subtract
)
118 spam2(op_mul
, PyNumber_Multiply
)
119 spam2(op_div
, PyNumber_Divide
)
120 spam2(op_mod
, PyNumber_Remainder
)
121 spam1(op_neg
, PyNumber_Negative
)
122 spam1(op_pos
, PyNumber_Positive
)
123 spam1(op_abs
, PyNumber_Absolute
)
124 spam1(op_inv
, PyNumber_Invert
)
125 spam2(op_lshift
, PyNumber_Lshift
)
126 spam2(op_rshift
, PyNumber_Rshift
)
127 spami(op_not_
, PyObject_Not
)
128 spam2(op_and_
, PyNumber_And
)
129 spam2(op_xor
, PyNumber_Xor
)
130 spam2(op_or_
, PyNumber_Or
)
131 spami(isSequenceType
, PySequence_Check
)
132 spam2(op_concat
, PySequence_Concat
)
133 spamoi(op_repeat
, PySequence_Repeat
)
134 spami2(op_contains
, PySequence_Contains
)
135 spami2(indexOf
, PySequence_Index
)
136 spami2(countOf
, PySequence_Count
)
137 spami(isMappingType
, PyMapping_Check
)
138 spam2(op_getitem
, PyObject_GetItem
)
139 spam2n(op_delitem
, PyObject_DelItem
)
140 spam3n(op_setitem
, PyObject_SetItem
)
149 if (!PyArg_ParseTuple(a
,"Oii",&a1
,&a2
,&a3
))
151 return PySequence_GetSlice(a1
,a2
,a3
);
161 if (!PyArg_ParseTuple(a
,"OiiO",&a1
,&a2
,&a3
,&a4
))
164 if (-1 == PySequence_SetSlice(a1
,a2
,a3
,a4
))
178 if(! PyArg_ParseTuple(a
,"Oii",&a1
,&a2
,&a3
))
181 if (-1 == PySequence_DelSlice(a1
,a2
,a3
))
191 #define spam1(OP,DOC) {"OP", OP, 1, DOC},
192 #define spam2(OP,ALTOP,DOC) {"OP", op_/**/OP, 1, DOC}, \
193 {"ALTOP", op_/**/OP, 1, DOC},
195 #define spam1(OP,DOC) {#OP, OP, 1, DOC},
196 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, 1, DOC}, \
197 {#ALTOP, op_##OP, 1, DOC},
200 static struct PyMethodDef operator_methods
[] = {
203 "isCallable(a) -- Same as callable(a).")
205 "isNumberType(a) -- Return 1 if a has a numeric type, and zero otherwise.")
206 spam1(isSequenceType
,
207 "isSequenceType(a) -- Return 1 if a has a sequence type, and zero otherwise.")
209 "truth(a) -- Return 1 if a is true, and 0 otherwise.")
210 spam2(contains
,sequenceIncludes
,
211 "contains(a, b) -- Same as b in a (note reversed operands).")
213 "indexOf(a, b) -- Return the first index of b in a.")
215 "countOf(a, b) -- Return the number of times b occurs in a.")
217 "isMappingType(a) -- Return 1 if a has a mapping type, and zero otherwise.")
219 spam2(add
,__add__
, "add(a, b) -- Same as a + b.")
220 spam2(sub
,__sub__
, "sub(a, b) -- Same as a - b.")
221 spam2(mul
,__mul__
, "mul(a, b) -- Same as a * b.")
222 spam2(div
,__div__
, "div(a, b) -- Same as a / b.")
223 spam2(mod
,__mod__
, "mod(a, b) -- Same as a % b.")
224 spam2(neg
,__neg__
, "neg(a) -- Same as -a.")
225 spam2(pos
,__pos__
, "pos(a) -- Same as +a.")
226 spam2(abs
,__abs__
, "abs(a) -- Same as abs(a).")
227 spam2(inv
,__inv__
, "inv(a) -- Same as ~a.")
228 spam2(lshift
,__lshift__
, "lshift(a, b) -- Same as a << b.")
229 spam2(rshift
,__rshift__
, "rshift(a, b) -- Same as a >> b.")
230 spam2(not_
,__not__
, "not_(a) -- Same as not a.")
231 spam2(and_
,__and__
, "and_(a, b) -- Same as a & b.")
232 spam2(xor,__xor__
, "xor(a, b) -- Same as a ^ b.")
233 spam2(or_
,__or__
, "or_(a, b) -- Same as a | b.")
234 spam2(concat
,__concat__
,
235 "concat(a, b) -- Same as a + b, for a and b sequences.")
236 spam2(repeat
,__repeat__
,
237 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
238 spam2(getitem
,__getitem__
,
239 "getitem(a, b) -- Same as a[b].")
240 spam2(setitem
,__setitem__
,
241 "setitem(a, b, c) -- Same as a[b] = c.")
242 spam2(delitem
,__delitem__
,
243 "delitem(a, b) -- Same as del a[b].")
244 spam2(getslice
,__getslice__
,
245 "getslice(a, b, c) -- Same as a[b:c].")
246 spam2(setslice
,__setslice__
,
247 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
248 spam2(delslice
,__delslice__
,
249 "delslice(a, b, c) -- Same as del a[b:c].")
251 {NULL
, NULL
} /* sentinel */
256 /* Initialization function for the module (*must* be called initoperator) */
261 /* Create the module and add the functions */
262 Py_InitModule4("operator", operator_methods
, operator_doc
,
263 (PyObject
*)NULL
, PYTHON_API_VERSION
);