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