blob: 25b0737dbfaa7bdeb50b1c7795f1a57eda589611 [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
Georg Brandl93b7d7e2012-06-24 13:54:51 +0200295 return PyBool_FromLong(rc);
Christian Heimes6cea6552012-06-24 13:48:32 +0200296}
297
298/* operator methods **********************************************************/
299
Neal Norwitz200788c2002-08-13 22:20:41 +0000300#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000301#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000303#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000304#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000306
307static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000308
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000309spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000310 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000311spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000312 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000313spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000314 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000315spam1(countOf,
316 "countOf(a, b) -- Return the number of times b occurs in a.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000317
Raymond Hettinger9543b342003-01-18 23:22:20 +0000318spam1(is_, "is_(a, b) -- Same as a is b.")
319spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000320spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000321spam2(add,__add__, "add(a, b) -- Same as a + b.")
322spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
323spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000324spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000325spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000326spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000327spam2o(neg,__neg__, "neg(a) -- Same as -a.")
328spam2o(pos,__pos__, "pos(a) -- Same as +a.")
329spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
330spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
331spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000332spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
333spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000334spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000335spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
336spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
337spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000338spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
339spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
340spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
341spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
342spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b")
343spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
344spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
345spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
346spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
347spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
348spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000349spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000350 "concat(a, b) -- Same as a + b, for a and b sequences.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000351spam2(iconcat,__iconcat__,
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000352 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000353spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000354 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000355spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000356 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000357spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000358 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000359spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000360spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000361spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
362spam2(le,__le__, "le(a, b) -- Same as a<=b.")
363spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
364spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
365spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
366spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000367
Christian Heimes6cea6552012-06-24 13:48:32 +0200368 {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,
369 compare_digest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 {NULL, NULL} /* sentinel */
Guido van Rossum037b9401996-07-30 16:55:54 +0000371
Guido van Rossum037b9401996-07-30 16:55:54 +0000372};
373
Raymond Hettinger166958b2003-12-01 13:18:39 +0000374/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000375
Raymond Hettinger166958b2003-12-01 13:18:39 +0000376typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject_HEAD
378 Py_ssize_t nitems;
379 PyObject *item;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000380} itemgetterobject;
381
382static PyTypeObject itemgetter_type;
383
384static PyObject *
385itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 itemgetterobject *ig;
388 PyObject *item;
389 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (!_PyArg_NoKeywords("itemgetter()", kwds))
392 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 nitems = PyTuple_GET_SIZE(args);
395 if (nitems <= 1) {
396 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
397 return NULL;
398 } else
399 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* create itemgetterobject structure */
402 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
403 if (ig == NULL)
404 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 Py_INCREF(item);
407 ig->item = item;
408 ig->nitems = nitems;
409
410 PyObject_GC_Track(ig);
411 return (PyObject *)ig;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000412}
413
414static void
415itemgetter_dealloc(itemgetterobject *ig)
416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 PyObject_GC_UnTrack(ig);
418 Py_XDECREF(ig->item);
419 PyObject_GC_Del(ig);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000420}
421
422static int
423itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 Py_VISIT(ig->item);
426 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000427}
428
429static PyObject *
430itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyObject *obj, *result;
433 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
436 return NULL;
437 if (nitems == 1)
438 return PyObject_GetItem(obj, ig->item);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 assert(PyTuple_Check(ig->item));
441 assert(PyTuple_GET_SIZE(ig->item) == nitems);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 result = PyTuple_New(nitems);
444 if (result == NULL)
445 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 for (i=0 ; i < nitems ; i++) {
448 PyObject *item, *val;
449 item = PyTuple_GET_ITEM(ig->item, i);
450 val = PyObject_GetItem(obj, item);
451 if (val == NULL) {
452 Py_DECREF(result);
453 return NULL;
454 }
455 PyTuple_SET_ITEM(result, i, val);
456 }
457 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000458}
459
460PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000461"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000462\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000463Return a callable object that fetches the given item(s) from its operand.\n\
464After, f=itemgetter(2), the call f(r) returns r[2].\n\
465After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000466
467static PyTypeObject itemgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyVarObject_HEAD_INIT(NULL, 0)
469 "operator.itemgetter", /* tp_name */
470 sizeof(itemgetterobject), /* tp_basicsize */
471 0, /* tp_itemsize */
472 /* methods */
473 (destructor)itemgetter_dealloc, /* tp_dealloc */
474 0, /* tp_print */
475 0, /* tp_getattr */
476 0, /* tp_setattr */
477 0, /* tp_reserved */
478 0, /* tp_repr */
479 0, /* tp_as_number */
480 0, /* tp_as_sequence */
481 0, /* tp_as_mapping */
482 0, /* tp_hash */
483 (ternaryfunc)itemgetter_call, /* tp_call */
484 0, /* tp_str */
485 PyObject_GenericGetAttr, /* tp_getattro */
486 0, /* tp_setattro */
487 0, /* tp_as_buffer */
488 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
489 itemgetter_doc, /* tp_doc */
490 (traverseproc)itemgetter_traverse, /* tp_traverse */
491 0, /* tp_clear */
492 0, /* tp_richcompare */
493 0, /* tp_weaklistoffset */
494 0, /* tp_iter */
495 0, /* tp_iternext */
496 0, /* tp_methods */
497 0, /* tp_members */
498 0, /* tp_getset */
499 0, /* tp_base */
500 0, /* tp_dict */
501 0, /* tp_descr_get */
502 0, /* tp_descr_set */
503 0, /* tp_dictoffset */
504 0, /* tp_init */
505 0, /* tp_alloc */
506 itemgetter_new, /* tp_new */
507 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000508};
509
510
511/* attrgetter object **********************************************************/
512
513typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyObject_HEAD
515 Py_ssize_t nattrs;
516 PyObject *attr;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000517} attrgetterobject;
518
519static PyTypeObject attrgetter_type;
520
521static PyObject *
522attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 attrgetterobject *ag;
525 PyObject *attr;
Antoine Pitroue9745712010-10-31 15:26:04 +0000526 Py_ssize_t nattrs, idx, char_idx;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (!_PyArg_NoKeywords("attrgetter()", kwds))
529 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 nattrs = PyTuple_GET_SIZE(args);
532 if (nattrs <= 1) {
533 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
534 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000535 }
536
537 attr = PyTuple_New(nattrs);
538 if (attr == NULL)
539 return NULL;
540
541 /* prepare attr while checking args */
542 for (idx = 0; idx < nattrs; ++idx) {
543 PyObject *item = PyTuple_GET_ITEM(args, idx);
544 Py_ssize_t item_len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200545 void *data;
546 unsigned int kind;
Antoine Pitroue9745712010-10-31 15:26:04 +0000547 int dot_count;
548
549 if (!PyUnicode_Check(item)) {
550 PyErr_SetString(PyExc_TypeError,
551 "attribute name must be a string");
552 Py_DECREF(attr);
553 return NULL;
554 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555 if (PyUnicode_READY(item)) {
556 Py_DECREF(attr);
557 return NULL;
558 }
559 item_len = PyUnicode_GET_LENGTH(item);
560 kind = PyUnicode_KIND(item);
561 data = PyUnicode_DATA(item);
Antoine Pitroue9745712010-10-31 15:26:04 +0000562
563 /* check whethere the string is dotted */
564 dot_count = 0;
565 for (char_idx = 0; char_idx < item_len; ++char_idx) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200566 if (PyUnicode_READ(kind, data, char_idx) == '.')
Antoine Pitroue9745712010-10-31 15:26:04 +0000567 ++dot_count;
568 }
569
570 if (dot_count == 0) {
571 Py_INCREF(item);
572 PyUnicode_InternInPlace(&item);
573 PyTuple_SET_ITEM(attr, idx, item);
574 } else { /* make it a tuple of non-dotted attrnames */
575 PyObject *attr_chain = PyTuple_New(dot_count + 1);
576 PyObject *attr_chain_item;
Antoine Pitrou87298c42010-10-31 21:03:01 +0000577 Py_ssize_t unibuff_from = 0;
578 Py_ssize_t unibuff_till = 0;
579 Py_ssize_t attr_chain_idx = 0;
Antoine Pitroue9745712010-10-31 15:26:04 +0000580
581 if (attr_chain == NULL) {
582 Py_DECREF(attr);
583 return NULL;
584 }
585
Antoine Pitroue9745712010-10-31 15:26:04 +0000586 for (; dot_count > 0; --dot_count) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
Antoine Pitroue9745712010-10-31 15:26:04 +0000588 ++unibuff_till;
589 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 attr_chain_item = PyUnicode_Substring(item,
591 unibuff_from,
592 unibuff_till);
Antoine Pitroue9745712010-10-31 15:26:04 +0000593 if (attr_chain_item == NULL) {
594 Py_DECREF(attr_chain);
595 Py_DECREF(attr);
596 return NULL;
597 }
598 PyUnicode_InternInPlace(&attr_chain_item);
599 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
600 ++attr_chain_idx;
601 unibuff_till = unibuff_from = unibuff_till + 1;
602 }
603
604 /* now add the last dotless name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200605 attr_chain_item = PyUnicode_Substring(item,
606 unibuff_from, item_len);
Antoine Pitroue9745712010-10-31 15:26:04 +0000607 if (attr_chain_item == NULL) {
608 Py_DECREF(attr_chain);
609 Py_DECREF(attr);
610 return NULL;
611 }
612 PyUnicode_InternInPlace(&attr_chain_item);
613 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
614
615 PyTuple_SET_ITEM(attr, idx, attr_chain);
616 }
617 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 /* create attrgetterobject structure */
620 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
Antoine Pitroue9745712010-10-31 15:26:04 +0000621 if (ag == NULL) {
622 Py_DECREF(attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000624 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 ag->attr = attr;
627 ag->nattrs = nattrs;
628
629 PyObject_GC_Track(ag);
630 return (PyObject *)ag;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000631}
632
633static void
634attrgetter_dealloc(attrgetterobject *ag)
635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyObject_GC_UnTrack(ag);
637 Py_XDECREF(ag->attr);
638 PyObject_GC_Del(ag);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000639}
640
641static int
642attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 Py_VISIT(ag->attr);
645 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000646}
647
648static PyObject *
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000649dotted_getattr(PyObject *obj, PyObject *attr)
650{
Antoine Pitroue9745712010-10-31 15:26:04 +0000651 PyObject *newobj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000652
Antoine Pitroue9745712010-10-31 15:26:04 +0000653 /* attr is either a tuple or instance of str.
654 Ensured by the setup code of attrgetter_new */
655 if (PyTuple_CheckExact(attr)) { /* chained getattr */
656 Py_ssize_t name_idx = 0, name_count;
657 PyObject *attr_name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000658
Antoine Pitroue9745712010-10-31 15:26:04 +0000659 name_count = PyTuple_GET_SIZE(attr);
660 Py_INCREF(obj);
661 for (name_idx = 0; name_idx < name_count; ++name_idx) {
662 attr_name = PyTuple_GET_ITEM(attr, name_idx);
663 newobj = PyObject_GetAttr(obj, attr_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 Py_DECREF(obj);
Antoine Pitroue9745712010-10-31 15:26:04 +0000665 if (newobj == NULL) {
666 return NULL;
667 }
668 /* here */
669 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 }
Antoine Pitroue9745712010-10-31 15:26:04 +0000671 } else { /* single getattr */
672 newobj = PyObject_GetAttr(obj, attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (newobj == NULL)
674 return NULL;
675 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return obj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000679}
680
681static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +0000682attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PyObject *obj, *result;
685 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
688 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000689 if (ag->nattrs == 1) /* ag->attr is always a tuple */
690 return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 assert(PyTuple_Check(ag->attr));
693 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 result = PyTuple_New(nattrs);
696 if (result == NULL)
697 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 for (i=0 ; i < nattrs ; i++) {
700 PyObject *attr, *val;
701 attr = PyTuple_GET_ITEM(ag->attr, i);
702 val = dotted_getattr(obj, attr);
703 if (val == NULL) {
704 Py_DECREF(result);
705 return NULL;
706 }
707 PyTuple_SET_ITEM(result, i, val);
708 }
709 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000710}
711
712PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000713"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000714\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000715Return a callable object that fetches the given attribute(s) from its operand.\n\
716After, f=attrgetter('name'), the call f(r) returns r.name.\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000717After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
718After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
719(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000720
721static PyTypeObject attrgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyVarObject_HEAD_INIT(NULL, 0)
723 "operator.attrgetter", /* tp_name */
724 sizeof(attrgetterobject), /* tp_basicsize */
725 0, /* tp_itemsize */
726 /* methods */
727 (destructor)attrgetter_dealloc, /* tp_dealloc */
728 0, /* tp_print */
729 0, /* tp_getattr */
730 0, /* tp_setattr */
731 0, /* tp_reserved */
732 0, /* tp_repr */
733 0, /* tp_as_number */
734 0, /* tp_as_sequence */
735 0, /* tp_as_mapping */
736 0, /* tp_hash */
737 (ternaryfunc)attrgetter_call, /* tp_call */
738 0, /* tp_str */
739 PyObject_GenericGetAttr, /* tp_getattro */
740 0, /* tp_setattro */
741 0, /* tp_as_buffer */
742 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
743 attrgetter_doc, /* tp_doc */
744 (traverseproc)attrgetter_traverse, /* tp_traverse */
745 0, /* tp_clear */
746 0, /* tp_richcompare */
747 0, /* tp_weaklistoffset */
748 0, /* tp_iter */
749 0, /* tp_iternext */
750 0, /* tp_methods */
751 0, /* tp_members */
752 0, /* tp_getset */
753 0, /* tp_base */
754 0, /* tp_dict */
755 0, /* tp_descr_get */
756 0, /* tp_descr_set */
757 0, /* tp_dictoffset */
758 0, /* tp_init */
759 0, /* tp_alloc */
760 attrgetter_new, /* tp_new */
761 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000762};
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000763
764
765/* methodcaller object **********************************************************/
766
767typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 PyObject_HEAD
769 PyObject *name;
770 PyObject *args;
771 PyObject *kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000772} methodcallerobject;
773
774static PyTypeObject methodcaller_type;
775
776static PyObject *
777methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 methodcallerobject *mc;
780 PyObject *name, *newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (PyTuple_GET_SIZE(args) < 1) {
783 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
784 "one argument, the method name");
785 return NULL;
786 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* create methodcallerobject structure */
789 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
790 if (mc == NULL)
791 return NULL;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
794 if (newargs == NULL) {
795 Py_DECREF(mc);
796 return NULL;
797 }
798 mc->args = newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 name = PyTuple_GET_ITEM(args, 0);
801 Py_INCREF(name);
802 mc->name = name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_XINCREF(kwds);
805 mc->kwds = kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject_GC_Track(mc);
808 return (PyObject *)mc;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000809}
810
811static void
812methodcaller_dealloc(methodcallerobject *mc)
813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject_GC_UnTrack(mc);
815 Py_XDECREF(mc->name);
816 Py_XDECREF(mc->args);
817 Py_XDECREF(mc->kwds);
818 PyObject_GC_Del(mc);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000819}
820
821static int
822methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 Py_VISIT(mc->args);
825 Py_VISIT(mc->kwds);
826 return 0;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000827}
828
829static PyObject *
830methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyObject *method, *obj, *result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
835 return NULL;
836 method = PyObject_GetAttr(obj, mc->name);
837 if (method == NULL)
838 return NULL;
839 result = PyObject_Call(method, mc->args, mc->kwds);
840 Py_DECREF(method);
841 return result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000842}
843
844PyDoc_STRVAR(methodcaller_doc,
845"methodcaller(name, ...) --> methodcaller object\n\
846\n\
847Return a callable object that calls the given method on its operand.\n\
848After, f = methodcaller('name'), the call f(r) returns r.name().\n\
849After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
850r.name('date', foo=1).");
851
852static PyTypeObject methodcaller_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 PyVarObject_HEAD_INIT(NULL, 0)
854 "operator.methodcaller", /* tp_name */
855 sizeof(methodcallerobject), /* tp_basicsize */
856 0, /* tp_itemsize */
857 /* methods */
858 (destructor)methodcaller_dealloc, /* tp_dealloc */
859 0, /* tp_print */
860 0, /* tp_getattr */
861 0, /* tp_setattr */
862 0, /* tp_reserved */
863 0, /* tp_repr */
864 0, /* tp_as_number */
865 0, /* tp_as_sequence */
866 0, /* tp_as_mapping */
867 0, /* tp_hash */
868 (ternaryfunc)methodcaller_call, /* tp_call */
869 0, /* tp_str */
870 PyObject_GenericGetAttr, /* tp_getattro */
871 0, /* tp_setattro */
872 0, /* tp_as_buffer */
873 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
874 methodcaller_doc, /* tp_doc */
875 (traverseproc)methodcaller_traverse, /* tp_traverse */
876 0, /* tp_clear */
877 0, /* tp_richcompare */
878 0, /* tp_weaklistoffset */
879 0, /* tp_iter */
880 0, /* tp_iternext */
881 0, /* tp_methods */
882 0, /* tp_members */
883 0, /* tp_getset */
884 0, /* tp_base */
885 0, /* tp_dict */
886 0, /* tp_descr_get */
887 0, /* tp_descr_set */
888 0, /* tp_dictoffset */
889 0, /* tp_init */
890 0, /* tp_alloc */
891 methodcaller_new, /* tp_new */
892 0, /* tp_free */
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000893};
894
895
Martin v. Löwis1a214512008-06-11 05:26:20 +0000896/* Initialization function for the module (*must* be called PyInit_operator) */
897
898
899static struct PyModuleDef operatormodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyModuleDef_HEAD_INIT,
901 "operator",
902 operator_doc,
903 -1,
904 operator_methods,
905 NULL,
906 NULL,
907 NULL,
908 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000909};
Guido van Rossum037b9401996-07-30 16:55:54 +0000910
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000911PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000912PyInit_operator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 PyObject *m;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* Create the module and add the functions */
917 m = PyModule_Create(&operatormodule);
918 if (m == NULL)
919 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (PyType_Ready(&itemgetter_type) < 0)
922 return NULL;
923 Py_INCREF(&itemgetter_type);
924 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (PyType_Ready(&attrgetter_type) < 0)
927 return NULL;
928 Py_INCREF(&attrgetter_type);
929 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
930
931 if (PyType_Ready(&methodcaller_type) < 0)
932 return NULL;
933 Py_INCREF(&methodcaller_type);
934 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
935 return m;
Guido van Rossum037b9401996-07-30 16:55:54 +0000936}