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