Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 1 | static char operator_doc[] = "\ |
| 2 | Operator interface.\n\ |
| 3 | \n\ |
| 4 | This module exports a set of functions implemented in C corresponding\n\ |
| 5 | to the intrinsic operators of Python. For example, operator.add(x, y)\n\ |
| 6 | is equivalent to the expression x+y. The function names are those\n\ |
| 7 | used 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 | |
| 113 | spami(isCallable , PyCallable_Check) |
| 114 | spami(isNumberType , PyNumber_Check) |
| 115 | spami(truth , PyObject_IsTrue) |
| 116 | spam2(__add , PyNumber_Add) |
| 117 | spam2(__sub , PyNumber_Subtract) |
| 118 | spam2(__mul , PyNumber_Multiply) |
| 119 | spam2(__div , PyNumber_Divide) |
| 120 | spam2(__mod , PyNumber_Remainder) |
| 121 | spam1(__neg , PyNumber_Negative) |
| 122 | spam1(__pos , PyNumber_Positive) |
| 123 | spam1(__abs , PyNumber_Absolute) |
| 124 | spam1(__inv , PyNumber_Invert) |
| 125 | spam2(__lshift , PyNumber_Lshift) |
| 126 | spam2(__rshift , PyNumber_Rshift) |
| 127 | spam2(__and , PyNumber_And) |
| 128 | spam2(__xor , PyNumber_Xor) |
| 129 | spam2(__or , PyNumber_Or) |
| 130 | spami(isSequenceType , PySequence_Check) |
| 131 | spam2(__concat , PySequence_Concat) |
| 132 | spamoi(__repeat , PySequence_Repeat) |
| 133 | spami2(sequenceIncludes, PySequence_In) |
| 134 | spami2(indexOf , PySequence_Index) |
| 135 | spami2(countOf , PySequence_Count) |
| 136 | spami(isMappingType , PyMapping_Check) |
| 137 | spam2(__getitem , PyObject_GetItem) |
| 138 | spam3n(__setitem , PyObject_SetItem) |
| 139 | |
| 140 | static PyObject* |
| 141 | __getslice(s,a) |
| 142 | PyObject *s, *a; |
| 143 | { |
| 144 | PyObject *a1; |
| 145 | long a2,a3; |
| 146 | |
| 147 | if(! PyArg_ParseTuple(a,"Oii",&a1,&a2,&a3)) return NULL; |
| 148 | |
| 149 | return PySequence_GetSlice(a1,a2,a3); |
| 150 | } |
| 151 | |
| 152 | static PyObject* |
| 153 | __setslice(s,a) |
| 154 | PyObject *s, *a; |
| 155 | { |
| 156 | PyObject *a1, *a4; |
| 157 | long a2,a3; |
| 158 | |
| 159 | if(! PyArg_ParseTuple(a,"OiiO",&a1,&a2,&a3,&a4)) return NULL; |
| 160 | |
| 161 | if(-1 == PySequence_SetSlice(a1,a2,a3,a4)) return NULL; |
| 162 | |
| 163 | Py_INCREF(Py_None); |
| 164 | return Py_None; |
| 165 | } |
| 166 | |
| 167 | #undef spam1 |
| 168 | #undef spam2 |
| 169 | #define spam1(OP,DOC) {#OP, OP, 1, #OP "(o) -- " DOC}, |
| 170 | #define spam2(OP,DOC) {#OP, OP, 1, #OP "(a,b) -- " DOC}, |
| 171 | #define spam3(OP,DOC) {#OP, OP, 1, #OP "(a,b,c) -- " DOC}, |
| 172 | |
| 173 | static struct PyMethodDef operator_methods[] = { |
| 174 | spam1(isCallable , "Return 1 if o is callable, and zero otherwise.") |
| 175 | spam1(isNumberType , "Return 1 if o has a numeric type, and zero otherwise.") |
| 176 | spam1(isSequenceType , "Return 1 if o has a sequence type, and zero otherwise.") |
| 177 | spam1(truth , "Return 1 if o is true, and 0 otherwise.") |
| 178 | spam2(sequenceIncludes, "Return 1 is a includes b, and 0 otherwise.") |
| 179 | spam2(indexOf , "Return the index of b in a.") |
| 180 | spam2(countOf , "Return the number of times b occurs in a.") |
| 181 | spam1(isMappingType , "Return 1 if o has a mapping type, and zero otherwise.") |
| 182 | |
| 183 | #undef spam1 |
| 184 | #undef spam2 |
| 185 | #undef spam3 |
| 186 | #define spam1(OP,DOC) {#OP, __ ## OP, 1, #OP "(o) -- " DOC}, \ |
| 187 | {"__" #OP "__", __ ## OP, 1, #OP "(o) -- " DOC}, |
| 188 | #define spam2(OP,DOC) {#OP, __ ## OP, 1, #OP "(a,b) -- " DOC}, \ |
| 189 | {"__" #OP "__", __ ## OP, 1, #OP "(a,b) -- " DOC}, |
| 190 | #define spam3(OP,DOC) {#OP, __ ## OP, 1, #OP "(a,b,c) -- " DOC}, \ |
| 191 | {"__" #OP "__", __ ## OP, 1, #OP "(a,b,c) -- " DOC}, |
| 192 | #define spam4(OP,DOC) {#OP, __ ## OP, 1, #OP "(a,b,c,v) -- " DOC}, \ |
| 193 | {"__" #OP "__", __ ## OP, 1, #OP "(a,b,c,v) -- " DOC}, |
| 194 | |
| 195 | |
| 196 | spam2(add , "Return a + b, for a and b numbers.") |
| 197 | spam2(sub , "Return a - b.") |
| 198 | spam2(mul , "Return a * b, for a and b numbers.") |
| 199 | spam2(div , "Return a / b.") |
| 200 | spam2(mod , "Return a % b.") |
| 201 | spam1(neg , "Return o negated.") |
| 202 | spam1(pos , "Return o positive.") |
| 203 | spam1(abs , "Return the absolute value of o.") |
| 204 | spam1(inv , "Return the inverse of o.") |
| 205 | spam2(lshift , "Return a shifted left by b.") |
| 206 | spam2(rshift , "Return a shifted right by b.") |
| 207 | spam2(and , "Return the bitwise and of a and b.") |
| 208 | spam2(xor , "Return the bitwise exclusive-or of a and b.") |
| 209 | spam2(or , "Return the bitwise or of a and b.") |
| 210 | spam2(concat , "Return a + b, for a and b sequences.") |
| 211 | spam2(repeat , "Return a + b, where a is a sequence, and b is an " |
| 212 | "integer.") |
| 213 | spam2(getitem , "Return the value of a at index b.") |
| 214 | spam3(setitem , "Set the value of a at b to c.") |
| 215 | spam2(getslice , "Return the slice of a from b to c-1.") |
| 216 | spam4(setslice , "Set the slice of a from b to c-1 to the sequence, v.") |
| 217 | {NULL, NULL} /* sentinel */ |
| 218 | |
| 219 | |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | /* Initialization function for the module (*must* be called initoperator) */ |
| 224 | |
| 225 | void |
| 226 | initoperator() |
| 227 | { |
| 228 | PyObject *m, *d; |
| 229 | |
| 230 | /* Create the module and add the functions */ |
| 231 | m = Py_InitModule4("operator", operator_methods, |
| 232 | operator_doc, |
| 233 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 234 | |
| 235 | /* Add some symbolic constants to the module */ |
| 236 | d = PyModule_GetDict(m); |
| 237 | PyDict_SetItemString(d, "__version__",PyString_FromString("$Rev$")); |
| 238 | |
| 239 | /* Check for errors */ |
| 240 | if (PyErr_Occurred()) |
| 241 | Py_FatalError("can't initialize module operator"); |
| 242 | } |