blob: 1398fcabb2cefb166c3232c034d44a9d64abde94 [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\
10used for special class 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(isNumberType , PyNumber_Check)
69spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000070spam2(op_add , PyNumber_Add)
71spam2(op_sub , PyNumber_Subtract)
72spam2(op_mul , PyNumber_Multiply)
Fred Drake428e75f2001-08-09 20:14:34 +000073spam2(op_floordiv , PyNumber_FloorDivide)
74spam2(op_truediv , PyNumber_TrueDivide)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000075spam2(op_mod , PyNumber_Remainder)
76spam1(op_neg , PyNumber_Negative)
77spam1(op_pos , PyNumber_Positive)
78spam1(op_abs , PyNumber_Absolute)
79spam1(op_inv , PyNumber_Invert)
Fred Drakeea4d3f02000-09-17 16:09:27 +000080spam1(op_invert , PyNumber_Invert)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000081spam2(op_lshift , PyNumber_Lshift)
82spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +000083spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000084spam2(op_and_ , PyNumber_And)
85spam2(op_xor , PyNumber_Xor)
86spam2(op_or_ , PyNumber_Or)
Armin Rigof5bd3b42005-12-29 16:50:42 +000087spam2(op_iadd , PyNumber_InPlaceAdd)
88spam2(op_isub , PyNumber_InPlaceSubtract)
89spam2(op_imul , PyNumber_InPlaceMultiply)
Armin Rigof5bd3b42005-12-29 16:50:42 +000090spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
91spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
92spam2(op_imod , PyNumber_InPlaceRemainder)
93spam2(op_ilshift , PyNumber_InPlaceLshift)
94spam2(op_irshift , PyNumber_InPlaceRshift)
95spam2(op_iand , PyNumber_InPlaceAnd)
96spam2(op_ixor , PyNumber_InPlaceXor)
97spam2(op_ior , PyNumber_InPlaceOr)
Guido van Rossum037b9401996-07-30 16:55:54 +000098spami(isSequenceType , PySequence_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000099spam2(op_concat , PySequence_Concat)
100spamoi(op_repeat , PySequence_Repeat)
Armin Rigof5bd3b42005-12-29 16:50:42 +0000101spam2(op_iconcat , PySequence_InPlaceConcat)
102spamoi(op_irepeat , PySequence_InPlaceRepeat)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000103spami2b(op_contains , PySequence_Contains)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104spamn2(indexOf , PySequence_Index)
105spamn2(countOf , PySequence_Count)
Guido van Rossum037b9401996-07-30 16:55:54 +0000106spami(isMappingType , PyMapping_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000107spam2(op_getitem , PyObject_GetItem)
108spam2n(op_delitem , PyObject_DelItem)
109spam3n(op_setitem , PyObject_SetItem)
Fred Drake428e75f2001-08-09 20:14:34 +0000110spamrc(op_lt , Py_LT)
111spamrc(op_le , Py_LE)
112spamrc(op_eq , Py_EQ)
113spamrc(op_ne , Py_NE)
114spamrc(op_gt , Py_GT)
115spamrc(op_ge , Py_GE)
Guido van Rossum037b9401996-07-30 16:55:54 +0000116
117static PyObject*
Raymond Hettinger5959c552002-08-19 03:19:09 +0000118op_pow(PyObject *s, PyObject *a)
119{
120 PyObject *a1, *a2;
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000121 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
Raymond Hettinger5959c552002-08-19 03:19:09 +0000122 return PyNumber_Power(a1, a2, Py_None);
123 return NULL;
124}
125
126static PyObject*
Armin Rigof5bd3b42005-12-29 16:50:42 +0000127op_ipow(PyObject *s, PyObject *a)
128{
129 PyObject *a1, *a2;
130 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
131 return PyNumber_InPlacePower(a1, a2, Py_None);
132 return NULL;
133}
134
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000135static PyObject *
136op_index(PyObject *s, PyObject *a)
137{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000138 return PyNumber_Index(a);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000139}
140
Armin Rigof5bd3b42005-12-29 16:50:42 +0000141static PyObject*
Raymond Hettinger9543b342003-01-18 23:22:20 +0000142is_(PyObject *s, PyObject *a)
143{
144 PyObject *a1, *a2, *result = NULL;
145 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
146 result = (a1 == a2) ? Py_True : Py_False;
147 Py_INCREF(result);
148 }
149 return result;
150}
151
152static PyObject*
153is_not(PyObject *s, PyObject *a)
154{
155 PyObject *a1, *a2, *result = NULL;
156 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
157 result = (a1 != a2) ? Py_True : Py_False;
158 Py_INCREF(result);
159 }
160 return result;
161}
162
Guido van Rossum037b9401996-07-30 16:55:54 +0000163#undef spam1
164#undef spam2
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000165#undef spam1o
166#undef spam1o
Neal Norwitz200788c2002-08-13 22:20:41 +0000167#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000168#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Neal Norwitz200788c2002-08-13 22:20:41 +0000169 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000170#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000171#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000172 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000173
174static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000175
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000176spam1o(isNumberType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000177 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000178spam1o(isSequenceType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000179 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000180spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000181 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000182spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000183 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000184spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000185 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000186spam1(countOf,
187 "countOf(a, b) -- Return the number of times b occurs in a.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000188spam1o(isMappingType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000189 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000190
Raymond Hettinger9543b342003-01-18 23:22:20 +0000191spam1(is_, "is_(a, b) -- Same as a is b.")
192spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000193spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000194spam2(add,__add__, "add(a, b) -- Same as a + b.")
195spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
196spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000197spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000198spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000199spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000200spam2o(neg,__neg__, "neg(a) -- Same as -a.")
201spam2o(pos,__pos__, "pos(a) -- Same as +a.")
202spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
203spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
204spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000205spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
206spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000207spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000208spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
209spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
210spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000211spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
212spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
213spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000214spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000215spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000216spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
217spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
218spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
219spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
220spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
221spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000222spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000223 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000224spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000225 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000226spam2(iconcat,__iconcat__,
227 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
228spam2(irepeat,__irepeat__,
229 "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 +0000230spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000231 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000232spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000233 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000234spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000235 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000236spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
237spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000238spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
239spam2(le,__le__, "le(a, b) -- Same as a<=b.")
240spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
241spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
242spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
243spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000244
Guido van Rossum037b9401996-07-30 16:55:54 +0000245 {NULL, NULL} /* sentinel */
246
Guido van Rossum037b9401996-07-30 16:55:54 +0000247};
248
Raymond Hettinger166958b2003-12-01 13:18:39 +0000249/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000250
Raymond Hettinger166958b2003-12-01 13:18:39 +0000251typedef struct {
252 PyObject_HEAD
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000253 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000254 PyObject *item;
255} itemgetterobject;
256
257static PyTypeObject itemgetter_type;
258
259static PyObject *
260itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
261{
262 itemgetterobject *ig;
263 PyObject *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000264 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000265
Georg Brandl02c42872005-08-26 06:42:30 +0000266 if (!_PyArg_NoKeywords("itemgetter()", kwds))
267 return NULL;
268
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000269 nitems = PyTuple_GET_SIZE(args);
270 if (nitems <= 1) {
271 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
272 return NULL;
273 } else
274 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000275
276 /* create itemgetterobject structure */
277 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
278 if (ig == NULL)
279 return NULL;
280
281 Py_INCREF(item);
282 ig->item = item;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000283 ig->nitems = nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000284
285 PyObject_GC_Track(ig);
286 return (PyObject *)ig;
287}
288
289static void
290itemgetter_dealloc(itemgetterobject *ig)
291{
292 PyObject_GC_UnTrack(ig);
293 Py_XDECREF(ig->item);
294 PyObject_GC_Del(ig);
295}
296
297static int
298itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
299{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 Py_VISIT(ig->item);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000301 return 0;
302}
303
304static PyObject *
305itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
306{
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000307 PyObject *obj, *result;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000308 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000309
310 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
311 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000312 if (nitems == 1)
313 return PyObject_GetItem(obj, ig->item);
314
315 assert(PyTuple_Check(ig->item));
316 assert(PyTuple_GET_SIZE(ig->item) == nitems);
317
318 result = PyTuple_New(nitems);
319 if (result == NULL)
320 return NULL;
321
322 for (i=0 ; i < nitems ; i++) {
323 PyObject *item, *val;
324 item = PyTuple_GET_ITEM(ig->item, i);
325 val = PyObject_GetItem(obj, item);
326 if (val == NULL) {
327 Py_DECREF(result);
328 return NULL;
329 }
330 PyTuple_SET_ITEM(result, i, val);
331 }
332 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000333}
334
335PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000336"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000337\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000338Return a callable object that fetches the given item(s) from its operand.\n\
339After, f=itemgetter(2), the call f(r) returns r[2].\n\
340After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000341
342static PyTypeObject itemgetter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000343 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettingerbd3a2402003-12-04 22:17:49 +0000344 "operator.itemgetter", /* tp_name */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000345 sizeof(itemgetterobject), /* tp_basicsize */
346 0, /* tp_itemsize */
347 /* methods */
348 (destructor)itemgetter_dealloc, /* tp_dealloc */
349 0, /* tp_print */
350 0, /* tp_getattr */
351 0, /* tp_setattr */
352 0, /* tp_compare */
353 0, /* tp_repr */
354 0, /* tp_as_number */
355 0, /* tp_as_sequence */
356 0, /* tp_as_mapping */
357 0, /* tp_hash */
358 (ternaryfunc)itemgetter_call, /* tp_call */
359 0, /* tp_str */
360 PyObject_GenericGetAttr, /* tp_getattro */
361 0, /* tp_setattro */
362 0, /* tp_as_buffer */
363 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
364 itemgetter_doc, /* tp_doc */
365 (traverseproc)itemgetter_traverse, /* tp_traverse */
366 0, /* tp_clear */
367 0, /* tp_richcompare */
368 0, /* tp_weaklistoffset */
369 0, /* tp_iter */
370 0, /* tp_iternext */
371 0, /* tp_methods */
372 0, /* tp_members */
373 0, /* tp_getset */
374 0, /* tp_base */
375 0, /* tp_dict */
376 0, /* tp_descr_get */
377 0, /* tp_descr_set */
378 0, /* tp_dictoffset */
379 0, /* tp_init */
380 0, /* tp_alloc */
381 itemgetter_new, /* tp_new */
382 0, /* tp_free */
383};
384
385
386/* attrgetter object **********************************************************/
387
388typedef struct {
389 PyObject_HEAD
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000390 Py_ssize_t nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000391 PyObject *attr;
392} attrgetterobject;
393
394static PyTypeObject attrgetter_type;
395
396static PyObject *
397attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
398{
399 attrgetterobject *ag;
400 PyObject *attr;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000401 Py_ssize_t nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000402
Georg Brandl02c42872005-08-26 06:42:30 +0000403 if (!_PyArg_NoKeywords("attrgetter()", kwds))
404 return NULL;
405
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000406 nattrs = PyTuple_GET_SIZE(args);
407 if (nattrs <= 1) {
408 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
409 return NULL;
410 } else
411 attr = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000412
413 /* create attrgetterobject structure */
414 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
415 if (ag == NULL)
416 return NULL;
417
418 Py_INCREF(attr);
419 ag->attr = attr;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000420 ag->nattrs = nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000421
422 PyObject_GC_Track(ag);
423 return (PyObject *)ag;
424}
425
426static void
427attrgetter_dealloc(attrgetterobject *ag)
428{
429 PyObject_GC_UnTrack(ag);
430 Py_XDECREF(ag->attr);
431 PyObject_GC_Del(ag);
432}
433
434static int
435attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
436{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 Py_VISIT(ag->attr);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000438 return 0;
439}
440
441static PyObject *
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000442dotted_getattr(PyObject *obj, PyObject *attr)
443{
444 char *s, *p;
445
446 if (!PyUnicode_Check(attr)) {
447 PyErr_SetString(PyExc_TypeError,
448 "attribute name must be a string");
449 return NULL;
450 }
451
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000452 s = _PyUnicode_AsString(attr);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000453 Py_INCREF(obj);
454 for (;;) {
455 PyObject *newobj, *str;
456 p = strchr(s, '.');
457 str = p ? PyUnicode_FromStringAndSize(s, (p-s)) :
458 PyUnicode_FromString(s);
459 if (str == NULL) {
460 Py_DECREF(obj);
461 return NULL;
462 }
463 newobj = PyObject_GetAttr(obj, str);
464 Py_DECREF(str);
465 Py_DECREF(obj);
466 if (newobj == NULL)
467 return NULL;
468 obj = newobj;
469 if (p == NULL) break;
470 s = p+1;
471 }
472
473 return obj;
474}
475
476static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +0000477attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
478{
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000479 PyObject *obj, *result;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000480 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000481
482 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
483 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000484 if (ag->nattrs == 1)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000485 return dotted_getattr(obj, ag->attr);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000486
487 assert(PyTuple_Check(ag->attr));
488 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
489
490 result = PyTuple_New(nattrs);
491 if (result == NULL)
492 return NULL;
493
494 for (i=0 ; i < nattrs ; i++) {
495 PyObject *attr, *val;
496 attr = PyTuple_GET_ITEM(ag->attr, i);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000497 val = dotted_getattr(obj, attr);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000498 if (val == NULL) {
499 Py_DECREF(result);
500 return NULL;
501 }
502 PyTuple_SET_ITEM(result, i, val);
503 }
504 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000505}
506
507PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000508"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000509\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000510Return a callable object that fetches the given attribute(s) from its operand.\n\
511After, f=attrgetter('name'), the call f(r) returns r.name.\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000512After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
513After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
514(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000515
516static PyTypeObject attrgetter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000517 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettingerbd3a2402003-12-04 22:17:49 +0000518 "operator.attrgetter", /* tp_name */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000519 sizeof(attrgetterobject), /* tp_basicsize */
520 0, /* tp_itemsize */
521 /* methods */
522 (destructor)attrgetter_dealloc, /* tp_dealloc */
523 0, /* tp_print */
524 0, /* tp_getattr */
525 0, /* tp_setattr */
526 0, /* tp_compare */
527 0, /* tp_repr */
528 0, /* tp_as_number */
529 0, /* tp_as_sequence */
530 0, /* tp_as_mapping */
531 0, /* tp_hash */
532 (ternaryfunc)attrgetter_call, /* tp_call */
533 0, /* tp_str */
534 PyObject_GenericGetAttr, /* tp_getattro */
535 0, /* tp_setattro */
536 0, /* tp_as_buffer */
537 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
538 attrgetter_doc, /* tp_doc */
539 (traverseproc)attrgetter_traverse, /* tp_traverse */
540 0, /* tp_clear */
541 0, /* tp_richcompare */
542 0, /* tp_weaklistoffset */
543 0, /* tp_iter */
544 0, /* tp_iternext */
545 0, /* tp_methods */
546 0, /* tp_members */
547 0, /* tp_getset */
548 0, /* tp_base */
549 0, /* tp_dict */
550 0, /* tp_descr_get */
551 0, /* tp_descr_set */
552 0, /* tp_dictoffset */
553 0, /* tp_init */
554 0, /* tp_alloc */
555 attrgetter_new, /* tp_new */
556 0, /* tp_free */
557};
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000558
559
560/* methodcaller object **********************************************************/
561
562typedef struct {
563 PyObject_HEAD
564 PyObject *name;
565 PyObject *args;
566 PyObject *kwds;
567} methodcallerobject;
568
569static PyTypeObject methodcaller_type;
570
571static PyObject *
572methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
573{
574 methodcallerobject *mc;
575 PyObject *name, *newargs;
576
577 if (PyTuple_GET_SIZE(args) < 1) {
578 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
579 "one argument, the method name");
580 return NULL;
581 }
582
583 /* create methodcallerobject structure */
584 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
585 if (mc == NULL)
586 return NULL;
587
588 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
589 if (newargs == NULL) {
590 Py_DECREF(mc);
591 return NULL;
592 }
593 mc->args = newargs;
594
595 name = PyTuple_GET_ITEM(args, 0);
596 Py_INCREF(name);
597 mc->name = name;
598
599 Py_XINCREF(kwds);
600 mc->kwds = kwds;
601
602 PyObject_GC_Track(mc);
603 return (PyObject *)mc;
604}
605
606static void
607methodcaller_dealloc(methodcallerobject *mc)
608{
609 PyObject_GC_UnTrack(mc);
610 Py_XDECREF(mc->name);
611 Py_XDECREF(mc->args);
612 Py_XDECREF(mc->kwds);
613 PyObject_GC_Del(mc);
614}
615
616static int
617methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
618{
619 Py_VISIT(mc->args);
620 Py_VISIT(mc->kwds);
621 return 0;
622}
623
624static PyObject *
625methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
626{
627 PyObject *method, *obj, *result;
628
629 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
630 return NULL;
631 method = PyObject_GetAttr(obj, mc->name);
632 if (method == NULL)
633 return NULL;
634 result = PyObject_Call(method, mc->args, mc->kwds);
635 Py_DECREF(method);
636 return result;
637}
638
639PyDoc_STRVAR(methodcaller_doc,
640"methodcaller(name, ...) --> methodcaller object\n\
641\n\
642Return a callable object that calls the given method on its operand.\n\
643After, f = methodcaller('name'), the call f(r) returns r.name().\n\
644After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
645r.name('date', foo=1).");
646
647static PyTypeObject methodcaller_type = {
648 PyVarObject_HEAD_INIT(NULL, 0)
649 "operator.methodcaller", /* tp_name */
650 sizeof(methodcallerobject), /* tp_basicsize */
651 0, /* tp_itemsize */
652 /* methods */
653 (destructor)methodcaller_dealloc, /* tp_dealloc */
654 0, /* tp_print */
655 0, /* tp_getattr */
656 0, /* tp_setattr */
657 0, /* tp_compare */
658 0, /* tp_repr */
659 0, /* tp_as_number */
660 0, /* tp_as_sequence */
661 0, /* tp_as_mapping */
662 0, /* tp_hash */
663 (ternaryfunc)methodcaller_call, /* tp_call */
664 0, /* tp_str */
665 PyObject_GenericGetAttr, /* tp_getattro */
666 0, /* tp_setattro */
667 0, /* tp_as_buffer */
668 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
669 methodcaller_doc, /* tp_doc */
670 (traverseproc)methodcaller_traverse, /* tp_traverse */
671 0, /* tp_clear */
672 0, /* tp_richcompare */
673 0, /* tp_weaklistoffset */
674 0, /* tp_iter */
675 0, /* tp_iternext */
676 0, /* tp_methods */
677 0, /* tp_members */
678 0, /* tp_getset */
679 0, /* tp_base */
680 0, /* tp_dict */
681 0, /* tp_descr_get */
682 0, /* tp_descr_set */
683 0, /* tp_dictoffset */
684 0, /* tp_init */
685 0, /* tp_alloc */
686 methodcaller_new, /* tp_new */
687 0, /* tp_free */
688};
689
690
Martin v. Löwis1a214512008-06-11 05:26:20 +0000691/* Initialization function for the module (*must* be called PyInit_operator) */
692
693
694static struct PyModuleDef operatormodule = {
695 PyModuleDef_HEAD_INIT,
696 "operator",
697 operator_doc,
698 -1,
699 operator_methods,
700 NULL,
701 NULL,
702 NULL,
703 NULL
704};
Guido van Rossum037b9401996-07-30 16:55:54 +0000705
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000706PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000707PyInit_operator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000708{
Raymond Hettinger166958b2003-12-01 13:18:39 +0000709 PyObject *m;
710
711 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000712 m = PyModule_Create(&operatormodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000713 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000714 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000715
716 if (PyType_Ready(&itemgetter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000717 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000718 Py_INCREF(&itemgetter_type);
719 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
720
721 if (PyType_Ready(&attrgetter_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000722 return NULL;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000723 Py_INCREF(&attrgetter_type);
724 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000725
726 if (PyType_Ready(&methodcaller_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000727 return NULL;
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000728 Py_INCREF(&methodcaller_type);
729 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000730 return m;
Guido van Rossum037b9401996-07-30 16:55:54 +0000731}