blob: b088d935a0f7bc55482babbd045b1c41fd5ab7a1 [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; \
49 return PyInt_FromLong(r); }
50
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; \
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
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
163static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000164op_getslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000165{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000166 PyObject *a1;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167 Py_ssize_t a2, a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000168
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169 if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000170 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000171 return PySequence_GetSlice(a1, a2, a3);
Guido van Rossum037b9401996-07-30 16:55:54 +0000172}
173
174static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000175op_setslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000176{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000177 PyObject *a1, *a4;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000178 Py_ssize_t a2, a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000179
Guido van Rossumd8faa362007-04-27 19:54:29 +0000180 if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000181 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000182
Guido van Rossumd8faa362007-04-27 19:54:29 +0000183 if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000184 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000185
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 Py_RETURN_NONE;
Guido van Rossum037b9401996-07-30 16:55:54 +0000187}
188
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000189static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000190op_delslice(PyObject *s, PyObject *a)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000191{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000192 PyObject *a1;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000193 Py_ssize_t a2, a3;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000194
Guido van Rossumd8faa362007-04-27 19:54:29 +0000195 if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000196 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000197
Guido van Rossumd8faa362007-04-27 19:54:29 +0000198 if (-1 == PySequence_DelSlice(a1, a2, a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000199 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000200
Guido van Rossumd8faa362007-04-27 19:54:29 +0000201 Py_RETURN_NONE;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000202}
203
Guido van Rossum037b9401996-07-30 16:55:54 +0000204#undef spam1
205#undef spam2
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000206#undef spam1o
207#undef spam1o
Neal Norwitz200788c2002-08-13 22:20:41 +0000208#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000209#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
Neal Norwitz200788c2002-08-13 22:20:41 +0000210 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000211#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
Armin Rigoc4308d52005-12-29 14:39:28 +0000212#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000213 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
Guido van Rossum037b9401996-07-30 16:55:54 +0000214
215static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000216
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000217spam1o(isNumberType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000218 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000219spam1o(isSequenceType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000220 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000221spam1o(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000222 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000223spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000224 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000225spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000226 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000227spam1(countOf,
228 "countOf(a, b) -- Return the number of times b occurs in a.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000229spam1o(isMappingType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000230 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000231
Raymond Hettinger9543b342003-01-18 23:22:20 +0000232spam1(is_, "is_(a, b) -- Same as a is b.")
233spam1(is_not, "is_not(a, b) -- Same as a is not b.")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000234spam2o(index, __index__, "index(a) -- Same as a.__index__()")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000235spam2(add,__add__, "add(a, b) -- Same as a + b.")
236spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
237spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000238spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000239spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000240spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000241spam2o(neg,__neg__, "neg(a) -- Same as -a.")
242spam2o(pos,__pos__, "pos(a) -- Same as +a.")
243spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
244spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
245spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000246spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
247spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000248spam2o(not_,__not__, "not_(a) -- Same as not a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000249spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
250spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
251spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000252spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
253spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
254spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000255spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000256spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000257spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
258spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
259spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
260spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
261spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
262spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000263spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000264 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000265spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000266 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000267spam2(iconcat,__iconcat__,
268 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
269spam2(irepeat,__irepeat__,
270 "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 +0000271spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000272 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000273spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000274 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000275spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000276 "delitem(a, b) -- Same as del a[b].")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000277spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
278spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000279spam2(getslice,__getslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000280 "getslice(a, b, c) -- Same as a[b:c].")
Guido van Rossum17202301996-08-19 22:01:39 +0000281spam2(setslice,__setslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000282"setslice(a, b, c, d) -- Same as a[b:c] = d.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000283spam2(delslice,__delslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000284"delslice(a, b, c) -- Same as del a[b:c].")
Fred Drake428e75f2001-08-09 20:14:34 +0000285spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
286spam2(le,__le__, "le(a, b) -- Same as a<=b.")
287spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
288spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
289spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
290spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000291
Guido van Rossum037b9401996-07-30 16:55:54 +0000292 {NULL, NULL} /* sentinel */
293
Guido van Rossum037b9401996-07-30 16:55:54 +0000294};
295
Raymond Hettinger166958b2003-12-01 13:18:39 +0000296/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000297
Raymond Hettinger166958b2003-12-01 13:18:39 +0000298typedef struct {
299 PyObject_HEAD
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000300 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000301 PyObject *item;
302} itemgetterobject;
303
304static PyTypeObject itemgetter_type;
305
306static PyObject *
307itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
308{
309 itemgetterobject *ig;
310 PyObject *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000311 Py_ssize_t nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000312
Georg Brandl02c42872005-08-26 06:42:30 +0000313 if (!_PyArg_NoKeywords("itemgetter()", kwds))
314 return NULL;
315
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000316 nitems = PyTuple_GET_SIZE(args);
317 if (nitems <= 1) {
318 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
319 return NULL;
320 } else
321 item = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000322
323 /* create itemgetterobject structure */
324 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
325 if (ig == NULL)
326 return NULL;
327
328 Py_INCREF(item);
329 ig->item = item;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000330 ig->nitems = nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000331
332 PyObject_GC_Track(ig);
333 return (PyObject *)ig;
334}
335
336static void
337itemgetter_dealloc(itemgetterobject *ig)
338{
339 PyObject_GC_UnTrack(ig);
340 Py_XDECREF(ig->item);
341 PyObject_GC_Del(ig);
342}
343
344static int
345itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
346{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 Py_VISIT(ig->item);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000348 return 0;
349}
350
351static PyObject *
352itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
353{
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000354 PyObject *obj, *result;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000355 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000356
357 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
358 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000359 if (nitems == 1)
360 return PyObject_GetItem(obj, ig->item);
361
362 assert(PyTuple_Check(ig->item));
363 assert(PyTuple_GET_SIZE(ig->item) == nitems);
364
365 result = PyTuple_New(nitems);
366 if (result == NULL)
367 return NULL;
368
369 for (i=0 ; i < nitems ; i++) {
370 PyObject *item, *val;
371 item = PyTuple_GET_ITEM(ig->item, i);
372 val = PyObject_GetItem(obj, item);
373 if (val == NULL) {
374 Py_DECREF(result);
375 return NULL;
376 }
377 PyTuple_SET_ITEM(result, i, val);
378 }
379 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000380}
381
382PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000383"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000384\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000385Return a callable object that fetches the given item(s) from its operand.\n\
386After, f=itemgetter(2), the call f(r) returns r[2].\n\
387After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000388
389static PyTypeObject itemgetter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000390 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettingerbd3a2402003-12-04 22:17:49 +0000391 "operator.itemgetter", /* tp_name */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000392 sizeof(itemgetterobject), /* tp_basicsize */
393 0, /* tp_itemsize */
394 /* methods */
395 (destructor)itemgetter_dealloc, /* tp_dealloc */
396 0, /* tp_print */
397 0, /* tp_getattr */
398 0, /* tp_setattr */
399 0, /* tp_compare */
400 0, /* tp_repr */
401 0, /* tp_as_number */
402 0, /* tp_as_sequence */
403 0, /* tp_as_mapping */
404 0, /* tp_hash */
405 (ternaryfunc)itemgetter_call, /* tp_call */
406 0, /* tp_str */
407 PyObject_GenericGetAttr, /* tp_getattro */
408 0, /* tp_setattro */
409 0, /* tp_as_buffer */
410 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
411 itemgetter_doc, /* tp_doc */
412 (traverseproc)itemgetter_traverse, /* tp_traverse */
413 0, /* tp_clear */
414 0, /* tp_richcompare */
415 0, /* tp_weaklistoffset */
416 0, /* tp_iter */
417 0, /* tp_iternext */
418 0, /* tp_methods */
419 0, /* tp_members */
420 0, /* tp_getset */
421 0, /* tp_base */
422 0, /* tp_dict */
423 0, /* tp_descr_get */
424 0, /* tp_descr_set */
425 0, /* tp_dictoffset */
426 0, /* tp_init */
427 0, /* tp_alloc */
428 itemgetter_new, /* tp_new */
429 0, /* tp_free */
430};
431
432
433/* attrgetter object **********************************************************/
434
435typedef struct {
436 PyObject_HEAD
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000437 Py_ssize_t nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000438 PyObject *attr;
439} attrgetterobject;
440
441static PyTypeObject attrgetter_type;
442
443static PyObject *
444attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
445{
446 attrgetterobject *ag;
447 PyObject *attr;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000448 Py_ssize_t nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000449
Georg Brandl02c42872005-08-26 06:42:30 +0000450 if (!_PyArg_NoKeywords("attrgetter()", kwds))
451 return NULL;
452
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000453 nattrs = PyTuple_GET_SIZE(args);
454 if (nattrs <= 1) {
455 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
456 return NULL;
457 } else
458 attr = args;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000459
460 /* create attrgetterobject structure */
461 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
462 if (ag == NULL)
463 return NULL;
464
465 Py_INCREF(attr);
466 ag->attr = attr;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000467 ag->nattrs = nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000468
469 PyObject_GC_Track(ag);
470 return (PyObject *)ag;
471}
472
473static void
474attrgetter_dealloc(attrgetterobject *ag)
475{
476 PyObject_GC_UnTrack(ag);
477 Py_XDECREF(ag->attr);
478 PyObject_GC_Del(ag);
479}
480
481static int
482attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
483{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484 Py_VISIT(ag->attr);
Raymond Hettinger166958b2003-12-01 13:18:39 +0000485 return 0;
486}
487
488static PyObject *
489attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
490{
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000491 PyObject *obj, *result;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000492 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000493
494 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
495 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000496 if (ag->nattrs == 1)
497 return PyObject_GetAttr(obj, ag->attr);
498
499 assert(PyTuple_Check(ag->attr));
500 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
501
502 result = PyTuple_New(nattrs);
503 if (result == NULL)
504 return NULL;
505
506 for (i=0 ; i < nattrs ; i++) {
507 PyObject *attr, *val;
508 attr = PyTuple_GET_ITEM(ag->attr, i);
509 val = PyObject_GetAttr(obj, attr);
510 if (val == NULL) {
511 Py_DECREF(result);
512 return NULL;
513 }
514 PyTuple_SET_ITEM(result, i, val);
515 }
516 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000517}
518
519PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000520"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +0000521\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000522Return a callable object that fetches the given attribute(s) from its operand.\n\
523After, f=attrgetter('name'), the call f(r) returns r.name.\n\
524After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).");
Raymond Hettinger166958b2003-12-01 13:18:39 +0000525
526static PyTypeObject attrgetter_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000527 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettingerbd3a2402003-12-04 22:17:49 +0000528 "operator.attrgetter", /* tp_name */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000529 sizeof(attrgetterobject), /* tp_basicsize */
530 0, /* tp_itemsize */
531 /* methods */
532 (destructor)attrgetter_dealloc, /* tp_dealloc */
533 0, /* tp_print */
534 0, /* tp_getattr */
535 0, /* tp_setattr */
536 0, /* tp_compare */
537 0, /* tp_repr */
538 0, /* tp_as_number */
539 0, /* tp_as_sequence */
540 0, /* tp_as_mapping */
541 0, /* tp_hash */
542 (ternaryfunc)attrgetter_call, /* tp_call */
543 0, /* tp_str */
544 PyObject_GenericGetAttr, /* tp_getattro */
545 0, /* tp_setattro */
546 0, /* tp_as_buffer */
547 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
548 attrgetter_doc, /* tp_doc */
549 (traverseproc)attrgetter_traverse, /* tp_traverse */
550 0, /* tp_clear */
551 0, /* tp_richcompare */
552 0, /* tp_weaklistoffset */
553 0, /* tp_iter */
554 0, /* tp_iternext */
555 0, /* tp_methods */
556 0, /* tp_members */
557 0, /* tp_getset */
558 0, /* tp_base */
559 0, /* tp_dict */
560 0, /* tp_descr_get */
561 0, /* tp_descr_set */
562 0, /* tp_dictoffset */
563 0, /* tp_init */
564 0, /* tp_alloc */
565 attrgetter_new, /* tp_new */
566 0, /* tp_free */
567};
Guido van Rossum037b9401996-07-30 16:55:54 +0000568/* Initialization function for the module (*must* be called initoperator) */
569
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000570PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000571initoperator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000572{
Raymond Hettinger166958b2003-12-01 13:18:39 +0000573 PyObject *m;
574
575 /* Create the module and add the functions */
576 m = Py_InitModule4("operator", operator_methods, operator_doc,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000577 (PyObject*)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000578 if (m == NULL)
579 return;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000580
581 if (PyType_Ready(&itemgetter_type) < 0)
582 return;
583 Py_INCREF(&itemgetter_type);
584 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
585
586 if (PyType_Ready(&attrgetter_type) < 0)
587 return;
588 Py_INCREF(&attrgetter_type);
589 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
Guido van Rossum037b9401996-07-30 16:55:54 +0000590}