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