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 * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 935 | PyNumber_FloorDivide(PyObject *v, PyObject *w) |
| 936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | return binary_op(v, w, NB_SLOT(nb_floor_divide), "//"); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | PyObject * |
| 941 | PyNumber_TrueDivide(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_true_divide), "/"); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 947 | PyNumber_Remainder(PyObject *v, PyObject *w) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | return binary_op(v, w, NB_SLOT(nb_remainder), "%"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 953 | PyNumber_Power(PyObject *v, PyObject *w, PyObject *z) |
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 ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()"); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 958 | /* Binary in-place operators */ |
| 959 | |
| 960 | /* The in-place operators are defined to fall back to the 'normal', |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 961 | 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] | 962 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 963 | - If the left hand object has the appropriate struct members, and |
| 964 | they are filled, call the appropriate function and return the |
| 965 | result. No coercion is done on the arguments; the left-hand object |
| 966 | is the one the operation is performed on, and it's up to the |
| 967 | function to deal with the right-hand object. |
Guido van Rossum | 7766091 | 2002-04-16 16:32:50 +0000 | [diff] [blame] | 968 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 969 | - Otherwise, in-place modification is not supported. Handle it exactly as |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 970 | a non in-place operation of the same kind. |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 971 | |
| 972 | */ |
| 973 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 974 | static PyObject * |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 975 | 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] | 976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 978 | if (mv != NULL) { |
| 979 | binaryfunc slot = NB_BINOP(mv, iop_slot); |
| 980 | if (slot) { |
| 981 | PyObject *x = (slot)(v, w); |
| 982 | if (x != Py_NotImplemented) { |
| 983 | return x; |
| 984 | } |
| 985 | Py_DECREF(x); |
| 986 | } |
| 987 | } |
| 988 | return binary_op1(v, w, op_slot); |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static PyObject * |
| 992 | 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] | 993 | const char *op_name) |
Neil Schemenauer | d4b0fea | 2002-12-30 20:18:15 +0000 | [diff] [blame] | 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | PyObject *result = binary_iop1(v, w, iop_slot, op_slot); |
| 996 | if (result == Py_NotImplemented) { |
| 997 | Py_DECREF(result); |
| 998 | return binop_type_error(v, w, op_name); |
| 999 | } |
| 1000 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1003 | #define INPLACE_BINOP(func, iop, op, op_name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | PyObject * \ |
| 1005 | func(PyObject *v, PyObject *w) { \ |
| 1006 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ |
| 1007 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1008 | |
Neil Schemenauer | 5a1f015 | 2001-01-04 01:39:06 +0000 | [diff] [blame] | 1009 | INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=") |
| 1010 | INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=") |
| 1011 | INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=") |
| 1012 | INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=") |
| 1013 | INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=") |
| 1014 | INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=") |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1015 | |
| 1016 | PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1017 | PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w) |
| 1018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide), |
| 1020 | NB_SLOT(nb_floor_divide), "//="); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | PyObject * |
| 1024 | PyNumber_InPlaceTrueDivide(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_true_divide), |
| 1027 | NB_SLOT(nb_true_divide), "/="); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1031 | PyNumber_InPlaceAdd(PyObject *v, PyObject *w) |
| 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add), |
| 1034 | NB_SLOT(nb_add)); |
| 1035 | if (result == Py_NotImplemented) { |
| 1036 | PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 1037 | Py_DECREF(result); |
| 1038 | if (m != NULL) { |
| 1039 | binaryfunc f = NULL; |
| 1040 | f = m->sq_inplace_concat; |
| 1041 | if (f == NULL) |
| 1042 | f = m->sq_concat; |
| 1043 | if (f != NULL) |
| 1044 | return (*f)(v, w); |
| 1045 | } |
| 1046 | result = binop_type_error(v, w, "+="); |
| 1047 | } |
| 1048 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | PyObject * |
| 1052 | PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) |
| 1053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply), |
| 1055 | NB_SLOT(nb_multiply)); |
| 1056 | if (result == Py_NotImplemented) { |
| 1057 | ssizeargfunc f = NULL; |
| 1058 | PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 1059 | PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 1060 | Py_DECREF(result); |
| 1061 | if (mv != NULL) { |
| 1062 | f = mv->sq_inplace_repeat; |
| 1063 | if (f == NULL) |
| 1064 | f = mv->sq_repeat; |
| 1065 | if (f != NULL) |
| 1066 | return sequence_repeat(f, v, w); |
| 1067 | } |
| 1068 | else if (mw != NULL) { |
| 1069 | /* Note that the right hand operand should not be |
| 1070 | * mutated in this case so sq_inplace_repeat is not |
| 1071 | * used. */ |
| 1072 | if (mw->sq_repeat) |
| 1073 | return sequence_repeat(mw->sq_repeat, w, v); |
| 1074 | } |
| 1075 | result = binop_type_error(v, w, "*="); |
| 1076 | } |
| 1077 | return result; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1080 | PyObject * |
| 1081 | PyNumber_InPlaceRemainder(PyObject *v, PyObject *w) |
| 1082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | return binary_iop(v, w, NB_SLOT(nb_inplace_remainder), |
| 1084 | NB_SLOT(nb_remainder), "%="); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | PyObject * |
| 1088 | PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z) |
| 1089 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | if (v->ob_type->tp_as_number && |
| 1091 | v->ob_type->tp_as_number->nb_inplace_power != NULL) { |
| 1092 | return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**="); |
| 1093 | } |
| 1094 | else { |
| 1095 | return ternary_op(v, w, z, NB_SLOT(nb_power), "**="); |
| 1096 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1100 | /* Unary operators and functions */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1101 | |
| 1102 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1103 | PyNumber_Negative(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | PyNumberMethods *m; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | if (o == NULL) |
| 1108 | return null_error(); |
| 1109 | m = o->ob_type->tp_as_number; |
| 1110 | if (m && m->nb_negative) |
| 1111 | return (*m->nb_negative)(o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | return type_error("bad operand type for unary -: '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1117 | PyNumber_Positive(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_positive) |
| 1125 | return (*m->nb_positive)(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_Invert(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_invert) |
| 1139 | return (*m->nb_invert)(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_Absolute(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 | e15dee5 | 1995-07-18 14:12:02 +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_absolute) |
| 1153 | return m->nb_absolute(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | return type_error("bad operand type for abs(): '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1158 | /* Return a Python int from the object item |
| 1159 | Raise TypeError if the result is not an int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1160 | or if the object cannot be interpreted as an index. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1161 | */ |
| 1162 | PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1163 | PyNumber_Index(PyObject *item) |
| 1164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | PyObject *result = NULL; |
| 1166 | if (item == NULL) |
| 1167 | return null_error(); |
| 1168 | if (PyLong_Check(item)) { |
| 1169 | Py_INCREF(item); |
| 1170 | return item; |
| 1171 | } |
| 1172 | if (PyIndex_Check(item)) { |
| 1173 | result = item->ob_type->tp_as_number->nb_index(item); |
| 1174 | if (result && !PyLong_Check(result)) { |
| 1175 | PyErr_Format(PyExc_TypeError, |
| 1176 | "__index__ returned non-int " |
| 1177 | "(type %.200s)", |
| 1178 | result->ob_type->tp_name); |
| 1179 | Py_DECREF(result); |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | } |
| 1183 | else { |
| 1184 | PyErr_Format(PyExc_TypeError, |
| 1185 | "'%.200s' object cannot be interpreted " |
| 1186 | "as an integer", item->ob_type->tp_name); |
| 1187 | } |
| 1188 | return result; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1191 | /* Return an error on Overflow only if err is not NULL*/ |
| 1192 | |
| 1193 | Py_ssize_t |
| 1194 | PyNumber_AsSsize_t(PyObject *item, PyObject *err) |
| 1195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | Py_ssize_t result; |
| 1197 | PyObject *runerr; |
| 1198 | PyObject *value = PyNumber_Index(item); |
| 1199 | if (value == NULL) |
| 1200 | return -1; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
| 1203 | result = PyLong_AsSsize_t(value); |
| 1204 | if (result != -1 || !(runerr = PyErr_Occurred())) |
| 1205 | goto finish; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | /* Error handling code -- only manage OverflowError differently */ |
| 1208 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 1209 | goto finish; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | PyErr_Clear(); |
| 1212 | /* If no error-handling desired then the default clipping |
| 1213 | is sufficient. |
| 1214 | */ |
| 1215 | if (!err) { |
| 1216 | assert(PyLong_Check(value)); |
| 1217 | /* Whether or not it is less than or equal to |
| 1218 | zero is determined by the sign of ob_size |
| 1219 | */ |
| 1220 | if (_PyLong_Sign(value) < 0) |
| 1221 | result = PY_SSIZE_T_MIN; |
| 1222 | else |
| 1223 | result = PY_SSIZE_T_MAX; |
| 1224 | } |
| 1225 | else { |
| 1226 | /* Otherwise replace the error with caller's error object. */ |
| 1227 | PyErr_Format(err, |
| 1228 | "cannot fit '%.200s' into an index-sized integer", |
| 1229 | item->ob_type->tp_name); |
| 1230 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1231 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1232 | finish: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | Py_DECREF(value); |
| 1234 | return result; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | |
Benjamin Peterson | b7f1da5 | 2012-03-21 14:44:43 -0400 | [diff] [blame] | 1238 | /* |
| 1239 | Returns the Integral instance converted to an int. The instance is expected |
| 1240 | to be an int or have an __int__ method. Steals integral's |
| 1241 | reference. error_format will be used to create the TypeError if integral |
| 1242 | isn't actually an Integral instance. error_format should be a format string |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 1243 | that can accept a char* naming integral's type. |
Benjamin Peterson | b7f1da5 | 2012-03-21 14:44:43 -0400 | [diff] [blame] | 1244 | */ |
| 1245 | static PyObject * |
| 1246 | convert_integral_to_int(PyObject *integral, const char *error_format) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1247 | { |
Benjamin Peterson | d614e70 | 2012-03-21 14:38:11 -0400 | [diff] [blame] | 1248 | PyNumberMethods *nb; |
| 1249 | if (PyLong_Check(integral)) |
| 1250 | return integral; |
| 1251 | nb = Py_TYPE(integral)->tp_as_number; |
| 1252 | if (nb->nb_int) { |
| 1253 | PyObject *as_int = nb->nb_int(integral); |
Mark Dickinson | 3d78385 | 2012-09-27 19:38:59 +0100 | [diff] [blame] | 1254 | if (!as_int || PyLong_Check(as_int)) { |
| 1255 | Py_DECREF(integral); |
Benjamin Peterson | d614e70 | 2012-03-21 14:38:11 -0400 | [diff] [blame] | 1256 | return as_int; |
Mark Dickinson | 3d78385 | 2012-09-27 19:38:59 +0100 | [diff] [blame] | 1257 | } |
Benjamin Peterson | d614e70 | 2012-03-21 14:38:11 -0400 | [diff] [blame] | 1258 | Py_DECREF(as_int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name); |
| 1261 | Py_DECREF(integral); |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 1262 | return NULL; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1266 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1267 | PyNumber_Long(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | PyNumberMethods *m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | PyObject *trunc_func; |
| 1271 | const char *buffer; |
| 1272 | Py_ssize_t buffer_len; |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 1273 | _Py_IDENTIFIER(__trunc__); |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | if (o == NULL) |
| 1276 | return null_error(); |
| 1277 | if (PyLong_CheckExact(o)) { |
| 1278 | Py_INCREF(o); |
| 1279 | return o; |
| 1280 | } |
| 1281 | m = o->ob_type->tp_as_number; |
| 1282 | if (m && m->nb_int) { /* This should include subclasses of int */ |
| 1283 | PyObject *res = m->nb_int(o); |
| 1284 | if (res && !PyLong_Check(res)) { |
| 1285 | PyErr_Format(PyExc_TypeError, |
| 1286 | "__int__ returned non-int (type %.200s)", |
| 1287 | res->ob_type->tp_name); |
| 1288 | Py_DECREF(res); |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | return res; |
| 1292 | } |
| 1293 | if (PyLong_Check(o)) /* An int subclass without nb_int */ |
| 1294 | return _PyLong_Copy((PyLongObject *)o); |
Benjamin Peterson | 1b1a8e7 | 2012-03-20 23:48:11 -0400 | [diff] [blame] | 1295 | trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | if (trunc_func) { |
| 1297 | PyObject *truncated = PyEval_CallObject(trunc_func, NULL); |
| 1298 | PyObject *int_instance; |
| 1299 | Py_DECREF(trunc_func); |
Mark Dickinson | c973448 | 2013-04-13 17:44:44 +0100 | [diff] [blame] | 1300 | if (truncated == NULL) |
| 1301 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | /* __trunc__ is specified to return an Integral type, |
Benjamin Peterson | 520e850 | 2012-03-21 14:51:14 -0400 | [diff] [blame] | 1303 | but int() needs to return a int. */ |
Benjamin Peterson | b7f1da5 | 2012-03-21 14:44:43 -0400 | [diff] [blame] | 1304 | int_instance = convert_integral_to_int(truncated, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | "__trunc__ returned non-Integral (type %.200s)"); |
| 1306 | return int_instance; |
| 1307 | } |
Benjamin Peterson | 1b1a8e7 | 2012-03-20 23:48:11 -0400 | [diff] [blame] | 1308 | if (PyErr_Occurred()) |
| 1309 | return NULL; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | if (PyBytes_Check(o)) |
| 1312 | /* need to do extra error checking that PyLong_FromString() |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 1313 | * doesn't do. In particular int('9\x005') must raise an |
| 1314 | * exception, not truncate at the null. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | */ |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 1316 | return _PyLong_FromBytes(PyBytes_AS_STRING(o), |
| 1317 | PyBytes_GET_SIZE(o), 10); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | if (PyUnicode_Check(o)) |
| 1319 | /* The above check is done in PyLong_FromUnicode(). */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | return PyLong_FromUnicodeObject(o, 10); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 1322 | return _PyLong_FromBytes(buffer, buffer_len, 10); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | return type_error("int() argument must be a string or a " |
| 1325 | "number, not '%.200s'", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1329 | PyNumber_Float(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | PyNumberMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | if (o == NULL) |
| 1334 | return null_error(); |
| 1335 | m = o->ob_type->tp_as_number; |
| 1336 | if (m && m->nb_float) { /* This should include subclasses of float */ |
| 1337 | PyObject *res = m->nb_float(o); |
| 1338 | if (res && !PyFloat_Check(res)) { |
| 1339 | PyErr_Format(PyExc_TypeError, |
| 1340 | "__float__ returned non-float (type %.200s)", |
| 1341 | res->ob_type->tp_name); |
| 1342 | Py_DECREF(res); |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | return res; |
| 1346 | } |
| 1347 | if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */ |
| 1348 | PyFloatObject *po = (PyFloatObject *)o; |
| 1349 | return PyFloat_FromDouble(po->ob_fval); |
| 1350 | } |
| 1351 | return PyFloat_FromString(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1354 | |
| 1355 | PyObject * |
| 1356 | PyNumber_ToBase(PyObject *n, int base) |
| 1357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | PyObject *res = NULL; |
| 1359 | PyObject *index = PyNumber_Index(n); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | if (!index) |
| 1362 | return NULL; |
| 1363 | if (PyLong_Check(index)) |
| 1364 | res = _PyLong_Format(index, base); |
| 1365 | else |
| 1366 | /* It should not be possible to get here, as |
| 1367 | PyNumber_Index already has a check for the same |
| 1368 | condition */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1369 | PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | Py_DECREF(index); |
| 1371 | return res; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1375 | /* Operations on sequences */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1376 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1377 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1378 | PySequence_Check(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | if (PyDict_Check(s)) |
| 1381 | return 0; |
| 1382 | return s != NULL && s->ob_type->tp_as_sequence && |
| 1383 | s->ob_type->tp_as_sequence->sq_item != NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1386 | Py_ssize_t |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1387 | PySequence_Size(PyObject *s) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | if (s == NULL) { |
| 1392 | null_error(); |
| 1393 | return -1; |
| 1394 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | m = s->ob_type->tp_as_sequence; |
| 1397 | if (m && m->sq_length) |
| 1398 | return m->sq_length(s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | type_error("object of type '%.200s' has no len()", s); |
| 1401 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1404 | #undef PySequence_Length |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1405 | Py_ssize_t |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1406 | PySequence_Length(PyObject *s) |
| 1407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | return PySequence_Size(s); |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1409 | } |
| 1410 | #define PySequence_Length PySequence_Size |
| 1411 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1412 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1413 | PySequence_Concat(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | if (s == NULL || o == NULL) |
| 1418 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | m = s->ob_type->tp_as_sequence; |
| 1421 | if (m && m->sq_concat) |
| 1422 | return m->sq_concat(s, o); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | /* Instances of user classes defining an __add__() method only |
| 1425 | have an nb_add slot, not an sq_concat slot. So we fall back |
| 1426 | to nb_add if both arguments appear to be sequences. */ |
| 1427 | if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1428 | PyObject *result = binary_op1(s, o, NB_SLOT(nb_add)); |
| 1429 | if (result != Py_NotImplemented) |
| 1430 | return result; |
| 1431 | Py_DECREF(result); |
| 1432 | } |
| 1433 | return type_error("'%.200s' object can't be concatenated", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1437 | PySequence_Repeat(PyObject *o, Py_ssize_t count) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | if (o == NULL) |
| 1442 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | m = o->ob_type->tp_as_sequence; |
| 1445 | if (m && m->sq_repeat) |
| 1446 | return m->sq_repeat(o, count); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | /* Instances of user classes defining a __mul__() method only |
| 1449 | have an nb_multiply slot, not an sq_repeat slot. so we fall back |
| 1450 | to nb_multiply if o appears to be a sequence. */ |
| 1451 | if (PySequence_Check(o)) { |
| 1452 | PyObject *n, *result; |
| 1453 | n = PyLong_FromSsize_t(count); |
| 1454 | if (n == NULL) |
| 1455 | return NULL; |
| 1456 | result = binary_op1(o, n, NB_SLOT(nb_multiply)); |
| 1457 | Py_DECREF(n); |
| 1458 | if (result != Py_NotImplemented) |
| 1459 | return result; |
| 1460 | Py_DECREF(result); |
| 1461 | } |
| 1462 | return type_error("'%.200s' object can't be repeated", o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1466 | PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
| 1467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | PySequenceMethods *m; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | if (s == NULL || o == NULL) |
| 1471 | return null_error(); |
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 | m = s->ob_type->tp_as_sequence; |
| 1474 | if (m && m->sq_inplace_concat) |
| 1475 | return m->sq_inplace_concat(s, o); |
| 1476 | if (m && m->sq_concat) |
| 1477 | return m->sq_concat(s, o); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1480 | PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add), |
| 1481 | NB_SLOT(nb_add)); |
| 1482 | if (result != Py_NotImplemented) |
| 1483 | return result; |
| 1484 | Py_DECREF(result); |
| 1485 | } |
| 1486 | return type_error("'%.200s' object can't be concatenated", s); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1490 | PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | PySequenceMethods *m; |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (o == NULL) |
| 1495 | return null_error(); |
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 | m = o->ob_type->tp_as_sequence; |
| 1498 | if (m && m->sq_inplace_repeat) |
| 1499 | return m->sq_inplace_repeat(o, count); |
| 1500 | if (m && m->sq_repeat) |
| 1501 | return m->sq_repeat(o, count); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | if (PySequence_Check(o)) { |
| 1504 | PyObject *n, *result; |
| 1505 | n = PyLong_FromSsize_t(count); |
| 1506 | if (n == NULL) |
| 1507 | return NULL; |
| 1508 | result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply), |
| 1509 | NB_SLOT(nb_multiply)); |
| 1510 | Py_DECREF(n); |
| 1511 | if (result != Py_NotImplemented) |
| 1512 | return result; |
| 1513 | Py_DECREF(result); |
| 1514 | } |
| 1515 | return type_error("'%.200s' object can't be repeated", o); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1519 | PySequence_GetItem(PyObject *s, Py_ssize_t i) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (s == NULL) |
| 1524 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | m = s->ob_type->tp_as_sequence; |
| 1527 | if (m && m->sq_item) { |
| 1528 | if (i < 0) { |
| 1529 | if (m->sq_length) { |
| 1530 | Py_ssize_t l = (*m->sq_length)(s); |
| 1531 | if (l < 0) |
| 1532 | return NULL; |
| 1533 | i += l; |
| 1534 | } |
| 1535 | } |
| 1536 | return m->sq_item(s, i); |
| 1537 | } |
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 | return type_error("'%.200s' object does not support indexing", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1543 | 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] | 1544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | if (!s) return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1550 | if (mp && mp->mp_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | PyObject *res; |
| 1552 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1553 | if (!slice) |
| 1554 | return NULL; |
| 1555 | res = mp->mp_subscript(s, slice); |
| 1556 | Py_DECREF(slice); |
| 1557 | return res; |
| 1558 | } |
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 | return type_error("'%.200s' object is unsliceable", s); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1564 | PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | PySequenceMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (s == NULL) { |
| 1569 | null_error(); |
| 1570 | return -1; |
| 1571 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | m = s->ob_type->tp_as_sequence; |
| 1574 | if (m && m->sq_ass_item) { |
| 1575 | if (i < 0) { |
| 1576 | if (m->sq_length) { |
| 1577 | Py_ssize_t l = (*m->sq_length)(s); |
| 1578 | if (l < 0) |
| 1579 | return -1; |
| 1580 | i += l; |
| 1581 | } |
| 1582 | } |
| 1583 | return m->sq_ass_item(s, i, o); |
| 1584 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | type_error("'%.200s' object does not support item assignment", s); |
| 1587 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1590 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1591 | PySequence_DelItem(PyObject *s, Py_ssize_t i) |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | PySequenceMethods *m; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | if (s == NULL) { |
| 1596 | null_error(); |
| 1597 | return -1; |
| 1598 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | m = s->ob_type->tp_as_sequence; |
| 1601 | if (m && m->sq_ass_item) { |
| 1602 | if (i < 0) { |
| 1603 | if (m->sq_length) { |
| 1604 | Py_ssize_t l = (*m->sq_length)(s); |
| 1605 | if (l < 0) |
| 1606 | return -1; |
| 1607 | i += l; |
| 1608 | } |
| 1609 | } |
| 1610 | return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 1611 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | type_error("'%.200s' object doesn't support item deletion", s); |
| 1614 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1617 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1618 | 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] | 1619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | PyMappingMethods *mp; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | if (s == NULL) { |
| 1623 | null_error(); |
| 1624 | return -1; |
| 1625 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1628 | if (mp && mp->mp_ass_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | int res; |
| 1630 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1631 | if (!slice) |
| 1632 | return -1; |
| 1633 | res = mp->mp_ass_subscript(s, slice, o); |
| 1634 | Py_DECREF(slice); |
| 1635 | return res; |
| 1636 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | type_error("'%.200s' object doesn't support slice assignment", s); |
| 1639 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1642 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1643 | 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] | 1644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | PyMappingMethods *mp; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | if (s == NULL) { |
| 1648 | null_error(); |
| 1649 | return -1; |
| 1650 | } |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | mp = s->ob_type->tp_as_mapping; |
Benjamin Peterson | 568867a | 2010-09-11 16:02:03 +0000 | [diff] [blame] | 1653 | if (mp && mp->mp_ass_subscript) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | int res; |
| 1655 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 1656 | if (!slice) |
| 1657 | return -1; |
| 1658 | res = mp->mp_ass_subscript(s, slice, NULL); |
| 1659 | Py_DECREF(slice); |
| 1660 | return res; |
| 1661 | } |
| 1662 | type_error("'%.200s' object doesn't support slice deletion", s); |
| 1663 | return -1; |
Guido van Rossum | 6cdc6f4 | 1996-08-21 17:41:54 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1666 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1667 | PySequence_Tuple(PyObject *v) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | PyObject *it; /* iter(v) */ |
| 1670 | Py_ssize_t n; /* guess for result tuple size */ |
| 1671 | PyObject *result = NULL; |
| 1672 | Py_ssize_t j; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | if (v == NULL) |
| 1675 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | /* Special-case the common tuple and list cases, for efficiency. */ |
| 1678 | if (PyTuple_CheckExact(v)) { |
| 1679 | /* Note that we can't know whether it's safe to return |
| 1680 | a tuple *subclass* instance as-is, hence the restriction |
| 1681 | to exact tuples here. In contrast, lists always make |
| 1682 | a copy, so there's no need for exactness below. */ |
| 1683 | Py_INCREF(v); |
| 1684 | return v; |
| 1685 | } |
| 1686 | if (PyList_Check(v)) |
| 1687 | return PyList_AsTuple(v); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | /* Get iterator. */ |
| 1690 | it = PyObject_GetIter(v); |
| 1691 | if (it == NULL) |
| 1692 | return NULL; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | /* Guess result size and allocate space. */ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 1695 | n = PyObject_LengthHint(v, 10); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | if (n == -1) |
| 1697 | goto Fail; |
| 1698 | result = PyTuple_New(n); |
| 1699 | if (result == NULL) |
| 1700 | goto Fail; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | /* Fill the tuple. */ |
| 1703 | for (j = 0; ; ++j) { |
| 1704 | PyObject *item = PyIter_Next(it); |
| 1705 | if (item == NULL) { |
| 1706 | if (PyErr_Occurred()) |
| 1707 | goto Fail; |
| 1708 | break; |
| 1709 | } |
| 1710 | if (j >= n) { |
| 1711 | Py_ssize_t oldn = n; |
| 1712 | /* The over-allocation strategy can grow a bit faster |
| 1713 | than for lists because unlike lists the |
| 1714 | over-allocation isn't permanent -- we reclaim |
| 1715 | the excess before the end of this routine. |
| 1716 | So, grow by ten and then add 25%. |
| 1717 | */ |
| 1718 | n += 10; |
| 1719 | n += n >> 2; |
| 1720 | if (n < oldn) { |
| 1721 | /* Check for overflow */ |
| 1722 | PyErr_NoMemory(); |
| 1723 | Py_DECREF(item); |
| 1724 | goto Fail; |
| 1725 | } |
| 1726 | if (_PyTuple_Resize(&result, n) != 0) { |
| 1727 | Py_DECREF(item); |
| 1728 | goto Fail; |
| 1729 | } |
| 1730 | } |
| 1731 | PyTuple_SET_ITEM(result, j, item); |
| 1732 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | /* Cut tuple back if guess was too large. */ |
| 1735 | if (j < n && |
| 1736 | _PyTuple_Resize(&result, j) != 0) |
| 1737 | goto Fail; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | Py_DECREF(it); |
| 1740 | return result; |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 1741 | |
| 1742 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | Py_XDECREF(result); |
| 1744 | Py_DECREF(it); |
| 1745 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1748 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1749 | PySequence_List(PyObject *v) |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | PyObject *result; /* result list */ |
| 1752 | PyObject *rv; /* return value from PyList_Extend */ |
Guido van Rossum | 4669fb4 | 1997-04-02 05:31:09 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | if (v == NULL) |
| 1755 | return null_error(); |
Guido van Rossum | 5dba9e8 | 1998-07-10 18:03:50 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | result = PyList_New(0); |
| 1758 | if (result == NULL) |
| 1759 | return NULL; |
Tim Peters | f553f89 | 2001-05-01 20:45:31 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | rv = _PyList_Extend((PyListObject *)result, v); |
| 1762 | if (rv == NULL) { |
| 1763 | Py_DECREF(result); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | Py_DECREF(rv); |
| 1767 | return result; |
Guido van Rossum | 3c5936a | 1996-12-05 21:51:24 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1770 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1771 | PySequence_Fast(PyObject *v, const char *m) |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | PyObject *it; |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | if (v == NULL) |
| 1776 | return null_error(); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) { |
| 1779 | Py_INCREF(v); |
| 1780 | return v; |
| 1781 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | it = PyObject_GetIter(v); |
| 1784 | if (it == NULL) { |
| 1785 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1786 | PyErr_SetString(PyExc_TypeError, m); |
| 1787 | return NULL; |
| 1788 | } |
Raymond Hettinger | 2fb7029 | 2004-01-11 23:26:51 +0000 | [diff] [blame] | 1789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | v = PySequence_List(it); |
| 1791 | Py_DECREF(it); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | return v; |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1796 | /* Iterate over seq. Result depends on the operation: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq. |
| 1798 | PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq; |
| 1799 | 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] | 1800 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error. |
| 1801 | */ |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1802 | Py_ssize_t |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1803 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | Py_ssize_t n; |
| 1806 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */ |
| 1807 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | if (seq == NULL || obj == NULL) { |
| 1810 | null_error(); |
| 1811 | return -1; |
| 1812 | } |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | it = PyObject_GetIter(seq); |
| 1815 | if (it == NULL) { |
| 1816 | type_error("argument of type '%.200s' is not iterable", seq); |
| 1817 | return -1; |
| 1818 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | n = wrapped = 0; |
| 1821 | for (;;) { |
| 1822 | int cmp; |
| 1823 | PyObject *item = PyIter_Next(it); |
| 1824 | if (item == NULL) { |
| 1825 | if (PyErr_Occurred()) |
| 1826 | goto Fail; |
| 1827 | break; |
| 1828 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | cmp = PyObject_RichCompareBool(obj, item, Py_EQ); |
| 1831 | Py_DECREF(item); |
| 1832 | if (cmp < 0) |
| 1833 | goto Fail; |
| 1834 | if (cmp > 0) { |
| 1835 | switch (operation) { |
| 1836 | case PY_ITERSEARCH_COUNT: |
| 1837 | if (n == PY_SSIZE_T_MAX) { |
| 1838 | PyErr_SetString(PyExc_OverflowError, |
| 1839 | "count exceeds C integer size"); |
| 1840 | goto Fail; |
| 1841 | } |
| 1842 | ++n; |
| 1843 | break; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | case PY_ITERSEARCH_INDEX: |
| 1846 | if (wrapped) { |
| 1847 | PyErr_SetString(PyExc_OverflowError, |
| 1848 | "index exceeds C integer size"); |
| 1849 | goto Fail; |
| 1850 | } |
| 1851 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | case PY_ITERSEARCH_CONTAINS: |
| 1854 | n = 1; |
| 1855 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | default: |
| 1858 | assert(!"unknown operation"); |
| 1859 | } |
| 1860 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | if (operation == PY_ITERSEARCH_INDEX) { |
| 1863 | if (n == PY_SSIZE_T_MAX) |
| 1864 | wrapped = 1; |
| 1865 | ++n; |
| 1866 | } |
| 1867 | } |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | if (operation != PY_ITERSEARCH_INDEX) |
| 1870 | goto Done; |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | PyErr_SetString(PyExc_ValueError, |
| 1873 | "sequence.index(x): x not in sequence"); |
| 1874 | /* fall into failure code */ |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1875 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | n = -1; |
| 1877 | /* fall through */ |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1878 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | Py_DECREF(it); |
| 1880 | return n; |
Tim Peters | 75f8e35 | 2001-05-05 11:33:43 +0000 | [diff] [blame] | 1881 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1884 | /* Return # of times o appears in s. */ |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1885 | Py_ssize_t |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 1886 | PySequence_Count(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1891 | /* 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] | 1892 | * Use sq_contains if possible, else defer to _PySequence_IterSearch(). |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1893 | */ |
| 1894 | int |
| 1895 | PySequence_Contains(PyObject *seq, PyObject *ob) |
| 1896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | Py_ssize_t result; |
| 1898 | PySequenceMethods *sqm = seq->ob_type->tp_as_sequence; |
| 1899 | if (sqm != NULL && sqm->sq_contains != NULL) |
| 1900 | return (*sqm->sq_contains)(seq, ob); |
| 1901 | result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
| 1902 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1905 | /* Backwards compatibility */ |
| 1906 | #undef PySequence_In |
| 1907 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1908 | PySequence_In(PyObject *w, PyObject *v) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | return PySequence_Contains(w, v); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Neal Norwitz | 1fc4b77 | 2006-03-04 18:49:58 +0000 | [diff] [blame] | 1913 | Py_ssize_t |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1914 | PySequence_Index(PyObject *s, PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1919 | /* Operations on mappings */ |
| 1920 | |
| 1921 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1922 | PyMapping_Check(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1923 | { |
Benjamin Peterson | 2199227 | 2011-12-28 12:01:31 -0600 | [diff] [blame] | 1924 | return o && o->ob_type->tp_as_mapping && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | o->ob_type->tp_as_mapping->mp_subscript; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1928 | Py_ssize_t |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1929 | PyMapping_Size(PyObject *o) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1931 | PyMappingMethods *m; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1933 | if (o == NULL) { |
| 1934 | null_error(); |
| 1935 | return -1; |
| 1936 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | m = o->ob_type->tp_as_mapping; |
| 1939 | if (m && m->mp_length) |
| 1940 | return m->mp_length(o); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | type_error("object of type '%.200s' has no len()", o); |
| 1943 | return -1; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1946 | #undef PyMapping_Length |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1947 | Py_ssize_t |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1948 | PyMapping_Length(PyObject *o) |
| 1949 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | return PyMapping_Size(o); |
Marc-André Lemburg | cf5f358 | 2000-07-17 09:22:55 +0000 | [diff] [blame] | 1951 | } |
| 1952 | #define PyMapping_Length PyMapping_Size |
| 1953 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1954 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1955 | PyMapping_GetItemString(PyObject *o, const char *key) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | PyObject *okey, *r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | if (key == NULL) |
| 1960 | return null_error(); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | okey = PyUnicode_FromString(key); |
| 1963 | if (okey == NULL) |
| 1964 | return NULL; |
| 1965 | r = PyObject_GetItem(o, okey); |
| 1966 | Py_DECREF(okey); |
| 1967 | return r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | int |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1971 | PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value) |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | PyObject *okey; |
| 1974 | int r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | if (key == NULL) { |
| 1977 | null_error(); |
| 1978 | return -1; |
| 1979 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | okey = PyUnicode_FromString(key); |
| 1982 | if (okey == NULL) |
| 1983 | return -1; |
| 1984 | r = PyObject_SetItem(o, okey, value); |
| 1985 | Py_DECREF(okey); |
| 1986 | return r; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | int |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1990 | PyMapping_HasKeyString(PyObject *o, const char *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | v = PyMapping_GetItemString(o, key); |
| 1995 | if (v) { |
| 1996 | Py_DECREF(v); |
| 1997 | return 1; |
| 1998 | } |
| 1999 | PyErr_Clear(); |
| 2000 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2003 | int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2004 | PyMapping_HasKey(PyObject *o, PyObject *key) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2005 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | PyObject *v; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | v = PyObject_GetItem(o, key); |
| 2009 | if (v) { |
| 2010 | Py_DECREF(v); |
| 2011 | return 1; |
| 2012 | } |
| 2013 | PyErr_Clear(); |
| 2014 | return 0; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2017 | PyObject * |
| 2018 | PyMapping_Keys(PyObject *o) |
| 2019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | PyObject *keys; |
| 2021 | PyObject *fast; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2022 | _Py_IDENTIFIER(keys); |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | if (PyDict_CheckExact(o)) |
| 2025 | return PyDict_Keys(o); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2026 | keys = _PyObject_CallMethodId(o, &PyId_keys, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (keys == NULL) |
| 2028 | return NULL; |
| 2029 | fast = PySequence_Fast(keys, "o.keys() are not iterable"); |
| 2030 | Py_DECREF(keys); |
| 2031 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | PyObject * |
| 2035 | PyMapping_Items(PyObject *o) |
| 2036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | PyObject *items; |
| 2038 | PyObject *fast; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2039 | _Py_IDENTIFIER(items); |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | if (PyDict_CheckExact(o)) |
| 2042 | return PyDict_Items(o); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2043 | items = _PyObject_CallMethodId(o, &PyId_items, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | if (items == NULL) |
| 2045 | return NULL; |
| 2046 | fast = PySequence_Fast(items, "o.items() are not iterable"); |
| 2047 | Py_DECREF(items); |
| 2048 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | PyObject * |
| 2052 | PyMapping_Values(PyObject *o) |
| 2053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | PyObject *values; |
| 2055 | PyObject *fast; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2056 | _Py_IDENTIFIER(values); |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | if (PyDict_CheckExact(o)) |
| 2059 | return PyDict_Values(o); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2060 | values = _PyObject_CallMethodId(o, &PyId_values, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | if (values == NULL) |
| 2062 | return NULL; |
| 2063 | fast = PySequence_Fast(values, "o.values() are not iterable"); |
| 2064 | Py_DECREF(values); |
| 2065 | return fast; |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2068 | /* Operations on callable objects */ |
| 2069 | |
| 2070 | /* XXX PyCallable_Check() is in object.c */ |
| 2071 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2072 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2073 | PyObject_CallObject(PyObject *o, PyObject *a) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2074 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | return PyEval_CallObjectWithKeywords(o, a, NULL); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2076 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2077 | |
| 2078 | PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2079 | PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2081 | ternaryfunc call; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | if ((call = func->ob_type->tp_call) != NULL) { |
| 2084 | PyObject *result; |
| 2085 | if (Py_EnterRecursiveCall(" while calling a Python object")) |
| 2086 | return NULL; |
| 2087 | result = (*call)(func, arg, kw); |
| 2088 | Py_LeaveRecursiveCall(); |
Victor Stinner | 3de5869 | 2013-07-15 17:50:07 +0200 | [diff] [blame] | 2089 | #ifdef NDEBUG |
| 2090 | if (result == NULL && !PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | PyErr_SetString( |
| 2092 | PyExc_SystemError, |
| 2093 | "NULL result without error in PyObject_Call"); |
Victor Stinner | 3de5869 | 2013-07-15 17:50:07 +0200 | [diff] [blame] | 2094 | } |
| 2095 | #else |
Victor Stinner | f243ee4 | 2013-07-16 01:02:12 +0200 | [diff] [blame] | 2096 | assert(result != NULL || PyErr_Occurred()); |
Victor Stinner | 3de5869 | 2013-07-15 17:50:07 +0200 | [diff] [blame] | 2097 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | return result; |
| 2099 | } |
| 2100 | PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable", |
| 2101 | func->ob_type->tp_name); |
| 2102 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2105 | static PyObject* |
| 2106 | call_function_tail(PyObject *callable, PyObject *args) |
| 2107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | PyObject *retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | if (args == NULL) |
| 2111 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | if (!PyTuple_Check(args)) { |
| 2114 | PyObject *a; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | a = PyTuple_New(1); |
| 2117 | if (a == NULL) { |
| 2118 | Py_DECREF(args); |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | PyTuple_SET_ITEM(a, 0, args); |
| 2122 | args = a; |
| 2123 | } |
| 2124 | retval = PyObject_Call(callable, args, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | Py_DECREF(args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2131 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2132 | PyObject_CallFunction(PyObject *callable, const char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | va_list va; |
| 2135 | PyObject *args; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | if (callable == NULL) |
| 2138 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | if (format && *format) { |
| 2141 | va_start(va, format); |
| 2142 | args = Py_VaBuildValue(format, va); |
| 2143 | va_end(va); |
| 2144 | } |
| 2145 | else |
| 2146 | args = PyTuple_New(0); |
Victor Stinner | 0b0c867 | 2013-10-29 19:29:52 +0100 | [diff] [blame] | 2147 | if (args == NULL) |
| 2148 | return NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | return call_function_tail(callable, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2151 | } |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2152 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2153 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2154 | _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | va_list va; |
| 2157 | PyObject *args; |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | if (callable == NULL) |
| 2160 | return null_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | if (format && *format) { |
| 2163 | va_start(va, format); |
| 2164 | args = _Py_VaBuildValue_SizeT(format, va); |
| 2165 | va_end(va); |
| 2166 | } |
| 2167 | else |
| 2168 | args = PyTuple_New(0); |
Guido van Rossum | cea1c8c | 1998-05-22 00:47:05 +0000 | [diff] [blame] | 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | return call_function_tail(callable, args); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2173 | static PyObject* |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2174 | 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] | 2175 | { |
| 2176 | PyObject *retval = NULL; |
| 2177 | PyObject *args; |
| 2178 | |
| 2179 | if (!PyCallable_Check(func)) { |
| 2180 | type_error("attribute of type '%.200s' is not callable", func); |
| 2181 | goto exit; |
| 2182 | } |
| 2183 | |
| 2184 | if (format && *format) { |
| 2185 | if (is_size_t) |
| 2186 | args = _Py_VaBuildValue_SizeT(format, va); |
| 2187 | else |
| 2188 | args = Py_VaBuildValue(format, va); |
| 2189 | } |
| 2190 | else |
| 2191 | args = PyTuple_New(0); |
| 2192 | |
| 2193 | retval = call_function_tail(func, args); |
| 2194 | |
| 2195 | exit: |
| 2196 | /* args gets consumed in call_function_tail */ |
| 2197 | Py_XDECREF(func); |
| 2198 | |
| 2199 | return retval; |
| 2200 | } |
| 2201 | |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2202 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2203 | PyObject_CallMethod(PyObject *o, const char *name, const char *format, ...) |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | va_list va; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | PyObject *func = NULL; |
| 2207 | PyObject *retval = NULL; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | if (o == NULL || name == NULL) |
| 2210 | return null_error(); |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | func = PyObject_GetAttrString(o, name); |
| 2213 | if (func == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | return 0; |
| 2215 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2216 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2217 | va_start(va, format); |
| 2218 | retval = callmethod(func, format, va, 0); |
| 2219 | va_end(va); |
| 2220 | return retval; |
| 2221 | } |
| 2222 | |
| 2223 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2224 | _PyObject_CallMethodId(PyObject *o, _Py_Identifier *name, |
| 2225 | const char *format, ...) |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2226 | { |
| 2227 | va_list va; |
| 2228 | PyObject *func = NULL; |
| 2229 | PyObject *retval = NULL; |
| 2230 | |
| 2231 | if (o == NULL || name == NULL) |
| 2232 | return null_error(); |
| 2233 | |
| 2234 | func = _PyObject_GetAttrId(o, name); |
| 2235 | if (func == NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2236 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | } |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2238 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2239 | va_start(va, format); |
| 2240 | retval = callmethod(func, format, va, 0); |
| 2241 | va_end(va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2246 | _PyObject_CallMethod_SizeT(PyObject *o, const char *name, |
| 2247 | const char *format, ...) |
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 | va_list va; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | PyObject *func = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2251 | PyObject *retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | if (o == NULL || name == NULL) |
| 2254 | return null_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | func = PyObject_GetAttrString(o, name); |
| 2257 | if (func == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | return 0; |
| 2259 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2260 | va_start(va, format); |
| 2261 | retval = callmethod(func, format, va, 1); |
| 2262 | va_end(va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | return retval; |
Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 2264 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2265 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2266 | PyObject * |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 2267 | _PyObject_CallMethodId_SizeT(PyObject *o, _Py_Identifier *name, |
| 2268 | const char *format, ...) |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2269 | { |
| 2270 | va_list va; |
| 2271 | PyObject *func = NULL; |
| 2272 | PyObject *retval; |
| 2273 | |
| 2274 | if (o == NULL || name == NULL) |
| 2275 | return null_error(); |
| 2276 | |
| 2277 | func = _PyObject_GetAttrId(o, name); |
| 2278 | if (func == NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
| 2281 | va_start(va, format); |
| 2282 | retval = callmethod(func, format, va, 1); |
| 2283 | va_end(va); |
| 2284 | return retval; |
| 2285 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2286 | |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2287 | static PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2288 | objargs_mktuple(va_list va) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | int i, n = 0; |
| 2291 | va_list countva; |
| 2292 | PyObject *result, *tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2293 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2294 | Py_VA_COPY(countva, va); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | while (((PyObject *)va_arg(countva, PyObject *)) != NULL) |
| 2297 | ++n; |
| 2298 | result = PyTuple_New(n); |
| 2299 | if (result != NULL && n > 0) { |
| 2300 | for (i = 0; i < n; ++i) { |
| 2301 | tmp = (PyObject *)va_arg(va, PyObject *); |
| 2302 | PyTuple_SET_ITEM(result, i, tmp); |
| 2303 | Py_INCREF(tmp); |
| 2304 | } |
| 2305 | } |
| 2306 | return result; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2310 | PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | PyObject *args, *tmp; |
| 2313 | va_list vargs; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | if (callable == NULL || name == NULL) |
| 2316 | return null_error(); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | callable = PyObject_GetAttr(callable, name); |
| 2319 | if (callable == NULL) |
| 2320 | return NULL; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | /* count the args */ |
| 2323 | va_start(vargs, name); |
| 2324 | args = objargs_mktuple(vargs); |
| 2325 | va_end(vargs); |
| 2326 | if (args == NULL) { |
| 2327 | Py_DECREF(callable); |
| 2328 | return NULL; |
| 2329 | } |
| 2330 | tmp = PyObject_Call(callable, args, NULL); |
| 2331 | Py_DECREF(args); |
| 2332 | Py_DECREF(callable); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | return tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | PyObject * |
Alexandre Vassalotti | 865eaa1 | 2013-05-02 10:44:04 -0700 | [diff] [blame] | 2338 | _PyObject_CallMethodIdObjArgs(PyObject *callable, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2339 | struct _Py_Identifier *name, ...) |
| 2340 | { |
| 2341 | PyObject *args, *tmp; |
| 2342 | va_list vargs; |
| 2343 | |
| 2344 | if (callable == NULL || name == NULL) |
| 2345 | return null_error(); |
| 2346 | |
| 2347 | callable = _PyObject_GetAttrId(callable, name); |
| 2348 | if (callable == NULL) |
| 2349 | return NULL; |
| 2350 | |
| 2351 | /* count the args */ |
| 2352 | va_start(vargs, name); |
| 2353 | args = objargs_mktuple(vargs); |
| 2354 | va_end(vargs); |
| 2355 | if (args == NULL) { |
| 2356 | Py_DECREF(callable); |
| 2357 | return NULL; |
| 2358 | } |
| 2359 | tmp = PyObject_Call(callable, args, NULL); |
| 2360 | Py_DECREF(args); |
| 2361 | Py_DECREF(callable); |
| 2362 | |
| 2363 | return tmp; |
| 2364 | } |
| 2365 | |
| 2366 | PyObject * |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 2367 | PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | PyObject *args, *tmp; |
| 2370 | va_list vargs; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | if (callable == NULL) |
| 2373 | return null_error(); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | /* count the args */ |
| 2376 | va_start(vargs, callable); |
| 2377 | args = objargs_mktuple(vargs); |
| 2378 | va_end(vargs); |
| 2379 | if (args == NULL) |
| 2380 | return NULL; |
| 2381 | tmp = PyObject_Call(callable, args, NULL); |
| 2382 | Py_DECREF(args); |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | return tmp; |
Fred Drake | b421b8c | 2001-10-26 16:21:32 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2388 | /* isinstance(), issubclass() */ |
| 2389 | |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2390 | /* abstract_get_bases() has logically 4 return states: |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 2391 | * |
Barry Warsaw | f16951c | 2002-04-23 22:45:44 +0000 | [diff] [blame] | 2392 | * 1. getattr(cls, '__bases__') could raise an AttributeError |
| 2393 | * 2. getattr(cls, '__bases__') could raise some other exception |
| 2394 | * 3. getattr(cls, '__bases__') could return a tuple |
| 2395 | * 4. getattr(cls, '__bases__') could return something other than a tuple |
| 2396 | * |
| 2397 | * Only state #3 is a non-error state and only it returns a non-NULL object |
| 2398 | * (it returns the retrieved tuple). |
| 2399 | * |
| 2400 | * Any raised AttributeErrors are masked by clearing the exception and |
| 2401 | * returning NULL. If an object other than a tuple comes out of __bases__, |
| 2402 | * then again, the return value is NULL. So yes, these two situations |
| 2403 | * produce exactly the same results: NULL is returned and no error is set. |
| 2404 | * |
| 2405 | * If some exception other than AttributeError is raised, then NULL is also |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | * 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] | 2407 | * exception to be propagated along. |
| 2408 | * |
| 2409 | * Callers are expected to test for PyErr_Occurred() when the return value |
| 2410 | * is NULL to decide whether a valid exception should be propagated or not. |
| 2411 | * When there's no exception to propagate, it's customary for the caller to |
| 2412 | * set a TypeError. |
| 2413 | */ |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2414 | static PyObject * |
| 2415 | abstract_get_bases(PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2416 | { |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2417 | _Py_IDENTIFIER(__bases__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | PyObject *bases; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | Py_ALLOW_RECURSION |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2421 | bases = _PyObject_GetAttrId(cls, &PyId___bases__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | Py_END_ALLOW_RECURSION |
| 2423 | if (bases == NULL) { |
| 2424 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2425 | PyErr_Clear(); |
| 2426 | return NULL; |
| 2427 | } |
| 2428 | if (!PyTuple_Check(bases)) { |
| 2429 | Py_DECREF(bases); |
| 2430 | return NULL; |
| 2431 | } |
| 2432 | return bases; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | |
| 2436 | static int |
| 2437 | abstract_issubclass(PyObject *derived, PyObject *cls) |
| 2438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | PyObject *bases = NULL; |
| 2440 | Py_ssize_t i, n; |
| 2441 | int r = 0; |
Neil Schemenauer | 6b47129 | 2001-10-18 03:18:43 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | while (1) { |
| 2444 | if (derived == cls) |
| 2445 | return 1; |
| 2446 | bases = abstract_get_bases(derived); |
| 2447 | if (bases == NULL) { |
| 2448 | if (PyErr_Occurred()) |
| 2449 | return -1; |
| 2450 | return 0; |
| 2451 | } |
| 2452 | n = PyTuple_GET_SIZE(bases); |
| 2453 | if (n == 0) { |
| 2454 | Py_DECREF(bases); |
| 2455 | return 0; |
| 2456 | } |
| 2457 | /* Avoid recursivity in the single inheritance case */ |
| 2458 | if (n == 1) { |
| 2459 | derived = PyTuple_GET_ITEM(bases, 0); |
| 2460 | Py_DECREF(bases); |
| 2461 | continue; |
| 2462 | } |
| 2463 | for (i = 0; i < n; i++) { |
| 2464 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
| 2465 | if (r != 0) |
| 2466 | break; |
| 2467 | } |
| 2468 | Py_DECREF(bases); |
| 2469 | return r; |
| 2470 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2473 | static int |
| 2474 | check_class(PyObject *cls, const char *error) |
| 2475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | PyObject *bases = abstract_get_bases(cls); |
| 2477 | if (bases == NULL) { |
| 2478 | /* Do not mask errors. */ |
| 2479 | if (!PyErr_Occurred()) |
| 2480 | PyErr_SetString(PyExc_TypeError, error); |
| 2481 | return 0; |
| 2482 | } |
| 2483 | Py_DECREF(bases); |
| 2484 | return -1; |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2487 | static int |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2488 | recursive_isinstance(PyObject *inst, PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 | PyObject *icls; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | int retval = 0; |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2492 | _Py_IDENTIFIER(__class__); |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 2493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 | if (PyType_Check(cls)) { |
| 2495 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
| 2496 | if (retval == 0) { |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2497 | PyObject *c = _PyObject_GetAttrId(inst, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | if (c == NULL) { |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2499 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2500 | PyErr_Clear(); |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2501 | else |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2502 | retval = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | } |
| 2504 | else { |
| 2505 | if (c != (PyObject *)(inst->ob_type) && |
| 2506 | PyType_Check(c)) |
| 2507 | retval = PyType_IsSubtype( |
| 2508 | (PyTypeObject *)c, |
| 2509 | (PyTypeObject *)cls); |
| 2510 | Py_DECREF(c); |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | else { |
| 2515 | if (!check_class(cls, |
Benjamin Peterson | e893af5 | 2010-06-28 19:43:42 +0000 | [diff] [blame] | 2516 | "isinstance() arg 2 must be a type or tuple of types")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 | return -1; |
Benjamin Peterson | 9fc9bf4 | 2012-03-20 23:26:41 -0400 | [diff] [blame] | 2518 | icls = _PyObject_GetAttrId(inst, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | if (icls == NULL) { |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2520 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2521 | PyErr_Clear(); |
Benjamin Peterson | 72288d4 | 2010-11-20 17:21:08 +0000 | [diff] [blame] | 2522 | else |
R. David Murray | 6bb9989 | 2010-11-20 16:33:30 +0000 | [diff] [blame] | 2523 | retval = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | } |
| 2525 | else { |
| 2526 | retval = abstract_issubclass(icls, cls); |
| 2527 | Py_DECREF(icls); |
| 2528 | } |
| 2529 | } |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | return retval; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | int |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2535 | PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 2536 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2537 | _Py_IDENTIFIER(__instancecheck__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | PyObject *checker; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | /* Quick test for an exact match */ |
| 2541 | if (Py_TYPE(inst) == (PyTypeObject *)cls) |
| 2542 | return 1; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | if (PyTuple_Check(cls)) { |
| 2545 | Py_ssize_t i; |
| 2546 | Py_ssize_t n; |
| 2547 | int r = 0; |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | if (Py_EnterRecursiveCall(" in __instancecheck__")) |
| 2550 | return -1; |
| 2551 | n = PyTuple_GET_SIZE(cls); |
| 2552 | for (i = 0; i < n; ++i) { |
| 2553 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 2554 | r = PyObject_IsInstance(inst, item); |
| 2555 | if (r != 0) |
| 2556 | /* either found it, or got an error */ |
| 2557 | break; |
| 2558 | } |
| 2559 | Py_LeaveRecursiveCall(); |
| 2560 | return r; |
| 2561 | } |
Benjamin Peterson | 88fe5f9 | 2009-05-16 21:55:24 +0000 | [diff] [blame] | 2562 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2563 | checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | if (checker != NULL) { |
| 2565 | PyObject *res; |
| 2566 | int ok = -1; |
| 2567 | if (Py_EnterRecursiveCall(" in __instancecheck__")) { |
| 2568 | Py_DECREF(checker); |
| 2569 | return ok; |
| 2570 | } |
| 2571 | res = PyObject_CallFunctionObjArgs(checker, inst, NULL); |
| 2572 | Py_LeaveRecursiveCall(); |
| 2573 | Py_DECREF(checker); |
| 2574 | if (res != NULL) { |
| 2575 | ok = PyObject_IsTrue(res); |
| 2576 | Py_DECREF(res); |
| 2577 | } |
| 2578 | return ok; |
| 2579 | } |
| 2580 | else if (PyErr_Occurred()) |
| 2581 | return -1; |
| 2582 | return recursive_isinstance(inst, cls); |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | static int |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2586 | recursive_issubclass(PyObject *derived, PyObject *cls) |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2588 | if (PyType_Check(cls) && PyType_Check(derived)) { |
| 2589 | /* Fast path (non-recursive) */ |
| 2590 | return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls); |
| 2591 | } |
| 2592 | if (!check_class(derived, |
| 2593 | "issubclass() arg 1 must be a class")) |
| 2594 | return -1; |
| 2595 | if (!check_class(cls, |
| 2596 | "issubclass() arg 2 must be a class" |
| 2597 | " or tuple of classes")) |
| 2598 | return -1; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | return abstract_issubclass(derived, cls); |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2601 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2602 | |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2603 | int |
| 2604 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 2605 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2606 | _Py_IDENTIFIER(__subclasscheck__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | PyObject *checker; |
Georg Brandl | dcfe8e4 | 2009-05-17 08:22:45 +0000 | [diff] [blame] | 2608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2609 | if (PyTuple_Check(cls)) { |
| 2610 | Py_ssize_t i; |
| 2611 | Py_ssize_t n; |
| 2612 | int r = 0; |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | if (Py_EnterRecursiveCall(" in __subclasscheck__")) |
| 2615 | return -1; |
| 2616 | n = PyTuple_GET_SIZE(cls); |
| 2617 | for (i = 0; i < n; ++i) { |
| 2618 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 2619 | r = PyObject_IsSubclass(derived, item); |
| 2620 | if (r != 0) |
| 2621 | /* either found it, or got an error */ |
| 2622 | break; |
| 2623 | } |
| 2624 | Py_LeaveRecursiveCall(); |
| 2625 | return r; |
| 2626 | } |
Benjamin Peterson | 88fe5f9 | 2009-05-16 21:55:24 +0000 | [diff] [blame] | 2627 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2628 | checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | if (checker != NULL) { |
| 2630 | PyObject *res; |
| 2631 | int ok = -1; |
| 2632 | if (Py_EnterRecursiveCall(" in __subclasscheck__")) { |
| 2633 | Py_DECREF(checker); |
| 2634 | return ok; |
| 2635 | } |
| 2636 | res = PyObject_CallFunctionObjArgs(checker, derived, NULL); |
| 2637 | Py_LeaveRecursiveCall(); |
| 2638 | Py_DECREF(checker); |
| 2639 | if (res != NULL) { |
| 2640 | ok = PyObject_IsTrue(res); |
| 2641 | Py_DECREF(res); |
| 2642 | } |
| 2643 | return ok; |
| 2644 | } |
| 2645 | else if (PyErr_Occurred()) |
| 2646 | return -1; |
| 2647 | return recursive_issubclass(derived, cls); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | int |
| 2651 | _PyObject_RealIsInstance(PyObject *inst, PyObject *cls) |
| 2652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | return recursive_isinstance(inst, cls); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | int |
| 2657 | _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls) |
| 2658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | return recursive_issubclass(derived, cls); |
Brett Cannon | 4f65331 | 2004-03-20 22:52:14 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2663 | PyObject * |
| 2664 | PyObject_GetIter(PyObject *o) |
| 2665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | PyTypeObject *t = o->ob_type; |
| 2667 | getiterfunc f = NULL; |
| 2668 | f = t->tp_iter; |
| 2669 | if (f == NULL) { |
| 2670 | if (PySequence_Check(o)) |
| 2671 | return PySeqIter_New(o); |
| 2672 | return type_error("'%.200s' object is not iterable", o); |
| 2673 | } |
| 2674 | else { |
| 2675 | PyObject *res = (*f)(o); |
| 2676 | if (res != NULL && !PyIter_Check(res)) { |
| 2677 | PyErr_Format(PyExc_TypeError, |
| 2678 | "iter() returned non-iterator " |
| 2679 | "of type '%.100s'", |
| 2680 | res->ob_type->tp_name); |
| 2681 | Py_DECREF(res); |
| 2682 | res = NULL; |
| 2683 | } |
| 2684 | return res; |
| 2685 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2688 | /* Return next item. |
| 2689 | * If an error occurs, return NULL. PyErr_Occurred() will be true. |
| 2690 | * If the iteration terminates normally, return NULL and clear the |
| 2691 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred() |
| 2692 | * will be false. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | * Else return the next object. PyErr_Occurred() will be false. |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2694 | */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2695 | PyObject * |
| 2696 | PyIter_Next(PyObject *iter) |
| 2697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | PyObject *result; |
| 2699 | result = (*iter->ob_type->tp_iternext)(iter); |
| 2700 | if (result == NULL && |
| 2701 | PyErr_Occurred() && |
| 2702 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2703 | PyErr_Clear(); |
| 2704 | return result; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2705 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2706 | |
| 2707 | |
| 2708 | /* |
| 2709 | * Flatten a sequence of bytes() objects into a C array of |
| 2710 | * NULL terminated string pointers with a NULL char* terminating the array. |
| 2711 | * (ie: an argv or env list) |
| 2712 | * |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2713 | * Memory allocated for the returned list is allocated using PyMem_Malloc() |
| 2714 | * and MUST be freed by _Py_FreeCharPArray(). |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2715 | */ |
| 2716 | char *const * |
| 2717 | _PySequence_BytesToCharpArray(PyObject* self) |
| 2718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | char **array; |
| 2720 | Py_ssize_t i, argc; |
| 2721 | PyObject *item = NULL; |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2722 | Py_ssize_t size; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | argc = PySequence_Size(self); |
| 2725 | if (argc == -1) |
| 2726 | return NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2727 | |
Stefan Krah | 7cacd2e | 2012-08-21 08:16:09 +0200 | [diff] [blame] | 2728 | assert(argc >= 0); |
| 2729 | |
| 2730 | if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) { |
| 2731 | PyErr_NoMemory(); |
| 2732 | return NULL; |
| 2733 | } |
| 2734 | |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2735 | array = PyMem_Malloc((argc + 1) * sizeof(char *)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | if (array == NULL) { |
| 2737 | PyErr_NoMemory(); |
| 2738 | return NULL; |
| 2739 | } |
| 2740 | for (i = 0; i < argc; ++i) { |
| 2741 | char *data; |
| 2742 | item = PySequence_GetItem(self, i); |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 2743 | if (item == NULL) { |
| 2744 | /* NULL terminate before freeing. */ |
| 2745 | array[i] = NULL; |
| 2746 | goto fail; |
| 2747 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | data = PyBytes_AsString(item); |
| 2749 | if (data == NULL) { |
| 2750 | /* NULL terminate before freeing. */ |
| 2751 | array[i] = NULL; |
| 2752 | goto fail; |
| 2753 | } |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2754 | size = PyBytes_GET_SIZE(item) + 1; |
| 2755 | array[i] = PyMem_Malloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | if (!array[i]) { |
| 2757 | PyErr_NoMemory(); |
| 2758 | goto fail; |
| 2759 | } |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2760 | memcpy(array[i], data, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | Py_DECREF(item); |
| 2762 | } |
| 2763 | array[argc] = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | return array; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2766 | |
| 2767 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | Py_XDECREF(item); |
| 2769 | _Py_FreeCharPArray(array); |
| 2770 | return NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | |
| 2774 | /* Free's a NULL terminated char** array of C strings. */ |
| 2775 | void |
| 2776 | _Py_FreeCharPArray(char *const array[]) |
| 2777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | Py_ssize_t i; |
| 2779 | for (i = 0; array[i] != NULL; ++i) { |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2780 | PyMem_Free(array[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | } |
Victor Stinner | 0e2d3cf | 2013-07-07 17:22:41 +0200 | [diff] [blame] | 2782 | PyMem_Free((void*)array); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2783 | } |