blob: 318d0ddcb032c6631905dd5dfe8e562748974a30 [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 Rossumec185bd1996-08-08 19:12:05 +0000113#ifdef _AIX
114#define __div __aix_div
115#define __abs __aix_abs
116#endif
117
Guido van Rossum037b9401996-07-30 16:55:54 +0000118spami(isCallable , PyCallable_Check)
119spami(isNumberType , PyNumber_Check)
120spami(truth , PyObject_IsTrue)
121spam2(__add , PyNumber_Add)
122spam2(__sub , PyNumber_Subtract)
123spam2(__mul , PyNumber_Multiply)
124spam2(__div , PyNumber_Divide)
125spam2(__mod , PyNumber_Remainder)
126spam1(__neg , PyNumber_Negative)
127spam1(__pos , PyNumber_Positive)
128spam1(__abs , PyNumber_Absolute)
129spam1(__inv , PyNumber_Invert)
130spam2(__lshift , PyNumber_Lshift)
131spam2(__rshift , PyNumber_Rshift)
132spam2(__and , PyNumber_And)
133spam2(__xor , PyNumber_Xor)
134spam2(__or , PyNumber_Or)
135spami(isSequenceType , PySequence_Check)
136spam2(__concat , PySequence_Concat)
137spamoi(__repeat , PySequence_Repeat)
138spami2(sequenceIncludes, PySequence_In)
139spami2(indexOf , PySequence_Index)
140spami2(countOf , PySequence_Count)
141spami(isMappingType , PyMapping_Check)
142spam2(__getitem , PyObject_GetItem)
143spam3n(__setitem , PyObject_SetItem)
144
145static PyObject*
146__getslice(s,a)
147 PyObject *s, *a;
148{
149 PyObject *a1;
150 long a2,a3;
151
152 if(! PyArg_ParseTuple(a,"Oii",&a1,&a2,&a3)) return NULL;
153
154 return PySequence_GetSlice(a1,a2,a3);
155}
156
157static PyObject*
158__setslice(s,a)
159 PyObject *s, *a;
160{
161 PyObject *a1, *a4;
162 long a2,a3;
163
164 if(! PyArg_ParseTuple(a,"OiiO",&a1,&a2,&a3,&a4)) return NULL;
165
166 if(-1 == PySequence_SetSlice(a1,a2,a3,a4)) return NULL;
167
168 Py_INCREF(Py_None);
169 return Py_None;
170}
171
172#undef spam1
173#undef spam2
Guido van Rossum17202301996-08-19 22:01:39 +0000174#ifdef HAVE_OLD_CPP
175#define spam1(OP,DOC) {"OP", OP, 1, DOC},
176#define spam2(OP,ALTOP,DOC) {"OP", __/**/OP, 1, DOC}, \
177 {"ALTOP", __/**/OP, 1, DOC},
178#else
179#define spam1(OP,DOC) {#OP, OP, 1, DOC},
180#define spam2(OP,ALTOP,DOC) {#OP, __ ## OP, 1, DOC}, \
181 {#ALTOP, __ ## OP, 1, DOC},
182#endif
Guido van Rossum037b9401996-07-30 16:55:54 +0000183
184static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000185
Guido van Rossum17202301996-08-19 22:01:39 +0000186spam1(isCallable,
187 "isCallable(o) -- Return 1 if o is callable, and zero otherwise.")
188spam1(isNumberType,
189 "isNumberType(o) -- Return 1 if o has a numeric type, and zero otherwise.")
190spam1(isSequenceType,
191 "isSequenceType(o) -- Return 1 if o has a sequence type, and zero otherwise.")
192spam1(truth,
193 "truth(o) -- Return 1 if o is true, and 0 otherwise.")
194spam1(sequenceIncludes,
195 "sequenceIncludes(a, b) -- Return 1 is a includes b, and 0 otherwise.")
196spam1(indexOf,
197 "indexOf(a, b) -- Return the index of b in a.")
198spam1(countOf,
199 "countOf(a, b) -- Return the number of times b occurs in a.")
200spam1(isMappingType,
201 "isMappingType(o) -- Return 1 if o has a mapping type, and zero otherwise.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000202
Guido van Rossum17202301996-08-19 22:01:39 +0000203spam2(add,__add__, "add(a, b) -- Return a + b, for a and b numbers.")
204spam2(sub,__sub__, "sub(a, b) -- Return a - b.")
205spam2(mul,__mul__, "mul(a, b) -- Return a * b, for a and b numbers.")
206spam2(div,__div__, "div(a, b) -- Return a / b.")
207spam2(mod,__mod__, "mod(a, b) -- Return a % b.")
208spam2(neg,__neg__, "neg(o) -- Return o negated.")
209spam2(pos,__pos__, "pos(o) -- Return o positive.")
210spam2(abs,__abs__, "abs(o) -- Return the absolute value of o.")
211spam2(inv,__inv__, "inv(o) -- Return the inverse of o.")
212spam2(lshift,__lshift__, "lshift(a, b) -- Return a shifted left by b.")
213spam2(rshift,__rshift__, "rshift(a, b) -- Return a shifted right by b.")
214spam2(and,__and__, "and(a, b) -- Return the bitwise and of a and b.")
215spam2(xor,__xor__, "xor(a, b) -- Return the bitwise exclusive-or of a and b.")
216spam2(or,__or__, "or(a, b) -- Return the bitwise or of a and b.")
217spam2(concat,__concat__,
218 "concat(a, b) -- Return a + b, for a and b sequences.")
219spam2(repeat,__repeat__,
220 "repeat(a, b) -- Return a + b, where a is a sequence, and b is an integer.")
221spam2(getitem,__getitem__,
222 "getitem(a, b) -- Return the value of a at index b.")
223spam2(setitem,__setitem__,
224 "setitem(a, b, c) -- Set the value of a at b to c.")
225spam2(getslice,__getslice__,
226 "getslice(a, b, c) -- Return the slice of a from b to c-1.")
227spam2(setslice,__setslice__,
228"setslice(a, b, c, v) -- Set the slice of a from b to c-1 to the sequence, v.")
Guido van Rossum037b9401996-07-30 16:55:54 +0000229
Guido van Rossum037b9401996-07-30 16:55:54 +0000230 {NULL, NULL} /* sentinel */
231
Guido van Rossum037b9401996-07-30 16:55:54 +0000232};
233
234
235/* Initialization function for the module (*must* be called initoperator) */
236
237void
238initoperator()
239{
240 PyObject *m, *d;
241
242 /* Create the module and add the functions */
243 m = Py_InitModule4("operator", operator_methods,
244 operator_doc,
245 (PyObject*)NULL,PYTHON_API_VERSION);
246
247 /* Add some symbolic constants to the module */
248 d = PyModule_GetDict(m);
249 PyDict_SetItemString(d, "__version__",PyString_FromString("$Rev$"));
250
251 /* Check for errors */
252 if (PyErr_Occurred())
253 Py_FatalError("can't initialize module operator");
254}