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