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