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