Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1 | /* Abstract Object Interface (many thanks to Jim Fulton) */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | fa0b6ab | 1998-05-22 15:23:36 +0000 | [diff] [blame] | 4 | #include <ctype.h> |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 5 | #include "structmember.h" /* we need the offsetof() macro from there */ |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 6 | #include "longintrepr.h" |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 7 | |
| 8 | #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \ |
Guido van Rossum | c318969 | 2001-01-17 15:29:42 +0000 | [diff] [blame] | 9 | Py_TPFLAGS_CHECKTYPES) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 11 | /* Shorthands to return certain errors */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 12 | |
| 13 | static PyObject * |
Fred Drake | 4201b9e | 2000-07-09 04:34:13 +0000 | [diff] [blame] | 14 | type_error(const char *msg) |
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 | PyErr_SetString(PyExc_TypeError, msg); |
| 17 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Guido van Rossum | 052b7e1 | 1996-11-11 15:08:19 +0000 | [diff] [blame] | 20 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 21 | null_error(void) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 22 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 23 | if (!PyErr_Occurred()) |
| 24 | PyErr_SetString(PyExc_SystemError, |
| 25 | "null argument to internal routine"); |
| 26 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 29 | /* Operations on any object */ |
| 30 | |
| 31 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 32 | PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 33 | { |
| 34 | int r; |
| 35 | |
| 36 | if (o1 == NULL || o2 == NULL) { |
| 37 | null_error(); |
| 38 | return -1; |
| 39 | } |
| 40 | r = PyObject_Compare(o1, o2); |
| 41 | if (PyErr_Occurred()) |
| 42 | return -1; |
| 43 | *result = r; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 46 | |
| 47 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 48 | PyObject_Type(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 49 | { |
| 50 | PyObject *v; |
| 51 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 52 | if (o == NULL) |
| 53 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 54 | v = (PyObject *)o->ob_type; |
| 55 | Py_INCREF(v); |
| 56 | return v; |
| 57 | } |
| 58 | |
| 59 | int |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 60 | PyObject_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 62 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 64 | if (o == NULL) { |
| 65 | null_error(); |
| 66 | return -1; |
| 67 | } |
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 | m = o->ob_type->tp_as_sequence; |
| 70 | if (m && m->sq_length) |
| 71 | return m->sq_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 72 | |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 73 | return PyMapping_Size(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 76 | #undef PyObject_Length |
| 77 | int |
| 78 | PyObject_Length(PyObject *o) |
| 79 | { |
| 80 | return PyObject_Size(o); |
| 81 | } |
| 82 | #define PyObject_Length PyObject_Size |
| 83 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 84 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 85 | PyObject_GetItem(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 86 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 87 | PyMappingMethods *m; |
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 | if (o == NULL || key == NULL) |
| 90 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 92 | m = o->ob_type->tp_as_mapping; |
| 93 | if (m && m->mp_subscript) |
| 94 | return m->mp_subscript(o, key); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 96 | if (o->ob_type->tp_as_sequence) { |
| 97 | if (PyInt_Check(key)) |
| 98 | return PySequence_GetItem(o, PyInt_AsLong(key)); |
Andrew M. Kuchling | 0f223d2 | 2000-02-23 22:21:50 +0000 | [diff] [blame] | 99 | else if (PyLong_Check(key)) { |
| 100 | long key_value = PyLong_AsLong(key); |
| 101 | if (key_value == -1 && PyErr_Occurred()) |
| 102 | return NULL; |
| 103 | return PySequence_GetItem(o, key_value); |
| 104 | } |
Guido van Rossum | 64585f6 | 2001-11-24 18:24:47 +0000 | [diff] [blame] | 105 | else if (o->ob_type->tp_as_sequence->sq_item) |
| 106 | return type_error("sequence index must be integer"); |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 107 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 108 | |
| 109 | return type_error("unsubscriptable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 113 | PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) |
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 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 117 | if (o == NULL || key == NULL || value == NULL) { |
| 118 | null_error(); |
| 119 | return -1; |
| 120 | } |
| 121 | m = o->ob_type->tp_as_mapping; |
| 122 | if (m && m->mp_ass_subscript) |
| 123 | return m->mp_ass_subscript(o, key, value); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 125 | if (o->ob_type->tp_as_sequence) { |
| 126 | if (PyInt_Check(key)) |
| 127 | return PySequence_SetItem(o, PyInt_AsLong(key), value); |
Andrew M. Kuchling | 0f223d2 | 2000-02-23 22:21:50 +0000 | [diff] [blame] | 128 | else if (PyLong_Check(key)) { |
| 129 | long key_value = PyLong_AsLong(key); |
| 130 | if (key_value == -1 && PyErr_Occurred()) |
| 131 | return -1; |
| 132 | return PySequence_SetItem(o, key_value, value); |
| 133 | } |
Guido van Rossum | 64585f6 | 2001-11-24 18:24:47 +0000 | [diff] [blame] | 134 | else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 135 | type_error("sequence index must be integer"); |
| 136 | return -1; |
| 137 | } |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 138 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 139 | |
| 140 | type_error("object does not support item assignment"); |
| 141 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 144 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 145 | PyObject_DelItem(PyObject *o, PyObject *key) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 147 | PyMappingMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 149 | if (o == NULL || key == NULL) { |
| 150 | null_error(); |
| 151 | return -1; |
| 152 | } |
| 153 | m = o->ob_type->tp_as_mapping; |
| 154 | if (m && m->mp_ass_subscript) |
| 155 | return m->mp_ass_subscript(o, key, (PyObject*)NULL); |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 157 | if (o->ob_type->tp_as_sequence) { |
| 158 | if (PyInt_Check(key)) |
| 159 | return PySequence_DelItem(o, PyInt_AsLong(key)); |
Andrew M. Kuchling | 0f223d2 | 2000-02-23 22:21:50 +0000 | [diff] [blame] | 160 | else if (PyLong_Check(key)) { |
| 161 | long key_value = PyLong_AsLong(key); |
| 162 | if (key_value == -1 && PyErr_Occurred()) |
| 163 | return -1; |
| 164 | return PySequence_DelItem(o, key_value); |
| 165 | } |
Guido van Rossum | 64585f6 | 2001-11-24 18:24:47 +0000 | [diff] [blame] | 166 | else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 167 | type_error("sequence index must be integer"); |
| 168 | return -1; |
| 169 | } |
Guido van Rossum | 2130824 | 1998-08-13 16:44:44 +0000 | [diff] [blame] | 170 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 171 | |
| 172 | type_error("object does not support item deletion"); |
| 173 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Martin v. Löwis | b0d71d0 | 2002-01-05 10:50:30 +0000 | [diff] [blame] | 176 | int |
| 177 | PyObject_DelItemString(PyObject *o, char *key) |
| 178 | { |
| 179 | PyObject *okey; |
| 180 | int ret; |
| 181 | |
| 182 | if (o == NULL || key == NULL) { |
| 183 | null_error(); |
| 184 | return -1; |
| 185 | } |
| 186 | okey = PyString_FromString(key); |
| 187 | if (okey == NULL) |
| 188 | return -1; |
| 189 | ret = PyObject_DelItem(o, okey); |
| 190 | Py_DECREF(okey); |
| 191 | return ret; |
| 192 | } |
| 193 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 194 | int PyObject_AsCharBuffer(PyObject *obj, |
| 195 | const char **buffer, |
| 196 | int *buffer_len) |
| 197 | { |
| 198 | PyBufferProcs *pb; |
| 199 | const char *pp; |
| 200 | int len; |
| 201 | |
| 202 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 203 | null_error(); |
| 204 | return -1; |
| 205 | } |
| 206 | pb = obj->ob_type->tp_as_buffer; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 207 | if (pb == NULL || |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 208 | pb->bf_getcharbuffer == NULL || |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 209 | pb->bf_getsegcount == NULL) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 210 | PyErr_SetString(PyExc_TypeError, |
| 211 | "expected a character buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 212 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 213 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 214 | if ((*pb->bf_getsegcount)(obj,NULL) != 1) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 215 | PyErr_SetString(PyExc_TypeError, |
| 216 | "expected a single-segment buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 217 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 218 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 219 | len = (*pb->bf_getcharbuffer)(obj, 0, &pp); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 220 | if (len < 0) |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 221 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 222 | *buffer = pp; |
| 223 | *buffer_len = len; |
| 224 | return 0; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 225 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 226 | |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 227 | int |
| 228 | PyObject_CheckReadBuffer(PyObject *obj) |
| 229 | { |
| 230 | PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 231 | |
| 232 | if (pb == NULL || |
| 233 | pb->bf_getreadbuffer == NULL || |
| 234 | pb->bf_getsegcount == NULL || |
| 235 | (*pb->bf_getsegcount)(obj, NULL) != 1) |
| 236 | return 0; |
| 237 | return 1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | int PyObject_AsReadBuffer(PyObject *obj, |
| 241 | const 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; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 253 | if (pb == NULL || |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 254 | pb->bf_getreadbuffer == NULL || |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 255 | pb->bf_getsegcount == NULL) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 256 | PyErr_SetString(PyExc_TypeError, |
| 257 | "expected a readable buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 258 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 259 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 260 | if ((*pb->bf_getsegcount)(obj, NULL) != 1) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 261 | PyErr_SetString(PyExc_TypeError, |
| 262 | "expected a single-segment buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 263 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 264 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 265 | len = (*pb->bf_getreadbuffer)(obj, 0, &pp); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 266 | if (len < 0) |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 267 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 268 | *buffer = pp; |
| 269 | *buffer_len = len; |
| 270 | return 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int PyObject_AsWriteBuffer(PyObject *obj, |
| 274 | void **buffer, |
| 275 | int *buffer_len) |
| 276 | { |
| 277 | PyBufferProcs *pb; |
| 278 | void*pp; |
| 279 | int len; |
| 280 | |
| 281 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 282 | null_error(); |
| 283 | return -1; |
| 284 | } |
| 285 | pb = obj->ob_type->tp_as_buffer; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 286 | if (pb == NULL || |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 287 | pb->bf_getwritebuffer == NULL || |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 288 | pb->bf_getsegcount == NULL) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 289 | PyErr_SetString(PyExc_TypeError, |
| 290 | "expected a writeable buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 291 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 292 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 293 | if ((*pb->bf_getsegcount)(obj, NULL) != 1) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 294 | PyErr_SetString(PyExc_TypeError, |
| 295 | "expected a single-segment buffer object"); |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 296 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 297 | } |
| 298 | len = (*pb->bf_getwritebuffer)(obj,0,&pp); |
| 299 | if (len < 0) |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 300 | return -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 301 | *buffer = pp; |
| 302 | *buffer_len = len; |
| 303 | return 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 306 | /* Operations on numbers */ |
| 307 | |
| 308 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 309 | PyNumber_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 310 | { |
Guido van Rossum | 6921eca | 2003-02-18 16:36:28 +0000 | [diff] [blame] | 311 | return o && o->ob_type->tp_as_number && |
| 312 | (o->ob_type->tp_as_number->nb_int || |
| 313 | o->ob_type->tp_as_number->nb_float); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 316 | /* Binary operators */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 317 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 318 | /* New style number protocol support */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 319 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 320 | #define NB_SLOT(x) offsetof(PyNumberMethods, x) |
| 321 | #define NB_BINOP(nb_methods, slot) \ |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 322 | (*(binaryfunc*)(& ((char*)nb_methods)[slot])) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 323 | #define NB_TERNOP(nb_methods, slot) \ |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 324 | (*(ternaryfunc*)(& ((char*)nb_methods)[slot])) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | Calling scheme used for binary operations: |
| 328 | |
| 329 | v w Action |
| 330 | ------------------------------------------------------------------- |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 331 | new new w.op(v,w)[*], v.op(v,w), w.op(v,w) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 332 | new old v.op(v,w), coerce(v,w), v.op(v,w) |
| 333 | old new w.op(v,w), coerce(v,w), v.op(v,w) |
| 334 | old old coerce(v,w), v.op(v,w) |
| 335 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 336 | [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of |
| 337 | v->ob_type |
| 338 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 339 | Legend: |
| 340 | ------- |
| 341 | * new == new style number |
| 342 | * old == old style number |
| 343 | * Action indicates the order in which operations are tried until either |
| 344 | a valid result is produced or an error occurs. |
| 345 | |
| 346 | */ |
| 347 | |
| 348 | static PyObject * |
| 349 | binary_op1(PyObject *v, PyObject *w, const int op_slot) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 350 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 351 | PyObject *x; |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 352 | binaryfunc slotv = NULL; |
| 353 | binaryfunc slotw = NULL; |
| 354 | |
| 355 | if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v)) |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 356 | slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot); |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 357 | if (w->ob_type != v->ob_type && |
| 358 | w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 359 | slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot); |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 360 | if (slotw == slotv) |
| 361 | slotw = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 362 | } |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 363 | if (slotv) { |
Guido van Rossum | 89c4264 | 2001-10-01 17:10:18 +0000 | [diff] [blame] | 364 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 365 | x = slotw(v, w); |
| 366 | if (x != Py_NotImplemented) |
| 367 | return x; |
| 368 | Py_DECREF(x); /* can't do it */ |
| 369 | slotw = NULL; |
| 370 | } |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 371 | x = slotv(v, w); |
| 372 | if (x != Py_NotImplemented) |
| 373 | return x; |
| 374 | Py_DECREF(x); /* can't do it */ |
| 375 | } |
| 376 | if (slotw) { |
| 377 | x = slotw(v, w); |
| 378 | if (x != Py_NotImplemented) |
| 379 | return x; |
| 380 | Py_DECREF(x); /* can't do it */ |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 381 | } |
| 382 | if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) { |
| 383 | int err = PyNumber_CoerceEx(&v, &w); |
| 384 | if (err < 0) { |
| 385 | return NULL; |
| 386 | } |
| 387 | if (err == 0) { |
| 388 | PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 389 | if (mv) { |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 390 | binaryfunc slot; |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 391 | slot = NB_BINOP(mv, op_slot); |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 392 | if (slot) { |
| 393 | PyObject *x = slot(v, w); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 394 | Py_DECREF(v); |
| 395 | Py_DECREF(w); |
| 396 | return x; |
| 397 | } |
| 398 | } |
| 399 | /* CoerceEx incremented the reference counts */ |
| 400 | Py_DECREF(v); |
| 401 | Py_DECREF(w); |
| 402 | } |
| 403 | } |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 404 | Py_INCREF(Py_NotImplemented); |
| 405 | return Py_NotImplemented; |
| 406 | } |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 407 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 408 | static PyObject * |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 409 | binop_type_error(PyObject *v, PyObject *w, const char *op_name) |
| 410 | { |
| 411 | PyErr_Format(PyExc_TypeError, |
| 412 | "unsupported operand type(s) for %s: '%s' and '%s'", |
| 413 | op_name, |
| 414 | v->ob_type->tp_name, |
| 415 | w->ob_type->tp_name); |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | static PyObject * |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 420 | binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) |
| 421 | { |
| 422 | PyObject *result = binary_op1(v, w, op_slot); |
| 423 | if (result == Py_NotImplemented) { |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 424 | Py_DECREF(result); |
| 425 | return binop_type_error(v, w, op_name); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 426 | } |
| 427 | return result; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | Calling scheme used for ternary operations: |
| 433 | |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 434 | *** In some cases, w.op is called before v.op; see binary_op1. *** |
| 435 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 436 | v w z Action |
| 437 | ------------------------------------------------------------------- |
| 438 | new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z) |
| 439 | new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 440 | old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 441 | old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 442 | new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 443 | new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 444 | old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z) |
| 445 | old old old coerce(v,w,z), v.op(v,w,z) |
| 446 | |
| 447 | Legend: |
| 448 | ------- |
| 449 | * new == new style number |
| 450 | * old == old style number |
| 451 | * Action indicates the order in which operations are tried until either |
| 452 | a valid result is produced or an error occurs. |
| 453 | * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and |
| 454 | only if z != Py_None; if z == Py_None, then it is treated as absent |
| 455 | variable and only coerce(v,w) is tried. |
| 456 | |
| 457 | */ |
| 458 | |
| 459 | static PyObject * |
| 460 | ternary_op(PyObject *v, |
| 461 | PyObject *w, |
| 462 | PyObject *z, |
| 463 | const int op_slot, |
| 464 | const char *op_name) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 465 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 466 | PyNumberMethods *mv, *mw, *mz; |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 467 | PyObject *x = NULL; |
| 468 | ternaryfunc slotv = NULL; |
| 469 | ternaryfunc slotw = NULL; |
| 470 | ternaryfunc slotz = NULL; |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 471 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 472 | mv = v->ob_type->tp_as_number; |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 473 | mw = w->ob_type->tp_as_number; |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 474 | if (mv != NULL && NEW_STYLE_NUMBER(v)) |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 475 | slotv = NB_TERNOP(mv, op_slot); |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 476 | if (w->ob_type != v->ob_type && |
Raymond Hettinger | c2e095f | 2002-12-07 10:05:27 +0000 | [diff] [blame] | 477 | mw != NULL && NEW_STYLE_NUMBER(w)) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 478 | slotw = NB_TERNOP(mw, op_slot); |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 479 | if (slotw == slotv) |
| 480 | slotw = NULL; |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 481 | } |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 482 | if (slotv) { |
Guido van Rossum | 89c4264 | 2001-10-01 17:10:18 +0000 | [diff] [blame] | 483 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 484 | x = slotw(v, w, z); |
| 485 | if (x != Py_NotImplemented) |
| 486 | return x; |
| 487 | Py_DECREF(x); /* can't do it */ |
| 488 | slotw = NULL; |
| 489 | } |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 490 | x = slotv(v, w, z); |
| 491 | if (x != Py_NotImplemented) |
| 492 | return x; |
| 493 | Py_DECREF(x); /* can't do it */ |
| 494 | } |
| 495 | if (slotw) { |
| 496 | x = slotw(v, w, z); |
| 497 | if (x != Py_NotImplemented) |
| 498 | return x; |
| 499 | Py_DECREF(x); /* can't do it */ |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 500 | } |
| 501 | mz = z->ob_type->tp_as_number; |
| 502 | if (mz != NULL && NEW_STYLE_NUMBER(z)) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 503 | slotz = NB_TERNOP(mz, op_slot); |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 504 | if (slotz == slotv || slotz == slotw) |
| 505 | slotz = NULL; |
| 506 | if (slotz) { |
| 507 | x = slotz(v, w, z); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 508 | if (x != Py_NotImplemented) |
| 509 | return x; |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 510 | Py_DECREF(x); /* can't do it */ |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) || |
| 515 | (z != Py_None && !NEW_STYLE_NUMBER(z))) { |
| 516 | /* we have an old style operand, coerce */ |
| 517 | PyObject *v1, *z1, *w2, *z2; |
| 518 | int c; |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 519 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 520 | c = PyNumber_Coerce(&v, &w); |
| 521 | if (c != 0) |
| 522 | goto error3; |
| 523 | |
| 524 | /* Special case: if the third argument is None, it is |
| 525 | treated as absent argument and not coerced. */ |
| 526 | if (z == Py_None) { |
| 527 | if (v->ob_type->tp_as_number) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 528 | slotz = NB_TERNOP(v->ob_type->tp_as_number, |
| 529 | op_slot); |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 530 | if (slotz) |
| 531 | x = slotz(v, w, z); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 532 | else |
| 533 | c = -1; |
| 534 | } |
| 535 | else |
| 536 | c = -1; |
| 537 | goto error2; |
| 538 | } |
| 539 | v1 = v; |
| 540 | z1 = z; |
| 541 | c = PyNumber_Coerce(&v1, &z1); |
| 542 | if (c != 0) |
| 543 | goto error2; |
| 544 | w2 = w; |
| 545 | z2 = z1; |
| 546 | c = PyNumber_Coerce(&w2, &z2); |
| 547 | if (c != 0) |
| 548 | goto error1; |
| 549 | |
| 550 | if (v1->ob_type->tp_as_number != NULL) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 551 | slotv = NB_TERNOP(v1->ob_type->tp_as_number, |
| 552 | op_slot); |
Guido van Rossum | 84675ac | 2001-09-29 01:05:03 +0000 | [diff] [blame] | 553 | if (slotv) |
| 554 | x = slotv(v1, w2, z2); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 555 | else |
| 556 | c = -1; |
| 557 | } |
| 558 | else |
| 559 | c = -1; |
| 560 | |
| 561 | Py_DECREF(w2); |
| 562 | Py_DECREF(z2); |
| 563 | error1: |
| 564 | Py_DECREF(v1); |
| 565 | Py_DECREF(z1); |
| 566 | error2: |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 567 | Py_DECREF(v); |
| 568 | Py_DECREF(w); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 569 | error3: |
| 570 | if (c >= 0) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 571 | return x; |
| 572 | } |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 573 | |
| 574 | if (z == Py_None) |
| 575 | PyErr_Format( |
| 576 | PyExc_TypeError, |
| 577 | "unsupported operand type(s) for ** or pow(): " |
| 578 | "'%s' and '%s'", |
| 579 | v->ob_type->tp_name, |
| 580 | w->ob_type->tp_name); |
| 581 | else |
| 582 | PyErr_Format( |
| 583 | PyExc_TypeError, |
| 584 | "unsupported operand type(s) for pow(): " |
| 585 | "'%s', '%s', '%s'", |
| 586 | v->ob_type->tp_name, |
| 587 | w->ob_type->tp_name, |
| 588 | z->ob_type->tp_name); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 589 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 592 | #define BINARY_FUNC(func, op, op_name) \ |
| 593 | PyObject * \ |
| 594 | func(PyObject *v, PyObject *w) { \ |
| 595 | return binary_op(v, w, NB_SLOT(op), op_name); \ |
| 596 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 597 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 598 | BINARY_FUNC(PyNumber_Or, nb_or, "|") |
| 599 | BINARY_FUNC(PyNumber_Xor, nb_xor, "^") |
| 600 | BINARY_FUNC(PyNumber_And, nb_and, "&") |
| 601 | BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<") |
| 602 | BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>") |
| 603 | BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-") |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 604 | BINARY_FUNC(PyNumber_Divide, nb_divide, "/") |
| 605 | BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()") |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 606 | |
| 607 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 608 | PyNumber_Add(PyObject *v, PyObject *w) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 609 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 610 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_add)); |
| 611 | if (result == Py_NotImplemented) { |
| 612 | PySequenceMethods *m = v->ob_type->tp_as_sequence; |
Jeremy Hylton | 0522d98 | 2002-03-08 21:28:54 +0000 | [diff] [blame] | 613 | if (m && m->sq_concat) { |
| 614 | Py_DECREF(result); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 615 | result = (*m->sq_concat)(v, w); |
Jeremy Hylton | 0522d98 | 2002-03-08 21:28:54 +0000 | [diff] [blame] | 616 | } |
Jeremy Hylton | 6ae6a43 | 2002-03-08 21:11:37 +0000 | [diff] [blame] | 617 | if (result == Py_NotImplemented) { |
Jeremy Hylton | 0522d98 | 2002-03-08 21:28:54 +0000 | [diff] [blame] | 618 | Py_DECREF(result); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 619 | return binop_type_error(v, w, "+"); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 620 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 621 | } |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 622 | return result; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 625 | static PyObject * |
| 626 | sequence_repeat(intargfunc repeatfunc, PyObject *seq, PyObject *n) |
| 627 | { |
| 628 | long count; |
| 629 | if (PyInt_Check(n)) { |
| 630 | count = PyInt_AsLong(n); |
| 631 | } |
| 632 | else if (PyLong_Check(n)) { |
| 633 | count = PyLong_AsLong(n); |
| 634 | if (count == -1 && PyErr_Occurred()) |
| 635 | return NULL; |
| 636 | } |
| 637 | else { |
| 638 | return type_error( |
Raymond Hettinger | 10c6606 | 2004-05-12 21:35:06 +0000 | [diff] [blame] | 639 | "can't multiply sequence by non-int"); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 640 | } |
| 641 | #if LONG_MAX != INT_MAX |
| 642 | if (count > INT_MAX) { |
| 643 | PyErr_SetString(PyExc_ValueError, |
| 644 | "sequence repeat count too large"); |
| 645 | return NULL; |
| 646 | } |
| 647 | else if (count < INT_MIN) |
| 648 | count = INT_MIN; |
| 649 | /* XXX Why don't I either |
| 650 | |
| 651 | - set count to -1 whenever it's negative (after all, |
| 652 | sequence repeat usually treats negative numbers |
| 653 | as zero(); or |
| 654 | |
| 655 | - raise an exception when it's less than INT_MIN? |
| 656 | |
| 657 | I'm thinking about a hypothetical use case where some |
| 658 | sequence type might use a negative value as a flag of |
| 659 | some kind. In those cases I don't want to break the |
| 660 | code by mapping all negative values to -1. But I also |
| 661 | don't want to break e.g. []*(-sys.maxint), which is |
| 662 | perfectly safe, returning []. As a compromise, I do |
| 663 | map out-of-range negative values. |
| 664 | */ |
| 665 | #endif |
| 666 | return (*repeatfunc)(seq, (int)count); |
| 667 | } |
| 668 | |
| 669 | PyObject * |
| 670 | PyNumber_Multiply(PyObject *v, PyObject *w) |
| 671 | { |
| 672 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply)); |
| 673 | if (result == Py_NotImplemented) { |
| 674 | PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 675 | PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
Guido van Rossum | a3a243e | 2002-12-31 19:50:03 +0000 | [diff] [blame] | 676 | Py_DECREF(result); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 677 | if (mv && mv->sq_repeat) { |
| 678 | return sequence_repeat(mv->sq_repeat, v, w); |
| 679 | } |
| 680 | else if (mw && mw->sq_repeat) { |
| 681 | return sequence_repeat(mw->sq_repeat, w, v); |
| 682 | } |
| 683 | result = binop_type_error(v, w, "*"); |
| 684 | } |
| 685 | return result; |
| 686 | } |
| 687 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 688 | PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 689 | PyNumber_FloorDivide(PyObject *v, PyObject *w) |
| 690 | { |
| 691 | /* XXX tp_flags test */ |
| 692 | return binary_op(v, w, NB_SLOT(nb_floor_divide), "//"); |
| 693 | } |
| 694 | |
| 695 | PyObject * |
| 696 | PyNumber_TrueDivide(PyObject *v, PyObject *w) |
| 697 | { |
| 698 | /* XXX tp_flags test */ |
| 699 | return binary_op(v, w, NB_SLOT(nb_true_divide), "/"); |
| 700 | } |
| 701 | |
| 702 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 703 | PyNumber_Remainder(PyObject *v, PyObject *w) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 704 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 705 | return binary_op(v, w, NB_SLOT(nb_remainder), "%"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 709 | PyNumber_Power(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 710 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 711 | return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 714 | /* Binary in-place operators */ |
| 715 | |
| 716 | /* The in-place operators are defined to fall back to the 'normal', |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 717 | non in-place operations, if the in-place methods are not in place. |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 718 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 719 | - If the left hand object has the appropriate struct members, and |
| 720 | they are filled, call the appropriate function and return the |
| 721 | result. No coercion is done on the arguments; the left-hand object |
| 722 | is the one the operation is performed on, and it's up to the |
| 723 | function to deal with the right-hand object. |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 724 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 725 | - Otherwise, in-place modification is not supported. Handle it exactly as |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 726 | a non in-place operation of the same kind. |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 727 | |
| 728 | */ |
| 729 | |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 730 | #define HASINPLACE(t) \ |
| 731 | PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 732 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 733 | static PyObject * |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 734 | binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 735 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 736 | PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 737 | if (mv != NULL && HASINPLACE(v)) { |
Neil Schemenauer | b808c5c | 2002-11-24 01:34:49 +0000 | [diff] [blame] | 738 | binaryfunc slot = NB_BINOP(mv, iop_slot); |
| 739 | if (slot) { |
| 740 | PyObject *x = (slot)(v, w); |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 741 | if (x != Py_NotImplemented) { |
| 742 | return x; |
| 743 | } |
| 744 | Py_DECREF(x); |
| 745 | } |
Thomas Wouters | f2b332d | 2000-09-02 08:34:40 +0000 | [diff] [blame] | 746 | } |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 747 | return binary_op1(v, w, op_slot); |
| 748 | } |
| 749 | |
| 750 | static PyObject * |
| 751 | binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, |
| 752 | const char *op_name) |
| 753 | { |
| 754 | PyObject *result = binary_iop1(v, w, iop_slot, op_slot); |
| 755 | if (result == Py_NotImplemented) { |
| 756 | Py_DECREF(result); |
| 757 | return binop_type_error(v, w, op_name); |
| 758 | } |
| 759 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 762 | #define INPLACE_BINOP(func, iop, op, op_name) \ |
| 763 | PyObject * \ |
| 764 | func(PyObject *v, PyObject *w) { \ |
| 765 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 768 | INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=") |
| 769 | INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=") |
| 770 | INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=") |
| 771 | INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=") |
| 772 | INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=") |
| 773 | INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=") |
| 774 | INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=") |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 775 | |
| 776 | PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 777 | PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w) |
| 778 | { |
| 779 | /* XXX tp_flags test */ |
| 780 | return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide), |
| 781 | NB_SLOT(nb_floor_divide), "//="); |
| 782 | } |
| 783 | |
| 784 | PyObject * |
| 785 | PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w) |
| 786 | { |
| 787 | /* XXX tp_flags test */ |
| 788 | return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide), |
| 789 | NB_SLOT(nb_true_divide), "/="); |
| 790 | } |
| 791 | |
| 792 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 793 | PyNumber_InPlaceAdd(PyObject *v, PyObject *w) |
| 794 | { |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 795 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add), |
| 796 | NB_SLOT(nb_add)); |
| 797 | if (result == Py_NotImplemented) { |
| 798 | PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 799 | Py_DECREF(result); |
| 800 | if (m != NULL) { |
| 801 | binaryfunc f = NULL; |
| 802 | if (HASINPLACE(v)) |
| 803 | f = m->sq_inplace_concat; |
| 804 | if (f == NULL) |
| 805 | f = m->sq_concat; |
| 806 | if (f != NULL) |
| 807 | return (*f)(v, w); |
| 808 | } |
| 809 | result = binop_type_error(v, w, "+="); |
Guido van Rossum | bb8be93 | 2000-09-01 23:27:32 +0000 | [diff] [blame] | 810 | } |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 811 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | PyObject * |
| 815 | PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) |
| 816 | { |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 817 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply), |
| 818 | NB_SLOT(nb_multiply)); |
| 819 | if (result == Py_NotImplemented) { |
| 820 | intargfunc f = NULL; |
| 821 | PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 822 | PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 823 | Py_DECREF(result); |
| 824 | if (mv != NULL) { |
| 825 | if (HASINPLACE(v)) |
| 826 | f = mv->sq_inplace_repeat; |
| 827 | if (f == NULL) |
| 828 | f = mv->sq_repeat; |
| 829 | if (f != NULL) |
| 830 | return sequence_repeat(f, v, w); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 831 | } |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 832 | else if (mw != NULL) { |
| 833 | /* Note that the right hand operand should not be |
| 834 | * mutated in this case so sq_inplace_repeat is not |
| 835 | * used. */ |
| 836 | if (mw->sq_repeat) |
| 837 | return sequence_repeat(mw->sq_repeat, w, v); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 838 | } |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 839 | result = binop_type_error(v, w, "*="); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 840 | } |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 841 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 844 | PyObject * |
| 845 | PyNumber_InPlaceRemainder(PyObject *v, PyObject *w) |
| 846 | { |
Neil Schemenauer | d46fbc3 | 2002-11-24 01:07:42 +0000 | [diff] [blame] | 847 | return binary_iop(v, w, NB_SLOT(nb_inplace_remainder), |
| 848 | NB_SLOT(nb_remainder), "%="); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | PyObject * |
| 852 | PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z) |
| 853 | { |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 854 | if (HASINPLACE(v) && v->ob_type->tp_as_number && |
| 855 | v->ob_type->tp_as_number->nb_inplace_power != NULL) { |
| 856 | return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**="); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 857 | } |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 858 | else { |
| 859 | return ternary_op(v, w, z, NB_SLOT(nb_power), "**="); |
| 860 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 864 | /* Unary operators and functions */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 865 | |
| 866 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 867 | PyNumber_Negative(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 868 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 869 | PyNumberMethods *m; |
| 870 | |
| 871 | if (o == NULL) |
| 872 | return null_error(); |
| 873 | m = o->ob_type->tp_as_number; |
| 874 | if (m && m->nb_negative) |
| 875 | return (*m->nb_negative)(o); |
| 876 | |
| 877 | return type_error("bad operand type for unary -"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 881 | PyNumber_Positive(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 | PyNumberMethods *m; |
| 884 | |
| 885 | if (o == NULL) |
| 886 | return null_error(); |
| 887 | m = o->ob_type->tp_as_number; |
| 888 | if (m && m->nb_positive) |
| 889 | return (*m->nb_positive)(o); |
| 890 | |
| 891 | return type_error("bad operand type for unary +"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 895 | PyNumber_Invert(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 896 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 897 | PyNumberMethods *m; |
| 898 | |
| 899 | if (o == NULL) |
| 900 | return null_error(); |
| 901 | m = o->ob_type->tp_as_number; |
| 902 | if (m && m->nb_invert) |
| 903 | return (*m->nb_invert)(o); |
| 904 | |
| 905 | return type_error("bad operand type for unary ~"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 909 | PyNumber_Absolute(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 910 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 911 | PyNumberMethods *m; |
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 | if (o == NULL) |
| 914 | return null_error(); |
| 915 | m = o->ob_type->tp_as_number; |
| 916 | if (m && m->nb_absolute) |
| 917 | return m->nb_absolute(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 919 | return type_error("bad operand type for abs()"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 922 | /* Add a check for embedded NULL-bytes in the argument. */ |
| 923 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 924 | int_from_string(const char *s, int len) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 925 | { |
| 926 | char *end; |
| 927 | PyObject *x; |
| 928 | |
| 929 | x = PyInt_FromString((char*)s, &end, 10); |
| 930 | if (x == NULL) |
| 931 | return NULL; |
| 932 | if (end != s + len) { |
| 933 | PyErr_SetString(PyExc_ValueError, |
| 934 | "null byte in argument for int()"); |
| 935 | Py_DECREF(x); |
| 936 | return NULL; |
| 937 | } |
| 938 | return x; |
| 939 | } |
| 940 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 941 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 942 | PyNumber_Int(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 943 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 944 | PyNumberMethods *m; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 945 | const char *buffer; |
| 946 | int buffer_len; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 948 | if (o == NULL) |
| 949 | return null_error(); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 950 | if (PyInt_CheckExact(o)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 951 | Py_INCREF(o); |
| 952 | return o; |
| 953 | } |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 954 | if (PyInt_Check(o)) { |
| 955 | PyIntObject *io = (PyIntObject*)o; |
| 956 | return PyInt_FromLong(io->ob_ival); |
| 957 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 958 | if (PyString_Check(o)) |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 959 | return int_from_string(PyString_AS_STRING(o), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 960 | PyString_GET_SIZE(o)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 961 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 962 | if (PyUnicode_Check(o)) |
| 963 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 964 | PyUnicode_GET_SIZE(o), |
| 965 | 10); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 966 | #endif |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 967 | m = o->ob_type->tp_as_number; |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 968 | if (m && m->nb_int) { |
| 969 | PyObject *res = m->nb_int(o); |
| 970 | if (res && (!PyInt_Check(res) && !PyLong_Check(res))) { |
| 971 | PyErr_Format(PyExc_TypeError, |
| 972 | "__int__ returned non-int (type %.200s)", |
| 973 | res->ob_type->tp_name); |
| 974 | Py_DECREF(res); |
| 975 | return NULL; |
| 976 | } |
| 977 | return res; |
| 978 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 979 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 980 | return int_from_string((char*)buffer, buffer_len); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 981 | |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 982 | return type_error("int() argument must be a string or a number"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 985 | /* Add a check for embedded NULL-bytes in the argument. */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 986 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 987 | long_from_string(const char *s, int len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 988 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 989 | char *end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 990 | PyObject *x; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 992 | x = PyLong_FromString((char*)s, &end, 10); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 993 | if (x == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 994 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 995 | if (end != s + len) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 996 | PyErr_SetString(PyExc_ValueError, |
| 997 | "null byte in argument for long()"); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 998 | Py_DECREF(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | } |
| 1001 | return x; |
| 1002 | } |
| 1003 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1004 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1005 | PyNumber_Long(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1006 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1007 | PyNumberMethods *m; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1008 | const char *buffer; |
| 1009 | int buffer_len; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1010 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1011 | if (o == NULL) |
| 1012 | return null_error(); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1013 | if (PyLong_CheckExact(o)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1014 | Py_INCREF(o); |
| 1015 | return o; |
| 1016 | } |
Tim Peters | db30ac4 | 2002-03-02 04:14:21 +0000 | [diff] [blame] | 1017 | if (PyLong_Check(o)) |
| 1018 | return _PyLong_Copy((PyLongObject *)o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1019 | if (PyString_Check(o)) |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 1020 | /* need to do extra error checking that PyLong_FromString() |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1021 | * doesn't do. In particular long('9.5') must raise an |
| 1022 | * exception, not truncate the float. |
| 1023 | */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1024 | return long_from_string(PyString_AS_STRING(o), |
| 1025 | PyString_GET_SIZE(o)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1026 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1027 | if (PyUnicode_Check(o)) |
| 1028 | /* The above check is done in PyLong_FromUnicode(). */ |
| 1029 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 1030 | PyUnicode_GET_SIZE(o), |
| 1031 | 10); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1032 | #endif |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1033 | m = o->ob_type->tp_as_number; |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1034 | if (m && m->nb_long) { |
| 1035 | PyObject *res = m->nb_long(o); |
| 1036 | if (res && (!PyInt_Check(res) && !PyLong_Check(res))) { |
| 1037 | PyErr_Format(PyExc_TypeError, |
| 1038 | "__long__ returned non-long (type %.200s)", |
| 1039 | res->ob_type->tp_name); |
| 1040 | Py_DECREF(res); |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | return res; |
| 1044 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1045 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
| 1046 | return long_from_string(buffer, buffer_len); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1047 | |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 1048 | return type_error("long() argument must be a string or a number"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1052 | PyNumber_Float(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1053 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1054 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1056 | if (o == NULL) |
| 1057 | return null_error(); |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1058 | if (PyFloat_CheckExact(o)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1059 | Py_INCREF(o); |
| 1060 | return o; |
| 1061 | } |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1062 | if (PyFloat_Check(o)) { |
| 1063 | PyFloatObject *po = (PyFloatObject *)o; |
| 1064 | return PyFloat_FromDouble(po->ob_fval); |
| 1065 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1066 | if (!PyString_Check(o)) { |
| 1067 | m = o->ob_type->tp_as_number; |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1068 | if (m && m->nb_float) { |
| 1069 | PyObject *res = m->nb_float(o); |
| 1070 | if (res && !PyFloat_Check(res)) { |
| 1071 | PyErr_Format(PyExc_TypeError, |
| 1072 | "__float__ returned non-float (type %.200s)", |
| 1073 | res->ob_type->tp_name); |
| 1074 | Py_DECREF(res); |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | return res; |
| 1078 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1079 | } |
| 1080 | return PyFloat_FromString(o, NULL); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1083 | /* Operations on sequences */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1084 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1085 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1086 | PySequence_Check(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1087 | { |
Raymond Hettinger | e2eda60 | 2004-04-04 08:51:41 +0000 | [diff] [blame] | 1088 | if (PyInstance_Check(s)) |
| 1089 | return PyObject_HasAttrString(s, "__getitem__"); |
Guido van Rossum | 8700b42 | 2001-09-07 20:20:11 +0000 | [diff] [blame] | 1090 | return s != NULL && s->ob_type->tp_as_sequence && |
| 1091 | s->ob_type->tp_as_sequence->sq_item != NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1094 | int |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1095 | PySequence_Size(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1096 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1097 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1098 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1099 | if (s == NULL) { |
| 1100 | null_error(); |
| 1101 | return -1; |
| 1102 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1103 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1104 | m = s->ob_type->tp_as_sequence; |
| 1105 | if (m && m->sq_length) |
| 1106 | return m->sq_length(s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1107 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1108 | type_error("len() of unsized object"); |
| 1109 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1112 | #undef PySequence_Length |
| 1113 | int |
| 1114 | PySequence_Length(PyObject *s) |
| 1115 | { |
| 1116 | return PySequence_Size(s); |
| 1117 | } |
| 1118 | #define PySequence_Length PySequence_Size |
| 1119 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1120 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1121 | PySequence_Concat(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1122 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1123 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1125 | if (s == NULL || o == NULL) |
| 1126 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1128 | m = s->ob_type->tp_as_sequence; |
| 1129 | if (m && m->sq_concat) |
| 1130 | return m->sq_concat(s, o); |
| 1131 | |
| 1132 | return type_error("object can't be concatenated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1136 | PySequence_Repeat(PyObject *o, int count) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1137 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1138 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1139 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1140 | if (o == NULL) |
| 1141 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1142 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1143 | m = o->ob_type->tp_as_sequence; |
| 1144 | if (m && m->sq_repeat) |
| 1145 | return m->sq_repeat(o, count); |
| 1146 | |
| 1147 | return type_error("object can't be repeated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1151 | PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
| 1152 | { |
| 1153 | PySequenceMethods *m; |
| 1154 | |
| 1155 | if (s == NULL || o == NULL) |
| 1156 | return null_error(); |
| 1157 | |
| 1158 | m = s->ob_type->tp_as_sequence; |
| 1159 | if (m && HASINPLACE(s) && m->sq_inplace_concat) |
| 1160 | return m->sq_inplace_concat(s, o); |
| 1161 | if (m && m->sq_concat) |
| 1162 | return m->sq_concat(s, o); |
| 1163 | |
| 1164 | return type_error("object can't be concatenated"); |
| 1165 | } |
| 1166 | |
| 1167 | PyObject * |
| 1168 | PySequence_InPlaceRepeat(PyObject *o, int count) |
| 1169 | { |
| 1170 | PySequenceMethods *m; |
| 1171 | |
| 1172 | if (o == NULL) |
| 1173 | return null_error(); |
| 1174 | |
| 1175 | m = o->ob_type->tp_as_sequence; |
| 1176 | if (m && HASINPLACE(o) && m->sq_inplace_repeat) |
| 1177 | return m->sq_inplace_repeat(o, count); |
| 1178 | if (m && m->sq_repeat) |
| 1179 | return m->sq_repeat(o, count); |
| 1180 | |
| 1181 | return type_error("object can't be repeated"); |
| 1182 | } |
| 1183 | |
| 1184 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1185 | PySequence_GetItem(PyObject *s, int i) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1186 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1187 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1188 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1189 | if (s == NULL) |
| 1190 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1192 | m = s->ob_type->tp_as_sequence; |
| 1193 | if (m && m->sq_item) { |
| 1194 | if (i < 0) { |
| 1195 | if (m->sq_length) { |
| 1196 | int l = (*m->sq_length)(s); |
| 1197 | if (l < 0) |
| 1198 | return NULL; |
| 1199 | i += l; |
| 1200 | } |
| 1201 | } |
| 1202 | return m->sq_item(s, i); |
| 1203 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1205 | return type_error("unindexable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1208 | static PyObject * |
| 1209 | sliceobj_from_intint(int i, int j) |
| 1210 | { |
| 1211 | PyObject *start, *end, *slice; |
| 1212 | start = PyInt_FromLong((long)i); |
| 1213 | if (!start) |
| 1214 | return NULL; |
| 1215 | end = PyInt_FromLong((long)j); |
| 1216 | if (!end) { |
| 1217 | Py_DECREF(start); |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | slice = PySlice_New(start, end, NULL); |
| 1221 | Py_DECREF(start); |
| 1222 | Py_DECREF(end); |
| 1223 | return slice; |
| 1224 | } |
| 1225 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1226 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1227 | PySequence_GetSlice(PyObject *s, int i1, int i2) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1228 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1229 | PySequenceMethods *m; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1230 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1231 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1232 | if (!s) return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1233 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1234 | m = s->ob_type->tp_as_sequence; |
| 1235 | if (m && m->sq_slice) { |
| 1236 | if (i1 < 0 || i2 < 0) { |
| 1237 | if (m->sq_length) { |
| 1238 | int l = (*m->sq_length)(s); |
| 1239 | if (l < 0) |
| 1240 | return NULL; |
| 1241 | if (i1 < 0) |
| 1242 | i1 += l; |
| 1243 | if (i2 < 0) |
| 1244 | i2 += l; |
| 1245 | } |
| 1246 | } |
| 1247 | return m->sq_slice(s, i1, i2); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1248 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) { |
| 1249 | PyObject *res; |
| 1250 | PyObject *slice = sliceobj_from_intint(i1, i2); |
| 1251 | if (!slice) |
| 1252 | return NULL; |
| 1253 | res = mp->mp_subscript(s, slice); |
| 1254 | Py_DECREF(slice); |
| 1255 | return res; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1256 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1258 | return type_error("unsliceable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1262 | PySequence_SetItem(PyObject *s, int i, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1263 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1264 | PySequenceMethods *m; |
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 | if (s == NULL) { |
| 1267 | null_error(); |
| 1268 | return -1; |
| 1269 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1270 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1271 | m = s->ob_type->tp_as_sequence; |
| 1272 | if (m && m->sq_ass_item) { |
| 1273 | if (i < 0) { |
| 1274 | if (m->sq_length) { |
| 1275 | int l = (*m->sq_length)(s); |
| 1276 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1277 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1278 | i += l; |
| 1279 | } |
| 1280 | } |
| 1281 | return m->sq_ass_item(s, i, o); |
| 1282 | } |
| 1283 | |
Raymond Hettinger | 98779e0 | 2003-10-27 09:22:16 +0000 | [diff] [blame] | 1284 | type_error("object does not support item assignment"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1285 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1288 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1289 | PySequence_DelItem(PyObject *s, int i) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1290 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1291 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1293 | if (s == NULL) { |
| 1294 | null_error(); |
| 1295 | return -1; |
| 1296 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1298 | m = s->ob_type->tp_as_sequence; |
| 1299 | if (m && m->sq_ass_item) { |
| 1300 | if (i < 0) { |
| 1301 | if (m->sq_length) { |
| 1302 | int l = (*m->sq_length)(s); |
| 1303 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1304 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1305 | i += l; |
| 1306 | } |
| 1307 | } |
| 1308 | return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 1309 | } |
| 1310 | |
| 1311 | type_error("object doesn't support item deletion"); |
| 1312 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1315 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1316 | PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1317 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1318 | PySequenceMethods *m; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1319 | PyMappingMethods *mp; |
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 | if (s == NULL) { |
| 1322 | null_error(); |
| 1323 | return -1; |
| 1324 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1326 | m = s->ob_type->tp_as_sequence; |
| 1327 | if (m && m->sq_ass_slice) { |
| 1328 | if (i1 < 0 || i2 < 0) { |
| 1329 | if (m->sq_length) { |
| 1330 | int l = (*m->sq_length)(s); |
| 1331 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1332 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1333 | if (i1 < 0) |
| 1334 | i1 += l; |
| 1335 | if (i2 < 0) |
| 1336 | i2 += l; |
| 1337 | } |
| 1338 | } |
| 1339 | return m->sq_ass_slice(s, i1, i2, o); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1340 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) { |
| 1341 | int res; |
| 1342 | PyObject *slice = sliceobj_from_intint(i1, i2); |
| 1343 | if (!slice) |
| 1344 | return -1; |
| 1345 | res = mp->mp_ass_subscript(s, slice, o); |
| 1346 | Py_DECREF(slice); |
| 1347 | return res; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1348 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1349 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1350 | type_error("object doesn't support slice assignment"); |
| 1351 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1354 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1355 | PySequence_DelSlice(PyObject *s, int i1, int i2) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1356 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1357 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1358 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1359 | if (s == NULL) { |
| 1360 | null_error(); |
| 1361 | return -1; |
| 1362 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1363 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1364 | m = s->ob_type->tp_as_sequence; |
| 1365 | if (m && m->sq_ass_slice) { |
| 1366 | if (i1 < 0 || i2 < 0) { |
| 1367 | if (m->sq_length) { |
| 1368 | int l = (*m->sq_length)(s); |
| 1369 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1370 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1371 | if (i1 < 0) |
| 1372 | i1 += l; |
| 1373 | if (i2 < 0) |
| 1374 | i2 += l; |
| 1375 | } |
| 1376 | } |
| 1377 | return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL); |
| 1378 | } |
| 1379 | type_error("object doesn't support slice deletion"); |
| 1380 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1383 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1384 | PySequence_Tuple(PyObject *v) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1385 | { |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1386 | PyObject *it; /* iter(v) */ |
| 1387 | int n; /* guess for result tuple size */ |
| 1388 | PyObject *result; |
| 1389 | int j; |
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 | if (v == NULL) |
| 1392 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1393 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1394 | /* Special-case the common tuple and list cases, for efficiency. */ |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1395 | if (PyTuple_CheckExact(v)) { |
| 1396 | /* Note that we can't know whether it's safe to return |
| 1397 | a tuple *subclass* instance as-is, hence the restriction |
Tim Peters | 8ff70a9 | 2001-09-10 23:53:53 +0000 | [diff] [blame] | 1398 | to exact tuples here. In contrast, lists always make |
| 1399 | a copy, so there's no need for exactness below. */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1400 | Py_INCREF(v); |
| 1401 | return v; |
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 (PyList_Check(v)) |
| 1404 | return PyList_AsTuple(v); |
| 1405 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1406 | /* Get iterator. */ |
| 1407 | it = PyObject_GetIter(v); |
| 1408 | if (it == NULL) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1409 | return NULL; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1410 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1411 | /* Guess result size and allocate space. */ |
Raymond Hettinger | 7832cd6 | 2004-01-04 06:08:16 +0000 | [diff] [blame] | 1412 | n = PyObject_Size(v); |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1413 | if (n < 0) { |
| 1414 | PyErr_Clear(); |
| 1415 | n = 10; /* arbitrary */ |
| 1416 | } |
| 1417 | result = PyTuple_New(n); |
| 1418 | if (result == NULL) |
| 1419 | goto Fail; |
| 1420 | |
| 1421 | /* Fill the tuple. */ |
| 1422 | for (j = 0; ; ++j) { |
| 1423 | PyObject *item = PyIter_Next(it); |
| 1424 | if (item == NULL) { |
| 1425 | if (PyErr_Occurred()) |
| 1426 | goto Fail; |
| 1427 | break; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1428 | } |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1429 | if (j >= n) { |
| 1430 | if (n < 500) |
| 1431 | n += 10; |
| 1432 | else |
| 1433 | n += 100; |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 1434 | if (_PyTuple_Resize(&result, n) != 0) { |
Tim Peters | 12d0a6c | 2001-05-05 04:10:25 +0000 | [diff] [blame] | 1435 | Py_DECREF(item); |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1436 | goto Fail; |
Tim Peters | 12d0a6c | 2001-05-05 04:10:25 +0000 | [diff] [blame] | 1437 | } |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1438 | } |
| 1439 | PyTuple_SET_ITEM(result, j, item); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1442 | /* Cut tuple back if guess was too large. */ |
| 1443 | if (j < n && |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 1444 | _PyTuple_Resize(&result, j) != 0) |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1445 | goto Fail; |
| 1446 | |
| 1447 | Py_DECREF(it); |
| 1448 | return result; |
| 1449 | |
| 1450 | Fail: |
| 1451 | Py_XDECREF(result); |
| 1452 | Py_DECREF(it); |
| 1453 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1456 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1457 | PySequence_List(PyObject *v) |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1458 | { |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1459 | PyObject *result; /* result list */ |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1460 | PyObject *rv; /* return value from PyList_Extend */ |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | 5dba9e8 | 1998-07-10 18:03:50 +0000 | [diff] [blame] | 1462 | if (v == NULL) |
| 1463 | return null_error(); |
| 1464 | |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1465 | result = PyList_New(0); |
| 1466 | if (result == NULL) |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1467 | return NULL; |
| 1468 | |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1469 | rv = _PyList_Extend((PyListObject *)result, v); |
| 1470 | if (rv == NULL) { |
| 1471 | Py_DECREF(result); |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1472 | return NULL; |
| 1473 | } |
Raymond Hettinger | 969d8c0 | 2004-03-17 05:24:23 +0000 | [diff] [blame] | 1474 | Py_DECREF(rv); |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1475 | return result; |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1478 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1479 | PySequence_Fast(PyObject *v, const char *m) |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1480 | { |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1481 | PyObject *it; |
| 1482 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1483 | if (v == NULL) |
| 1484 | return null_error(); |
| 1485 | |
Michael W. Hudson | 2801fe1 | 2002-11-05 18:05:49 +0000 | [diff] [blame] | 1486 | if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) { |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1487 | Py_INCREF(v); |
| 1488 | return v; |
| 1489 | } |
| 1490 | |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1491 | it = PyObject_GetIter(v); |
| 1492 | if (it == NULL) { |
| 1493 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1494 | return type_error(m); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | v = PySequence_Tuple(it); |
| 1499 | Py_DECREF(it); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1500 | |
| 1501 | return v; |
| 1502 | } |
| 1503 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1504 | /* Iterate over seq. Result depends on the operation: |
| 1505 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq. |
| 1506 | PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq; |
| 1507 | set ValueError and return -1 if none found; also return -1 on error. |
| 1508 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error. |
| 1509 | */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1510 | int |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1511 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1512 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1513 | int n; |
| 1514 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */ |
| 1515 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1516 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1517 | if (seq == NULL || obj == NULL) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1518 | null_error(); |
| 1519 | return -1; |
| 1520 | } |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1521 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1522 | it = PyObject_GetIter(seq); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1523 | if (it == NULL) { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1524 | type_error("iterable argument required"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1525 | return -1; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1526 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1527 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1528 | n = wrapped = 0; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1529 | for (;;) { |
| 1530 | int cmp; |
| 1531 | PyObject *item = PyIter_Next(it); |
| 1532 | if (item == NULL) { |
| 1533 | if (PyErr_Occurred()) |
| 1534 | goto Fail; |
| 1535 | break; |
| 1536 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1537 | |
| 1538 | cmp = PyObject_RichCompareBool(obj, item, Py_EQ); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1539 | Py_DECREF(item); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1540 | if (cmp < 0) |
| 1541 | goto Fail; |
| 1542 | if (cmp > 0) { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1543 | switch (operation) { |
| 1544 | case PY_ITERSEARCH_COUNT: |
| 1545 | ++n; |
| 1546 | if (n <= 0) { |
| 1547 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1548 | "count exceeds C int size"); |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1549 | goto Fail; |
| 1550 | } |
| 1551 | break; |
| 1552 | |
| 1553 | case PY_ITERSEARCH_INDEX: |
| 1554 | if (wrapped) { |
| 1555 | PyErr_SetString(PyExc_OverflowError, |
| 1556 | "index exceeds C int size"); |
| 1557 | goto Fail; |
| 1558 | } |
| 1559 | goto Done; |
| 1560 | |
| 1561 | case PY_ITERSEARCH_CONTAINS: |
| 1562 | n = 1; |
| 1563 | goto Done; |
| 1564 | |
| 1565 | default: |
| 1566 | assert(!"unknown operation"); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1567 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | if (operation == PY_ITERSEARCH_INDEX) { |
| 1571 | ++n; |
| 1572 | if (n <= 0) |
| 1573 | wrapped = 1; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1574 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1575 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (operation != PY_ITERSEARCH_INDEX) |
| 1578 | goto Done; |
| 1579 | |
| 1580 | PyErr_SetString(PyExc_ValueError, |
| 1581 | "sequence.index(x): x not in sequence"); |
| 1582 | /* fall into failure code */ |
| 1583 | Fail: |
| 1584 | n = -1; |
| 1585 | /* fall through */ |
| 1586 | Done: |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1587 | Py_DECREF(it); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1588 | return n; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1589 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1592 | /* Return # of times o appears in s. */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1593 | int |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1594 | PySequence_Count(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1595 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1596 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1599 | /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq. |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1600 | * Use sq_contains if possible, else defer to _PySequence_IterSearch(). |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1601 | */ |
| 1602 | int |
| 1603 | PySequence_Contains(PyObject *seq, PyObject *ob) |
| 1604 | { |
| 1605 | if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) { |
| 1606 | PySequenceMethods *sqm = seq->ob_type->tp_as_sequence; |
| 1607 | if (sqm != NULL && sqm->sq_contains != NULL) |
| 1608 | return (*sqm->sq_contains)(seq, ob); |
| 1609 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1610 | return _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1613 | /* Backwards compatibility */ |
| 1614 | #undef PySequence_In |
| 1615 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1616 | PySequence_In(PyObject *w, PyObject *v) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1617 | { |
| 1618 | return PySequence_Contains(w, v); |
| 1619 | } |
| 1620 | |
| 1621 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1622 | PySequence_Index(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1623 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1624 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1627 | /* Operations on mappings */ |
| 1628 | |
| 1629 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1630 | PyMapping_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1631 | { |
Raymond Hettinger | e2eda60 | 2004-04-04 08:51:41 +0000 | [diff] [blame] | 1632 | if (PyInstance_Check(o)) |
| 1633 | return PyObject_HasAttrString(o, "__getitem__"); |
| 1634 | |
| 1635 | return o && o->ob_type->tp_as_mapping && |
| 1636 | o->ob_type->tp_as_mapping->mp_subscript && |
Raymond Hettinger | ed9192e | 2004-04-05 08:14:48 +0000 | [diff] [blame] | 1637 | !(o->ob_type->tp_as_sequence && |
| 1638 | o->ob_type->tp_as_sequence->sq_slice); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1641 | int |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1642 | PyMapping_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1643 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1644 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1645 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1646 | if (o == NULL) { |
| 1647 | null_error(); |
| 1648 | return -1; |
| 1649 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1650 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1651 | m = o->ob_type->tp_as_mapping; |
| 1652 | if (m && m->mp_length) |
| 1653 | return m->mp_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1655 | type_error("len() of unsized object"); |
| 1656 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1659 | #undef PyMapping_Length |
| 1660 | int |
| 1661 | PyMapping_Length(PyObject *o) |
| 1662 | { |
| 1663 | return PyMapping_Size(o); |
| 1664 | } |
| 1665 | #define PyMapping_Length PyMapping_Size |
| 1666 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1667 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1668 | PyMapping_GetItemString(PyObject *o, char *key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1669 | { |
| 1670 | PyObject *okey, *r; |
| 1671 | |
| 1672 | if (key == NULL) |
| 1673 | return null_error(); |
| 1674 | |
| 1675 | okey = PyString_FromString(key); |
| 1676 | if (okey == NULL) |
| 1677 | return NULL; |
| 1678 | r = PyObject_GetItem(o, okey); |
| 1679 | Py_DECREF(okey); |
| 1680 | return r; |
| 1681 | } |
| 1682 | |
| 1683 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1684 | PyMapping_SetItemString(PyObject *o, char *key, PyObject *value) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1685 | { |
| 1686 | PyObject *okey; |
| 1687 | int r; |
| 1688 | |
| 1689 | if (key == NULL) { |
| 1690 | null_error(); |
| 1691 | return -1; |
| 1692 | } |
| 1693 | |
| 1694 | okey = PyString_FromString(key); |
| 1695 | if (okey == NULL) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1696 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1697 | r = PyObject_SetItem(o, okey, value); |
| 1698 | Py_DECREF(okey); |
| 1699 | return r; |
| 1700 | } |
| 1701 | |
| 1702 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1703 | PyMapping_HasKeyString(PyObject *o, char *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1704 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1705 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1706 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1707 | v = PyMapping_GetItemString(o, key); |
| 1708 | if (v) { |
| 1709 | Py_DECREF(v); |
| 1710 | return 1; |
| 1711 | } |
| 1712 | PyErr_Clear(); |
| 1713 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1716 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1717 | PyMapping_HasKey(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1718 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1719 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1720 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1721 | v = PyObject_GetItem(o, key); |
| 1722 | if (v) { |
| 1723 | Py_DECREF(v); |
| 1724 | return 1; |
| 1725 | } |
| 1726 | PyErr_Clear(); |
| 1727 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1730 | /* Operations on callable objects */ |
| 1731 | |
| 1732 | /* XXX PyCallable_Check() is in object.c */ |
| 1733 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1734 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1735 | PyObject_CallObject(PyObject *o, PyObject *a) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1736 | { |
Guido van Rossum | 5560b74 | 2001-09-14 16:47:50 +0000 | [diff] [blame] | 1737 | return PyEval_CallObjectWithKeywords(o, a, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1738 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1739 | |
| 1740 | PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1741 | PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 1742 | { |
| 1743 | ternaryfunc call; |
| 1744 | |
| 1745 | if ((call = func->ob_type->tp_call) != NULL) { |
| 1746 | PyObject *result = (*call)(func, arg, kw); |
| 1747 | if (result == NULL && !PyErr_Occurred()) |
| 1748 | PyErr_SetString( |
| 1749 | PyExc_SystemError, |
| 1750 | "NULL result without error in PyObject_Call"); |
| 1751 | return result; |
| 1752 | } |
Fred Drake | 573395a | 2001-11-01 20:26:12 +0000 | [diff] [blame] | 1753 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1754 | func->ob_type->tp_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1755 | return NULL; |
| 1756 | } |
| 1757 | |
| 1758 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1759 | PyObject_CallFunction(PyObject *callable, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1760 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1761 | va_list va; |
| 1762 | PyObject *args, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1763 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1764 | if (callable == NULL) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1765 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1766 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1767 | if (format && *format) { |
| 1768 | va_start(va, format); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1769 | args = Py_VaBuildValue(format, va); |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1770 | va_end(va); |
| 1771 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1772 | else |
| 1773 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1774 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1775 | if (args == NULL) |
| 1776 | return NULL; |
| 1777 | |
| 1778 | if (!PyTuple_Check(args)) { |
| 1779 | PyObject *a; |
| 1780 | |
| 1781 | a = PyTuple_New(1); |
| 1782 | if (a == NULL) |
| 1783 | return NULL; |
| 1784 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1785 | return NULL; |
| 1786 | args = a; |
| 1787 | } |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 1788 | retval = PyObject_Call(callable, args, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1789 | |
| 1790 | Py_DECREF(args); |
| 1791 | |
| 1792 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | PyObject * |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1796 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1797 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1798 | va_list va; |
| 1799 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1800 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1801 | if (o == NULL || name == NULL) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1802 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1804 | func = PyObject_GetAttrString(o, name); |
| 1805 | if (func == NULL) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1806 | PyErr_SetString(PyExc_AttributeError, name); |
| 1807 | return 0; |
| 1808 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1809 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1810 | if (!PyCallable_Check(func)) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1811 | return type_error("call of non-callable attribute"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1812 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1813 | if (format && *format) { |
| 1814 | va_start(va, format); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1815 | args = Py_VaBuildValue(format, va); |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1816 | va_end(va); |
| 1817 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1818 | else |
| 1819 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1820 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1821 | if (!args) |
| 1822 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1823 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1824 | if (!PyTuple_Check(args)) { |
| 1825 | PyObject *a; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1826 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1827 | a = PyTuple_New(1); |
| 1828 | if (a == NULL) |
| 1829 | return NULL; |
| 1830 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1831 | return NULL; |
| 1832 | args = a; |
| 1833 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1834 | |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 1835 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1837 | Py_DECREF(args); |
| 1838 | Py_DECREF(func); |
| 1839 | |
| 1840 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1841 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1842 | |
| 1843 | |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1844 | static PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1845 | objargs_mktuple(va_list va) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1846 | { |
| 1847 | int i, n = 0; |
| 1848 | va_list countva; |
| 1849 | PyObject *result, *tmp; |
| 1850 | |
| 1851 | #ifdef VA_LIST_IS_ARRAY |
| 1852 | memcpy(countva, va, sizeof(va_list)); |
| 1853 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 1854 | #ifdef __va_copy |
| 1855 | __va_copy(countva, va); |
| 1856 | #else |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1857 | countva = va; |
| 1858 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 1859 | #endif |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1860 | |
| 1861 | while (((PyObject *)va_arg(countva, PyObject *)) != NULL) |
| 1862 | ++n; |
| 1863 | result = PyTuple_New(n); |
| 1864 | if (result != NULL && n > 0) { |
| 1865 | for (i = 0; i < n; ++i) { |
| 1866 | tmp = (PyObject *)va_arg(va, PyObject *); |
| 1867 | PyTuple_SET_ITEM(result, i, tmp); |
| 1868 | Py_INCREF(tmp); |
| 1869 | } |
| 1870 | } |
| 1871 | return result; |
| 1872 | } |
| 1873 | |
| 1874 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1875 | PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1876 | { |
| 1877 | PyObject *args, *tmp; |
| 1878 | va_list vargs; |
| 1879 | |
| 1880 | if (callable == NULL || name == NULL) |
| 1881 | return null_error(); |
| 1882 | |
| 1883 | callable = PyObject_GetAttr(callable, name); |
| 1884 | if (callable == NULL) |
| 1885 | return NULL; |
| 1886 | |
| 1887 | /* count the args */ |
| 1888 | va_start(vargs, name); |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1889 | args = objargs_mktuple(vargs); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1890 | va_end(vargs); |
| 1891 | if (args == NULL) { |
| 1892 | Py_DECREF(callable); |
| 1893 | return NULL; |
| 1894 | } |
| 1895 | tmp = PyObject_Call(callable, args, NULL); |
| 1896 | Py_DECREF(args); |
| 1897 | Py_DECREF(callable); |
| 1898 | |
| 1899 | return tmp; |
| 1900 | } |
| 1901 | |
| 1902 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1903 | PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1904 | { |
| 1905 | PyObject *args, *tmp; |
| 1906 | va_list vargs; |
| 1907 | |
| 1908 | if (callable == NULL) |
| 1909 | return null_error(); |
| 1910 | |
| 1911 | /* count the args */ |
| 1912 | va_start(vargs, callable); |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1913 | args = objargs_mktuple(vargs); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1914 | va_end(vargs); |
| 1915 | if (args == NULL) |
| 1916 | return NULL; |
| 1917 | tmp = PyObject_Call(callable, args, NULL); |
| 1918 | Py_DECREF(args); |
| 1919 | |
| 1920 | return tmp; |
| 1921 | } |
| 1922 | |
| 1923 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1924 | /* isinstance(), issubclass() */ |
| 1925 | |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1926 | /* abstract_get_bases() has logically 4 return states, with a sort of 0th |
| 1927 | * state that will almost never happen. |
| 1928 | * |
| 1929 | * 0. creating the __bases__ static string could get a MemoryError |
| 1930 | * 1. getattr(cls, '__bases__') could raise an AttributeError |
| 1931 | * 2. getattr(cls, '__bases__') could raise some other exception |
| 1932 | * 3. getattr(cls, '__bases__') could return a tuple |
| 1933 | * 4. getattr(cls, '__bases__') could return something other than a tuple |
| 1934 | * |
| 1935 | * Only state #3 is a non-error state and only it returns a non-NULL object |
| 1936 | * (it returns the retrieved tuple). |
| 1937 | * |
| 1938 | * Any raised AttributeErrors are masked by clearing the exception and |
| 1939 | * returning NULL. If an object other than a tuple comes out of __bases__, |
| 1940 | * then again, the return value is NULL. So yes, these two situations |
| 1941 | * produce exactly the same results: NULL is returned and no error is set. |
| 1942 | * |
| 1943 | * If some exception other than AttributeError is raised, then NULL is also |
| 1944 | * returned, but the exception is not cleared. That's because we want the |
| 1945 | * exception to be propagated along. |
| 1946 | * |
| 1947 | * Callers are expected to test for PyErr_Occurred() when the return value |
| 1948 | * is NULL to decide whether a valid exception should be propagated or not. |
| 1949 | * When there's no exception to propagate, it's customary for the caller to |
| 1950 | * set a TypeError. |
| 1951 | */ |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1952 | static PyObject * |
| 1953 | abstract_get_bases(PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1954 | { |
| 1955 | static PyObject *__bases__ = NULL; |
| 1956 | PyObject *bases; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1957 | |
| 1958 | if (__bases__ == NULL) { |
| 1959 | __bases__ = PyString_FromString("__bases__"); |
| 1960 | if (__bases__ == NULL) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1961 | return NULL; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1962 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1963 | bases = PyObject_GetAttr(cls, __bases__); |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1964 | if (bases == NULL) { |
| 1965 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1966 | PyErr_Clear(); |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1967 | return NULL; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1968 | } |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1969 | if (!PyTuple_Check(bases)) { |
| 1970 | Py_DECREF(bases); |
| 1971 | return NULL; |
| 1972 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1973 | return bases; |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | static int |
| 1978 | abstract_issubclass(PyObject *derived, PyObject *cls) |
| 1979 | { |
| 1980 | PyObject *bases; |
| 1981 | int i, n; |
| 1982 | int r = 0; |
| 1983 | |
| 1984 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1985 | if (derived == cls) |
| 1986 | return 1; |
| 1987 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1988 | if (PyTuple_Check(cls)) { |
| 1989 | /* Not a general sequence -- that opens up the road to |
| 1990 | recursion and stack overflow. */ |
| 1991 | n = PyTuple_GET_SIZE(cls); |
| 1992 | for (i = 0; i < n; i++) { |
| 1993 | if (derived == PyTuple_GET_ITEM(cls, i)) |
| 1994 | return 1; |
| 1995 | } |
| 1996 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1997 | bases = abstract_get_bases(derived); |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1998 | if (bases == NULL) { |
| 1999 | if (PyErr_Occurred()) |
| 2000 | return -1; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2001 | return 0; |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 2002 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2003 | n = PyTuple_GET_SIZE(bases); |
| 2004 | for (i = 0; i < n; i++) { |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2005 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2006 | if (r != 0) |
| 2007 | break; |
| 2008 | } |
| 2009 | |
| 2010 | Py_DECREF(bases); |
| 2011 | |
| 2012 | return r; |
| 2013 | } |
| 2014 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2015 | static int |
| 2016 | check_class(PyObject *cls, const char *error) |
| 2017 | { |
| 2018 | PyObject *bases = abstract_get_bases(cls); |
| 2019 | if (bases == NULL) { |
| 2020 | /* Do not mask errors. */ |
| 2021 | if (!PyErr_Occurred()) |
| 2022 | PyErr_SetString(PyExc_TypeError, error); |
| 2023 | return 0; |
| 2024 | } |
| 2025 | Py_DECREF(bases); |
| 2026 | return -1; |
| 2027 | } |
| 2028 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2029 | static int |
| 2030 | recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2031 | { |
| 2032 | PyObject *icls; |
| 2033 | static PyObject *__class__ = NULL; |
| 2034 | int retval = 0; |
| 2035 | |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2036 | if (__class__ == NULL) { |
| 2037 | __class__ = PyString_FromString("__class__"); |
| 2038 | if (__class__ == NULL) |
| 2039 | return -1; |
| 2040 | } |
| 2041 | |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2042 | if (PyClass_Check(cls) && PyInstance_Check(inst)) { |
| 2043 | PyObject *inclass = |
| 2044 | (PyObject*)((PyInstanceObject*)inst)->in_class; |
| 2045 | retval = PyClass_IsSubclass(inclass, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2046 | } |
| 2047 | else if (PyType_Check(cls)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2048 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2049 | if (retval == 0) { |
| 2050 | PyObject *c = PyObject_GetAttr(inst, __class__); |
| 2051 | if (c == NULL) { |
| 2052 | PyErr_Clear(); |
| 2053 | } |
| 2054 | else { |
Guido van Rossum | e5b130b | 2003-02-12 03:36:05 +0000 | [diff] [blame] | 2055 | if (c != (PyObject *)(inst->ob_type) && |
| 2056 | PyType_Check(c)) |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2057 | retval = PyType_IsSubtype( |
| 2058 | (PyTypeObject *)c, |
| 2059 | (PyTypeObject *)cls); |
| 2060 | Py_DECREF(c); |
| 2061 | } |
| 2062 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2063 | } |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2064 | else if (PyTuple_Check(cls)) { |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2065 | int i, n; |
| 2066 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2067 | if (!recursion_depth) { |
| 2068 | PyErr_SetString(PyExc_RuntimeError, |
| 2069 | "nest level of tuple too deep"); |
Tim Peters | 5f112eb | 2004-03-21 16:59:09 +0000 | [diff] [blame] | 2070 | return -1; |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2073 | n = PyTuple_GET_SIZE(cls); |
| 2074 | for (i = 0; i < n; i++) { |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2075 | retval = recursive_isinstance( |
| 2076 | inst, |
| 2077 | PyTuple_GET_ITEM(cls, i), |
| 2078 | recursion_depth-1); |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2079 | if (retval != 0) |
| 2080 | break; |
| 2081 | } |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2082 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2083 | else { |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2084 | if (!check_class(cls, |
| 2085 | "isinstance() arg 2 must be a class, type," |
| 2086 | " or tuple of classes and types")) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2087 | return -1; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2088 | icls = PyObject_GetAttr(inst, __class__); |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2089 | if (icls == NULL) { |
| 2090 | PyErr_Clear(); |
| 2091 | retval = 0; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2092 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2093 | else { |
| 2094 | retval = abstract_issubclass(icls, cls); |
| 2095 | Py_DECREF(icls); |
| 2096 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2097 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2098 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2099 | return retval; |
| 2100 | } |
| 2101 | |
| 2102 | int |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2103 | PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 2104 | { |
| 2105 | return recursive_isinstance(inst, cls, Py_GetRecursionLimit()); |
| 2106 | } |
| 2107 | |
| 2108 | static int |
| 2109 | recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2110 | { |
| 2111 | int retval; |
| 2112 | |
| 2113 | if (!PyClass_Check(derived) || !PyClass_Check(cls)) { |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2114 | if (!check_class(derived, |
| 2115 | "issubclass() arg 1 must be a class")) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2116 | return -1; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2117 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2118 | if (PyTuple_Check(cls)) { |
| 2119 | int i; |
| 2120 | int n = PyTuple_GET_SIZE(cls); |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (!recursion_depth) { |
| 2123 | PyErr_SetString(PyExc_RuntimeError, |
| 2124 | "nest level of tuple too deep"); |
Tim Peters | 5f112eb | 2004-03-21 16:59:09 +0000 | [diff] [blame] | 2125 | return -1; |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2126 | } |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2127 | for (i = 0; i < n; ++i) { |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2128 | retval = recursive_issubclass( |
| 2129 | derived, |
| 2130 | PyTuple_GET_ITEM(cls, i), |
| 2131 | recursion_depth-1); |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2132 | if (retval != 0) { |
| 2133 | /* either found it, or got an error */ |
Walter Dörwald | 7e5c6a0 | 2002-12-12 19:14:08 +0000 | [diff] [blame] | 2134 | return retval; |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2135 | } |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2136 | } |
Walter Dörwald | 7e5c6a0 | 2002-12-12 19:14:08 +0000 | [diff] [blame] | 2137 | return 0; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2138 | } |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2139 | else { |
| 2140 | if (!check_class(cls, |
| 2141 | "issubclass() arg 2 must be a class" |
| 2142 | " or tuple of classes")) |
| 2143 | return -1; |
| 2144 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2145 | |
| 2146 | retval = abstract_issubclass(derived, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2147 | } |
| 2148 | else { |
| 2149 | /* shortcut */ |
| 2150 | if (!(retval = (derived == cls))) |
| 2151 | retval = PyClass_IsSubclass(derived, cls); |
| 2152 | } |
| 2153 | |
| 2154 | return retval; |
| 2155 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2156 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2157 | int |
| 2158 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 2159 | { |
| 2160 | return recursive_issubclass(derived, cls, Py_GetRecursionLimit()); |
| 2161 | } |
| 2162 | |
| 2163 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2164 | PyObject * |
| 2165 | PyObject_GetIter(PyObject *o) |
| 2166 | { |
| 2167 | PyTypeObject *t = o->ob_type; |
| 2168 | getiterfunc f = NULL; |
| 2169 | if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER)) |
| 2170 | f = t->tp_iter; |
| 2171 | if (f == NULL) { |
| 2172 | if (PySequence_Check(o)) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2173 | return PySeqIter_New(o); |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 2174 | PyErr_SetString(PyExc_TypeError, |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 2175 | "iteration over non-sequence"); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2176 | return NULL; |
| 2177 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2178 | else { |
| 2179 | PyObject *res = (*f)(o); |
| 2180 | if (res != NULL && !PyIter_Check(res)) { |
| 2181 | PyErr_Format(PyExc_TypeError, |
| 2182 | "iter() returned non-iterator " |
| 2183 | "of type '%.100s'", |
| 2184 | res->ob_type->tp_name); |
| 2185 | Py_DECREF(res); |
| 2186 | res = NULL; |
| 2187 | } |
| 2188 | return res; |
| 2189 | } |
| 2190 | } |
| 2191 | |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2192 | /* Return next item. |
| 2193 | * If an error occurs, return NULL. PyErr_Occurred() will be true. |
| 2194 | * If the iteration terminates normally, return NULL and clear the |
| 2195 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred() |
| 2196 | * will be false. |
| 2197 | * Else return the next object. PyErr_Occurred() will be false. |
| 2198 | */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2199 | PyObject * |
| 2200 | PyIter_Next(PyObject *iter) |
| 2201 | { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2202 | PyObject *result; |
Raymond Hettinger | 8049dde | 2003-03-01 01:44:32 +0000 | [diff] [blame] | 2203 | assert(PyIter_Check(iter)); |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2204 | result = (*iter->ob_type->tp_iternext)(iter); |
| 2205 | if (result == NULL && |
| 2206 | PyErr_Occurred() && |
| 2207 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2208 | PyErr_Clear(); |
| 2209 | return result; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2210 | } |