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