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 PyLong_FromLong(r); }
51 #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
52 PyObject *a1, *a2; Py_ssize_t r; \
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
54 if(-1 == (r=AOP(a1,a2))) return NULL; \
55 return PyLong_FromSsize_t(r); }
57 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
58 PyObject *a1, *a2; long r; \
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
60 if(-1 == (r=AOP(a1,a2))) return NULL; \
61 return PyBool_FromLong(r); }
63 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
65 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
66 return PyObject_RichCompare(a1,a2,A); }
68 spami(truth
, PyObject_IsTrue
)
69 spam2(op_add
, PyNumber_Add
)
70 spam2(op_sub
, PyNumber_Subtract
)
71 spam2(op_mul
, PyNumber_Multiply
)
72 spam2(op_floordiv
, PyNumber_FloorDivide
)
73 spam2(op_truediv
, PyNumber_TrueDivide
)
74 spam2(op_mod
, PyNumber_Remainder
)
75 spam1(op_neg
, PyNumber_Negative
)
76 spam1(op_pos
, PyNumber_Positive
)
77 spam1(op_abs
, PyNumber_Absolute
)
78 spam1(op_inv
, PyNumber_Invert
)
79 spam1(op_invert
, PyNumber_Invert
)
80 spam2(op_lshift
, PyNumber_Lshift
)
81 spam2(op_rshift
, PyNumber_Rshift
)
82 spami(op_not_
, PyObject_Not
)
83 spam2(op_and_
, PyNumber_And
)
84 spam2(op_xor
, PyNumber_Xor
)
85 spam2(op_or_
, PyNumber_Or
)
86 spam2(op_iadd
, PyNumber_InPlaceAdd
)
87 spam2(op_isub
, PyNumber_InPlaceSubtract
)
88 spam2(op_imul
, PyNumber_InPlaceMultiply
)
89 spam2(op_ifloordiv
, PyNumber_InPlaceFloorDivide
)
90 spam2(op_itruediv
, PyNumber_InPlaceTrueDivide
)
91 spam2(op_imod
, PyNumber_InPlaceRemainder
)
92 spam2(op_ilshift
, PyNumber_InPlaceLshift
)
93 spam2(op_irshift
, PyNumber_InPlaceRshift
)
94 spam2(op_iand
, PyNumber_InPlaceAnd
)
95 spam2(op_ixor
, PyNumber_InPlaceXor
)
96 spam2(op_ior
, PyNumber_InPlaceOr
)
97 spam2(op_concat
, PySequence_Concat
)
98 spam2(op_iconcat
, PySequence_InPlaceConcat
)
99 spami2b(op_contains
, PySequence_Contains
)
100 spamn2(indexOf
, PySequence_Index
)
101 spamn2(countOf
, PySequence_Count
)
102 spam2(op_getitem
, PyObject_GetItem
)
103 spam2n(op_delitem
, PyObject_DelItem
)
104 spam3n(op_setitem
, PyObject_SetItem
)
105 spamrc(op_lt
, Py_LT
)
106 spamrc(op_le
, Py_LE
)
107 spamrc(op_eq
, Py_EQ
)
108 spamrc(op_ne
, Py_NE
)
109 spamrc(op_gt
, Py_GT
)
110 spamrc(op_ge
, Py_GE
)
113 op_pow(PyObject
*s
, PyObject
*a
)
116 if (PyArg_UnpackTuple(a
,"pow", 2, 2, &a1
, &a2
))
117 return PyNumber_Power(a1
, a2
, Py_None
);
122 op_ipow(PyObject
*s
, PyObject
*a
)
125 if (PyArg_UnpackTuple(a
,"ipow", 2, 2, &a1
, &a2
))
126 return PyNumber_InPlacePower(a1
, a2
, Py_None
);
131 op_index(PyObject
*s
, PyObject
*a
)
133 return PyNumber_Index(a
);
137 is_(PyObject
*s
, PyObject
*a
)
139 PyObject
*a1
, *a2
, *result
= NULL
;
140 if (PyArg_UnpackTuple(a
,"is_", 2, 2, &a1
, &a2
)) {
141 result
= (a1
== a2
) ? Py_True
: Py_False
;
148 is_not(PyObject
*s
, PyObject
*a
)
150 PyObject
*a1
, *a2
, *result
= NULL
;
151 if (PyArg_UnpackTuple(a
,"is_not", 2, 2, &a1
, &a2
)) {
152 result
= (a1
!= a2
) ? Py_True
: Py_False
;
162 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
163 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
164 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
165 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
166 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
167 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
169 static struct PyMethodDef operator_methods
[] = {
172 "truth(a) -- Return True if a is true, False otherwise.")
173 spam2(contains
,__contains__
,
174 "contains(a, b) -- Same as b in a (note reversed operands).")
176 "indexOf(a, b) -- Return the first index of b in a.")
178 "countOf(a, b) -- Return the number of times b occurs in a.")
180 spam1(is_
, "is_(a, b) -- Same as a is b.")
181 spam1(is_not
, "is_not(a, b) -- Same as a is not b.")
182 spam2o(index
, __index__
, "index(a) -- Same as a.__index__()")
183 spam2(add
,__add__
, "add(a, b) -- Same as a + b.")
184 spam2(sub
,__sub__
, "sub(a, b) -- Same as a - b.")
185 spam2(mul
,__mul__
, "mul(a, b) -- Same as a * b.")
186 spam2(floordiv
,__floordiv__
, "floordiv(a, b) -- Same as a // b.")
187 spam2(truediv
,__truediv__
, "truediv(a, b) -- Same as a / b.")
188 spam2(mod
,__mod__
, "mod(a, b) -- Same as a % b.")
189 spam2o(neg
,__neg__
, "neg(a) -- Same as -a.")
190 spam2o(pos
,__pos__
, "pos(a) -- Same as +a.")
191 spam2o(abs
,__abs__
, "abs(a) -- Same as abs(a).")
192 spam2o(inv
,__inv__
, "inv(a) -- Same as ~a.")
193 spam2o(invert
,__invert__
, "invert(a) -- Same as ~a.")
194 spam2(lshift
,__lshift__
, "lshift(a, b) -- Same as a << b.")
195 spam2(rshift
,__rshift__
, "rshift(a, b) -- Same as a >> b.")
196 spam2o(not_
,__not__
, "not_(a) -- Same as not a.")
197 spam2(and_
,__and__
, "and_(a, b) -- Same as a & b.")
198 spam2(xor,__xor__
, "xor(a, b) -- Same as a ^ b.")
199 spam2(or_
,__or__
, "or_(a, b) -- Same as a | b.")
200 spam2(iadd
,__iadd__
, "iadd(a, b) -- Same as a += b.")
201 spam2(isub
,__isub__
, "isub(a, b) -- Same as a -= b.")
202 spam2(imul
,__imul__
, "imul(a, b) -- Same as a *= b.")
203 spam2(ifloordiv
,__ifloordiv__
, "ifloordiv(a, b) -- Same as a //= b.")
204 spam2(itruediv
,__itruediv__
, "itruediv(a, b) -- Same as a /= b.")
205 spam2(imod
,__imod__
, "imod(a, b) -- Same as a %= b.")
206 spam2(ilshift
,__ilshift__
, "ilshift(a, b) -- Same as a <<= b.")
207 spam2(irshift
,__irshift__
, "irshift(a, b) -- Same as a >>= b.")
208 spam2(iand
,__iand__
, "iand(a, b) -- Same as a &= b.")
209 spam2(ixor
,__ixor__
, "ixor(a, b) -- Same as a ^= b.")
210 spam2(ior
,__ior__
, "ior(a, b) -- Same as a |= b.")
211 spam2(concat
,__concat__
,
212 "concat(a, b) -- Same as a + b, for a and b sequences.")
213 spam2(iconcat
,__iconcat__
,
214 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
215 spam2(getitem
,__getitem__
,
216 "getitem(a, b) -- Same as a[b].")
217 spam2(setitem
,__setitem__
,
218 "setitem(a, b, c) -- Same as a[b] = c.")
219 spam2(delitem
,__delitem__
,
220 "delitem(a, b) -- Same as del a[b].")
221 spam2(pow
,__pow__
, "pow(a, b) -- Same as a ** b.")
222 spam2(ipow
,__ipow__
, "ipow(a, b) -- Same as a **= b.")
223 spam2(lt
,__lt__
, "lt(a, b) -- Same as a<b.")
224 spam2(le
,__le__
, "le(a, b) -- Same as a<=b.")
225 spam2(eq
,__eq__
, "eq(a, b) -- Same as a==b.")
226 spam2(ne
,__ne__
, "ne(a, b) -- Same as a!=b.")
227 spam2(gt
,__gt__
, "gt(a, b) -- Same as a>b.")
228 spam2(ge
,__ge__
, "ge(a, b) -- Same as a>=b.")
230 {NULL
, NULL
} /* sentinel */
234 /* itemgetter object **********************************************************/
242 static PyTypeObject itemgetter_type
;
245 itemgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
247 itemgetterobject
*ig
;
251 if (!_PyArg_NoKeywords("itemgetter()", kwds
))
254 nitems
= PyTuple_GET_SIZE(args
);
256 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &item
))
261 /* create itemgetterobject structure */
262 ig
= PyObject_GC_New(itemgetterobject
, &itemgetter_type
);
270 PyObject_GC_Track(ig
);
271 return (PyObject
*)ig
;
275 itemgetter_dealloc(itemgetterobject
*ig
)
277 PyObject_GC_UnTrack(ig
);
278 Py_XDECREF(ig
->item
);
283 itemgetter_traverse(itemgetterobject
*ig
, visitproc visit
, void *arg
)
290 itemgetter_call(itemgetterobject
*ig
, PyObject
*args
, PyObject
*kw
)
292 PyObject
*obj
, *result
;
293 Py_ssize_t i
, nitems
=ig
->nitems
;
295 if (!PyArg_UnpackTuple(args
, "itemgetter", 1, 1, &obj
))
298 return PyObject_GetItem(obj
, ig
->item
);
300 assert(PyTuple_Check(ig
->item
));
301 assert(PyTuple_GET_SIZE(ig
->item
) == nitems
);
303 result
= PyTuple_New(nitems
);
307 for (i
=0 ; i
< nitems
; i
++) {
308 PyObject
*item
, *val
;
309 item
= PyTuple_GET_ITEM(ig
->item
, i
);
310 val
= PyObject_GetItem(obj
, item
);
315 PyTuple_SET_ITEM(result
, i
, val
);
320 PyDoc_STRVAR(itemgetter_doc
,
321 "itemgetter(item, ...) --> itemgetter object\n\
323 Return a callable object that fetches the given item(s) from its operand.\n\
324 After, f=itemgetter(2), the call f(r) returns r[2].\n\
325 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
327 static PyTypeObject itemgetter_type
= {
328 PyVarObject_HEAD_INIT(NULL
, 0)
329 "operator.itemgetter", /* tp_name */
330 sizeof(itemgetterobject
), /* tp_basicsize */
333 (destructor
)itemgetter_dealloc
, /* tp_dealloc */
339 0, /* tp_as_number */
340 0, /* tp_as_sequence */
341 0, /* tp_as_mapping */
343 (ternaryfunc
)itemgetter_call
, /* tp_call */
345 PyObject_GenericGetAttr
, /* tp_getattro */
347 0, /* tp_as_buffer */
348 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
349 itemgetter_doc
, /* tp_doc */
350 (traverseproc
)itemgetter_traverse
, /* tp_traverse */
352 0, /* tp_richcompare */
353 0, /* tp_weaklistoffset */
361 0, /* tp_descr_get */
362 0, /* tp_descr_set */
363 0, /* tp_dictoffset */
366 itemgetter_new
, /* tp_new */
371 /* attrgetter object **********************************************************/
379 static PyTypeObject attrgetter_type
;
382 attrgetter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
384 attrgetterobject
*ag
;
388 if (!_PyArg_NoKeywords("attrgetter()", kwds
))
391 nattrs
= PyTuple_GET_SIZE(args
);
393 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &attr
))
398 /* create attrgetterobject structure */
399 ag
= PyObject_GC_New(attrgetterobject
, &attrgetter_type
);
407 PyObject_GC_Track(ag
);
408 return (PyObject
*)ag
;
412 attrgetter_dealloc(attrgetterobject
*ag
)
414 PyObject_GC_UnTrack(ag
);
415 Py_XDECREF(ag
->attr
);
420 attrgetter_traverse(attrgetterobject
*ag
, visitproc visit
, void *arg
)
427 dotted_getattr(PyObject
*obj
, PyObject
*attr
)
431 if (!PyUnicode_Check(attr
)) {
432 PyErr_SetString(PyExc_TypeError
,
433 "attribute name must be a string");
437 s
= _PyUnicode_AsString(attr
);
440 PyObject
*newobj
, *str
;
442 str
= p
? PyUnicode_FromStringAndSize(s
, (p
-s
)) :
443 PyUnicode_FromString(s
);
448 newobj
= PyObject_GetAttr(obj
, str
);
454 if (p
== NULL
) break;
462 attrgetter_call(attrgetterobject
*ag
, PyObject
*args
, PyObject
*kw
)
464 PyObject
*obj
, *result
;
465 Py_ssize_t i
, nattrs
=ag
->nattrs
;
467 if (!PyArg_UnpackTuple(args
, "attrgetter", 1, 1, &obj
))
470 return dotted_getattr(obj
, ag
->attr
);
472 assert(PyTuple_Check(ag
->attr
));
473 assert(PyTuple_GET_SIZE(ag
->attr
) == nattrs
);
475 result
= PyTuple_New(nattrs
);
479 for (i
=0 ; i
< nattrs
; i
++) {
480 PyObject
*attr
, *val
;
481 attr
= PyTuple_GET_ITEM(ag
->attr
, i
);
482 val
= dotted_getattr(obj
, attr
);
487 PyTuple_SET_ITEM(result
, i
, val
);
492 PyDoc_STRVAR(attrgetter_doc
,
493 "attrgetter(attr, ...) --> attrgetter object\n\
495 Return a callable object that fetches the given attribute(s) from its operand.\n\
496 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
497 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
498 After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
499 (r.name.first, r.name.last).");
501 static PyTypeObject attrgetter_type
= {
502 PyVarObject_HEAD_INIT(NULL
, 0)
503 "operator.attrgetter", /* tp_name */
504 sizeof(attrgetterobject
), /* tp_basicsize */
507 (destructor
)attrgetter_dealloc
, /* tp_dealloc */
513 0, /* tp_as_number */
514 0, /* tp_as_sequence */
515 0, /* tp_as_mapping */
517 (ternaryfunc
)attrgetter_call
, /* tp_call */
519 PyObject_GenericGetAttr
, /* tp_getattro */
521 0, /* tp_as_buffer */
522 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
523 attrgetter_doc
, /* tp_doc */
524 (traverseproc
)attrgetter_traverse
, /* tp_traverse */
526 0, /* tp_richcompare */
527 0, /* tp_weaklistoffset */
535 0, /* tp_descr_get */
536 0, /* tp_descr_set */
537 0, /* tp_dictoffset */
540 attrgetter_new
, /* tp_new */
545 /* methodcaller object **********************************************************/
552 } methodcallerobject
;
554 static PyTypeObject methodcaller_type
;
557 methodcaller_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
559 methodcallerobject
*mc
;
560 PyObject
*name
, *newargs
;
562 if (PyTuple_GET_SIZE(args
) < 1) {
563 PyErr_SetString(PyExc_TypeError
, "methodcaller needs at least "
564 "one argument, the method name");
568 /* create methodcallerobject structure */
569 mc
= PyObject_GC_New(methodcallerobject
, &methodcaller_type
);
573 newargs
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
574 if (newargs
== NULL
) {
580 name
= PyTuple_GET_ITEM(args
, 0);
587 PyObject_GC_Track(mc
);
588 return (PyObject
*)mc
;
592 methodcaller_dealloc(methodcallerobject
*mc
)
594 PyObject_GC_UnTrack(mc
);
595 Py_XDECREF(mc
->name
);
596 Py_XDECREF(mc
->args
);
597 Py_XDECREF(mc
->kwds
);
602 methodcaller_traverse(methodcallerobject
*mc
, visitproc visit
, void *arg
)
610 methodcaller_call(methodcallerobject
*mc
, PyObject
*args
, PyObject
*kw
)
612 PyObject
*method
, *obj
, *result
;
614 if (!PyArg_UnpackTuple(args
, "methodcaller", 1, 1, &obj
))
616 method
= PyObject_GetAttr(obj
, mc
->name
);
619 result
= PyObject_Call(method
, mc
->args
, mc
->kwds
);
624 PyDoc_STRVAR(methodcaller_doc
,
625 "methodcaller(name, ...) --> methodcaller object\n\
627 Return a callable object that calls the given method on its operand.\n\
628 After, f = methodcaller('name'), the call f(r) returns r.name().\n\
629 After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
630 r.name('date', foo=1).");
632 static PyTypeObject methodcaller_type
= {
633 PyVarObject_HEAD_INIT(NULL
, 0)
634 "operator.methodcaller", /* tp_name */
635 sizeof(methodcallerobject
), /* tp_basicsize */
638 (destructor
)methodcaller_dealloc
, /* tp_dealloc */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
648 (ternaryfunc
)methodcaller_call
, /* tp_call */
650 PyObject_GenericGetAttr
, /* tp_getattro */
652 0, /* tp_as_buffer */
653 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
654 methodcaller_doc
, /* tp_doc */
655 (traverseproc
)methodcaller_traverse
, /* tp_traverse */
657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
668 0, /* tp_dictoffset */
671 methodcaller_new
, /* tp_new */
676 /* Initialization function for the module (*must* be called PyInit_operator) */
679 static struct PyModuleDef operatormodule
= {
680 PyModuleDef_HEAD_INIT
,
692 PyInit_operator(void)
696 /* Create the module and add the functions */
697 m
= PyModule_Create(&operatormodule
);
701 if (PyType_Ready(&itemgetter_type
) < 0)
703 Py_INCREF(&itemgetter_type
);
704 PyModule_AddObject(m
, "itemgetter", (PyObject
*)&itemgetter_type
);
706 if (PyType_Ready(&attrgetter_type
) < 0)
708 Py_INCREF(&attrgetter_type
);
709 PyModule_AddObject(m
, "attrgetter", (PyObject
*)&attrgetter_type
);
711 if (PyType_Ready(&methodcaller_type
) < 0)
713 Py_INCREF(&methodcaller_type
);
714 PyModule_AddObject(m
, "methodcaller", (PyObject
*)&methodcaller_type
);