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