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