blob: d1ea181ef494a5b0aab567b435c8063504dfdba6 [file] [log] [blame]
Guido van Rossum037b9401996-07-30 16:55:54 +00001static char operator_doc[] = "\
2Operator interface.\n\
3\n\
4This module exports a set of functions implemented in C corresponding\n\
5to the intrinsic operators of Python. For example, operator.add(x, y)\n\
6is equivalent to the expression x+y. The function names are those\n\
7used for special class methods; variants without leading and trailing\n\
8'__' are also provided for convenience.\n\
9";
10
Guido van Rossum037b9401996-07-30 16:55:54 +000011#include "Python.h"
12
Fred Drake5639ba42000-07-08 04:12:08 +000013#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000014 PyObject *a1; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000015 if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000016 return AOP(a1); }
17
Fred Drake5639ba42000-07-08 04:12:08 +000018#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000019 PyObject *a1, *a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000020 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000021 return AOP(a1,a2); }
22
Fred Drake5639ba42000-07-08 04:12:08 +000023#define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000024 PyObject *a1; int a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000025 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000026 return AOP(a1,a2); }
27
Fred Drake5639ba42000-07-08 04:12:08 +000028#define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000029 PyObject *a1, *a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000030 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000031 if(-1 == AOP(a1,a2)) return NULL; \
32 Py_INCREF(Py_None); \
33 return Py_None; }
34
Fred Drake5639ba42000-07-08 04:12:08 +000035#define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000036 PyObject *a1, *a2, *a3; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000037 if(! PyArg_ParseTuple(a,"OOO:" #OP,&a1,&a2,&a3)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000038 if(-1 == AOP(a1,a2,a3)) return NULL; \
39 Py_INCREF(Py_None); \
40 return Py_None; }
41
Fred Drake5639ba42000-07-08 04:12:08 +000042#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000043 PyObject *a1; long r; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000044 if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000045 if(-1 == (r=AOP(a1))) return NULL; \
Guido van Rossum77f6a652002-04-03 22:41:51 +000046 return PyBool_FromLong(r); }
Guido van Rossum037b9401996-07-30 16:55:54 +000047
Fred Drake5639ba42000-07-08 04:12:08 +000048#define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000049 PyObject *a1, *a2; long r; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000050 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000051 if(-1 == (r=AOP(a1,a2))) return NULL; \
52 return PyInt_FromLong(r); }
53
Guido van Rossum77f6a652002-04-03 22:41:51 +000054#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
55 PyObject *a1, *a2; long r; \
56 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
57 if(-1 == (r=AOP(a1,a2))) return NULL; \
58 return PyBool_FromLong(r); }
59
Fred Drake428e75f2001-08-09 20:14:34 +000060#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
61 PyObject *a1, *a2; \
62 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
63 return PyObject_RichCompare(a1,a2,A); }
64
Guido van Rossum037b9401996-07-30 16:55:54 +000065spami(isCallable , PyCallable_Check)
66spami(isNumberType , PyNumber_Check)
67spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000068spam2(op_add , PyNumber_Add)
69spam2(op_sub , PyNumber_Subtract)
70spam2(op_mul , PyNumber_Multiply)
71spam2(op_div , PyNumber_Divide)
Fred Drake428e75f2001-08-09 20:14:34 +000072spam2(op_floordiv , PyNumber_FloorDivide)
73spam2(op_truediv , PyNumber_TrueDivide)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000074spam2(op_mod , PyNumber_Remainder)
75spam1(op_neg , PyNumber_Negative)
76spam1(op_pos , PyNumber_Positive)
77spam1(op_abs , PyNumber_Absolute)
78spam1(op_inv , PyNumber_Invert)
Fred Drakeea4d3f02000-09-17 16:09:27 +000079spam1(op_invert , PyNumber_Invert)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000080spam2(op_lshift , PyNumber_Lshift)
81spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +000082spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000083spam2(op_and_ , PyNumber_And)
84spam2(op_xor , PyNumber_Xor)
85spam2(op_or_ , PyNumber_Or)
Guido van Rossum037b9401996-07-30 16:55:54 +000086spami(isSequenceType , PySequence_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000087spam2(op_concat , PySequence_Concat)
88spamoi(op_repeat , PySequence_Repeat)
Guido van Rossum77f6a652002-04-03 22:41:51 +000089spami2b(op_contains , PySequence_Contains)
90spami2b(sequenceIncludes, PySequence_Contains)
Guido van Rossum037b9401996-07-30 16:55:54 +000091spami2(indexOf , PySequence_Index)
92spami2(countOf , PySequence_Count)
93spami(isMappingType , PyMapping_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +000094spam2(op_getitem , PyObject_GetItem)
95spam2n(op_delitem , PyObject_DelItem)
96spam3n(op_setitem , PyObject_SetItem)
Fred Drake428e75f2001-08-09 20:14:34 +000097spamrc(op_lt , Py_LT)
98spamrc(op_le , Py_LE)
99spamrc(op_eq , Py_EQ)
100spamrc(op_ne , Py_NE)
101spamrc(op_gt , Py_GT)
102spamrc(op_ge , Py_GE)
Guido van Rossum037b9401996-07-30 16:55:54 +0000103
104static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000105op_getslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000106{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000107 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000108 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000109
Fred Drakeea4d3f02000-09-17 16:09:27 +0000110 if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000111 return NULL;
112 return PySequence_GetSlice(a1,a2,a3);
Guido van Rossum037b9401996-07-30 16:55:54 +0000113}
114
115static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000116op_setslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000117{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000118 PyObject *a1, *a4;
Guido van Rossum24a49941997-08-28 18:11:05 +0000119 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000120
Fred Drakeea4d3f02000-09-17 16:09:27 +0000121 if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000122 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000123
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000124 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
125 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000126
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000127 Py_INCREF(Py_None);
128 return Py_None;
Guido van Rossum037b9401996-07-30 16:55:54 +0000129}
130
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000131static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000132op_delslice(PyObject *s, PyObject *a)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000133{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000134 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000135 int a2,a3;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000136
Fred Drakeea4d3f02000-09-17 16:09:27 +0000137 if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000138 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000139
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000140 if (-1 == PySequence_DelSlice(a1,a2,a3))
141 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000142
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000143 Py_INCREF(Py_None);
144 return Py_None;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000145}
146
Guido van Rossum037b9401996-07-30 16:55:54 +0000147#undef spam1
148#undef spam2
Fred Drake5639ba42000-07-08 04:12:08 +0000149#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, DOC},
150#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, DOC}, \
151 {#ALTOP, op_##OP, METH_VARARGS, DOC},
Guido van Rossum037b9401996-07-30 16:55:54 +0000152
153static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000154
Guido van Rossum17202301996-08-19 22:01:39 +0000155spam1(isCallable,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000156 "isCallable(a) -- Same as callable(a).")
Guido van Rossum17202301996-08-19 22:01:39 +0000157spam1(isNumberType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000158 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000159spam1(isSequenceType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000160 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000161spam1(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000162 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000163spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000164 "contains(a, b) -- Same as b in a (note reversed operands).")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000165spam1(sequenceIncludes,
166 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
Guido van Rossum17202301996-08-19 22:01:39 +0000167spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000168 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000169spam1(countOf,
170 "countOf(a, b) -- Return the number of times b occurs in a.")
171spam1(isMappingType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000172 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000173
Guido van Rossum832f6d21998-05-22 18:12:59 +0000174spam2(add,__add__, "add(a, b) -- Same as a + b.")
175spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
176spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000177spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
178spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
179spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000180spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
181spam2(neg,__neg__, "neg(a) -- Same as -a.")
182spam2(pos,__pos__, "pos(a) -- Same as +a.")
183spam2(abs,__abs__, "abs(a) -- Same as abs(a).")
184spam2(inv,__inv__, "inv(a) -- Same as ~a.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000185spam2(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000186spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
187spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
188spam2(not_,__not__, "not_(a) -- Same as not a.")
189spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
190spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
191spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000192spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000193 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000194spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000195 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Guido van Rossum17202301996-08-19 22:01:39 +0000196spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000197 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000198spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000199 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000200spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000201 "delitem(a, b) -- Same as del a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000202spam2(getslice,__getslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000203 "getslice(a, b, c) -- Same as a[b:c].")
Guido van Rossum17202301996-08-19 22:01:39 +0000204spam2(setslice,__setslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000205"setslice(a, b, c, d) -- Same as a[b:c] = d.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000206spam2(delslice,__delslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000207"delslice(a, b, c) -- Same as del a[b:c].")
Fred Drake428e75f2001-08-09 20:14:34 +0000208spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
209spam2(le,__le__, "le(a, b) -- Same as a<=b.")
210spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
211spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
212spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
213spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000214
Guido van Rossum037b9401996-07-30 16:55:54 +0000215 {NULL, NULL} /* sentinel */
216
Guido van Rossum037b9401996-07-30 16:55:54 +0000217};
218
219
220/* Initialization function for the module (*must* be called initoperator) */
221
Guido van Rossum3886bb61998-12-04 18:50:17 +0000222DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000223initoperator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000224{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000225 /* Create the module and add the functions */
Guido van Rossum832f6d21998-05-22 18:12:59 +0000226 Py_InitModule4("operator", operator_methods, operator_doc,
227 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum037b9401996-07-30 16:55:54 +0000228}