blob: 12fdad54d605b9208f54ee7338d256ef38c511c9 [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"
Georg Brandla1bc35f2012-06-24 16:07:33 +0200214"Return 'a == b'. This function uses an approach designed to prevent\n"
215"timing analysis, making it appropriate for cryptography.\n"
216"a and b must both be of the same type: either str (ASCII only),\n"
217"or any type that supports the buffer protocol (e.g. bytes).\n"
Christian Heimes6cea6552012-06-24 13:48:32 +0200218"\n"
Georg Brandla1bc35f2012-06-24 16:07:33 +0200219"Note: If a and b are of different lengths, or if an error occurs,\n"
Larry Hastings48986d62012-06-25 00:59:34 -0700220"a timing attack could theoretically reveal information about the\n"
221"types and lengths of a and b--but not their values.\n");
Christian Heimes6cea6552012-06-24 13:48:32 +0200222
223static PyObject*
224compare_digest(PyObject *self, PyObject *args)
225{
226 PyObject *a, *b;
227 int rc;
Christian Heimes6cea6552012-06-24 13:48:32 +0200228
229 if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) {
230 return NULL;
231 }
232
233 /* ASCII unicode string */
234 if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
235 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
236 return NULL;
237 }
238 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
239 PyErr_SetString(PyExc_TypeError,
240 "comparing strings with non-ASCII characters is "
241 "not supported");
242 return NULL;
243 }
244
245 rc = _tscmp(PyUnicode_DATA(a),
246 PyUnicode_DATA(b),
247 PyUnicode_GET_LENGTH(a),
248 PyUnicode_GET_LENGTH(b));
249 }
250 /* fallback to buffer interface for bytes, bytesarray and other */
251 else {
252 Py_buffer view_a;
253 Py_buffer view_b;
254
255 if ((PyObject_CheckBuffer(a) == 0) & (PyObject_CheckBuffer(b) == 0)) {
256 PyErr_Format(PyExc_TypeError,
257 "unsupported operand types(s) or combination of types: "
258 "'%.100s' and '%.100s'",
259 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
260 return NULL;
261 }
262
263 if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
264 return NULL;
265 }
266 if (view_a.ndim > 1) {
267 PyErr_SetString(PyExc_BufferError,
268 "Buffer must be single dimension");
269 PyBuffer_Release(&view_a);
270 return NULL;
271 }
272
273 if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
274 PyBuffer_Release(&view_a);
275 return NULL;
276 }
277 if (view_b.ndim > 1) {
278 PyErr_SetString(PyExc_BufferError,
279 "Buffer must be single dimension");
280 PyBuffer_Release(&view_a);
281 PyBuffer_Release(&view_b);
282 return NULL;
283 }
284
285 rc = _tscmp((const unsigned char*)view_a.buf,
286 (const unsigned char*)view_b.buf,
287 view_a.len,
288 view_b.len);
289
290 PyBuffer_Release(&view_a);
291 PyBuffer_Release(&view_b);
292 }
293
Georg Brandl93b7d7e2012-06-24 13:54:51 +0200294 return PyBool_FromLong(rc);
Christian Heimes6cea6552012-06-24 13:48:32 +0200295}
296
297/* operator methods **********************************************************/
298
Neal Norwitz200788c2002-08-13 22:20:41 +0000299#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000300#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000302#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000303#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000305
306static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000307
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000308spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000309 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000310spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000311 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000312spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000313 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000314spam1(countOf,
315 "countOf(a, b) -- Return the number of times b occurs in a.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000316
Raymond Hettinger9543b342003-01-18 23:22:20 +0000317spam1(is_, "is_(a, b) -- Same as a is b.")
318spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000319spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000320spam2(add,__add__, "add(a, b) -- Same as a + b.")
321spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
322spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000323spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000324spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000325spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000326spam2o(neg,__neg__, "neg(a) -- Same as -a.")
327spam2o(pos,__pos__, "pos(a) -- Same as +a.")
328spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
329spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
330spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000331spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
332spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000333spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000334spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
335spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
336spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000337spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
338spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
339spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
340spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
341spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b")
342spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
343spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
344spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
345spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
346spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
347spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000348spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000349 "concat(a, b) -- Same as a + b, for a and b sequences.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000350spam2(iconcat,__iconcat__,
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000351 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000352spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000353 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000354spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000355 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000356spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000357 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000358spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000359spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000360spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
361spam2(le,__le__, "le(a, b) -- Same as a<=b.")
362spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
363spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
364spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
365spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000366
Christian Heimes6cea6552012-06-24 13:48:32 +0200367 {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,
368 compare_digest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 {NULL, NULL} /* sentinel */
Guido van Rossum037b9401996-07-30 16:55:54 +0000370
Guido van Rossum037b9401996-07-30 16:55:54 +0000371};
372
Raymond Hettinger166958b2003-12-01 13:18:39 +0000373/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000374
Raymond Hettinger166958b2003-12-01 13:18:39 +0000375typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject_HEAD
377 Py_ssize_t nitems;
378 PyObject *item;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000379} itemgetterobject;
380
381static PyTypeObject itemgetter_type;
382
383static PyObject *
384itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 itemgetterobject *ig;
387 PyObject *item;
388 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (!_PyArg_NoKeywords("itemgetter()", kwds))
391 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 nitems = PyTuple_GET_SIZE(args);
394 if (nitems <= 1) {
395 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
396 return NULL;
397 } else
398 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* create itemgetterobject structure */
401 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
402 if (ig == NULL)
403 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 Py_INCREF(item);
406 ig->item = item;
407 ig->nitems = nitems;
408
409 PyObject_GC_Track(ig);
410 return (PyObject *)ig;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000411}
412
413static void
414itemgetter_dealloc(itemgetterobject *ig)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 PyObject_GC_UnTrack(ig);
417 Py_XDECREF(ig->item);
418 PyObject_GC_Del(ig);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000419}
420
421static int
422itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 Py_VISIT(ig->item);
425 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000426}
427
428static PyObject *
429itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyObject *obj, *result;
432 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
435 return NULL;
436 if (nitems == 1)
437 return PyObject_GetItem(obj, ig->item);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 assert(PyTuple_Check(ig->item));
440 assert(PyTuple_GET_SIZE(ig->item) == nitems);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 result = PyTuple_New(nitems);
443 if (result == NULL)
444 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 for (i=0 ; i < nitems ; i++) {
447 PyObject *item, *val;
448 item = PyTuple_GET_ITEM(ig->item, i);
449 val = PyObject_GetItem(obj, item);
450 if (val == NULL) {
451 Py_DECREF(result);
452 return NULL;
453 }
454 PyTuple_SET_ITEM(result, i, val);
455 }
456 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000457}
458
459PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000460"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000461\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000462Return a callable object that fetches the given item(s) from its operand.\n\
463After, f=itemgetter(2), the call f(r) returns r[2].\n\
464After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000465
466static PyTypeObject itemgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyVarObject_HEAD_INIT(NULL, 0)
468 "operator.itemgetter", /* tp_name */
469 sizeof(itemgetterobject), /* tp_basicsize */
470 0, /* tp_itemsize */
471 /* methods */
472 (destructor)itemgetter_dealloc, /* tp_dealloc */
473 0, /* tp_print */
474 0, /* tp_getattr */
475 0, /* tp_setattr */
476 0, /* tp_reserved */
477 0, /* tp_repr */
478 0, /* tp_as_number */
479 0, /* tp_as_sequence */
480 0, /* tp_as_mapping */
481 0, /* tp_hash */
482 (ternaryfunc)itemgetter_call, /* tp_call */
483 0, /* tp_str */
484 PyObject_GenericGetAttr, /* tp_getattro */
485 0, /* tp_setattro */
486 0, /* tp_as_buffer */
487 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
488 itemgetter_doc, /* tp_doc */
489 (traverseproc)itemgetter_traverse, /* tp_traverse */
490 0, /* tp_clear */
491 0, /* tp_richcompare */
492 0, /* tp_weaklistoffset */
493 0, /* tp_iter */
494 0, /* tp_iternext */
495 0, /* tp_methods */
496 0, /* tp_members */
497 0, /* tp_getset */
498 0, /* tp_base */
499 0, /* tp_dict */
500 0, /* tp_descr_get */
501 0, /* tp_descr_set */
502 0, /* tp_dictoffset */
503 0, /* tp_init */
504 0, /* tp_alloc */
505 itemgetter_new, /* tp_new */
506 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000507};
508
509
510/* attrgetter object **********************************************************/
511
512typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 PyObject_HEAD
514 Py_ssize_t nattrs;
515 PyObject *attr;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000516} attrgetterobject;
517
518static PyTypeObject attrgetter_type;
519
520static PyObject *
521attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 attrgetterobject *ag;
524 PyObject *attr;
Antoine Pitroue9745712010-10-31 15:26:04 +0000525 Py_ssize_t nattrs, idx, char_idx;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (!_PyArg_NoKeywords("attrgetter()", kwds))
528 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 nattrs = PyTuple_GET_SIZE(args);
531 if (nattrs <= 1) {
532 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
533 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000534 }
535
536 attr = PyTuple_New(nattrs);
537 if (attr == NULL)
538 return NULL;
539
540 /* prepare attr while checking args */
541 for (idx = 0; idx < nattrs; ++idx) {
542 PyObject *item = PyTuple_GET_ITEM(args, idx);
543 Py_ssize_t item_len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200544 void *data;
545 unsigned int kind;
Antoine Pitroue9745712010-10-31 15:26:04 +0000546 int dot_count;
547
548 if (!PyUnicode_Check(item)) {
549 PyErr_SetString(PyExc_TypeError,
550 "attribute name must be a string");
551 Py_DECREF(attr);
552 return NULL;
553 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200554 if (PyUnicode_READY(item)) {
555 Py_DECREF(attr);
556 return NULL;
557 }
558 item_len = PyUnicode_GET_LENGTH(item);
559 kind = PyUnicode_KIND(item);
560 data = PyUnicode_DATA(item);
Antoine Pitroue9745712010-10-31 15:26:04 +0000561
562 /* check whethere the string is dotted */
563 dot_count = 0;
564 for (char_idx = 0; char_idx < item_len; ++char_idx) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200565 if (PyUnicode_READ(kind, data, char_idx) == '.')
Antoine Pitroue9745712010-10-31 15:26:04 +0000566 ++dot_count;
567 }
568
569 if (dot_count == 0) {
570 Py_INCREF(item);
571 PyUnicode_InternInPlace(&item);
572 PyTuple_SET_ITEM(attr, idx, item);
573 } else { /* make it a tuple of non-dotted attrnames */
574 PyObject *attr_chain = PyTuple_New(dot_count + 1);
575 PyObject *attr_chain_item;
Antoine Pitrou87298c42010-10-31 21:03:01 +0000576 Py_ssize_t unibuff_from = 0;
577 Py_ssize_t unibuff_till = 0;
578 Py_ssize_t attr_chain_idx = 0;
Antoine Pitroue9745712010-10-31 15:26:04 +0000579
580 if (attr_chain == NULL) {
581 Py_DECREF(attr);
582 return NULL;
583 }
584
Antoine Pitroue9745712010-10-31 15:26:04 +0000585 for (; dot_count > 0; --dot_count) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
Antoine Pitroue9745712010-10-31 15:26:04 +0000587 ++unibuff_till;
588 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 attr_chain_item = PyUnicode_Substring(item,
590 unibuff_from,
591 unibuff_till);
Antoine Pitroue9745712010-10-31 15:26:04 +0000592 if (attr_chain_item == NULL) {
593 Py_DECREF(attr_chain);
594 Py_DECREF(attr);
595 return NULL;
596 }
597 PyUnicode_InternInPlace(&attr_chain_item);
598 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
599 ++attr_chain_idx;
600 unibuff_till = unibuff_from = unibuff_till + 1;
601 }
602
603 /* now add the last dotless name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 attr_chain_item = PyUnicode_Substring(item,
605 unibuff_from, item_len);
Antoine Pitroue9745712010-10-31 15:26:04 +0000606 if (attr_chain_item == NULL) {
607 Py_DECREF(attr_chain);
608 Py_DECREF(attr);
609 return NULL;
610 }
611 PyUnicode_InternInPlace(&attr_chain_item);
612 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
613
614 PyTuple_SET_ITEM(attr, idx, attr_chain);
615 }
616 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* create attrgetterobject structure */
619 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
Antoine Pitroue9745712010-10-31 15:26:04 +0000620 if (ag == NULL) {
621 Py_DECREF(attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000623 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 ag->attr = attr;
626 ag->nattrs = nattrs;
627
628 PyObject_GC_Track(ag);
629 return (PyObject *)ag;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000630}
631
632static void
633attrgetter_dealloc(attrgetterobject *ag)
634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyObject_GC_UnTrack(ag);
636 Py_XDECREF(ag->attr);
637 PyObject_GC_Del(ag);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000638}
639
640static int
641attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 Py_VISIT(ag->attr);
644 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000645}
646
647static PyObject *
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000648dotted_getattr(PyObject *obj, PyObject *attr)
649{
Antoine Pitroue9745712010-10-31 15:26:04 +0000650 PyObject *newobj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000651
Antoine Pitroue9745712010-10-31 15:26:04 +0000652 /* attr is either a tuple or instance of str.
653 Ensured by the setup code of attrgetter_new */
654 if (PyTuple_CheckExact(attr)) { /* chained getattr */
655 Py_ssize_t name_idx = 0, name_count;
656 PyObject *attr_name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000657
Antoine Pitroue9745712010-10-31 15:26:04 +0000658 name_count = PyTuple_GET_SIZE(attr);
659 Py_INCREF(obj);
660 for (name_idx = 0; name_idx < name_count; ++name_idx) {
661 attr_name = PyTuple_GET_ITEM(attr, name_idx);
662 newobj = PyObject_GetAttr(obj, attr_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 Py_DECREF(obj);
Antoine Pitroue9745712010-10-31 15:26:04 +0000664 if (newobj == NULL) {
665 return NULL;
666 }
667 /* here */
668 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
Antoine Pitroue9745712010-10-31 15:26:04 +0000670 } else { /* single getattr */
671 newobj = PyObject_GetAttr(obj, attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (newobj == NULL)
673 return NULL;
674 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 return obj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000678}
679
680static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +0000681attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject *obj, *result;
684 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
687 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +0000688 if (ag->nattrs == 1) /* ag->attr is always a tuple */
689 return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 assert(PyTuple_Check(ag->attr));
692 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 result = PyTuple_New(nattrs);
695 if (result == NULL)
696 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 for (i=0 ; i < nattrs ; i++) {
699 PyObject *attr, *val;
700 attr = PyTuple_GET_ITEM(ag->attr, i);
701 val = dotted_getattr(obj, attr);
702 if (val == NULL) {
703 Py_DECREF(result);
704 return NULL;
705 }
706 PyTuple_SET_ITEM(result, i, val);
707 }
708 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000709}
710
711PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000712"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000713\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000714Return a callable object that fetches the given attribute(s) from its operand.\n\
715After, f=attrgetter('name'), the call f(r) returns r.name.\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000716After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
717After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
718(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000719
720static PyTypeObject attrgetter_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 PyVarObject_HEAD_INIT(NULL, 0)
722 "operator.attrgetter", /* tp_name */
723 sizeof(attrgetterobject), /* tp_basicsize */
724 0, /* tp_itemsize */
725 /* methods */
726 (destructor)attrgetter_dealloc, /* tp_dealloc */
727 0, /* tp_print */
728 0, /* tp_getattr */
729 0, /* tp_setattr */
730 0, /* tp_reserved */
731 0, /* tp_repr */
732 0, /* tp_as_number */
733 0, /* tp_as_sequence */
734 0, /* tp_as_mapping */
735 0, /* tp_hash */
736 (ternaryfunc)attrgetter_call, /* tp_call */
737 0, /* tp_str */
738 PyObject_GenericGetAttr, /* tp_getattro */
739 0, /* tp_setattro */
740 0, /* tp_as_buffer */
741 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
742 attrgetter_doc, /* tp_doc */
743 (traverseproc)attrgetter_traverse, /* tp_traverse */
744 0, /* tp_clear */
745 0, /* tp_richcompare */
746 0, /* tp_weaklistoffset */
747 0, /* tp_iter */
748 0, /* tp_iternext */
749 0, /* tp_methods */
750 0, /* tp_members */
751 0, /* tp_getset */
752 0, /* tp_base */
753 0, /* tp_dict */
754 0, /* tp_descr_get */
755 0, /* tp_descr_set */
756 0, /* tp_dictoffset */
757 0, /* tp_init */
758 0, /* tp_alloc */
759 attrgetter_new, /* tp_new */
760 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000761};
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000762
763
764/* methodcaller object **********************************************************/
765
766typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PyObject_HEAD
768 PyObject *name;
769 PyObject *args;
770 PyObject *kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000771} methodcallerobject;
772
773static PyTypeObject methodcaller_type;
774
775static PyObject *
776methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 methodcallerobject *mc;
779 PyObject *name, *newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (PyTuple_GET_SIZE(args) < 1) {
782 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
783 "one argument, the method name");
784 return NULL;
785 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 /* create methodcallerobject structure */
788 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
789 if (mc == NULL)
790 return NULL;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
793 if (newargs == NULL) {
794 Py_DECREF(mc);
795 return NULL;
796 }
797 mc->args = newargs;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 name = PyTuple_GET_ITEM(args, 0);
800 Py_INCREF(name);
801 mc->name = name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 Py_XINCREF(kwds);
804 mc->kwds = kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject_GC_Track(mc);
807 return (PyObject *)mc;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000808}
809
810static void
811methodcaller_dealloc(methodcallerobject *mc)
812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject_GC_UnTrack(mc);
814 Py_XDECREF(mc->name);
815 Py_XDECREF(mc->args);
816 Py_XDECREF(mc->kwds);
817 PyObject_GC_Del(mc);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000818}
819
820static int
821methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 Py_VISIT(mc->args);
824 Py_VISIT(mc->kwds);
825 return 0;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000826}
827
828static PyObject *
829methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyObject *method, *obj, *result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
834 return NULL;
835 method = PyObject_GetAttr(obj, mc->name);
836 if (method == NULL)
837 return NULL;
838 result = PyObject_Call(method, mc->args, mc->kwds);
839 Py_DECREF(method);
840 return result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000841}
842
843PyDoc_STRVAR(methodcaller_doc,
844"methodcaller(name, ...) --> methodcaller object\n\
845\n\
846Return a callable object that calls the given method on its operand.\n\
847After, f = methodcaller('name'), the call f(r) returns r.name().\n\
848After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
849r.name('date', foo=1).");
850
851static PyTypeObject methodcaller_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyVarObject_HEAD_INIT(NULL, 0)
853 "operator.methodcaller", /* tp_name */
854 sizeof(methodcallerobject), /* tp_basicsize */
855 0, /* tp_itemsize */
856 /* methods */
857 (destructor)methodcaller_dealloc, /* tp_dealloc */
858 0, /* tp_print */
859 0, /* tp_getattr */
860 0, /* tp_setattr */
861 0, /* tp_reserved */
862 0, /* tp_repr */
863 0, /* tp_as_number */
864 0, /* tp_as_sequence */
865 0, /* tp_as_mapping */
866 0, /* tp_hash */
867 (ternaryfunc)methodcaller_call, /* tp_call */
868 0, /* tp_str */
869 PyObject_GenericGetAttr, /* tp_getattro */
870 0, /* tp_setattro */
871 0, /* tp_as_buffer */
872 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
873 methodcaller_doc, /* tp_doc */
874 (traverseproc)methodcaller_traverse, /* tp_traverse */
875 0, /* tp_clear */
876 0, /* tp_richcompare */
877 0, /* tp_weaklistoffset */
878 0, /* tp_iter */
879 0, /* tp_iternext */
880 0, /* tp_methods */
881 0, /* tp_members */
882 0, /* tp_getset */
883 0, /* tp_base */
884 0, /* tp_dict */
885 0, /* tp_descr_get */
886 0, /* tp_descr_set */
887 0, /* tp_dictoffset */
888 0, /* tp_init */
889 0, /* tp_alloc */
890 methodcaller_new, /* tp_new */
891 0, /* tp_free */
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000892};
893
894
Martin v. Löwis1a214512008-06-11 05:26:20 +0000895/* Initialization function for the module (*must* be called PyInit_operator) */
896
897
898static struct PyModuleDef operatormodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyModuleDef_HEAD_INIT,
900 "operator",
901 operator_doc,
902 -1,
903 operator_methods,
904 NULL,
905 NULL,
906 NULL,
907 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000908};
Guido van Rossum037b9401996-07-30 16:55:54 +0000909
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000910PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000911PyInit_operator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *m;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 /* Create the module and add the functions */
916 m = PyModule_Create(&operatormodule);
917 if (m == NULL)
918 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (PyType_Ready(&itemgetter_type) < 0)
921 return NULL;
922 Py_INCREF(&itemgetter_type);
923 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (PyType_Ready(&attrgetter_type) < 0)
926 return NULL;
927 Py_INCREF(&attrgetter_type);
928 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
929
930 if (PyType_Ready(&methodcaller_type) < 0)
931 return NULL;
932 Py_INCREF(&methodcaller_type);
933 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
934 return m;
Guido van Rossum037b9401996-07-30 16:55:54 +0000935}