| 1 |
|
|---|
| 2 | #include "Python.h"
|
|---|
| 3 |
|
|---|
| 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.");
|
|---|
| 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 |
|
|---|
| 68 | spami(isCallable , PyCallable_Check)
|
|---|
| 69 | spami(isNumberType , PyNumber_Check)
|
|---|
| 70 | spami(truth , PyObject_IsTrue)
|
|---|
| 71 | spam2(op_add , PyNumber_Add)
|
|---|
| 72 | spam2(op_sub , PyNumber_Subtract)
|
|---|
| 73 | spam2(op_mul , PyNumber_Multiply)
|
|---|
| 74 | spam2(op_div , PyNumber_Divide)
|
|---|
| 75 | spam2(op_floordiv , PyNumber_FloorDivide)
|
|---|
| 76 | spam2(op_truediv , PyNumber_TrueDivide)
|
|---|
| 77 | spam2(op_mod , PyNumber_Remainder)
|
|---|
| 78 | spam1(op_neg , PyNumber_Negative)
|
|---|
| 79 | spam1(op_pos , PyNumber_Positive)
|
|---|
| 80 | spam1(op_abs , PyNumber_Absolute)
|
|---|
| 81 | spam1(op_inv , PyNumber_Invert)
|
|---|
| 82 | spam1(op_invert , PyNumber_Invert)
|
|---|
| 83 | spam2(op_lshift , PyNumber_Lshift)
|
|---|
| 84 | spam2(op_rshift , PyNumber_Rshift)
|
|---|
| 85 | spami(op_not_ , PyObject_Not)
|
|---|
| 86 | spam2(op_and_ , PyNumber_And)
|
|---|
| 87 | spam2(op_xor , PyNumber_Xor)
|
|---|
| 88 | spam2(op_or_ , PyNumber_Or)
|
|---|
| 89 | spam2(op_iadd , PyNumber_InPlaceAdd)
|
|---|
| 90 | spam2(op_isub , PyNumber_InPlaceSubtract)
|
|---|
| 91 | spam2(op_imul , PyNumber_InPlaceMultiply)
|
|---|
|
|---|