Added information on function name added to LogRecord, and the 'extra' keyword parameter.
[python.git] / Modules / operator.c
blob4817d3389cb0a005fc3442a2109842277cc79cb3
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 spam2(op_iadd , PyNumber_InPlaceAdd)
84 spam2(op_isub , PyNumber_InPlaceSubtract)
85 spam2(op_imul , PyNumber_InPlaceMultiply)
86 spam2(op_idiv , PyNumber_InPlaceDivide)
87 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
88 spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
89 spam2(op_imod , PyNumber_InPlaceRemainder)
90 spam2(op_ilshift , PyNumber_InPlaceLshift)
91 spam2(op_irshift , PyNumber_InPlaceRshift)
92 spam2(op_iand , PyNumber_InPlaceAnd)
93 spam2(op_ixor , PyNumber_InPlaceXor)
94 spam2(op_ior , PyNumber_InPlaceOr)
95 spami(isSequenceType , PySequence_Check)
96 spam2(op_concat , PySequence_Concat)
97 spamoi(op_repeat , PySequence_Repeat)
98 spam2(op_iconcat , PySequence_InPlaceConcat)
99 spamoi(op_irepeat , PySequence_InPlaceRepeat)
100 spami2b(op_contains , PySequence_Contains)
101 spami2b(sequenceIncludes, PySequence_Contains)
102 spami2(indexOf , PySequence_Index)
103 spami2(countOf , PySequence_Count)
104 spami(isMappingType , PyMapping_Check)
105 spam2(op_getitem , PyObject_GetItem)
106 spam2n(op_delitem , PyObject_DelItem)
107 spam3n(op_setitem , PyObject_SetItem)
108 spamrc(op_lt , Py_LT)
109 spamrc(op_le , Py_LE)
110 spamrc(op_eq , Py_EQ)
111 spamrc(op_ne , Py_NE)
112 spamrc(op_gt , Py_GT)
113 spamrc(op_ge , Py_GE)
115 static PyObject*
116 op_pow(PyObject *s, PyObject *a)
118 PyObject *a1, *a2;
119 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
120 return PyNumber_Power(a1, a2, Py_None);
121 return NULL;
124 static PyObject*
125 op_ipow(PyObject *s, PyObject *a)
127 PyObject *a1, *a2;
128 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
129 return PyNumber_InPlacePower(a1, a2, Py_None);
130 return NULL;
133 static PyObject*
134 is_(PyObject *s, PyObject *a)
136 PyObject *a1, *a2, *result = NULL;
137 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
138 result = (a1 == a2) ? Py_True : Py_False;
139 Py_INCREF(result);
141 return result;
144 static PyObject*
145 is_not(PyObject *s, PyObject *a)
147 PyObject *a1, *a2, *result = NULL;
148 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
149 result = (a1 != a2) ? Py_True : Py_False;
150 Py_INCREF(result);
152 return result;
155 static PyObject*
156 op_getslice(PyObject *s, PyObject *a)
158 PyObject *a1;
159 int a2,a3;
161 if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
162 return NULL;
163 return PySequence_GetSlice(a1,a2,a3);
166 static PyObject*
167 op_setslice(PyObject *s, PyObject *a)
169 PyObject *a1, *a4;
170 int a2,a3;
172 if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
173 return NULL;
175 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
176 return NULL;
178 Py_INCREF(Py_None);
179 return Py_None;
182 static PyObject*
183 op_delslice(PyObject *s, PyObject *a)
185 PyObject *a1;
186 int a2,a3;
188 if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
189 return NULL;
191 if (-1 == PySequence_DelSlice(a1,a2,a3))
192 return NULL;
194 Py_INCREF(Py_None);
195 return Py_None;
198 #undef spam1
199 #undef spam2
200 #undef spam1o
201 #undef spam1o
202 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
203 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
204 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
205 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
206 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
207 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
209 static struct PyMethodDef operator_methods[] = {
211 spam1o(isCallable,
212 "isCallable(a) -- Same as callable(a).")
213 spam1o(isNumberType,
214 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
215 spam1o(isSequenceType,
216 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
217 spam1o(truth,
218 "truth(a) -- Return True if a is true, False otherwise.")
219 spam2(contains,__contains__,
220 "contains(a, b) -- Same as b in a (note reversed operands).")
221 spam1(sequenceIncludes,
222 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
223 spam1(indexOf,
224 "indexOf(a, b) -- Return the first index of b in a.")
225 spam1(countOf,
226 "countOf(a, b) -- Return the number of times b occurs in a.")
227 spam1o(isMappingType,
228 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
230 spam1(is_, "is_(a, b) -- Same as a is b.")
231 spam1(is_not, "is_not(a, b) -- Same as a is not b.")
232 spam2(add,__add__, "add(a, b) -- Same as a + b.")
233 spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
234 spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
235 spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
236 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
237 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
238 spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
239 spam2o(neg,__neg__, "neg(a) -- Same as -a.")
240 spam2o(pos,__pos__, "pos(a) -- Same as +a.")
241 spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
242 spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
243 spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
244 spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
245 spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
246 spam2o(not_,__not__, "not_(a) -- Same as not a.")
247 spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
248 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
249 spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
250 spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
251 spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
252 spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
253 spam2(idiv,__idiv__, "idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
254 spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
255 spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
256 spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
257 spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
258 spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
259 spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
260 spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
261 spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
262 spam2(concat,__concat__,
263 "concat(a, b) -- Same as a + b, for a and b sequences.")
264 spam2(repeat,__repeat__,
265 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
266 spam2(iconcat,__iconcat__,
267 "iconcat(a, b) -- Same as a += b, for a and b sequences.")
268 spam2(irepeat,__irepeat__,
269 "irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
270 spam2(getitem,__getitem__,
271 "getitem(a, b) -- Same as a[b].")
272 spam2(setitem,__setitem__,
273 "setitem(a, b, c) -- Same as a[b] = c.")
274 spam2(delitem,__delitem__,
275 "delitem(a, b) -- Same as del a[b].")
276 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
277 spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
278 spam2(getslice,__getslice__,
279 "getslice(a, b, c) -- Same as a[b:c].")
280 spam2(setslice,__setslice__,
281 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
282 spam2(delslice,__delslice__,
283 "delslice(a, b, c) -- Same as del a[b:c].")
284 spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
285 spam2(le,__le__, "le(a, b) -- Same as a<=b.")
286 spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
287 spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
288 spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
289 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
291 {NULL, NULL} /* sentinel */
295 /* itemgetter object **********************************************************/
297 typedef struct {
298 PyObject_HEAD
299 int nitems;
300 PyObject *item;
301 } itemgetterobject;
303 static PyTypeObject itemgetter_type;
305 static PyObject *
306 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
308 itemgetterobject *ig;
309 PyObject *item;
310 int nitems;
312 if (!_PyArg_NoKeywords("itemgetter()", kwds))
313 return NULL;
315 nitems = PyTuple_GET_SIZE(args);
316 if (nitems <= 1) {
317 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
318 return NULL;
319 } else
320 item = args;
322 /* create itemgetterobject structure */
323 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
324 if (ig == NULL)
325 return NULL;
327 Py_INCREF(item);
328 ig->item = item;
329 ig->nitems = nitems;
331 PyObject_GC_Track(ig);
332 return (PyObject *)ig;
335 static void
336 itemgetter_dealloc(itemgetterobject *ig)
338 PyObject_GC_UnTrack(ig);
339 Py_XDECREF(ig->item);
340 PyObject_GC_Del(ig);
343 static int
344 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
346 if (ig->item)
347 return visit(ig->item, arg);
348 return 0;
351 static PyObject *
352 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
354 PyObject *obj, *result;
355 int i, nitems=ig->nitems;
357 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
358 return NULL;
359 if (nitems == 1)
360 return PyObject_GetItem(obj, ig->item);
362 assert(PyTuple_Check(ig->item));
363 assert(PyTuple_GET_SIZE(ig->item) == nitems);
365 result = PyTuple_New(nitems);
366 if (result == NULL)
367 return NULL;
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;
377 PyTuple_SET_ITEM(result, i, val);
379 return result;
382 PyDoc_STRVAR(itemgetter_doc,
383 "itemgetter(item, ...) --> itemgetter object\n\
385 Return a callable object that fetches the given item(s) from its operand.\n\
386 After, f=itemgetter(2), the call f(r) returns r[2].\n\
387 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
389 static PyTypeObject itemgetter_type = {
390 PyObject_HEAD_INIT(NULL)
391 0, /* ob_size */
392 "operator.itemgetter", /* tp_name */
393 sizeof(itemgetterobject), /* tp_basicsize */
394 0, /* tp_itemsize */
395 /* methods */
396 (destructor)itemgetter_dealloc, /* tp_dealloc */
397 0, /* tp_print */
398 0, /* tp_getattr */
399 0, /* tp_setattr */
400 0, /* tp_compare */
401 0, /* tp_repr */
402 0, /* tp_as_number */
403 0, /* tp_as_sequence */
404 0, /* tp_as_mapping */
405 0, /* tp_hash */
406 (ternaryfunc)itemgetter_call, /* tp_call */
407 0, /* tp_str */
408 PyObject_GenericGetAttr, /* tp_getattro */
409 0, /* tp_setattro */
410 0, /* tp_as_buffer */
411 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
412 itemgetter_doc, /* tp_doc */
413 (traverseproc)itemgetter_traverse, /* tp_traverse */
414 0, /* tp_clear */
415 0, /* tp_richcompare */
416 0, /* tp_weaklistoffset */
417 0, /* tp_iter */
418 0, /* tp_iternext */
419 0, /* tp_methods */
420 0, /* tp_members */
421 0, /* tp_getset */
422 0, /* tp_base */
423 0, /* tp_dict */
424 0, /* tp_descr_get */
425 0, /* tp_descr_set */
426 0, /* tp_dictoffset */
427 0, /* tp_init */
428 0, /* tp_alloc */
429 itemgetter_new, /* tp_new */
430 0, /* tp_free */
434 /* attrgetter object **********************************************************/
436 typedef struct {
437 PyObject_HEAD
438 int nattrs;
439 PyObject *attr;
440 } attrgetterobject;
442 static PyTypeObject attrgetter_type;
444 static PyObject *
445 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
447 attrgetterobject *ag;
448 PyObject *attr;
449 int nattrs;
451 if (!_PyArg_NoKeywords("attrgetter()", kwds))
452 return NULL;
454 nattrs = PyTuple_GET_SIZE(args);
455 if (nattrs <= 1) {
456 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
457 return NULL;
458 } else
459 attr = args;
461 /* create attrgetterobject structure */
462 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
463 if (ag == NULL)
464 return NULL;
466 Py_INCREF(attr);
467 ag->attr = attr;
468 ag->nattrs = nattrs;
470 PyObject_GC_Track(ag);
471 return (PyObject *)ag;
474 static void
475 attrgetter_dealloc(attrgetterobject *ag)
477 PyObject_GC_UnTrack(ag);
478 Py_XDECREF(ag->attr);
479 PyObject_GC_Del(ag);
482 static int
483 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
485 if (ag->attr)
486 return visit(ag->attr, arg);
487 return 0;
490 static PyObject *
491 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
493 PyObject *obj, *result;
494 int i, nattrs=ag->nattrs;
496 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
497 return NULL;
498 if (ag->nattrs == 1)
499 return PyObject_GetAttr(obj, ag->attr);
501 assert(PyTuple_Check(ag->attr));
502 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
504 result = PyTuple_New(nattrs);
505 if (result == NULL)
506 return NULL;
508 for (i=0 ; i < nattrs ; i++) {
509 PyObject *attr, *val;
510 attr = PyTuple_GET_ITEM(ag->attr, i);
511 val = PyObject_GetAttr(obj, attr);
512 if (val == NULL) {
513 Py_DECREF(result);
514 return NULL;
516 PyTuple_SET_ITEM(result, i, val);
518 return result;
521 PyDoc_STRVAR(attrgetter_doc,
522 "attrgetter(attr, ...) --> attrgetter object\n\
524 Return a callable object that fetches the given attribute(s) from its operand.\n\
525 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
526 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).");
528 static PyTypeObject attrgetter_type = {
529 PyObject_HEAD_INIT(NULL)
530 0, /* ob_size */
531 "operator.attrgetter", /* tp_name */
532 sizeof(attrgetterobject), /* tp_basicsize */
533 0, /* tp_itemsize */
534 /* methods */
535 (destructor)attrgetter_dealloc, /* tp_dealloc */
536 0, /* tp_print */
537 0, /* tp_getattr */
538 0, /* tp_setattr */
539 0, /* tp_compare */
540 0, /* tp_repr */
541 0, /* tp_as_number */
542 0, /* tp_as_sequence */
543 0, /* tp_as_mapping */
544 0, /* tp_hash */
545 (ternaryfunc)attrgetter_call, /* tp_call */
546 0, /* tp_str */
547 PyObject_GenericGetAttr, /* tp_getattro */
548 0, /* tp_setattro */
549 0, /* tp_as_buffer */
550 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
551 attrgetter_doc, /* tp_doc */
552 (traverseproc)attrgetter_traverse, /* tp_traverse */
553 0, /* tp_clear */
554 0, /* tp_richcompare */
555 0, /* tp_weaklistoffset */
556 0, /* tp_iter */
557 0, /* tp_iternext */
558 0, /* tp_methods */
559 0, /* tp_members */
560 0, /* tp_getset */
561 0, /* tp_base */
562 0, /* tp_dict */
563 0, /* tp_descr_get */
564 0, /* tp_descr_set */
565 0, /* tp_dictoffset */
566 0, /* tp_init */
567 0, /* tp_alloc */
568 attrgetter_new, /* tp_new */
569 0, /* tp_free */
571 /* Initialization function for the module (*must* be called initoperator) */
573 PyMODINIT_FUNC
574 initoperator(void)
576 PyObject *m;
578 /* Create the module and add the functions */
579 m = Py_InitModule4("operator", operator_methods, operator_doc,
580 (PyObject*)NULL, PYTHON_API_VERSION);
581 if (m == NULL)
582 return;
584 if (PyType_Ready(&itemgetter_type) < 0)
585 return;
586 Py_INCREF(&itemgetter_type);
587 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
589 if (PyType_Ready(&attrgetter_type) < 0)
590 return;
591 Py_INCREF(&attrgetter_type);
592 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);