blob: 8b3c56be9002e822c3fa50ed376f43eeeb1fe369 [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
72#define spam1(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
73 PyObject *a1; \
74 if(! PyArg_ParseTuple(a,"O",&a1)) return NULL; \
75 return AOP(a1); }
76
77#define spam2(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
78 PyObject *a1, *a2; \
79 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
80 return AOP(a1,a2); }
81
82#define spamoi(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
83 PyObject *a1; int a2; \
84 if(! PyArg_ParseTuple(a,"Oi",&a1,&a2)) return NULL; \
85 return AOP(a1,a2); }
86
87#define spam2n(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
88 PyObject *a1, *a2; \
89 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
90 if(-1 == AOP(a1,a2)) return NULL; \
91 Py_INCREF(Py_None); \
92 return Py_None; }
93
94#define spam3n(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
95 PyObject *a1, *a2, *a3; \
96 if(! PyArg_ParseTuple(a,"OOO",&a1,&a2,&a3)) return NULL; \
97 if(-1 == AOP(a1,a2,a3)) return NULL; \
98 Py_INCREF(Py_None); \
99 return Py_None; }
100
101#define spami(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
102 PyObject *a1; long r; \
103 if(! PyArg_ParseTuple(a,"O",&a1)) return NULL; \
104 if(-1 == (r=AOP(a1))) return NULL; \
105 return PyInt_FromLong(r); }
106
107#define spami2(OP,AOP) static PyObject *OP(s,a) PyObject *s, *a; { \
108 PyObject *a1, *a2; long r; \
109 if(! PyArg_ParseTuple(a,"OO",&a1,&a2)) return NULL; \
110 if(-1 == (r=AOP(a1,a2))) return NULL; \
111 return PyInt_FromLong(r); }
112
Guido van Rossum037b9401996-07-30 16:55:54 +0000113spami(isCallable , PyCallable_Check)
114spami(isNumberType , PyNumber_Check)
115spami(truth , PyObject_IsTrue)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000116spam2(op_add , PyNumber_Add)
117spam2(op_sub , PyNumber_Subtract)
118spam2(op_mul , PyNumber_Multiply)
119spam2(op_div , PyNumber_Divide)
120spam2(op_mod , PyNumber_Remainder)
121spam1(op_neg , PyNumber_Negative)
122spam1(op_pos , PyNumber_Positive)
123spam1(op_abs , PyNumber_Absolute)
124spam1(op_inv , PyNumber_Invert)
125spam2(op_lshift , PyNumber_Lshift)
126spam2(op_rshift , PyNumber_Rshift)
Guido van Rossum99c185e1998-04-09 17:54:26 +0000127spami(op_not_ , PyObject_Not)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000128spam2(op_and_ , PyNumber_And)
129spam2(op_xor , PyNumber_Xor)
130spam2(op_or_ , PyNumber_Or)
Guido van Rossum037b9401996-07-30 16:55:54 +0000131spami(isSequenceType , PySequence_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000132spam2(op_concat , PySequence_Concat)
133spamoi(op_repeat , PySequence_Repeat)
Guido van Rossum832f6d21998-05-22 18:12:59 +0000134spami2(op_contains , PySequence_Contains)
Guido van Rossum037b9401996-07-30 16:55:54 +0000135spami2(indexOf , PySequence_Index)
136spami2(countOf , PySequence_Count)
137spami(isMappingType , PyMapping_Check)
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000138spam2(op_getitem , PyObject_GetItem)
139spam2n(op_delitem , PyObject_DelItem)
140spam3n(op_setitem , PyObject_SetItem)
Guido van Rossum037b9401996-07-30 16:55:54 +0000141
142static PyObject*
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000143op_getslice(s,a)
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000144 PyObject *s, *a;
Guido van Rossum037b9401996-07-30 16:55:54 +0000145{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000146 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000147 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000148
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000149 if (!PyArg_ParseTuple(a,"Oii",&a1,&a2,&a3))
150 return NULL;
151 return PySequence_GetSlice(a1,a2,a3);
Guido van Rossum037b9401996-07-30 16:55:54 +0000152}
153
154static PyObject*
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000155op_setslice(s,a)
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000156 PyObject *s, *a;
Guido van Rossum037b9401996-07-30 16:55:54 +0000157{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000158 PyObject *a1, *a4;
Guido van Rossum24a49941997-08-28 18:11:05 +0000159 int a2,a3;
Guido van Rossum037b9401996-07-30 16:55:54 +0000160
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000161 if (!PyArg_ParseTuple(a,"OiiO",&a1,&a2,&a3,&a4))
162 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000163
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000164 if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
165 return NULL;
Guido van Rossum037b9401996-07-30 16:55:54 +0000166
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000167 Py_INCREF(Py_None);
168 return Py_None;
Guido van Rossum037b9401996-07-30 16:55:54 +0000169}
170
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000171static PyObject*
172op_delslice(s,a)
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000173 PyObject *s, *a;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000174{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000175 PyObject *a1;
Guido van Rossum24a49941997-08-28 18:11:05 +0000176 int a2,a3;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000177
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000178 if(! PyArg_ParseTuple(a,"Oii",&a1,&a2,&a3))
179 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000180
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000181 if (-1 == PySequence_DelSlice(a1,a2,a3))
182 return NULL;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000183
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000184 Py_INCREF(Py_None);
185 return Py_None;
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000186}
187
Guido van Rossum037b9401996-07-30 16:55:54 +0000188#undef spam1
189#undef spam2
Guido van Rossum17202301996-08-19 22:01:39 +0000190#ifdef HAVE_OLD_CPP
191#define spam1(OP,DOC) {"OP", OP, 1, DOC},
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000192#define spam2(OP,ALTOP,DOC) {"OP", op_/**/OP, 1, DOC}, \
193 {"ALTOP", op_/**/OP, 1, DOC},
Guido van Rossum17202301996-08-19 22:01:39 +0000194#else
195#define spam1(OP,DOC) {#OP, OP, 1, DOC},
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000196#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, 1, DOC}, \
197 {#ALTOP, op_##OP, 1, DOC},
Guido van Rossum17202301996-08-19 22:01:39 +0000198#endif
Guido van Rossum037b9401996-07-30 16:55:54 +0000199
200static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000201
Guido van Rossum17202301996-08-19 22:01:39 +0000202spam1(isCallable,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000203 "isCallable(a) -- Same as callable(a).")
Guido van Rossum17202301996-08-19 22:01:39 +0000204spam1(isNumberType,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000205 "isNumberType(a) -- Return 1 if a has a numeric type, and zero otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000206spam1(isSequenceType,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000207 "isSequenceType(a) -- Return 1 if a has a sequence type, and zero otherwise.")
Guido van Rossum17202301996-08-19 22:01:39 +0000208spam1(truth,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000209 "truth(a) -- Return 1 if a is true, and 0 otherwise.")
210spam2(contains,sequenceIncludes,
211 "contains(a, b) -- Same as b in a (note reversed operands).")
Guido van Rossum17202301996-08-19 22:01:39 +0000212spam1(indexOf,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000213 "indexOf(a, b) -- Return the first index of b in a.")
Guido van Rossum17202301996-08-19 22:01:39 +0000214spam1(countOf,
215 "countOf(a, b) -- Return the number of times b occurs in a.")
216spam1(isMappingType,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000217 "isMappingType(a) -- Return 1 if a has a mapping type, and zero otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000218
Guido van Rossum832f6d21998-05-22 18:12:59 +0000219spam2(add,__add__, "add(a, b) -- Same as a + b.")
220spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
221spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
222spam2(div,__div__, "div(a, b) -- Same as a / b.")
223spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
224spam2(neg,__neg__, "neg(a) -- Same as -a.")
225spam2(pos,__pos__, "pos(a) -- Same as +a.")
226spam2(abs,__abs__, "abs(a) -- Same as abs(a).")
227spam2(inv,__inv__, "inv(a) -- Same as ~a.")
228spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
229spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
230spam2(not_,__not__, "not_(a) -- Same as not a.")
231spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
232spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
233spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
Guido van Rossum17202301996-08-19 22:01:39 +0000234spam2(concat,__concat__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000235 "concat(a, b) -- Same as a + b, for a and b sequences.")
Guido van Rossum17202301996-08-19 22:01:39 +0000236spam2(repeat,__repeat__,
Guido van Rossum36a484f1996-12-05 19:01:16 +0000237 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
Guido van Rossum17202301996-08-19 22:01:39 +0000238spam2(getitem,__getitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000239 "getitem(a, b) -- Same as a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000240spam2(setitem,__setitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000241 "setitem(a, b, c) -- Same as a[b] = c.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000242spam2(delitem,__delitem__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000243 "delitem(a, b) -- Same as del a[b].")
Guido van Rossum17202301996-08-19 22:01:39 +0000244spam2(getslice,__getslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000245 "getslice(a, b, c) -- Same as a[b:c].")
Guido van Rossum17202301996-08-19 22:01:39 +0000246spam2(setslice,__setslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000247"setslice(a, b, c, d) -- Same as a[b:c] = d.")
Guido van Rossumc9fb47e1996-08-21 17:40:51 +0000248spam2(delslice,__delslice__,
Guido van Rossum832f6d21998-05-22 18:12:59 +0000249"delslice(a, b, c) -- Same as del a[b:c].")
Guido van Rossum037b9401996-07-30 16:55:54 +0000250
Guido van Rossum037b9401996-07-30 16:55:54 +0000251 {NULL, NULL} /* sentinel */
252
Guido van Rossum037b9401996-07-30 16:55:54 +0000253};
254
255
256/* Initialization function for the module (*must* be called initoperator) */
257
258void
259initoperator()
260{
Barry Warsaw19f61ae1996-12-18 19:50:00 +0000261 /* Create the module and add the functions */
Guido van Rossum832f6d21998-05-22 18:12:59 +0000262 Py_InitModule4("operator", operator_methods, operator_doc,
263 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum037b9401996-07-30 16:55:54 +0000264}