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