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