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