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 spam2(op_iadd
, PyNumber_InPlaceAdd
)
84 spam2(op_isub
, PyNumber_InPlaceSubtract
)
85 spam2(op_imul
, PyNumber_InPlaceMultiply
)
86 spam2(op_idiv
, PyNumber_InPlaceDivide
)
87 spam2(op_ifloordiv
, PyNumber_InPlaceFloorDivide
)
88 spam2(op_itruediv
, PyNumber_InPlaceTrueDivide
)
89 spam2(op_imod
, PyNumber_InPlaceRemainder
)
90 spam2(op_ilshift
, PyNumber_InPlaceLshift
)
91 spam2(op_irshift
, PyNumber_InPlaceRshift
)
92 spam2(op_iand
, PyNumber_InPlaceAnd
)
93 spam2(op_ixor
, PyNumber_InPlaceXor
)
94 spam2(op_ior
, PyNumber_InPlaceOr
)
95 spami(isSequenceType
, PySequence_Check
)
96 spam2(op_concat
, PySequence_Concat
)
97 spamoi(op_repeat
, PySequence_Repeat
)
98 spam2(op_iconcat
, PySequence_InPlaceConcat
)
99 spamoi(op_irepeat
, PySequence_InPlaceRepeat
)
100 spami2b(op_contains
, PySequence_Contains
)
101 spami2b(sequenceIncludes
, PySequence_Contains
)
102 spami2(indexOf
, PySequence_Index
)
103 spami2(countOf
, PySequence_Count
)
104 spami(isMappingType
, PyMapping_Check
)
105 spam2(op_getitem
, PyObject_GetItem
)
106 spam2n(op_delitem
, PyObject_DelItem
)
107 spam3n(op_setitem
, PyObject_SetItem
)
108 spamrc(op_lt
, Py_LT
)
109 spamrc(op_le
, Py_LE
)
110 spamrc(op_eq
, Py_EQ
)
111 spamrc(op_ne
, Py_NE
)
112 spamrc(op_gt
, Py_GT
)
113 spamrc(op_ge
, Py_GE
)
116 op_pow(PyObject
*s
, PyObject
*a
)
119 if (PyArg_UnpackTuple(a
,"pow", 2, 2, &a1
, &a2
))
120 return PyNumber_Power(a1
, a2
, Py_None
);
125 op_ipow(PyObject
*s
, PyObject
*a
)
128 if (PyArg_UnpackTuple(a
,"ipow", 2, 2, &a1
, &a2
))
129 return PyNumber_InPlacePower(a1
, a2
, Py_None
);
134 is_(PyObject
*s
, PyObject
*a
)
136 PyObject
*a1
, *a2
, *result
= NULL
;
137 if (PyArg_UnpackTuple(a
,"is_", 2, 2, &a1
, &a2
)) {
138 result
= (a1
== a2
) ? Py_True
: Py_False
;
145 is_not(PyObject
*s
, PyObject
*a
)
147 PyObject
*a1
, *a2
, *result
= NULL
;
148 if (PyArg_UnpackTuple(a
,"is_not", 2, 2, &a1
, &a2
)) {
149 result
= (a1
!= a2
) ? Py_True
: Py_False
;
156 op_getslice(PyObject
*s
, PyObject
*a
)
161 if (!PyArg_ParseTuple(a
,"Oii:getslice",&a1
,&a2
,&a3
))
163 return PySequence_GetSlice(a1
,a2
,a3
);
167 op_setslice(PyObject
*s
, PyObject
*a
)
172 if (!PyArg_ParseTuple(a
,"OiiO:setslice",&a1
,&a2
,&a3
,&a4
))
175 if (-1 == PySequence_SetSlice(a1
,a2
,a3
,a4
))
183 op_delslice(PyObject
*s
, PyObject
*a
)
188 if(! PyArg_ParseTuple(a
,"Oii:delslice",&a1
,&a2
,&a3
))
191 if (-1 == PySequence_DelSlice(a1
,a2
,a3
))
202 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
203 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
204 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
205 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
206 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
207 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
209 static struct PyMethodDef operator_methods
[] = {
212 "isCallable(a) -- Same as callable(a).")
214 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
215 spam1o(isSequenceType
,
216 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
218 "truth(a) -- Return True if a is true, False otherwise.")
219 spam2(contains
,__contains__
,
220 "contains(a, b) -- Same as b in a (note reversed operands).")
221 spam1(sequenceIncludes
,
222 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
224 "indexOf(a, b) -- Return the first index of b in a.")
226 "countOf(a, b) -- Return the number of times b occurs in a.")
227 spam1o(isMappingType
,
228 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
230 spam1(is_
, "is_(a, b) -- Same as a is b.")
231 spam1(is_not
, "is_not(a, b) -- Same as a is not b.")
232 spam2(add
,__add__
, "add(a, b) -- Same as a + b.")
233 spam2(sub
,__sub__
, "sub(a, b) -- Same as a - b.")
234 spam2(mul
,__mul__
, "mul(a, b) -- Same as a * b.")
235 spam2(div
,__div__
, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
236 spam2(floordiv
,__floordiv__
, "floordiv(a, b) -- Same as a // b.")
237 spam2(truediv
,__truediv__
, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
238 spam2(mod
,__mod__
, "mod(a, b) -- Same as a % b.")
239 spam2o(neg
,__neg__
, "neg(a) -- Same as -a.")
240 spam2o(pos
,__pos__
, "pos(a) -- Same as +a.")
241 spam2o(abs
,__abs__
, "abs(a) -- Same as abs(a).")
242 spam2o(inv
,__inv__
, "inv(a) -- Same as ~a.")
243 spam2o(invert
,__invert__
, "invert(a) -- Same as ~a.")
244 spam2(lshift
,__lshift__
, "lshift(a, b) -- Same as a << b.")
245 spam2(rshift
,__rshift__
, "rshift(a, b) -- Same as a >> b.")
246 spam2o(not_
,__not__
, "not_(a) -- Same as not a.")
247 spam2(and_
,__and__
, "and_(a, b) -- Same as a & b.")
248 spam2(xor,__xor__
, "xor(a, b) -- Same as a ^ b.")
249 spam2(or_
,__or__
, "or_(a, b) -- Same as a | b.")
250 spam2(iadd
,__iadd__
, "iadd(a, b) -- Same as a += b.")
251 spam2(isub
,__isub__
, "isub(a, b) -- Same as a -= b.")
252 spam2(imul
,__imul__
, "imul(a, b) -- Same as a *= b.")
253 spam2(idiv
,__idiv__
, "idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
254 spam2(ifloordiv
,__ifloordiv__
, "ifloordiv(a, b) -- Same as a //= b.")
255 spam2(itruediv
,__itruediv__
, "itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
256 spam2(imod
,__imod__
, "imod(a, b) -- Same as a %= b.")
257 spam2(ilshift
,__ilshift__
, "ilshift(a, b) -- Same as a <<= b.")
258 spam2(irshift
,__irshift__
, "irshift(a, b) -- Same as a >>= b.")
259 spam2(iand
,__iand__
, "iand(a, b) -- Same as a &= b.")
260 spam2(ixor
,__ixor__
, "ixor(a, b) -- Same as a ^= b.")
261 spam2(ior
,__ior__
, "ior(a, b) -- Same as a |= b.")
262 spam2(concat
,__concat__
,
263 "concat(a, b) -- Same as a + b, for a and b sequences.")
264 spam2(repeat
,__repeat__
,
265 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
266 spam2(iconcat
,__iconcat__
,
267 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
268 spam2(irepeat
,__irepeat__
,
269 "irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
270 spam2(getitem
,__getitem__
,
271 "getitem(a, b) -- Same as a[b].")
272 spam2(setitem
,__setitem__
,
273 "setitem(a, b, c) -- Same as a[b] = c.")
274 spam2(delitem
,__delitem__
,
275 "delitem(a, b) -- Same as del a[b].")
276 spam2(pow
,__pow__
, "pow(a, b) -- Same as a ** b.")
277 spam2(ipow
,__ipow__
, "ipow(a, b) -- Same as a **= b.")
278 spam2(getslice
,__getslice__
,
279 "getslice(a, b, c) -- Same as a[b:c].")
280 spam2(setslice
,__setslice__
,
281 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
282 spam2(delslice
,__delslice__
,
283 "delslice(a, b, c) -- Same as del a[b:c].")
284 spam2(lt
,__lt__
, "lt(a, b) -- Same as a<b.")
285 spam2(le
,__le__
, "le(a, b) -- Same as a<=b.")
286 spam2(eq
,__eq__
, "eq(a, b) -- Same as a==b.")
287 spam2(ne
,__ne__
, "ne(a, b) -- Same as a!=b.")
288 spam2(gt
,__gt__
, "gt(a, b) -- Same as a>b.")
289 spam2(ge
,__ge__
, "ge(a, b) -- Same as a>=b.")
291 {NULL
, NULL
} /* sentinel */
295 /* itemgetter object **********************************************************/
303 static PyTypeObject itemgetter_type
;
306 itemgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
308 itemgetterobject
*ig
;
312 if (!_PyArg_NoKeywords("itemgetter()", kwds
))
315 nitems
= PyTuple_GET_SIZE(args
);
317 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &item
))
322 /* create itemgetterobject structure */
323 ig
= PyObject_GC_New(itemgetterobject
, &itemgetter_type
);
331 PyObject_GC_Track(ig
);
332 return (PyObject
*)ig
;
336 itemgetter_dealloc(itemgetterobject
*ig
)
338 PyObject_GC_UnTrack(ig
);
339 Py_XDECREF(ig
->item
);
344 itemgetter_traverse(itemgetterobject
*ig
, visitproc visit
, void *arg
)
347 return visit(ig
->item
, arg
);
352 itemgetter_call(itemgetterobject
*ig
, PyObject
*args
, PyObject
*kw
)
354 PyObject
*obj
, *result
;
355 int i
, nitems
=ig
->nitems
;
357 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &obj
))
360 return PyObject_GetItem(obj
, ig
->item
);
362 assert(PyTuple_Check(ig
->item
));
363 assert(PyTuple_GET_SIZE(ig
->item
) == nitems
);
365 result
= PyTuple_New(nitems
);
369 for (i
=0 ; i
< nitems
; i
++) {
370 PyObject
*item
, *val
;
371 item
= PyTuple_GET_ITEM(ig
->item
, i
);
372 val
= PyObject_GetItem(obj
, item
);
377 PyTuple_SET_ITEM(result
, i
, val
);
382 PyDoc_STRVAR(itemgetter_doc
,
383 "itemgetter(item, ...) --> itemgetter object\n\
385 Return a callable object that fetches the given item(s) from its operand.\n\
386 After, f=itemgetter(2), the call f(r) returns r[2].\n\
387 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
389 static PyTypeObject itemgetter_type
= {
390 PyObject_HEAD_INIT(NULL
)
392 "operator.itemgetter", /* tp_name */
393 sizeof(itemgetterobject
), /* tp_basicsize */
396 (destructor
)itemgetter_dealloc
, /* tp_dealloc */
402 0, /* tp_as_number */
403 0, /* tp_as_sequence */
404 0, /* tp_as_mapping */
406 (ternaryfunc
)itemgetter_call
, /* tp_call */
408 PyObject_GenericGetAttr
, /* tp_getattro */
410 0, /* tp_as_buffer */
411 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
412 itemgetter_doc
, /* tp_doc */
413 (traverseproc
)itemgetter_traverse
, /* tp_traverse */
415 0, /* tp_richcompare */
416 0, /* tp_weaklistoffset */
424 0, /* tp_descr_get */
425 0, /* tp_descr_set */
426 0, /* tp_dictoffset */
429 itemgetter_new
, /* tp_new */
434 /* attrgetter object **********************************************************/
442 static PyTypeObject attrgetter_type
;
445 attrgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
447 attrgetterobject
*ag
;
451 if (!_PyArg_NoKeywords("attrgetter()", kwds
))
454 nattrs
= PyTuple_GET_SIZE(args
);
456 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &attr
))
461 /* create attrgetterobject structure */
462 ag
= PyObject_GC_New(attrgetterobject
, &attrgetter_type
);
470 PyObject_GC_Track(ag
);
471 return (PyObject
*)ag
;
475 attrgetter_dealloc(attrgetterobject
*ag
)
477 PyObject_GC_UnTrack(ag
);
478 Py_XDECREF(ag
->attr
);
483 attrgetter_traverse(attrgetterobject
*ag
, visitproc visit
, void *arg
)
486 return visit(ag
->attr
, arg
);
491 attrgetter_call(attrgetterobject
*ag
, PyObject
*args
, PyObject
*kw
)
493 PyObject
*obj
, *result
;
494 int i
, nattrs
=ag
->nattrs
;
496 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &obj
))
499 return PyObject_GetAttr(obj
, ag
->attr
);
501 assert(PyTuple_Check(ag
->attr
));
502 assert(PyTuple_GET_SIZE(ag
->attr
) == nattrs
);
504 result
= PyTuple_New(nattrs
);
508 for (i
=0 ; i
< nattrs
; i
++) {
509 PyObject
*attr
, *val
;
510 attr
= PyTuple_GET_ITEM(ag
->attr
, i
);
511 val
= PyObject_GetAttr(obj
, attr
);
516 PyTuple_SET_ITEM(result
, i
, val
);
521 PyDoc_STRVAR(attrgetter_doc
,
522 "attrgetter(attr, ...) --> attrgetter object\n\
524 Return a callable object that fetches the given attribute(s) from its operand.\n\
525 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
526 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).");
528 static PyTypeObject attrgetter_type
= {
529 PyObject_HEAD_INIT(NULL
)
531 "operator.attrgetter", /* tp_name */
532 sizeof(attrgetterobject
), /* tp_basicsize */
535 (destructor
)attrgetter_dealloc
, /* tp_dealloc */
541 0, /* tp_as_number */
542 0, /* tp_as_sequence */
543 0, /* tp_as_mapping */
545 (ternaryfunc
)attrgetter_call
, /* tp_call */
547 PyObject_GenericGetAttr
, /* tp_getattro */
549 0, /* tp_as_buffer */
550 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
551 attrgetter_doc
, /* tp_doc */
552 (traverseproc
)attrgetter_traverse
, /* tp_traverse */
554 0, /* tp_richcompare */
555 0, /* tp_weaklistoffset */
563 0, /* tp_descr_get */
564 0, /* tp_descr_set */
565 0, /* tp_dictoffset */
568 attrgetter_new
, /* tp_new */
571 /* Initialization function for the module (*must* be called initoperator) */
578 /* Create the module and add the functions */
579 m
= Py_InitModule4("operator", operator_methods
, operator_doc
,
580 (PyObject
*)NULL
, PYTHON_API_VERSION
);
584 if (PyType_Ready(&itemgetter_type
) < 0)
586 Py_INCREF(&itemgetter_type
);
587 PyModule_AddObject(m
, "itemgetter", (PyObject
*)&itemgetter_type
);
589 if (PyType_Ready(&attrgetter_type
) < 0)
591 Py_INCREF(&attrgetter_type
);
592 PyModule_AddObject(m
, "attrgetter", (PyObject
*)&attrgetter_type
);