blob: c38ec1693a529b4839d92fe9c17c6305a09d93b0 [file] [log] [blame]
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001
2#include "Python.h"
3
4PyDoc_STRVAR(operator_doc,
5"Operator interface.\n\
Guido van Rossum037b9401996-07-30 16:55:54 +00006\n\
7This module exports a set of functions implemented in C corresponding\n\
8to the intrinsic operators of Python. For example, operator.add(x, y)\n\
9is equivalent to the expression x+y. The function names are those\n\
Benjamin Petersona0dfa822009-11-13 02:25:08 +000010used for special methods; variants without leading and trailing\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011'__' are also provided for convenience.");
Guido van Rossum037b9401996-07-30 16:55:54 +000012
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +000013#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000014 return AOP(a1); }
15
Fred Drake5639ba42000-07-08 04:12:08 +000016#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000017 PyObject *a1, *a2; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000018 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000019 return AOP(a1,a2); }
20
Fred Drake5639ba42000-07-08 04:12:08 +000021#define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000022 PyObject *a1; int a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000023 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000024 return AOP(a1,a2); }
25
Fred Drake5639ba42000-07-08 04:12:08 +000026#define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000027 PyObject *a1, *a2; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000028 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000029 if(-1 == AOP(a1,a2)) return NULL; \
30 Py_INCREF(Py_None); \
31 return Py_None; }
32
Fred Drake5639ba42000-07-08 04:12:08 +000033#define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000034 PyObject *a1, *a2, *a3; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000035 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000036 if(-1 == AOP(a1,a2,a3)) return NULL; \
37 Py_INCREF(Py_None); \
38 return Py_None; }
39
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +000040#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
41 long r; \
Guido van Rossum037b9401996-07-30 16:55:54 +000042 if(-1 == (r=AOP(a1))) return NULL; \
Guido van Rossum77f6a652002-04-03 22:41:51 +000043 return PyBool_FromLong(r); }
Guido van Rossum037b9401996-07-30 16:55:54 +000044
Fred Drake5639ba42000-07-08 04:12:08 +000045#define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000046 PyObject *a1, *a2; long r; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000047 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000048 if(-1 == (r=AOP(a1,a2))) return NULL; \
Christian Heimes217cfd12007-12-02 14:31:20 +000049 return PyLong_FromLong(r); }
Guido van Rossum037b9401996-07-30 16:55:54 +000050
Thomas Wouters477c8d52006-05-27 19:21:47 +000051#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 Heimes217cfd12007-12-02 14:31:20 +000055 return PyLong_FromSsize_t(r); }
Thomas Wouters477c8d52006-05-27 19:21:47 +000056
Guido van Rossum77f6a652002-04-03 22:41:51 +000057#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
58 PyObject *a1, *a2; long r; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000059 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
Guido van Rossum77f6a652002-04-03 22:41:51 +000060 if(-1 == (r=AOP(a1,a2))) return NULL; \
61 return PyBool_FromLong(r); }
62
Fred Drake428e75f2001-08-09 20:14:34 +000063#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
64 PyObject *a1, *a2; \
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000065 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
Fred Drake428e75f2001-08-09 20:14:34 +000066 return PyObject_RichCompare(a1,a2,A); }
67
Guido van Rossum037b9401996-07-30 16:55:54 +000068spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000069spam2(op_add , PyNumber_Add)
70spam2(op_sub , PyNumber_Subtract)
71spam2(op_mul , PyNumber_Multiply)
Fred Drake428e75f2001-08-09 20:14:34 +000072spam2(op_floordiv , PyNumber_FloorDivide)
73spam2(op_truediv , PyNumber_TrueDivide)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000074spam2(op_mod , PyNumber_Remainder)
75spam1(op_neg , PyNumber_Negative)
76spam1(op_pos , PyNumber_Positive)
77spam1(op_abs , PyNumber_Absolute)
78spam1(op_inv , PyNumber_Invert)
Fred Drakeea4d3f02000-09-17 16:09:27 +000079spam1(op_invert , PyNumber_Invert)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000080spam2(op_lshift , PyNumber_Lshift)
81spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +000082spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000083spam2(op_and_ , PyNumber_And)
84spam2(op_xor , PyNumber_Xor)
85spam2(op_or_ , PyNumber_Or)
Armin Rigof5bd3b42005-12-29 16:50:42 +000086spam2(op_iadd , PyNumber_InPlaceAdd)
87spam2(op_isub , PyNumber_InPlaceSubtract)
88spam2(op_imul , PyNumber_InPlaceMultiply)
Armin Rigof5bd3b42005-12-29 16:50:42 +000089spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
90spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
91spam2(op_imod , PyNumber_InPlaceRemainder)
92spam2(op_ilshift , PyNumber_InPlaceLshift)
93spam2(op_irshift , PyNumber_InPlaceRshift)
94spam2(op_iand , PyNumber_InPlaceAnd)
95spam2(op_ixor , PyNumber_InPlaceXor)
96spam2(op_ior , PyNumber_InPlaceOr)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000097spam2(op_concat , PySequence_Concat)
Armin Rigof5bd3b42005-12-29 16:50:42 +000098spam2(op_iconcat , PySequence_InPlaceConcat)
Guido van Rossum77f6a652002-04-03 22:41:51 +000099spami2b(op_contains , PySequence_Contains)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000100spamn2(indexOf , PySequence_Index)
101spamn2(countOf , PySequence_Count)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000102spam2(op_getitem , PyObject_GetItem)
103spam2n(op_delitem , PyObject_DelItem)
104spam3n(op_setitem , PyObject_SetItem)
Fred Drake428e75f2001-08-09 20:14:34 +0000105spamrc(op_lt , Py_LT)
106spamrc(op_le , Py_LE)
107spamrc(op_eq , Py_EQ)
108spamrc(op_ne , Py_NE)
109spamrc(op_gt , Py_GT)
110spamrc(op_ge , Py_GE)
Guido van Rossum037b9401996-07-30 16:55:54 +0000111
112static PyObject*
Raymond Hettinger5959c552002-08-19 03:19:09 +0000113op_pow(PyObject *s, PyObject *a)
114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 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 Hettinger5959c552002-08-19 03:19:09 +0000119}
120
121static PyObject*
Armin Rigof5bd3b42005-12-29 16:50:42 +0000122op_ipow(PyObject *s, PyObject *a)
123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 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 Rigof5bd3b42005-12-29 16:50:42 +0000128}
129
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000130static PyObject *
131op_index(PyObject *s, PyObject *a)
132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return PyNumber_Index(a);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000134}
135
Armin Rigof5bd3b42005-12-29 16:50:42 +0000136static PyObject*
Raymond Hettinger9543b342003-01-18 23:22:20 +0000137is_(PyObject *s, PyObject *a)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 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 Hettinger9543b342003-01-18 23:22:20 +0000145}
146
147static PyObject*
148is_not(PyObject *s, PyObject *a)
149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 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 Hettinger9543b342003-01-18 23:22:20 +0000156}
157
Guido van Rossum037b9401996-07-30 16:55:54 +0000158#undef spam1
159#undef spam2
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000160#undef spam1o
161#undef spam1o
Christian Heimes6cea6552012-06-24 13:48:32 +0200162
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 */
173static 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
211PyDoc_STRVAR(compare_digest__doc__,
212"compare_digest(a, b) -> bool\n"
213"\n"
214"Return the equivalent of 'a == b', but avoid any short circuiting to\n"
215"counterfeit timing analysis of input data. The function should be used to\n"
216"compare cryptographic secrets. a and b must both either support the buffer\n"
217"protocol (e.g. bytes) or be ASCII only str instances at the same time.\n"
218"\n"
219"Note: In case of an error or different lengths the function may disclose\n"
220"some timing information about the types and lengths of a and b.\n");
221
222
223static PyObject*
224compare_digest(PyObject *self, PyObject *args)
225{
226 PyObject *a, *b;
227 int rc;
228 PyObject *result;
229
230 if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) {
231 return NULL;
232 }
233
234 /* ASCII unicode string */
235 if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
236 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
237 return NULL;
238 }
239 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
240 PyErr_SetString(PyExc_TypeError,
241 "comparing strings with non-ASCII characters is "
242 "not supported");
243 return NULL;
244 }
245
246 rc = _tscmp(PyUnicode_DATA(a),
247 PyUnicode_DATA(b),
248 PyUnicode_GET_LENGTH(a),
249 PyUnicode_GET_LENGTH(b));
250 }
251 /* fallback to buffer interface for bytes, bytesarray and other */
252 else {
253 Py_buffer view_a;
254 Py_buffer view_b;
255
256 if ((PyObject_CheckBuffer(a) == 0) & (PyObject_CheckBuffer(b) == 0)) {
257 PyErr_Format(PyExc_TypeError,
258 "unsupported operand types(s) or combination of types: "
259 "'%.100s' and '%.100s'",
260 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
261 return NULL;
262 }
263
264 if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
265 return NULL;
266 }
267 if (view_a.ndim > 1) {
268 PyErr_SetString(PyExc_BufferError,
269 "Buffer must be single dimension");
270 PyBuffer_Release(&view_a);
271 return NULL;
272 }
273
274 if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
275 PyBuffer_Release(&view_a);
276 return NULL;
277 }
278 if (view_b.ndim > 1) {
279 PyErr_SetString(PyExc_BufferError,
280 "Buffer must be single dimension");
281 PyBuffer_Release(&view_a);
282 PyBuffer_Release(&view_b);
283 return NULL;
284 }
285
286 rc = _tscmp((const unsigned char*)view_a.buf,
287 (const unsigned char*)view_b.buf,
288 view_a.len,
289 view_b.len);
290
291 PyBuffer_Release(&view_a);
292 PyBuffer_Release(&view_b);
293 }
294
295 result = PyBool_FromLong(rc);
296 Py_INCREF(result);
297 return result;
298}
299
300/* operator methods **********************************************************/
301
Neal Norwitz200788c2002-08-13 22:20:41 +0000302#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000303#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000305#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000306#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000308
309static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000310
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000311spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000312 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000313spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000314 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000315spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000316 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000317spam1(countOf,
318 "countOf(a, b) -- Return the number of times b occurs in a.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000319
Raymond Hettinger9543b342003-01-18 23:22:20 +0000320spam1(is_, "is_(a, b) -- Same as a is b.")
321spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000322spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000323spam2(add,__add__, "add(a, b) -- Same as a + b.")
324spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
325spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000326spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000327spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000328spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000329spam2o(neg,__neg__, "neg(a) -- Same as -a.")
330spam2o(pos,__pos__, "pos(a) -- Same as +a.")
331spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
332spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
333spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000334spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
335spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000336spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000337spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
338spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
339spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000340spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
341spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
342spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
343spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
344spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b")
345spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
346spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
347spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
348spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
349spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
350spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000351spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000352 "concat(a, b) -- Same as a + b, for a and b sequences.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000353spam2(iconcat,__iconcat__,
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000354 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000355spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000356 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000357spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000358 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000359spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000360 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000361spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000362spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000363spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
364spam2(le,__le__, "le(a, b) -- Same as a<=b.")
365spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
366spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
367spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
368spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000369
Christian Heimes6cea6552012-06-24 13:48:32 +0200370 {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,
371 compare_digest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 {NULL, NULL} /* sentinel */
Guido van Rossum037b9401996-07-30 16:55:54 +0000373
Guido van Rossum037b9401996-07-30 16:55:54 +0000374};
375
Raymond Hettinger166958b2003-12-01 13:18:39 +0000376/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000377
Raymond Hettinger166958b2003-12-01 13:18:39 +0000378typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyObject_HEAD
380 Py_ssize_t nitems;
381 PyObject *item;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000382} itemgetterobject;
383
384static PyTypeObject itemgetter_type;
385
386static PyObject *
387itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 itemgetterobject *ig;
390 PyObject *item;
391 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (!_PyArg_NoKeywords("itemgetter()", kwds))
394 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 nitems = PyTuple_GET_SIZE(args);
397 if (nitems <= 1) {
398 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
399 return NULL;
400 } else
401 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 /* create itemgetterobject structure */
404 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
405 if (ig == NULL)
406 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 Py_INCREF(item);
409 ig->item = item;
410 ig->nitems = nitems;
411
412 PyObject_GC_Track(ig);
413 return (PyObject *)ig;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000414}
415
416static void
417itemgetter_dealloc(itemgetterobject *ig)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyObject_GC_UnTrack(ig);
420 Py_XDECREF(ig->item);
421 PyObject_GC_Del(ig);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000422}
423
424static int
425itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 Py_VISIT(ig->item);
428 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000429}
430
431static PyObject *
432itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyObject *obj, *result;
435 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
438 return NULL;
439 if (nitems == 1)
440 return PyObject_GetItem(obj, ig->item);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 assert(PyTuple_Check(ig->item));
443 assert(PyTuple_GET_SIZE(ig->item) == nitems);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 result = PyTuple_New(nitems);
446 if (result == NULL)
447 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 for (i=0 ; i < nitems ; i++) {
450 PyObject *item, *val;
451 item = PyTuple_GET_ITEM(ig->item, i);
452 val = PyObject_GetItem(obj, item);
453 if (val == NULL) {
454 Py_DECREF(result);
455 return NULL;
456 }
457 PyTuple_SET_ITEM(result, i, val);
458 }
459 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000460}
461
462PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000463"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000464\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000465Return a callable object that fetches the given item(s) from its operand.\n\
466After, f=itemgetter(2), the call f(r) returns r[2].\n\
467After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000468
469static PyTypeObject itemgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyVarObject_HEAD_INIT(NULL, 0)
471 "operator.itemgetter", /* tp_name */
472 sizeof(itemgetterobject), /* tp_basicsize */
473 0, /* tp_itemsize */
474 /* methods */
475 (destructor)itemgetter_dealloc, /* tp_dealloc */
476 0, /* tp_print */
477 0, /* tp_getattr */
478 0, /* tp_setattr */
479 0, /* tp_reserved */
480 0, /* tp_repr */
481 0, /* tp_as_number */
482 0, /* tp_as_sequence */
483 0, /* tp_as_mapping */
484 0, /* tp_hash */
485 (ternaryfunc)itemgetter_call, /* tp_call */
486 0, /* tp_str */
487 PyObject_GenericGetAttr, /* tp_getattro */
488 0, /* tp_setattro */
489 0, /* tp_as_buffer */
490 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
491 itemgetter_doc, /* tp_doc */
492 (traverseproc)itemgetter_traverse, /* tp_traverse */
493 0, /* tp_clear */
494 0, /* tp_richcompare */
495 0, /* tp_weaklistoffset */
496 0, /* tp_iter */
497 0, /* tp_iternext */
498 0, /* tp_methods */
499 0, /* tp_members */
500 0, /* tp_getset */
501 0, /* tp_base */
502 0, /* tp_dict */
503 0, /* tp_descr_get */
504 0, /* tp_descr_set */
505 0, /* tp_dictoffset */
506 0, /* tp_init */
507 0, /* tp_alloc */
508 itemgetter_new, /* tp_new */
509 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000510};
511
512
513/* attrgetter object **********************************************************/
514
515typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 PyObject_HEAD
517 Py_ssize_t nattrs;
518 PyObject *attr;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000519} attrgetterobject;
520
521static PyTypeObject attrgetter_type;
522
523static PyObject *
524attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 attrgetterobject *ag;
527 PyObject *attr;
Antoine Pitroue9745712010-10-31 15:26:04 +0000528 Py_ssize_t nattrs, idx, char_idx;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (!_PyArg_NoKeywords("attrgetter()", kwds))
531 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 nattrs = PyTuple_GET_SIZE(args);
534 if (nattrs <= 1) {
535 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
536 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000537 }
538
539 attr = PyTuple_New(nattrs);
540 if (attr == NULL)
541 return NULL;
542
543 /* prepare attr while checking args */
544 for (idx = 0; idx < nattrs; ++idx) {
545 PyObject *item = PyTuple_GET_ITEM(args, idx);
546 Py_ssize_t item_len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200547 void *data;
548 unsigned int kind;
Antoine Pitroue9745712010-10-31 15:26:04 +0000549 int dot_count;
550
551 if (!PyUnicode_Check(item)) {
552 PyErr_SetString(PyExc_TypeError,
553 "attribute name must be a string");
554 Py_DECREF(attr);
555 return NULL;
556 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200557 if (PyUnicode_READY(item)) {
558 Py_DECREF(attr);
559 return NULL;
560 }
561 item_len = PyUnicode_GET_LENGTH(item);
562 kind = PyUnicode_KIND(item);
563 data = PyUnicode_DATA(item);
Antoine Pitroue9745712010-10-31 15:26:04 +0000564
565 /* check whethere the string is dotted */
566 dot_count = 0;
567 for (char_idx = 0; char_idx < item_len; ++char_idx) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200568 if (PyUnicode_READ(kind, data, char_idx) == '.')
Antoine Pitroue9745712010-10-31 15:26:04 +0000569 ++dot_count;
570 }
571
572 if (dot_count == 0) {
573 Py_INCREF(item);
574 PyUnicode_InternInPlace(&item);
575 PyTuple_SET_ITEM(attr, idx, item);
576 } else { /* make it a tuple of non-dotted attrnames */
577 PyObject *attr_chain = PyTuple_New(dot_count + 1);
578 PyObject *attr_chain_item;
Antoine Pitrou87298c42010-10-31 21:03:01 +0000579 Py_ssize_t unibuff_from = 0;
580 Py_ssize_t unibuff_till = 0;
581 Py_ssize_t attr_chain_idx = 0;
Antoine Pitroue9745712010-10-31 15:26:04 +0000582
583 if (attr_chain == NULL) {
584 Py_DECREF(attr);
585 return NULL;
586 }
587
Antoine Pitroue9745712010-10-31 15:26:04 +0000588 for (; dot_count > 0; --dot_count) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
Antoine Pitroue9745712010-10-31 15:26:04 +0000590 ++unibuff_till;
591 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 attr_chain_item = PyUnicode_Substring(item,
593 unibuff_from,
594 unibuff_till);
Antoine Pitroue9745712010-10-31 15:26:04 +0000595 if (attr_chain_item == NULL) {
596 Py_DECREF(attr_chain);
597 Py_DECREF(attr);
598 return NULL;
599 }
600 PyUnicode_InternInPlace(&attr_chain_item);
601 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
602 ++attr_chain_idx;
603 unibuff_till = unibuff_from = unibuff_till + 1;
604 }
605
606 /* now add the last dotless name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200607 attr_chain_item = PyUnicode_Substring(item,
608 unibuff_from, item_len);
Antoine Pitroue9745712010-10-31 15:26:04 +0000609 if (attr_chain_item == NULL) {
610 Py_DECREF(attr_chain);
611 Py_DECREF(attr);
612 return NULL;
613 }
614 PyUnicode_InternInPlace(&attr_chain_item);
615 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
616
617 PyTuple_SET_ITEM(attr, idx, attr_chain);
618 }
619 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 /* create attrgetterobject structure */
622 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
Antoine Pitroue9745712010-10-31 15:26:04 +0000623 if (ag == NULL) {
624 Py_DECREF(attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000626 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 ag->attr = attr;
629 ag->nattrs = nattrs;
630
631 PyObject_GC_Track(ag);
632 return (PyObject *)ag;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000633}
634
635static void
636attrgetter_dealloc(attrgetterobject *ag)
637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject_GC_UnTrack(ag);
639 Py_XDECREF(ag->attr);
640 PyObject_GC_Del(ag);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000641}
642
643static int
644attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 Py_VISIT(ag->attr);
647 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000648}
649
650static PyObject *
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000651dotted_getattr(PyObject *obj, PyObject *attr)
652{
Antoine Pitroue9745712010-10-31 15:26:04 +0000653 PyObject *newobj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000654
Antoine Pitroue9745712010-10-31 15:26:04 +0000655 /* attr is either a tuple or instance of str.
656 Ensured by the setup code of attrgetter_new */
657 if (PyTuple_CheckExact(attr)) { /* chained getattr */
658 Py_ssize_t name_idx = 0, name_count;
659 PyObject *attr_name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000660
Antoine Pitroue9745712010-10-31 15:26:04 +0000661 name_count = PyTuple_GET_SIZE(attr);
662 Py_INCREF(obj);
663 for (name_idx = 0; name_idx < name_count; ++name_idx) {
664 attr_name = PyTuple_GET_ITEM(attr, name_idx);
665 newobj = PyObject_GetAttr(obj, attr_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_DECREF(obj);
Antoine Pitroue9745712010-10-31 15:26:04 +0000667 if (newobj == NULL) {
668 return NULL;
669 }
670 /* here */
671 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
Antoine Pitroue9745712010-10-31 15:26:04 +0000673 } else { /* single getattr */
674 newobj = PyObject_GetAttr(obj, attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 if (newobj == NULL)
676 return NULL;
677 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return obj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000681}
682
683static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +0000684attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyObject *obj, *result;
687 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
690 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000691 if (ag->nattrs == 1) /* ag->attr is always a tuple */
692 return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 assert(PyTuple_Check(ag->attr));
695 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 result = PyTuple_New(nattrs);
698 if (result == NULL)
699 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 for (i=0 ; i < nattrs ; i++) {
702 PyObject *attr, *val;
703 attr = PyTuple_GET_ITEM(ag->attr, i);
704 val = dotted_getattr(obj, attr);
705 if (val == NULL) {
706 Py_DECREF(result);
707 return NULL;
708 }
709 PyTuple_SET_ITEM(result, i, val);
710 }
711 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000712}
713
714PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000715"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000716\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000717Return a callable object that fetches the given attribute(s) from its operand.\n\
718After, f=attrgetter('name'), the call f(r) returns r.name.\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000719After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
720After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
721(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000722
723static PyTypeObject attrgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 PyVarObject_HEAD_INIT(NULL, 0)
725 "operator.attrgetter", /* tp_name */
726 sizeof(attrgetterobject), /* tp_basicsize */
727 0, /* tp_itemsize */
728 /* methods */
729 (destructor)attrgetter_dealloc, /* tp_dealloc */
730 0, /* tp_print */
731 0, /* tp_getattr */
732 0, /* tp_setattr */
733 0, /* tp_reserved */
734 0, /* tp_repr */
735 0, /* tp_as_number */
736 0, /* tp_as_sequence */
737 0, /* tp_as_mapping */
738 0, /* tp_hash */
739 (ternaryfunc)attrgetter_call, /* tp_call */
740 0, /* tp_str */
741 PyObject_GenericGetAttr, /* tp_getattro */
742 0, /* tp_setattro */
743 0, /* tp_as_buffer */
744 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
745 attrgetter_doc, /* tp_doc */
746 (traverseproc)attrgetter_traverse, /* tp_traverse */
747 0, /* tp_clear */
748 0, /* tp_richcompare */
749 0, /* tp_weaklistoffset */
750 0, /* tp_iter */
751 0, /* tp_iternext */
752 0, /* tp_methods */
753 0, /* tp_members */
754 0, /* tp_getset */
755 0, /* tp_base */
756 0, /* tp_dict */
757 0, /* tp_descr_get */
758 0, /* tp_descr_set */
759 0, /* tp_dictoffset */
760 0, /* tp_init */
761 0, /* tp_alloc */
762 attrgetter_new, /* tp_new */
763 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000764};
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000765
766
767/* methodcaller object **********************************************************/
768
769typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject_HEAD
771 PyObject *name;
772 PyObject *args;
773 PyObject *kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000774} methodcallerobject;
775
776static PyTypeObject methodcaller_type;
777
778static PyObject *
779methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 methodcallerobject *mc;
782 PyObject *name, *newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (PyTuple_GET_SIZE(args) < 1) {
785 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
786 "one argument, the method name");
787 return NULL;
788 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 /* create methodcallerobject structure */
791 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
792 if (mc == NULL)
793 return NULL;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
796 if (newargs == NULL) {
797 Py_DECREF(mc);
798 return NULL;
799 }
800 mc->args = newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 name = PyTuple_GET_ITEM(args, 0);
803 Py_INCREF(name);
804 mc->name = name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_XINCREF(kwds);
807 mc->kwds = kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject_GC_Track(mc);
810 return (PyObject *)mc;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000811}
812
813static void
814methodcaller_dealloc(methodcallerobject *mc)
815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject_GC_UnTrack(mc);
817 Py_XDECREF(mc->name);
818 Py_XDECREF(mc->args);
819 Py_XDECREF(mc->kwds);
820 PyObject_GC_Del(mc);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000821}
822
823static int
824methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 Py_VISIT(mc->args);
827 Py_VISIT(mc->kwds);
828 return 0;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000829}
830
831static PyObject *
832methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 PyObject *method, *obj, *result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
837 return NULL;
838 method = PyObject_GetAttr(obj, mc->name);
839 if (method == NULL)
840 return NULL;
841 result = PyObject_Call(method, mc->args, mc->kwds);
842 Py_DECREF(method);
843 return result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000844}
845
846PyDoc_STRVAR(methodcaller_doc,
847"methodcaller(name, ...) --> methodcaller object\n\
848\n\
849Return a callable object that calls the given method on its operand.\n\
850After, f = methodcaller('name'), the call f(r) returns r.name().\n\
851After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
852r.name('date', foo=1).");
853
854static PyTypeObject methodcaller_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyVarObject_HEAD_INIT(NULL, 0)
856 "operator.methodcaller", /* tp_name */
857 sizeof(methodcallerobject), /* tp_basicsize */
858 0, /* tp_itemsize */
859 /* methods */
860 (destructor)methodcaller_dealloc, /* tp_dealloc */
861 0, /* tp_print */
862 0, /* tp_getattr */
863 0, /* tp_setattr */
864 0, /* tp_reserved */
865 0, /* tp_repr */
866 0, /* tp_as_number */
867 0, /* tp_as_sequence */
868 0, /* tp_as_mapping */
869 0, /* tp_hash */
870 (ternaryfunc)methodcaller_call, /* tp_call */
871 0, /* tp_str */
872 PyObject_GenericGetAttr, /* tp_getattro */
873 0, /* tp_setattro */
874 0, /* tp_as_buffer */
875 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
876 methodcaller_doc, /* tp_doc */
877 (traverseproc)methodcaller_traverse, /* tp_traverse */
878 0, /* tp_clear */
879 0, /* tp_richcompare */
880 0, /* tp_weaklistoffset */
881 0, /* tp_iter */
882 0, /* tp_iternext */
883 0, /* tp_methods */
884 0, /* tp_members */
885 0, /* tp_getset */
886 0, /* tp_base */
887 0, /* tp_dict */
888 0, /* tp_descr_get */
889 0, /* tp_descr_set */
890 0, /* tp_dictoffset */
891 0, /* tp_init */
892 0, /* tp_alloc */
893 methodcaller_new, /* tp_new */
894 0, /* tp_free */
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000895};
896
897
Martin v. Löwis1a214512008-06-11 05:26:20 +0000898/* Initialization function for the module (*must* be called PyInit_operator) */
899
900
901static struct PyModuleDef operatormodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyModuleDef_HEAD_INIT,
903 "operator",
904 operator_doc,
905 -1,
906 operator_methods,
907 NULL,
908 NULL,
909 NULL,
910 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000911};
Guido van Rossum037b9401996-07-30 16:55:54 +0000912
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000913PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000914PyInit_operator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject *m;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Create the module and add the functions */
919 m = PyModule_Create(&operatormodule);
920 if (m == NULL)
921 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (PyType_Ready(&itemgetter_type) < 0)
924 return NULL;
925 Py_INCREF(&itemgetter_type);
926 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (PyType_Ready(&attrgetter_type) < 0)
929 return NULL;
930 Py_INCREF(&attrgetter_type);
931 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
932
933 if (PyType_Ready(&methodcaller_type) < 0)
934 return NULL;
935 Py_INCREF(&methodcaller_type);
936 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
937 return m;
Guido van Rossum037b9401996-07-30 16:55:54 +0000938}