Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Abstract Object Interface (many thanks to Jim Fulton) */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 37 | /* Shorthands to return certain errors */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 38 | |
| 39 | static PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 40 | type_error(msg) |
| 41 | char *msg; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 42 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 43 | PyErr_SetString(PyExc_TypeError, msg); |
| 44 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Guido van Rossum | 052b7e1 | 1996-11-11 15:08:19 +0000 | [diff] [blame] | 47 | static PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 48 | null_error() |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 49 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 50 | if (!PyErr_Occurred()) |
| 51 | PyErr_SetString(PyExc_SystemError, |
| 52 | "null argument to internal routine"); |
| 53 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 56 | /* Copied with modifications from stropmodule.c: atoi, atof, atol */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 58 | static PyObject * |
| 59 | int_from_string(v) |
| 60 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 62 | extern long PyOS_strtol Py_PROTO((const char *, char **, int)); |
| 63 | char *s, *end; |
| 64 | long x; |
| 65 | char buffer[256]; /* For errors */ |
| 66 | |
| 67 | s = PyString_AS_STRING(v); |
| 68 | while (*s && isspace(Py_CHARMASK(*s))) |
| 69 | s++; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 70 | errno = 0; |
| 71 | x = PyOS_strtol(s, &end, 10); |
Guido van Rossum | 3b2b347 | 1998-06-22 03:54:15 +0000 | [diff] [blame^] | 72 | if (end == s || !isdigit(end[-1])) { |
| 73 | PyErr_SetString(PyExc_ValueError, "no digits in int constant"); |
| 74 | return NULL; |
| 75 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 76 | while (*end && isspace(Py_CHARMASK(*end))) |
| 77 | end++; |
| 78 | if (*end != '\0') { |
| 79 | sprintf(buffer, "invalid literal for int(): %.200s", s); |
| 80 | PyErr_SetString(PyExc_ValueError, buffer); |
| 81 | return NULL; |
| 82 | } |
Guido van Rossum | 3b2b347 | 1998-06-22 03:54:15 +0000 | [diff] [blame^] | 83 | else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 84 | PyErr_SetString(PyExc_ValueError, |
| 85 | "null byte in argument for int()"); |
| 86 | return NULL; |
| 87 | } |
| 88 | else if (errno != 0) { |
| 89 | sprintf(buffer, "int() literal too large: %.200s", s); |
| 90 | PyErr_SetString(PyExc_ValueError, buffer); |
| 91 | return NULL; |
| 92 | } |
| 93 | return PyInt_FromLong(x); |
| 94 | } |
| 95 | |
| 96 | static PyObject * |
| 97 | long_from_string(v) |
| 98 | PyObject *v; |
| 99 | { |
| 100 | char *s, *end; |
| 101 | PyObject *x; |
| 102 | char buffer[256]; /* For errors */ |
| 103 | |
| 104 | s = PyString_AS_STRING(v); |
| 105 | while (*s && isspace(Py_CHARMASK(*s))) |
| 106 | s++; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 107 | x = PyLong_FromString(s, &end, 10); |
| 108 | if (x == NULL) |
| 109 | return NULL; |
| 110 | while (*end && isspace(Py_CHARMASK(*end))) |
| 111 | end++; |
| 112 | if (*end != '\0') { |
| 113 | sprintf(buffer, "invalid literal for long(): %.200s", s); |
| 114 | PyErr_SetString(PyExc_ValueError, buffer); |
| 115 | Py_DECREF(x); |
| 116 | return NULL; |
| 117 | } |
Guido van Rossum | 3b2b347 | 1998-06-22 03:54:15 +0000 | [diff] [blame^] | 118 | else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 119 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 3b2b347 | 1998-06-22 03:54:15 +0000 | [diff] [blame^] | 120 | "null byte in argument for long()"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | return x; |
| 124 | } |
| 125 | |
| 126 | static PyObject * |
| 127 | float_from_string(v) |
| 128 | PyObject *v; |
| 129 | { |
| 130 | extern double strtod Py_PROTO((const char *, char **)); |
| 131 | char *s, *end; |
| 132 | double x; |
| 133 | char buffer[256]; /* For errors */ |
| 134 | |
| 135 | s = PyString_AS_STRING(v); |
| 136 | while (*s && isspace(Py_CHARMASK(*s))) |
| 137 | s++; |
| 138 | if (s[0] == '\0') { |
| 139 | PyErr_SetString(PyExc_ValueError, "empty string for float()"); |
| 140 | return NULL; |
| 141 | } |
| 142 | errno = 0; |
| 143 | PyFPE_START_PROTECT("float_from_string", return 0) |
| 144 | x = strtod(s, &end); |
| 145 | PyFPE_END_PROTECT(x) |
| 146 | while (*end && isspace(Py_CHARMASK(*end))) |
| 147 | end++; |
| 148 | if (*end != '\0') { |
| 149 | sprintf(buffer, "invalid literal for float(): %.200s", s); |
| 150 | PyErr_SetString(PyExc_ValueError, buffer); |
| 151 | return NULL; |
| 152 | } |
Guido van Rossum | 3b2b347 | 1998-06-22 03:54:15 +0000 | [diff] [blame^] | 153 | else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 154 | PyErr_SetString(PyExc_ValueError, |
| 155 | "null byte in argument for float()"); |
| 156 | return NULL; |
| 157 | } |
| 158 | else if (errno != 0) { |
| 159 | sprintf(buffer, "float() literal too large: %.200s", s); |
| 160 | PyErr_SetString(PyExc_ValueError, buffer); |
| 161 | return NULL; |
| 162 | } |
| 163 | return PyFloat_FromDouble(x); |
| 164 | } |
| 165 | |
| 166 | /* Operations on any object */ |
| 167 | |
| 168 | int |
| 169 | PyObject_Cmp(o1, o2, result) |
| 170 | PyObject *o1; |
| 171 | PyObject *o2; |
| 172 | int *result; |
| 173 | { |
| 174 | int r; |
| 175 | |
| 176 | if (o1 == NULL || o2 == NULL) { |
| 177 | null_error(); |
| 178 | return -1; |
| 179 | } |
| 180 | r = PyObject_Compare(o1, o2); |
| 181 | if (PyErr_Occurred()) |
| 182 | return -1; |
| 183 | *result = r; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 186 | |
| 187 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 188 | PyObject_Type(o) |
| 189 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 190 | { |
| 191 | PyObject *v; |
| 192 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 193 | if (o == NULL) |
| 194 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 195 | v = (PyObject *)o->ob_type; |
| 196 | Py_INCREF(v); |
| 197 | return v; |
| 198 | } |
| 199 | |
| 200 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 201 | PyObject_Length(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 202 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 204 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 206 | if (o == NULL) { |
| 207 | null_error(); |
| 208 | return -1; |
| 209 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 211 | m = o->ob_type->tp_as_sequence; |
| 212 | if (m && m->sq_length) |
| 213 | return m->sq_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 215 | return PyMapping_Length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 219 | PyObject_GetItem(o, key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 220 | PyObject *o; |
| 221 | PyObject *key; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 222 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 223 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 225 | if (o == NULL || key == NULL) |
| 226 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 228 | m = o->ob_type->tp_as_mapping; |
| 229 | if (m && m->mp_subscript) |
| 230 | return m->mp_subscript(o, key); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 232 | if (PyInt_Check(key)) |
| 233 | return PySequence_GetItem(o, PyInt_AsLong(key)); |
| 234 | |
| 235 | return type_error("unsubscriptable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 239 | PyObject_SetItem(o, key, value) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 240 | PyObject *o; |
| 241 | PyObject *key; |
| 242 | PyObject *value; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 243 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 244 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 246 | if (o == NULL || key == NULL || value == NULL) { |
| 247 | null_error(); |
| 248 | return -1; |
| 249 | } |
| 250 | m = o->ob_type->tp_as_mapping; |
| 251 | if (m && m->mp_ass_subscript) |
| 252 | return m->mp_ass_subscript(o, key, value); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 254 | if (PyInt_Check(key)) |
| 255 | return PySequence_SetItem(o, PyInt_AsLong(key), value); |
| 256 | |
| 257 | type_error("object does not support item assignment"); |
| 258 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 261 | int |
| 262 | PyObject_DelItem(o, key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 263 | PyObject *o; |
| 264 | PyObject *key; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 265 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 266 | PyMappingMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 268 | if (o == NULL || key == NULL) { |
| 269 | null_error(); |
| 270 | return -1; |
| 271 | } |
| 272 | m = o->ob_type->tp_as_mapping; |
| 273 | if (m && m->mp_ass_subscript) |
| 274 | return m->mp_ass_subscript(o, key, (PyObject*)NULL); |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 276 | if (PyInt_Check(key)) |
| 277 | return PySequence_DelItem(o, PyInt_AsLong(key)); |
| 278 | |
| 279 | type_error("object does not support item deletion"); |
| 280 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 283 | /* Operations on numbers */ |
| 284 | |
| 285 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 286 | PyNumber_Check(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 287 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 288 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 289 | return o && o->ob_type->tp_as_number; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 292 | /* Binary operators */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 294 | #define BINOP(v, w, opname, ropname, thisfunc) \ |
| 295 | if (PyInstance_Check(v) || PyInstance_Check(w)) \ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 296 | return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) |
| 297 | |
| 298 | PyObject * |
| 299 | PyNumber_Or(v, w) |
| 300 | PyObject *v, *w; |
| 301 | { |
| 302 | extern int PyNumber_Coerce(); |
| 303 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 304 | BINOP(v, w, "__or__", "__ror__", PyNumber_Or); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 305 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 306 | PyObject *x = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 307 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 308 | if (PyNumber_Coerce(&v, &w) != 0) |
| 309 | return NULL; |
| 310 | if ((f = v->ob_type->tp_as_number->nb_or) != NULL) |
| 311 | x = (*f)(v, w); |
| 312 | Py_DECREF(v); |
| 313 | Py_DECREF(w); |
| 314 | if (f != NULL) |
| 315 | return x; |
| 316 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 317 | return type_error("bad operand type(s) for |"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | PyObject * |
| 321 | PyNumber_Xor(v, w) |
| 322 | PyObject *v, *w; |
| 323 | { |
| 324 | extern int PyNumber_Coerce(); |
| 325 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 326 | BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 327 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 328 | PyObject *x = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 329 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 330 | if (PyNumber_Coerce(&v, &w) != 0) |
| 331 | return NULL; |
| 332 | if ((f = v->ob_type->tp_as_number->nb_xor) != NULL) |
| 333 | x = (*f)(v, w); |
| 334 | Py_DECREF(v); |
| 335 | Py_DECREF(w); |
| 336 | if (f != NULL) |
| 337 | return x; |
| 338 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 339 | return type_error("bad operand type(s) for ^"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | PyObject * |
| 343 | PyNumber_And(v, w) |
| 344 | PyObject *v, *w; |
| 345 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 346 | BINOP(v, w, "__and__", "__rand__", PyNumber_And); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 347 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 348 | PyObject *x = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 349 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 350 | if (PyNumber_Coerce(&v, &w) != 0) |
| 351 | return NULL; |
| 352 | if ((f = v->ob_type->tp_as_number->nb_and) != NULL) |
| 353 | x = (*f)(v, w); |
| 354 | Py_DECREF(v); |
| 355 | Py_DECREF(w); |
| 356 | if (f != NULL) |
| 357 | return x; |
| 358 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 359 | return type_error("bad operand type(s) for &"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | PyObject * |
| 363 | PyNumber_Lshift(v, w) |
| 364 | PyObject *v, *w; |
| 365 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 366 | BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 367 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 368 | PyObject *x = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 369 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 370 | if (PyNumber_Coerce(&v, &w) != 0) |
| 371 | return NULL; |
| 372 | if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL) |
| 373 | x = (*f)(v, w); |
| 374 | Py_DECREF(v); |
| 375 | Py_DECREF(w); |
| 376 | if (f != NULL) |
| 377 | return x; |
| 378 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 379 | return type_error("bad operand type(s) for <<"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | PyObject * |
| 383 | PyNumber_Rshift(v, w) |
| 384 | PyObject *v, *w; |
| 385 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 386 | BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 387 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 388 | PyObject *x = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 389 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 390 | if (PyNumber_Coerce(&v, &w) != 0) |
| 391 | return NULL; |
| 392 | if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL) |
| 393 | x = (*f)(v, w); |
| 394 | Py_DECREF(v); |
| 395 | Py_DECREF(w); |
| 396 | if (f != NULL) |
| 397 | return x; |
| 398 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 399 | return type_error("bad operand type(s) for >>"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | PyObject * |
| 403 | PyNumber_Add(v, w) |
| 404 | PyObject *v, *w; |
| 405 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 406 | PySequenceMethods *m; |
| 407 | |
| 408 | BINOP(v, w, "__add__", "__radd__", PyNumber_Add); |
| 409 | m = v->ob_type->tp_as_sequence; |
| 410 | if (m && m->sq_concat) |
| 411 | return (*m->sq_concat)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 412 | else if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 413 | PyObject *x = NULL; |
| 414 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 415 | if (PyNumber_Coerce(&v, &w) != 0) |
| 416 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 417 | if ((f = v->ob_type->tp_as_number->nb_add) != NULL) |
| 418 | x = (*f)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 419 | Py_DECREF(v); |
| 420 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 421 | if (f != NULL) |
| 422 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 423 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 424 | return type_error("bad operand type(s) for +"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | PyObject * |
| 428 | PyNumber_Subtract(v, w) |
| 429 | PyObject *v, *w; |
| 430 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 431 | BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 432 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 433 | PyObject *x = NULL; |
| 434 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 435 | if (PyNumber_Coerce(&v, &w) != 0) |
| 436 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 437 | if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL) |
| 438 | x = (*f)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 439 | Py_DECREF(v); |
| 440 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 441 | if (f != NULL) |
| 442 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 443 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 444 | return type_error("bad operand type(s) for -"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | PyObject * |
| 448 | PyNumber_Multiply(v, w) |
| 449 | PyObject *v, *w; |
| 450 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 451 | PyTypeObject *tp = v->ob_type; |
| 452 | PySequenceMethods *m; |
| 453 | |
| 454 | BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 455 | if (tp->tp_as_number != NULL && |
| 456 | w->ob_type->tp_as_sequence != NULL && |
| 457 | !PyInstance_Check(v)) { |
| 458 | /* number*sequence -- swap v and w */ |
| 459 | PyObject *tmp = v; |
| 460 | v = w; |
| 461 | w = tmp; |
| 462 | tp = v->ob_type; |
| 463 | } |
| 464 | if (tp->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 465 | PyObject *x = NULL; |
| 466 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 467 | if (PyInstance_Check(v)) { |
| 468 | /* Instances of user-defined classes get their |
| 469 | other argument uncoerced, so they may |
| 470 | implement sequence*number as well as |
| 471 | number*number. */ |
| 472 | Py_INCREF(v); |
| 473 | Py_INCREF(w); |
| 474 | } |
| 475 | else if (PyNumber_Coerce(&v, &w) != 0) |
| 476 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 477 | if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL) |
| 478 | x = (*f)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 479 | Py_DECREF(v); |
| 480 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 481 | if (f != NULL) |
| 482 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 484 | m = tp->tp_as_sequence; |
| 485 | if (m && m->sq_repeat) { |
| 486 | if (!PyInt_Check(w)) |
| 487 | return type_error( |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 488 | "can't multiply sequence with non-int"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 489 | return (*m->sq_repeat)(v, (int)PyInt_AsLong(w)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 490 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 491 | return type_error("bad operand type(s) for *"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | PyObject * |
| 495 | PyNumber_Divide(v, w) |
| 496 | PyObject *v, *w; |
| 497 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 498 | BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 499 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 500 | PyObject *x = NULL; |
| 501 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 502 | if (PyNumber_Coerce(&v, &w) != 0) |
| 503 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 504 | if ((f = v->ob_type->tp_as_number->nb_divide) != NULL) |
| 505 | x = (*f)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 506 | Py_DECREF(v); |
| 507 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 508 | if (f != NULL) |
| 509 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 510 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 511 | return type_error("bad operand type(s) for /"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | PyObject * |
| 515 | PyNumber_Remainder(v, w) |
| 516 | PyObject *v, *w; |
| 517 | { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 518 | if (PyString_Check(v)) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 519 | return PyString_Format(v, w); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 520 | BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 521 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 522 | PyObject *x = NULL; |
| 523 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 524 | if (PyNumber_Coerce(&v, &w) != 0) |
| 525 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 526 | if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL) |
| 527 | x = (*f)(v, w); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 528 | Py_DECREF(v); |
| 529 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 530 | if (f != NULL) |
| 531 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 532 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 533 | return type_error("bad operand type(s) for %"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | PyObject * |
| 537 | PyNumber_Divmod(v, w) |
| 538 | PyObject *v, *w; |
| 539 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 540 | BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod); |
| 541 | if (v->ob_type->tp_as_number != NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 542 | PyObject *x = NULL; |
| 543 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 544 | if (PyNumber_Coerce(&v, &w) != 0) |
| 545 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 546 | if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL) |
| 547 | x = (*f)(v, w); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 548 | Py_DECREF(v); |
| 549 | Py_DECREF(w); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 550 | if (f != NULL) |
| 551 | return x; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 552 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 553 | return type_error("bad operand type(s) for divmod()"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 556 | /* Power (binary or ternary) */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 557 | |
| 558 | static PyObject * |
| 559 | do_pow(v, w) |
| 560 | PyObject *v, *w; |
| 561 | { |
| 562 | PyObject *res; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 563 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *)); |
| 564 | BINOP(v, w, "__pow__", "__rpow__", do_pow); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 565 | if (v->ob_type->tp_as_number == NULL || |
| 566 | w->ob_type->tp_as_number == NULL) { |
| 567 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 568 | "pow(x, y) requires numeric arguments"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 569 | return NULL; |
| 570 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 571 | if ( |
| 572 | #ifndef WITHOUT_COMPLEX |
| 573 | !PyComplex_Check(v) && |
| 574 | #endif |
| 575 | PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) { |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 576 | if (!PyErr_Occurred()) |
| 577 | PyErr_SetString(PyExc_ValueError, |
| 578 | "negative number to float power"); |
| 579 | return NULL; |
| 580 | } |
| 581 | if (PyNumber_Coerce(&v, &w) != 0) |
| 582 | return NULL; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 583 | if ((f = v->ob_type->tp_as_number->nb_power) != NULL) |
| 584 | res = (*f)(v, w, Py_None); |
| 585 | else |
| 586 | res = type_error("pow(x, y) not defined for these operands"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 587 | Py_DECREF(v); |
| 588 | Py_DECREF(w); |
| 589 | return res; |
| 590 | } |
| 591 | |
| 592 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 593 | PyNumber_Power(v, w, z) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 594 | PyObject *v, *w, *z; |
| 595 | { |
| 596 | PyObject *res; |
| 597 | PyObject *v1, *z1, *w2, *z2; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 598 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *)); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 599 | |
| 600 | if (z == Py_None) |
| 601 | return do_pow(v, w); |
| 602 | /* XXX The ternary version doesn't do class instance coercions */ |
| 603 | if (PyInstance_Check(v)) |
| 604 | return v->ob_type->tp_as_number->nb_power(v, w, z); |
| 605 | if (v->ob_type->tp_as_number == NULL || |
| 606 | z->ob_type->tp_as_number == NULL || |
| 607 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 608 | return type_error("pow(x, y, z) requires numeric arguments"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 609 | } |
| 610 | if (PyNumber_Coerce(&v, &w) != 0) |
| 611 | return NULL; |
| 612 | res = NULL; |
| 613 | v1 = v; |
| 614 | z1 = z; |
| 615 | if (PyNumber_Coerce(&v1, &z1) != 0) |
| 616 | goto error2; |
| 617 | w2 = w; |
| 618 | z2 = z1; |
| 619 | if (PyNumber_Coerce(&w2, &z2) != 0) |
| 620 | goto error1; |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 621 | if ((f = v1->ob_type->tp_as_number->nb_power) != NULL) |
| 622 | res = (*f)(v1, w2, z2); |
| 623 | else |
| 624 | res = type_error( |
| 625 | "pow(x, y, z) not defined for these operands"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 626 | Py_DECREF(w2); |
| 627 | Py_DECREF(z2); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 628 | error1: |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 629 | Py_DECREF(v1); |
| 630 | Py_DECREF(z1); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 631 | error2: |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 632 | Py_DECREF(v); |
| 633 | Py_DECREF(w); |
| 634 | return res; |
| 635 | } |
| 636 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 637 | /* Unary operators and functions */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 638 | |
| 639 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 640 | PyNumber_Negative(o) |
| 641 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 642 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 643 | PyNumberMethods *m; |
| 644 | |
| 645 | if (o == NULL) |
| 646 | return null_error(); |
| 647 | m = o->ob_type->tp_as_number; |
| 648 | if (m && m->nb_negative) |
| 649 | return (*m->nb_negative)(o); |
| 650 | |
| 651 | return type_error("bad operand type for unary -"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 655 | PyNumber_Positive(o) |
| 656 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 657 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 658 | PyNumberMethods *m; |
| 659 | |
| 660 | if (o == NULL) |
| 661 | return null_error(); |
| 662 | m = o->ob_type->tp_as_number; |
| 663 | if (m && m->nb_positive) |
| 664 | return (*m->nb_positive)(o); |
| 665 | |
| 666 | return type_error("bad operand type for unary +"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 670 | PyNumber_Invert(o) |
| 671 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 672 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 673 | PyNumberMethods *m; |
| 674 | |
| 675 | if (o == NULL) |
| 676 | return null_error(); |
| 677 | m = o->ob_type->tp_as_number; |
| 678 | if (m && m->nb_invert) |
| 679 | return (*m->nb_invert)(o); |
| 680 | |
| 681 | return type_error("bad operand type for unary ~"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 685 | PyNumber_Absolute(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 686 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 687 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 688 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 689 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 690 | if (o == NULL) |
| 691 | return null_error(); |
| 692 | m = o->ob_type->tp_as_number; |
| 693 | if (m && m->nb_absolute) |
| 694 | return m->nb_absolute(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 695 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 696 | return type_error("bad operand type for abs()"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 700 | PyNumber_Int(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 701 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 702 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 703 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 704 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 705 | if (o == NULL) |
| 706 | return null_error(); |
| 707 | if (PyString_Check(o)) |
| 708 | return int_from_string(o); |
| 709 | m = o->ob_type->tp_as_number; |
| 710 | if (m && m->nb_int) |
| 711 | return m->nb_int(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 713 | return type_error("object can't be converted to int"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 717 | PyNumber_Long(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 718 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 719 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 720 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 721 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 722 | if (o == NULL) |
| 723 | return null_error(); |
| 724 | if (PyString_Check(o)) |
| 725 | return long_from_string(o); |
| 726 | m = o->ob_type->tp_as_number; |
| 727 | if (m && m->nb_long) |
| 728 | return m->nb_long(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 730 | return type_error("object can't be converted to long"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 734 | PyNumber_Float(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 735 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 736 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 737 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 739 | if (o == NULL) |
| 740 | return null_error(); |
| 741 | if (PyString_Check(o)) |
| 742 | return float_from_string(o); |
| 743 | m = o->ob_type->tp_as_number; |
| 744 | if (m && m->nb_float) |
| 745 | return m->nb_float(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 746 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 747 | return type_error("object can't be converted to float"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 750 | /* Operations on sequences */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 751 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 752 | int |
| 753 | PySequence_Check(s) |
| 754 | PyObject *s; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 755 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 756 | return s != NULL && s->ob_type->tp_as_sequence; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 759 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 760 | PySequence_Length(s) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 761 | PyObject *s; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 762 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 763 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 765 | if (s == NULL) { |
| 766 | null_error(); |
| 767 | return -1; |
| 768 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 770 | m = s->ob_type->tp_as_sequence; |
| 771 | if (m && m->sq_length) |
| 772 | return m->sq_length(s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 773 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 774 | type_error("len() of unsized object"); |
| 775 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 779 | PySequence_Concat(s, o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 780 | PyObject *s; |
| 781 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 782 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 783 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 785 | if (s == NULL || o == NULL) |
| 786 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 788 | m = s->ob_type->tp_as_sequence; |
| 789 | if (m && m->sq_concat) |
| 790 | return m->sq_concat(s, o); |
| 791 | |
| 792 | return type_error("object can't be concatenated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 796 | PySequence_Repeat(o, count) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 797 | PyObject *o; |
| 798 | int count; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 799 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 800 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 802 | if (o == NULL) |
| 803 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 804 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 805 | m = o->ob_type->tp_as_sequence; |
| 806 | if (m && m->sq_repeat) |
| 807 | return m->sq_repeat(o, count); |
| 808 | |
| 809 | return type_error("object can't be repeated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 813 | PySequence_GetItem(s, i) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 814 | PyObject *s; |
| 815 | int i; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 816 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 817 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 819 | if (s == NULL) |
| 820 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 821 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 822 | m = s->ob_type->tp_as_sequence; |
| 823 | if (m && m->sq_item) { |
| 824 | if (i < 0) { |
| 825 | if (m->sq_length) { |
| 826 | int l = (*m->sq_length)(s); |
| 827 | if (l < 0) |
| 828 | return NULL; |
| 829 | i += l; |
| 830 | } |
| 831 | } |
| 832 | return m->sq_item(s, i); |
| 833 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 835 | return type_error("unindexable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 839 | PySequence_GetSlice(s, i1, i2) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 840 | PyObject *s; |
| 841 | int i1; |
| 842 | int i2; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 843 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 844 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 845 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 846 | if (!s) return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 848 | m = s->ob_type->tp_as_sequence; |
| 849 | if (m && m->sq_slice) { |
| 850 | if (i1 < 0 || i2 < 0) { |
| 851 | if (m->sq_length) { |
| 852 | int l = (*m->sq_length)(s); |
| 853 | if (l < 0) |
| 854 | return NULL; |
| 855 | if (i1 < 0) |
| 856 | i1 += l; |
| 857 | if (i2 < 0) |
| 858 | i2 += l; |
| 859 | } |
| 860 | } |
| 861 | return m->sq_slice(s, i1, i2); |
| 862 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 863 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 864 | return type_error("unsliceable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 868 | PySequence_SetItem(s, i, o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 869 | PyObject *s; |
| 870 | int i; |
| 871 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 872 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 873 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 875 | if (s == NULL) { |
| 876 | null_error(); |
| 877 | return -1; |
| 878 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 879 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 880 | m = s->ob_type->tp_as_sequence; |
| 881 | if (m && m->sq_ass_item) { |
| 882 | if (i < 0) { |
| 883 | if (m->sq_length) { |
| 884 | int l = (*m->sq_length)(s); |
| 885 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 886 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 887 | i += l; |
| 888 | } |
| 889 | } |
| 890 | return m->sq_ass_item(s, i, o); |
| 891 | } |
| 892 | |
| 893 | type_error("object doesn't support item assignment"); |
| 894 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 897 | int |
| 898 | PySequence_DelItem(s, i) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 899 | PyObject *s; |
| 900 | int i; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 901 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 902 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 903 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 904 | if (s == NULL) { |
| 905 | null_error(); |
| 906 | return -1; |
| 907 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 908 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 909 | m = s->ob_type->tp_as_sequence; |
| 910 | if (m && m->sq_ass_item) { |
| 911 | if (i < 0) { |
| 912 | if (m->sq_length) { |
| 913 | int l = (*m->sq_length)(s); |
| 914 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 915 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 916 | i += l; |
| 917 | } |
| 918 | } |
| 919 | return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 920 | } |
| 921 | |
| 922 | type_error("object doesn't support item deletion"); |
| 923 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 926 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 927 | PySequence_SetSlice(s, i1, i2, o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 928 | PyObject *s; |
| 929 | int i1; |
| 930 | int i2; |
| 931 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 932 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 933 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 934 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 935 | if (s == NULL) { |
| 936 | null_error(); |
| 937 | return -1; |
| 938 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 940 | m = s->ob_type->tp_as_sequence; |
| 941 | if (m && m->sq_ass_slice) { |
| 942 | if (i1 < 0 || i2 < 0) { |
| 943 | if (m->sq_length) { |
| 944 | int l = (*m->sq_length)(s); |
| 945 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 946 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 947 | if (i1 < 0) |
| 948 | i1 += l; |
| 949 | if (i2 < 0) |
| 950 | i2 += l; |
| 951 | } |
| 952 | } |
| 953 | return m->sq_ass_slice(s, i1, i2, o); |
| 954 | } |
| 955 | type_error("object doesn't support slice assignment"); |
| 956 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 959 | int |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 960 | PySequence_DelSlice(s, i1, i2) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 961 | PyObject *s; |
| 962 | int i1; |
| 963 | int i2; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 964 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 965 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 966 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 967 | if (s == NULL) { |
| 968 | null_error(); |
| 969 | return -1; |
| 970 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 972 | m = s->ob_type->tp_as_sequence; |
| 973 | if (m && m->sq_ass_slice) { |
| 974 | if (i1 < 0 || i2 < 0) { |
| 975 | if (m->sq_length) { |
| 976 | int l = (*m->sq_length)(s); |
| 977 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 978 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 979 | if (i1 < 0) |
| 980 | i1 += l; |
| 981 | if (i2 < 0) |
| 982 | i2 += l; |
| 983 | } |
| 984 | } |
| 985 | return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL); |
| 986 | } |
| 987 | type_error("object doesn't support slice deletion"); |
| 988 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 991 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 992 | PySequence_Tuple(v) |
| 993 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 994 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 995 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 996 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 997 | if (v == NULL) |
| 998 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 999 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1000 | if (PyTuple_Check(v)) { |
| 1001 | Py_INCREF(v); |
| 1002 | return v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1003 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (PyList_Check(v)) |
| 1006 | return PyList_AsTuple(v); |
| 1007 | |
| 1008 | if (PyString_Check(v)) { |
| 1009 | int n = PyString_Size(v); |
| 1010 | PyObject *t = PyTuple_New(n); |
| 1011 | if (t != NULL) { |
| 1012 | int i; |
| 1013 | char *p = PyString_AsString(v); |
| 1014 | for (i = 0; i < n; i++) { |
| 1015 | PyObject *item = |
| 1016 | PyString_FromStringAndSize(p+i, 1); |
| 1017 | if (item == NULL) { |
| 1018 | Py_DECREF(t); |
| 1019 | t = NULL; |
| 1020 | break; |
| 1021 | } |
| 1022 | PyTuple_SetItem(t, i, item); |
| 1023 | } |
| 1024 | } |
| 1025 | return t; |
| 1026 | } |
| 1027 | |
| 1028 | /* Generic sequence object */ |
| 1029 | m = v->ob_type->tp_as_sequence; |
| 1030 | if (m && m->sq_item) { |
| 1031 | /* XXX Should support indefinite-length sequences */ |
| 1032 | int i; |
| 1033 | PyObject *t; |
| 1034 | int n = PySequence_Length(v); |
| 1035 | if (n < 0) |
| 1036 | return NULL; |
| 1037 | t = PyTuple_New(n); |
| 1038 | if (t == NULL) |
| 1039 | return NULL; |
| 1040 | for (i = 0; i < n; i++) { |
| 1041 | PyObject *item = (*m->sq_item)(v, i); |
| 1042 | if (item == NULL) { |
| 1043 | Py_DECREF(t); |
| 1044 | t = NULL; |
| 1045 | break; |
| 1046 | } |
| 1047 | PyTuple_SetItem(t, i, item); |
| 1048 | } |
| 1049 | return t; |
| 1050 | } |
| 1051 | |
| 1052 | /* None of the above */ |
| 1053 | return type_error("tuple() argument must be a sequence"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1056 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1057 | PySequence_List(v) |
| 1058 | PyObject *v; |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1059 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1060 | PySequenceMethods *m; |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1061 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1062 | m = v->ob_type->tp_as_sequence; |
| 1063 | if (m && m->sq_item) { |
| 1064 | /* XXX Should support indefinite-length sequences */ |
| 1065 | int i; |
| 1066 | PyObject *l; |
| 1067 | int n = PySequence_Length(v); |
| 1068 | if (n < 0) |
| 1069 | return NULL; |
| 1070 | l = PyList_New(n); |
| 1071 | if (l == NULL) |
| 1072 | return NULL; |
| 1073 | for (i = 0; i < n; i++) { |
| 1074 | PyObject *item = (*m->sq_item)(v, i); |
| 1075 | if (item == NULL) { |
| 1076 | Py_DECREF(l); |
| 1077 | l = NULL; |
| 1078 | break; |
| 1079 | } |
| 1080 | PyList_SetItem(l, i, item); |
| 1081 | } |
| 1082 | return l; |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1083 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1084 | return type_error("list() argument must be a sequence"); |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1087 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1088 | PySequence_Count(s, o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1089 | PyObject *s; |
| 1090 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1091 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1092 | int l, i, n, cmp, err; |
| 1093 | PyObject *item; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1094 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1095 | if (s == NULL || o == NULL) { |
| 1096 | null_error(); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | l = PySequence_Length(s); |
| 1101 | if (l < 0) |
| 1102 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1103 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1104 | n = 0; |
| 1105 | for (i = 0; i < l; i++) { |
| 1106 | item = PySequence_GetItem(s, i); |
| 1107 | if (item == NULL) |
| 1108 | return -1; |
| 1109 | err = PyObject_Cmp(item, o, &cmp); |
| 1110 | Py_DECREF(item); |
| 1111 | if (err < 0) |
| 1112 | return err; |
| 1113 | if (cmp == 0) |
| 1114 | n++; |
| 1115 | } |
| 1116 | return n; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1119 | int |
| 1120 | PySequence_Contains(w, v) /* v in w */ |
| 1121 | PyObject *w; |
| 1122 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1123 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1124 | int i, cmp; |
| 1125 | PyObject *x; |
| 1126 | PySequenceMethods *sq; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1128 | /* Special case for char in string */ |
| 1129 | if (PyString_Check(w)) { |
| 1130 | register char *s, *end; |
| 1131 | register char c; |
| 1132 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
| 1133 | PyErr_SetString(PyExc_TypeError, |
| 1134 | "string member test needs char left operand"); |
| 1135 | return -1; |
| 1136 | } |
| 1137 | c = PyString_AsString(v)[0]; |
| 1138 | s = PyString_AsString(w); |
| 1139 | end = s + PyString_Size(w); |
| 1140 | while (s < end) { |
| 1141 | if (c == *s++) |
| 1142 | return 1; |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1146 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1147 | sq = w->ob_type->tp_as_sequence; |
| 1148 | if (sq == NULL || sq->sq_item == NULL) { |
| 1149 | PyErr_SetString(PyExc_TypeError, |
| 1150 | "'in' or 'not in' needs sequence right argument"); |
| 1151 | return -1; |
| 1152 | } |
| 1153 | |
| 1154 | for (i = 0; ; i++) { |
| 1155 | x = (*sq->sq_item)(w, i); |
| 1156 | if (x == NULL) { |
Guido van Rossum | 08570de | 1998-05-28 19:24:35 +0000 | [diff] [blame] | 1157 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1158 | PyErr_Clear(); |
| 1159 | break; |
| 1160 | } |
| 1161 | return -1; |
| 1162 | } |
| 1163 | cmp = PyObject_Compare(v, x); |
| 1164 | Py_XDECREF(x); |
| 1165 | if (cmp == 0) |
| 1166 | return 1; |
| 1167 | if (PyErr_Occurred()) |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
| 1171 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1174 | /* Backwards compatibility */ |
| 1175 | #undef PySequence_In |
| 1176 | int |
| 1177 | PySequence_In(w, v) |
| 1178 | PyObject *w; |
| 1179 | PyObject *v; |
| 1180 | { |
| 1181 | return PySequence_Contains(w, v); |
| 1182 | } |
| 1183 | |
| 1184 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1185 | PySequence_Index(s, o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1186 | PyObject *s; |
| 1187 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1188 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1189 | int l, i, cmp, err; |
| 1190 | PyObject *item; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1192 | if (s == NULL || o == NULL) { |
| 1193 | null_error(); |
| 1194 | return -1; |
| 1195 | } |
| 1196 | |
| 1197 | l = PySequence_Length(s); |
| 1198 | if (l < 0) |
| 1199 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1200 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1201 | for (i = 0; i < l; i++) { |
| 1202 | item = PySequence_GetItem(s, i); |
| 1203 | if (item == NULL) |
| 1204 | return -1; |
| 1205 | err = PyObject_Cmp(item, o, &cmp); |
| 1206 | Py_DECREF(item); |
| 1207 | if (err < 0) |
| 1208 | return err; |
| 1209 | if (cmp == 0) |
| 1210 | return i; |
| 1211 | } |
| 1212 | |
| 1213 | PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list"); |
| 1214 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1217 | /* Operations on mappings */ |
| 1218 | |
| 1219 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1220 | PyMapping_Check(o) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1221 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1222 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1223 | return o && o->ob_type->tp_as_mapping; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1226 | int |
| 1227 | PyMapping_Length(o) |
| 1228 | PyObject *o; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1229 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1230 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1231 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1232 | if (o == NULL) { |
| 1233 | null_error(); |
| 1234 | return -1; |
| 1235 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1236 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1237 | m = o->ob_type->tp_as_mapping; |
| 1238 | if (m && m->mp_length) |
| 1239 | return m->mp_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1240 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1241 | type_error("len() of unsized object"); |
| 1242 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1245 | PyObject * |
| 1246 | PyMapping_GetItemString(o, key) |
| 1247 | PyObject *o; |
| 1248 | char *key; |
| 1249 | { |
| 1250 | PyObject *okey, *r; |
| 1251 | |
| 1252 | if (key == NULL) |
| 1253 | return null_error(); |
| 1254 | |
| 1255 | okey = PyString_FromString(key); |
| 1256 | if (okey == NULL) |
| 1257 | return NULL; |
| 1258 | r = PyObject_GetItem(o, okey); |
| 1259 | Py_DECREF(okey); |
| 1260 | return r; |
| 1261 | } |
| 1262 | |
| 1263 | int |
| 1264 | PyMapping_SetItemString(o, key, value) |
| 1265 | PyObject *o; |
| 1266 | char *key; |
| 1267 | PyObject *value; |
| 1268 | { |
| 1269 | PyObject *okey; |
| 1270 | int r; |
| 1271 | |
| 1272 | if (key == NULL) { |
| 1273 | null_error(); |
| 1274 | return -1; |
| 1275 | } |
| 1276 | |
| 1277 | okey = PyString_FromString(key); |
| 1278 | if (okey == NULL) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1279 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1280 | r = PyObject_SetItem(o, okey, value); |
| 1281 | Py_DECREF(okey); |
| 1282 | return r; |
| 1283 | } |
| 1284 | |
| 1285 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1286 | PyMapping_HasKeyString(o, key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1287 | PyObject *o; |
| 1288 | char *key; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1289 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1290 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1291 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1292 | v = PyMapping_GetItemString(o, key); |
| 1293 | if (v) { |
| 1294 | Py_DECREF(v); |
| 1295 | return 1; |
| 1296 | } |
| 1297 | PyErr_Clear(); |
| 1298 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1301 | int |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1302 | PyMapping_HasKey(o, key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1303 | PyObject *o; |
| 1304 | PyObject *key; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1305 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1306 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1308 | v = PyObject_GetItem(o, key); |
| 1309 | if (v) { |
| 1310 | Py_DECREF(v); |
| 1311 | return 1; |
| 1312 | } |
| 1313 | PyErr_Clear(); |
| 1314 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1317 | /* Operations on callable objects */ |
| 1318 | |
| 1319 | /* XXX PyCallable_Check() is in object.c */ |
| 1320 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1321 | PyObject * |
Guido van Rossum | 8ba873b | 1995-09-18 21:17:59 +0000 | [diff] [blame] | 1322 | PyObject_CallObject(o, a) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1323 | PyObject *o, *a; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1324 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1325 | PyObject *r; |
| 1326 | PyObject *args = a; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1327 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1328 | if (args == NULL) { |
| 1329 | args = PyTuple_New(0); |
| 1330 | if (args == NULL) |
| 1331 | return NULL; |
| 1332 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1333 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1334 | r = PyEval_CallObject(o, args); |
| 1335 | |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 1336 | if (args != a) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1337 | Py_DECREF(args); |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 1338 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1339 | |
| 1340 | return r; |
| 1341 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1342 | |
| 1343 | PyObject * |
| 1344 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1345 | /* VARARGS 2 */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1346 | PyObject_CallFunction(PyObject *callable, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1347 | #else |
| 1348 | /* VARARGS */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1349 | PyObject_CallFunction(va_alist) va_dcl |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1350 | #endif |
| 1351 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1352 | va_list va; |
| 1353 | PyObject *args, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1354 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1355 | va_start(va, format); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1356 | #else |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1357 | PyObject *callable; |
| 1358 | char *format; |
| 1359 | va_start(va); |
| 1360 | callable = va_arg(va, PyObject *); |
| 1361 | format = va_arg(va, char *); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1362 | #endif |
| 1363 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1364 | if (callable == NULL) { |
| 1365 | va_end(va); |
| 1366 | return null_error(); |
| 1367 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1368 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1369 | if (format) |
| 1370 | args = Py_VaBuildValue(format, va); |
| 1371 | else |
| 1372 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1373 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1374 | va_end(va); |
| 1375 | |
| 1376 | if (args == NULL) |
| 1377 | return NULL; |
| 1378 | |
| 1379 | if (!PyTuple_Check(args)) { |
| 1380 | PyObject *a; |
| 1381 | |
| 1382 | a = PyTuple_New(1); |
| 1383 | if (a == NULL) |
| 1384 | return NULL; |
| 1385 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1386 | return NULL; |
| 1387 | args = a; |
| 1388 | } |
| 1389 | retval = PyObject_CallObject(callable, args); |
| 1390 | |
| 1391 | Py_DECREF(args); |
| 1392 | |
| 1393 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | PyObject * |
| 1397 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1398 | /* VARARGS 2 */ |
| 1399 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
| 1400 | #else |
| 1401 | /* VARARGS */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1402 | PyObject_CallMethod(va_alist) va_dcl |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1403 | #endif |
| 1404 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1405 | va_list va; |
| 1406 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1407 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1408 | va_start(va, format); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1409 | #else |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1410 | PyObject *o; |
| 1411 | char *name; |
| 1412 | char *format; |
| 1413 | va_start(va); |
| 1414 | o = va_arg(va, PyObject *); |
| 1415 | name = va_arg(va, char *); |
| 1416 | format = va_arg(va, char *); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1417 | #endif |
| 1418 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1419 | if (o == NULL || name == NULL) { |
| 1420 | va_end(va); |
| 1421 | return null_error(); |
| 1422 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1423 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1424 | func = PyObject_GetAttrString(o, name); |
| 1425 | if (func == NULL) { |
| 1426 | va_end(va); |
| 1427 | PyErr_SetString(PyExc_AttributeError, name); |
| 1428 | return 0; |
| 1429 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1430 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1431 | if (!PyCallable_Check(func)) { |
| 1432 | va_end(va); |
| 1433 | return type_error("call of non-callable attribute"); |
| 1434 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1435 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1436 | if (format && *format) |
| 1437 | args = Py_VaBuildValue(format, va); |
| 1438 | else |
| 1439 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1440 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1441 | va_end(va); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1442 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1443 | if (!args) |
| 1444 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1445 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1446 | if (!PyTuple_Check(args)) { |
| 1447 | PyObject *a; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1448 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1449 | a = PyTuple_New(1); |
| 1450 | if (a == NULL) |
| 1451 | return NULL; |
| 1452 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1453 | return NULL; |
| 1454 | args = a; |
| 1455 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1456 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1457 | retval = PyObject_CallObject(func, args); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1458 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1459 | Py_DECREF(args); |
| 1460 | Py_DECREF(func); |
| 1461 | |
| 1462 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1463 | } |