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