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 |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 162 | |
| 163 | /* compare_digest **********************************************************/ |
| 164 | |
| 165 | /* |
| 166 | * timing safe compare |
| 167 | * |
| 168 | * Returns 1 of the strings are equal. |
| 169 | * In case of len(a) != len(b) the function tries to keep the timing |
| 170 | * dependent on the length of b. CPU cache locally may still alter timing |
| 171 | * a bit. |
| 172 | */ |
| 173 | static int |
| 174 | _tscmp(const unsigned char *a, const unsigned char *b, |
| 175 | Py_ssize_t len_a, Py_ssize_t len_b) |
| 176 | { |
| 177 | /* The volatile type declarations make sure that the compiler has no |
| 178 | * chance to optimize and fold the code in any way that may change |
| 179 | * the timing. |
| 180 | */ |
| 181 | volatile Py_ssize_t length; |
| 182 | volatile const unsigned char *left; |
| 183 | volatile const unsigned char *right; |
| 184 | Py_ssize_t i; |
| 185 | unsigned char result; |
| 186 | |
| 187 | /* loop count depends on length of b */ |
| 188 | length = len_b; |
| 189 | left = NULL; |
| 190 | right = b; |
| 191 | |
| 192 | /* don't use else here to keep the amount of CPU instructions constant, |
| 193 | * volatile forces re-evaluation |
| 194 | * */ |
| 195 | if (len_a == length) { |
| 196 | left = *((volatile const unsigned char**)&a); |
| 197 | result = 0; |
| 198 | } |
| 199 | if (len_a != length) { |
| 200 | left = b; |
| 201 | result = 1; |
| 202 | } |
| 203 | |
| 204 | for (i=0; i < length; i++) { |
| 205 | result |= *left++ ^ *right++; |
| 206 | } |
| 207 | |
| 208 | return (result == 0); |
| 209 | } |
| 210 | |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 211 | PyDoc_STRVAR(length_hint__doc__, |
| 212 | "length_hint(obj, default=0) -> int\n" |
| 213 | "Return an estimate of the number of items in obj.\n" |
| 214 | "This is useful for presizing containers when building from an\n" |
| 215 | "iterable.\n" |
| 216 | "\n" |
| 217 | "If the object supports len(), the result will be\n" |
| 218 | "exact. Otherwise, it may over- or under-estimate by an\n" |
| 219 | "arbitrary amount. The result will be an integer >= 0."); |
| 220 | |
| 221 | static PyObject *length_hint(PyObject *self, PyObject *args) |
| 222 | { |
| 223 | PyObject *obj; |
| 224 | Py_ssize_t defaultvalue = 0, res; |
| 225 | if (!PyArg_ParseTuple(args, "O|n:length_hint", &obj, &defaultvalue)) { |
| 226 | return NULL; |
| 227 | } |
| 228 | res = PyObject_LengthHint(obj, defaultvalue); |
| 229 | if (res == -1 && PyErr_Occurred()) { |
| 230 | return NULL; |
| 231 | } |
| 232 | return PyLong_FromSsize_t(res); |
| 233 | } |
| 234 | |
| 235 | |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 236 | PyDoc_STRVAR(compare_digest__doc__, |
| 237 | "compare_digest(a, b) -> bool\n" |
| 238 | "\n" |
Georg Brandl | a1bc35f | 2012-06-24 16:07:33 +0200 | [diff] [blame] | 239 | "Return 'a == b'. This function uses an approach designed to prevent\n" |
| 240 | "timing analysis, making it appropriate for cryptography.\n" |
| 241 | "a and b must both be of the same type: either str (ASCII only),\n" |
| 242 | "or any type that supports the buffer protocol (e.g. bytes).\n" |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 243 | "\n" |
Georg Brandl | a1bc35f | 2012-06-24 16:07:33 +0200 | [diff] [blame] | 244 | "Note: If a and b are of different lengths, or if an error occurs,\n" |
Larry Hastings | 48986d6 | 2012-06-25 00:59:34 -0700 | [diff] [blame] | 245 | "a timing attack could theoretically reveal information about the\n" |
| 246 | "types and lengths of a and b--but not their values.\n"); |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 247 | |
| 248 | static PyObject* |
| 249 | compare_digest(PyObject *self, PyObject *args) |
| 250 | { |
| 251 | PyObject *a, *b; |
| 252 | int rc; |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 253 | |
| 254 | if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) { |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | /* ASCII unicode string */ |
| 259 | if(PyUnicode_Check(a) && PyUnicode_Check(b)) { |
| 260 | if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { |
| 261 | return NULL; |
| 262 | } |
| 263 | if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) { |
| 264 | PyErr_SetString(PyExc_TypeError, |
| 265 | "comparing strings with non-ASCII characters is " |
| 266 | "not supported"); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | rc = _tscmp(PyUnicode_DATA(a), |
| 271 | PyUnicode_DATA(b), |
| 272 | PyUnicode_GET_LENGTH(a), |
| 273 | PyUnicode_GET_LENGTH(b)); |
| 274 | } |
| 275 | /* fallback to buffer interface for bytes, bytesarray and other */ |
| 276 | else { |
| 277 | Py_buffer view_a; |
| 278 | Py_buffer view_b; |
| 279 | |
| 280 | if ((PyObject_CheckBuffer(a) == 0) & (PyObject_CheckBuffer(b) == 0)) { |
| 281 | PyErr_Format(PyExc_TypeError, |
| 282 | "unsupported operand types(s) or combination of types: " |
| 283 | "'%.100s' and '%.100s'", |
| 284 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) { |
| 289 | return NULL; |
| 290 | } |
| 291 | if (view_a.ndim > 1) { |
| 292 | PyErr_SetString(PyExc_BufferError, |
| 293 | "Buffer must be single dimension"); |
| 294 | PyBuffer_Release(&view_a); |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) { |
| 299 | PyBuffer_Release(&view_a); |
| 300 | return NULL; |
| 301 | } |
| 302 | if (view_b.ndim > 1) { |
| 303 | PyErr_SetString(PyExc_BufferError, |
| 304 | "Buffer must be single dimension"); |
| 305 | PyBuffer_Release(&view_a); |
| 306 | PyBuffer_Release(&view_b); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | rc = _tscmp((const unsigned char*)view_a.buf, |
| 311 | (const unsigned char*)view_b.buf, |
| 312 | view_a.len, |
| 313 | view_b.len); |
| 314 | |
| 315 | PyBuffer_Release(&view_a); |
| 316 | PyBuffer_Release(&view_b); |
| 317 | } |
| 318 | |
Georg Brandl | 93b7d7e | 2012-06-24 13:54:51 +0200 | [diff] [blame] | 319 | return PyBool_FromLong(rc); |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* operator methods **********************************************************/ |
| 323 | |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 324 | #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)}, |
Armin Rigo | c4308d5 | 2005-12-29 14:39:28 +0000 | [diff] [blame] | 325 | #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] | 326 | {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 327 | #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)}, |
Armin Rigo | c4308d5 | 2005-12-29 14:39:28 +0000 | [diff] [blame] | 328 | #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] | 329 | {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)}, |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 330 | |
| 331 | static struct PyMethodDef operator_methods[] = { |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 332 | |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 333 | spam1o(truth, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 334 | "truth(a) -- Return True if a is true, False otherwise.") |
Fred Drake | ea4d3f0 | 2000-09-17 16:09:27 +0000 | [diff] [blame] | 335 | spam2(contains,__contains__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 336 | "contains(a, b) -- Same as b in a (note reversed operands).") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 337 | spam1(indexOf, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 338 | "indexOf(a, b) -- Return the first index of b in a.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 339 | spam1(countOf, |
| 340 | "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] | 341 | |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 342 | spam1(is_, "is_(a, b) -- Same as a is b.") |
| 343 | 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] | 344 | spam2o(index, __index__, "index(a) -- Same as a.__index__()") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 345 | spam2(add,__add__, "add(a, b) -- Same as a + b.") |
| 346 | spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") |
| 347 | spam2(mul,__mul__, "mul(a, b) -- Same as a * b.") |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 348 | spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.") |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 349 | spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 350 | spam2(mod,__mod__, "mod(a, b) -- Same as a % b.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 351 | spam2o(neg,__neg__, "neg(a) -- Same as -a.") |
| 352 | spam2o(pos,__pos__, "pos(a) -- Same as +a.") |
| 353 | spam2o(abs,__abs__, "abs(a) -- Same as abs(a).") |
| 354 | spam2o(inv,__inv__, "inv(a) -- Same as ~a.") |
| 355 | spam2o(invert,__invert__, "invert(a) -- Same as ~a.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 356 | spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.") |
| 357 | spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.") |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 358 | spam2o(not_,__not__, "not_(a) -- Same as not a.") |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 359 | spam2(and_,__and__, "and_(a, b) -- Same as a & b.") |
| 360 | spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.") |
| 361 | spam2(or_,__or__, "or_(a, b) -- Same as a | b.") |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 362 | spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.") |
| 363 | spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.") |
| 364 | spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.") |
| 365 | spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.") |
| 366 | spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b") |
| 367 | spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.") |
| 368 | spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.") |
| 369 | spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.") |
| 370 | spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.") |
| 371 | spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.") |
| 372 | spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 373 | spam2(concat,__concat__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 374 | "concat(a, b) -- Same as a + b, for a and b sequences.") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 375 | spam2(iconcat,__iconcat__, |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 376 | "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] | 377 | spam2(getitem,__getitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 378 | "getitem(a, b) -- Same as a[b].") |
Guido van Rossum | 1720230 | 1996-08-19 22:01:39 +0000 | [diff] [blame] | 379 | spam2(setitem,__setitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 380 | "setitem(a, b, c) -- Same as a[b] = c.") |
Guido van Rossum | c9fb47e | 1996-08-21 17:40:51 +0000 | [diff] [blame] | 381 | spam2(delitem,__delitem__, |
Guido van Rossum | 832f6d2 | 1998-05-22 18:12:59 +0000 | [diff] [blame] | 382 | "delitem(a, b) -- Same as del a[b].") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 383 | spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.") |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 384 | spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.") |
Fred Drake | 428e75f | 2001-08-09 20:14:34 +0000 | [diff] [blame] | 385 | spam2(lt,__lt__, "lt(a, b) -- Same as a<b.") |
| 386 | spam2(le,__le__, "le(a, b) -- Same as a<=b.") |
| 387 | spam2(eq,__eq__, "eq(a, b) -- Same as a==b.") |
| 388 | spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.") |
| 389 | spam2(gt,__gt__, "gt(a, b) -- Same as a>b.") |
| 390 | spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.") |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 391 | |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 392 | {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS, |
| 393 | compare_digest__doc__}, |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 394 | {"length_hint", (PyCFunction)length_hint, METH_VARARGS, |
| 395 | length_hint__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 400 | /* itemgetter object **********************************************************/ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 401 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 402 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | PyObject_HEAD |
| 404 | Py_ssize_t nitems; |
| 405 | PyObject *item; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 406 | } itemgetterobject; |
| 407 | |
| 408 | static PyTypeObject itemgetter_type; |
| 409 | |
| 410 | static PyObject * |
| 411 | itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | itemgetterobject *ig; |
| 414 | PyObject *item; |
| 415 | Py_ssize_t nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | if (!_PyArg_NoKeywords("itemgetter()", kwds)) |
| 418 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | nitems = PyTuple_GET_SIZE(args); |
| 421 | if (nitems <= 1) { |
| 422 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item)) |
| 423 | return NULL; |
| 424 | } else |
| 425 | item = args; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | /* create itemgetterobject structure */ |
| 428 | ig = PyObject_GC_New(itemgetterobject, &itemgetter_type); |
| 429 | if (ig == NULL) |
| 430 | return NULL; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | Py_INCREF(item); |
| 433 | ig->item = item; |
| 434 | ig->nitems = nitems; |
| 435 | |
| 436 | PyObject_GC_Track(ig); |
| 437 | return (PyObject *)ig; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void |
| 441 | itemgetter_dealloc(itemgetterobject *ig) |
| 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | PyObject_GC_UnTrack(ig); |
| 444 | Py_XDECREF(ig->item); |
| 445 | PyObject_GC_Del(ig); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static int |
| 449 | itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg) |
| 450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_VISIT(ig->item); |
| 452 | return 0; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static PyObject * |
| 456 | itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw) |
| 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | PyObject *obj, *result; |
| 459 | Py_ssize_t i, nitems=ig->nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj)) |
| 462 | return NULL; |
| 463 | if (nitems == 1) |
| 464 | return PyObject_GetItem(obj, ig->item); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | assert(PyTuple_Check(ig->item)); |
| 467 | assert(PyTuple_GET_SIZE(ig->item) == nitems); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | result = PyTuple_New(nitems); |
| 470 | if (result == NULL) |
| 471 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | for (i=0 ; i < nitems ; i++) { |
| 474 | PyObject *item, *val; |
| 475 | item = PyTuple_GET_ITEM(ig->item, i); |
| 476 | val = PyObject_GetItem(obj, item); |
| 477 | if (val == NULL) { |
| 478 | Py_DECREF(result); |
| 479 | return NULL; |
| 480 | } |
| 481 | PyTuple_SET_ITEM(result, i, val); |
| 482 | } |
| 483 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | PyDoc_STRVAR(itemgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 487 | "itemgetter(item, ...) --> itemgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 488 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 489 | Return a callable object that fetches the given item(s) from its operand.\n\ |
| 490 | After, f=itemgetter(2), the call f(r) returns r[2].\n\ |
| 491 | 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] | 492 | |
| 493 | static PyTypeObject itemgetter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | PyVarObject_HEAD_INIT(NULL, 0) |
| 495 | "operator.itemgetter", /* tp_name */ |
| 496 | sizeof(itemgetterobject), /* tp_basicsize */ |
| 497 | 0, /* tp_itemsize */ |
| 498 | /* methods */ |
| 499 | (destructor)itemgetter_dealloc, /* tp_dealloc */ |
| 500 | 0, /* tp_print */ |
| 501 | 0, /* tp_getattr */ |
| 502 | 0, /* tp_setattr */ |
| 503 | 0, /* tp_reserved */ |
| 504 | 0, /* tp_repr */ |
| 505 | 0, /* tp_as_number */ |
| 506 | 0, /* tp_as_sequence */ |
| 507 | 0, /* tp_as_mapping */ |
| 508 | 0, /* tp_hash */ |
| 509 | (ternaryfunc)itemgetter_call, /* tp_call */ |
| 510 | 0, /* tp_str */ |
| 511 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 512 | 0, /* tp_setattro */ |
| 513 | 0, /* tp_as_buffer */ |
| 514 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 515 | itemgetter_doc, /* tp_doc */ |
| 516 | (traverseproc)itemgetter_traverse, /* tp_traverse */ |
| 517 | 0, /* tp_clear */ |
| 518 | 0, /* tp_richcompare */ |
| 519 | 0, /* tp_weaklistoffset */ |
| 520 | 0, /* tp_iter */ |
| 521 | 0, /* tp_iternext */ |
| 522 | 0, /* tp_methods */ |
| 523 | 0, /* tp_members */ |
| 524 | 0, /* tp_getset */ |
| 525 | 0, /* tp_base */ |
| 526 | 0, /* tp_dict */ |
| 527 | 0, /* tp_descr_get */ |
| 528 | 0, /* tp_descr_set */ |
| 529 | 0, /* tp_dictoffset */ |
| 530 | 0, /* tp_init */ |
| 531 | 0, /* tp_alloc */ |
| 532 | itemgetter_new, /* tp_new */ |
| 533 | 0, /* tp_free */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | |
| 537 | /* attrgetter object **********************************************************/ |
| 538 | |
| 539 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | PyObject_HEAD |
| 541 | Py_ssize_t nattrs; |
| 542 | PyObject *attr; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 543 | } attrgetterobject; |
| 544 | |
| 545 | static PyTypeObject attrgetter_type; |
| 546 | |
| 547 | static PyObject * |
| 548 | attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | attrgetterobject *ag; |
| 551 | PyObject *attr; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 552 | Py_ssize_t nattrs, idx, char_idx; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | if (!_PyArg_NoKeywords("attrgetter()", kwds)) |
| 555 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | nattrs = PyTuple_GET_SIZE(args); |
| 558 | if (nattrs <= 1) { |
| 559 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr)) |
| 560 | return NULL; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | attr = PyTuple_New(nattrs); |
| 564 | if (attr == NULL) |
| 565 | return NULL; |
| 566 | |
| 567 | /* prepare attr while checking args */ |
| 568 | for (idx = 0; idx < nattrs; ++idx) { |
| 569 | PyObject *item = PyTuple_GET_ITEM(args, idx); |
| 570 | Py_ssize_t item_len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 571 | void *data; |
| 572 | unsigned int kind; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 573 | int dot_count; |
| 574 | |
| 575 | if (!PyUnicode_Check(item)) { |
| 576 | PyErr_SetString(PyExc_TypeError, |
| 577 | "attribute name must be a string"); |
| 578 | Py_DECREF(attr); |
| 579 | return NULL; |
| 580 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 581 | if (PyUnicode_READY(item)) { |
| 582 | Py_DECREF(attr); |
| 583 | return NULL; |
| 584 | } |
| 585 | item_len = PyUnicode_GET_LENGTH(item); |
| 586 | kind = PyUnicode_KIND(item); |
| 587 | data = PyUnicode_DATA(item); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 588 | |
| 589 | /* check whethere the string is dotted */ |
| 590 | dot_count = 0; |
| 591 | for (char_idx = 0; char_idx < item_len; ++char_idx) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 592 | if (PyUnicode_READ(kind, data, char_idx) == '.') |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 593 | ++dot_count; |
| 594 | } |
| 595 | |
| 596 | if (dot_count == 0) { |
| 597 | Py_INCREF(item); |
| 598 | PyUnicode_InternInPlace(&item); |
| 599 | PyTuple_SET_ITEM(attr, idx, item); |
| 600 | } else { /* make it a tuple of non-dotted attrnames */ |
| 601 | PyObject *attr_chain = PyTuple_New(dot_count + 1); |
| 602 | PyObject *attr_chain_item; |
Antoine Pitrou | 87298c4 | 2010-10-31 21:03:01 +0000 | [diff] [blame] | 603 | Py_ssize_t unibuff_from = 0; |
| 604 | Py_ssize_t unibuff_till = 0; |
| 605 | Py_ssize_t attr_chain_idx = 0; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 606 | |
| 607 | if (attr_chain == NULL) { |
| 608 | Py_DECREF(attr); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 612 | for (; dot_count > 0; --dot_count) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 613 | while (PyUnicode_READ(kind, data, unibuff_till) != '.') { |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 614 | ++unibuff_till; |
| 615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 616 | attr_chain_item = PyUnicode_Substring(item, |
| 617 | unibuff_from, |
| 618 | unibuff_till); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 619 | if (attr_chain_item == NULL) { |
| 620 | Py_DECREF(attr_chain); |
| 621 | Py_DECREF(attr); |
| 622 | return NULL; |
| 623 | } |
| 624 | PyUnicode_InternInPlace(&attr_chain_item); |
| 625 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
| 626 | ++attr_chain_idx; |
| 627 | unibuff_till = unibuff_from = unibuff_till + 1; |
| 628 | } |
| 629 | |
| 630 | /* now add the last dotless name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 631 | attr_chain_item = PyUnicode_Substring(item, |
| 632 | unibuff_from, item_len); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 633 | if (attr_chain_item == NULL) { |
| 634 | Py_DECREF(attr_chain); |
| 635 | Py_DECREF(attr); |
| 636 | return NULL; |
| 637 | } |
| 638 | PyUnicode_InternInPlace(&attr_chain_item); |
| 639 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
| 640 | |
| 641 | PyTuple_SET_ITEM(attr, idx, attr_chain); |
| 642 | } |
| 643 | } |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | /* create attrgetterobject structure */ |
| 646 | ag = PyObject_GC_New(attrgetterobject, &attrgetter_type); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 647 | if (ag == NULL) { |
| 648 | Py_DECREF(attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | return NULL; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 650 | } |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | ag->attr = attr; |
| 653 | ag->nattrs = nattrs; |
| 654 | |
| 655 | PyObject_GC_Track(ag); |
| 656 | return (PyObject *)ag; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static void |
| 660 | attrgetter_dealloc(attrgetterobject *ag) |
| 661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | PyObject_GC_UnTrack(ag); |
| 663 | Py_XDECREF(ag->attr); |
| 664 | PyObject_GC_Del(ag); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int |
| 668 | attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg) |
| 669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | Py_VISIT(ag->attr); |
| 671 | return 0; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static PyObject * |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 675 | dotted_getattr(PyObject *obj, PyObject *attr) |
| 676 | { |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 677 | PyObject *newobj; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 679 | /* attr is either a tuple or instance of str. |
| 680 | Ensured by the setup code of attrgetter_new */ |
| 681 | if (PyTuple_CheckExact(attr)) { /* chained getattr */ |
| 682 | Py_ssize_t name_idx = 0, name_count; |
| 683 | PyObject *attr_name; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 685 | name_count = PyTuple_GET_SIZE(attr); |
| 686 | Py_INCREF(obj); |
| 687 | for (name_idx = 0; name_idx < name_count; ++name_idx) { |
| 688 | attr_name = PyTuple_GET_ITEM(attr, name_idx); |
| 689 | newobj = PyObject_GetAttr(obj, attr_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | Py_DECREF(obj); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 691 | if (newobj == NULL) { |
| 692 | return NULL; |
| 693 | } |
| 694 | /* here */ |
| 695 | obj = newobj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | } |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 697 | } else { /* single getattr */ |
| 698 | newobj = PyObject_GetAttr(obj, attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | if (newobj == NULL) |
| 700 | return NULL; |
| 701 | obj = newobj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | } |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | return obj; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static PyObject * |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 708 | attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw) |
| 709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | PyObject *obj, *result; |
| 711 | Py_ssize_t i, nattrs=ag->nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj)) |
| 714 | return NULL; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 715 | if (ag->nattrs == 1) /* ag->attr is always a tuple */ |
| 716 | return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0)); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | assert(PyTuple_Check(ag->attr)); |
| 719 | assert(PyTuple_GET_SIZE(ag->attr) == nattrs); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | result = PyTuple_New(nattrs); |
| 722 | if (result == NULL) |
| 723 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | for (i=0 ; i < nattrs ; i++) { |
| 726 | PyObject *attr, *val; |
| 727 | attr = PyTuple_GET_ITEM(ag->attr, i); |
| 728 | val = dotted_getattr(obj, attr); |
| 729 | if (val == NULL) { |
| 730 | Py_DECREF(result); |
| 731 | return NULL; |
| 732 | } |
| 733 | PyTuple_SET_ITEM(result, i, val); |
| 734 | } |
| 735 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | PyDoc_STRVAR(attrgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 739 | "attrgetter(attr, ...) --> attrgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 740 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 741 | Return a callable object that fetches the given attribute(s) from its operand.\n\ |
| 742 | After, f=attrgetter('name'), the call f(r) returns r.name.\n\ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 743 | After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ |
| 744 | After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\ |
| 745 | (r.name.first, r.name.last)."); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 746 | |
| 747 | static PyTypeObject attrgetter_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | PyVarObject_HEAD_INIT(NULL, 0) |
| 749 | "operator.attrgetter", /* tp_name */ |
| 750 | sizeof(attrgetterobject), /* tp_basicsize */ |
| 751 | 0, /* tp_itemsize */ |
| 752 | /* methods */ |
| 753 | (destructor)attrgetter_dealloc, /* tp_dealloc */ |
| 754 | 0, /* tp_print */ |
| 755 | 0, /* tp_getattr */ |
| 756 | 0, /* tp_setattr */ |
| 757 | 0, /* tp_reserved */ |
| 758 | 0, /* tp_repr */ |
| 759 | 0, /* tp_as_number */ |
| 760 | 0, /* tp_as_sequence */ |
| 761 | 0, /* tp_as_mapping */ |
| 762 | 0, /* tp_hash */ |
| 763 | (ternaryfunc)attrgetter_call, /* tp_call */ |
| 764 | 0, /* tp_str */ |
| 765 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 766 | 0, /* tp_setattro */ |
| 767 | 0, /* tp_as_buffer */ |
| 768 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 769 | attrgetter_doc, /* tp_doc */ |
| 770 | (traverseproc)attrgetter_traverse, /* tp_traverse */ |
| 771 | 0, /* tp_clear */ |
| 772 | 0, /* tp_richcompare */ |
| 773 | 0, /* tp_weaklistoffset */ |
| 774 | 0, /* tp_iter */ |
| 775 | 0, /* tp_iternext */ |
| 776 | 0, /* tp_methods */ |
| 777 | 0, /* tp_members */ |
| 778 | 0, /* tp_getset */ |
| 779 | 0, /* tp_base */ |
| 780 | 0, /* tp_dict */ |
| 781 | 0, /* tp_descr_get */ |
| 782 | 0, /* tp_descr_set */ |
| 783 | 0, /* tp_dictoffset */ |
| 784 | 0, /* tp_init */ |
| 785 | 0, /* tp_alloc */ |
| 786 | attrgetter_new, /* tp_new */ |
| 787 | 0, /* tp_free */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 788 | }; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 789 | |
| 790 | |
| 791 | /* methodcaller object **********************************************************/ |
| 792 | |
| 793 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | PyObject_HEAD |
| 795 | PyObject *name; |
| 796 | PyObject *args; |
| 797 | PyObject *kwds; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 798 | } methodcallerobject; |
| 799 | |
| 800 | static PyTypeObject methodcaller_type; |
| 801 | |
| 802 | static PyObject * |
| 803 | methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | methodcallerobject *mc; |
| 806 | PyObject *name, *newargs; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | if (PyTuple_GET_SIZE(args) < 1) { |
| 809 | PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " |
| 810 | "one argument, the method name"); |
| 811 | return NULL; |
| 812 | } |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | /* create methodcallerobject structure */ |
| 815 | mc = PyObject_GC_New(methodcallerobject, &methodcaller_type); |
| 816 | if (mc == NULL) |
| 817 | return NULL; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 820 | if (newargs == NULL) { |
| 821 | Py_DECREF(mc); |
| 822 | return NULL; |
| 823 | } |
| 824 | mc->args = newargs; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | name = PyTuple_GET_ITEM(args, 0); |
| 827 | Py_INCREF(name); |
| 828 | mc->name = name; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | Py_XINCREF(kwds); |
| 831 | mc->kwds = kwds; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | PyObject_GC_Track(mc); |
| 834 | return (PyObject *)mc; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static void |
| 838 | methodcaller_dealloc(methodcallerobject *mc) |
| 839 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | PyObject_GC_UnTrack(mc); |
| 841 | Py_XDECREF(mc->name); |
| 842 | Py_XDECREF(mc->args); |
| 843 | Py_XDECREF(mc->kwds); |
| 844 | PyObject_GC_Del(mc); |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static int |
| 848 | methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) |
| 849 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | Py_VISIT(mc->args); |
| 851 | Py_VISIT(mc->kwds); |
| 852 | return 0; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static PyObject * |
| 856 | methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) |
| 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | PyObject *method, *obj, *result; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj)) |
| 861 | return NULL; |
| 862 | method = PyObject_GetAttr(obj, mc->name); |
| 863 | if (method == NULL) |
| 864 | return NULL; |
| 865 | result = PyObject_Call(method, mc->args, mc->kwds); |
| 866 | Py_DECREF(method); |
| 867 | return result; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | PyDoc_STRVAR(methodcaller_doc, |
| 871 | "methodcaller(name, ...) --> methodcaller object\n\ |
| 872 | \n\ |
| 873 | Return a callable object that calls the given method on its operand.\n\ |
| 874 | After, f = methodcaller('name'), the call f(r) returns r.name().\n\ |
| 875 | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ |
| 876 | r.name('date', foo=1)."); |
| 877 | |
| 878 | static PyTypeObject methodcaller_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | PyVarObject_HEAD_INIT(NULL, 0) |
| 880 | "operator.methodcaller", /* tp_name */ |
| 881 | sizeof(methodcallerobject), /* tp_basicsize */ |
| 882 | 0, /* tp_itemsize */ |
| 883 | /* methods */ |
| 884 | (destructor)methodcaller_dealloc, /* tp_dealloc */ |
| 885 | 0, /* tp_print */ |
| 886 | 0, /* tp_getattr */ |
| 887 | 0, /* tp_setattr */ |
| 888 | 0, /* tp_reserved */ |
| 889 | 0, /* tp_repr */ |
| 890 | 0, /* tp_as_number */ |
| 891 | 0, /* tp_as_sequence */ |
| 892 | 0, /* tp_as_mapping */ |
| 893 | 0, /* tp_hash */ |
| 894 | (ternaryfunc)methodcaller_call, /* tp_call */ |
| 895 | 0, /* tp_str */ |
| 896 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 897 | 0, /* tp_setattro */ |
| 898 | 0, /* tp_as_buffer */ |
| 899 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 900 | methodcaller_doc, /* tp_doc */ |
| 901 | (traverseproc)methodcaller_traverse, /* tp_traverse */ |
| 902 | 0, /* tp_clear */ |
| 903 | 0, /* tp_richcompare */ |
| 904 | 0, /* tp_weaklistoffset */ |
| 905 | 0, /* tp_iter */ |
| 906 | 0, /* tp_iternext */ |
| 907 | 0, /* tp_methods */ |
| 908 | 0, /* tp_members */ |
| 909 | 0, /* tp_getset */ |
| 910 | 0, /* tp_base */ |
| 911 | 0, /* tp_dict */ |
| 912 | 0, /* tp_descr_get */ |
| 913 | 0, /* tp_descr_set */ |
| 914 | 0, /* tp_dictoffset */ |
| 915 | 0, /* tp_init */ |
| 916 | 0, /* tp_alloc */ |
| 917 | methodcaller_new, /* tp_new */ |
| 918 | 0, /* tp_free */ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 919 | }; |
| 920 | |
| 921 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 922 | /* Initialization function for the module (*must* be called PyInit_operator) */ |
| 923 | |
| 924 | |
| 925 | static struct PyModuleDef operatormodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | PyModuleDef_HEAD_INIT, |
| 927 | "operator", |
| 928 | operator_doc, |
| 929 | -1, |
| 930 | operator_methods, |
| 931 | NULL, |
| 932 | NULL, |
| 933 | NULL, |
| 934 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 935 | }; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 936 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 937 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 938 | PyInit_operator(void) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | PyObject *m; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | /* Create the module and add the functions */ |
| 943 | m = PyModule_Create(&operatormodule); |
| 944 | if (m == NULL) |
| 945 | return NULL; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | if (PyType_Ready(&itemgetter_type) < 0) |
| 948 | return NULL; |
| 949 | Py_INCREF(&itemgetter_type); |
| 950 | PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type); |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | if (PyType_Ready(&attrgetter_type) < 0) |
| 953 | return NULL; |
| 954 | Py_INCREF(&attrgetter_type); |
| 955 | PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); |
| 956 | |
| 957 | if (PyType_Ready(&methodcaller_type) < 0) |
| 958 | return NULL; |
| 959 | Py_INCREF(&methodcaller_type); |
| 960 | PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type); |
| 961 | return m; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 962 | } |