Whitespace normalization.
[python/dscho.git] / Modules / operator.c
blob468440e7aae43bf2da63acba8c517157acccb2f0
2 #include "Python.h"
4 PyDoc_STRVAR(operator_doc,
5 "Operator interface.\n\
6 \n\
7 This module exports a set of functions implemented in C corresponding\n\
8 to the intrinsic operators of Python. For example, operator.add(x, y)\n\
9 is equivalent to the expression x+y. The function names are those\n\
10 used for special class methods; variants without leading and trailing\n\
11 '__' are also provided for convenience.");
13 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
14 return AOP(a1); }
16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
17 PyObject *a1, *a2; \
18 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
19 return AOP(a1,a2); }
21 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
22 PyObject *a1; int a2; \
23 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
24 return AOP(a1,a2); }
26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
27 PyObject *a1, *a2; \
28 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
29 if(-1 == AOP(a1,a2)) return NULL; \
30 Py_INCREF(Py_None); \
31 return Py_None; }
33 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
34 PyObject *a1, *a2, *a3; \
35 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
36 if(-1 == AOP(a1,a2,a3)) return NULL; \
37 Py_INCREF(Py_None); \
38 return Py_None; }
40 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
41 long r; \
42 if(-1 == (r=AOP(a1))) return NULL; \
43 return PyBool_FromLong(r); }
45 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
46 PyObject *a1, *a2; long r; \
47 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
48 if(-1 == (r=AOP(a1,a2))) return NULL; \
49 return PyInt_FromLong(r); }
51 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
52 PyObject *a1, *a2; long r; \
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
54 if(-1 == (r=AOP(a1,a2))) return NULL; \
55 return PyBool_FromLong(r); }
57 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
58 PyObject *a1, *a2; \
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
60 return PyObject_RichCompare(a1,a2,A); }
62 spami(isCallable , PyCallable_Check)
63 spami(isNumberType , PyNumber_Check)
64 spami(truth , PyObject_IsTrue)
65 spam2(op_add , PyNumber_Add)
66 spam2(op_sub , PyNumber_Subtract)
67 spam2(op_mul , PyNumber_Multiply)
68 spam2(op_div , PyNumber_Divide)
69 spam2(op_floordiv , PyNumber_FloorDivide)
70 spam2(op_truediv , PyNumber_TrueDivide)
71 spam2(op_mod , PyNumber_Remainder)
72 spam1(op_neg , PyNumber_Negative)
73 spam1(op_pos , PyNumber_Positive)
74 spam1(op_abs , PyNumber_Absolute)
75 spam1(op_inv , PyNumber_Invert)
76 spam1(op_invert , PyNumber_Invert)
77 spam2(op_lshift , PyNumber_Lshift)
78 spam2(op_rshift , PyNumber_Rshift)
79 spami(op_not_ , PyObject_Not)
80 spam2(op_and_ , PyNumber_And)
81 spam2(op_xor , PyNumber_Xor)
82 spam2(op_or_ , PyNumber_Or)
83 spami(isSequenceType , PySequence_Check)
84 spam2(op_concat , PySequence_Concat)
85 spamoi(op_repeat , PySequence_Repeat)
86 spami2b(op_contains , PySequence_Contains)
87 spami2b(sequenceIncludes, PySequence_Contains)
88 spami2(indexOf , PySequence_Index)
89 spami2(countOf , PySequence_Count)
90 spami(isMappingType , PyMapping_Check)
91 spam2(op_getitem , PyObject_GetItem)
92 spam2n(op_delitem , PyObject_DelItem)
93 spam3n(op_setitem , PyObject_SetItem)
94 spamrc(op_lt , Py_LT)
95 spamrc(op_le , Py_LE)
96 spamrc(op_eq , Py_EQ)
97 spamrc(op_ne , Py_NE)
98 spamrc(op_gt , Py_GT)
99 spamrc(op_ge , Py_GE)
101 static PyObject*
102 op_pow(PyObject *s, PyObject *a)
104 PyObject *a1, *a2;
105 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
106 return PyNumber_Power(a1, a2, Py_None);
107 return NULL;
110 static PyObject*
111 is_(PyObject *s, PyObject *a)
113 PyObject *a1, *a2, *result = NULL;
114 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
115 result = (a1 == a2) ? Py_True : Py_False;
116 Py_INCREF(result);
118 return result;
121 static PyObject*
122 is_not(PyObject *s, PyObject *a)
124 PyObject *a1, *a2, *result = NULL;
125 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
126 result = (a1 != a2) ? Py_True : Py_False;
127 Py_INCREF(result);
129 return result;
132 static PyObject*
133 op_getslice(PyObject *s, PyObject *a)
135 PyObject *a1;
136 int a2,a3;
138 if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
139 return NULL;
140 return PySequence_GetSlice(a1,a2,a3);
143 static PyObject*
144 op_setslice(PyObject *s, PyObject *a)
146 PyObject *a1, *a4;
147 int a2,a3;
149 if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
150 return NULL;
152 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
153 return NULL;
155 Py_INCREF(Py_None);
156 return Py_None;
159 static PyObject*
160 op_delslice(PyObject *s, PyObject *a)
162 PyObject *a1;
163 int a2,a3;
165 if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
166 return NULL;
168 if (-1 == PySequence_DelSlice(a1,a2,a3))
169 return NULL;
171 Py_INCREF(Py_None);
172 return Py_None;
175 #undef spam1
176 #undef spam2
177 #undef spam1o
178 #undef spam1o
179 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
180 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, DOC}, \
181 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
182 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
183 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, DOC}, \
184 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
186 static struct PyMethodDef operator_methods[] = {
188 spam1o(isCallable,
189 "isCallable(a) -- Same as callable(a).")
190 spam1o(isNumberType,
191 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
192 spam1o(isSequenceType,
193 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
194 spam1o(truth,
195 "truth(a) -- Return True if a is true, False otherwise.")
196 spam2(contains,__contains__,
197 "contains(a, b) -- Same as b in a (note reversed operands).")
198 spam1(sequenceIncludes,
199 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
200 spam1(indexOf,
201 "indexOf(a, b) -- Return the first index of b in a.")
202 spam1(countOf,
203 "countOf(a, b) -- Return the number of times b occurs in a.")
204 spam1o(isMappingType,
205 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
207 spam1(is_, "is_(a, b) -- Same as a is b.")
208 spam1(is_not, "is_not(a, b) -- Same as a is not b.")
209 spam2(add,__add__, "add(a, b) -- Same as a + b.")
210 spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
211 spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
212 spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
213 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
214 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
215 spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
216 spam2o(neg,__neg__, "neg(a) -- Same as -a.")
217 spam2o(pos,__pos__, "pos(a) -- Same as +a.")
218 spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
219 spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
220 spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
221 spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
222 spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
223 spam2o(not_,__not__, "not_(a) -- Same as not a.")
224 spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
225 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
226 spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
227 spam2(concat,__concat__,
228 "concat(a, b) -- Same as a + b, for a and b sequences.")
229 spam2(repeat,__repeat__,
230 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
231 spam2(getitem,__getitem__,
232 "getitem(a, b) -- Same as a[b].")
233 spam2(setitem,__setitem__,
234 "setitem(a, b, c) -- Same as a[b] = c.")
235 spam2(delitem,__delitem__,
236 "delitem(a, b) -- Same as del a[b].")
237 spam2(pow,__pow__, "pow(a, b) -- Same as a**b.")
238 spam2(getslice,__getslice__,
239 "getslice(a, b, c) -- Same as a[b:c].")
240 spam2(setslice,__setslice__,
241 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
242 spam2(delslice,__delslice__,
243 "delslice(a, b, c) -- Same as del a[b:c].")
244 spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
245 spam2(le,__le__, "le(a, b) -- Same as a<=b.")
246 spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
247 spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
248 spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
249 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
251 {NULL, NULL} /* sentinel */
255 /* itemgetter object **********************************************************/
257 typedef struct {
258 PyObject_HEAD
259 PyObject *item;
260 } itemgetterobject;
262 static PyTypeObject itemgetter_type;
264 static PyObject *
265 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
267 itemgetterobject *ig;
268 PyObject *item;
270 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
271 return NULL;
273 /* create itemgetterobject structure */
274 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
275 if (ig == NULL)
276 return NULL;
278 Py_INCREF(item);
279 ig->item = item;
281 PyObject_GC_Track(ig);
282 return (PyObject *)ig;
285 static void
286 itemgetter_dealloc(itemgetterobject *ig)
288 PyObject_GC_UnTrack(ig);
289 Py_XDECREF(ig->item);
290 PyObject_GC_Del(ig);
293 static int
294 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
296 if (ig->item)
297 return visit(ig->item, arg);
298 return 0;
301 static PyObject *
302 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
304 PyObject * obj;
306 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
307 return NULL;
308 return PyObject_GetItem(obj, ig->item);
311 PyDoc_STRVAR(itemgetter_doc,
312 "itemgetter(item) --> itemgetter object\n\
314 Return a callable object that fetches the given item from its operand.\n\
315 After, f=itemgetter(2), the call f(b) returns b[2].");
317 static PyTypeObject itemgetter_type = {
318 PyObject_HEAD_INIT(NULL)
319 0, /* ob_size */
320 "operator.itemgetter", /* tp_name */
321 sizeof(itemgetterobject), /* tp_basicsize */
322 0, /* tp_itemsize */
323 /* methods */
324 (destructor)itemgetter_dealloc, /* tp_dealloc */
325 0, /* tp_print */
326 0, /* tp_getattr */
327 0, /* tp_setattr */
328 0, /* tp_compare */
329 0, /* tp_repr */
330 0, /* tp_as_number */
331 0, /* tp_as_sequence */
332 0, /* tp_as_mapping */
333 0, /* tp_hash */
334 (ternaryfunc)itemgetter_call, /* tp_call */
335 0, /* tp_str */
336 PyObject_GenericGetAttr, /* tp_getattro */
337 0, /* tp_setattro */
338 0, /* tp_as_buffer */
339 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
340 itemgetter_doc, /* tp_doc */
341 (traverseproc)itemgetter_traverse, /* tp_traverse */
342 0, /* tp_clear */
343 0, /* tp_richcompare */
344 0, /* tp_weaklistoffset */
345 0, /* tp_iter */
346 0, /* tp_iternext */
347 0, /* tp_methods */
348 0, /* tp_members */
349 0, /* tp_getset */
350 0, /* tp_base */
351 0, /* tp_dict */
352 0, /* tp_descr_get */
353 0, /* tp_descr_set */
354 0, /* tp_dictoffset */
355 0, /* tp_init */
356 0, /* tp_alloc */
357 itemgetter_new, /* tp_new */
358 0, /* tp_free */
362 /* attrgetter object **********************************************************/
364 typedef struct {
365 PyObject_HEAD
366 PyObject *attr;
367 } attrgetterobject;
369 static PyTypeObject attrgetter_type;
371 static PyObject *
372 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
374 attrgetterobject *ag;
375 PyObject *attr;
377 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
378 return NULL;
380 /* create attrgetterobject structure */
381 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
382 if (ag == NULL)
383 return NULL;
385 Py_INCREF(attr);
386 ag->attr = attr;
388 PyObject_GC_Track(ag);
389 return (PyObject *)ag;
392 static void
393 attrgetter_dealloc(attrgetterobject *ag)
395 PyObject_GC_UnTrack(ag);
396 Py_XDECREF(ag->attr);
397 PyObject_GC_Del(ag);
400 static int
401 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
403 if (ag->attr)
404 return visit(ag->attr, arg);
405 return 0;
408 static PyObject *
409 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
411 PyObject * obj;
413 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
414 return NULL;
415 return PyObject_GetAttr(obj, ag->attr);
418 PyDoc_STRVAR(attrgetter_doc,
419 "attrgetter(attr) --> attrgetter object\n\
421 Return a callable object that fetches the given attribute from its operand.\n\
422 After, f=attrgetter('name'), the call f(b) returns b.name.");
424 static PyTypeObject attrgetter_type = {
425 PyObject_HEAD_INIT(NULL)
426 0, /* ob_size */
427 "operator.attrgetter", /* tp_name */
428 sizeof(attrgetterobject), /* tp_basicsize */
429 0, /* tp_itemsize */
430 /* methods */
431 (destructor)attrgetter_dealloc, /* tp_dealloc */
432 0, /* tp_print */
433 0, /* tp_getattr */
434 0, /* tp_setattr */
435 0, /* tp_compare */
436 0, /* tp_repr */
437 0, /* tp_as_number */
438 0, /* tp_as_sequence */
439 0, /* tp_as_mapping */
440 0, /* tp_hash */
441 (ternaryfunc)attrgetter_call, /* tp_call */
442 0, /* tp_str */
443 PyObject_GenericGetAttr, /* tp_getattro */
444 0, /* tp_setattro */
445 0, /* tp_as_buffer */
446 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
447 attrgetter_doc, /* tp_doc */
448 (traverseproc)attrgetter_traverse, /* tp_traverse */
449 0, /* tp_clear */
450 0, /* tp_richcompare */
451 0, /* tp_weaklistoffset */
452 0, /* tp_iter */
453 0, /* tp_iternext */
454 0, /* tp_methods */
455 0, /* tp_members */
456 0, /* tp_getset */
457 0, /* tp_base */
458 0, /* tp_dict */
459 0, /* tp_descr_get */
460 0, /* tp_descr_set */
461 0, /* tp_dictoffset */
462 0, /* tp_init */
463 0, /* tp_alloc */
464 attrgetter_new, /* tp_new */
465 0, /* tp_free */
467 /* Initialization function for the module (*must* be called initoperator) */
469 PyMODINIT_FUNC
470 initoperator(void)
472 PyObject *m;
474 /* Create the module and add the functions */
475 m = Py_InitModule4("operator", operator_methods, operator_doc,
476 (PyObject*)NULL, PYTHON_API_VERSION);
478 if (PyType_Ready(&itemgetter_type) < 0)
479 return;
480 Py_INCREF(&itemgetter_type);
481 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
483 if (PyType_Ready(&attrgetter_type) < 0)
484 return;
485 Py_INCREF(&attrgetter_type);
486 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);