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