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