No issues found
1 #include "Python.h"
2
3 PyDoc_STRVAR(operator_doc,
4 "Operator interface.\n\
5 \n\
6 This module exports a set of functions implemented in C corresponding\n\
7 to the intrinsic operators of Python. For example, operator.add(x, y)\n\
8 is equivalent to the expression x+y. The function names are those\n\
9 used for special methods; variants without leading and trailing\n\
10 '__' are also provided for convenience.");
11
12 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
13 return AOP(a1); }
14
15 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
16 PyObject *a1, *a2; \
17 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
18 return AOP(a1,a2); }
19
20 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
21 PyObject *a1; int a2; \
22 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
23 return AOP(a1,a2); }
24
25 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
26 PyObject *a1, *a2; \
27 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
28 if(-1 == AOP(a1,a2)) return NULL; \
29 Py_INCREF(Py_None); \
30 return Py_None; }
31
32 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
33 PyObject *a1, *a2, *a3; \
34 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
35 if(-1 == AOP(a1,a2,a3)) return NULL; \
36 Py_INCREF(Py_None); \
37 return Py_None; }
38
39 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
40 long r; \
41 if(-1 == (r=AOP(a1))) return NULL; \
42 return PyBool_FromLong(r); }
43
44 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
45 PyObject *a1, *a2; long r; \
46 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
47 if(-1 == (r=AOP(a1,a2))) return NULL; \
48 return PyInt_FromLong(r); }
49
50 #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
51 PyObject *a1, *a2; Py_ssize_t r; \
52 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
53 if(-1 == (r=AOP(a1,a2))) return NULL; \
54 return PyInt_FromSsize_t(r); }
55
56 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
57 PyObject *a1, *a2; long r; \
58 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
59 if(-1 == (r=AOP(a1,a2))) return NULL; \
60 return PyBool_FromLong(r); }
61
62 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
63 PyObject *a1, *a2; \
64 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
65 return PyObject_RichCompare(a1,a2,A); }
66
67 /* Deprecated operators that need warnings. */
68 static int
69 op_isCallable(PyObject *x)
70 {
71 if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "
72 "Use hasattr(obj, '__call__').", 1) < 0)
73 return -1;
74 return PyCallable_Check(x);
75 }
76
77 static int
78 op_sequenceIncludes(PyObject *seq, PyObject* ob)
79 {
80 if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "
81 "in 3.x. Use operator.contains().", 1) < 0)
82 return -1;
83 return PySequence_Contains(seq, ob);
84 }
85
86 spami(isCallable , op_isCallable)
87 spami(isNumberType , PyNumber_Check)
88 spami(truth , PyObject_IsTrue)
89 spam2(op_add , PyNumber_Add)
90 spam2(op_sub , PyNumber_Subtract)
91 spam2(op_mul , PyNumber_Multiply)
92 spam2(op_div , PyNumber_Divide)
93 spam2(op_floordiv , PyNumber_FloorDivide)
94 spam2(op_truediv , PyNumber_TrueDivide)
95 spam2(op_mod , PyNumber_Remainder)
96 spam1(op_neg , PyNumber_Negative)
97 spam1(op_pos , PyNumber_Positive)
98 spam1(op_abs , PyNumber_Absolute)
99 spam1(op_inv , PyNumber_Invert)
100 spam1(op_invert , PyNumber_Invert)
101 spam2(op_lshift , PyNumber_Lshift)
102 spam2(op_rshift , PyNumber_Rshift)
103 spami(op_not_ , PyObject_Not)
104 spam2(op_and_ , PyNumber_And)
105 spam2(op_xor , PyNumber_Xor)
106 spam2(op_or_ , PyNumber_Or)
107 spam2(op_iadd , PyNumber_InPlaceAdd)
108 spam2(op_isub , PyNumber_InPlaceSubtract)
109 spam2(op_imul , PyNumber_InPlaceMultiply)
110 spam2(op_idiv , PyNumber_InPlaceDivide)
111 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
112 spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
113 spam2(op_imod , PyNumber_InPlaceRemainder)
114 spam2(op_ilshift , PyNumber_InPlaceLshift)
115 spam2(op_irshift , PyNumber_InPlaceRshift)
116 spam2(op_iand , PyNumber_InPlaceAnd)
117 spam2(op_ixor , PyNumber_InPlaceXor)
118 spam2(op_ior , PyNumber_InPlaceOr)
119 spami(isSequenceType , PySequence_Check)
120 spam2(op_concat , PySequence_Concat)
121 spamoi(op_repeat , PySequence_Repeat)
122 spam2(op_iconcat , PySequence_InPlaceConcat)
123 spamoi(op_irepeat , PySequence_InPlaceRepeat)
124 spami2b(op_contains , PySequence_Contains)
125 spami2b(sequenceIncludes, op_sequenceIncludes)
126 spamn2(indexOf , PySequence_Index)
127 spamn2(countOf , PySequence_Count)
128 spami(isMappingType , PyMapping_Check)
129 spam2(op_getitem , PyObject_GetItem)
130 spam2n(op_delitem , PyObject_DelItem)
131 spam3n(op_setitem , PyObject_SetItem)
132 spamrc(op_lt , Py_LT)
133 spamrc(op_le , Py_LE)
134 spamrc(op_eq , Py_EQ)
135 spamrc(op_ne , Py_NE)
136 spamrc(op_gt , Py_GT)
137 spamrc(op_ge , Py_GE)
138
139 static PyObject*
140 op_pow(PyObject *s, PyObject *a)
141 {
142 PyObject *a1, *a2;
143 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
144 return PyNumber_Power(a1, a2, Py_None);
145 return NULL;
146 }
147
148 static PyObject*
149 op_ipow(PyObject *s, PyObject *a)
150 {
151 PyObject *a1, *a2;
152 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
153 return PyNumber_InPlacePower(a1, a2, Py_None);
154 return NULL;
155 }
156
157 static PyObject *
158 op_index(PyObject *s, PyObject *a)
159 {
160 return PyNumber_Index(a);
161 }
162
163 static PyObject*
164 is_(PyObject *s, PyObject *a)
165 {
166 PyObject *a1, *a2, *result = NULL;
167 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
168 result = (a1 == a2) ? Py_True : Py_False;
169 Py_INCREF(result);
170 }
171 return result;
172 }
173
174 static PyObject*
175 is_not(PyObject *s, PyObject *a)
176 {
177 PyObject *a1, *a2, *result = NULL;
178 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
179 result = (a1 != a2) ? Py_True : Py_False;
180 Py_INCREF(result);
181 }
182 return result;
183 }
184
185 static PyObject*
186 op_getslice(PyObject *s, PyObject *a)
187 {
188 PyObject *a1;
189 Py_ssize_t a2, a3;
190
191 if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
192 return NULL;
193 return PySequence_GetSlice(a1, a2, a3);
194 }
195
196 static PyObject*
197 op_setslice(PyObject *s, PyObject *a)
198 {
199 PyObject *a1, *a4;
200 Py_ssize_t a2, a3;
201
202 if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
203 return NULL;
204
205 if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
206 return NULL;
207
208 Py_RETURN_NONE;
209 }
210
211 static PyObject*
212 op_delslice(PyObject *s, PyObject *a)
213 {
214 PyObject *a1;
215 Py_ssize_t a2, a3;
216
217 if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
218 return NULL;
219
220 if (-1 == PySequence_DelSlice(a1, a2, a3))
221 return NULL;
222
223 Py_RETURN_NONE;
224 }
225
226 #undef spam1
227 #undef spam2
228 #undef spam1o
229 #undef spam1o
230 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
231 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
232 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
233 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
234 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
235 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
236
237 static struct PyMethodDef operator_methods[] = {
238
239 spam1o(isCallable,
240 "isCallable(a) -- Same as callable(a).")
241 spam1o(isNumberType,
242 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
243 spam1o(isSequenceType,
244 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
245 spam1o(truth,
246 "truth(a) -- Return True if a is true, False otherwise.")
247 spam2(contains,__contains__,
248 "contains(a, b) -- Same as b in a (note reversed operands).")
249 spam1(sequenceIncludes,
250 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
251 spam1(indexOf,
252 "indexOf(a, b) -- Return the first index of b in a.")
253 spam1(countOf,
254 "countOf(a, b) -- Return the number of times b occurs in a.")
255 spam1o(isMappingType,
256 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
257
258 spam1(is_, "is_(a, b) -- Same as a is b.")
259 spam1(is_not, "is_not(a, b) -- Same as a is not b.")
260 spam2o(index, __index__, "index(a) -- Same as a.__index__()")
261 spam2(add,__add__, "add(a, b) -- Same as a + b.")
262 spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
263 spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
264 spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
265 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
266 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
267 spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
268 spam2o(neg,__neg__, "neg(a) -- Same as -a.")
269 spam2o(pos,__pos__, "pos(a) -- Same as +a.")
270 spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
271 spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
272 spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
273 spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
274 spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
275 spam2o(not_,__not__, "not_(a) -- Same as not a.")
276 spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
277 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
278 spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
279 spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
280 spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
281 spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
282 spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
283 spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
284 spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
285 spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
286 spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
287 spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
288 spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
289 spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
290 spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
291 spam2(concat,__concat__,
292 "concat(a, b) -- Same as a + b, for a and b sequences.")
293 spam2(repeat,__repeat__,
294 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
295 spam2(iconcat,__iconcat__,
296 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
297 spam2(irepeat,__irepeat__,
298 "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
299 spam2(getitem,__getitem__,
300 "getitem(a, b) -- Same as a[b].")
301 spam2(setitem,__setitem__,
302 "setitem(a, b, c) -- Same as a[b] = c.")
303 spam2(delitem,__delitem__,
304 "delitem(a, b) -- Same as del a[b].")
305 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
306 spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
307 spam2(getslice,__getslice__,
308 "getslice(a, b, c) -- Same as a[b:c].")
309 spam2(setslice,__setslice__,
310 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
311 spam2(delslice,__delslice__,
312 "delslice(a, b, c) -- Same as del a[b:c].")
313 spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
314 spam2(le,__le__, "le(a, b) -- Same as a<=b.")
315 spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
316 spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
317 spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
318 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
319
320 {NULL, NULL} /* sentinel */
321
322 };
323
324 /* itemgetter object **********************************************************/
325
326 typedef struct {
327 PyObject_HEAD
328 Py_ssize_t nitems;
329 PyObject *item;
330 } itemgetterobject;
331
332 static PyTypeObject itemgetter_type;
333
334 static PyObject *
335 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
336 {
337 itemgetterobject *ig;
338 PyObject *item;
339 Py_ssize_t nitems;
340
341 if (!_PyArg_NoKeywords("itemgetter()", kwds))
342 return NULL;
343
344 nitems = PyTuple_GET_SIZE(args);
345 if (nitems <= 1) {
346 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
347 return NULL;
348 } else
349 item = args;
350
351 /* create itemgetterobject structure */
352 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
353 if (ig == NULL)
354 return NULL;
355
356 Py_INCREF(item);
357 ig->item = item;
358 ig->nitems = nitems;
359
360 PyObject_GC_Track(ig);
361 return (PyObject *)ig;
362 }
363
364 static void
365 itemgetter_dealloc(itemgetterobject *ig)
366 {
367 PyObject_GC_UnTrack(ig);
368 Py_XDECREF(ig->item);
369 PyObject_GC_Del(ig);
370 }
371
372 static int
373 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
374 {
375 Py_VISIT(ig->item);
376 return 0;
377 }
378
379 static PyObject *
380 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
381 {
382 PyObject *obj, *result;
383 Py_ssize_t i, nitems=ig->nitems;
384
385 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
386 return NULL;
387 if (nitems == 1)
388 return PyObject_GetItem(obj, ig->item);
389
390 assert(PyTuple_Check(ig->item));
391 assert(PyTuple_GET_SIZE(ig->item) == nitems);
392
393 result = PyTuple_New(nitems);
394 if (result == NULL)
395 return NULL;
396
397 for (i=0 ; i < nitems ; i++) {
398 PyObject *item, *val;
399 item = PyTuple_GET_ITEM(ig->item, i);
400 val = PyObject_GetItem(obj, item);
401 if (val == NULL) {
402 Py_DECREF(result);
403 return NULL;
404 }
405 PyTuple_SET_ITEM(result, i, val);
406 }
407 return result;
408 }
409
410 PyDoc_STRVAR(itemgetter_doc,
411 "itemgetter(item, ...) --> itemgetter object\n\
412 \n\
413 Return a callable object that fetches the given item(s) from its operand.\n\
414 After, f=itemgetter(2), the call f(r) returns r[2].\n\
415 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
416
417 static PyTypeObject itemgetter_type = {
418 PyVarObject_HEAD_INIT(NULL, 0)
419 "operator.itemgetter", /* tp_name */
420 sizeof(itemgetterobject), /* tp_basicsize */
421 0, /* tp_itemsize */
422 /* methods */
423 (destructor)itemgetter_dealloc, /* tp_dealloc */
424 0, /* tp_print */
425 0, /* tp_getattr */
426 0, /* tp_setattr */
427 0, /* tp_compare */
428 0, /* tp_repr */
429 0, /* tp_as_number */
430 0, /* tp_as_sequence */
431 0, /* tp_as_mapping */
432 0, /* tp_hash */
433 (ternaryfunc)itemgetter_call, /* tp_call */
434 0, /* tp_str */
435 PyObject_GenericGetAttr, /* tp_getattro */
436 0, /* tp_setattro */
437 0, /* tp_as_buffer */
438 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
439 itemgetter_doc, /* tp_doc */
440 (traverseproc)itemgetter_traverse, /* tp_traverse */
441 0, /* tp_clear */
442 0, /* tp_richcompare */
443 0, /* tp_weaklistoffset */
444 0, /* tp_iter */
445 0, /* tp_iternext */
446 0, /* tp_methods */
447 0, /* tp_members */
448 0, /* tp_getset */
449 0, /* tp_base */
450 0, /* tp_dict */
451 0, /* tp_descr_get */
452 0, /* tp_descr_set */
453 0, /* tp_dictoffset */
454 0, /* tp_init */
455 0, /* tp_alloc */
456 itemgetter_new, /* tp_new */
457 0, /* tp_free */
458 };
459
460
461 /* attrgetter object **********************************************************/
462
463 typedef struct {
464 PyObject_HEAD
465 Py_ssize_t nattrs;
466 PyObject *attr;
467 } attrgetterobject;
468
469 static PyTypeObject attrgetter_type;
470
471 static PyObject *
472 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
473 {
474 attrgetterobject *ag;
475 PyObject *attr;
476 Py_ssize_t nattrs;
477
478 if (!_PyArg_NoKeywords("attrgetter()", kwds))
479 return NULL;
480
481 nattrs = PyTuple_GET_SIZE(args);
482 if (nattrs <= 1) {
483 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
484 return NULL;
485 } else
486 attr = args;
487
488 /* create attrgetterobject structure */
489 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
490 if (ag == NULL)
491 return NULL;
492
493 Py_INCREF(attr);
494 ag->attr = attr;
495 ag->nattrs = nattrs;
496
497 PyObject_GC_Track(ag);
498 return (PyObject *)ag;
499 }
500
501 static void
502 attrgetter_dealloc(attrgetterobject *ag)
503 {
504 PyObject_GC_UnTrack(ag);
505 Py_XDECREF(ag->attr);
506 PyObject_GC_Del(ag);
507 }
508
509 static int
510 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
511 {
512 Py_VISIT(ag->attr);
513 return 0;
514 }
515
516 static PyObject *
517 dotted_getattr(PyObject *obj, PyObject *attr)
518 {
519 char *s, *p;
520
521 #ifdef Py_USING_UNICODE
522 if (PyUnicode_Check(attr)) {
523 attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);
524 if (attr == NULL)
525 return NULL;
526 }
527 #endif
528
529 if (!PyString_Check(attr)) {
530 PyErr_SetString(PyExc_TypeError,
531 "attribute name must be a string");
532 return NULL;
533 }
534
535 s = PyString_AS_STRING(attr);
536 Py_INCREF(obj);
537 for (;;) {
538 PyObject *newobj, *str;
539 p = strchr(s, '.');
540 str = p ? PyString_FromStringAndSize(s, (p-s)) :
541 PyString_FromString(s);
542 if (str == NULL) {
543 Py_DECREF(obj);
544 return NULL;
545 }
546 newobj = PyObject_GetAttr(obj, str);
547 Py_DECREF(str);
548 Py_DECREF(obj);
549 if (newobj == NULL)
550 return NULL;
551 obj = newobj;
552 if (p == NULL) break;
553 s = p+1;
554 }
555
556 return obj;
557 }
558
559 static PyObject *
560 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
561 {
562 PyObject *obj, *result;
563 Py_ssize_t i, nattrs=ag->nattrs;
564
565 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
566 return NULL;
567 if (ag->nattrs == 1)
568 return dotted_getattr(obj, ag->attr);
569
570 assert(PyTuple_Check(ag->attr));
571 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
572
573 result = PyTuple_New(nattrs);
574 if (result == NULL)
575 return NULL;
576
577 for (i=0 ; i < nattrs ; i++) {
578 PyObject *attr, *val;
579 attr = PyTuple_GET_ITEM(ag->attr, i);
580 val = dotted_getattr(obj, attr);
581 if (val == NULL) {
582 Py_DECREF(result);
583 return NULL;
584 }
585 PyTuple_SET_ITEM(result, i, val);
586 }
587 return result;
588 }
589
590 PyDoc_STRVAR(attrgetter_doc,
591 "attrgetter(attr, ...) --> attrgetter object\n\
592 \n\
593 Return a callable object that fetches the given attribute(s) from its operand.\n\
594 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
595 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
596 After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
597 (r.name.first, r.name.last).");
598
599 static PyTypeObject attrgetter_type = {
600 PyVarObject_HEAD_INIT(NULL, 0)
601 "operator.attrgetter", /* tp_name */
602 sizeof(attrgetterobject), /* tp_basicsize */
603 0, /* tp_itemsize */
604 /* methods */
605 (destructor)attrgetter_dealloc, /* tp_dealloc */
606 0, /* tp_print */
607 0, /* tp_getattr */
608 0, /* tp_setattr */
609 0, /* tp_compare */
610 0, /* tp_repr */
611 0, /* tp_as_number */
612 0, /* tp_as_sequence */
613 0, /* tp_as_mapping */
614 0, /* tp_hash */
615 (ternaryfunc)attrgetter_call, /* tp_call */
616 0, /* tp_str */
617 PyObject_GenericGetAttr, /* tp_getattro */
618 0, /* tp_setattro */
619 0, /* tp_as_buffer */
620 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
621 attrgetter_doc, /* tp_doc */
622 (traverseproc)attrgetter_traverse, /* tp_traverse */
623 0, /* tp_clear */
624 0, /* tp_richcompare */
625 0, /* tp_weaklistoffset */
626 0, /* tp_iter */
627 0, /* tp_iternext */
628 0, /* tp_methods */
629 0, /* tp_members */
630 0, /* tp_getset */
631 0, /* tp_base */
632 0, /* tp_dict */
633 0, /* tp_descr_get */
634 0, /* tp_descr_set */
635 0, /* tp_dictoffset */
636 0, /* tp_init */
637 0, /* tp_alloc */
638 attrgetter_new, /* tp_new */
639 0, /* tp_free */
640 };
641
642
643 /* methodcaller object **********************************************************/
644
645 typedef struct {
646 PyObject_HEAD
647 PyObject *name;
648 PyObject *args;
649 PyObject *kwds;
650 } methodcallerobject;
651
652 static PyTypeObject methodcaller_type;
653
654 static PyObject *
655 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
656 {
657 methodcallerobject *mc;
658 PyObject *name, *newargs;
659
660 if (PyTuple_GET_SIZE(args) < 1) {
661 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
662 "one argument, the method name");
663 return NULL;
664 }
665
666 /* create methodcallerobject structure */
667 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
668 if (mc == NULL)
669 return NULL;
670
671 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
672 if (newargs == NULL) {
673 Py_DECREF(mc);
674 return NULL;
675 }
676 mc->args = newargs;
677
678 name = PyTuple_GET_ITEM(args, 0);
679 Py_INCREF(name);
680 mc->name = name;
681
682 Py_XINCREF(kwds);
683 mc->kwds = kwds;
684
685 PyObject_GC_Track(mc);
686 return (PyObject *)mc;
687 }
688
689 static void
690 methodcaller_dealloc(methodcallerobject *mc)
691 {
692 PyObject_GC_UnTrack(mc);
693 Py_XDECREF(mc->name);
694 Py_XDECREF(mc->args);
695 Py_XDECREF(mc->kwds);
696 PyObject_GC_Del(mc);
697 }
698
699 static int
700 methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
701 {
702 Py_VISIT(mc->args);
703 Py_VISIT(mc->kwds);
704 return 0;
705 }
706
707 static PyObject *
708 methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
709 {
710 PyObject *method, *obj, *result;
711
712 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
713 return NULL;
714 method = PyObject_GetAttr(obj, mc->name);
715 if (method == NULL)
716 return NULL;
717 result = PyObject_Call(method, mc->args, mc->kwds);
718 Py_DECREF(method);
719 return result;
720 }
721
722 PyDoc_STRVAR(methodcaller_doc,
723 "methodcaller(name, ...) --> methodcaller object\n\
724 \n\
725 Return a callable object that calls the given method on its operand.\n\
726 After, f = methodcaller('name'), the call f(r) returns r.name().\n\
727 After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
728 r.name('date', foo=1).");
729
730 static PyTypeObject methodcaller_type = {
731 PyVarObject_HEAD_INIT(NULL, 0)
732 "operator.methodcaller", /* tp_name */
733 sizeof(methodcallerobject), /* tp_basicsize */
734 0, /* tp_itemsize */
735 /* methods */
736 (destructor)methodcaller_dealloc, /* tp_dealloc */
737 0, /* tp_print */
738 0, /* tp_getattr */
739 0, /* tp_setattr */
740 0, /* tp_compare */
741 0, /* tp_repr */
742 0, /* tp_as_number */
743 0, /* tp_as_sequence */
744 0, /* tp_as_mapping */
745 0, /* tp_hash */
746 (ternaryfunc)methodcaller_call, /* tp_call */
747 0, /* tp_str */
748 PyObject_GenericGetAttr, /* tp_getattro */
749 0, /* tp_setattro */
750 0, /* tp_as_buffer */
751 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
752 methodcaller_doc, /* tp_doc */
753 (traverseproc)methodcaller_traverse, /* tp_traverse */
754 0, /* tp_clear */
755 0, /* tp_richcompare */
756 0, /* tp_weaklistoffset */
757 0, /* tp_iter */
758 0, /* tp_iternext */
759 0, /* tp_methods */
760 0, /* tp_members */
761 0, /* tp_getset */
762 0, /* tp_base */
763 0, /* tp_dict */
764 0, /* tp_descr_get */
765 0, /* tp_descr_set */
766 0, /* tp_dictoffset */
767 0, /* tp_init */
768 0, /* tp_alloc */
769 methodcaller_new, /* tp_new */
770 0, /* tp_free */
771 };
772
773
774 /* Initialization function for the module (*must* be called initoperator) */
775
776 PyMODINIT_FUNC
777 initoperator(void)
778 {
779 PyObject *m;
780
781 /* Create the module and add the functions */
782 m = Py_InitModule4("operator", operator_methods, operator_doc,
783 (PyObject*)NULL, PYTHON_API_VERSION);
784 if (m == NULL)
785 return;
786
787 if (PyType_Ready(&itemgetter_type) < 0)
788 return;
789 Py_INCREF(&itemgetter_type);
790 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
791
792 if (PyType_Ready(&attrgetter_type) < 0)
793 return;
794 Py_INCREF(&attrgetter_type);
795 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
796
797 if (PyType_Ready(&methodcaller_type) < 0)
798 return;
799 Py_INCREF(&methodcaller_type);
800 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
801 }