source: trunk/essentials/dev-lang/python/Modules/operator.c@ 3403

Last change on this file since 3403 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 17.6 KB
Line 
1
2#include "Python.h"
3
4PyDoc_STRVAR(operator_doc,
5"Operator interface.\n\
6\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\
11'__' are also provided for convenience.");
12
13#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
14 return AOP(a1); }
15
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); }
20
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); }
25
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; }
32
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; }
39
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); }
44
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); }
50
51#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
57#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
58 PyObject *a1, *a2; long r; \
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
60 if(-1 == (r=AOP(a1,a2))) return NULL; \
61 return PyBool_FromLong(r); }
62
63#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
64 PyObject *a1, *a2; \
65 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
66 return PyObject_RichCompare(a1,a2,A); }
67
68spami(isCallable , PyCallable_Check)
69spami(isNumberType , PyNumber_Check)
70spami(truth , PyObject_IsTrue)
71spam2(op_add , PyNumber_Add)
72spam2(op_sub , PyNumber_Subtract)
73spam2(op_mul , PyNumber_Multiply)
74spam2(op_div , PyNumber_Divide)
75spam2(op_floordiv , PyNumber_FloorDivide)
76spam2(op_truediv , PyNumber_TrueDivide)
77spam2(op_mod , PyNumber_Remainder)
78spam1(op_neg , PyNumber_Negative)
79spam1(op_pos , PyNumber_Positive)
80spam1(op_abs , PyNumber_Absolute)
81spam1(op_inv , PyNumber_Invert)
82spam1(op_invert , PyNumber_Invert)
83spam2(op_lshift , PyNumber_Lshift)
84spam2(op_rshift , PyNumber_Rshift)
85spami(op_not_ , PyObject_Not)
86spam2(op_and_ , PyNumber_And)
87spam2(op_xor , PyNumber_Xor)
88spam2(op_or_ , PyNumber_Or)
89spam2(op_iadd , PyNumber_InPlaceAdd)
90spam2(op_isub , PyNumber_InPlaceSubtract)
91spam2(op_imul , PyNumber_InPlaceMultiply)