blob: d73744d5116d02f45a57da8cd1377ad3e6f949a8 [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
11/*
12
13 Copyright
14
15 Copyright 1996 Digital Creations, L.C., 910 Princess Anne
16 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
17 rights reserved. Copyright in this software is owned by DCLC,
18 unless otherwise indicated. Permission to use, copy and
19 distribute this software is hereby granted, provided that the
20 above copyright notice appear in all copies and that both that
21 copyright notice and this permission notice appear. Note that
22 any product, process or technology described in this software
23 may be the subject of other Intellectual Property rights
24 reserved by Digital Creations, L.C. and are not licensed
25 hereunder.
26
27 Trademarks
28
29 Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
30 All other trademarks are owned by their respective companies.
31
32 No Warranty
33
34 The software is provided "as is" without warranty of any kind,
35 either express or implied, including, but not limited to, the
36 implied warranties of merchantability, fitness for a particular
37 purpose, or non-infringement. This software could include
38 technical inaccuracies or typographical errors. Changes are
39 periodically made to the software; these changes will be
40 incorporated in new editions of the software. DCLC may make
41 improvements and/or changes in this software at any time
42 without notice.
43
44 Limitation Of Liability
45
46 In no event will DCLC be liable for direct, indirect, special,
47 incidental, economic, cover, or consequential damages arising
48 out of the use of or inability to use this software even if
49 advised of the possibility of such damages. Some states do not
50 allow the exclusion or limitation of implied warranties or
51 limitation of liability for incidental or consequential
52 damages, so the above limitation or exclusion may not apply to
53 you.
54
55
56 If you have questions regarding this software,
57 contact:
58
59 Jim Fulton, jim@digicool.com
60 Digital Creations L.C.
61
62 (540) 371-6909
63
64 Modifications
65
66 Renamed and slightly rearranged by Guido van Rossum
67
68*/
69
70#include "Python.h"
71
Fred Drake5639ba42000-07-08 04:12:08 +000072#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000073 PyObject *a1; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000074 if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000075 return AOP(a1); }
76
Fred Drake5639ba42000-07-08 04:12:08 +000077#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000078 PyObject *a1, *a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000079 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000080 return AOP(a1,a2); }
81
Fred Drake5639ba42000-07-08 04:12:08 +000082#define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000083 PyObject *a1; int a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000084 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000085 return AOP(a1,a2); }
86
Fred Drake5639ba42000-07-08 04:12:08 +000087#define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000088 PyObject *a1, *a2; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000089 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000090 if(-1 == AOP(a1,a2)) return NULL; \
91 Py_INCREF(Py_None); \
92 return Py_None; }
93
Fred Drake5639ba42000-07-08 04:12:08 +000094#define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +000095 PyObject *a1, *a2, *a3; \
Fred Drakeea4d3f02000-09-17 16:09:27 +000096 if(! PyArg_ParseTuple(a,"OOO:" #OP,&a1,&a2,&a3)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +000097 if(-1 == AOP(a1,a2,a3)) return NULL; \
98 Py_INCREF(Py_None); \
99 return Py_None; }
100
Fred Drake5639ba42000-07-08 04:12:08 +0000101#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +0000102 PyObject *a1; long r; \
Fred Drakeea4d3f02000-09-17 16:09:27 +0000103 if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +0000104 if(-1 == (r=AOP(a1))) return NULL; \
Guido van Rossum77f6a652002-04-03 22:41:51 +0000105 return PyBool_FromLong(r); }
Guido van Rossum037b9401996-07-30 16:55:54 +0000106
Fred Drake5639ba42000-07-08 04:12:08 +0000107#define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
Guido van Rossum037b9401996-07-30 16:55:54 +0000108 PyObject *a1, *a2; long r; \
Fred Drakeea4d3f02000-09-17 16:09:27 +0000109 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
Guido van Rossum037b9401996-07-30 16:55:54 +0000110 if(-1 == (r=AOP(a1,a2))) return NULL; \
111 return PyInt_FromLong(r); }
112
Guido van Rossum77f6a652002-04-03 22:41:51 +0000113#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
114 PyObject *a1, *a2; long r; \
115 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
116 if(-1 == (r=AOP(a1,a2))) return NULL; \
117 return PyBool_FromLong(r); }
118
Fred Drake428e75f2001-08-09 20:14:34 +0000119#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
120 PyObject *a1, *a2; \
121 if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
122 return PyObject_RichCompare(a1,a2,A); }
123
Guido van Rossum037b9401996-07-30 16:55:54 +0000124spami(isCallable , PyCallable_Check)
125spami(isNumberType , PyNumber_Check)
126spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000127spam2(op_add , PyNumber_Add)
128spam2(op_sub , PyNumber_Subtract)
129spam2(op_mul , PyNumber_Multiply)
130spam2(op_div , PyNumber_Divide)
Fred Drake428e75f2001-08-09 20:14:34 +0000131spam2(op_floordiv , PyNumber_FloorDivide)
132spam2(op_truediv , PyNumber_TrueDivide)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000133spam2(op_mod , PyNumber_Remainder)
134spam1(op_neg , PyNumber_Negative)
135spam1(op_pos , PyNumber_Positive)
136spam1(op_abs , PyNumber_Absolute)
137spam1(op_inv , PyNumber_Invert)
Fred Drakeea4d3f02000-09-17 16:09:27 +0000138spam1(op_invert , PyNumber_Invert)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000139spam2(op_lshift , PyNumber_Lshift)
140spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +0000141spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000142spam2(op_and_ , PyNumber_And)
143spam2(op_xor , PyNumber_Xor)
144spam2(op_or_ , PyNumber_Or)
Guido van Rossum037b9401996-07-30 16:55:54 +0000145spami(isSequenceType , PySequence_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000146spam2(op_concat , PySequence_Concat)
147spamoi(op_repeat , PySequence_Repeat)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000148spami2b(op_contains , PySequence_Contains)
149spami2b(sequenceIncludes, PySequence_Contains)
Guido van Rossum037b9401996-07-30 16:55:54 +0000150spami2(indexOf , PySequence_Index)
151spami2(countOf , PySequence_Count)
152spami(isMappingType , PyMapping_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000153spam2(op_getitem , PyObject_GetItem)
154spam2n(op_delitem , PyObject_DelItem)
155spam3n(op_setitem , PyObject_SetItem)
Fred Drake428e75f2001-08-09 20:14:34 +0000156spamrc(op_lt , Py_LT)
157spamrc(op_le , Py_LE)
158spamrc(op_eq , Py_EQ)
159spamrc(op_ne , Py_NE)
160spamrc(op_gt , Py_GT)
161spamrc(op_ge , Py_GE)
Guido van Rossum037b9401996-07-30 16:55:54 +0000162
163static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000164op_getslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000165{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000166 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000167 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000168
Fred Drakeea4d3f02000-09-17 16:09:27 +0000169 if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000170 return NULL;
171 return PySequence_GetSlice(a1,a2,a3);
Guido van Rossum037b9401996-07-30 16:55:54 +0000172}
173
174static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000175op_setslice(PyObject *s, PyObject *a)
Guido van Rossum037b9401996-07-30 16:55:54 +0000176{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000177 PyObject *a1, *a4;
Guido van Rossum24a49941997-08-28 18:11:05 +0000178 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000179
Fred Drakeea4d3f02000-09-17 16:09:27 +0000180 if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000181 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000182
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000183 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
184 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000185
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000186 Py_INCREF(Py_None);
187 return Py_None;
Guido van Rossum037b9401996-07-30 16:55:54 +0000188}
189
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000190static PyObject*
Fred Drake5639ba42000-07-08 04:12:08 +0000191op_delslice(PyObject *s, PyObject *a)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000192{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000193 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000194 int a2,a3;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000195
Fred Drakeea4d3f02000-09-17 16:09:27 +0000196 if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000197 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000198
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000199 if (-1 == PySequence_DelSlice(a1,a2,a3))
200 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000201
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000202 Py_INCREF(Py_None);
203 return Py_None;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000204}
205
Guido van Rossum037b9401996-07-30 16:55:54 +0000206#undef spam1
207#undef spam2
Fred Drake5639ba42000-07-08 04:12:08 +0000208#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, DOC},
209#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, DOC}, \
210 {#ALTOP, op_##OP, METH_VARARGS, DOC},
Guido van Rossum037b9401996-07-30 16:55:54 +0000211
212static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000213
Guido van Rossum17202301996-08-19 22:01:39 +0000214spam1(isCallable,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000215 "isCallable(a) -- Same as callable(a).")
Guido van Rossum17202301996-08-19 22:01:39 +0000216spam1(isNumberType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000217 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000218spam1(isSequenceType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000219 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000220spam1(truth,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000221 "truth(a) -- Return True if a is true, False otherwise.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000222spam2(contains,__contains__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000223 "contains(a, b) -- Same as b in a (note reversed operands).")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000224spam1(sequenceIncludes,
225 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
Guido van Rossum17202301996-08-19 22:01:39 +0000226spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000227 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000228spam1(countOf,
229 "countOf(a, b) -- Return the number of times b occurs in a.")
230spam1(isMappingType,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000231 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000232
Guido van Rossum832f6d21998-05-22 18:12:59 +0000233spam2(add,__add__, "add(a, b) -- Same as a + b.")
234spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
235spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
Fred Drake428e75f2001-08-09 20:14:34 +0000236spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
237spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
238spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000239spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
240spam2(neg,__neg__, "neg(a) -- Same as -a.")
241spam2(pos,__pos__, "pos(a) -- Same as +a.")
242spam2(abs,__abs__, "abs(a) -- Same as abs(a).")
243spam2(inv,__inv__, "inv(a) -- Same as ~a.")
Fred Drakeea4d3f02000-09-17 16:09:27 +0000244spam2(invert,__invert__, "invert(a) -- Same as ~a.")
Guido van Rossum832f6d21998-05-22 18:12:59 +0000245spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
246spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
247spam2(not_,__not__, "not_(a) -- Same as not a.")
248spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
249spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
250spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000251spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000252 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000253spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000254 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Guido van Rossum17202301996-08-19 22:01:39 +0000255spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000256 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000257spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000258 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000259spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000260 "delitem(a, b) -- Same as del a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000261spam2(getslice,__getslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000262 "getslice(a, b, c) -- Same as a[b:c].")
Guido van Rossum17202301996-08-19 22:01:39 +0000263spam2(setslice,__setslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000264"setslice(a, b, c, d) -- Same as a[b:c] = d.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000265spam2(delslice,__delslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000266"delslice(a, b, c) -- Same as del a[b:c].")
Fred Drake428e75f2001-08-09 20:14:34 +0000267spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
268spam2(le,__le__, "le(a, b) -- Same as a<=b.")
269spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
270spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
271spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
272spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000273
Guido van Rossum037b9401996-07-30 16:55:54 +0000274 {NULL, NULL} /* sentinel */
275
Guido van Rossum037b9401996-07-30 16:55:54 +0000276};
277
278
279/* Initialization function for the module (*must* be called initoperator) */
280
Guido van Rossum3886bb61998-12-04 18:50:17 +0000281DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000282initoperator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +0000283{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000284 /* Create the module and add the functions */
Guido van Rossum832f6d21998-05-22 18:12:59 +0000285 Py_InitModule4("operator", operator_methods, operator_doc,
286 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum037b9401996-07-30 16:55:54 +0000287}