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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name); |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 30 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 31 | PyObject_Type(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 32 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | if (o == NULL) |
| 36 | return null_error(); |
| 37 | v = (PyObject *)o->ob_type; |
| 38 | Py_INCREF(v); |
| 39 | return v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 42 | Py_ssize_t |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 43 | PyObject_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 44 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | if (o == NULL) { |
| 48 | null_error(); |
| 49 | return -1; |
| 50 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | m = o->ob_type->tp_as_sequence; |
| 53 | if (m && m->sq_length) |
| 54 | return m->sq_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | return PyMapping_Size(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 59 | #undef PyObject_Length |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 60 | Py_ssize_t |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 61 | PyObject_Length(PyObject *o) |
| 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | return PyObject_Size(o); |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 64 | } |
| 65 | #define PyObject_Length PyObject_Size |
| 66 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 67 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 68 | /* The length hint function returns a non-negative value from o.__len__() |
| 69 | or o.__length_hint__(). If those methods aren't found or return a negative |
Raymond Hettinger | e836423 | 2009-02-02 22:55:09 +0000 | [diff] [blame] | 70 | value, then the defaultvalue is returned. If one of the calls fails, |
| 71 | this function returns -1. |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | Py_ssize_t |
| 75 | _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) |
| 76 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | static PyObject *hintstrobj = NULL; |
| 78 | PyObject *ro, *hintmeth; |
| 79 | Py_ssize_t rv; |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | /* try o.__len__() */ |
| 82 | rv = PyObject_Size(o); |
| 83 | if (rv >= 0) |
| 84 | return rv; |
| 85 | if (PyErr_Occurred()) { |
| 86 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 87 | return -1; |
| 88 | PyErr_Clear(); |
| 89 | } |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | /* try o.__length_hint__() */ |
| 92 | hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj); |
| 93 | if (hintmeth == NULL) { |
| 94 | if (PyErr_Occurred()) |
| 95 | return -1; |
| 96 | else |
| 97 | return defaultvalue; |
| 98 | } |
| 99 | ro = PyObject_CallFunctionObjArgs(hintmeth, NULL); |
| 100 | Py_DECREF(hintmeth); |
| 101 | if (ro == NULL) { |
| 102 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 103 | return -1; |
| 104 | PyErr_Clear(); |
| 105 | return defaultvalue; |
| 106 | } |
| 107 | rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue; |
| 108 | Py_DECREF(ro); |
| 109 | return rv; |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 112 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 113 | PyObject_GetItem(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | if (o == NULL || key == NULL) |
| 118 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | m = o->ob_type->tp_as_mapping; |
| 121 | if (m && m->mp_subscript) |
| 122 | return m->mp_subscript(o, key); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | if (o->ob_type->tp_as_sequence) { |
| 125 | if (PyIndex_Check(key)) { |
| 126 | Py_ssize_t key_value; |
| 127 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 128 | if (key_value == -1 && PyErr_Occurred()) |
| 129 | return NULL; |
| 130 | return PySequence_GetItem(o, key_value); |
| 131 | } |
| 132 | else if (o->ob_type->tp_as_sequence->sq_item) |
| 133 | return type_error("sequence index must " |
| 134 | "be integer, not '%.200s'", key); |
| 135 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | return type_error("'%.200s' object is not subscriptable", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 141 | PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | if (o == NULL || key == NULL || value == NULL) { |
| 146 | null_error(); |
| 147 | return -1; |
| 148 | } |
| 149 | m = o->ob_type->tp_as_mapping; |
| 150 | if (m && m->mp_ass_subscript) |
| 151 | return m->mp_ass_subscript(o, key, value); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | if (o->ob_type->tp_as_sequence) { |
| 154 | if (PyIndex_Check(key)) { |
| 155 | Py_ssize_t key_value; |
| 156 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 157 | if (key_value == -1 && PyErr_Occurred()) |
| 158 | return -1; |
| 159 | return PySequence_SetItem(o, key_value, value); |
| 160 | } |
| 161 | else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 162 | type_error("sequence index must be " |
| 163 | "integer, not '%.200s'", key); |
| 164 | return -1; |
| 165 | } |
| 166 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | type_error("'%.200s' object does not support item assignment", o); |
| 169 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 172 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 173 | PyObject_DelItem(PyObject *o, PyObject *key) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | PyMappingMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (o == NULL || key == NULL) { |
| 178 | null_error(); |
| 179 | return -1; |
| 180 | } |
| 181 | m = o->ob_type->tp_as_mapping; |
| 182 | if (m && m->mp_ass_subscript) |
| 183 | return m->mp_ass_subscript(o, key, (PyObject*)NULL); |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | if (o->ob_type->tp_as_sequence) { |
| 186 | if (PyIndex_Check(key)) { |
| 187 | Py_ssize_t key_value; |
| 188 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 189 | if (key_value == -1 && PyErr_Occurred()) |
| 190 | return -1; |
| 191 | return PySequence_DelItem(o, key_value); |
| 192 | } |
| 193 | else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 194 | type_error("sequence index must be " |
| 195 | "integer, not '%.200s'", key); |
| 196 | return -1; |
| 197 | } |
| 198 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | type_error("'%.200s' object does not support item deletion", o); |
| 201 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Martin v. Löwis | b0d71d0 | 2002-01-05 10:50:30 +0000 | [diff] [blame] | 204 | int |
| 205 | PyObject_DelItemString(PyObject *o, char *key) |
| 206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | PyObject *okey; |
| 208 | int ret; |
Martin v. Löwis | b0d71d0 | 2002-01-05 10:50:30 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (o == NULL || key == NULL) { |
| 211 | null_error(); |
| 212 | return -1; |
| 213 | } |
| 214 | okey = PyUnicode_FromString(key); |
| 215 | if (okey == NULL) |
| 216 | return -1; |
| 217 | ret = PyObject_DelItem(o, okey); |
| 218 | Py_DECREF(okey); |
| 219 | return ret; |
Martin v. Löwis | b0d71d0 | 2002-01-05 10:50:30 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 222 | /* We release the buffer right after use of this function which could |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 223 | cause issues later on. Don't use these functions in new code. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 224 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 225 | int |
| 226 | PyObject_AsCharBuffer(PyObject *obj, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | const char **buffer, |
| 228 | Py_ssize_t *buffer_len) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | PyBufferProcs *pb; |
| 231 | Py_buffer view; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 234 | null_error(); |
| 235 | return -1; |
| 236 | } |
| 237 | pb = obj->ob_type->tp_as_buffer; |
| 238 | if (pb == NULL || pb->bf_getbuffer == NULL) { |
| 239 | PyErr_SetString(PyExc_TypeError, |
| 240 | "expected an object with the buffer interface"); |
| 241 | return -1; |
| 242 | } |
| 243 | if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | *buffer = view.buf; |
| 246 | *buffer_len = view.len; |
| 247 | if (pb->bf_releasebuffer != NULL) |
| 248 | (*pb->bf_releasebuffer)(obj, &view); |
| 249 | Py_XDECREF(view.obj); |
| 250 | return 0; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 251 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 252 | |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 253 | int |
| 254 | PyObject_CheckReadBuffer(PyObject *obj) |
| 255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 257 | Py_buffer view; |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (pb == NULL || |
| 260 | pb->bf_getbuffer == NULL) |
| 261 | return 0; |
| 262 | if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) { |
| 263 | PyErr_Clear(); |
| 264 | return 0; |
| 265 | } |
| 266 | PyBuffer_Release(&view); |
| 267 | return 1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int PyObject_AsReadBuffer(PyObject *obj, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | const void **buffer, |
| 272 | Py_ssize_t *buffer_len) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | PyBufferProcs *pb; |
| 275 | Py_buffer view; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 278 | null_error(); |
| 279 | return -1; |
| 280 | } |
| 281 | pb = obj->ob_type->tp_as_buffer; |
| 282 | if (pb == NULL || |
| 283 | pb->bf_getbuffer == NULL) { |
| 284 | PyErr_SetString(PyExc_TypeError, |
| 285 | "expected an object with a buffer interface"); |
| 286 | return -1; |
| 287 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | *buffer = view.buf; |
| 292 | *buffer_len = view.len; |
| 293 | if (pb->bf_releasebuffer != NULL) |
| 294 | (*pb->bf_releasebuffer)(obj, &view); |
| 295 | Py_XDECREF(view.obj); |
| 296 | return 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | int PyObject_AsWriteBuffer(PyObject *obj, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | void **buffer, |
| 301 | Py_ssize_t *buffer_len) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | PyBufferProcs *pb; |
| 304 | Py_buffer view; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 307 | null_error(); |
| 308 | return -1; |
| 309 | } |
| 310 | pb = obj->ob_type->tp_as_buffer; |
| 311 | if (pb == NULL || |
| 312 | pb->bf_getbuffer == NULL || |
| 313 | ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) { |
| 314 | PyErr_SetString(PyExc_TypeError, |
| 315 | "expected an object with a writable buffer interface"); |
| 316 | return -1; |
| 317 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | *buffer = view.buf; |
| 320 | *buffer_len = view.len; |
| 321 | if (pb->bf_releasebuffer != NULL) |
| 322 | (*pb->bf_releasebuffer)(obj, &view); |
| 323 | Py_XDECREF(view.obj); |
| 324 | return 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 327 | /* Buffer C-API for Python 3.0 */ |
| 328 | |
| 329 | int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 330 | PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | if (!PyObject_CheckBuffer(obj)) { |
| 333 | PyErr_Format(PyExc_TypeError, |
| 334 | "'%100s' does not support the buffer interface", |
| 335 | Py_TYPE(obj)->tp_name); |
| 336 | return -1; |
| 337 | } |
| 338 | return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 341 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 342 | _IsFortranContiguous(Py_buffer *view) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | Py_ssize_t sd, dim; |
| 345 | int i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | if (view->ndim == 0) return 1; |
| 348 | if (view->strides == NULL) return (view->ndim == 1); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | sd = view->itemsize; |
| 351 | if (view->ndim == 1) return (view->shape[0] == 1 || |
| 352 | sd == view->strides[0]); |
| 353 | for (i=0; i<view->ndim; i++) { |
| 354 | dim = view->shape[i]; |
| 355 | if (dim == 0) return 1; |
| 356 | if (view->strides[i] != sd) return 0; |
| 357 | sd *= dim; |
| 358 | } |
| 359 | return 1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 363 | _IsCContiguous(Py_buffer *view) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | Py_ssize_t sd, dim; |
| 366 | int i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (view->ndim == 0) return 1; |
| 369 | if (view->strides == NULL) return 1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | sd = view->itemsize; |
| 372 | if (view->ndim == 1) return (view->shape[0] == 1 || |
| 373 | sd == view->strides[0]); |
| 374 | for (i=view->ndim-1; i>=0; i--) { |
| 375 | dim = view->shape[i]; |
| 376 | if (dim == 0) return 1; |
| 377 | if (view->strides[i] != sd) return 0; |
| 378 | sd *= dim; |
| 379 | } |
| 380 | return 1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 384 | PyBuffer_IsContiguous(Py_buffer *view, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 385 | { |
| 386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | if (view->suboffsets != NULL) return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (fort == 'C') |
| 390 | return _IsCContiguous(view); |
| 391 | else if (fort == 'F') |
| 392 | return _IsFortranContiguous(view); |
| 393 | else if (fort == 'A') |
| 394 | return (_IsCContiguous(view) || _IsFortranContiguous(view)); |
| 395 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 399 | void* |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 400 | PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | char* pointer; |
| 403 | int i; |
| 404 | pointer = (char *)view->buf; |
| 405 | for (i = 0; i < view->ndim; i++) { |
| 406 | pointer += view->strides[i]*indices[i]; |
| 407 | if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) { |
| 408 | pointer = *((char**)pointer) + view->suboffsets[i]; |
| 409 | } |
| 410 | } |
| 411 | return (void*)pointer; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 415 | void |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 416 | _Py_add_one_to_index_F(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | int k; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | for (k=0; k<nd; k++) { |
| 421 | if (index[k] < shape[k]-1) { |
| 422 | index[k]++; |
| 423 | break; |
| 424 | } |
| 425 | else { |
| 426 | index[k] = 0; |
| 427 | } |
| 428 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 431 | void |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 432 | _Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | int k; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | for (k=nd-1; k>=0; k--) { |
| 437 | if (index[k] < shape[k]-1) { |
| 438 | index[k]++; |
| 439 | break; |
| 440 | } |
| 441 | else { |
| 442 | index[k] = 0; |
| 443 | } |
| 444 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* view is not checked for consistency in either of these. It is |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 448 | assumed that the size of the buffer is view->len in |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 449 | view->len / view->itemsize elements. |
| 450 | */ |
| 451 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 452 | int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 453 | PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | int k; |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 456 | void (*addone)(int, Py_ssize_t *, const Py_ssize_t *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | Py_ssize_t *indices, elements; |
| 458 | char *dest, *ptr; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | if (len > view->len) { |
| 461 | len = view->len; |
| 462 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | if (PyBuffer_IsContiguous(view, fort)) { |
| 465 | /* simplest copy is all that is needed */ |
| 466 | memcpy(buf, view->buf, len); |
| 467 | return 0; |
| 468 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | /* Otherwise a more elaborate scheme is needed */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | /* XXX(nnorwitz): need to check for overflow! */ |
| 473 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim)); |
| 474 | if (indices == NULL) { |
| 475 | PyErr_NoMemory(); |
| 476 | return -1; |
| 477 | } |
| 478 | for (k=0; k<view->ndim;k++) { |
| 479 | indices[k] = 0; |
| 480 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | if (fort == 'F') { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 483 | addone = _Py_add_one_to_index_F; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | } |
| 485 | else { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 486 | addone = _Py_add_one_to_index_C; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | } |
| 488 | dest = buf; |
| 489 | /* XXX : This is not going to be the fastest code in the world |
| 490 | several optimizations are possible. |
| 491 | */ |
| 492 | elements = len / view->itemsize; |
| 493 | while (elements--) { |
| 494 | addone(view->ndim, indices, view->shape); |
| 495 | ptr = PyBuffer_GetPointer(view, indices); |
| 496 | memcpy(dest, ptr, view->itemsize); |
| 497 | dest += view->itemsize; |
| 498 | } |
| 499 | PyMem_Free(indices); |
| 500 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 504 | PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | int k; |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 507 | void (*addone)(int, Py_ssize_t *, const Py_ssize_t *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | Py_ssize_t *indices, elements; |
| 509 | char *src, *ptr; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | if (len > view->len) { |
| 512 | len = view->len; |
| 513 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | if (PyBuffer_IsContiguous(view, fort)) { |
| 516 | /* simplest copy is all that is needed */ |
| 517 | memcpy(view->buf, buf, len); |
| 518 | return 0; |
| 519 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | /* Otherwise a more elaborate scheme is needed */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | /* XXX(nnorwitz): need to check for overflow! */ |
| 524 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim)); |
| 525 | if (indices == NULL) { |
| 526 | PyErr_NoMemory(); |
| 527 | return -1; |
| 528 | } |
| 529 | for (k=0; k<view->ndim;k++) { |
| 530 | indices[k] = 0; |
| 531 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | if (fort == 'F') { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 534 | addone = _Py_add_one_to_index_F; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
| 536 | else { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 537 | addone = _Py_add_one_to_index_C; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | } |
| 539 | src = buf; |
| 540 | /* XXX : This is not going to be the fastest code in the world |
| 541 | several optimizations are possible. |
| 542 | */ |
| 543 | elements = len / view->itemsize; |
| 544 | while (elements--) { |
| 545 | addone(view->ndim, indices, view->shape); |
| 546 | ptr = PyBuffer_GetPointer(view, indices); |
| 547 | memcpy(ptr, src, view->itemsize); |
| 548 | src += view->itemsize; |
| 549 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | PyMem_Free(indices); |
| 552 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 555 | int PyObject_CopyData(PyObject *dest, PyObject *src) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | Py_buffer view_dest, view_src; |
| 558 | int k; |
| 559 | Py_ssize_t *indices, elements; |
| 560 | char *dptr, *sptr; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | if (!PyObject_CheckBuffer(dest) || |
| 563 | !PyObject_CheckBuffer(src)) { |
| 564 | PyErr_SetString(PyExc_TypeError, |
| 565 | "both destination and source must have the "\ |
| 566 | "buffer interface"); |
| 567 | return -1; |
| 568 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1; |
| 571 | if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) { |
| 572 | PyBuffer_Release(&view_dest); |
| 573 | return -1; |
| 574 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | if (view_dest.len < view_src.len) { |
| 577 | PyErr_SetString(PyExc_BufferError, |
| 578 | "destination is too small to receive data from source"); |
| 579 | PyBuffer_Release(&view_dest); |
| 580 | PyBuffer_Release(&view_src); |
| 581 | return -1; |
| 582 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | if ((PyBuffer_IsContiguous(&view_dest, 'C') && |
| 585 | PyBuffer_IsContiguous(&view_src, 'C')) || |
| 586 | (PyBuffer_IsContiguous(&view_dest, 'F') && |
| 587 | PyBuffer_IsContiguous(&view_src, 'F'))) { |
| 588 | /* simplest copy is all that is needed */ |
| 589 | memcpy(view_dest.buf, view_src.buf, view_src.len); |
| 590 | PyBuffer_Release(&view_dest); |
| 591 | PyBuffer_Release(&view_src); |
| 592 | return 0; |
| 593 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | /* Otherwise a more elaborate copy scheme is needed */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | /* XXX(nnorwitz): need to check for overflow! */ |
| 598 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim); |
| 599 | if (indices == NULL) { |
| 600 | PyErr_NoMemory(); |
| 601 | PyBuffer_Release(&view_dest); |
| 602 | PyBuffer_Release(&view_src); |
| 603 | return -1; |
| 604 | } |
| 605 | for (k=0; k<view_src.ndim;k++) { |
| 606 | indices[k] = 0; |
| 607 | } |
| 608 | elements = 1; |
| 609 | for (k=0; k<view_src.ndim; k++) { |
| 610 | /* XXX(nnorwitz): can this overflow? */ |
| 611 | elements *= view_src.shape[k]; |
| 612 | } |
| 613 | while (elements--) { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 614 | _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | dptr = PyBuffer_GetPointer(&view_dest, indices); |
| 616 | sptr = PyBuffer_GetPointer(&view_src, indices); |
| 617 | memcpy(dptr, sptr, view_src.itemsize); |
| 618 | } |
| 619 | PyMem_Free(indices); |
| 620 | PyBuffer_Release(&view_dest); |
| 621 | PyBuffer_Release(&view_src); |
| 622 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void |
| 626 | PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | Py_ssize_t *strides, int itemsize, |
| 628 | char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | int k; |
| 631 | Py_ssize_t sd; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | sd = itemsize; |
| 634 | if (fort == 'F') { |
| 635 | for (k=0; k<nd; k++) { |
| 636 | strides[k] = sd; |
| 637 | sd *= shape[k]; |
| 638 | } |
| 639 | } |
| 640 | else { |
| 641 | for (k=nd-1; k>=0; k--) { |
| 642 | strides[k] = sd; |
| 643 | sd *= shape[k]; |
| 644 | } |
| 645 | } |
| 646 | return; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | int |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 650 | PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | int readonly, int flags) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | if (view == NULL) return 0; |
| 654 | if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) && |
| 655 | (readonly == 1)) { |
| 656 | PyErr_SetString(PyExc_BufferError, |
| 657 | "Object is not writable."); |
| 658 | return -1; |
| 659 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | view->obj = obj; |
| 662 | if (obj) |
| 663 | Py_INCREF(obj); |
| 664 | view->buf = buf; |
| 665 | view->len = len; |
| 666 | view->readonly = readonly; |
| 667 | view->itemsize = 1; |
| 668 | view->format = NULL; |
| 669 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 670 | view->format = "B"; |
| 671 | view->ndim = 1; |
| 672 | view->shape = NULL; |
| 673 | if ((flags & PyBUF_ND) == PyBUF_ND) |
| 674 | view->shape = &(view->len); |
| 675 | view->strides = NULL; |
| 676 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) |
| 677 | view->strides = &(view->itemsize); |
| 678 | view->suboffsets = NULL; |
| 679 | view->internal = NULL; |
| 680 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 683 | void |
| 684 | PyBuffer_Release(Py_buffer *view) |
| 685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | PyObject *obj = view->obj; |
| 687 | if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer) |
| 688 | Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view); |
| 689 | Py_XDECREF(obj); |
| 690 | view->obj = NULL; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 693 | PyObject * |
| 694 | PyObject_Format(PyObject *obj, PyObject *format_spec) |
| 695 | { |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 696 | PyObject *meth; |
| 697 | PyObject *empty = NULL; |
| 698 | PyObject *result = NULL; |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 699 | static PyObject *format_cache = NULL; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 700 | |
| 701 | /* If no format_spec is provided, use an empty string */ |
| 702 | if (format_spec == NULL) { |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 703 | empty = PyUnicode_FromUnicode(NULL, 0); |
| 704 | format_spec = empty; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 707 | /* Find the (unbound!) __format__ method (a borrowed reference) */ |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 708 | meth = _PyObject_LookupSpecial(obj, "__format__", &format_cache); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 709 | if (meth == NULL) { |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 710 | if (!PyErr_Occurred()) |
| 711 | PyErr_Format(PyExc_TypeError, |
| 712 | "Type %.100s doesn't define __format__", |
| 713 | Py_TYPE(obj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | goto done; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 717 | /* And call it. */ |
| 718 | result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL); |
Benjamin Peterson | 6f889ad3 | 2010-06-05 02:11:45 +0000 | [diff] [blame] | 719 | Py_DECREF(meth); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 720 | |
| 721 | if (result && !PyUnicode_Check(result)) { |
Benjamin Peterson | da2cf04 | 2010-06-05 00:45:37 +0000 | [diff] [blame] | 722 | PyErr_SetString(PyExc_TypeError, |
| 723 | "__format__ method did not return string"); |
| 724 | Py_DECREF(result); |
| 725 | result = NULL; |
| 726 | goto done; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | done: |
| 730 | Py_XDECREF(empty); |
| 731 | return result; |
| 732 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 733 | /* Operations on numbers */ |
| 734 | |
| 735 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 736 | PyNumber_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | return o && o->ob_type->tp_as_number && |
| 739 | (o->ob_type->tp_as_number->nb_int || |
| 740 | o->ob_type->tp_as_number->nb_float); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 743 | /* Binary operators */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 744 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 745 | #define NB_SLOT(x) offsetof(PyNumberMethods, x) |
| 746 | #define NB_BINOP(nb_methods, slot) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | (*(binaryfunc*)(& ((char*)nb_methods)[slot])) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 748 | #define NB_TERNOP(nb_methods, slot) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | (*(ternaryfunc*)(& ((char*)nb_methods)[slot])) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | Calling scheme used for binary operations: |
| 753 | |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 754 | Order operations are tried until either a valid result or error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | w.op(v,w)[*], v.op(v,w), w.op(v,w) |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 756 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 757 | [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of |
| 758 | v->ob_type |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 759 | */ |
| 760 | |
| 761 | static PyObject * |
| 762 | binary_op1(PyObject *v, PyObject *w, const int op_slot) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | PyObject *x; |
| 765 | binaryfunc slotv = NULL; |
| 766 | binaryfunc slotw = NULL; |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | if (v->ob_type->tp_as_number != NULL) |
| 769 | slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot); |
| 770 | if (w->ob_type != v->ob_type && |
| 771 | w->ob_type->tp_as_number != NULL) { |
| 772 | slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot); |
| 773 | if (slotw == slotv) |
| 774 | slotw = NULL; |
| 775 | } |
| 776 | if (slotv) { |
| 777 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 778 | x = slotw(v, w); |
| 779 | if (x != Py_NotImplemented) |
| 780 | return x; |
| 781 | Py_DECREF(x); /* can't do it */ |
| 782 | slotw = NULL; |
| 783 | } |
| 784 | x = slotv(v, w); |
| 785 | if (x != Py_NotImplemented) |
| 786 | return x; |
| 787 | Py_DECREF(x); /* can't do it */ |
| 788 | } |
| 789 | if (slotw) { |
| 790 | x = slotw(v, w); |
| 791 | if (x != Py_NotImplemented) |
| 792 | return x; |
| 793 | Py_DECREF(x); /* can't do it */ |
| 794 | } |
| 795 | Py_INCREF(Py_NotImplemented); |
| 796 | return Py_NotImplemented; |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 797 | } |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 798 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 799 | static PyObject * |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 800 | binop_type_error(PyObject *v, PyObject *w, const char *op_name) |
| 801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | PyErr_Format(PyExc_TypeError, |
| 803 | "unsupported operand type(s) for %.100s: " |
| 804 | "'%.100s' and '%.100s'", |
| 805 | op_name, |
| 806 | v->ob_type->tp_name, |
| 807 | w->ob_type->tp_name); |
| 808 | return NULL; |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static PyObject * |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 812 | binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) |
| 813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyObject *result = binary_op1(v, w, op_slot); |
| 815 | if (result == Py_NotImplemented) { |
| 816 | Py_DECREF(result); |
| 817 | return binop_type_error(v, w, op_name); |
| 818 | } |
| 819 | return result; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | Calling scheme used for ternary operations: |
| 825 | |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 826 | Order operations are tried until either a valid result or error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | 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] | 828 | */ |
| 829 | |
| 830 | static PyObject * |
| 831 | ternary_op(PyObject *v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | PyObject *w, |
| 833 | PyObject *z, |
| 834 | const int op_slot, |
| 835 | const char *op_name) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | PyNumberMethods *mv, *mw, *mz; |
| 838 | PyObject *x = NULL; |
| 839 | ternaryfunc slotv = NULL; |
| 840 | ternaryfunc slotw = NULL; |
| 841 | ternaryfunc slotz = NULL; |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | mv = v->ob_type->tp_as_number; |
| 844 | mw = w->ob_type->tp_as_number; |
| 845 | if (mv != NULL) |
| 846 | slotv = NB_TERNOP(mv, op_slot); |
| 847 | if (w->ob_type != v->ob_type && |
| 848 | mw != NULL) { |
| 849 | slotw = NB_TERNOP(mw, op_slot); |
| 850 | if (slotw == slotv) |
| 851 | slotw = NULL; |
| 852 | } |
| 853 | if (slotv) { |
| 854 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 855 | x = slotw(v, w, z); |
| 856 | if (x != Py_NotImplemented) |
| 857 | return x; |
| 858 | Py_DECREF(x); /* can't do it */ |
| 859 | slotw = NULL; |
| 860 | } |
| 861 | x = slotv(v, w, z); |
| 862 | if (x != Py_NotImplemented) |
| 863 | return x; |
| 864 | Py_DECREF(x); /* can't do it */ |
| 865 | } |
| 866 | if (slotw) { |
| 867 | x = slotw(v, w, z); |
| 868 | if (x != Py_NotImplemented) |
| 869 | return x; |
| 870 | Py_DECREF(x); /* can't do it */ |
| 871 | } |
| 872 | mz = z->ob_type->tp_as_number; |
| 873 | if (mz != NULL) { |
| 874 | slotz = NB_TERNOP(mz, op_slot); |
| 875 | if (slotz == slotv || slotz == slotw) |
| 876 | slotz = NULL; |
| 877 | if (slotz) { |
| 878 | x = slotz(v, w, z); |
| 879 | if (x != Py_NotImplemented) |
| 880 | return x; |
| 881 | Py_DECREF(x); /* can't do it */ |
| 882 | } |
| 883 | } |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | if (z == Py_None) |
| 886 | PyErr_Format( |
| 887 | PyExc_TypeError, |
| 888 | "unsupported operand type(s) for ** or pow(): " |
| 889 | "'%.100s' and '%.100s'", |
| 890 | v->ob_type->tp_name, |
| 891 | w->ob_type->tp_name); |
| 892 | else |
| 893 | PyErr_Format( |
| 894 | PyExc_TypeError, |
| 895 | "unsupported operand type(s) for pow(): " |
| 896 | "'%.100s', '%.100s', '%.100s'", |
| 897 | v->ob_type->tp_name, |
| 898 | w->ob_type->tp_name, |
| 899 | z->ob_type->tp_name); |
| 900 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 903 | #define BINARY_FUNC(func, op, op_name) \ |
| 904 | PyObject * \ |
| 905 | func(PyObject *v, PyObject *w) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | return binary_op(v, w, NB_SLOT(op), op_name); \ |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 907 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 908 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 909 | BINARY_FUNC(PyNumber_Or, nb_or, "|") |
| 910 | BINARY_FUNC(PyNumber_Xor, nb_xor, "^") |
| 911 | BINARY_FUNC(PyNumber_And, nb_and, "&") |
| 912 | BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<") |
| 913 | BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>") |
| 914 | BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-") |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 915 | BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()") |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 916 | |
| 917 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 918 | PyNumber_Add(PyObject *v, PyObject *w) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_add)); |
| 921 | if (result == Py_NotImplemented) { |
| 922 | PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 923 | Py_DECREF(result); |
| 924 | if (m && m->sq_concat) { |
| 925 | return (*m->sq_concat)(v, w); |
| 926 | } |
| 927 | result = binop_type_error(v, w, "+"); |
| 928 | } |
| 929 | return result; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 932 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 933 | sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n) |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | Py_ssize_t count; |
| 936 | if (PyIndex_Check(n)) { |
| 937 | count = PyNumber_AsSsize_t(n, PyExc_OverflowError); |
| 938 | if (count == -1 && PyErr_Occurred()) |
| 939 | return NULL; |
| 940 | } |
| 941 | else { |
| 942 | return type_error("can't multiply sequence by " |
| 943 | "non-int of type '%.200s'", n); |
| 944 | } |
| 945 | return (*repeatfunc)(seq, count); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | PyObject * |
| 949 | PyNumber_Multiply(PyObject *v, PyObject *w) |
| 950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply)); |
| 952 | if (result == Py_NotImplemented) { |
| 953 | PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 954 | PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 955 | Py_DECREF(result); |
| 956 | if (mv && mv->sq_repeat) { |
| 957 | return sequence_repeat(mv->sq_repeat, v, w); |
| 958 | } |
| 959 | else if (mw && mw->sq_repeat) { |
| 960 | return sequence_repeat(mw->sq_repeat, w, v); |
| 961 | } |
| 962 | result = binop_type_error(v, w, "*"); |
| 963 | } |
| 964 | return result; |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 967 | PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 968 | PyNumber_FloorDivide(PyObject *v, PyObject *w) |
| 969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | return binary_op(v, w, NB_SLOT(nb_floor_divide), "//"); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | PyObject * |
| 974 | PyNumber_TrueDivide(PyObject *v, PyObject *w) |
| 975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | return binary_op(v, w, NB_SLOT(nb_true_divide), "/"); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 980 | PyNumber_Remainder(PyObject *v, PyObject *w) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | return binary_op(v, w, NB_SLOT(nb_remainder), "%"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 986 | PyNumber_Power(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | 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] | 989 | } |
| 990 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 991 | /* Binary in-place operators */ |
| 992 | |
| 993 | /* The in-place operators are defined to fall back to the 'normal', |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 994 | 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] | 995 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 996 | - If the left hand object has the appropriate struct members, and |
| 997 | they are filled, call the appropriate function and return the |
| 998 | result. No coercion is done on the arguments; the left-hand object |
| 999 | is the one the operation is performed on, and it's up to the |
| 1000 | function to deal with the right-hand object. |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 1001 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1002 | - Otherwise, in-place modification is not supported. Handle it exactly as |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1003 | a non in-place operation of the same kind. |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1004 | |
| 1005 | */ |
| 1006 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1007 | static PyObject * |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 1008 | 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] | 1009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 1011 | if (mv != NULL) { |
| 1012 | binaryfunc slot = NB_BINOP(mv, iop_slot); |
| 1013 | if (slot) { |
| 1014 | PyObject *x = (slot)(v, w); |
| 1015 | if (x != Py_NotImplemented) { |
| 1016 | return x; |
| 1017 | } |
| 1018 | Py_DECREF(x); |
| 1019 | } |
| 1020 | } |
| 1021 | return binary_op1(v, w, op_slot); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static PyObject * |
| 1025 | binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | const char *op_name) |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 1027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | PyObject *result = binary_iop1(v, w, iop_slot, op_slot); |
| 1029 | if (result == Py_NotImplemented) { |
| 1030 | Py_DECREF(result); |
| 1031 | return binop_type_error(v, w, op_name); |
| 1032 | } |
| 1033 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1036 | #define INPLACE_BINOP(func, iop, op, op_name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | PyObject * \ |
| 1038 | func(PyObject *v, PyObject *w) { \ |
| 1039 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ |
| 1040 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1041 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1042 | INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=") |
| 1043 | INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=") |
| 1044 | INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=") |
| 1045 | INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=") |
| 1046 | INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=") |
| 1047 | INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=") |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1048 | |
| 1049 | PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1050 | PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w) |
| 1051 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide), |
| 1053 | NB_SLOT(nb_floor_divide), "//="); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | PyObject * |
| 1057 | PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w) |
| 1058 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide), |
| 1060 | NB_SLOT(nb_true_divide), "/="); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1064 | PyNumber_InPlaceAdd(PyObject *v, PyObject *w) |
| 1065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add), |
| 1067 | NB_SLOT(nb_add)); |
| 1068 | if (result == Py_NotImplemented) { |
| 1069 | PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 1070 | Py_DECREF(result); |
| 1071 | if (m != NULL) { |
| 1072 | binaryfunc f = NULL; |
| 1073 | f = m->sq_inplace_concat; |
| 1074 | if (f == NULL) |
| 1075 | f = m->sq_concat; |
| 1076 | if (f != NULL) |
| 1077 | return (*f)(v, w); |
| 1078 | } |
| 1079 | result = binop_type_error(v, w, "+="); |
| 1080 | } |
| 1081 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | PyObject * |
| 1085 | PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) |
| 1086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply), |
| 1088 | NB_SLOT(nb_multiply)); |
| 1089 | if (result == Py_NotImplemented) { |
| 1090 | ssizeargfunc f = NULL; |
| 1091 | PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 1092 | PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 1093 | Py_DECREF(result); |
| 1094 | if (mv != NULL) { |
| 1095 | f = mv->sq_inplace_repeat; |
| 1096 | if (f == NULL) |
| 1097 | f = mv->sq_repeat; |
| 1098 | if (f != NULL) |
| 1099 | return sequence_repeat(f, v, w); |
| 1100 | } |
| 1101 | else if (mw != NULL) { |
| 1102 | /* Note that the right hand operand should not be |
| 1103 | * mutated in this case so sq_inplace_repeat is not |
| 1104 | * used. */ |
| 1105 | if (mw->sq_repeat) |
| 1106 | return sequence_repeat(mw->sq_repeat, w, v); |
| 1107 | } |
| 1108 | result = binop_type_error(v, w, "*="); |
| 1109 | } |
| 1110 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1113 | PyObject * |
| 1114 | PyNumber_InPlaceRemainder(PyObject *v, PyObject *w) |
| 1115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | return binary_iop(v, w, NB_SLOT(nb_inplace_remainder), |
| 1117 | NB_SLOT(nb_remainder), "%="); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | PyObject * |
| 1121 | PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z) |
| 1122 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (v->ob_type->tp_as_number && |
| 1124 | v->ob_type->tp_as_number->nb_inplace_power != NULL) { |
| 1125 | return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**="); |
| 1126 | } |
| 1127 | else { |
| 1128 | return ternary_op(v, w, z, NB_SLOT(nb_power), "**="); |
| 1129 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1133 | /* Unary operators and functions */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1134 | |
| 1135 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1136 | PyNumber_Negative(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | PyNumberMethods *m; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | if (o == NULL) |
| 1141 | return null_error(); |
| 1142 | m = o->ob_type->tp_as_number; |
| 1143 | if (m && m->nb_negative) |
| 1144 | return (*m->nb_negative)(o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | return type_error("bad operand type for unary -: '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1150 | PyNumber_Positive(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | PyNumberMethods *m; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | if (o == NULL) |
| 1155 | return null_error(); |
| 1156 | m = o->ob_type->tp_as_number; |
| 1157 | if (m && m->nb_positive) |
| 1158 | return (*m->nb_positive)(o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | return type_error("bad operand type for unary +: '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1164 | PyNumber_Invert(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | PyNumberMethods *m; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | if (o == NULL) |
| 1169 | return null_error(); |
| 1170 | m = o->ob_type->tp_as_number; |
| 1171 | if (m && m->nb_invert) |
| 1172 | return (*m->nb_invert)(o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | return type_error("bad operand type for unary ~: '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1178 | PyNumber_Absolute(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | if (o == NULL) |
| 1183 | return null_error(); |
| 1184 | m = o->ob_type->tp_as_number; |
| 1185 | if (m && m->nb_absolute) |
| 1186 | return m->nb_absolute(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | return type_error("bad operand type for abs(): '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1191 | /* Return a Python Int or Long from the object item |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1192 | Raise TypeError if the result is not an int-or-long |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1193 | or if the object cannot be interpreted as an index. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1194 | */ |
| 1195 | PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1196 | PyNumber_Index(PyObject *item) |
| 1197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | PyObject *result = NULL; |
| 1199 | if (item == NULL) |
| 1200 | return null_error(); |
| 1201 | if (PyLong_Check(item)) { |
| 1202 | Py_INCREF(item); |
| 1203 | return item; |
| 1204 | } |
| 1205 | if (PyIndex_Check(item)) { |
| 1206 | result = item->ob_type->tp_as_number->nb_index(item); |
| 1207 | if (result && !PyLong_Check(result)) { |
| 1208 | PyErr_Format(PyExc_TypeError, |
| 1209 | "__index__ returned non-int " |
| 1210 | "(type %.200s)", |
| 1211 | result->ob_type->tp_name); |
| 1212 | Py_DECREF(result); |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | } |
| 1216 | else { |
| 1217 | PyErr_Format(PyExc_TypeError, |
| 1218 | "'%.200s' object cannot be interpreted " |
| 1219 | "as an integer", item->ob_type->tp_name); |
| 1220 | } |
| 1221 | return result; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1224 | /* Return an error on Overflow only if err is not NULL*/ |
| 1225 | |
| 1226 | Py_ssize_t |
| 1227 | PyNumber_AsSsize_t(PyObject *item, PyObject *err) |
| 1228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | Py_ssize_t result; |
| 1230 | PyObject *runerr; |
| 1231 | PyObject *value = PyNumber_Index(item); |
| 1232 | if (value == NULL) |
| 1233 | return -1; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
| 1236 | result = PyLong_AsSsize_t(value); |
| 1237 | if (result != -1 || !(runerr = PyErr_Occurred())) |
| 1238 | goto finish; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | /* Error handling code -- only manage OverflowError differently */ |
| 1241 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 1242 | goto finish; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | PyErr_Clear(); |
| 1245 | /* If no error-handling desired then the default clipping |
| 1246 | is sufficient. |
| 1247 | */ |
| 1248 | if (!err) { |
| 1249 | assert(PyLong_Check(value)); |
| 1250 | /* Whether or not it is less than or equal to |
| 1251 | zero is determined by the sign of ob_size |
| 1252 | */ |
| 1253 | if (_PyLong_Sign(value) < 0) |
| 1254 | result = PY_SSIZE_T_MIN; |
| 1255 | else |
| 1256 | result = PY_SSIZE_T_MAX; |
| 1257 | } |
| 1258 | else { |
| 1259 | /* Otherwise replace the error with caller's error object. */ |
| 1260 | PyErr_Format(err, |
| 1261 | "cannot fit '%.200s' into an index-sized integer", |
| 1262 | item->ob_type->tp_name); |
| 1263 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1264 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1265 | finish: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | Py_DECREF(value); |
| 1267 | return result; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1271 | PyObject * |
| 1272 | _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format) |
| 1273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | static PyObject *int_name = NULL; |
| 1275 | if (int_name == NULL) { |
| 1276 | int_name = PyUnicode_InternFromString("__int__"); |
| 1277 | if (int_name == NULL) |
| 1278 | return NULL; |
| 1279 | } |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | if (integral && !PyLong_Check(integral)) { |
| 1282 | /* Don't go through tp_as_number->nb_int to avoid |
| 1283 | hitting the classic class fallback to __trunc__. */ |
| 1284 | PyObject *int_func = PyObject_GetAttr(integral, int_name); |
| 1285 | if (int_func == NULL) { |
| 1286 | PyErr_Clear(); /* Raise a different error. */ |
| 1287 | goto non_integral_error; |
| 1288 | } |
| 1289 | Py_DECREF(integral); |
| 1290 | integral = PyEval_CallObject(int_func, NULL); |
| 1291 | Py_DECREF(int_func); |
| 1292 | if (integral && !PyLong_Check(integral)) { |
| 1293 | goto non_integral_error; |
| 1294 | } |
| 1295 | } |
| 1296 | return integral; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1297 | |
| 1298 | non_integral_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name); |
| 1300 | Py_DECREF(integral); |
| 1301 | return NULL; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1305 | /* Add a check for embedded NULL-bytes in the argument. */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1306 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1307 | long_from_string(const char *s, Py_ssize_t len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | char *end; |
| 1310 | PyObject *x; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | x = PyLong_FromString((char*)s, &end, 10); |
| 1313 | if (x == NULL) |
| 1314 | return NULL; |
| 1315 | if (end != s + len) { |
| 1316 | PyErr_SetString(PyExc_ValueError, |
| 1317 | "null byte in argument for int()"); |
| 1318 | Py_DECREF(x); |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | return x; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1324 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1325 | PyNumber_Long(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | PyNumberMethods *m; |
| 1328 | static PyObject *trunc_name = NULL; |
| 1329 | PyObject *trunc_func; |
| 1330 | const char *buffer; |
| 1331 | Py_ssize_t buffer_len; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | if (trunc_name == NULL) { |
| 1334 | trunc_name = PyUnicode_InternFromString("__trunc__"); |
| 1335 | if (trunc_name == NULL) |
| 1336 | return NULL; |
| 1337 | } |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | if (o == NULL) |
| 1340 | return null_error(); |
| 1341 | if (PyLong_CheckExact(o)) { |
| 1342 | Py_INCREF(o); |
| 1343 | return o; |
| 1344 | } |
| 1345 | m = o->ob_type->tp_as_number; |
| 1346 | if (m && m->nb_int) { /* This should include subclasses of int */ |
| 1347 | PyObject *res = m->nb_int(o); |
| 1348 | if (res && !PyLong_Check(res)) { |
| 1349 | PyErr_Format(PyExc_TypeError, |
| 1350 | "__int__ returned non-int (type %.200s)", |
| 1351 | res->ob_type->tp_name); |
| 1352 | Py_DECREF(res); |
| 1353 | return NULL; |
| 1354 | } |
| 1355 | return res; |
| 1356 | } |
| 1357 | if (PyLong_Check(o)) /* An int subclass without nb_int */ |
| 1358 | return _PyLong_Copy((PyLongObject *)o); |
| 1359 | trunc_func = PyObject_GetAttr(o, trunc_name); |
| 1360 | if (trunc_func) { |
| 1361 | PyObject *truncated = PyEval_CallObject(trunc_func, NULL); |
| 1362 | PyObject *int_instance; |
| 1363 | Py_DECREF(trunc_func); |
| 1364 | /* __trunc__ is specified to return an Integral type, |
| 1365 | but long() needs to return a long. */ |
| 1366 | int_instance = _PyNumber_ConvertIntegralToInt( |
| 1367 | truncated, |
| 1368 | "__trunc__ returned non-Integral (type %.200s)"); |
| 1369 | return int_instance; |
| 1370 | } |
| 1371 | PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */ |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | if (PyBytes_Check(o)) |
| 1374 | /* need to do extra error checking that PyLong_FromString() |
| 1375 | * doesn't do. In particular long('9.5') must raise an |
| 1376 | * exception, not truncate the float. |
| 1377 | */ |
| 1378 | return long_from_string(PyBytes_AS_STRING(o), |
| 1379 | PyBytes_GET_SIZE(o)); |
| 1380 | if (PyUnicode_Check(o)) |
| 1381 | /* The above check is done in PyLong_FromUnicode(). */ |
| 1382 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 1383 | PyUnicode_GET_SIZE(o), |
| 1384 | 10); |
| 1385 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
| 1386 | return long_from_string(buffer, buffer_len); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | return type_error("int() argument must be a string or a " |
| 1389 | "number, not '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1393 | PyNumber_Float(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | if (o == NULL) |
| 1398 | return null_error(); |
| 1399 | m = o->ob_type->tp_as_number; |
| 1400 | if (m && m->nb_float) { /* This should include subclasses of float */ |
| 1401 | PyObject *res = m->nb_float(o); |
| 1402 | if (res && !PyFloat_Check(res)) { |
| 1403 | PyErr_Format(PyExc_TypeError, |
| 1404 | "__float__ returned non-float (type %.200s)", |
| 1405 | res->ob_type->tp_name); |
| 1406 | Py_DECREF(res); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | return res; |
| 1410 | } |
| 1411 | if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */ |
| 1412 | PyFloatObject *po = (PyFloatObject *)o; |
| 1413 | return PyFloat_FromDouble(po->ob_fval); |
| 1414 | } |
| 1415 | return PyFloat_FromString(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1418 | |
| 1419 | PyObject * |
| 1420 | PyNumber_ToBase(PyObject *n, int base) |
| 1421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | PyObject *res = NULL; |
| 1423 | PyObject *index = PyNumber_Index(n); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | if (!index) |
| 1426 | return NULL; |
| 1427 | if (PyLong_Check(index)) |
| 1428 | res = _PyLong_Format(index, base); |
| 1429 | else |
| 1430 | /* It should not be possible to get here, as |
| 1431 | PyNumber_Index already has a check for the same |
| 1432 | condition */ |
| 1433 | PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not " |
| 1434 | "int or long"); |
| 1435 | Py_DECREF(index); |
| 1436 | return res; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1440 | /* Operations on sequences */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1441 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1442 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1443 | PySequence_Check(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | if (PyDict_Check(s)) |
| 1446 | return 0; |
| 1447 | return s != NULL && s->ob_type->tp_as_sequence && |
| 1448 | s->ob_type->tp_as_sequence->sq_item != NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1451 | Py_ssize_t |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1452 | PySequence_Size(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | if (s == NULL) { |
| 1457 | null_error(); |
| 1458 | return -1; |
| 1459 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | m = s->ob_type->tp_as_sequence; |
| 1462 | if (m && m->sq_length) |
| 1463 | return m->sq_length(s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | type_error("object of type '%.200s' has no len()", s); |
| 1466 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1469 | #undef PySequence_Length |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1470 | Py_ssize_t |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1471 | PySequence_Length(PyObject *s) |
| 1472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | return PySequence_Size(s); |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1474 | } |
| 1475 | #define PySequence_Length PySequence_Size |
| 1476 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1477 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1478 | PySequence_Concat(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | if (s == NULL || o == NULL) |
| 1483 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | m = s->ob_type->tp_as_sequence; |
| 1486 | if (m && m->sq_concat) |
| 1487 | return m->sq_concat(s, o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | /* Instances of user classes defining an __add__() method only |
| 1490 | have an nb_add slot, not an sq_concat slot. So we fall back |
| 1491 | to nb_add if both arguments appear to be sequences. */ |
| 1492 | if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1493 | PyObject *result = binary_op1(s, o, NB_SLOT(nb_add)); |
| 1494 | if (result != Py_NotImplemented) |
| 1495 | return result; |
| 1496 | Py_DECREF(result); |
| 1497 | } |
| 1498 | return type_error("'%.200s' object can't be concatenated", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1502 | PySequence_Repeat(PyObject *o, Py_ssize_t count) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | if (o == NULL) |
| 1507 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | m = o->ob_type->tp_as_sequence; |
| 1510 | if (m && m->sq_repeat) |
| 1511 | return m->sq_repeat(o, count); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | /* Instances of user classes defining a __mul__() method only |
| 1514 | have an nb_multiply slot, not an sq_repeat slot. so we fall back |
| 1515 | to nb_multiply if o appears to be a sequence. */ |
| 1516 | if (PySequence_Check(o)) { |
| 1517 | PyObject *n, *result; |
| 1518 | n = PyLong_FromSsize_t(count); |
| 1519 | if (n == NULL) |
| 1520 | return NULL; |
| 1521 | result = binary_op1(o, n, NB_SLOT(nb_multiply)); |
| 1522 | Py_DECREF(n); |
| 1523 | if (result != Py_NotImplemented) |
| 1524 | return result; |
| 1525 | Py_DECREF(result); |
| 1526 | } |
| 1527 | return type_error("'%.200s' object can't be repeated", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1531 | PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
| 1532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | PySequenceMethods *m; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | if (s == NULL || o == NULL) |
| 1536 | return null_error(); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | m = s->ob_type->tp_as_sequence; |
| 1539 | if (m && m->sq_inplace_concat) |
| 1540 | return m->sq_inplace_concat(s, o); |
| 1541 | if (m && m->sq_concat) |
| 1542 | return m->sq_concat(s, o); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1545 | PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add), |
| 1546 | NB_SLOT(nb_add)); |
| 1547 | if (result != Py_NotImplemented) |
| 1548 | return result; |
| 1549 | Py_DECREF(result); |
| 1550 | } |
| 1551 | return type_error("'%.200s' object can't be concatenated", s); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1555 | PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | PySequenceMethods *m; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | if (o == NULL) |
| 1560 | return null_error(); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | m = o->ob_type->tp_as_sequence; |
| 1563 | if (m && m->sq_inplace_repeat) |
| 1564 | return m->sq_inplace_repeat(o, count); |
| 1565 | if (m && m->sq_repeat) |
| 1566 | return m->sq_repeat(o, count); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (PySequence_Check(o)) { |
| 1569 | PyObject *n, *result; |
| 1570 | n = PyLong_FromSsize_t(count); |
| 1571 | if (n == NULL) |
| 1572 | return NULL; |
| 1573 | result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply), |
| 1574 | NB_SLOT(nb_multiply)); |
| 1575 | Py_DECREF(n); |
| 1576 | if (result != Py_NotImplemented) |
| 1577 | return result; |
| 1578 | Py_DECREF(result); |
| 1579 | } |
| 1580 | return type_error("'%.200s' object can't be repeated", o); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1584 | PySequence_GetItem(PyObject *s, Py_ssize_t i) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | if (s == NULL) |
| 1589 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | m = s->ob_type->tp_as_sequence; |
| 1592 | if (m && m->sq_item) { |
| 1593 | if (i < 0) { |
| 1594 | if (m->sq_length) { |
| 1595 | Py_ssize_t l = (*m->sq_length)(s); |
| 1596 | if (l < 0) |
| 1597 | return NULL; |
| 1598 | i += l; |
| 1599 | } |
| 1600 | } |
| 1601 | return m->sq_item(s, i); |
| 1602 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | return type_error("'%.200s' object does not support indexing", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1608 | 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] | 1609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | if (!s) return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1615 | if (mp && mp->mp_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | PyObject *res; |
| 1617 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1618 | if (!slice) |
| 1619 | return NULL; |
| 1620 | res = mp->mp_subscript(s, slice); |
| 1621 | Py_DECREF(slice); |
| 1622 | return res; |
| 1623 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | return type_error("'%.200s' object is unsliceable", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1629 | PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | if (s == NULL) { |
| 1634 | null_error(); |
| 1635 | return -1; |
| 1636 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | m = s->ob_type->tp_as_sequence; |
| 1639 | if (m && m->sq_ass_item) { |
| 1640 | if (i < 0) { |
| 1641 | if (m->sq_length) { |
| 1642 | Py_ssize_t l = (*m->sq_length)(s); |
| 1643 | if (l < 0) |
| 1644 | return -1; |
| 1645 | i += l; |
| 1646 | } |
| 1647 | } |
| 1648 | return m->sq_ass_item(s, i, o); |
| 1649 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | type_error("'%.200s' object does not support item assignment", s); |
| 1652 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1655 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1656 | PySequence_DelItem(PyObject *s, Py_ssize_t i) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | if (s == NULL) { |
| 1661 | null_error(); |
| 1662 | return -1; |
| 1663 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | m = s->ob_type->tp_as_sequence; |
| 1666 | if (m && m->sq_ass_item) { |
| 1667 | if (i < 0) { |
| 1668 | if (m->sq_length) { |
| 1669 | Py_ssize_t l = (*m->sq_length)(s); |
| 1670 | if (l < 0) |
| 1671 | return -1; |
| 1672 | i += l; |
| 1673 | } |
| 1674 | } |
| 1675 | return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 1676 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | type_error("'%.200s' object doesn't support item deletion", s); |
| 1679 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1682 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1683 | 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] | 1684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | if (s == NULL) { |
| 1688 | null_error(); |
| 1689 | return -1; |
| 1690 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1693 | if (mp && mp->mp_ass_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | int res; |
| 1695 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1696 | if (!slice) |
| 1697 | return -1; |
| 1698 | res = mp->mp_ass_subscript(s, slice, o); |
| 1699 | Py_DECREF(slice); |
| 1700 | return res; |
| 1701 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | type_error("'%.200s' object doesn't support slice assignment", s); |
| 1704 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1707 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1708 | 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] | 1709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | PyMappingMethods *mp; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | if (s == NULL) { |
| 1713 | null_error(); |
| 1714 | return -1; |
| 1715 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1718 | if (mp && mp->mp_ass_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | int res; |
| 1720 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1721 | if (!slice) |
| 1722 | return -1; |
| 1723 | res = mp->mp_ass_subscript(s, slice, NULL); |
| 1724 | Py_DECREF(slice); |
| 1725 | return res; |
| 1726 | } |
| 1727 | type_error("'%.200s' object doesn't support slice deletion", s); |
| 1728 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1731 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1732 | PySequence_Tuple(PyObject *v) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1733 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | PyObject *it; /* iter(v) */ |
| 1735 | Py_ssize_t n; /* guess for result tuple size */ |
| 1736 | PyObject *result = NULL; |
| 1737 | Py_ssize_t j; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | if (v == NULL) |
| 1740 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | /* Special-case the common tuple and list cases, for efficiency. */ |
| 1743 | if (PyTuple_CheckExact(v)) { |
| 1744 | /* Note that we can't know whether it's safe to return |
| 1745 | a tuple *subclass* instance as-is, hence the restriction |
| 1746 | to exact tuples here. In contrast, lists always make |
| 1747 | a copy, so there's no need for exactness below. */ |
| 1748 | Py_INCREF(v); |
| 1749 | return v; |
| 1750 | } |
| 1751 | if (PyList_Check(v)) |
| 1752 | return PyList_AsTuple(v); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | /* Get iterator. */ |
| 1755 | it = PyObject_GetIter(v); |
| 1756 | if (it == NULL) |
| 1757 | return NULL; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | /* Guess result size and allocate space. */ |
| 1760 | n = _PyObject_LengthHint(v, 10); |
| 1761 | if (n == -1) |
| 1762 | goto Fail; |
| 1763 | result = PyTuple_New(n); |
| 1764 | if (result == NULL) |
| 1765 | goto Fail; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | /* Fill the tuple. */ |
| 1768 | for (j = 0; ; ++j) { |
| 1769 | PyObject *item = PyIter_Next(it); |
| 1770 | if (item == NULL) { |
| 1771 | if (PyErr_Occurred()) |
| 1772 | goto Fail; |
| 1773 | break; |
| 1774 | } |
| 1775 | if (j >= n) { |
| 1776 | Py_ssize_t oldn = n; |
| 1777 | /* The over-allocation strategy can grow a bit faster |
| 1778 | than for lists because unlike lists the |
| 1779 | over-allocation isn't permanent -- we reclaim |
| 1780 | the excess before the end of this routine. |
| 1781 | So, grow by ten and then add 25%. |
| 1782 | */ |
| 1783 | n += 10; |
| 1784 | n += n >> 2; |
| 1785 | if (n < oldn) { |
| 1786 | /* Check for overflow */ |
| 1787 | PyErr_NoMemory(); |
| 1788 | Py_DECREF(item); |
| 1789 | goto Fail; |
| 1790 | } |
| 1791 | if (_PyTuple_Resize(&result, n) != 0) { |
| 1792 | Py_DECREF(item); |
| 1793 | goto Fail; |
| 1794 | } |
| 1795 | } |
| 1796 | PyTuple_SET_ITEM(result, j, item); |
| 1797 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | /* Cut tuple back if guess was too large. */ |
| 1800 | if (j < n && |
| 1801 | _PyTuple_Resize(&result, j) != 0) |
| 1802 | goto Fail; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | Py_DECREF(it); |
| 1805 | return result; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1806 | |
| 1807 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | Py_XDECREF(result); |
| 1809 | Py_DECREF(it); |
| 1810 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1813 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1814 | PySequence_List(PyObject *v) |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | PyObject *result; /* result list */ |
| 1817 | PyObject *rv; /* return value from PyList_Extend */ |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | if (v == NULL) |
| 1820 | return null_error(); |
Guido van Rossum | 5dba9e8 | 1998-07-10 18:03:50 +0000 | [diff] [blame] | 1821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | result = PyList_New(0); |
| 1823 | if (result == NULL) |
| 1824 | return NULL; |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | rv = _PyList_Extend((PyListObject *)result, v); |
| 1827 | if (rv == NULL) { |
| 1828 | Py_DECREF(result); |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | Py_DECREF(rv); |
| 1832 | return result; |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1835 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1836 | PySequence_Fast(PyObject *v, const char *m) |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | PyObject *it; |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | if (v == NULL) |
| 1841 | return null_error(); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) { |
| 1844 | Py_INCREF(v); |
| 1845 | return v; |
| 1846 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | it = PyObject_GetIter(v); |
| 1849 | if (it == NULL) { |
| 1850 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1851 | PyErr_SetString(PyExc_TypeError, m); |
| 1852 | return NULL; |
| 1853 | } |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | v = PySequence_List(it); |
| 1856 | Py_DECREF(it); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | return v; |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1861 | /* Iterate over seq. Result depends on the operation: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq. |
| 1863 | PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq; |
| 1864 | set ValueError and return -1 if none found; also return -1 on error. |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1865 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error. |
| 1866 | */ |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1867 | Py_ssize_t |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1868 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | Py_ssize_t n; |
| 1871 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */ |
| 1872 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | if (seq == NULL || obj == NULL) { |
| 1875 | null_error(); |
| 1876 | return -1; |
| 1877 | } |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | it = PyObject_GetIter(seq); |
| 1880 | if (it == NULL) { |
| 1881 | type_error("argument of type '%.200s' is not iterable", seq); |
| 1882 | return -1; |
| 1883 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | n = wrapped = 0; |
| 1886 | for (;;) { |
| 1887 | int cmp; |
| 1888 | PyObject *item = PyIter_Next(it); |
| 1889 | if (item == NULL) { |
| 1890 | if (PyErr_Occurred()) |
| 1891 | goto Fail; |
| 1892 | break; |
| 1893 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | cmp = PyObject_RichCompareBool(obj, item, Py_EQ); |
| 1896 | Py_DECREF(item); |
| 1897 | if (cmp < 0) |
| 1898 | goto Fail; |
| 1899 | if (cmp > 0) { |
| 1900 | switch (operation) { |
| 1901 | case PY_ITERSEARCH_COUNT: |
| 1902 | if (n == PY_SSIZE_T_MAX) { |
| 1903 | PyErr_SetString(PyExc_OverflowError, |
| 1904 | "count exceeds C integer size"); |
| 1905 | goto Fail; |
| 1906 | } |
| 1907 | ++n; |
| 1908 | break; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | case PY_ITERSEARCH_INDEX: |
| 1911 | if (wrapped) { |
| 1912 | PyErr_SetString(PyExc_OverflowError, |
| 1913 | "index exceeds C integer size"); |
| 1914 | goto Fail; |
| 1915 | } |
| 1916 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | case PY_ITERSEARCH_CONTAINS: |
| 1919 | n = 1; |
| 1920 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | default: |
| 1923 | assert(!"unknown operation"); |
| 1924 | } |
| 1925 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | if (operation == PY_ITERSEARCH_INDEX) { |
| 1928 | if (n == PY_SSIZE_T_MAX) |
| 1929 | wrapped = 1; |
| 1930 | ++n; |
| 1931 | } |
| 1932 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | if (operation != PY_ITERSEARCH_INDEX) |
| 1935 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | PyErr_SetString(PyExc_ValueError, |
| 1938 | "sequence.index(x): x not in sequence"); |
| 1939 | /* fall into failure code */ |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1940 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | n = -1; |
| 1942 | /* fall through */ |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1943 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | Py_DECREF(it); |
| 1945 | return n; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1946 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1949 | /* Return # of times o appears in s. */ |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1950 | Py_ssize_t |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1951 | PySequence_Count(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1956 | /* 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] | 1957 | * Use sq_contains if possible, else defer to _PySequence_IterSearch(). |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1958 | */ |
| 1959 | int |
| 1960 | PySequence_Contains(PyObject *seq, PyObject *ob) |
| 1961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | Py_ssize_t result; |
| 1963 | PySequenceMethods *sqm = seq->ob_type->tp_as_sequence; |
| 1964 | if (sqm != NULL && sqm->sq_contains != NULL) |
| 1965 | return (*sqm->sq_contains)(seq, ob); |
| 1966 | result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
| 1967 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1970 | /* Backwards compatibility */ |
| 1971 | #undef PySequence_In |
| 1972 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1973 | PySequence_In(PyObject *w, PyObject *v) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | return PySequence_Contains(w, v); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1978 | Py_ssize_t |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1979 | PySequence_Index(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1984 | /* Operations on mappings */ |
| 1985 | |
| 1986 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1987 | PyMapping_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | return o && o->ob_type->tp_as_mapping && |
| 1990 | o->ob_type->tp_as_mapping->mp_subscript; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1993 | Py_ssize_t |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1994 | PyMapping_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1995 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | if (o == NULL) { |
| 1999 | null_error(); |
| 2000 | return -1; |
| 2001 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | m = o->ob_type->tp_as_mapping; |
| 2004 | if (m && m->mp_length) |
| 2005 | return m->mp_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | type_error("object of type '%.200s' has no len()", o); |
| 2008 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 2011 | #undef PyMapping_Length |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2012 | Py_ssize_t |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 2013 | PyMapping_Length(PyObject *o) |
| 2014 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | return PyMapping_Size(o); |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 2016 | } |
| 2017 | #define PyMapping_Length PyMapping_Size |
| 2018 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2019 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2020 | PyMapping_GetItemString(PyObject *o, char *key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | PyObject *okey, *r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | if (key == NULL) |
| 2025 | return null_error(); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | okey = PyUnicode_FromString(key); |
| 2028 | if (okey == NULL) |
| 2029 | return NULL; |
| 2030 | r = PyObject_GetItem(o, okey); |
| 2031 | Py_DECREF(okey); |
| 2032 | return r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2036 | PyMapping_SetItemString(PyObject *o, char *key, PyObject *value) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2037 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | PyObject *okey; |
| 2039 | int r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | if (key == NULL) { |
| 2042 | null_error(); |
| 2043 | return -1; |
| 2044 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | okey = PyUnicode_FromString(key); |
| 2047 | if (okey == NULL) |
| 2048 | return -1; |
| 2049 | r = PyObject_SetItem(o, okey, value); |
| 2050 | Py_DECREF(okey); |
| 2051 | return r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2055 | PyMapping_HasKeyString(PyObject *o, char *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | v = PyMapping_GetItemString(o, key); |
| 2060 | if (v) { |
| 2061 | Py_DECREF(v); |
| 2062 | return 1; |
| 2063 | } |
| 2064 | PyErr_Clear(); |
| 2065 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2068 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2069 | PyMapping_HasKey(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | v = PyObject_GetItem(o, key); |
| 2074 | if (v) { |
| 2075 | Py_DECREF(v); |
| 2076 | return 1; |
| 2077 | } |
| 2078 | PyErr_Clear(); |
| 2079 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2082 | PyObject * |
| 2083 | PyMapping_Keys(PyObject *o) |
| 2084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | PyObject *keys; |
| 2086 | PyObject *fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | if (PyDict_CheckExact(o)) |
| 2089 | return PyDict_Keys(o); |
| 2090 | keys = PyObject_CallMethod(o, "keys", NULL); |
| 2091 | if (keys == NULL) |
| 2092 | return NULL; |
| 2093 | fast = PySequence_Fast(keys, "o.keys() are not iterable"); |
| 2094 | Py_DECREF(keys); |
| 2095 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | PyObject * |
| 2099 | PyMapping_Items(PyObject *o) |
| 2100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | PyObject *items; |
| 2102 | PyObject *fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | if (PyDict_CheckExact(o)) |
| 2105 | return PyDict_Items(o); |
| 2106 | items = PyObject_CallMethod(o, "items", NULL); |
| 2107 | if (items == NULL) |
| 2108 | return NULL; |
| 2109 | fast = PySequence_Fast(items, "o.items() are not iterable"); |
| 2110 | Py_DECREF(items); |
| 2111 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | PyObject * |
| 2115 | PyMapping_Values(PyObject *o) |
| 2116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | PyObject *values; |
| 2118 | PyObject *fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | if (PyDict_CheckExact(o)) |
| 2121 | return PyDict_Values(o); |
| 2122 | values = PyObject_CallMethod(o, "values", NULL); |
| 2123 | if (values == NULL) |
| 2124 | return NULL; |
| 2125 | fast = PySequence_Fast(values, "o.values() are not iterable"); |
| 2126 | Py_DECREF(values); |
| 2127 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2130 | /* Operations on callable objects */ |
| 2131 | |
| 2132 | /* XXX PyCallable_Check() is in object.c */ |
| 2133 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2134 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2135 | PyObject_CallObject(PyObject *o, PyObject *a) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | return PyEval_CallObjectWithKeywords(o, a, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2138 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2139 | |
| 2140 | PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2141 | PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | ternaryfunc call; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | if ((call = func->ob_type->tp_call) != NULL) { |
| 2146 | PyObject *result; |
| 2147 | if (Py_EnterRecursiveCall(" while calling a Python object")) |
| 2148 | return NULL; |
| 2149 | result = (*call)(func, arg, kw); |
| 2150 | Py_LeaveRecursiveCall(); |
| 2151 | if (result == NULL && !PyErr_Occurred()) |
| 2152 | PyErr_SetString( |
| 2153 | PyExc_SystemError, |
| 2154 | "NULL result without error in PyObject_Call"); |
| 2155 | return result; |
| 2156 | } |
| 2157 | PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable", |
| 2158 | func->ob_type->tp_name); |
| 2159 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2162 | static PyObject* |
| 2163 | call_function_tail(PyObject *callable, PyObject *args) |
| 2164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | PyObject *retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | if (args == NULL) |
| 2168 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | if (!PyTuple_Check(args)) { |
| 2171 | PyObject *a; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | a = PyTuple_New(1); |
| 2174 | if (a == NULL) { |
| 2175 | Py_DECREF(args); |
| 2176 | return NULL; |
| 2177 | } |
| 2178 | PyTuple_SET_ITEM(a, 0, args); |
| 2179 | args = a; |
| 2180 | } |
| 2181 | retval = PyObject_Call(callable, args, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | Py_DECREF(args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2188 | PyObject * |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2189 | PyObject_CallFunction(PyObject *callable, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | va_list va; |
| 2192 | PyObject *args; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | if (callable == NULL) |
| 2195 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | if (format && *format) { |
| 2198 | va_start(va, format); |
| 2199 | args = Py_VaBuildValue(format, va); |
| 2200 | va_end(va); |
| 2201 | } |
| 2202 | else |
| 2203 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | return call_function_tail(callable, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2206 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2207 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2208 | PyObject * |
| 2209 | _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...) |
| 2210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | va_list va; |
| 2212 | PyObject *args; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | if (callable == NULL) |
| 2215 | return null_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | if (format && *format) { |
| 2218 | va_start(va, format); |
| 2219 | args = _Py_VaBuildValue_SizeT(format, va); |
| 2220 | va_end(va); |
| 2221 | } |
| 2222 | else |
| 2223 | args = PyTuple_New(0); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | return call_function_tail(callable, args); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | PyObject * |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2229 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | va_list va; |
| 2232 | PyObject *args; |
| 2233 | PyObject *func = NULL; |
| 2234 | PyObject *retval = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | if (o == NULL || name == NULL) |
| 2237 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | func = PyObject_GetAttrString(o, name); |
| 2240 | if (func == NULL) { |
| 2241 | PyErr_SetString(PyExc_AttributeError, name); |
| 2242 | return 0; |
| 2243 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | if (!PyCallable_Check(func)) { |
| 2246 | type_error("attribute of type '%.200s' is not callable", func); |
| 2247 | goto exit; |
| 2248 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | if (format && *format) { |
| 2251 | va_start(va, format); |
| 2252 | args = Py_VaBuildValue(format, va); |
| 2253 | va_end(va); |
| 2254 | } |
| 2255 | else |
| 2256 | args = PyTuple_New(0); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | retval = call_function_tail(func, args); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2259 | |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 2260 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | /* args gets consumed in call_function_tail */ |
| 2262 | Py_XDECREF(func); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | PyObject * |
| 2268 | _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) |
| 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | va_list va; |
| 2271 | PyObject *args; |
| 2272 | PyObject *func = NULL; |
| 2273 | PyObject *retval = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | if (o == NULL || name == NULL) |
| 2276 | return null_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | func = PyObject_GetAttrString(o, name); |
| 2279 | if (func == NULL) { |
| 2280 | PyErr_SetString(PyExc_AttributeError, name); |
| 2281 | return 0; |
| 2282 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | if (!PyCallable_Check(func)) { |
| 2285 | type_error("attribute of type '%.200s' is not callable", func); |
| 2286 | goto exit; |
| 2287 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | if (format && *format) { |
| 2290 | va_start(va, format); |
| 2291 | args = _Py_VaBuildValue_SizeT(format, va); |
| 2292 | va_end(va); |
| 2293 | } |
| 2294 | else |
| 2295 | args = PyTuple_New(0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | retval = call_function_tail(func, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2298 | |
| 2299 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | /* args gets consumed in call_function_tail */ |
| 2301 | Py_XDECREF(func); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2304 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2305 | |
| 2306 | |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2307 | static PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2308 | objargs_mktuple(va_list va) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | int i, n = 0; |
| 2311 | va_list countva; |
| 2312 | PyObject *result, *tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2313 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2314 | Py_VA_COPY(countva, va); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | while (((PyObject *)va_arg(countva, PyObject *)) != NULL) |
| 2317 | ++n; |
| 2318 | result = PyTuple_New(n); |
| 2319 | if (result != NULL && n > 0) { |
| 2320 | for (i = 0; i < n; ++i) { |
| 2321 | tmp = (PyObject *)va_arg(va, PyObject *); |
| 2322 | PyTuple_SET_ITEM(result, i, tmp); |
| 2323 | Py_INCREF(tmp); |
| 2324 | } |
| 2325 | } |
| 2326 | return result; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2330 | PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | PyObject *args, *tmp; |
| 2333 | va_list vargs; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | if (callable == NULL || name == NULL) |
| 2336 | return null_error(); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | callable = PyObject_GetAttr(callable, name); |
| 2339 | if (callable == NULL) |
| 2340 | return NULL; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | /* count the args */ |
| 2343 | va_start(vargs, name); |
| 2344 | args = objargs_mktuple(vargs); |
| 2345 | va_end(vargs); |
| 2346 | if (args == NULL) { |
| 2347 | Py_DECREF(callable); |
| 2348 | return NULL; |
| 2349 | } |
| 2350 | tmp = PyObject_Call(callable, args, NULL); |
| 2351 | Py_DECREF(args); |
| 2352 | Py_DECREF(callable); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | return tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2358 | PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | PyObject *args, *tmp; |
| 2361 | va_list vargs; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | if (callable == NULL) |
| 2364 | return null_error(); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | /* count the args */ |
| 2367 | va_start(vargs, callable); |
| 2368 | args = objargs_mktuple(vargs); |
| 2369 | va_end(vargs); |
| 2370 | if (args == NULL) |
| 2371 | return NULL; |
| 2372 | tmp = PyObject_Call(callable, args, NULL); |
| 2373 | Py_DECREF(args); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | return tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2379 | /* isinstance(), issubclass() */ |
| 2380 | |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 2381 | /* abstract_get_bases() has logically 4 return states, with a sort of 0th |
| 2382 | * state that will almost never happen. |
| 2383 | * |
| 2384 | * 0. creating the __bases__ static string could get a MemoryError |
| 2385 | * 1. getattr(cls, '__bases__') could raise an AttributeError |
| 2386 | * 2. getattr(cls, '__bases__') could raise some other exception |
| 2387 | * 3. getattr(cls, '__bases__') could return a tuple |
| 2388 | * 4. getattr(cls, '__bases__') could return something other than a tuple |
| 2389 | * |
| 2390 | * Only state #3 is a non-error state and only it returns a non-NULL object |
| 2391 | * (it returns the retrieved tuple). |
| 2392 | * |
| 2393 | * Any raised AttributeErrors are masked by clearing the exception and |
| 2394 | * returning NULL. If an object other than a tuple comes out of __bases__, |
| 2395 | * then again, the return value is NULL. So yes, these two situations |
| 2396 | * produce exactly the same results: NULL is returned and no error is set. |
| 2397 | * |
| 2398 | * If some exception other than AttributeError is raised, then NULL is also |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | * returned, but the exception is not cleared. That's because we want the |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 2400 | * exception to be propagated along. |
| 2401 | * |
| 2402 | * Callers are expected to test for PyErr_Occurred() when the return value |
| 2403 | * is NULL to decide whether a valid exception should be propagated or not. |
| 2404 | * When there's no exception to propagate, it's customary for the caller to |
| 2405 | * set a TypeError. |
| 2406 | */ |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2407 | static PyObject * |
| 2408 | abstract_get_bases(PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | static PyObject *__bases__ = NULL; |
| 2411 | PyObject *bases; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | if (__bases__ == NULL) { |
| 2414 | __bases__ = PyUnicode_InternFromString("__bases__"); |
| 2415 | if (__bases__ == NULL) |
| 2416 | return NULL; |
| 2417 | } |
| 2418 | Py_ALLOW_RECURSION |
| 2419 | bases = PyObject_GetAttr(cls, __bases__); |
| 2420 | Py_END_ALLOW_RECURSION |
| 2421 | if (bases == NULL) { |
| 2422 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2423 | PyErr_Clear(); |
| 2424 | return NULL; |
| 2425 | } |
| 2426 | if (!PyTuple_Check(bases)) { |
| 2427 | Py_DECREF(bases); |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | return bases; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | |
| 2434 | static int |
| 2435 | abstract_issubclass(PyObject *derived, PyObject *cls) |
| 2436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | PyObject *bases = NULL; |
| 2438 | Py_ssize_t i, n; |
| 2439 | int r = 0; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | while (1) { |
| 2442 | if (derived == cls) |
| 2443 | return 1; |
| 2444 | bases = abstract_get_bases(derived); |
| 2445 | if (bases == NULL) { |
| 2446 | if (PyErr_Occurred()) |
| 2447 | return -1; |
| 2448 | return 0; |
| 2449 | } |
| 2450 | n = PyTuple_GET_SIZE(bases); |
| 2451 | if (n == 0) { |
| 2452 | Py_DECREF(bases); |
| 2453 | return 0; |
| 2454 | } |
| 2455 | /* Avoid recursivity in the single inheritance case */ |
| 2456 | if (n == 1) { |
| 2457 | derived = PyTuple_GET_ITEM(bases, 0); |
| 2458 | Py_DECREF(bases); |
| 2459 | continue; |
| 2460 | } |
| 2461 | for (i = 0; i < n; i++) { |
| 2462 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
| 2463 | if (r != 0) |
| 2464 | break; |
| 2465 | } |
| 2466 | Py_DECREF(bases); |
| 2467 | return r; |
| 2468 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2471 | static int |
| 2472 | check_class(PyObject *cls, const char *error) |
| 2473 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | PyObject *bases = abstract_get_bases(cls); |
| 2475 | if (bases == NULL) { |
| 2476 | /* Do not mask errors. */ |
| 2477 | if (!PyErr_Occurred()) |
| 2478 | PyErr_SetString(PyExc_TypeError, error); |
| 2479 | return 0; |
| 2480 | } |
| 2481 | Py_DECREF(bases); |
| 2482 | return -1; |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2485 | static int |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2486 | recursive_isinstance(PyObject *inst, PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | PyObject *icls; |
| 2489 | static PyObject *__class__ = NULL; |
| 2490 | int retval = 0; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | if (__class__ == NULL) { |
| 2493 | __class__ = PyUnicode_InternFromString("__class__"); |
| 2494 | if (__class__ == NULL) |
| 2495 | return -1; |
| 2496 | } |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | if (PyType_Check(cls)) { |
| 2499 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
| 2500 | if (retval == 0) { |
| 2501 | PyObject *c = PyObject_GetAttr(inst, __class__); |
| 2502 | if (c == NULL) { |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2503 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2504 | PyErr_Clear(); |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2505 | else |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2506 | retval = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | } |
| 2508 | else { |
| 2509 | if (c != (PyObject *)(inst->ob_type) && |
| 2510 | PyType_Check(c)) |
| 2511 | retval = PyType_IsSubtype( |
| 2512 | (PyTypeObject *)c, |
| 2513 | (PyTypeObject *)cls); |
| 2514 | Py_DECREF(c); |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | else { |
| 2519 | if (!check_class(cls, |
Benjamin Peterson | e893af5 | 2010-06-28 19:43:42 +0000 | [diff] [blame] | 2520 | "isinstance() arg 2 must be a type or tuple of types")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | return -1; |
| 2522 | icls = PyObject_GetAttr(inst, __class__); |
| 2523 | if (icls == NULL) { |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2524 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2525 | PyErr_Clear(); |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2526 | else |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2527 | retval = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | } |
| 2529 | else { |
| 2530 | retval = abstract_issubclass(icls, cls); |
| 2531 | Py_DECREF(icls); |
| 2532 | } |
| 2533 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | return retval; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | int |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2539 | PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 2540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | static PyObject *name = NULL; |
| 2542 | PyObject *checker; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | /* Quick test for an exact match */ |
| 2545 | if (Py_TYPE(inst) == (PyTypeObject *)cls) |
| 2546 | return 1; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | if (PyTuple_Check(cls)) { |
| 2549 | Py_ssize_t i; |
| 2550 | Py_ssize_t n; |
| 2551 | int r = 0; |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | if (Py_EnterRecursiveCall(" in __instancecheck__")) |
| 2554 | return -1; |
| 2555 | n = PyTuple_GET_SIZE(cls); |
| 2556 | for (i = 0; i < n; ++i) { |
| 2557 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 2558 | r = PyObject_IsInstance(inst, item); |
| 2559 | if (r != 0) |
| 2560 | /* either found it, or got an error */ |
| 2561 | break; |
| 2562 | } |
| 2563 | Py_LeaveRecursiveCall(); |
| 2564 | return r; |
| 2565 | } |
Benjamin Peterson | 88fe5f9 | 2009-05-16 21:55:24 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name); |
| 2568 | if (checker != NULL) { |
| 2569 | PyObject *res; |
| 2570 | int ok = -1; |
| 2571 | if (Py_EnterRecursiveCall(" in __instancecheck__")) { |
| 2572 | Py_DECREF(checker); |
| 2573 | return ok; |
| 2574 | } |
| 2575 | res = PyObject_CallFunctionObjArgs(checker, inst, NULL); |
| 2576 | Py_LeaveRecursiveCall(); |
| 2577 | Py_DECREF(checker); |
| 2578 | if (res != NULL) { |
| 2579 | ok = PyObject_IsTrue(res); |
| 2580 | Py_DECREF(res); |
| 2581 | } |
| 2582 | return ok; |
| 2583 | } |
| 2584 | else if (PyErr_Occurred()) |
| 2585 | return -1; |
| 2586 | return recursive_isinstance(inst, cls); |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | static int |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2590 | recursive_issubclass(PyObject *derived, PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | if (PyType_Check(cls) && PyType_Check(derived)) { |
| 2593 | /* Fast path (non-recursive) */ |
| 2594 | return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls); |
| 2595 | } |
| 2596 | if (!check_class(derived, |
| 2597 | "issubclass() arg 1 must be a class")) |
| 2598 | return -1; |
| 2599 | if (!check_class(cls, |
| 2600 | "issubclass() arg 2 must be a class" |
| 2601 | " or tuple of classes")) |
| 2602 | return -1; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | return abstract_issubclass(derived, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2605 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2606 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2607 | int |
| 2608 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 2609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | static PyObject *name = NULL; |
| 2611 | PyObject *checker; |
Georg Brandl | dcfe8e4 | 2009-05-17 08:22:45 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | if (PyTuple_Check(cls)) { |
| 2614 | Py_ssize_t i; |
| 2615 | Py_ssize_t n; |
| 2616 | int r = 0; |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | if (Py_EnterRecursiveCall(" in __subclasscheck__")) |
| 2619 | return -1; |
| 2620 | n = PyTuple_GET_SIZE(cls); |
| 2621 | for (i = 0; i < n; ++i) { |
| 2622 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 2623 | r = PyObject_IsSubclass(derived, item); |
| 2624 | if (r != 0) |
| 2625 | /* either found it, or got an error */ |
| 2626 | break; |
| 2627 | } |
| 2628 | Py_LeaveRecursiveCall(); |
| 2629 | return r; |
| 2630 | } |
Benjamin Peterson | 88fe5f9 | 2009-05-16 21:55:24 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name); |
| 2633 | if (checker != NULL) { |
| 2634 | PyObject *res; |
| 2635 | int ok = -1; |
| 2636 | if (Py_EnterRecursiveCall(" in __subclasscheck__")) { |
| 2637 | Py_DECREF(checker); |
| 2638 | return ok; |
| 2639 | } |
| 2640 | res = PyObject_CallFunctionObjArgs(checker, derived, NULL); |
| 2641 | Py_LeaveRecursiveCall(); |
| 2642 | Py_DECREF(checker); |
| 2643 | if (res != NULL) { |
| 2644 | ok = PyObject_IsTrue(res); |
| 2645 | Py_DECREF(res); |
| 2646 | } |
| 2647 | return ok; |
| 2648 | } |
| 2649 | else if (PyErr_Occurred()) |
| 2650 | return -1; |
| 2651 | return recursive_issubclass(derived, cls); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | int |
| 2655 | _PyObject_RealIsInstance(PyObject *inst, PyObject *cls) |
| 2656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | return recursive_isinstance(inst, cls); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | int |
| 2661 | _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls) |
| 2662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | return recursive_issubclass(derived, cls); |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2667 | PyObject * |
| 2668 | PyObject_GetIter(PyObject *o) |
| 2669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | PyTypeObject *t = o->ob_type; |
| 2671 | getiterfunc f = NULL; |
| 2672 | f = t->tp_iter; |
| 2673 | if (f == NULL) { |
| 2674 | if (PySequence_Check(o)) |
| 2675 | return PySeqIter_New(o); |
| 2676 | return type_error("'%.200s' object is not iterable", o); |
| 2677 | } |
| 2678 | else { |
| 2679 | PyObject *res = (*f)(o); |
| 2680 | if (res != NULL && !PyIter_Check(res)) { |
| 2681 | PyErr_Format(PyExc_TypeError, |
| 2682 | "iter() returned non-iterator " |
| 2683 | "of type '%.100s'", |
| 2684 | res->ob_type->tp_name); |
| 2685 | Py_DECREF(res); |
| 2686 | res = NULL; |
| 2687 | } |
| 2688 | return res; |
| 2689 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2692 | /* Return next item. |
| 2693 | * If an error occurs, return NULL. PyErr_Occurred() will be true. |
| 2694 | * If the iteration terminates normally, return NULL and clear the |
| 2695 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred() |
| 2696 | * will be false. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | * Else return the next object. PyErr_Occurred() will be false. |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2698 | */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2699 | PyObject * |
| 2700 | PyIter_Next(PyObject *iter) |
| 2701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | PyObject *result; |
| 2703 | result = (*iter->ob_type->tp_iternext)(iter); |
| 2704 | if (result == NULL && |
| 2705 | PyErr_Occurred() && |
| 2706 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2707 | PyErr_Clear(); |
| 2708 | return result; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2709 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2710 | |
| 2711 | |
| 2712 | /* |
| 2713 | * Flatten a sequence of bytes() objects into a C array of |
| 2714 | * NULL terminated string pointers with a NULL char* terminating the array. |
| 2715 | * (ie: an argv or env list) |
| 2716 | * |
| 2717 | * Memory allocated for the returned list is allocated using malloc() and MUST |
| 2718 | * be freed by the caller using a free() loop or _Py_FreeCharPArray(). |
| 2719 | */ |
| 2720 | char *const * |
| 2721 | _PySequence_BytesToCharpArray(PyObject* self) |
| 2722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | char **array; |
| 2724 | Py_ssize_t i, argc; |
| 2725 | PyObject *item = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | argc = PySequence_Size(self); |
| 2728 | if (argc == -1) |
| 2729 | return NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | array = malloc((argc + 1) * sizeof(char *)); |
| 2732 | if (array == NULL) { |
| 2733 | PyErr_NoMemory(); |
| 2734 | return NULL; |
| 2735 | } |
| 2736 | for (i = 0; i < argc; ++i) { |
| 2737 | char *data; |
| 2738 | item = PySequence_GetItem(self, i); |
| 2739 | data = PyBytes_AsString(item); |
| 2740 | if (data == NULL) { |
| 2741 | /* NULL terminate before freeing. */ |
| 2742 | array[i] = NULL; |
| 2743 | goto fail; |
| 2744 | } |
| 2745 | array[i] = strdup(data); |
| 2746 | if (!array[i]) { |
| 2747 | PyErr_NoMemory(); |
| 2748 | goto fail; |
| 2749 | } |
| 2750 | Py_DECREF(item); |
| 2751 | } |
| 2752 | array[argc] = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | return array; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2755 | |
| 2756 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | Py_XDECREF(item); |
| 2758 | _Py_FreeCharPArray(array); |
| 2759 | return NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | /* Free's a NULL terminated char** array of C strings. */ |
| 2764 | void |
| 2765 | _Py_FreeCharPArray(char *const array[]) |
| 2766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | Py_ssize_t i; |
| 2768 | for (i = 0; array[i] != NULL; ++i) { |
| 2769 | free(array[i]); |
| 2770 | } |
| 2771 | free((void*)array); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2772 | } |