Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | |
| 4 | PyDoc_STRVAR(operator_doc, |
| 5 | "Operator interface.\n\ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 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\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11 | '__' are also provided for convenience."); |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 12 | |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 13 | #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 14 | return AOP(a1); } |
| 15 | |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 16 | #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 17 | PyObject *a1, *a2; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 18 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 19 | return AOP(a1,a2); } |
| 20 | |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 21 | #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 22 | PyObject *a1; int a2; \ |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 23 | if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 24 | return AOP(a1,a2); } |
| 25 | |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 26 | #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 27 | PyObject *a1, *a2; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 28 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 29 | if(-1 == AOP(a1,a2)) return NULL; \ |
| 30 | Py_INCREF(Py_None); \ |
| 31 | return Py_None; } |
| 32 | |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 33 | #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 34 | PyObject *a1, *a2, *a3; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 35 | if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 36 | if(-1 == AOP(a1,a2,a3)) return NULL; \ |
| 37 | Py_INCREF(Py_None); \ |
| 38 | return Py_None; } |
| 39 | |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 40 | #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \ |
| 41 | long r; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 42 | if(-1 == (r=AOP(a1))) return NULL; \ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 43 | return PyBool_FromLong(r); } |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 44 | |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 45 | #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 46 | PyObject *a1, *a2; long r; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 47 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 48 | if(-1 == (r=AOP(a1,a2))) return NULL; \ |
| 49 | return PyInt_FromLong(r); } |
| 50 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 51 | #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 52 | PyObject *a1, *a2; long r; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 53 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 54 | if(-1 == (r=AOP(a1,a2))) return NULL; \ |
| 55 | return PyBool_FromLong(r); } |
| 56 | |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 57 | #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 58 | PyObject *a1, *a2; \ |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 59 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 60 | return PyObject_RichCompare(a1,a2,A); } |
| 61 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 62 | spami(isCallable , PyCallable_Check) |
| 63 | spami(isNumberType , PyNumber_Check) |
| 64 | spami(truth , PyObject_IsTrue) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 65 | spam2(op_add , PyNumber_Add) |
| 66 | spam2(op_sub , PyNumber_Subtract) |
| 67 | spam2(op_mul , PyNumber_Multiply) |
| 68 | spam2(op_div , PyNumber_Divide) |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 69 | spam2(op_floordiv , PyNumber_FloorDivide) |
| 70 | spam2(op_truediv , PyNumber_TrueDivide) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 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) |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 76 | spam1(op_invert , PyNumber_Invert) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 77 | spam2(op_lshift , PyNumber_Lshift) |
| 78 | spam2(op_rshift , PyNumber_Rshift) |
Guido van Rossum | 99c185e | 1998-04-09 17:54:26 +0000 | [diff] [blame] | 79 | spami(op_not_ , PyObject_Not) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 80 | spam2(op_and_ , PyNumber_And) |
| 81 | spam2(op_xor , PyNumber_Xor) |
| 82 | spam2(op_or_ , PyNumber_Or) |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 95 | spami(isSequenceType , PySequence_Check) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 96 | spam2(op_concat , PySequence_Concat) |
| 97 | spamoi(op_repeat , PySequence_Repeat) |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 98 | spam2(op_iconcat , PySequence_InPlaceConcat) |
| 99 | spamoi(op_irepeat , PySequence_InPlaceRepeat) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 100 | spami2b(op_contains , PySequence_Contains) |
| 101 | spami2b(sequenceIncludes, PySequence_Contains) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 102 | spami2(indexOf , PySequence_Index) |
| 103 | spami2(countOf , PySequence_Count) |
| 104 | spami(isMappingType , PyMapping_Check) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 105 | spam2(op_getitem , PyObject_GetItem) |
| 106 | spam2n(op_delitem , PyObject_DelItem) |
| 107 | spam3n(op_setitem , PyObject_SetItem) |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 114 | |
| 115 | static PyObject* |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 116 | op_pow(PyObject *s, PyObject *a) |
| 117 | { |
| 118 | PyObject *a1, *a2; |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 119 | if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2)) |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 120 | return PyNumber_Power(a1, a2, Py_None); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | static PyObject* |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 125 | op_ipow(PyObject *s, PyObject *a) |
| 126 | { |
| 127 | PyObject *a1, *a2; |
| 128 | if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2)) |
| 129 | return PyNumber_InPlacePower(a1, a2, Py_None); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | static PyObject* |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 134 | is_(PyObject *s, PyObject *a) |
| 135 | { |
| 136 | PyObject *a1, *a2, *result = NULL; |
| 137 | if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) { |
| 138 | result = (a1 == a2) ? Py_True : Py_False; |
| 139 | Py_INCREF(result); |
| 140 | } |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | static PyObject* |
| 145 | is_not(PyObject *s, PyObject *a) |
| 146 | { |
| 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; |
| 150 | Py_INCREF(result); |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | static PyObject* |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 156 | op_getslice(PyObject *s, PyObject *a) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 157 | { |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 158 | PyObject *a1; |
Guido van Rossum | 24a4994 | 1997-08-28 18:11:05 +0000 | [diff] [blame] | 159 | int a2,a3; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 160 | |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 161 | if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3)) |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | return PySequence_GetSlice(a1,a2,a3); |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static PyObject* |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 167 | op_setslice(PyObject *s, PyObject *a) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 168 | { |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 169 | PyObject *a1, *a4; |
Guido van Rossum | 24a4994 | 1997-08-28 18:11:05 +0000 | [diff] [blame] | 170 | int a2,a3; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 171 | |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 172 | if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4)) |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 173 | return NULL; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 174 | |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 175 | if (-1 == PySequence_SetSlice(a1,a2,a3,a4)) |
| 176 | return NULL; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 177 | |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 178 | Py_INCREF(Py_None); |
| 179 | return Py_None; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 182 | static PyObject* |
Fred Drake | 5639ba4 | 2000-07-08 04:12:08 +0000 | [diff] [blame] | 183 | op_delslice(PyObject *s, PyObject *a) |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 184 | { |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 185 | PyObject *a1; |
Guido van Rossum | 24a4994 | 1997-08-28 18:11:05 +0000 | [diff] [blame] | 186 | int a2,a3; |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 187 | |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 188 | if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3)) |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 189 | return NULL; |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 190 | |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 191 | if (-1 == PySequence_DelSlice(a1,a2,a3)) |
| 192 | return NULL; |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 193 | |
Barry Warsaw | 19f61ae | 1996-12-18 19:50:00 +0000 | [diff] [blame] | 194 | Py_INCREF(Py_None); |
| 195 | return Py_None; |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 198 | #undef spam1 |
| 199 | #undef spam2 |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 200 | #undef spam1o |
| 201 | #undef spam1o |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 202 | #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)}, |
Armin Rigo | c4308d5 | 2005-12-29 14:39:28 +0000 | [diff] [blame] | 203 | #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \ |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 204 | {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 205 | #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)}, |
Armin Rigo | c4308d5 | 2005-12-29 14:39:28 +0000 | [diff] [blame] | 206 | #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 207 | {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)}, |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 208 | |
| 209 | static struct PyMethodDef operator_methods[] = { |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 210 | |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 211 | spam1o(isCallable, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 212 | "isCallable(a) -- Same as callable(a).") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 213 | spam1o(isNumberType, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 214 | "isNumberType(a) -- Return True if a has a numeric type, False otherwise.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 215 | spam1o(isSequenceType, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 216 | "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 217 | spam1o(truth, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 218 | "truth(a) -- Return True if a is true, False otherwise.") |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 219 | spam2(contains,__contains__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 220 | "contains(a, b) -- Same as b in a (note reversed operands).") |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 221 | spam1(sequenceIncludes, |
| 222 | "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 223 | spam1(indexOf, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 224 | "indexOf(a, b) -- Return the first index of b in a.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 225 | spam1(countOf, |
| 226 | "countOf(a, b) -- Return the number of times b occurs in a.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 227 | spam1o(isMappingType, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 228 | "isMappingType(a) -- Return True if a has a mapping type, False otherwise.") |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 229 | |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 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.") |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 238 | spam2(mod,__mod__, "mod(a, b) -- Same as a % b.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 244 | spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.") |
| 245 | spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 246 | spam2o(not_,__not__, "not_(a) -- Same as not a.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 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.") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 262 | spam2(concat,__concat__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 263 | "concat(a, b) -- Same as a + b, for a and b sequences.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 264 | spam2(repeat,__repeat__, |
Guido van Rossum | 36a484f | 1996-12-05 19:01:16 +0000 | [diff] [blame] | 265 | "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 270 | spam2(getitem,__getitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 271 | "getitem(a, b) -- Same as a[b].") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 272 | spam2(setitem,__setitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 273 | "setitem(a, b, c) -- Same as a[b] = c.") |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 274 | spam2(delitem,__delitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 275 | "delitem(a, b) -- Same as del a[b].") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 276 | spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.") |
| 277 | spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 278 | spam2(getslice,__getslice__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 279 | "getslice(a, b, c) -- Same as a[b:c].") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 280 | spam2(setslice,__setslice__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 281 | "setslice(a, b, c, d) -- Same as a[b:c] = d.") |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 282 | spam2(delslice,__delslice__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 283 | "delslice(a, b, c) -- Same as del a[b:c].") |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 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.") |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 291 | {NULL, NULL} /* sentinel */ |
| 292 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 295 | /* itemgetter object **********************************************************/ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 296 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 297 | typedef struct { |
| 298 | PyObject_HEAD |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 299 | int nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 300 | PyObject *item; |
| 301 | } itemgetterobject; |
| 302 | |
| 303 | static PyTypeObject itemgetter_type; |
| 304 | |
| 305 | static PyObject * |
| 306 | itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 307 | { |
| 308 | itemgetterobject *ig; |
| 309 | PyObject *item; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 310 | int nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 311 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 312 | if (!_PyArg_NoKeywords("itemgetter()", kwds)) |
| 313 | return NULL; |
| 314 | |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 315 | nitems = PyTuple_GET_SIZE(args); |
| 316 | if (nitems <= 1) { |
| 317 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item)) |
| 318 | return NULL; |
| 319 | } else |
| 320 | item = args; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 321 | |
| 322 | /* create itemgetterobject structure */ |
| 323 | ig = PyObject_GC_New(itemgetterobject, &itemgetter_type); |
| 324 | if (ig == NULL) |
| 325 | return NULL; |
| 326 | |
| 327 | Py_INCREF(item); |
| 328 | ig->item = item; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 329 | ig->nitems = nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 330 | |
| 331 | PyObject_GC_Track(ig); |
| 332 | return (PyObject *)ig; |
| 333 | } |
| 334 | |
| 335 | static void |
| 336 | itemgetter_dealloc(itemgetterobject *ig) |
| 337 | { |
| 338 | PyObject_GC_UnTrack(ig); |
| 339 | Py_XDECREF(ig->item); |
| 340 | PyObject_GC_Del(ig); |
| 341 | } |
| 342 | |
| 343 | static int |
| 344 | itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg) |
| 345 | { |
| 346 | if (ig->item) |
| 347 | return visit(ig->item, arg); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static PyObject * |
| 352 | itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw) |
| 353 | { |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 354 | PyObject *obj, *result; |
| 355 | int i, nitems=ig->nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 356 | |
| 357 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj)) |
| 358 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 359 | if (nitems == 1) |
| 360 | return PyObject_GetItem(obj, ig->item); |
| 361 | |
| 362 | assert(PyTuple_Check(ig->item)); |
| 363 | assert(PyTuple_GET_SIZE(ig->item) == nitems); |
| 364 | |
| 365 | result = PyTuple_New(nitems); |
| 366 | if (result == NULL) |
| 367 | return NULL; |
| 368 | |
| 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); |
| 373 | if (val == NULL) { |
| 374 | Py_DECREF(result); |
| 375 | return NULL; |
| 376 | } |
| 377 | PyTuple_SET_ITEM(result, i, val); |
| 378 | } |
| 379 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | PyDoc_STRVAR(itemgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 383 | "itemgetter(item, ...) --> itemgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 384 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 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])"); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 388 | |
| 389 | static PyTypeObject itemgetter_type = { |
| 390 | PyObject_HEAD_INIT(NULL) |
| 391 | 0, /* ob_size */ |
Raymond Hettinger | bd3a240 | 2003-12-04 22:17:49 +0000 | [diff] [blame] | 392 | "operator.itemgetter", /* tp_name */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 393 | sizeof(itemgetterobject), /* tp_basicsize */ |
| 394 | 0, /* tp_itemsize */ |
| 395 | /* methods */ |
| 396 | (destructor)itemgetter_dealloc, /* tp_dealloc */ |
| 397 | 0, /* tp_print */ |
| 398 | 0, /* tp_getattr */ |
| 399 | 0, /* tp_setattr */ |
| 400 | 0, /* tp_compare */ |
| 401 | 0, /* tp_repr */ |
| 402 | 0, /* tp_as_number */ |
| 403 | 0, /* tp_as_sequence */ |
| 404 | 0, /* tp_as_mapping */ |
| 405 | 0, /* tp_hash */ |
| 406 | (ternaryfunc)itemgetter_call, /* tp_call */ |
| 407 | 0, /* tp_str */ |
| 408 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 409 | 0, /* tp_setattro */ |
| 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 */ |
| 414 | 0, /* tp_clear */ |
| 415 | 0, /* tp_richcompare */ |
| 416 | 0, /* tp_weaklistoffset */ |
| 417 | 0, /* tp_iter */ |
| 418 | 0, /* tp_iternext */ |
| 419 | 0, /* tp_methods */ |
| 420 | 0, /* tp_members */ |
| 421 | 0, /* tp_getset */ |
| 422 | 0, /* tp_base */ |
| 423 | 0, /* tp_dict */ |
| 424 | 0, /* tp_descr_get */ |
| 425 | 0, /* tp_descr_set */ |
| 426 | 0, /* tp_dictoffset */ |
| 427 | 0, /* tp_init */ |
| 428 | 0, /* tp_alloc */ |
| 429 | itemgetter_new, /* tp_new */ |
| 430 | 0, /* tp_free */ |
| 431 | }; |
| 432 | |
| 433 | |
| 434 | /* attrgetter object **********************************************************/ |
| 435 | |
| 436 | typedef struct { |
| 437 | PyObject_HEAD |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 438 | int nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 439 | PyObject *attr; |
| 440 | } attrgetterobject; |
| 441 | |
| 442 | static PyTypeObject attrgetter_type; |
| 443 | |
| 444 | static PyObject * |
| 445 | attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 446 | { |
| 447 | attrgetterobject *ag; |
| 448 | PyObject *attr; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 449 | int nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 450 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 451 | if (!_PyArg_NoKeywords("attrgetter()", kwds)) |
| 452 | return NULL; |
| 453 | |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 454 | nattrs = PyTuple_GET_SIZE(args); |
| 455 | if (nattrs <= 1) { |
| 456 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr)) |
| 457 | return NULL; |
| 458 | } else |
| 459 | attr = args; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 460 | |
| 461 | /* create attrgetterobject structure */ |
| 462 | ag = PyObject_GC_New(attrgetterobject, &attrgetter_type); |
| 463 | if (ag == NULL) |
| 464 | return NULL; |
| 465 | |
| 466 | Py_INCREF(attr); |
| 467 | ag->attr = attr; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 468 | ag->nattrs = nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 469 | |
| 470 | PyObject_GC_Track(ag); |
| 471 | return (PyObject *)ag; |
| 472 | } |
| 473 | |
| 474 | static void |
| 475 | attrgetter_dealloc(attrgetterobject *ag) |
| 476 | { |
| 477 | PyObject_GC_UnTrack(ag); |
| 478 | Py_XDECREF(ag->attr); |
| 479 | PyObject_GC_Del(ag); |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg) |
| 484 | { |
| 485 | if (ag->attr) |
| 486 | return visit(ag->attr, arg); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static PyObject * |
| 491 | attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw) |
| 492 | { |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 493 | PyObject *obj, *result; |
| 494 | int i, nattrs=ag->nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 495 | |
| 496 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj)) |
| 497 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 498 | if (ag->nattrs == 1) |
| 499 | return PyObject_GetAttr(obj, ag->attr); |
| 500 | |
| 501 | assert(PyTuple_Check(ag->attr)); |
| 502 | assert(PyTuple_GET_SIZE(ag->attr) == nattrs); |
| 503 | |
| 504 | result = PyTuple_New(nattrs); |
| 505 | if (result == NULL) |
| 506 | return NULL; |
| 507 | |
| 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); |
| 512 | if (val == NULL) { |
| 513 | Py_DECREF(result); |
| 514 | return NULL; |
| 515 | } |
| 516 | PyTuple_SET_ITEM(result, i, val); |
| 517 | } |
| 518 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | PyDoc_STRVAR(attrgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 522 | "attrgetter(attr, ...) --> attrgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 523 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 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)."); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 527 | |
| 528 | static PyTypeObject attrgetter_type = { |
| 529 | PyObject_HEAD_INIT(NULL) |
| 530 | 0, /* ob_size */ |
Raymond Hettinger | bd3a240 | 2003-12-04 22:17:49 +0000 | [diff] [blame] | 531 | "operator.attrgetter", /* tp_name */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 532 | sizeof(attrgetterobject), /* tp_basicsize */ |
| 533 | 0, /* tp_itemsize */ |
| 534 | /* methods */ |
| 535 | (destructor)attrgetter_dealloc, /* tp_dealloc */ |
| 536 | 0, /* tp_print */ |
| 537 | 0, /* tp_getattr */ |
| 538 | 0, /* tp_setattr */ |
| 539 | 0, /* tp_compare */ |
| 540 | 0, /* tp_repr */ |
| 541 | 0, /* tp_as_number */ |
| 542 | 0, /* tp_as_sequence */ |
| 543 | 0, /* tp_as_mapping */ |
| 544 | 0, /* tp_hash */ |
| 545 | (ternaryfunc)attrgetter_call, /* tp_call */ |
| 546 | 0, /* tp_str */ |
| 547 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 548 | 0, /* tp_setattro */ |
| 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 */ |
| 553 | 0, /* tp_clear */ |
| 554 | 0, /* tp_richcompare */ |
| 555 | 0, /* tp_weaklistoffset */ |
| 556 | 0, /* tp_iter */ |
| 557 | 0, /* tp_iternext */ |
| 558 | 0, /* tp_methods */ |
| 559 | 0, /* tp_members */ |
| 560 | 0, /* tp_getset */ |
| 561 | 0, /* tp_base */ |
| 562 | 0, /* tp_dict */ |
| 563 | 0, /* tp_descr_get */ |
| 564 | 0, /* tp_descr_set */ |
| 565 | 0, /* tp_dictoffset */ |
| 566 | 0, /* tp_init */ |
| 567 | 0, /* tp_alloc */ |
| 568 | attrgetter_new, /* tp_new */ |
| 569 | 0, /* tp_free */ |
| 570 | }; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 571 | /* Initialization function for the module (*must* be called initoperator) */ |
| 572 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 573 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 574 | initoperator(void) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 575 | { |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 576 | PyObject *m; |
| 577 | |
| 578 | /* Create the module and add the functions */ |
| 579 | m = Py_InitModule4("operator", operator_methods, operator_doc, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 580 | (PyObject*)NULL, PYTHON_API_VERSION); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame^] | 581 | if (m == NULL) |
| 582 | return; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 583 | |
| 584 | if (PyType_Ready(&itemgetter_type) < 0) |
| 585 | return; |
| 586 | Py_INCREF(&itemgetter_type); |
| 587 | PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type); |
| 588 | |
| 589 | if (PyType_Ready(&attrgetter_type) < 0) |
| 590 | return; |
| 591 | Py_INCREF(&attrgetter_type); |
| 592 | PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 593 | } |