blob: 7ddd123f405f7be45e87a0642129b1bcad026590 [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\
Georg Brandlefc28582009-11-04 07:38:12 +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; \
49 return PyInt_FromLong(r); }
50
Martin v. Löwis26fd9602006-04-22 11:15:41 +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; \
55 return PyInt_FromSsize_t(r); }
56
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
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +000068/* Deprecated operators that need warnings. */
69static int
70op_isCallable(PyObject *x)
71{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "
73 "Use hasattr(obj, '__call__').", 1) < 0)
74 return -1;
75 return PyCallable_Check(x);
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +000076}
77
78static int
79op_sequenceIncludes(PyObject *seq, PyObject* ob)
80{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "
82 "in 3.x. Use operator.contains().", 1) < 0)
83 return -1;
84 return PySequence_Contains(seq, ob);
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +000085}
86
87spami(isCallable , op_isCallable)
Guido van Rossum037b9401996-07-30 16:55:54 +000088spami(isNumberType , PyNumber_Check)
89spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000090spam2(op_add , PyNumber_Add)
91spam2(op_sub , PyNumber_Subtract)
92spam2(op_mul , PyNumber_Multiply)
93spam2(op_div , PyNumber_Divide)
Fred Drake428e75f2001-08-09 20:14:34 +000094spam2(op_floordiv , PyNumber_FloorDivide)
95spam2(op_truediv , PyNumber_TrueDivide)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000096spam2(op_mod , PyNumber_Remainder)
97spam1(op_neg , PyNumber_Negative)
98spam1(op_pos , PyNumber_Positive)
99spam1(op_abs , PyNumber_Absolute)
100spam1(op_inv , PyNumber_Invert)
Fred Drakeea4d3f02000-09-17 16:09:27 +0000101spam1(op_invert , PyNumber_Invert)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000102spam2(op_lshift , PyNumber_Lshift)
103spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +0000104spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000105spam2(op_and_ , PyNumber_And)
106spam2(op_xor , PyNumber_Xor)
107spam2(op_or_ , PyNumber_Or)
Armin Rigof5bd3b42005-12-29 16:50:42 +0000108spam2(op_iadd , PyNumber_InPlaceAdd)
109spam2(op_isub , PyNumber_InPlaceSubtract)
110spam2(op_imul , PyNumber_InPlaceMultiply)
111spam2(op_idiv , PyNumber_InPlaceDivide)
112spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
113spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
114spam2(op_imod , PyNumber_InPlaceRemainder)
115spam2(op_ilshift , PyNumber_InPlaceLshift)
116spam2(op_irshift , PyNumber_InPlaceRshift)
117spam2(op_iand , PyNumber_InPlaceAnd)
118spam2(op_ixor , PyNumber_InPlaceXor)
119spam2(op_ior , PyNumber_InPlaceOr)
Guido van Rossum037b9401996-07-30 16:55:54 +0000120spami(isSequenceType , PySequence_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000121spam2(op_concat , PySequence_Concat)
122spamoi(op_repeat , PySequence_Repeat)
Armin Rigof5bd3b42005-12-29 16:50:42 +0000123spam2(op_iconcat , PySequence_InPlaceConcat)
124spamoi(op_irepeat , PySequence_InPlaceRepeat)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000125spami2b(op_contains , PySequence_Contains)
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000126spami2b(sequenceIncludes, op_sequenceIncludes)
Martin v. Löwis26fd9602006-04-22 11:15:41 +0000127spamn2(indexOf , PySequence_Index)
128spamn2(countOf , PySequence_Count)
Guido van Rossum037b9401996-07-30 16:55:54 +0000129spami(isMappingType , PyMapping_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000130spam2(op_getitem , PyObject_GetItem)
131spam2n(op_delitem , PyObject_DelItem)
132spam3n(op_setitem , PyObject_SetItem)
Fred Drake428e75f2001-08-09 20:14:34 +0000133spamrc(op_lt , Py_LT)
134spamrc(op_le , Py_LE)
135spamrc(op_eq , Py_EQ)
136spamrc(op_ne , Py_NE)
137spamrc(op_gt , Py_GT)
138spamrc(op_ge , Py_GE)
Guido van Rossum037b9401996-07-30 16:55:54 +0000139
140static PyObject*
Raymond Hettinger5959c552002-08-19 03:19:09 +0000141op_pow(PyObject *s, PyObject *a)
142{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 PyObject *a1, *a2;
144 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
145 return PyNumber_Power(a1, a2, Py_None);
146 return NULL;
Raymond Hettinger5959c552002-08-19 03:19:09 +0000147}
148
149static PyObject*
Armin Rigof5bd3b42005-12-29 16:50:42 +0000150op_ipow(PyObject *s, PyObject *a)
151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 PyObject *a1, *a2;
153 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
154 return PyNumber_InPlacePower(a1, a2, Py_None);
155 return NULL;
Armin Rigof5bd3b42005-12-29 16:50:42 +0000156}
157
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000158static PyObject *
159op_index(PyObject *s, PyObject *a)
160{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 return PyNumber_Index(a);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000162}
163
Armin Rigof5bd3b42005-12-29 16:50:42 +0000164static PyObject*
Raymond Hettinger9543b342003-01-18 23:22:20 +0000165is_(PyObject *s, PyObject *a)
166{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 PyObject *a1, *a2, *result = NULL;
168 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
169 result = (a1 == a2) ? Py_True : Py_False;
170 Py_INCREF(result);
171 }
172 return result;
Raymond Hettinger9543b342003-01-18 23:22:20 +0000173}
174
175static PyObject*
176is_not(PyObject *s, PyObject *a)
177{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000178 PyObject *a1, *a2, *result = NULL;
179 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
180 result = (a1 != a2) ? Py_True : Py_False;
181 Py_INCREF(result);
182 }
183 return result;
Raymond Hettinger9543b342003-01-18 23:22:20 +0000184}
185
186static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000187op_getslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000188{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 PyObject *a1;
190 Py_ssize_t a2, a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000191
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000192 if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
193 return NULL;
194 return PySequence_GetSlice(a1, a2, a3);
Guido van Rossum037b9401996-07-30 16:55:54 +0000195}
196
197static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000198op_setslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000199{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 PyObject *a1, *a4;
201 Py_ssize_t a2, a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
204 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000205
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
207 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000208
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 Py_RETURN_NONE;
Guido van Rossum037b9401996-07-30 16:55:54 +0000210}
211
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000212static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000213op_delslice(PyObject *s, PyObject *a)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000214{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 PyObject *a1;
216 Py_ssize_t a2, a3;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000217
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
219 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000220
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000221 if (-1 == PySequence_DelSlice(a1, a2, a3))
222 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 Py_RETURN_NONE;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000225}
226
Guido van Rossum037b9401996-07-30 16:55:54 +0000227#undef spam1
228#undef spam2
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000229#undef spam1o
230#undef spam1o
Neal Norwitz200788c2002-08-13 22:20:41 +0000231#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000232#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000234#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000235#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000237
Benjamin Peterson629026a2014-05-11 16:11:44 -0700238
239
240/* compare_digest **********************************************************/
241
242/*
243 * timing safe compare
244 *
245 * Returns 1 of the strings are equal.
246 * In case of len(a) != len(b) the function tries to keep the timing
247 * dependent on the length of b. CPU cache locally may still alter timing
248 * a bit.
249 */
250static int
251_tscmp(const unsigned char *a, const unsigned char *b,
252 Py_ssize_t len_a, Py_ssize_t len_b)
253{
254 /* The volatile type declarations make sure that the compiler has no
255 * chance to optimize and fold the code in any way that may change
256 * the timing.
257 */
258 volatile Py_ssize_t length;
259 volatile const unsigned char *left;
260 volatile const unsigned char *right;
261 Py_ssize_t i;
262 unsigned char result;
263
264 /* loop count depends on length of b */
265 length = len_b;
266 left = NULL;
267 right = b;
268
269 /* don't use else here to keep the amount of CPU instructions constant,
270 * volatile forces re-evaluation
271 * */
272 if (len_a == length) {
273 left = *((volatile const unsigned char**)&a);
274 result = 0;
275 }
276 if (len_a != length) {
277 left = b;
278 result = 1;
279 }
280
281 for (i=0; i < length; i++) {
282 result |= *left++ ^ *right++;
283 }
284
285 return (result == 0);
286}
287
288PyDoc_STRVAR(compare_digest__doc__,
289"compare_digest(a, b) -> bool\n"
290"\n"
291"Return 'a == b'. This function uses an approach designed to prevent\n"
292"timing analysis, making it appropriate for cryptography.\n"
293"a and b must both be of the same type: either str (ASCII only),\n"
294"or any type that supports the buffer protocol (e.g. bytes).\n"
295"\n"
296"Note: If a and b are of different lengths, or if an error occurs,\n"
297"a timing attack could theoretically reveal information about the\n"
298"types and lengths of a and b--but not their values.\n");
299
300static PyObject*
301compare_digest(PyObject *self, PyObject *args)
302{
303 PyObject *a, *b;
304 int rc;
305
306 if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) {
307 return NULL;
308 }
309
310 /* Unicode string */
311 if (PyUnicode_Check(a) && PyUnicode_Check(b)) {
Benjamin Petersona1ccfb52014-05-11 16:14:00 -0700312 rc = _tscmp((const unsigned char *)PyUnicode_AS_DATA(a),
313 (const unsigned char *)PyUnicode_AS_DATA(b),
Benjamin Peterson629026a2014-05-11 16:11:44 -0700314 PyUnicode_GET_DATA_SIZE(a),
315 PyUnicode_GET_DATA_SIZE(b));
316 }
317 /* fallback to buffer interface for bytes, bytesarray and other */
318 else {
319 Py_buffer view_a;
320 Py_buffer view_b;
321
Benjamin Peterson8c166052014-05-11 16:17:02 -0700322 if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
Benjamin Peterson629026a2014-05-11 16:11:44 -0700323 PyErr_Format(PyExc_TypeError,
324 "unsupported operand types(s) or combination of types: "
325 "'%.100s' and '%.100s'",
326 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
327 return NULL;
328 }
329
330 if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
331 return NULL;
332 }
333 if (view_a.ndim > 1) {
334 PyErr_SetString(PyExc_BufferError,
335 "Buffer must be single dimension");
336 PyBuffer_Release(&view_a);
337 return NULL;
338 }
339
340 if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
341 PyBuffer_Release(&view_a);
342 return NULL;
343 }
344 if (view_b.ndim > 1) {
345 PyErr_SetString(PyExc_BufferError,
346 "Buffer must be single dimension");
347 PyBuffer_Release(&view_a);
348 PyBuffer_Release(&view_b);
349 return NULL;
350 }
351
352 rc = _tscmp((const unsigned char*)view_a.buf,
353 (const unsigned char*)view_b.buf,
354 view_a.len,
355 view_b.len);
356
357 PyBuffer_Release(&view_a);
358 PyBuffer_Release(&view_b);
359 }
360
361 return PyBool_FromLong(rc);
362}
363
Guido van Rossum037b9401996-07-30 16:55:54 +0000364static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000365
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000366spam1o(isCallable,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000367 "isCallable(a) -- Same as callable(a).")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000368spam1o(isNumberType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000369 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000370spam1o(isSequenceType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000371 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000372spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000373 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000374spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000375 "contains(a, b) -- Same as b in a (note reversed operands).")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000376spam1(sequenceIncludes,
377 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
Guido van Rossum17202301996-08-19 22:01:39 +0000378spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000379 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000380spam1(countOf,
381 "countOf(a, b) -- Return the number of times b occurs in a.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000382spam1o(isMappingType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000383 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000384
Raymond Hettinger9543b342003-01-18 23:22:20 +0000385spam1(is_, "is_(a, b) -- Same as a is b.")
386spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000387spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000388spam2(add,__add__, "add(a, b) -- Same as a + b.")
389spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
390spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000391spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
392spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
393spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000394spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000395spam2o(neg,__neg__, "neg(a) -- Same as -a.")
396spam2o(pos,__pos__, "pos(a) -- Same as +a.")
397spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
398spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
399spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000400spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
401spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000402spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000403spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
404spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
405spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Georg Brandlefc28582009-11-04 07:38:12 +0000406spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
407spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
408spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
409spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
410spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
411spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
412spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
413spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
414spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
415spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
416spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
417spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000418spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000419 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000420spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000421 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000422spam2(iconcat,__iconcat__,
Georg Brandlefc28582009-11-04 07:38:12 +0000423 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000424spam2(irepeat,__irepeat__,
Georg Brandlefc28582009-11-04 07:38:12 +0000425 "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
Guido van Rossum17202301996-08-19 22:01:39 +0000426spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000427 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000428spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000429 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000430spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000431 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000432spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
Georg Brandlefc28582009-11-04 07:38:12 +0000433spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000434spam2(getslice,__getslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000435 "getslice(a, b, c) -- Same as a[b:c].")
Guido van Rossum17202301996-08-19 22:01:39 +0000436spam2(setslice,__setslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000437"setslice(a, b, c, d) -- Same as a[b:c] = d.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000438spam2(delslice,__delslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000439"delslice(a, b, c) -- Same as del a[b:c].")
Fred Drake428e75f2001-08-09 20:14:34 +0000440spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
441spam2(le,__le__, "le(a, b) -- Same as a<=b.")
442spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
443spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
444spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
445spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000446
Benjamin Peterson629026a2014-05-11 16:11:44 -0700447 {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,
448 compare_digest__doc__},
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 {NULL, NULL} /* sentinel */
Guido van Rossum037b9401996-07-30 16:55:54 +0000450
Guido van Rossum037b9401996-07-30 16:55:54 +0000451};
452
Raymond Hettinger166958b2003-12-01 13:18:39 +0000453/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000454
Raymond Hettinger166958b2003-12-01 13:18:39 +0000455typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000456 PyObject_HEAD
457 Py_ssize_t nitems;
458 PyObject *item;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000459} itemgetterobject;
460
461static PyTypeObject itemgetter_type;
462
463static PyObject *
464itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
465{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 itemgetterobject *ig;
467 PyObject *item;
468 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000469
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 if (!_PyArg_NoKeywords("itemgetter()", kwds))
471 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000472
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 nitems = PyTuple_GET_SIZE(args);
474 if (nitems <= 1) {
475 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
476 return NULL;
477 } else
478 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000479
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 /* create itemgetterobject structure */
481 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
482 if (ig == NULL)
483 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000484
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 Py_INCREF(item);
486 ig->item = item;
487 ig->nitems = nitems;
488
489 PyObject_GC_Track(ig);
490 return (PyObject *)ig;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000491}
492
493static void
494itemgetter_dealloc(itemgetterobject *ig)
495{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 PyObject_GC_UnTrack(ig);
497 Py_XDECREF(ig->item);
498 PyObject_GC_Del(ig);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000499}
500
501static int
502itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
503{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000504 Py_VISIT(ig->item);
505 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000506}
507
508static PyObject *
509itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 PyObject *obj, *result;
512 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000513
Serhiy Storchaka4ef4eda2016-04-29 09:12:38 +0300514 if (kw != NULL && !_PyArg_NoKeywords("itemgetter", kw))
Serhiy Storchaka0207e762016-04-23 10:53:28 +0300515 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
517 return NULL;
518 if (nitems == 1)
519 return PyObject_GetItem(obj, ig->item);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000520
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 assert(PyTuple_Check(ig->item));
522 assert(PyTuple_GET_SIZE(ig->item) == nitems);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000523
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000524 result = PyTuple_New(nitems);
525 if (result == NULL)
526 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000527
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 for (i=0 ; i < nitems ; i++) {
529 PyObject *item, *val;
530 item = PyTuple_GET_ITEM(ig->item, i);
531 val = PyObject_GetItem(obj, item);
532 if (val == NULL) {
533 Py_DECREF(result);
534 return NULL;
535 }
536 PyTuple_SET_ITEM(result, i, val);
537 }
538 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000539}
540
541PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000542"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000543\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000544Return a callable object that fetches the given item(s) from its operand.\n\
Ezio Melotti5f4ba6b2013-05-08 10:53:11 +0300545After f = itemgetter(2), the call f(r) returns r[2].\n\
546After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000547
548static PyTypeObject itemgetter_type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 PyVarObject_HEAD_INIT(NULL, 0)
550 "operator.itemgetter", /* tp_name */
551 sizeof(itemgetterobject), /* tp_basicsize */
552 0, /* tp_itemsize */
553 /* methods */
554 (destructor)itemgetter_dealloc, /* tp_dealloc */
555 0, /* tp_print */
556 0, /* tp_getattr */
557 0, /* tp_setattr */
558 0, /* tp_compare */
559 0, /* tp_repr */
560 0, /* tp_as_number */
561 0, /* tp_as_sequence */
562 0, /* tp_as_mapping */
563 0, /* tp_hash */
564 (ternaryfunc)itemgetter_call, /* tp_call */
565 0, /* tp_str */
566 PyObject_GenericGetAttr, /* tp_getattro */
567 0, /* tp_setattro */
568 0, /* tp_as_buffer */
569 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
570 itemgetter_doc, /* tp_doc */
571 (traverseproc)itemgetter_traverse, /* tp_traverse */
572 0, /* tp_clear */
573 0, /* tp_richcompare */
574 0, /* tp_weaklistoffset */
575 0, /* tp_iter */
576 0, /* tp_iternext */
577 0, /* tp_methods */
578 0, /* tp_members */
579 0, /* tp_getset */
580 0, /* tp_base */
581 0, /* tp_dict */
582 0, /* tp_descr_get */
583 0, /* tp_descr_set */
584 0, /* tp_dictoffset */
585 0, /* tp_init */
586 0, /* tp_alloc */
587 itemgetter_new, /* tp_new */
588 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000589};
590
591
592/* attrgetter object **********************************************************/
593
594typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 PyObject_HEAD
596 Py_ssize_t nattrs;
597 PyObject *attr;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000598} attrgetterobject;
599
600static PyTypeObject attrgetter_type;
601
602static PyObject *
603attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
604{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000605 attrgetterobject *ag;
606 PyObject *attr;
607 Py_ssize_t nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000608
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000609 if (!_PyArg_NoKeywords("attrgetter()", kwds))
610 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000611
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 nattrs = PyTuple_GET_SIZE(args);
613 if (nattrs <= 1) {
614 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
615 return NULL;
616 } else
617 attr = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000618
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 /* create attrgetterobject structure */
620 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
621 if (ag == NULL)
622 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000623
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 Py_INCREF(attr);
625 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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000643 Py_VISIT(ag->attr);
644 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000645}
646
647static PyObject *
Georg Brandle2065c62008-02-23 23:02:23 +0000648dotted_getattr(PyObject *obj, PyObject *attr)
649{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000650 char *s, *p;
Georg Brandle2065c62008-02-23 23:02:23 +0000651
652#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 if (PyUnicode_Check(attr)) {
654 attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);
655 if (attr == NULL)
656 return NULL;
657 }
Georg Brandle2065c62008-02-23 23:02:23 +0000658#endif
Georg Brandle2065c62008-02-23 23:02:23 +0000659
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000660 if (!PyString_Check(attr)) {
661 PyErr_SetString(PyExc_TypeError,
662 "attribute name must be a string");
663 return NULL;
664 }
Georg Brandle2065c62008-02-23 23:02:23 +0000665
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 s = PyString_AS_STRING(attr);
667 Py_INCREF(obj);
668 for (;;) {
669 PyObject *newobj, *str;
670 p = strchr(s, '.');
671 str = p ? PyString_FromStringAndSize(s, (p-s)) :
672 PyString_FromString(s);
673 if (str == NULL) {
674 Py_DECREF(obj);
675 return NULL;
676 }
677 newobj = PyObject_GetAttr(obj, str);
678 Py_DECREF(str);
679 Py_DECREF(obj);
680 if (newobj == NULL)
681 return NULL;
682 obj = newobj;
683 if (p == NULL) break;
684 s = p+1;
685 }
686
687 return obj;
Georg Brandle2065c62008-02-23 23:02:23 +0000688}
689
690static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +0000691attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
692{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 PyObject *obj, *result;
694 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000695
Serhiy Storchaka4ef4eda2016-04-29 09:12:38 +0300696 if (kw != NULL && !_PyArg_NoKeywords("attrgetter", kw))
Serhiy Storchaka0207e762016-04-23 10:53:28 +0300697 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
699 return NULL;
700 if (ag->nattrs == 1)
701 return dotted_getattr(obj, ag->attr);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000702
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000703 assert(PyTuple_Check(ag->attr));
704 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000705
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 result = PyTuple_New(nattrs);
707 if (result == NULL)
708 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000709
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000710 for (i=0 ; i < nattrs ; i++) {
711 PyObject *attr, *val;
712 attr = PyTuple_GET_ITEM(ag->attr, i);
713 val = dotted_getattr(obj, attr);
714 if (val == NULL) {
715 Py_DECREF(result);
716 return NULL;
717 }
718 PyTuple_SET_ITEM(result, i, val);
719 }
720 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000721}
722
723PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000724"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000725\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000726Return a callable object that fetches the given attribute(s) from its operand.\n\
Ezio Melotti5f4ba6b2013-05-08 10:53:11 +0300727After f = attrgetter('name'), the call f(r) returns r.name.\n\
728After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
729After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
Georg Brandle2065c62008-02-23 23:02:23 +0000730(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000731
732static PyTypeObject attrgetter_type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000733 PyVarObject_HEAD_INIT(NULL, 0)
734 "operator.attrgetter", /* tp_name */
735 sizeof(attrgetterobject), /* tp_basicsize */
736 0, /* tp_itemsize */
737 /* methods */
738 (destructor)attrgetter_dealloc, /* tp_dealloc */
739 0, /* tp_print */
740 0, /* tp_getattr */
741 0, /* tp_setattr */
742 0, /* tp_compare */
743 0, /* tp_repr */
744 0, /* tp_as_number */
745 0, /* tp_as_sequence */
746 0, /* tp_as_mapping */
747 0, /* tp_hash */
748 (ternaryfunc)attrgetter_call, /* tp_call */
749 0, /* tp_str */
750 PyObject_GenericGetAttr, /* tp_getattro */
751 0, /* tp_setattro */
752 0, /* tp_as_buffer */
753 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
754 attrgetter_doc, /* tp_doc */
755 (traverseproc)attrgetter_traverse, /* tp_traverse */
756 0, /* tp_clear */
757 0, /* tp_richcompare */
758 0, /* tp_weaklistoffset */
759 0, /* tp_iter */
760 0, /* tp_iternext */
761 0, /* tp_methods */
762 0, /* tp_members */
763 0, /* tp_getset */
764 0, /* tp_base */
765 0, /* tp_dict */
766 0, /* tp_descr_get */
767 0, /* tp_descr_set */
768 0, /* tp_dictoffset */
769 0, /* tp_init */
770 0, /* tp_alloc */
771 attrgetter_new, /* tp_new */
772 0, /* tp_free */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000773};
Georg Brandlebcfd112008-02-23 23:04:35 +0000774
775
776/* methodcaller object **********************************************************/
777
778typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 PyObject_HEAD
780 PyObject *name;
781 PyObject *args;
782 PyObject *kwds;
Georg Brandlebcfd112008-02-23 23:04:35 +0000783} methodcallerobject;
784
785static PyTypeObject methodcaller_type;
786
787static PyObject *
788methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
789{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000790 methodcallerobject *mc;
Benjamin Petersonaed5a4e2016-08-16 23:35:35 -0700791 PyObject *name;
Georg Brandlebcfd112008-02-23 23:04:35 +0000792
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 if (PyTuple_GET_SIZE(args) < 1) {
794 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
795 "one argument, the method name");
796 return NULL;
797 }
Georg Brandlebcfd112008-02-23 23:04:35 +0000798
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 /* create methodcallerobject structure */
800 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
801 if (mc == NULL)
802 return NULL;
Georg Brandlebcfd112008-02-23 23:04:35 +0000803
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 name = PyTuple_GET_ITEM(args, 0);
805 Py_INCREF(name);
806 mc->name = name;
Georg Brandlebcfd112008-02-23 23:04:35 +0000807
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 Py_XINCREF(kwds);
809 mc->kwds = kwds;
Georg Brandlebcfd112008-02-23 23:04:35 +0000810
Benjamin Petersonaed5a4e2016-08-16 23:35:35 -0700811 mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
812 if (mc->args == NULL) {
813 Py_DECREF(mc);
814 return NULL;
815 }
816
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 PyObject_GC_Track(mc);
818 return (PyObject *)mc;
Georg Brandlebcfd112008-02-23 23:04:35 +0000819}
820
821static void
822methodcaller_dealloc(methodcallerobject *mc)
823{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 PyObject_GC_UnTrack(mc);
825 Py_XDECREF(mc->name);
826 Py_XDECREF(mc->args);
827 Py_XDECREF(mc->kwds);
828 PyObject_GC_Del(mc);
Georg Brandlebcfd112008-02-23 23:04:35 +0000829}
830
831static int
832methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
833{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000834 Py_VISIT(mc->args);
835 Py_VISIT(mc->kwds);
836 return 0;
Georg Brandlebcfd112008-02-23 23:04:35 +0000837}
838
839static PyObject *
840methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
841{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842 PyObject *method, *obj, *result;
Georg Brandlebcfd112008-02-23 23:04:35 +0000843
Serhiy Storchaka4ef4eda2016-04-29 09:12:38 +0300844 if (kw != NULL && !_PyArg_NoKeywords("methodcaller", kw))
Serhiy Storchaka0207e762016-04-23 10:53:28 +0300845 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
847 return NULL;
848 method = PyObject_GetAttr(obj, mc->name);
849 if (method == NULL)
850 return NULL;
851 result = PyObject_Call(method, mc->args, mc->kwds);
852 Py_DECREF(method);
853 return result;
Georg Brandlebcfd112008-02-23 23:04:35 +0000854}
855
856PyDoc_STRVAR(methodcaller_doc,
857"methodcaller(name, ...) --> methodcaller object\n\
858\n\
859Return a callable object that calls the given method on its operand.\n\
Ezio Melotti5f4ba6b2013-05-08 10:53:11 +0300860After f = methodcaller('name'), the call f(r) returns r.name().\n\
861After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
Georg Brandlebcfd112008-02-23 23:04:35 +0000862r.name('date', foo=1).");
863
864static PyTypeObject methodcaller_type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000865 PyVarObject_HEAD_INIT(NULL, 0)
866 "operator.methodcaller", /* tp_name */
867 sizeof(methodcallerobject), /* tp_basicsize */
868 0, /* tp_itemsize */
869 /* methods */
870 (destructor)methodcaller_dealloc, /* tp_dealloc */
871 0, /* tp_print */
872 0, /* tp_getattr */
873 0, /* tp_setattr */
874 0, /* tp_compare */
875 0, /* tp_repr */
876 0, /* tp_as_number */
877 0, /* tp_as_sequence */
878 0, /* tp_as_mapping */
879 0, /* tp_hash */
880 (ternaryfunc)methodcaller_call, /* tp_call */
881 0, /* tp_str */
882 PyObject_GenericGetAttr, /* tp_getattro */
883 0, /* tp_setattro */
884 0, /* tp_as_buffer */
885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
886 methodcaller_doc, /* tp_doc */
887 (traverseproc)methodcaller_traverse, /* tp_traverse */
888 0, /* tp_clear */
889 0, /* tp_richcompare */
890 0, /* tp_weaklistoffset */
891 0, /* tp_iter */
892 0, /* tp_iternext */
893 0, /* tp_methods */
894 0, /* tp_members */
895 0, /* tp_getset */
896 0, /* tp_base */
897 0, /* tp_dict */
898 0, /* tp_descr_get */
899 0, /* tp_descr_set */
900 0, /* tp_dictoffset */
901 0, /* tp_init */
902 0, /* tp_alloc */
903 methodcaller_new, /* tp_new */
904 0, /* tp_free */
Georg Brandlebcfd112008-02-23 23:04:35 +0000905};
906
907
Guido van Rossum037b9401996-07-30 16:55:54 +0000908/* Initialization function for the module (*must* be called initoperator) */
909
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000910PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000911initoperator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000912{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 PyObject *m;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000914
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 /* Create the module and add the functions */
916 m = Py_InitModule4("operator", operator_methods, operator_doc,
917 (PyObject*)NULL, PYTHON_API_VERSION);
918 if (m == NULL)
919 return;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 if (PyType_Ready(&itemgetter_type) < 0)
922 return;
923 Py_INCREF(&itemgetter_type);
924 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
Georg Brandlebcfd112008-02-23 23:04:35 +0000925
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000926 if (PyType_Ready(&attrgetter_type) < 0)
927 return;
928 Py_INCREF(&attrgetter_type);
929 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
930
931 if (PyType_Ready(&methodcaller_type) < 0)
932 return;
933 Py_INCREF(&methodcaller_type);
934 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
Guido van Rossum037b9401996-07-30 16:55:54 +0000935}