4 PyDoc_STRVAR(operator_doc
,
5 "Operator interface.\n\
7 This module exports a set of functions implemented in C corresponding\n\
8 to the intrinsic operators of Python. For example, operator.add(x, y)\n\
9 is equivalent to the expression x+y. The function names are those\n\
10 used for special class methods; variants without leading and trailing\n\
11 '__' are also provided for convenience.");
13 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
18 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
21 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
22 PyObject *a1; int a2; \
23 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
28 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
29 if(-1 == AOP(a1,a2)) return NULL; \
33 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
34 PyObject *a1, *a2, *a3; \
35 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
36 if(-1 == AOP(a1,a2,a3)) return NULL; \
40 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
42 if(-1 == (r=AOP(a1))) return NULL; \
43 return PyBool_FromLong(r); }
45 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
46 PyObject *a1, *a2; long r; \
47 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
48 if(-1 == (r=AOP(a1,a2))) return NULL; \
49 return PyInt_FromLong(r); }
51 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
52 PyObject *a1, *a2; long r; \
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
54 if(-1 == (r=AOP(a1,a2))) return NULL; \
55 return PyBool_FromLong(r); }
57 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
60 return PyObject_RichCompare(a1,a2,A); }
62 spami(isCallable
, PyCallable_Check
)
63 spami(isNumberType
, PyNumber_Check
)
64 spami(truth
, PyObject_IsTrue
)
65 spam2(op_add
, PyNumber_Add
)
66 spam2(op_sub
, PyNumber_Subtract
)
67 spam2(op_mul
, PyNumber_Multiply
)
68 spam2(op_div
, PyNumber_Divide
)
69 spam2(op_floordiv
, PyNumber_FloorDivide
)
70 spam2(op_truediv
, PyNumber_TrueDivide
)
71 spam2(op_mod
, PyNumber_Remainder
)
72 spam1(op_neg
, PyNumber_Negative
)
73 spam1(op_pos
, PyNumber_Positive
)
74 spam1(op_abs
, PyNumber_Absolute
)
75 spam1(op_inv
, PyNumber_Invert
)
76 spam1(op_invert
, PyNumber_Invert
)
77 spam2(op_lshift
, PyNumber_Lshift
)
78 spam2(op_rshift
, PyNumber_Rshift
)
79 spami(op_not_
, PyObject_Not
)
80 spam2(op_and_
, PyNumber_And
)
81 spam2(op_xor
, PyNumber_Xor
)
82 spam2(op_or_
, PyNumber_Or
)
83 spami(isSequenceType
, PySequence_Check
)
84 spam2(op_concat
, PySequence_Concat
)
85 spamoi(op_repeat
, PySequence_Repeat
)
86 spami2b(op_contains
, PySequence_Contains
)
87 spami2b(sequenceIncludes
, PySequence_Contains
)
88 spami2(indexOf
, PySequence_Index
)
89 spami2(countOf
, PySequence_Count
)
90 spami(isMappingType
, PyMapping_Check
)
91 spam2(op_getitem
, PyObject_GetItem
)
92 spam2n(op_delitem
, PyObject_DelItem
)
93 spam3n(op_setitem
, PyObject_SetItem
)
102 op_pow(PyObject
*s
, PyObject
*a
)
105 if (PyArg_UnpackTuple(a
,"pow", 2, 2, &a1
, &a2
))
106 return PyNumber_Power(a1
, a2
, Py_None
);
111 is_(PyObject
*s
, PyObject
*a
)
113 PyObject
*a1
, *a2
, *result
= NULL
;
114 if (PyArg_UnpackTuple(a
,"is_", 2, 2, &a1
, &a2
)) {
115 result
= (a1
== a2
) ? Py_True
: Py_False
;
122 is_not(PyObject
*s
, PyObject
*a
)
124 PyObject
*a1
, *a2
, *result
= NULL
;
125 if (PyArg_UnpackTuple(a
,"is_not", 2, 2, &a1
, &a2
)) {
126 result
= (a1
!= a2
) ? Py_True
: Py_False
;
133 op_getslice(PyObject
*s
, PyObject
*a
)
138 if (!PyArg_ParseTuple(a
,"Oii:getslice",&a1
,&a2
,&a3
))
140 return PySequence_GetSlice(a1
,a2
,a3
);
144 op_setslice(PyObject
*s
, PyObject
*a
)
149 if (!PyArg_ParseTuple(a
,"OiiO:setslice",&a1
,&a2
,&a3
,&a4
))
152 if (-1 == PySequence_SetSlice(a1
,a2
,a3
,a4
))
160 op_delslice(PyObject
*s
, PyObject
*a
)
165 if(! PyArg_ParseTuple(a
,"Oii:delslice",&a1
,&a2
,&a3
))
168 if (-1 == PySequence_DelSlice(a1
,a2
,a3
))
179 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
180 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, DOC}, \
181 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
182 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
183 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, DOC}, \
184 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
186 static struct PyMethodDef operator_methods
[] = {
189 "isCallable(a) -- Same as callable(a).")
191 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
192 spam1o(isSequenceType
,
193 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
195 "truth(a) -- Return True if a is true, False otherwise.")
196 spam2(contains
,__contains__
,
197 "contains(a, b) -- Same as b in a (note reversed operands).")
198 spam1(sequenceIncludes
,
199 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
201 "indexOf(a, b) -- Return the first index of b in a.")
203 "countOf(a, b) -- Return the number of times b occurs in a.")
204 spam1o(isMappingType
,
205 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
207 spam1(is_
, "is_(a, b) -- Same as a is b.")
208 spam1(is_not
, "is_not(a, b) -- Same as a is not b.")
209 spam2(add
,__add__
, "add(a, b) -- Same as a + b.")
210 spam2(sub
,__sub__
, "sub(a, b) -- Same as a - b.")
211 spam2(mul
,__mul__
, "mul(a, b) -- Same as a * b.")
212 spam2(div
,__div__
, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
213 spam2(floordiv
,__floordiv__
, "floordiv(a, b) -- Same as a // b.")
214 spam2(truediv
,__truediv__
, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
215 spam2(mod
,__mod__
, "mod(a, b) -- Same as a % b.")
216 spam2o(neg
,__neg__
, "neg(a) -- Same as -a.")
217 spam2o(pos
,__pos__
, "pos(a) -- Same as +a.")
218 spam2o(abs
,__abs__
, "abs(a) -- Same as abs(a).")
219 spam2o(inv
,__inv__
, "inv(a) -- Same as ~a.")
220 spam2o(invert
,__invert__
, "invert(a) -- Same as ~a.")
221 spam2(lshift
,__lshift__
, "lshift(a, b) -- Same as a << b.")
222 spam2(rshift
,__rshift__
, "rshift(a, b) -- Same as a >> b.")
223 spam2o(not_
,__not__
, "not_(a) -- Same as not a.")
224 spam2(and_
,__and__
, "and_(a, b) -- Same as a & b.")
225 spam2(xor,__xor__
, "xor(a, b) -- Same as a ^ b.")
226 spam2(or_
,__or__
, "or_(a, b) -- Same as a | b.")
227 spam2(concat
,__concat__
,
228 "concat(a, b) -- Same as a + b, for a and b sequences.")
229 spam2(repeat
,__repeat__
,
230 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
231 spam2(getitem
,__getitem__
,
232 "getitem(a, b) -- Same as a[b].")
233 spam2(setitem
,__setitem__
,
234 "setitem(a, b, c) -- Same as a[b] = c.")
235 spam2(delitem
,__delitem__
,
236 "delitem(a, b) -- Same as del a[b].")
237 spam2(pow
,__pow__
, "pow(a, b) -- Same as a**b.")
238 spam2(getslice
,__getslice__
,
239 "getslice(a, b, c) -- Same as a[b:c].")
240 spam2(setslice
,__setslice__
,
241 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
242 spam2(delslice
,__delslice__
,
243 "delslice(a, b, c) -- Same as del a[b:c].")
244 spam2(lt
,__lt__
, "lt(a, b) -- Same as a<b.")
245 spam2(le
,__le__
, "le(a, b) -- Same as a<=b.")
246 spam2(eq
,__eq__
, "eq(a, b) -- Same as a==b.")
247 spam2(ne
,__ne__
, "ne(a, b) -- Same as a!=b.")
248 spam2(gt
,__gt__
, "gt(a, b) -- Same as a>b.")
249 spam2(ge
,__ge__
, "ge(a, b) -- Same as a>=b.")
251 {NULL
, NULL
} /* sentinel */
255 /* itemgetter object **********************************************************/
262 static PyTypeObject itemgetter_type
;
265 itemgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
267 itemgetterobject
*ig
;
270 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &item
))
273 /* create itemgetterobject structure */
274 ig
= PyObject_GC_New(itemgetterobject
, &itemgetter_type
);
281 PyObject_GC_Track(ig
);
282 return (PyObject
*)ig
;
286 itemgetter_dealloc(itemgetterobject
*ig
)
288 PyObject_GC_UnTrack(ig
);
289 Py_XDECREF(ig
->item
);
294 itemgetter_traverse(itemgetterobject
*ig
, visitproc visit
, void *arg
)
297 return visit(ig
->item
, arg
);
302 itemgetter_call(itemgetterobject
*ig
, PyObject
*args
, PyObject
*kw
)
306 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &obj
))
308 return PyObject_GetItem(obj
, ig
->item
);
311 PyDoc_STRVAR(itemgetter_doc
,
312 "itemgetter(item) --> itemgetter object\n\
314 Return a callable object that fetches the given item from its operand.\n\
315 After, f=itemgetter(2), the call f(b) returns b[2].");
317 static PyTypeObject itemgetter_type
= {
318 PyObject_HEAD_INIT(NULL
)
320 "operator.itemgetter", /* tp_name */
321 sizeof(itemgetterobject
), /* tp_basicsize */
324 (destructor
)itemgetter_dealloc
, /* tp_dealloc */
330 0, /* tp_as_number */
331 0, /* tp_as_sequence */
332 0, /* tp_as_mapping */
334 (ternaryfunc
)itemgetter_call
, /* tp_call */
336 PyObject_GenericGetAttr
, /* tp_getattro */
338 0, /* tp_as_buffer */
339 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
340 itemgetter_doc
, /* tp_doc */
341 (traverseproc
)itemgetter_traverse
, /* tp_traverse */
343 0, /* tp_richcompare */
344 0, /* tp_weaklistoffset */
352 0, /* tp_descr_get */
353 0, /* tp_descr_set */
354 0, /* tp_dictoffset */
357 itemgetter_new
, /* tp_new */
362 /* attrgetter object **********************************************************/
369 static PyTypeObject attrgetter_type
;
372 attrgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
374 attrgetterobject
*ag
;
377 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &attr
))
380 /* create attrgetterobject structure */
381 ag
= PyObject_GC_New(attrgetterobject
, &attrgetter_type
);
388 PyObject_GC_Track(ag
);
389 return (PyObject
*)ag
;
393 attrgetter_dealloc(attrgetterobject
*ag
)
395 PyObject_GC_UnTrack(ag
);
396 Py_XDECREF(ag
->attr
);
401 attrgetter_traverse(attrgetterobject
*ag
, visitproc visit
, void *arg
)
404 return visit(ag
->attr
, arg
);
409 attrgetter_call(attrgetterobject
*ag
, PyObject
*args
, PyObject
*kw
)
413 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &obj
))
415 return PyObject_GetAttr(obj
, ag
->attr
);
418 PyDoc_STRVAR(attrgetter_doc
,
419 "attrgetter(attr) --> attrgetter object\n\
421 Return a callable object that fetches the given attribute from its operand.\n\
422 After, f=attrgetter('name'), the call f(b) returns b.name.");
424 static PyTypeObject attrgetter_type
= {
425 PyObject_HEAD_INIT(NULL
)
427 "operator.attrgetter", /* tp_name */
428 sizeof(attrgetterobject
), /* tp_basicsize */
431 (destructor
)attrgetter_dealloc
, /* tp_dealloc */
437 0, /* tp_as_number */
438 0, /* tp_as_sequence */
439 0, /* tp_as_mapping */
441 (ternaryfunc
)attrgetter_call
, /* tp_call */
443 PyObject_GenericGetAttr
, /* tp_getattro */
445 0, /* tp_as_buffer */
446 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
447 attrgetter_doc
, /* tp_doc */
448 (traverseproc
)attrgetter_traverse
, /* tp_traverse */
450 0, /* tp_richcompare */
451 0, /* tp_weaklistoffset */
459 0, /* tp_descr_get */
460 0, /* tp_descr_set */
461 0, /* tp_dictoffset */
464 attrgetter_new
, /* tp_new */
467 /* Initialization function for the module (*must* be called initoperator) */
474 /* Create the module and add the functions */
475 m
= Py_InitModule4("operator", operator_methods
, operator_doc
,
476 (PyObject
*)NULL
, PYTHON_API_VERSION
);
478 if (PyType_Ready(&itemgetter_type
) < 0)
480 Py_INCREF(&itemgetter_type
);
481 PyModule_AddObject(m
, "itemgetter", (PyObject
*)&itemgetter_type
);
483 if (PyType_Ready(&attrgetter_type
) < 0)
485 Py_INCREF(&attrgetter_type
);
486 PyModule_AddObject(m
, "attrgetter", (PyObject
*)&attrgetter_type
);