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