Merged revisions 75248 via svnmerge from
[python/dscho.git] / Modules / operator.c
blob0ba8b09941f18c07b69cd52835dd5be9bb2a703e
2 #include "Python.h"
4 PyDoc_STRVAR(operator_doc,
5 "Operator interface.\n\
6 \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) { \
14 return AOP(a1); }
16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
17 PyObject *a1, *a2; \
18 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
19 return AOP(a1,a2); }
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; \
24 return AOP(a1,a2); }
26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
27 PyObject *a1, *a2; \
28 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
29 if(-1 == AOP(a1,a2)) return NULL; \
30 Py_INCREF(Py_None); \
31 return Py_None; }
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; \
37 Py_INCREF(Py_None); \
38 return Py_None; }
40 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
41 long r; \
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) { \
64 PyObject *a1, *a2; \
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)
112 static PyObject*
113 op_pow(PyObject *s, PyObject *a)
115 PyObject *a1, *a2;
116 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
117 return PyNumber_Power(a1, a2, Py_None);
118 return NULL;
121 static PyObject*
122 op_ipow(PyObject *s, PyObject *a)
124 PyObject *a1, *a2;
125 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
126 return PyNumber_InPlacePower(a1, a2, Py_None);
127 return NULL;
130 static PyObject *
131 op_index(PyObject *s, PyObject *a)
133 return PyNumber_Index(a);
136 static PyObject*
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;
142 Py_INCREF(result);
144 return result;
147 static PyObject*
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;
153 Py_INCREF(result);
155 return result;
158 #undef spam1
159 #undef spam2
160 #undef spam1o
161 #undef spam1o
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[] = {
171 spam1o(truth,
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).")
175 spam1(indexOf,
176 "indexOf(a, b) -- Return the first index of b in a.")
177 spam1(countOf,
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 **********************************************************/
236 typedef struct {
237 PyObject_HEAD
238 Py_ssize_t nitems;
239 PyObject *item;
240 } itemgetterobject;
242 static PyTypeObject itemgetter_type;
244 static PyObject *
245 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
247 itemgetterobject *ig;
248 PyObject *item;
249 Py_ssize_t nitems;
251 if (!_PyArg_NoKeywords("itemgetter()", kwds))
252 return NULL;
254 nitems = PyTuple_GET_SIZE(args);
255 if (nitems <= 1) {
256 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
257 return NULL;
258 } else
259 item = args;
261 /* create itemgetterobject structure */
262 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
263 if (ig == NULL)
264 return NULL;
266 Py_INCREF(item);
267 ig->item = item;
268 ig->nitems = nitems;
270 PyObject_GC_Track(ig);
271 return (PyObject *)ig;
274 static void
275 itemgetter_dealloc(itemgetterobject *ig)
277 PyObject_GC_UnTrack(ig);
278 Py_XDECREF(ig->item);
279 PyObject_GC_Del(ig);
282 static int
283 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
285 Py_VISIT(ig->item);
286 return 0;
289 static PyObject *
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))
296 return NULL;
297 if (nitems == 1)
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);
304 if (result == NULL)
305 return NULL;
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);
311 if (val == NULL) {
312 Py_DECREF(result);
313 return NULL;
315 PyTuple_SET_ITEM(result, i, val);
317 return result;
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 */
331 0, /* tp_itemsize */
332 /* methods */
333 (destructor)itemgetter_dealloc, /* tp_dealloc */
334 0, /* tp_print */
335 0, /* tp_getattr */
336 0, /* tp_setattr */
337 0, /* tp_reserved */
338 0, /* tp_repr */
339 0, /* tp_as_number */
340 0, /* tp_as_sequence */
341 0, /* tp_as_mapping */
342 0, /* tp_hash */
343 (ternaryfunc)itemgetter_call, /* tp_call */
344 0, /* tp_str */
345 PyObject_GenericGetAttr, /* tp_getattro */
346 0, /* tp_setattro */
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 */
351 0, /* tp_clear */
352 0, /* tp_richcompare */
353 0, /* tp_weaklistoffset */
354 0, /* tp_iter */
355 0, /* tp_iternext */
356 0, /* tp_methods */
357 0, /* tp_members */
358 0, /* tp_getset */
359 0, /* tp_base */
360 0, /* tp_dict */
361 0, /* tp_descr_get */
362 0, /* tp_descr_set */
363 0, /* tp_dictoffset */
364 0, /* tp_init */
365 0, /* tp_alloc */
366 itemgetter_new, /* tp_new */
367 0, /* tp_free */
371 /* attrgetter object **********************************************************/
373 typedef struct {
374 PyObject_HEAD
375 Py_ssize_t nattrs;
376 PyObject *attr;
377 } attrgetterobject;
379 static PyTypeObject attrgetter_type;
381 static PyObject *
382 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
384 attrgetterobject *ag;
385 PyObject *attr;
386 Py_ssize_t nattrs;
388 if (!_PyArg_NoKeywords("attrgetter()", kwds))
389 return NULL;
391 nattrs = PyTuple_GET_SIZE(args);
392 if (nattrs <= 1) {
393 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
394 return NULL;
395 } else
396 attr = args;
398 /* create attrgetterobject structure */
399 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
400 if (ag == NULL)
401 return NULL;
403 Py_INCREF(attr);
404 ag->attr = attr;
405 ag->nattrs = nattrs;
407 PyObject_GC_Track(ag);
408 return (PyObject *)ag;
411 static void
412 attrgetter_dealloc(attrgetterobject *ag)
414 PyObject_GC_UnTrack(ag);
415 Py_XDECREF(ag->attr);
416 PyObject_GC_Del(ag);
419 static int
420 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
422 Py_VISIT(ag->attr);
423 return 0;
426 static PyObject *
427 dotted_getattr(PyObject *obj, PyObject *attr)
429 char *s, *p;
431 if (!PyUnicode_Check(attr)) {
432 PyErr_SetString(PyExc_TypeError,
433 "attribute name must be a string");
434 return NULL;
437 s = _PyUnicode_AsString(attr);
438 Py_INCREF(obj);
439 for (;;) {
440 PyObject *newobj, *str;
441 p = strchr(s, '.');
442 str = p ? PyUnicode_FromStringAndSize(s, (p-s)) :
443 PyUnicode_FromString(s);
444 if (str == NULL) {
445 Py_DECREF(obj);
446 return NULL;
448 newobj = PyObject_GetAttr(obj, str);
449 Py_DECREF(str);
450 Py_DECREF(obj);
451 if (newobj == NULL)
452 return NULL;
453 obj = newobj;
454 if (p == NULL) break;
455 s = p+1;
458 return obj;
461 static PyObject *
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))
468 return NULL;
469 if (ag->nattrs == 1)
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);
476 if (result == NULL)
477 return NULL;
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);
483 if (val == NULL) {
484 Py_DECREF(result);
485 return NULL;
487 PyTuple_SET_ITEM(result, i, val);
489 return result;
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 */
505 0, /* tp_itemsize */
506 /* methods */
507 (destructor)attrgetter_dealloc, /* tp_dealloc */
508 0, /* tp_print */
509 0, /* tp_getattr */
510 0, /* tp_setattr */
511 0, /* tp_reserved */
512 0, /* tp_repr */
513 0, /* tp_as_number */
514 0, /* tp_as_sequence */
515 0, /* tp_as_mapping */
516 0, /* tp_hash */
517 (ternaryfunc)attrgetter_call, /* tp_call */
518 0, /* tp_str */
519 PyObject_GenericGetAttr, /* tp_getattro */
520 0, /* tp_setattro */
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 */
525 0, /* tp_clear */
526 0, /* tp_richcompare */
527 0, /* tp_weaklistoffset */
528 0, /* tp_iter */
529 0, /* tp_iternext */
530 0, /* tp_methods */
531 0, /* tp_members */
532 0, /* tp_getset */
533 0, /* tp_base */
534 0, /* tp_dict */
535 0, /* tp_descr_get */
536 0, /* tp_descr_set */
537 0, /* tp_dictoffset */
538 0, /* tp_init */
539 0, /* tp_alloc */
540 attrgetter_new, /* tp_new */
541 0, /* tp_free */
545 /* methodcaller object **********************************************************/
547 typedef struct {
548 PyObject_HEAD
549 PyObject *name;
550 PyObject *args;
551 PyObject *kwds;
552 } methodcallerobject;
554 static PyTypeObject methodcaller_type;
556 static PyObject *
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");
565 return NULL;
568 /* create methodcallerobject structure */
569 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
570 if (mc == NULL)
571 return NULL;
573 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
574 if (newargs == NULL) {
575 Py_DECREF(mc);
576 return NULL;
578 mc->args = newargs;
580 name = PyTuple_GET_ITEM(args, 0);
581 Py_INCREF(name);
582 mc->name = name;
584 Py_XINCREF(kwds);
585 mc->kwds = kwds;
587 PyObject_GC_Track(mc);
588 return (PyObject *)mc;
591 static void
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);
598 PyObject_GC_Del(mc);
601 static int
602 methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
604 Py_VISIT(mc->args);
605 Py_VISIT(mc->kwds);
606 return 0;
609 static PyObject *
610 methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
612 PyObject *method, *obj, *result;
614 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
615 return NULL;
616 method = PyObject_GetAttr(obj, mc->name);
617 if (method == NULL)
618 return NULL;
619 result = PyObject_Call(method, mc->args, mc->kwds);
620 Py_DECREF(method);
621 return result;
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 */
636 0, /* tp_itemsize */
637 /* methods */
638 (destructor)methodcaller_dealloc, /* tp_dealloc */
639 0, /* tp_print */
640 0, /* tp_getattr */
641 0, /* tp_setattr */
642 0, /* tp_reserved */
643 0, /* tp_repr */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
647 0, /* tp_hash */
648 (ternaryfunc)methodcaller_call, /* tp_call */
649 0, /* tp_str */
650 PyObject_GenericGetAttr, /* tp_getattro */
651 0, /* tp_setattro */
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 */
656 0, /* tp_clear */
657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
659 0, /* tp_iter */
660 0, /* tp_iternext */
661 0, /* tp_methods */
662 0, /* tp_members */
663 0, /* tp_getset */
664 0, /* tp_base */
665 0, /* tp_dict */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
668 0, /* tp_dictoffset */
669 0, /* tp_init */
670 0, /* tp_alloc */
671 methodcaller_new, /* tp_new */
672 0, /* tp_free */
676 /* Initialization function for the module (*must* be called PyInit_operator) */
679 static struct PyModuleDef operatormodule = {
680 PyModuleDef_HEAD_INIT,
681 "operator",
682 operator_doc,
684 operator_methods,
685 NULL,
686 NULL,
687 NULL,
688 NULL
691 PyMODINIT_FUNC
692 PyInit_operator(void)
694 PyObject *m;
696 /* Create the module and add the functions */
697 m = PyModule_Create(&operatormodule);
698 if (m == NULL)
699 return NULL;
701 if (PyType_Ready(&itemgetter_type) < 0)
702 return NULL;
703 Py_INCREF(&itemgetter_type);
704 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
706 if (PyType_Ready(&attrgetter_type) < 0)
707 return NULL;
708 Py_INCREF(&attrgetter_type);
709 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
711 if (PyType_Ready(&methodcaller_type) < 0)
712 return NULL;
713 Py_INCREF(&methodcaller_type);
714 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
715 return m;