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