[Bug #848556] Remove \d* from second alternative to avoid exponential case when repea...
[pytest.git] / Modules / operator.c
blob7fc1f8abe820e3dc8cd17dbba2b97d91e78fcb69
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 PyInt_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 PyInt_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(isCallable , PyCallable_Check)
69 spami(isNumberType , PyNumber_Check)
70 spami(truth , PyObject_IsTrue)
71 spam2(op_add , PyNumber_Add)
72 spam2(op_sub , PyNumber_Subtract)
73 spam2(op_mul , PyNumber_Multiply)
74 spam2(op_div , PyNumber_Divide)
75 spam2(op_floordiv , PyNumber_FloorDivide)
76 spam2(op_truediv , PyNumber_TrueDivide)
77 spam2(op_mod , PyNumber_Remainder)
78 spam1(op_neg , PyNumber_Negative)
79 spam1(op_pos , PyNumber_Positive)
80 spam1(op_abs , PyNumber_Absolute)
81 spam1(op_inv , PyNumber_Invert)
82 spam1(op_invert , PyNumber_Invert)
83 spam2(op_lshift , PyNumber_Lshift)
84 spam2(op_rshift , PyNumber_Rshift)
85 spami(op_not_ , PyObject_Not)
86 spam2(op_and_ , PyNumber_And)
87 spam2(op_xor , PyNumber_Xor)
88 spam2(op_or_ , PyNumber_Or)
89 spam2(op_iadd , PyNumber_InPlaceAdd)
90 spam2(op_isub , PyNumber_InPlaceSubtract)
91 spam2(op_imul , PyNumber_InPlaceMultiply)
92 spam2(op_idiv , PyNumber_InPlaceDivide)
93 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
94 spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
95 spam2(op_imod , PyNumber_InPlaceRemainder)
96 spam2(op_ilshift , PyNumber_InPlaceLshift)
97 spam2(op_irshift , PyNumber_InPlaceRshift)
98 spam2(op_iand , PyNumber_InPlaceAnd)
99 spam2(op_ixor , PyNumber_InPlaceXor)
100 spam2(op_ior , PyNumber_InPlaceOr)
101 spami(isSequenceType , PySequence_Check)
102 spam2(op_concat , PySequence_Concat)
103 spamoi(op_repeat , PySequence_Repeat)
104 spam2(op_iconcat , PySequence_InPlaceConcat)
105 spamoi(op_irepeat , PySequence_InPlaceRepeat)
106 spami2b(op_contains , PySequence_Contains)
107 spami2b(sequenceIncludes, PySequence_Contains)
108 spamn2(indexOf , PySequence_Index)
109 spamn2(countOf , PySequence_Count)
110 spami(isMappingType , PyMapping_Check)
111 spam2(op_getitem , PyObject_GetItem)
112 spam2n(op_delitem , PyObject_DelItem)
113 spam3n(op_setitem , PyObject_SetItem)
114 spamrc(op_lt , Py_LT)
115 spamrc(op_le , Py_LE)
116 spamrc(op_eq , Py_EQ)
117 spamrc(op_ne , Py_NE)
118 spamrc(op_gt , Py_GT)
119 spamrc(op_ge , Py_GE)
121 static PyObject*
122 op_pow(PyObject *s, PyObject *a)
124 PyObject *a1, *a2;
125 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
126 return PyNumber_Power(a1, a2, Py_None);
127 return NULL;
130 static PyObject*
131 op_ipow(PyObject *s, PyObject *a)
133 PyObject *a1, *a2;
134 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
135 return PyNumber_InPlacePower(a1, a2, Py_None);
136 return NULL;
139 static PyObject *
140 op_index(PyObject *s, PyObject *a)
142 Py_ssize_t i;
143 PyObject *a1;
144 if (!PyArg_UnpackTuple(a,"index", 1, 1, &a1))
145 return NULL;
146 i = PyNumber_Index(a1);
147 if (i == -1 && PyErr_Occurred())
148 return NULL;
149 else
150 return PyInt_FromSsize_t(i);
153 static PyObject*
154 is_(PyObject *s, PyObject *a)
156 PyObject *a1, *a2, *result = NULL;
157 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
158 result = (a1 == a2) ? Py_True : Py_False;
159 Py_INCREF(result);
161 return result;
164 static PyObject*
165 is_not(PyObject *s, PyObject *a)
167 PyObject *a1, *a2, *result = NULL;
168 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
169 result = (a1 != a2) ? Py_True : Py_False;
170 Py_INCREF(result);
172 return result;
175 static PyObject*
176 op_getslice(PyObject *s, PyObject *a)
178 PyObject *a1;
179 int a2,a3;
181 if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
182 return NULL;
183 return PySequence_GetSlice(a1,a2,a3);
186 static PyObject*
187 op_setslice(PyObject *s, PyObject *a)
189 PyObject *a1, *a4;
190 int a2,a3;
192 if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
193 return NULL;
195 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
196 return NULL;
198 Py_INCREF(Py_None);
199 return Py_None;
202 static PyObject*
203 op_delslice(PyObject *s, PyObject *a)
205 PyObject *a1;
206 int a2,a3;
208 if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
209 return NULL;
211 if (-1 == PySequence_DelSlice(a1,a2,a3))
212 return NULL;
214 Py_INCREF(Py_None);
215 return Py_None;
218 #undef spam1
219 #undef spam2
220 #undef spam1o
221 #undef spam1o
222 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
223 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
224 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
225 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
226 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
227 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
229 static struct PyMethodDef operator_methods[] = {
231 spam1o(isCallable,
232 "isCallable(a) -- Same as callable(a).")
233 spam1o(isNumberType,
234 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
235 spam1o(isSequenceType,
236 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
237 spam1o(truth,
238 "truth(a) -- Return True if a is true, False otherwise.")
239 spam2(contains,__contains__,
240 "contains(a, b) -- Same as b in a (note reversed operands).")
241 spam1(sequenceIncludes,
242 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
243 spam1(indexOf,
244 "indexOf(a, b) -- Return the first index of b in a.")
245 spam1(countOf,
246 "countOf(a, b) -- Return the number of times b occurs in a.")
247 spam1o(isMappingType,
248 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
250 spam1(is_, "is_(a, b) -- Same as a is b.")
251 spam1(is_not, "is_not(a, b) -- Same as a is not b.")
252 spam2(index, __index__, "index(a) -- Same as a.__index__()")
253 spam2(add,__add__, "add(a, b) -- Same as a + b.")
254 spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
255 spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
256 spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
257 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
258 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
259 spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
260 spam2o(neg,__neg__, "neg(a) -- Same as -a.")
261 spam2o(pos,__pos__, "pos(a) -- Same as +a.")
262 spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
263 spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
264 spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
265 spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
266 spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
267 spam2o(not_,__not__, "not_(a) -- Same as not a.")
268 spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
269 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
270 spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
271 spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
272 spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
273 spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
274 spam2(idiv,__idiv__, "idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
275 spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
276 spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
277 spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
278 spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
279 spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
280 spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
281 spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
282 spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
283 spam2(concat,__concat__,
284 "concat(a, b) -- Same as a + b, for a and b sequences.")
285 spam2(repeat,__repeat__,
286 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
287 spam2(iconcat,__iconcat__,
288 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
289 spam2(irepeat,__irepeat__,
290 "irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
291 spam2(getitem,__getitem__,
292 "getitem(a, b) -- Same as a[b].")
293 spam2(setitem,__setitem__,
294 "setitem(a, b, c) -- Same as a[b] = c.")
295 spam2(delitem,__delitem__,
296 "delitem(a, b) -- Same as del a[b].")
297 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
298 spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
299 spam2(getslice,__getslice__,
300 "getslice(a, b, c) -- Same as a[b:c].")
301 spam2(setslice,__setslice__,
302 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
303 spam2(delslice,__delslice__,
304 "delslice(a, b, c) -- Same as del a[b:c].")
305 spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
306 spam2(le,__le__, "le(a, b) -- Same as a<=b.")
307 spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
308 spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
309 spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
310 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
312 {NULL, NULL} /* sentinel */
316 /* itemgetter object **********************************************************/
318 typedef struct {
319 PyObject_HEAD
320 Py_ssize_t nitems;
321 PyObject *item;
322 } itemgetterobject;
324 static PyTypeObject itemgetter_type;
326 static PyObject *
327 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
329 itemgetterobject *ig;
330 PyObject *item;
331 Py_ssize_t nitems;
333 if (!_PyArg_NoKeywords("itemgetter()", kwds))
334 return NULL;
336 nitems = PyTuple_GET_SIZE(args);
337 if (nitems <= 1) {
338 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
339 return NULL;
340 } else
341 item = args;
343 /* create itemgetterobject structure */
344 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
345 if (ig == NULL)
346 return NULL;
348 Py_INCREF(item);
349 ig->item = item;
350 ig->nitems = nitems;
352 PyObject_GC_Track(ig);
353 return (PyObject *)ig;
356 static void
357 itemgetter_dealloc(itemgetterobject *ig)
359 PyObject_GC_UnTrack(ig);
360 Py_XDECREF(ig->item);
361 PyObject_GC_Del(ig);
364 static int
365 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
367 Py_VISIT(ig->item);
368 return 0;
371 static PyObject *
372 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
374 PyObject *obj, *result;
375 Py_ssize_t i, nitems=ig->nitems;
377 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
378 return NULL;
379 if (nitems == 1)
380 return PyObject_GetItem(obj, ig->item);
382 assert(PyTuple_Check(ig->item));
383 assert(PyTuple_GET_SIZE(ig->item) == nitems);
385 result = PyTuple_New(nitems);
386 if (result == NULL)
387 return NULL;
389 for (i=0 ; i < nitems ; i++) {
390 PyObject *item, *val;
391 item = PyTuple_GET_ITEM(ig->item, i);
392 val = PyObject_GetItem(obj, item);
393 if (val == NULL) {
394 Py_DECREF(result);
395 return NULL;
397 PyTuple_SET_ITEM(result, i, val);
399 return result;
402 PyDoc_STRVAR(itemgetter_doc,
403 "itemgetter(item, ...) --> itemgetter object\n\
405 Return a callable object that fetches the given item(s) from its operand.\n\
406 After, f=itemgetter(2), the call f(r) returns r[2].\n\
407 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
409 static PyTypeObject itemgetter_type = {
410 PyObject_HEAD_INIT(NULL)
411 0, /* ob_size */
412 "operator.itemgetter", /* tp_name */
413 sizeof(itemgetterobject), /* tp_basicsize */
414 0, /* tp_itemsize */
415 /* methods */
416 (destructor)itemgetter_dealloc, /* tp_dealloc */
417 0, /* tp_print */
418 0, /* tp_getattr */
419 0, /* tp_setattr */
420 0, /* tp_compare */
421 0, /* tp_repr */
422 0, /* tp_as_number */
423 0, /* tp_as_sequence */
424 0, /* tp_as_mapping */
425 0, /* tp_hash */
426 (ternaryfunc)itemgetter_call, /* tp_call */
427 0, /* tp_str */
428 PyObject_GenericGetAttr, /* tp_getattro */
429 0, /* tp_setattro */
430 0, /* tp_as_buffer */
431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
432 itemgetter_doc, /* tp_doc */
433 (traverseproc)itemgetter_traverse, /* tp_traverse */
434 0, /* tp_clear */
435 0, /* tp_richcompare */
436 0, /* tp_weaklistoffset */
437 0, /* tp_iter */
438 0, /* tp_iternext */
439 0, /* tp_methods */
440 0, /* tp_members */
441 0, /* tp_getset */
442 0, /* tp_base */
443 0, /* tp_dict */
444 0, /* tp_descr_get */
445 0, /* tp_descr_set */
446 0, /* tp_dictoffset */
447 0, /* tp_init */
448 0, /* tp_alloc */
449 itemgetter_new, /* tp_new */
450 0, /* tp_free */
454 /* attrgetter object **********************************************************/
456 typedef struct {
457 PyObject_HEAD
458 Py_ssize_t nattrs;
459 PyObject *attr;
460 } attrgetterobject;
462 static PyTypeObject attrgetter_type;
464 static PyObject *
465 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
467 attrgetterobject *ag;
468 PyObject *attr;
469 Py_ssize_t nattrs;
471 if (!_PyArg_NoKeywords("attrgetter()", kwds))
472 return NULL;
474 nattrs = PyTuple_GET_SIZE(args);
475 if (nattrs <= 1) {
476 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
477 return NULL;
478 } else
479 attr = args;
481 /* create attrgetterobject structure */
482 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
483 if (ag == NULL)
484 return NULL;
486 Py_INCREF(attr);
487 ag->attr = attr;
488 ag->nattrs = nattrs;
490 PyObject_GC_Track(ag);
491 return (PyObject *)ag;
494 static void
495 attrgetter_dealloc(attrgetterobject *ag)
497 PyObject_GC_UnTrack(ag);
498 Py_XDECREF(ag->attr);
499 PyObject_GC_Del(ag);
502 static int
503 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
505 Py_VISIT(ag->attr);
506 return 0;
509 static PyObject *
510 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
512 PyObject *obj, *result;
513 Py_ssize_t i, nattrs=ag->nattrs;
515 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
516 return NULL;
517 if (ag->nattrs == 1)
518 return PyObject_GetAttr(obj, ag->attr);
520 assert(PyTuple_Check(ag->attr));
521 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
523 result = PyTuple_New(nattrs);
524 if (result == NULL)
525 return NULL;
527 for (i=0 ; i < nattrs ; i++) {
528 PyObject *attr, *val;
529 attr = PyTuple_GET_ITEM(ag->attr, i);
530 val = PyObject_GetAttr(obj, attr);
531 if (val == NULL) {
532 Py_DECREF(result);
533 return NULL;
535 PyTuple_SET_ITEM(result, i, val);
537 return result;
540 PyDoc_STRVAR(attrgetter_doc,
541 "attrgetter(attr, ...) --> attrgetter object\n\
543 Return a callable object that fetches the given attribute(s) from its operand.\n\
544 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
545 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).");
547 static PyTypeObject attrgetter_type = {
548 PyObject_HEAD_INIT(NULL)
549 0, /* ob_size */
550 "operator.attrgetter", /* tp_name */
551 sizeof(attrgetterobject), /* tp_basicsize */
552 0, /* tp_itemsize */
553 /* methods */
554 (destructor)attrgetter_dealloc, /* tp_dealloc */
555 0, /* tp_print */
556 0, /* tp_getattr */
557 0, /* tp_setattr */
558 0, /* tp_compare */
559 0, /* tp_repr */
560 0, /* tp_as_number */
561 0, /* tp_as_sequence */
562 0, /* tp_as_mapping */
563 0, /* tp_hash */
564 (ternaryfunc)attrgetter_call, /* tp_call */
565 0, /* tp_str */
566 PyObject_GenericGetAttr, /* tp_getattro */
567 0, /* tp_setattro */
568 0, /* tp_as_buffer */
569 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
570 attrgetter_doc, /* tp_doc */
571 (traverseproc)attrgetter_traverse, /* tp_traverse */
572 0, /* tp_clear */
573 0, /* tp_richcompare */
574 0, /* tp_weaklistoffset */
575 0, /* tp_iter */
576 0, /* tp_iternext */
577 0, /* tp_methods */
578 0, /* tp_members */
579 0, /* tp_getset */
580 0, /* tp_base */
581 0, /* tp_dict */
582 0, /* tp_descr_get */
583 0, /* tp_descr_set */
584 0, /* tp_dictoffset */
585 0, /* tp_init */
586 0, /* tp_alloc */
587 attrgetter_new, /* tp_new */
588 0, /* tp_free */
590 /* Initialization function for the module (*must* be called initoperator) */
592 PyMODINIT_FUNC
593 initoperator(void)
595 PyObject *m;
597 /* Create the module and add the functions */
598 m = Py_InitModule4("operator", operator_methods, operator_doc,
599 (PyObject*)NULL, PYTHON_API_VERSION);
600 if (m == NULL)
601 return;
603 if (PyType_Ready(&itemgetter_type) < 0)
604 return;
605 Py_INCREF(&itemgetter_type);
606 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
608 if (PyType_Ready(&attrgetter_type) < 0)
609 return;
610 Py_INCREF(&attrgetter_type);
611 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);