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( |
| 639 | "can't multiply sequence to non-int"); |
| 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; |
| 968 | if (m && m->nb_int) |
| 969 | return m->nb_int(o); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 970 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 971 | return int_from_string((char*)buffer, buffer_len); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 972 | |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 973 | 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] | 974 | } |
| 975 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 976 | /* Add a check for embedded NULL-bytes in the argument. */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 977 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 978 | long_from_string(const char *s, int len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 979 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 980 | char *end; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 981 | PyObject *x; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 983 | x = PyLong_FromString((char*)s, &end, 10); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 984 | if (x == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 985 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 986 | if (end != s + len) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 987 | PyErr_SetString(PyExc_ValueError, |
| 988 | "null byte in argument for long()"); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 989 | Py_DECREF(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 990 | return NULL; |
| 991 | } |
| 992 | return x; |
| 993 | } |
| 994 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 995 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 996 | PyNumber_Long(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 997 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 998 | PyNumberMethods *m; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 999 | const char *buffer; |
| 1000 | int buffer_len; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1002 | if (o == NULL) |
| 1003 | return null_error(); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1004 | if (PyLong_CheckExact(o)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1005 | Py_INCREF(o); |
| 1006 | return o; |
| 1007 | } |
Tim Peters | db30ac4 | 2002-03-02 04:14:21 +0000 | [diff] [blame] | 1008 | if (PyLong_Check(o)) |
| 1009 | return _PyLong_Copy((PyLongObject *)o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1010 | if (PyString_Check(o)) |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 1011 | /* need to do extra error checking that PyLong_FromString() |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1012 | * doesn't do. In particular long('9.5') must raise an |
| 1013 | * exception, not truncate the float. |
| 1014 | */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1015 | return long_from_string(PyString_AS_STRING(o), |
| 1016 | PyString_GET_SIZE(o)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1017 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1018 | if (PyUnicode_Check(o)) |
| 1019 | /* The above check is done in PyLong_FromUnicode(). */ |
| 1020 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 1021 | PyUnicode_GET_SIZE(o), |
| 1022 | 10); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1023 | #endif |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1024 | m = o->ob_type->tp_as_number; |
| 1025 | if (m && m->nb_long) |
| 1026 | return m->nb_long(o); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1027 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
| 1028 | return long_from_string(buffer, buffer_len); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1029 | |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 1030 | 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] | 1031 | } |
| 1032 | |
| 1033 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1034 | PyNumber_Float(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1035 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1036 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1037 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1038 | if (o == NULL) |
| 1039 | return null_error(); |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1040 | if (PyFloat_CheckExact(o)) { |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1041 | Py_INCREF(o); |
| 1042 | return o; |
| 1043 | } |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1044 | if (PyFloat_Check(o)) { |
| 1045 | PyFloatObject *po = (PyFloatObject *)o; |
| 1046 | return PyFloat_FromDouble(po->ob_fval); |
| 1047 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1048 | if (!PyString_Check(o)) { |
| 1049 | m = o->ob_type->tp_as_number; |
| 1050 | if (m && m->nb_float) |
| 1051 | return m->nb_float(o); |
| 1052 | } |
| 1053 | return PyFloat_FromString(o, NULL); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1056 | /* Operations on sequences */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1057 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1058 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1059 | PySequence_Check(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1060 | { |
Guido van Rossum | 8700b42 | 2001-09-07 20:20:11 +0000 | [diff] [blame] | 1061 | return s != NULL && s->ob_type->tp_as_sequence && |
| 1062 | s->ob_type->tp_as_sequence->sq_item != NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1065 | int |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1066 | PySequence_Size(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1067 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1068 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1070 | if (s == NULL) { |
| 1071 | null_error(); |
| 1072 | return -1; |
| 1073 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1074 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1075 | m = s->ob_type->tp_as_sequence; |
| 1076 | if (m && m->sq_length) |
| 1077 | return m->sq_length(s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1078 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1079 | type_error("len() of unsized object"); |
| 1080 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1083 | #undef PySequence_Length |
| 1084 | int |
| 1085 | PySequence_Length(PyObject *s) |
| 1086 | { |
| 1087 | return PySequence_Size(s); |
| 1088 | } |
| 1089 | #define PySequence_Length PySequence_Size |
| 1090 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1091 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1092 | PySequence_Concat(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1093 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1094 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1095 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1096 | if (s == NULL || o == NULL) |
| 1097 | return null_error(); |
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 | m = s->ob_type->tp_as_sequence; |
| 1100 | if (m && m->sq_concat) |
| 1101 | return m->sq_concat(s, o); |
| 1102 | |
| 1103 | return type_error("object can't be concatenated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1107 | PySequence_Repeat(PyObject *o, int count) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1108 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1109 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1110 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1111 | if (o == NULL) |
| 1112 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1113 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1114 | m = o->ob_type->tp_as_sequence; |
| 1115 | if (m && m->sq_repeat) |
| 1116 | return m->sq_repeat(o, count); |
| 1117 | |
| 1118 | return type_error("object can't be repeated"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1122 | PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
| 1123 | { |
| 1124 | PySequenceMethods *m; |
| 1125 | |
| 1126 | if (s == NULL || o == NULL) |
| 1127 | return null_error(); |
| 1128 | |
| 1129 | m = s->ob_type->tp_as_sequence; |
| 1130 | if (m && HASINPLACE(s) && m->sq_inplace_concat) |
| 1131 | return m->sq_inplace_concat(s, o); |
| 1132 | if (m && m->sq_concat) |
| 1133 | return m->sq_concat(s, o); |
| 1134 | |
| 1135 | return type_error("object can't be concatenated"); |
| 1136 | } |
| 1137 | |
| 1138 | PyObject * |
| 1139 | PySequence_InPlaceRepeat(PyObject *o, int count) |
| 1140 | { |
| 1141 | PySequenceMethods *m; |
| 1142 | |
| 1143 | if (o == NULL) |
| 1144 | return null_error(); |
| 1145 | |
| 1146 | m = o->ob_type->tp_as_sequence; |
| 1147 | if (m && HASINPLACE(o) && m->sq_inplace_repeat) |
| 1148 | return m->sq_inplace_repeat(o, count); |
| 1149 | if (m && m->sq_repeat) |
| 1150 | return m->sq_repeat(o, count); |
| 1151 | |
| 1152 | return type_error("object can't be repeated"); |
| 1153 | } |
| 1154 | |
| 1155 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1156 | PySequence_GetItem(PyObject *s, int i) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1157 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1158 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1159 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1160 | if (s == NULL) |
| 1161 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1162 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1163 | m = s->ob_type->tp_as_sequence; |
| 1164 | if (m && m->sq_item) { |
| 1165 | if (i < 0) { |
| 1166 | if (m->sq_length) { |
| 1167 | int l = (*m->sq_length)(s); |
| 1168 | if (l < 0) |
| 1169 | return NULL; |
| 1170 | i += l; |
| 1171 | } |
| 1172 | } |
| 1173 | return m->sq_item(s, i); |
| 1174 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1175 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1176 | return type_error("unindexable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1179 | static PyObject * |
| 1180 | sliceobj_from_intint(int i, int j) |
| 1181 | { |
| 1182 | PyObject *start, *end, *slice; |
| 1183 | start = PyInt_FromLong((long)i); |
| 1184 | if (!start) |
| 1185 | return NULL; |
| 1186 | end = PyInt_FromLong((long)j); |
| 1187 | if (!end) { |
| 1188 | Py_DECREF(start); |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | slice = PySlice_New(start, end, NULL); |
| 1192 | Py_DECREF(start); |
| 1193 | Py_DECREF(end); |
| 1194 | return slice; |
| 1195 | } |
| 1196 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1197 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1198 | PySequence_GetSlice(PyObject *s, int i1, int i2) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1199 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1200 | PySequenceMethods *m; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1201 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1202 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1203 | if (!s) return null_error(); |
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 | m = s->ob_type->tp_as_sequence; |
| 1206 | if (m && m->sq_slice) { |
| 1207 | if (i1 < 0 || i2 < 0) { |
| 1208 | if (m->sq_length) { |
| 1209 | int l = (*m->sq_length)(s); |
| 1210 | if (l < 0) |
| 1211 | return NULL; |
| 1212 | if (i1 < 0) |
| 1213 | i1 += l; |
| 1214 | if (i2 < 0) |
| 1215 | i2 += l; |
| 1216 | } |
| 1217 | } |
| 1218 | return m->sq_slice(s, i1, i2); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1219 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) { |
| 1220 | PyObject *res; |
| 1221 | PyObject *slice = sliceobj_from_intint(i1, i2); |
| 1222 | if (!slice) |
| 1223 | return NULL; |
| 1224 | res = mp->mp_subscript(s, slice); |
| 1225 | Py_DECREF(slice); |
| 1226 | return res; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1227 | } |
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 | return type_error("unsliceable object"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1233 | PySequence_SetItem(PyObject *s, int i, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1234 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1235 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1236 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1237 | if (s == NULL) { |
| 1238 | null_error(); |
| 1239 | return -1; |
| 1240 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1241 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1242 | m = s->ob_type->tp_as_sequence; |
| 1243 | if (m && m->sq_ass_item) { |
| 1244 | if (i < 0) { |
| 1245 | if (m->sq_length) { |
| 1246 | int l = (*m->sq_length)(s); |
| 1247 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1248 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1249 | i += l; |
| 1250 | } |
| 1251 | } |
| 1252 | return m->sq_ass_item(s, i, o); |
| 1253 | } |
| 1254 | |
Raymond Hettinger | 98779e0 | 2003-10-27 09:22:16 +0000 | [diff] [blame] | 1255 | type_error("object does not support item assignment"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1256 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1259 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1260 | PySequence_DelItem(PyObject *s, int i) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1261 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1262 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1263 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1264 | if (s == NULL) { |
| 1265 | null_error(); |
| 1266 | return -1; |
| 1267 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1268 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1269 | m = s->ob_type->tp_as_sequence; |
| 1270 | if (m && m->sq_ass_item) { |
| 1271 | if (i < 0) { |
| 1272 | if (m->sq_length) { |
| 1273 | int l = (*m->sq_length)(s); |
| 1274 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1275 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1276 | i += l; |
| 1277 | } |
| 1278 | } |
| 1279 | return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 1280 | } |
| 1281 | |
| 1282 | type_error("object doesn't support item deletion"); |
| 1283 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1286 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1287 | PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1288 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1289 | PySequenceMethods *m; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1290 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1291 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1292 | if (s == NULL) { |
| 1293 | null_error(); |
| 1294 | return -1; |
| 1295 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1296 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1297 | m = s->ob_type->tp_as_sequence; |
| 1298 | if (m && m->sq_ass_slice) { |
| 1299 | if (i1 < 0 || i2 < 0) { |
| 1300 | if (m->sq_length) { |
| 1301 | int l = (*m->sq_length)(s); |
| 1302 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1303 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1304 | if (i1 < 0) |
| 1305 | i1 += l; |
| 1306 | if (i2 < 0) |
| 1307 | i2 += l; |
| 1308 | } |
| 1309 | } |
| 1310 | return m->sq_ass_slice(s, i1, i2, o); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1311 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) { |
| 1312 | int res; |
| 1313 | PyObject *slice = sliceobj_from_intint(i1, i2); |
| 1314 | if (!slice) |
| 1315 | return -1; |
| 1316 | res = mp->mp_ass_subscript(s, slice, o); |
| 1317 | Py_DECREF(slice); |
| 1318 | return res; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1319 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1320 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1321 | type_error("object doesn't support slice assignment"); |
| 1322 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1325 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1326 | PySequence_DelSlice(PyObject *s, int i1, int i2) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1327 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1328 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1330 | if (s == NULL) { |
| 1331 | null_error(); |
| 1332 | return -1; |
| 1333 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1335 | m = s->ob_type->tp_as_sequence; |
| 1336 | if (m && m->sq_ass_slice) { |
| 1337 | if (i1 < 0 || i2 < 0) { |
| 1338 | if (m->sq_length) { |
| 1339 | int l = (*m->sq_length)(s); |
| 1340 | if (l < 0) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1341 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1342 | if (i1 < 0) |
| 1343 | i1 += l; |
| 1344 | if (i2 < 0) |
| 1345 | i2 += l; |
| 1346 | } |
| 1347 | } |
| 1348 | return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL); |
| 1349 | } |
| 1350 | type_error("object doesn't support slice deletion"); |
| 1351 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1354 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1355 | PySequence_Tuple(PyObject *v) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1356 | { |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1357 | PyObject *it; /* iter(v) */ |
| 1358 | int n; /* guess for result tuple size */ |
| 1359 | PyObject *result; |
| 1360 | int j; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1361 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1362 | if (v == NULL) |
| 1363 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1364 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1365 | /* Special-case the common tuple and list cases, for efficiency. */ |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1366 | if (PyTuple_CheckExact(v)) { |
| 1367 | /* Note that we can't know whether it's safe to return |
| 1368 | a tuple *subclass* instance as-is, hence the restriction |
Tim Peters | 8ff70a9 | 2001-09-10 23:53:53 +0000 | [diff] [blame] | 1369 | to exact tuples here. In contrast, lists always make |
| 1370 | a copy, so there's no need for exactness below. */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1371 | Py_INCREF(v); |
| 1372 | return v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1373 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1374 | if (PyList_Check(v)) |
| 1375 | return PyList_AsTuple(v); |
| 1376 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1377 | /* Get iterator. */ |
| 1378 | it = PyObject_GetIter(v); |
| 1379 | if (it == NULL) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1380 | return NULL; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1381 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1382 | /* Guess result size and allocate space. */ |
Raymond Hettinger | 7832cd6 | 2004-01-04 06:08:16 +0000 | [diff] [blame] | 1383 | n = PyObject_Size(v); |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1384 | if (n < 0) { |
| 1385 | PyErr_Clear(); |
| 1386 | n = 10; /* arbitrary */ |
| 1387 | } |
| 1388 | result = PyTuple_New(n); |
| 1389 | if (result == NULL) |
| 1390 | goto Fail; |
| 1391 | |
| 1392 | /* Fill the tuple. */ |
| 1393 | for (j = 0; ; ++j) { |
| 1394 | PyObject *item = PyIter_Next(it); |
| 1395 | if (item == NULL) { |
| 1396 | if (PyErr_Occurred()) |
| 1397 | goto Fail; |
| 1398 | break; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1399 | } |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1400 | if (j >= n) { |
| 1401 | if (n < 500) |
| 1402 | n += 10; |
| 1403 | else |
| 1404 | n += 100; |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 1405 | if (_PyTuple_Resize(&result, n) != 0) { |
Tim Peters | 12d0a6c | 2001-05-05 04:10:25 +0000 | [diff] [blame] | 1406 | Py_DECREF(item); |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1407 | goto Fail; |
Tim Peters | 12d0a6c | 2001-05-05 04:10:25 +0000 | [diff] [blame] | 1408 | } |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1409 | } |
| 1410 | PyTuple_SET_ITEM(result, j, item); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1413 | /* Cut tuple back if guess was too large. */ |
| 1414 | if (j < n && |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 1415 | _PyTuple_Resize(&result, j) != 0) |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1416 | goto Fail; |
| 1417 | |
| 1418 | Py_DECREF(it); |
| 1419 | return result; |
| 1420 | |
| 1421 | Fail: |
| 1422 | Py_XDECREF(result); |
| 1423 | Py_DECREF(it); |
| 1424 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1427 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1428 | PySequence_List(PyObject *v) |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1429 | { |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1430 | PyObject *result; /* result list */ |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1431 | PyObject *rv; /* return value from PyList_Extend */ |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1432 | |
Guido van Rossum | 5dba9e8 | 1998-07-10 18:03:50 +0000 | [diff] [blame] | 1433 | if (v == NULL) |
| 1434 | return null_error(); |
| 1435 | |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1436 | result = PyList_New(0); |
| 1437 | if (result == NULL) |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1438 | return NULL; |
| 1439 | |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 1440 | rv = _PyList_Extend((PyListObject *)result, v); |
| 1441 | if (rv == NULL) { |
| 1442 | Py_DECREF(result); |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1443 | return NULL; |
| 1444 | } |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1445 | return result; |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1448 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1449 | PySequence_Fast(PyObject *v, const char *m) |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1450 | { |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1451 | PyObject *it; |
| 1452 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1453 | if (v == NULL) |
| 1454 | return null_error(); |
| 1455 | |
Michael W. Hudson | 2801fe1 | 2002-11-05 18:05:49 +0000 | [diff] [blame] | 1456 | if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) { |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1457 | Py_INCREF(v); |
| 1458 | return v; |
| 1459 | } |
| 1460 | |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1461 | it = PyObject_GetIter(v); |
| 1462 | if (it == NULL) { |
| 1463 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1464 | return type_error(m); |
| 1465 | return NULL; |
| 1466 | } |
| 1467 | |
| 1468 | v = PySequence_Tuple(it); |
| 1469 | Py_DECREF(it); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1470 | |
| 1471 | return v; |
| 1472 | } |
| 1473 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1474 | /* Iterate over seq. Result depends on the operation: |
| 1475 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq. |
| 1476 | PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq; |
| 1477 | set ValueError and return -1 if none found; also return -1 on error. |
| 1478 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error. |
| 1479 | */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1480 | int |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1481 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1482 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1483 | int n; |
| 1484 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */ |
| 1485 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1486 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1487 | if (seq == NULL || obj == NULL) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1488 | null_error(); |
| 1489 | return -1; |
| 1490 | } |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1491 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1492 | it = PyObject_GetIter(seq); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1493 | if (it == NULL) { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1494 | type_error("iterable argument required"); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1495 | return -1; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1496 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1497 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1498 | n = wrapped = 0; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1499 | for (;;) { |
| 1500 | int cmp; |
| 1501 | PyObject *item = PyIter_Next(it); |
| 1502 | if (item == NULL) { |
| 1503 | if (PyErr_Occurred()) |
| 1504 | goto Fail; |
| 1505 | break; |
| 1506 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1507 | |
| 1508 | cmp = PyObject_RichCompareBool(obj, item, Py_EQ); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1509 | Py_DECREF(item); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1510 | if (cmp < 0) |
| 1511 | goto Fail; |
| 1512 | if (cmp > 0) { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1513 | switch (operation) { |
| 1514 | case PY_ITERSEARCH_COUNT: |
| 1515 | ++n; |
| 1516 | if (n <= 0) { |
| 1517 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1518 | "count exceeds C int size"); |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1519 | goto Fail; |
| 1520 | } |
| 1521 | break; |
| 1522 | |
| 1523 | case PY_ITERSEARCH_INDEX: |
| 1524 | if (wrapped) { |
| 1525 | PyErr_SetString(PyExc_OverflowError, |
| 1526 | "index exceeds C int size"); |
| 1527 | goto Fail; |
| 1528 | } |
| 1529 | goto Done; |
| 1530 | |
| 1531 | case PY_ITERSEARCH_CONTAINS: |
| 1532 | n = 1; |
| 1533 | goto Done; |
| 1534 | |
| 1535 | default: |
| 1536 | assert(!"unknown operation"); |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1537 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | if (operation == PY_ITERSEARCH_INDEX) { |
| 1541 | ++n; |
| 1542 | if (n <= 0) |
| 1543 | wrapped = 1; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1544 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1545 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1546 | |
| 1547 | if (operation != PY_ITERSEARCH_INDEX) |
| 1548 | goto Done; |
| 1549 | |
| 1550 | PyErr_SetString(PyExc_ValueError, |
| 1551 | "sequence.index(x): x not in sequence"); |
| 1552 | /* fall into failure code */ |
| 1553 | Fail: |
| 1554 | n = -1; |
| 1555 | /* fall through */ |
| 1556 | Done: |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1557 | Py_DECREF(it); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1558 | return n; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1559 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1562 | /* Return # of times o appears in s. */ |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1563 | int |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1564 | PySequence_Count(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1565 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1566 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1569 | /* 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] | 1570 | * Use sq_contains if possible, else defer to _PySequence_IterSearch(). |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1571 | */ |
| 1572 | int |
| 1573 | PySequence_Contains(PyObject *seq, PyObject *ob) |
| 1574 | { |
| 1575 | if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) { |
| 1576 | PySequenceMethods *sqm = seq->ob_type->tp_as_sequence; |
| 1577 | if (sqm != NULL && sqm->sq_contains != NULL) |
| 1578 | return (*sqm->sq_contains)(seq, ob); |
| 1579 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1580 | return _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1583 | /* Backwards compatibility */ |
| 1584 | #undef PySequence_In |
| 1585 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1586 | PySequence_In(PyObject *w, PyObject *v) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1587 | { |
| 1588 | return PySequence_Contains(w, v); |
| 1589 | } |
| 1590 | |
| 1591 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1592 | PySequence_Index(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1593 | { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1594 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1597 | /* Operations on mappings */ |
| 1598 | |
| 1599 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1600 | PyMapping_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1601 | { |
Guido van Rossum | 8700b42 | 2001-09-07 20:20:11 +0000 | [diff] [blame] | 1602 | return o && o->ob_type->tp_as_mapping && |
| 1603 | o->ob_type->tp_as_mapping->mp_subscript; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1606 | int |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1607 | PyMapping_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1608 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1609 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1611 | if (o == NULL) { |
| 1612 | null_error(); |
| 1613 | return -1; |
| 1614 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1615 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1616 | m = o->ob_type->tp_as_mapping; |
| 1617 | if (m && m->mp_length) |
| 1618 | return m->mp_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1619 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1620 | type_error("len() of unsized object"); |
| 1621 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1624 | #undef PyMapping_Length |
| 1625 | int |
| 1626 | PyMapping_Length(PyObject *o) |
| 1627 | { |
| 1628 | return PyMapping_Size(o); |
| 1629 | } |
| 1630 | #define PyMapping_Length PyMapping_Size |
| 1631 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1632 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1633 | PyMapping_GetItemString(PyObject *o, char *key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1634 | { |
| 1635 | PyObject *okey, *r; |
| 1636 | |
| 1637 | if (key == NULL) |
| 1638 | return null_error(); |
| 1639 | |
| 1640 | okey = PyString_FromString(key); |
| 1641 | if (okey == NULL) |
| 1642 | return NULL; |
| 1643 | r = PyObject_GetItem(o, okey); |
| 1644 | Py_DECREF(okey); |
| 1645 | return r; |
| 1646 | } |
| 1647 | |
| 1648 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1649 | PyMapping_SetItemString(PyObject *o, char *key, PyObject *value) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1650 | { |
| 1651 | PyObject *okey; |
| 1652 | int r; |
| 1653 | |
| 1654 | if (key == NULL) { |
| 1655 | null_error(); |
| 1656 | return -1; |
| 1657 | } |
| 1658 | |
| 1659 | okey = PyString_FromString(key); |
| 1660 | if (okey == NULL) |
Guido van Rossum | ed6219b | 1998-05-29 02:59:33 +0000 | [diff] [blame] | 1661 | return -1; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1662 | r = PyObject_SetItem(o, okey, value); |
| 1663 | Py_DECREF(okey); |
| 1664 | return r; |
| 1665 | } |
| 1666 | |
| 1667 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1668 | PyMapping_HasKeyString(PyObject *o, char *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1669 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1670 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1671 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1672 | v = PyMapping_GetItemString(o, key); |
| 1673 | if (v) { |
| 1674 | Py_DECREF(v); |
| 1675 | return 1; |
| 1676 | } |
| 1677 | PyErr_Clear(); |
| 1678 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1681 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1682 | PyMapping_HasKey(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1683 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1684 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1685 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1686 | v = PyObject_GetItem(o, key); |
| 1687 | if (v) { |
| 1688 | Py_DECREF(v); |
| 1689 | return 1; |
| 1690 | } |
| 1691 | PyErr_Clear(); |
| 1692 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1695 | /* Operations on callable objects */ |
| 1696 | |
| 1697 | /* XXX PyCallable_Check() is in object.c */ |
| 1698 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1699 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1700 | PyObject_CallObject(PyObject *o, PyObject *a) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1701 | { |
Guido van Rossum | 5560b74 | 2001-09-14 16:47:50 +0000 | [diff] [blame] | 1702 | return PyEval_CallObjectWithKeywords(o, a, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1703 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1704 | |
| 1705 | PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1706 | PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 1707 | { |
| 1708 | ternaryfunc call; |
| 1709 | |
| 1710 | if ((call = func->ob_type->tp_call) != NULL) { |
| 1711 | PyObject *result = (*call)(func, arg, kw); |
| 1712 | if (result == NULL && !PyErr_Occurred()) |
| 1713 | PyErr_SetString( |
| 1714 | PyExc_SystemError, |
| 1715 | "NULL result without error in PyObject_Call"); |
| 1716 | return result; |
| 1717 | } |
Fred Drake | 573395a | 2001-11-01 20:26:12 +0000 | [diff] [blame] | 1718 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1719 | func->ob_type->tp_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1720 | return NULL; |
| 1721 | } |
| 1722 | |
| 1723 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1724 | PyObject_CallFunction(PyObject *callable, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1725 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1726 | va_list va; |
| 1727 | PyObject *args, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1728 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1729 | if (callable == NULL) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1730 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1731 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1732 | if (format && *format) { |
| 1733 | va_start(va, format); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1734 | args = Py_VaBuildValue(format, va); |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1735 | va_end(va); |
| 1736 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1737 | else |
| 1738 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1739 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1740 | if (args == NULL) |
| 1741 | return NULL; |
| 1742 | |
| 1743 | if (!PyTuple_Check(args)) { |
| 1744 | PyObject *a; |
| 1745 | |
| 1746 | a = PyTuple_New(1); |
| 1747 | if (a == NULL) |
| 1748 | return NULL; |
| 1749 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1750 | return NULL; |
| 1751 | args = a; |
| 1752 | } |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 1753 | retval = PyObject_Call(callable, args, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1754 | |
| 1755 | Py_DECREF(args); |
| 1756 | |
| 1757 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | PyObject * |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1761 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1762 | { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1763 | va_list va; |
| 1764 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1765 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1766 | if (o == NULL || name == NULL) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1767 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1768 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1769 | func = PyObject_GetAttrString(o, name); |
| 1770 | if (func == NULL) { |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1771 | PyErr_SetString(PyExc_AttributeError, name); |
| 1772 | return 0; |
| 1773 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1774 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1775 | if (!PyCallable_Check(func)) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1776 | return type_error("call of non-callable attribute"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1777 | |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1778 | if (format && *format) { |
| 1779 | va_start(va, format); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1780 | args = Py_VaBuildValue(format, va); |
Fred Drake | b92cf06 | 2001-10-27 06:16:31 +0000 | [diff] [blame] | 1781 | va_end(va); |
| 1782 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1783 | else |
| 1784 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1785 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1786 | if (!args) |
| 1787 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1788 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1789 | if (!PyTuple_Check(args)) { |
| 1790 | PyObject *a; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1792 | a = PyTuple_New(1); |
| 1793 | if (a == NULL) |
| 1794 | return NULL; |
| 1795 | if (PyTuple_SetItem(a, 0, args) < 0) |
| 1796 | return NULL; |
| 1797 | args = a; |
| 1798 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1799 | |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 1800 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1801 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1802 | Py_DECREF(args); |
| 1803 | Py_DECREF(func); |
| 1804 | |
| 1805 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1806 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1807 | |
| 1808 | |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1809 | static PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1810 | objargs_mktuple(va_list va) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1811 | { |
| 1812 | int i, n = 0; |
| 1813 | va_list countva; |
| 1814 | PyObject *result, *tmp; |
| 1815 | |
| 1816 | #ifdef VA_LIST_IS_ARRAY |
| 1817 | memcpy(countva, va, sizeof(va_list)); |
| 1818 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 1819 | #ifdef __va_copy |
| 1820 | __va_copy(countva, va); |
| 1821 | #else |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1822 | countva = va; |
| 1823 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 1824 | #endif |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1825 | |
| 1826 | while (((PyObject *)va_arg(countva, PyObject *)) != NULL) |
| 1827 | ++n; |
| 1828 | result = PyTuple_New(n); |
| 1829 | if (result != NULL && n > 0) { |
| 1830 | for (i = 0; i < n; ++i) { |
| 1831 | tmp = (PyObject *)va_arg(va, PyObject *); |
| 1832 | PyTuple_SET_ITEM(result, i, tmp); |
| 1833 | Py_INCREF(tmp); |
| 1834 | } |
| 1835 | } |
| 1836 | return result; |
| 1837 | } |
| 1838 | |
| 1839 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1840 | PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1841 | { |
| 1842 | PyObject *args, *tmp; |
| 1843 | va_list vargs; |
| 1844 | |
| 1845 | if (callable == NULL || name == NULL) |
| 1846 | return null_error(); |
| 1847 | |
| 1848 | callable = PyObject_GetAttr(callable, name); |
| 1849 | if (callable == NULL) |
| 1850 | return NULL; |
| 1851 | |
| 1852 | /* count the args */ |
| 1853 | va_start(vargs, name); |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1854 | args = objargs_mktuple(vargs); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1855 | va_end(vargs); |
| 1856 | if (args == NULL) { |
| 1857 | Py_DECREF(callable); |
| 1858 | return NULL; |
| 1859 | } |
| 1860 | tmp = PyObject_Call(callable, args, NULL); |
| 1861 | Py_DECREF(args); |
| 1862 | Py_DECREF(callable); |
| 1863 | |
| 1864 | return tmp; |
| 1865 | } |
| 1866 | |
| 1867 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1868 | PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1869 | { |
| 1870 | PyObject *args, *tmp; |
| 1871 | va_list vargs; |
| 1872 | |
| 1873 | if (callable == NULL) |
| 1874 | return null_error(); |
| 1875 | |
| 1876 | /* count the args */ |
| 1877 | va_start(vargs, callable); |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 1878 | args = objargs_mktuple(vargs); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 1879 | va_end(vargs); |
| 1880 | if (args == NULL) |
| 1881 | return NULL; |
| 1882 | tmp = PyObject_Call(callable, args, NULL); |
| 1883 | Py_DECREF(args); |
| 1884 | |
| 1885 | return tmp; |
| 1886 | } |
| 1887 | |
| 1888 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1889 | /* isinstance(), issubclass() */ |
| 1890 | |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1891 | /* abstract_get_bases() has logically 4 return states, with a sort of 0th |
| 1892 | * state that will almost never happen. |
| 1893 | * |
| 1894 | * 0. creating the __bases__ static string could get a MemoryError |
| 1895 | * 1. getattr(cls, '__bases__') could raise an AttributeError |
| 1896 | * 2. getattr(cls, '__bases__') could raise some other exception |
| 1897 | * 3. getattr(cls, '__bases__') could return a tuple |
| 1898 | * 4. getattr(cls, '__bases__') could return something other than a tuple |
| 1899 | * |
| 1900 | * Only state #3 is a non-error state and only it returns a non-NULL object |
| 1901 | * (it returns the retrieved tuple). |
| 1902 | * |
| 1903 | * Any raised AttributeErrors are masked by clearing the exception and |
| 1904 | * returning NULL. If an object other than a tuple comes out of __bases__, |
| 1905 | * then again, the return value is NULL. So yes, these two situations |
| 1906 | * produce exactly the same results: NULL is returned and no error is set. |
| 1907 | * |
| 1908 | * If some exception other than AttributeError is raised, then NULL is also |
| 1909 | * returned, but the exception is not cleared. That's because we want the |
| 1910 | * exception to be propagated along. |
| 1911 | * |
| 1912 | * Callers are expected to test for PyErr_Occurred() when the return value |
| 1913 | * is NULL to decide whether a valid exception should be propagated or not. |
| 1914 | * When there's no exception to propagate, it's customary for the caller to |
| 1915 | * set a TypeError. |
| 1916 | */ |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1917 | static PyObject * |
| 1918 | abstract_get_bases(PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1919 | { |
| 1920 | static PyObject *__bases__ = NULL; |
| 1921 | PyObject *bases; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1922 | |
| 1923 | if (__bases__ == NULL) { |
| 1924 | __bases__ = PyString_FromString("__bases__"); |
| 1925 | if (__bases__ == NULL) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1926 | return NULL; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1927 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1928 | bases = PyObject_GetAttr(cls, __bases__); |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1929 | if (bases == NULL) { |
| 1930 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1931 | PyErr_Clear(); |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1932 | return NULL; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1933 | } |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1934 | if (!PyTuple_Check(bases)) { |
| 1935 | Py_DECREF(bases); |
| 1936 | return NULL; |
| 1937 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1938 | return bases; |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | static int |
| 1943 | abstract_issubclass(PyObject *derived, PyObject *cls) |
| 1944 | { |
| 1945 | PyObject *bases; |
| 1946 | int i, n; |
| 1947 | int r = 0; |
| 1948 | |
| 1949 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1950 | if (derived == cls) |
| 1951 | return 1; |
| 1952 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1953 | if (PyTuple_Check(cls)) { |
| 1954 | /* Not a general sequence -- that opens up the road to |
| 1955 | recursion and stack overflow. */ |
| 1956 | n = PyTuple_GET_SIZE(cls); |
| 1957 | for (i = 0; i < n; i++) { |
| 1958 | if (derived == PyTuple_GET_ITEM(cls, i)) |
| 1959 | return 1; |
| 1960 | } |
| 1961 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1962 | bases = abstract_get_bases(derived); |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1963 | if (bases == NULL) { |
| 1964 | if (PyErr_Occurred()) |
| 1965 | return -1; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1966 | return 0; |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 1967 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1968 | n = PyTuple_GET_SIZE(bases); |
| 1969 | for (i = 0; i < n; i++) { |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 1970 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1971 | if (r != 0) |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | Py_DECREF(bases); |
| 1976 | |
| 1977 | return r; |
| 1978 | } |
| 1979 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1980 | static int |
| 1981 | check_class(PyObject *cls, const char *error) |
| 1982 | { |
| 1983 | PyObject *bases = abstract_get_bases(cls); |
| 1984 | if (bases == NULL) { |
| 1985 | /* Do not mask errors. */ |
| 1986 | if (!PyErr_Occurred()) |
| 1987 | PyErr_SetString(PyExc_TypeError, error); |
| 1988 | return 0; |
| 1989 | } |
| 1990 | Py_DECREF(bases); |
| 1991 | return -1; |
| 1992 | } |
| 1993 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1994 | int |
| 1995 | PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 1996 | { |
| 1997 | PyObject *icls; |
| 1998 | static PyObject *__class__ = NULL; |
| 1999 | int retval = 0; |
| 2000 | |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2001 | if (__class__ == NULL) { |
| 2002 | __class__ = PyString_FromString("__class__"); |
| 2003 | if (__class__ == NULL) |
| 2004 | return -1; |
| 2005 | } |
| 2006 | |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2007 | if (PyClass_Check(cls) && PyInstance_Check(inst)) { |
| 2008 | PyObject *inclass = |
| 2009 | (PyObject*)((PyInstanceObject*)inst)->in_class; |
| 2010 | retval = PyClass_IsSubclass(inclass, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2011 | } |
| 2012 | else if (PyType_Check(cls)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2013 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2014 | if (retval == 0) { |
| 2015 | PyObject *c = PyObject_GetAttr(inst, __class__); |
| 2016 | if (c == NULL) { |
| 2017 | PyErr_Clear(); |
| 2018 | } |
| 2019 | else { |
Guido van Rossum | e5b130b | 2003-02-12 03:36:05 +0000 | [diff] [blame] | 2020 | if (c != (PyObject *)(inst->ob_type) && |
| 2021 | PyType_Check(c)) |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2022 | retval = PyType_IsSubtype( |
| 2023 | (PyTypeObject *)c, |
| 2024 | (PyTypeObject *)cls); |
| 2025 | Py_DECREF(c); |
| 2026 | } |
| 2027 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2028 | } |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2029 | else if (PyTuple_Check(cls)) { |
| 2030 | /* Not a general sequence -- that opens up the road to |
| 2031 | recursion and stack overflow. */ |
| 2032 | int i, n; |
| 2033 | |
| 2034 | n = PyTuple_GET_SIZE(cls); |
| 2035 | for (i = 0; i < n; i++) { |
| 2036 | retval = PyObject_IsInstance( |
| 2037 | inst, PyTuple_GET_ITEM(cls, i)); |
| 2038 | if (retval != 0) |
| 2039 | break; |
| 2040 | } |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2041 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2042 | else { |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2043 | if (!check_class(cls, |
| 2044 | "isinstance() arg 2 must be a class, type," |
| 2045 | " or tuple of classes and types")) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2046 | return -1; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2047 | icls = PyObject_GetAttr(inst, __class__); |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2048 | if (icls == NULL) { |
| 2049 | PyErr_Clear(); |
| 2050 | retval = 0; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2051 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2052 | else { |
| 2053 | retval = abstract_issubclass(icls, cls); |
| 2054 | Py_DECREF(icls); |
| 2055 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2056 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2058 | return retval; |
| 2059 | } |
| 2060 | |
| 2061 | int |
| 2062 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 2063 | { |
| 2064 | int retval; |
| 2065 | |
| 2066 | if (!PyClass_Check(derived) || !PyClass_Check(cls)) { |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2067 | if (!check_class(derived, |
| 2068 | "issubclass() arg 1 must be a class")) |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2069 | return -1; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2070 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2071 | if (PyTuple_Check(cls)) { |
| 2072 | int i; |
| 2073 | int n = PyTuple_GET_SIZE(cls); |
| 2074 | for (i = 0; i < n; ++i) { |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2075 | retval = PyObject_IsSubclass( |
| 2076 | derived, PyTuple_GET_ITEM(cls, i)); |
| 2077 | if (retval != 0) { |
| 2078 | /* either found it, or got an error */ |
Walter Dörwald | 7e5c6a0 | 2002-12-12 19:14:08 +0000 | [diff] [blame] | 2079 | return retval; |
Guido van Rossum | f6c9ba8 | 2003-02-10 16:05:43 +0000 | [diff] [blame] | 2080 | } |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2081 | } |
Walter Dörwald | 7e5c6a0 | 2002-12-12 19:14:08 +0000 | [diff] [blame] | 2082 | return 0; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2083 | } |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2084 | else { |
| 2085 | if (!check_class(cls, |
| 2086 | "issubclass() arg 2 must be a class" |
| 2087 | " or tuple of classes")) |
| 2088 | return -1; |
| 2089 | } |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2090 | |
| 2091 | retval = abstract_issubclass(derived, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2092 | } |
| 2093 | else { |
| 2094 | /* shortcut */ |
| 2095 | if (!(retval = (derived == cls))) |
| 2096 | retval = PyClass_IsSubclass(derived, cls); |
| 2097 | } |
| 2098 | |
| 2099 | return retval; |
| 2100 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2101 | |
| 2102 | PyObject * |
| 2103 | PyObject_GetIter(PyObject *o) |
| 2104 | { |
| 2105 | PyTypeObject *t = o->ob_type; |
| 2106 | getiterfunc f = NULL; |
| 2107 | if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER)) |
| 2108 | f = t->tp_iter; |
| 2109 | if (f == NULL) { |
| 2110 | if (PySequence_Check(o)) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2111 | return PySeqIter_New(o); |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 2112 | PyErr_SetString(PyExc_TypeError, |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 2113 | "iteration over non-sequence"); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2114 | return NULL; |
| 2115 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2116 | else { |
| 2117 | PyObject *res = (*f)(o); |
| 2118 | if (res != NULL && !PyIter_Check(res)) { |
| 2119 | PyErr_Format(PyExc_TypeError, |
| 2120 | "iter() returned non-iterator " |
| 2121 | "of type '%.100s'", |
| 2122 | res->ob_type->tp_name); |
| 2123 | Py_DECREF(res); |
| 2124 | res = NULL; |
| 2125 | } |
| 2126 | return res; |
| 2127 | } |
| 2128 | } |
| 2129 | |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2130 | /* Return next item. |
| 2131 | * If an error occurs, return NULL. PyErr_Occurred() will be true. |
| 2132 | * If the iteration terminates normally, return NULL and clear the |
| 2133 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred() |
| 2134 | * will be false. |
| 2135 | * Else return the next object. PyErr_Occurred() will be false. |
| 2136 | */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2137 | PyObject * |
| 2138 | PyIter_Next(PyObject *iter) |
| 2139 | { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2140 | PyObject *result; |
Raymond Hettinger | 8049dde | 2003-03-01 01:44:32 +0000 | [diff] [blame] | 2141 | assert(PyIter_Check(iter)); |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2142 | result = (*iter->ob_type->tp_iternext)(iter); |
| 2143 | if (result == NULL && |
| 2144 | PyErr_Occurred() && |
| 2145 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2146 | PyErr_Clear(); |
| 2147 | return result; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2148 | } |