Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 9 | |
| 10 | /* Support type attribute cache */ |
| 11 | |
| 12 | /* The cache can keep references to the names alive for longer than |
| 13 | they normally would. This is why the maximum size is limited to |
| 14 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 15 | strings are used as attribute names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | #define MCACHE_MAX_ATTR_SIZE 100 |
| 17 | #define MCACHE_SIZE_EXP 10 |
| 18 | #define MCACHE_HASH(version, name_hash) \ |
| 19 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 20 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 21 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 23 | ((PyASCIIObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 24 | #define MCACHE_CACHEABLE_NAME(name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | PyUnicode_CheckExact(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 26 | PyUnicode_READY(name) != -1 && \ |
| 27 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 28 | |
| 29 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | unsigned int version; |
| 31 | PyObject *name; /* reference to exactly a str or None */ |
| 32 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 36 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 38 | _Py_IDENTIFIER(__class__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 39 | _Py_IDENTIFIER(__dict__); |
| 40 | _Py_IDENTIFIER(__doc__); |
| 41 | _Py_IDENTIFIER(__getitem__); |
| 42 | _Py_IDENTIFIER(__getattribute__); |
| 43 | _Py_IDENTIFIER(__hash__); |
| 44 | _Py_IDENTIFIER(__module__); |
| 45 | _Py_IDENTIFIER(__name__); |
| 46 | _Py_IDENTIFIER(__new__); |
| 47 | |
| 48 | static PyObject * |
| 49 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 50 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 51 | unsigned int |
| 52 | PyType_ClearCache(void) |
| 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | Py_ssize_t i; |
| 55 | unsigned int cur_version_tag = next_version_tag - 1; |
| 56 | |
| 57 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 58 | method_cache[i].version = 0; |
| 59 | Py_CLEAR(method_cache[i].name); |
| 60 | method_cache[i].value = NULL; |
| 61 | } |
| 62 | next_version_tag = 0; |
| 63 | /* mark all version tags as invalid */ |
| 64 | PyType_Modified(&PyBaseObject_Type); |
| 65 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 66 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 67 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 68 | void |
| 69 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 70 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | /* Invalidate any cached data for the specified type and all |
| 72 | subclasses. This function is called after the base |
| 73 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 78 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 79 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 82 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 85 | type (so it must first clear it on all subclasses). The |
| 86 | tp_version_tag value is meaningless unless this flag is set. |
| 87 | We don't assign new version tags eagerly, but only as |
| 88 | needed. |
| 89 | */ |
| 90 | PyObject *raw, *ref; |
| 91 | Py_ssize_t i, n; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 94 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | raw = type->tp_subclasses; |
| 97 | if (raw != NULL) { |
| 98 | n = PyList_GET_SIZE(raw); |
| 99 | for (i = 0; i < n; i++) { |
| 100 | ref = PyList_GET_ITEM(raw, i); |
| 101 | ref = PyWeakref_GET_OBJECT(ref); |
| 102 | if (ref != Py_None) { |
| 103 | PyType_Modified((PyTypeObject *)ref); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void |
| 111 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | /* |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 113 | Check that all base classes or elements of the MRO of type are |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | able to be cached. This function is called after the base |
| 115 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 118 | has a custom MRO that includes a type which is not officially |
| 119 | super type. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | Called from mro_internal, which will subsequently be called on |
| 122 | each subclass when their mro is recursively updated. |
| 123 | */ |
| 124 | Py_ssize_t i, n; |
| 125 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 128 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | n = PyTuple_GET_SIZE(bases); |
| 131 | for (i = 0; i < n; i++) { |
| 132 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 133 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 134 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 135 | assert(PyType_Check(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 139 | !PyType_IsSubtype(type, cls)) { |
| 140 | clear = 1; |
| 141 | break; |
| 142 | } |
| 143 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | if (clear) |
| 146 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 147 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int |
| 151 | assign_version_tag(PyTypeObject *type) |
| 152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | /* Ensure that the tp_version_tag is valid and set |
| 154 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 155 | must first be done on all super classes. Return 0 if this |
| 156 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 157 | */ |
| 158 | Py_ssize_t i, n; |
| 159 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 162 | return 1; |
| 163 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 164 | return 0; |
| 165 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 166 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | type->tp_version_tag = next_version_tag++; |
| 169 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | if (type->tp_version_tag == 0) { |
| 172 | /* wrap-around or just starting Python - clear the whole |
| 173 | cache by filling names with references to Py_None. |
| 174 | Values are also set to NULL for added protection, as they |
| 175 | are borrowed reference */ |
| 176 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 177 | method_cache[i].value = NULL; |
| 178 | Py_XDECREF(method_cache[i].name); |
| 179 | method_cache[i].name = Py_None; |
| 180 | Py_INCREF(Py_None); |
| 181 | } |
| 182 | /* mark all version tags as invalid */ |
| 183 | PyType_Modified(&PyBaseObject_Type); |
| 184 | return 1; |
| 185 | } |
| 186 | bases = type->tp_bases; |
| 187 | n = PyTuple_GET_SIZE(bases); |
| 188 | for (i = 0; i < n; i++) { |
| 189 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 190 | assert(PyType_Check(b)); |
| 191 | if (!assign_version_tag((PyTypeObject *)b)) |
| 192 | return 0; |
| 193 | } |
| 194 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 195 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 199 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 200 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 201 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 203 | {"__weakrefoffset__", T_LONG, |
| 204 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 205 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 206 | {"__dictoffset__", T_LONG, |
| 207 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 208 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 209 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 210 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 212 | static int |
| 213 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 214 | { |
| 215 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 216 | PyErr_Format(PyExc_TypeError, |
| 217 | "can't set %s.%s", type->tp_name, name); |
| 218 | return 0; |
| 219 | } |
| 220 | if (!value) { |
| 221 | PyErr_Format(PyExc_TypeError, |
| 222 | "can't delete %s.%s", type->tp_name, name); |
| 223 | return 0; |
| 224 | } |
| 225 | return 1; |
| 226 | } |
| 227 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 229 | type_name(PyTypeObject *type, void *context) |
| 230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 234 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | Py_INCREF(et->ht_name); |
| 237 | return et->ht_name; |
| 238 | } |
| 239 | else { |
| 240 | s = strrchr(type->tp_name, '.'); |
| 241 | if (s == NULL) |
| 242 | s = type->tp_name; |
| 243 | else |
| 244 | s++; |
| 245 | return PyUnicode_FromString(s); |
| 246 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 249 | static PyObject * |
| 250 | type_qualname(PyTypeObject *type, void *context) |
| 251 | { |
| 252 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 253 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 254 | Py_INCREF(et->ht_qualname); |
| 255 | return et->ht_qualname; |
| 256 | } |
| 257 | else { |
| 258 | return type_name(type, context); |
| 259 | } |
| 260 | } |
| 261 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 262 | static int |
| 263 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | PyHeapTypeObject* et; |
| 266 | char *tp_name; |
| 267 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 268 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 269 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (!PyUnicode_Check(value)) { |
| 272 | PyErr_Format(PyExc_TypeError, |
| 273 | "can only assign string to %s.__name__, not '%s'", |
| 274 | type->tp_name, Py_TYPE(value)->tp_name); |
| 275 | return -1; |
| 276 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | /* Check absence of null characters */ |
| 279 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 280 | if (tmp == NULL) |
| 281 | return -1; |
| 282 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 283 | Py_DECREF(tmp); |
| 284 | PyErr_Format(PyExc_ValueError, |
| 285 | "__name__ must not contain null bytes"); |
| 286 | return -1; |
| 287 | } |
| 288 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | tp_name = _PyUnicode_AsString(value); |
| 291 | if (tp_name == NULL) |
| 292 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | Py_DECREF(et->ht_name); |
| 299 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | type->tp_name = tp_name; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 306 | static int |
| 307 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 308 | { |
| 309 | PyHeapTypeObject* et; |
| 310 | |
| 311 | if (!PyUnicode_Check(value)) { |
| 312 | PyErr_Format(PyExc_TypeError, |
| 313 | "can only assign string to %s.__qualname__, not '%s'", |
| 314 | type->tp_name, Py_TYPE(value)->tp_name); |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | et = (PyHeapTypeObject*)type; |
| 319 | Py_INCREF(value); |
| 320 | Py_DECREF(et->ht_qualname); |
| 321 | et->ht_qualname = value; |
| 322 | return 0; |
| 323 | } |
| 324 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 325 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 326 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | PyObject *mod; |
| 329 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 332 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | if (!mod) { |
| 334 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 335 | return 0; |
| 336 | } |
| 337 | Py_XINCREF(mod); |
| 338 | return mod; |
| 339 | } |
| 340 | else { |
| 341 | s = strrchr(type->tp_name, '.'); |
| 342 | if (s != NULL) |
| 343 | return PyUnicode_FromStringAndSize( |
| 344 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 345 | return PyUnicode_FromString("builtins"); |
| 346 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 349 | static int |
| 350 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 351 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 352 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 356 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 357 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 360 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 361 | type_abstractmethods(PyTypeObject *type, void *context) |
| 362 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 363 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 364 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 365 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 366 | if (type != &PyType_Type) |
| 367 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (!mod) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 369 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
| 372 | Py_XINCREF(mod); |
| 373 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int |
| 377 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | /* __abstractmethods__ should only be set once on a type, in |
| 380 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 381 | special to update subclasses. |
| 382 | */ |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 383 | int res; |
| 384 | if (value != NULL) { |
| 385 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value); |
| 386 | } |
| 387 | else { |
| 388 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__"); |
| 389 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | if (res == 0) { |
| 395 | PyType_Modified(type); |
| 396 | if (value && PyObject_IsTrue(value)) { |
| 397 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 398 | } |
| 399 | else { |
| 400 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 401 | } |
| 402 | } |
| 403 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 407 | type_get_bases(PyTypeObject *type, void *context) |
| 408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | Py_INCREF(type->tp_bases); |
| 410 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static PyTypeObject *best_base(PyObject *); |
| 414 | static int mro_internal(PyTypeObject *); |
| 415 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 416 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 417 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 418 | static void update_all_slots(PyTypeObject *); |
| 419 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 420 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 421 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 423 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 425 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 426 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 427 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | PyTypeObject *subclass; |
| 430 | PyObject *ref, *subclasses, *old_mro; |
| 431 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | subclasses = type->tp_subclasses; |
| 434 | if (subclasses == NULL) |
| 435 | return 0; |
| 436 | assert(PyList_Check(subclasses)); |
| 437 | n = PyList_GET_SIZE(subclasses); |
| 438 | for (i = 0; i < n; i++) { |
| 439 | ref = PyList_GET_ITEM(subclasses, i); |
| 440 | assert(PyWeakref_CheckRef(ref)); |
| 441 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 442 | assert(subclass != NULL); |
| 443 | if ((PyObject *)subclass == Py_None) |
| 444 | continue; |
| 445 | assert(PyType_Check(subclass)); |
| 446 | old_mro = subclass->tp_mro; |
| 447 | if (mro_internal(subclass) < 0) { |
| 448 | subclass->tp_mro = old_mro; |
| 449 | return -1; |
| 450 | } |
| 451 | else { |
| 452 | PyObject* tuple; |
| 453 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 454 | Py_DECREF(old_mro); |
| 455 | if (!tuple) |
| 456 | return -1; |
| 457 | if (PyList_Append(temp, tuple) < 0) |
| 458 | return -1; |
| 459 | Py_DECREF(tuple); |
| 460 | } |
| 461 | if (mro_subclasses(subclass, temp) < 0) |
| 462 | return -1; |
| 463 | } |
| 464 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static int |
| 468 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | Py_ssize_t i; |
| 471 | int r = 0; |
| 472 | PyObject *ob, *temp; |
| 473 | PyTypeObject *new_base, *old_base; |
| 474 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 475 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 476 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | if (!PyTuple_Check(value)) { |
| 479 | PyErr_Format(PyExc_TypeError, |
| 480 | "can only assign tuple to %s.__bases__, not %s", |
| 481 | type->tp_name, Py_TYPE(value)->tp_name); |
| 482 | return -1; |
| 483 | } |
| 484 | if (PyTuple_GET_SIZE(value) == 0) { |
| 485 | PyErr_Format(PyExc_TypeError, |
| 486 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 487 | type->tp_name); |
| 488 | return -1; |
| 489 | } |
| 490 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 491 | ob = PyTuple_GET_ITEM(value, i); |
| 492 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 493 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 494 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 495 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 496 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | } |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 498 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 499 | PyErr_SetString(PyExc_TypeError, |
| 500 | "a __bases__ item causes an inheritance cycle"); |
| 501 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 506 | |
Benjamin Peterson | ab3c1c1 | 2012-04-01 18:48:02 -0400 | [diff] [blame] | 507 | if (!new_base) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 511 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | Py_INCREF(new_base); |
| 514 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | old_bases = type->tp_bases; |
| 517 | old_base = type->tp_base; |
| 518 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | type->tp_bases = value; |
| 521 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | if (mro_internal(type) < 0) { |
| 524 | goto bail; |
| 525 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | temp = PyList_New(0); |
| 528 | if (!temp) |
| 529 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | if (r < 0) { |
| 534 | for (i = 0; i < PyList_Size(temp); i++) { |
| 535 | PyTypeObject* cls; |
| 536 | PyObject* mro; |
| 537 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 538 | "", 2, 2, &cls, &mro); |
| 539 | Py_INCREF(mro); |
| 540 | ob = cls->tp_mro; |
| 541 | cls->tp_mro = mro; |
| 542 | Py_DECREF(ob); |
| 543 | } |
| 544 | Py_DECREF(temp); |
| 545 | goto bail; |
| 546 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | /* any base that was in __bases__ but now isn't, we |
| 551 | need to remove |type| from its tp_subclasses. |
| 552 | conversely, any class now in __bases__ that wasn't |
| 553 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* for now, sod that: just remove from all old_bases, |
| 556 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 559 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 560 | if (PyType_Check(ob)) { |
| 561 | remove_subclass( |
| 562 | (PyTypeObject*)ob, type); |
| 563 | } |
| 564 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 567 | ob = PyTuple_GET_ITEM(value, i); |
| 568 | if (PyType_Check(ob)) { |
| 569 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 570 | r = -1; |
| 571 | } |
| 572 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | Py_DECREF(old_bases); |
| 577 | Py_DECREF(old_base); |
| 578 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 581 | |
| 582 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_DECREF(type->tp_bases); |
| 584 | Py_DECREF(type->tp_base); |
| 585 | if (type->tp_mro != old_mro) { |
| 586 | Py_DECREF(type->tp_mro); |
| 587 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | type->tp_bases = old_bases; |
| 590 | type->tp_base = old_base; |
| 591 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 597 | type_dict(PyTypeObject *type, void *context) |
| 598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | if (type->tp_dict == NULL) { |
| 600 | Py_INCREF(Py_None); |
| 601 | return Py_None; |
| 602 | } |
| 603 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 606 | static PyObject * |
| 607 | type_get_doc(PyTypeObject *type, void *context) |
| 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | PyObject *result; |
| 610 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 611 | return PyUnicode_FromString(type->tp_doc); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 612 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | if (result == NULL) { |
| 614 | result = Py_None; |
| 615 | Py_INCREF(result); |
| 616 | } |
| 617 | else if (Py_TYPE(result)->tp_descr_get) { |
| 618 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 619 | (PyObject *)type); |
| 620 | } |
| 621 | else { |
| 622 | Py_INCREF(result); |
| 623 | } |
| 624 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 627 | static int |
| 628 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 629 | { |
| 630 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 631 | return -1; |
| 632 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 633 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 634 | } |
| 635 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 636 | static PyObject * |
| 637 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 640 | case -1: |
| 641 | return NULL; |
| 642 | case 0: |
| 643 | Py_RETURN_FALSE; |
| 644 | default: |
| 645 | Py_RETURN_TRUE; |
| 646 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 651 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 654 | case -1: |
| 655 | return NULL; |
| 656 | case 0: |
| 657 | Py_RETURN_FALSE; |
| 658 | default: |
| 659 | Py_RETURN_TRUE; |
| 660 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 663 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 664 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 666 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 668 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 669 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 670 | (setter)type_set_abstractmethods, NULL}, |
| 671 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 672 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 676 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 677 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | mod = type_module(type, NULL); |
| 682 | if (mod == NULL) |
| 683 | PyErr_Clear(); |
| 684 | else if (!PyUnicode_Check(mod)) { |
| 685 | Py_DECREF(mod); |
| 686 | mod = NULL; |
| 687 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 688 | name = type_qualname(type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | if (name == NULL) |
| 690 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 693 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 694 | else |
| 695 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | Py_XDECREF(mod); |
| 698 | Py_DECREF(name); |
| 699 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 702 | static PyObject * |
| 703 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (type->tp_new == NULL) { |
| 708 | PyErr_Format(PyExc_TypeError, |
| 709 | "cannot create '%.100s' instances", |
| 710 | type->tp_name); |
| 711 | return NULL; |
| 712 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | obj = type->tp_new(type, args, kwds); |
| 715 | if (obj != NULL) { |
| 716 | /* Ugly exception: when the call was type(something), |
| 717 | don't call tp_init on the result. */ |
| 718 | if (type == &PyType_Type && |
| 719 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 720 | (kwds == NULL || |
| 721 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 722 | return obj; |
| 723 | /* If the returned object is not an instance of type, |
| 724 | it won't be initialized. */ |
| 725 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 726 | return obj; |
| 727 | type = Py_TYPE(obj); |
| 728 | if (type->tp_init != NULL && |
| 729 | type->tp_init(obj, args, kwds) < 0) { |
| 730 | Py_DECREF(obj); |
| 731 | obj = NULL; |
| 732 | } |
| 733 | } |
| 734 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 738 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | PyObject *obj; |
| 741 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 742 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | if (PyType_IS_GC(type)) |
| 745 | obj = _PyObject_GC_Malloc(size); |
| 746 | else |
| 747 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | if (obj == NULL) |
| 750 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 755 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | if (type->tp_itemsize == 0) |
| 758 | PyObject_INIT(obj, type); |
| 759 | else |
| 760 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | if (PyType_IS_GC(type)) |
| 763 | _PyObject_GC_TRACK(obj); |
| 764 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | PyObject * |
| 768 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 773 | /* Helpers for subtyping */ |
| 774 | |
| 775 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 776 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | Py_ssize_t i, n; |
| 779 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | n = Py_SIZE(type); |
| 782 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 783 | for (i = 0; i < n; i++, mp++) { |
| 784 | if (mp->type == T_OBJECT_EX) { |
| 785 | char *addr = (char *)self + mp->offset; |
| 786 | PyObject *obj = *(PyObject **)addr; |
| 787 | if (obj != NULL) { |
| 788 | int err = visit(obj, arg); |
| 789 | if (err) |
| 790 | return err; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 798 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | PyTypeObject *type, *base; |
| 801 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | /* Find the nearest base with a different tp_traverse, |
| 804 | and traverse slots while we're at it */ |
| 805 | type = Py_TYPE(self); |
| 806 | base = type; |
| 807 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 808 | if (Py_SIZE(base)) { |
| 809 | int err = traverse_slots(base, self, visit, arg); |
| 810 | if (err) |
| 811 | return err; |
| 812 | } |
| 813 | base = base->tp_base; |
| 814 | assert(base); |
| 815 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 818 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 819 | if (dictptr && *dictptr) |
| 820 | Py_VISIT(*dictptr); |
| 821 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 824 | /* For a heaptype, the instances count as references |
| 825 | to the type. Traverse the type so the collector |
| 826 | can find cycles involving this link. */ |
| 827 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | if (basetraverse) |
| 830 | return basetraverse(self, visit, arg); |
| 831 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void |
| 835 | clear_slots(PyTypeObject *type, PyObject *self) |
| 836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | Py_ssize_t i, n; |
| 838 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | n = Py_SIZE(type); |
| 841 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 842 | for (i = 0; i < n; i++, mp++) { |
| 843 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 844 | char *addr = (char *)self + mp->offset; |
| 845 | PyObject *obj = *(PyObject **)addr; |
| 846 | if (obj != NULL) { |
| 847 | *(PyObject **)addr = NULL; |
| 848 | Py_DECREF(obj); |
| 849 | } |
| 850 | } |
| 851 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static int |
| 855 | subtype_clear(PyObject *self) |
| 856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | PyTypeObject *type, *base; |
| 858 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | /* Find the nearest base with a different tp_clear |
| 861 | and clear slots while we're at it */ |
| 862 | type = Py_TYPE(self); |
| 863 | base = type; |
| 864 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 865 | if (Py_SIZE(base)) |
| 866 | clear_slots(base, self); |
| 867 | base = base->tp_base; |
| 868 | assert(base); |
| 869 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 870 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 871 | /* Clear the instance dict (if any), to break cycles involving only |
| 872 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 873 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 874 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 875 | if (dictptr && *dictptr) |
| 876 | Py_CLEAR(*dictptr); |
| 877 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (baseclear) |
| 880 | return baseclear(self); |
| 881 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 882 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 883 | |
| 884 | static void |
| 885 | subtype_dealloc(PyObject *self) |
| 886 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | PyTypeObject *type, *base; |
| 888 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | /* Extract the type; we expect it to be a heap type */ |
| 891 | type = Py_TYPE(self); |
| 892 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | if (!PyType_IS_GC(type)) { |
| 897 | /* It's really rare to find a dynamic type that doesn't have |
| 898 | GC; it can only happen when deriving from 'object' and not |
| 899 | adding any slots or instance variables. This allows |
| 900 | certain simplifications: there's no need to call |
| 901 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | /* Maybe call finalizer; exit early if resurrected */ |
| 904 | if (type->tp_del) { |
| 905 | type->tp_del(self); |
| 906 | if (self->ob_refcnt > 0) |
| 907 | return; |
| 908 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | /* Find the nearest base with a different tp_dealloc */ |
| 911 | base = type; |
| 912 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 913 | assert(Py_SIZE(base) == 0); |
| 914 | base = base->tp_base; |
| 915 | assert(base); |
| 916 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | /* Extract the type again; tp_del may have changed it */ |
| 919 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | /* Call the base tp_dealloc() */ |
| 922 | assert(basedealloc); |
| 923 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | /* Can't reference self beyond this point */ |
| 926 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | /* Done */ |
| 929 | return; |
| 930 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 935 | /* See explanation at end of function for full disclosure */ |
| 936 | PyObject_GC_UnTrack(self); |
| 937 | ++_PyTrash_delete_nesting; |
| 938 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 939 | --_PyTrash_delete_nesting; |
| 940 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 941 | * (if any, and whether directly here or indirectly in something we |
| 942 | * call) may trigger GC, and if self is tracked at that point, it |
| 943 | * will look like trash to GC and GC will try to delete self again. |
| 944 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | /* Find the nearest base with a different tp_dealloc */ |
| 947 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 948 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | base = base->tp_base; |
| 950 | assert(base); |
| 951 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 954 | the finalizer (__del__), clearing slots, or clearing the instance |
| 955 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 958 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* Maybe call finalizer; exit early if resurrected */ |
| 961 | if (type->tp_del) { |
| 962 | _PyObject_GC_TRACK(self); |
| 963 | type->tp_del(self); |
| 964 | if (self->ob_refcnt > 0) |
| 965 | goto endlabel; /* resurrected */ |
| 966 | else |
| 967 | _PyObject_GC_UNTRACK(self); |
| 968 | /* New weakrefs could be created during the finalizer call. |
| 969 | If this occurs, clear them out without calling their |
| 970 | finalizers since they might rely on part of the object |
| 971 | being finalized that has already been destroyed. */ |
| 972 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 973 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 974 | PyWeakReference **list = (PyWeakReference **) \ |
| 975 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 976 | while (*list) |
| 977 | _PyWeakref_ClearRef(*list); |
| 978 | } |
| 979 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 982 | base = type; |
| 983 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 984 | if (Py_SIZE(base)) |
| 985 | clear_slots(base, self); |
| 986 | base = base->tp_base; |
| 987 | assert(base); |
| 988 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | /* If we added a dict, DECREF it */ |
| 991 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 992 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 993 | if (dictptr != NULL) { |
| 994 | PyObject *dict = *dictptr; |
| 995 | if (dict != NULL) { |
| 996 | Py_DECREF(dict); |
| 997 | *dictptr = NULL; |
| 998 | } |
| 999 | } |
| 1000 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | /* Extract the type again; tp_del may have changed it */ |
| 1003 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | /* Call the base tp_dealloc(); first retrack self if |
| 1006 | * basedealloc knows about gc. |
| 1007 | */ |
| 1008 | if (PyType_IS_GC(base)) |
| 1009 | _PyObject_GC_TRACK(self); |
| 1010 | assert(basedealloc); |
| 1011 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | /* Can't reference self beyond this point */ |
| 1014 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1015 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1016 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | ++_PyTrash_delete_nesting; |
| 1018 | Py_TRASHCAN_SAFE_END(self); |
| 1019 | --_PyTrash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1026 | For one, this explains why there must be a call to GC-untrack |
| 1027 | before the trashcan begin macro. Without understanding the |
| 1028 | trashcan code, the answers to the following questions don't make |
| 1029 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1032 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | A. In the case that the base class is GC-aware, the base class |
| 1035 | probably GC-untracks the object. If it does that using the |
| 1036 | UNTRACK macro, this will crash when the object is already |
| 1037 | untracked. Because we don't know what the base class does, the |
| 1038 | only safe thing is to make sure the object is tracked when we |
| 1039 | call the base class dealloc. But... The trashcan begin macro |
| 1040 | requires that the object is *untracked* before it is called. So |
| 1041 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | GC untrack |
| 1044 | trashcan begin |
| 1045 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | Q. Why did the last question say "immediately GC-track again"? |
| 1048 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1051 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1052 | (which can happen if any weakref callback gets invoked), it |
| 1053 | looks like trash to gc too, and gc also tries to delete self |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1054 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | Q. Why the bizarre (net-zero) manipulation of |
| 1058 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1061 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | - the trashcan limit is not yet reached, so the trashcan level |
| 1068 | is incremented and the code between trashcan begin and end is |
| 1069 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | - this destroys much of the object's contents, including its |
| 1072 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | - basedealloc() is called; this is really list_dealloc(), or |
| 1075 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | - the trashcan limit is now reached, so the object is put on the |
| 1078 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | - later, the trashcan code starts deleting the objects from its |
| 1087 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | - at the very least (if the destroyed slots and __dict__ don't |
| 1092 | cause problems) the object's type gets decref'ed a second |
| 1093 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | The remedy is to make sure that if the code between trashcan |
| 1096 | begin and end in subtype_dealloc() is called, the code between |
| 1097 | trashcan begin and end in basedealloc() will also be called. |
| 1098 | This is done by decrementing the level after passing into the |
| 1099 | trashcan block, and incrementing it just before leaving the |
| 1100 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | But now it's possible that a chain of objects consisting solely |
| 1103 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1104 | the trashcan mechanism completely: the decremented level means |
| 1105 | that the effective level never reaches the limit. Therefore, we |
| 1106 | *increment* the level *before* entering the trashcan block, and |
| 1107 | matchingly decrement it after leaving. This means the trashcan |
| 1108 | code will trigger a little early, but that's no big deal. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | Q. Are there any live examples of code in need of all this |
| 1111 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1114 | compiled in debug mode) before the trashcan level manipulations |
| 1115 | were added. For more discussion, see SF patches 581742, 575073 |
| 1116 | and bug 574207. |
| 1117 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1120 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1121 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1122 | /* type test with subclassing support */ |
| 1123 | |
| 1124 | int |
| 1125 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | mro = a->tp_mro; |
| 1130 | if (mro != NULL) { |
| 1131 | /* Deal with multiple inheritance without recursion |
| 1132 | by walking the MRO tuple */ |
| 1133 | Py_ssize_t i, n; |
| 1134 | assert(PyTuple_Check(mro)); |
| 1135 | n = PyTuple_GET_SIZE(mro); |
| 1136 | for (i = 0; i < n; i++) { |
| 1137 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1138 | return 1; |
| 1139 | } |
| 1140 | return 0; |
| 1141 | } |
| 1142 | else { |
| 1143 | /* a is not completely initilized yet; follow tp_base */ |
| 1144 | do { |
| 1145 | if (a == b) |
| 1146 | return 1; |
| 1147 | a = a->tp_base; |
| 1148 | } while (a != NULL); |
| 1149 | return b == &PyBaseObject_Type; |
| 1150 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1153 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1154 | without looking in the instance dictionary |
| 1155 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1157 | the method name as a C string, and the address of a |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1158 | static variable used to cache the interned Python string. |
| 1159 | |
| 1160 | Two variants: |
| 1161 | |
| 1162 | - lookup_maybe() returns NULL without raising an exception |
| 1163 | when the _PyType_Lookup() call fails; |
| 1164 | |
| 1165 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1166 | |
| 1167 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1168 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1169 | |
| 1170 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1171 | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1172 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1173 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1174 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1175 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1176 | if (res != NULL) { |
| 1177 | descrgetfunc f; |
| 1178 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1179 | Py_INCREF(res); |
| 1180 | else |
| 1181 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1182 | } |
| 1183 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1186 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1187 | lookup_method(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1188 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1189 | PyObject *res = lookup_maybe(self, attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | if (res == NULL && !PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1191 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1195 | PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1196 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1197 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1198 | return lookup_maybe(self, attrid); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1201 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1203 | as lookup_method to cache the interned name string object. */ |
| 1204 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1205 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1206 | call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | va_list va; |
| 1209 | PyObject *args, *func = 0, *retval; |
| 1210 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1211 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1212 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (func == NULL) { |
| 1214 | va_end(va); |
| 1215 | if (!PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1216 | PyErr_SetObject(PyExc_AttributeError, nameid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | return NULL; |
| 1218 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | if (format && *format) |
| 1221 | args = Py_VaBuildValue(format, va); |
| 1222 | else |
| 1223 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | if (args == NULL) |
| 1228 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | assert(PyTuple_Check(args)); |
| 1231 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | Py_DECREF(args); |
| 1234 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1240 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1241 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1242 | call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | va_list va; |
| 1245 | PyObject *args, *func = 0, *retval; |
| 1246 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1247 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1248 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | if (func == NULL) { |
| 1250 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1251 | if (!PyErr_Occurred()) |
| 1252 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | return NULL; |
| 1254 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | if (format && *format) |
| 1257 | args = Py_VaBuildValue(format, va); |
| 1258 | else |
| 1259 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | if (args == NULL) |
| 1264 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | assert(PyTuple_Check(args)); |
| 1267 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | Py_DECREF(args); |
| 1270 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1275 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1276 | Method resolution order algorithm C3 described in |
| 1277 | "A Monotonic Superclass Linearization for Dylan", |
| 1278 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1279 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1280 | (OOPSLA 1996) |
| 1281 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1282 | Some notes about the rules implied by C3: |
| 1283 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1284 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1285 | It isn't legal to repeat a class in a list of base classes. |
| 1286 | |
| 1287 | The next three properties are the 3 constraints in "C3". |
| 1288 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1289 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1290 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1291 | subclasses of C. |
| 1292 | |
| 1293 | Monotonicity. |
| 1294 | The MRO of a class must be an extension without reordering of the |
| 1295 | MRO of each of its superclasses. |
| 1296 | |
| 1297 | Extended Precedence Graph (EPG). |
| 1298 | Linearization is consistent if there is a path in the EPG from |
| 1299 | each class to all its successors in the linearization. See |
| 1300 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1301 | */ |
| 1302 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1303 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1304 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | Py_ssize_t j, size; |
| 1306 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | for (j = whence+1; j < size; j++) { |
| 1309 | if (PyList_GET_ITEM(list, j) == o) |
| 1310 | return 1; |
| 1311 | } |
| 1312 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1315 | static PyObject * |
| 1316 | class_name(PyObject *cls) |
| 1317 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1318 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | if (name == NULL) { |
| 1320 | PyErr_Clear(); |
| 1321 | Py_XDECREF(name); |
| 1322 | name = PyObject_Repr(cls); |
| 1323 | } |
| 1324 | if (name == NULL) |
| 1325 | return NULL; |
| 1326 | if (!PyUnicode_Check(name)) { |
| 1327 | Py_DECREF(name); |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static int |
| 1334 | check_duplicates(PyObject *list) |
| 1335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | Py_ssize_t i, j, n; |
| 1337 | /* Let's use a quadratic time algorithm, |
| 1338 | assuming that the bases lists is short. |
| 1339 | */ |
| 1340 | n = PyList_GET_SIZE(list); |
| 1341 | for (i = 0; i < n; i++) { |
| 1342 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1343 | for (j = i + 1; j < n; j++) { |
| 1344 | if (PyList_GET_ITEM(list, j) == o) { |
| 1345 | o = class_name(o); |
| 1346 | if (o != NULL) { |
| 1347 | PyErr_Format(PyExc_TypeError, |
| 1348 | "duplicate base class %U", |
| 1349 | o); |
| 1350 | Py_DECREF(o); |
| 1351 | } else { |
| 1352 | PyErr_SetString(PyExc_TypeError, |
| 1353 | "duplicate base class"); |
| 1354 | } |
| 1355 | return -1; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | /* Raise a TypeError for an MRO order disagreement. |
| 1363 | |
| 1364 | It's hard to produce a good error message. In the absence of better |
| 1365 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | to be put next into the MRO. There is some conflict between the |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1367 | order in which they should be put in the MRO, but it's hard to |
| 1368 | diagnose what constraint can't be satisfied. |
| 1369 | */ |
| 1370 | |
| 1371 | static void |
| 1372 | set_mro_error(PyObject *to_merge, int *remain) |
| 1373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | Py_ssize_t i, n, off, to_merge_size; |
| 1375 | char buf[1000]; |
| 1376 | PyObject *k, *v; |
| 1377 | PyObject *set = PyDict_New(); |
| 1378 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1381 | for (i = 0; i < to_merge_size; i++) { |
| 1382 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1383 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1384 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1385 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1386 | Py_DECREF(set); |
| 1387 | return; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1394 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | i = 0; |
| 1396 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1397 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1398 | char *name_str; |
| 1399 | if (name != NULL) { |
| 1400 | name_str = _PyUnicode_AsString(name); |
| 1401 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1402 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1403 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1404 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1405 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | Py_XDECREF(name); |
| 1407 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1408 | buf[off++] = ','; |
| 1409 | buf[off] = '\0'; |
| 1410 | } |
| 1411 | } |
| 1412 | PyErr_SetString(PyExc_TypeError, buf); |
| 1413 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1416 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1417 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1419 | int *remain; |
| 1420 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | /* remain stores an index into each sublist of to_merge. |
| 1425 | remain[i] is the index of the next base in to_merge[i] |
| 1426 | that is not included in acc. |
| 1427 | */ |
| 1428 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1429 | if (remain == NULL) |
| 1430 | return -1; |
| 1431 | for (i = 0; i < to_merge_size; i++) |
| 1432 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1433 | |
| 1434 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | empty_cnt = 0; |
| 1436 | for (i = 0; i < to_merge_size; i++) { |
| 1437 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1442 | empty_cnt++; |
| 1443 | continue; |
| 1444 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | The input sequences alone can determine the choice. |
| 1449 | If not, choose the class which appears in the MRO |
| 1450 | of the earliest direct superclass of the new class. |
| 1451 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1454 | for (j = 0; j < to_merge_size; j++) { |
| 1455 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1456 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1457 | goto skip; /* continue outer loop */ |
| 1458 | } |
| 1459 | } |
| 1460 | ok = PyList_Append(acc, candidate); |
| 1461 | if (ok < 0) { |
| 1462 | PyMem_Free(remain); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | for (j = 0; j < to_merge_size; j++) { |
| 1466 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1467 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1468 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1469 | remain[j]++; |
| 1470 | } |
| 1471 | } |
| 1472 | goto again; |
| 1473 | skip: ; |
| 1474 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | if (empty_cnt == to_merge_size) { |
| 1477 | PyMem_FREE(remain); |
| 1478 | return 0; |
| 1479 | } |
| 1480 | set_mro_error(to_merge, remain); |
| 1481 | PyMem_FREE(remain); |
| 1482 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1485 | static PyObject * |
| 1486 | mro_implementation(PyTypeObject *type) |
| 1487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | Py_ssize_t i, n; |
| 1489 | int ok; |
| 1490 | PyObject *bases, *result; |
| 1491 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | if (type->tp_dict == NULL) { |
| 1494 | if (PyType_Ready(type) < 0) |
| 1495 | return NULL; |
| 1496 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | /* Find a superclass linearization that honors the constraints |
| 1499 | of the explicit lists of bases and the constraints implied by |
| 1500 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | to_merge is a list of lists, where each list is a superclass |
| 1503 | linearization implied by a base class. The last element of |
| 1504 | to_merge is the declared list of bases. |
| 1505 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | bases = type->tp_bases; |
| 1508 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | to_merge = PyList_New(n+1); |
| 1511 | if (to_merge == NULL) |
| 1512 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | for (i = 0; i < n; i++) { |
| 1515 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1516 | PyObject *parentMRO; |
| 1517 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1518 | if (parentMRO == NULL) { |
| 1519 | Py_DECREF(to_merge); |
| 1520 | return NULL; |
| 1521 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1524 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | bases_aslist = PySequence_List(bases); |
| 1527 | if (bases_aslist == NULL) { |
| 1528 | Py_DECREF(to_merge); |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | /* This is just a basic sanity check. */ |
| 1532 | if (check_duplicates(bases_aslist) < 0) { |
| 1533 | Py_DECREF(to_merge); |
| 1534 | Py_DECREF(bases_aslist); |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1540 | if (result == NULL) { |
| 1541 | Py_DECREF(to_merge); |
| 1542 | return NULL; |
| 1543 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | ok = pmerge(result, to_merge); |
| 1546 | Py_DECREF(to_merge); |
| 1547 | if (ok < 0) { |
| 1548 | Py_DECREF(result); |
| 1549 | return NULL; |
| 1550 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1556 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | static int |
| 1564 | mro_internal(PyTypeObject *type) |
| 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | PyObject *mro, *result, *tuple; |
| 1567 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | if (Py_TYPE(type) == &PyType_Type) { |
| 1570 | result = mro_implementation(type); |
| 1571 | } |
| 1572 | else { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1573 | _Py_IDENTIFIER(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | checkit = 1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1575 | mro = lookup_method((PyObject *)type, &PyId_mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | if (mro == NULL) |
| 1577 | return -1; |
| 1578 | result = PyObject_CallObject(mro, NULL); |
| 1579 | Py_DECREF(mro); |
| 1580 | } |
| 1581 | if (result == NULL) |
| 1582 | return -1; |
| 1583 | tuple = PySequence_Tuple(result); |
| 1584 | Py_DECREF(result); |
| 1585 | if (tuple == NULL) |
| 1586 | return -1; |
| 1587 | if (checkit) { |
| 1588 | Py_ssize_t i, len; |
| 1589 | PyObject *cls; |
| 1590 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | for (i = 0; i < len; i++) { |
| 1597 | PyTypeObject *t; |
| 1598 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1599 | if (!PyType_Check(cls)) { |
| 1600 | PyErr_Format(PyExc_TypeError, |
| 1601 | "mro() returned a non-class ('%.500s')", |
| 1602 | Py_TYPE(cls)->tp_name); |
| 1603 | Py_DECREF(tuple); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | t = (PyTypeObject*)cls; |
| 1607 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1608 | PyErr_Format(PyExc_TypeError, |
| 1609 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1610 | t->tp_name); |
| 1611 | Py_DECREF(tuple); |
| 1612 | return -1; |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1619 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | from the custom MRO */ |
| 1621 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | /* Calculate the best base amongst multiple base classes. |
| 1630 | This is the first one that's on the path to the "solid base". */ |
| 1631 | |
| 1632 | static PyTypeObject * |
| 1633 | best_base(PyObject *bases) |
| 1634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | Py_ssize_t i, n; |
| 1636 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1637 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | assert(PyTuple_Check(bases)); |
| 1640 | n = PyTuple_GET_SIZE(bases); |
| 1641 | assert(n > 0); |
| 1642 | base = NULL; |
| 1643 | winner = NULL; |
| 1644 | for (i = 0; i < n; i++) { |
| 1645 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1646 | if (!PyType_Check(base_proto)) { |
| 1647 | PyErr_SetString( |
| 1648 | PyExc_TypeError, |
| 1649 | "bases must be types"); |
| 1650 | return NULL; |
| 1651 | } |
| 1652 | base_i = (PyTypeObject *)base_proto; |
| 1653 | if (base_i->tp_dict == NULL) { |
| 1654 | if (PyType_Ready(base_i) < 0) |
| 1655 | return NULL; |
| 1656 | } |
| 1657 | candidate = solid_base(base_i); |
| 1658 | if (winner == NULL) { |
| 1659 | winner = candidate; |
| 1660 | base = base_i; |
| 1661 | } |
| 1662 | else if (PyType_IsSubtype(winner, candidate)) |
| 1663 | ; |
| 1664 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1665 | winner = candidate; |
| 1666 | base = base_i; |
| 1667 | } |
| 1668 | else { |
| 1669 | PyErr_SetString( |
| 1670 | PyExc_TypeError, |
| 1671 | "multiple bases have " |
| 1672 | "instance lay-out conflict"); |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1676 | assert (base != NULL); |
| 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | static int |
| 1682 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | size_t t_size = type->tp_basicsize; |
| 1685 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1688 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1689 | /* If itemsize is involved, stricter rules */ |
| 1690 | return t_size != b_size || |
| 1691 | type->tp_itemsize != base->tp_itemsize; |
| 1692 | } |
| 1693 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1694 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1695 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1696 | t_size -= sizeof(PyObject *); |
| 1697 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1698 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1699 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1700 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static PyTypeObject * |
| 1706 | solid_base(PyTypeObject *type) |
| 1707 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | if (type->tp_base) |
| 1711 | base = solid_base(type->tp_base); |
| 1712 | else |
| 1713 | base = &PyBaseObject_Type; |
| 1714 | if (extra_ivars(type, base)) |
| 1715 | return type; |
| 1716 | else |
| 1717 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1720 | static void object_dealloc(PyObject *); |
| 1721 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1722 | static int update_slot(PyTypeObject *, PyObject *); |
| 1723 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1724 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1727 | * inherited from various builtin types. The builtin base usually provides |
| 1728 | * its own __dict__ descriptor, so we use that when we can. |
| 1729 | */ |
| 1730 | static PyTypeObject * |
| 1731 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | while (type->tp_base != NULL) { |
| 1734 | if (type->tp_dictoffset != 0 && |
| 1735 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1736 | return type; |
| 1737 | type = type->tp_base; |
| 1738 | } |
| 1739 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | static PyObject * |
| 1743 | get_dict_descriptor(PyTypeObject *type) |
| 1744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1746 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1747 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1749 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | static void |
| 1755 | raise_dict_descr_error(PyObject *obj) |
| 1756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | PyErr_Format(PyExc_TypeError, |
| 1758 | "this __dict__ descriptor does not support " |
| 1759 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1762 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1763 | subtype_dict(PyObject *obj, void *context) |
| 1764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1768 | if (base != NULL) { |
| 1769 | descrgetfunc func; |
| 1770 | PyObject *descr = get_dict_descriptor(base); |
| 1771 | if (descr == NULL) { |
| 1772 | raise_dict_descr_error(obj); |
| 1773 | return NULL; |
| 1774 | } |
| 1775 | func = Py_TYPE(descr)->tp_descr_get; |
| 1776 | if (func == NULL) { |
| 1777 | raise_dict_descr_error(obj); |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1781 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1782 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1785 | static int |
| 1786 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1787 | { |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1788 | PyObject *dict, **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1792 | if (base != NULL) { |
| 1793 | descrsetfunc func; |
| 1794 | PyObject *descr = get_dict_descriptor(base); |
| 1795 | if (descr == NULL) { |
| 1796 | raise_dict_descr_error(obj); |
| 1797 | return -1; |
| 1798 | } |
| 1799 | func = Py_TYPE(descr)->tp_descr_set; |
| 1800 | if (func == NULL) { |
| 1801 | raise_dict_descr_error(obj); |
| 1802 | return -1; |
| 1803 | } |
| 1804 | return func(descr, obj, value); |
| 1805 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1806 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | dictptr = _PyObject_GetDictPtr(obj); |
| 1808 | if (dictptr == NULL) { |
| 1809 | PyErr_SetString(PyExc_AttributeError, |
| 1810 | "This object has no __dict__"); |
| 1811 | return -1; |
| 1812 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 1813 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | PyErr_Format(PyExc_TypeError, |
| 1815 | "__dict__ must be set to a dictionary, " |
| 1816 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1817 | return -1; |
| 1818 | } |
| 1819 | dict = *dictptr; |
| 1820 | Py_XINCREF(value); |
| 1821 | *dictptr = value; |
| 1822 | Py_XDECREF(dict); |
| 1823 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1826 | static PyObject * |
| 1827 | subtype_getweakref(PyObject *obj, void *context) |
| 1828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | PyObject **weaklistptr; |
| 1830 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1833 | PyErr_SetString(PyExc_AttributeError, |
| 1834 | "This object has no __weakref__"); |
| 1835 | return NULL; |
| 1836 | } |
| 1837 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1838 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1839 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1840 | weaklistptr = (PyObject **) |
| 1841 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1842 | if (*weaklistptr == NULL) |
| 1843 | result = Py_None; |
| 1844 | else |
| 1845 | result = *weaklistptr; |
| 1846 | Py_INCREF(result); |
| 1847 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1850 | /* Three variants on the subtype_getsets list. */ |
| 1851 | |
| 1852 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | {"__dict__", subtype_dict, subtype_setdict, |
| 1854 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1855 | {"__weakref__", subtype_getweakref, NULL, |
| 1856 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1857 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1858 | }; |
| 1859 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1860 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | {"__dict__", subtype_dict, subtype_setdict, |
| 1862 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1863 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1864 | }; |
| 1865 | |
| 1866 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | {"__weakref__", subtype_getweakref, NULL, |
| 1868 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1869 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1870 | }; |
| 1871 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1872 | static int |
| 1873 | valid_identifier(PyObject *s) |
| 1874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | if (!PyUnicode_Check(s)) { |
| 1876 | PyErr_Format(PyExc_TypeError, |
| 1877 | "__slots__ items must be strings, not '%.200s'", |
| 1878 | Py_TYPE(s)->tp_name); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | if (!PyUnicode_IsIdentifier(s)) { |
| 1882 | PyErr_SetString(PyExc_TypeError, |
| 1883 | "__slots__ must be identifiers"); |
| 1884 | return 0; |
| 1885 | } |
| 1886 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1889 | /* Forward */ |
| 1890 | static int |
| 1891 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1892 | |
| 1893 | static int |
| 1894 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1895 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | assert(args != NULL && PyTuple_Check(args)); |
| 1899 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1902 | PyErr_SetString(PyExc_TypeError, |
| 1903 | "type.__init__() takes no keyword arguments"); |
| 1904 | return -1; |
| 1905 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | if (args != NULL && PyTuple_Check(args) && |
| 1908 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1909 | PyErr_SetString(PyExc_TypeError, |
| 1910 | "type.__init__() takes 1 or 3 arguments"); |
| 1911 | return -1; |
| 1912 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | /* Call object.__init__(self) now. */ |
| 1915 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1916 | args = PyTuple_GetSlice(args, 0, 0); |
| 1917 | res = object_init(cls, args, NULL); |
| 1918 | Py_DECREF(args); |
| 1919 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 1922 | long |
| 1923 | PyType_GetFlags(PyTypeObject *type) |
| 1924 | { |
| 1925 | return type->tp_flags; |
| 1926 | } |
| 1927 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 1928 | /* Determine the most derived metatype. */ |
| 1929 | PyTypeObject * |
| 1930 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 1931 | { |
| 1932 | Py_ssize_t i, nbases; |
| 1933 | PyTypeObject *winner; |
| 1934 | PyObject *tmp; |
| 1935 | PyTypeObject *tmptype; |
| 1936 | |
| 1937 | /* Determine the proper metatype to deal with this, |
| 1938 | and check for metatype conflicts while we're at it. |
| 1939 | Note that if some other metatype wins to contract, |
| 1940 | it's possible that its instances are not types. */ |
| 1941 | |
| 1942 | nbases = PyTuple_GET_SIZE(bases); |
| 1943 | winner = metatype; |
| 1944 | for (i = 0; i < nbases; i++) { |
| 1945 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1946 | tmptype = Py_TYPE(tmp); |
| 1947 | if (PyType_IsSubtype(winner, tmptype)) |
| 1948 | continue; |
| 1949 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1950 | winner = tmptype; |
| 1951 | continue; |
| 1952 | } |
| 1953 | /* else: */ |
| 1954 | PyErr_SetString(PyExc_TypeError, |
| 1955 | "metaclass conflict: " |
| 1956 | "the metaclass of a derived class " |
| 1957 | "must be a (non-strict) subclass " |
| 1958 | "of the metaclasses of all its bases"); |
| 1959 | return NULL; |
| 1960 | } |
| 1961 | return winner; |
| 1962 | } |
| 1963 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1964 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1965 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1966 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1967 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1969 | PyObject *qualname, *slots = NULL, *tmp, *newslots; |
| 1970 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | PyHeapTypeObject *et; |
| 1972 | PyMemberDef *mp; |
| 1973 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1974 | int j, may_add_dict, may_add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1975 | _Py_IDENTIFIER(__qualname__); |
| 1976 | _Py_IDENTIFIER(__slots__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | assert(args != NULL && PyTuple_Check(args)); |
| 1979 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | /* Special case: type(x) should return x->ob_type */ |
| 1982 | { |
| 1983 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1984 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1987 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 1988 | Py_INCREF(Py_TYPE(x)); |
| 1989 | return (PyObject *) Py_TYPE(x); |
| 1990 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1993 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1994 | a msg saying type() needs exactly 3. */ |
| 1995 | if (nargs + nkwds != 3) { |
| 1996 | PyErr_SetString(PyExc_TypeError, |
| 1997 | "type() takes 1 or 3 arguments"); |
| 1998 | return NULL; |
| 1999 | } |
| 2000 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | /* Check arguments: (name, bases, dict) */ |
| 2003 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2004 | &name, |
| 2005 | &PyTuple_Type, &bases, |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2006 | &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2008 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2009 | /* Determine the proper metatype to deal with this: */ |
| 2010 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2011 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | return NULL; |
| 2013 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | if (winner != metatype) { |
| 2016 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2017 | return winner->tp_new(winner, args, kwds); |
| 2018 | metatype = winner; |
| 2019 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2022 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | if (nbases == 0) { |
| 2024 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2025 | if (bases == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2026 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | nbases = 1; |
| 2028 | } |
| 2029 | else |
| 2030 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | /* Calculate best base, and check that all bases are type objects */ |
| 2033 | base = best_base(bases); |
| 2034 | if (base == NULL) { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2035 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | } |
| 2037 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2038 | PyErr_Format(PyExc_TypeError, |
| 2039 | "type '%.100s' is not an acceptable base type", |
| 2040 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2041 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2043 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2044 | dict = PyDict_Copy(orig_dict); |
| 2045 | if (dict == NULL) |
| 2046 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2049 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | nslots = 0; |
| 2051 | add_dict = 0; |
| 2052 | add_weak = 0; |
| 2053 | may_add_dict = base->tp_dictoffset == 0; |
| 2054 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2055 | if (slots == NULL) { |
| 2056 | if (may_add_dict) { |
| 2057 | add_dict++; |
| 2058 | } |
| 2059 | if (may_add_weak) { |
| 2060 | add_weak++; |
| 2061 | } |
| 2062 | } |
| 2063 | else { |
| 2064 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | /* Make it into a tuple */ |
| 2067 | if (PyUnicode_Check(slots)) |
| 2068 | slots = PyTuple_Pack(1, slots); |
| 2069 | else |
| 2070 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2071 | if (slots == NULL) |
| 2072 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | /* Are slots allowed? */ |
| 2076 | nslots = PyTuple_GET_SIZE(slots); |
| 2077 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2078 | PyErr_Format(PyExc_TypeError, |
| 2079 | "nonempty __slots__ " |
| 2080 | "not supported for subtype of '%s'", |
| 2081 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2082 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | /* Check for valid slot names and two special cases */ |
| 2086 | for (i = 0; i < nslots; i++) { |
| 2087 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2088 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2089 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | assert(PyUnicode_Check(tmp)); |
| 2091 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2092 | if (!may_add_dict || add_dict) { |
| 2093 | PyErr_SetString(PyExc_TypeError, |
| 2094 | "__dict__ slot disallowed: " |
| 2095 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2096 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | } |
| 2098 | add_dict++; |
| 2099 | } |
| 2100 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2101 | if (!may_add_weak || add_weak) { |
| 2102 | PyErr_SetString(PyExc_TypeError, |
| 2103 | "__weakref__ slot disallowed: " |
| 2104 | "either we already got one, " |
| 2105 | "or __itemsize__ != 0"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2106 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | } |
| 2108 | add_weak++; |
| 2109 | } |
| 2110 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | /* Copy slots into a list, mangle names and sort them. |
| 2113 | Sorted names are needed for __class__ assignment. |
| 2114 | Convert them back to tuple at the end. |
| 2115 | */ |
| 2116 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2117 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2118 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | for (i = j = 0; i < nslots; i++) { |
| 2120 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2121 | if ((add_dict && |
| 2122 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2123 | (add_weak && |
| 2124 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2125 | continue; |
| 2126 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2127 | if (!tmp) { |
| 2128 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2129 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2130 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2132 | if (PyDict_GetItem(dict, tmp)) { |
| 2133 | PyErr_Format(PyExc_ValueError, |
| 2134 | "%R in __slots__ conflicts with class variable", |
| 2135 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2136 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2137 | goto error; |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2138 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | j++; |
| 2140 | } |
| 2141 | assert(j == nslots - add_dict - add_weak); |
| 2142 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2143 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2146 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | } |
| 2148 | slots = PyList_AsTuple(newslots); |
| 2149 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2150 | if (slots == NULL) |
| 2151 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | /* Secondary bases may provide weakrefs or dict */ |
| 2154 | if (nbases > 1 && |
| 2155 | ((may_add_dict && !add_dict) || |
| 2156 | (may_add_weak && !add_weak))) { |
| 2157 | for (i = 0; i < nbases; i++) { |
| 2158 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2159 | if (tmp == (PyObject *)base) |
| 2160 | continue; /* Skip primary base */ |
| 2161 | assert(PyType_Check(tmp)); |
| 2162 | tmptype = (PyTypeObject *)tmp; |
| 2163 | if (may_add_dict && !add_dict && |
| 2164 | tmptype->tp_dictoffset != 0) |
| 2165 | add_dict++; |
| 2166 | if (may_add_weak && !add_weak && |
| 2167 | tmptype->tp_weaklistoffset != 0) |
| 2168 | add_weak++; |
| 2169 | if (may_add_dict && !add_dict) |
| 2170 | continue; |
| 2171 | if (may_add_weak && !add_weak) |
| 2172 | continue; |
| 2173 | /* Nothing more to check */ |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | /* Allocate the type object */ |
| 2180 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2181 | if (type == NULL) |
| 2182 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | /* Keep name and slots alive in the extended type object */ |
| 2185 | et = (PyHeapTypeObject *)type; |
| 2186 | Py_INCREF(name); |
| 2187 | et->ht_name = name; |
| 2188 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2189 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | /* Initialize tp_flags */ |
| 2192 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2193 | Py_TPFLAGS_BASETYPE; |
| 2194 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2195 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | /* Initialize essential fields */ |
| 2198 | type->tp_as_number = &et->as_number; |
| 2199 | type->tp_as_sequence = &et->as_sequence; |
| 2200 | type->tp_as_mapping = &et->as_mapping; |
| 2201 | type->tp_as_buffer = &et->as_buffer; |
| 2202 | type->tp_name = _PyUnicode_AsString(name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2203 | if (!type->tp_name) |
| 2204 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | /* Set tp_base and tp_bases */ |
| 2207 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2208 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | Py_INCREF(base); |
| 2210 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2213 | Py_INCREF(dict); |
| 2214 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2217 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | tmp = PyEval_GetGlobals(); |
| 2219 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2220 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2222 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2223 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2224 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2228 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2229 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2230 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2231 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2232 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2233 | if (qualname != NULL) { |
| 2234 | if (!PyUnicode_Check(qualname)) { |
| 2235 | PyErr_Format(PyExc_TypeError, |
| 2236 | "type __qualname__ must be a str, not %s", |
| 2237 | Py_TYPE(qualname)->tp_name); |
| 2238 | goto error; |
| 2239 | } |
| 2240 | } |
| 2241 | else { |
| 2242 | qualname = et->ht_name; |
| 2243 | } |
| 2244 | Py_INCREF(qualname); |
| 2245 | et->ht_qualname = qualname; |
| 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2248 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2249 | if that fails, it will still look into __dict__. |
| 2250 | */ |
| 2251 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2252 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2254 | Py_ssize_t len; |
| 2255 | char *doc_str; |
| 2256 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | doc_str = _PyUnicode_AsString(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2259 | if (doc_str == NULL) |
| 2260 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2262 | len = strlen(doc_str); |
| 2263 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2264 | if (tp_doc == NULL) |
| 2265 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | memcpy(tp_doc, doc_str, len + 1); |
| 2267 | type->tp_doc = tp_doc; |
| 2268 | } |
| 2269 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | /* Special-case __new__: if it's a plain function, |
| 2272 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2273 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2275 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2276 | if (tmp == NULL) |
| 2277 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2278 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) |
| 2279 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | Py_DECREF(tmp); |
| 2281 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2284 | mp = PyHeapType_GET_MEMBERS(et); |
| 2285 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2286 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | for (i = 0; i < nslots; i++, mp++) { |
| 2288 | mp->name = _PyUnicode_AsString( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2289 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2290 | if (mp->name == NULL) |
| 2291 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | mp->type = T_OBJECT_EX; |
| 2293 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* __dict__ and __weakref__ are already filtered out */ |
| 2296 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2297 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | slotoffset += sizeof(PyObject *); |
| 2300 | } |
| 2301 | } |
| 2302 | if (add_dict) { |
| 2303 | if (base->tp_itemsize) |
| 2304 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2305 | else |
| 2306 | type->tp_dictoffset = slotoffset; |
| 2307 | slotoffset += sizeof(PyObject *); |
| 2308 | } |
| 2309 | if (add_weak) { |
| 2310 | assert(!base->tp_itemsize); |
| 2311 | type->tp_weaklistoffset = slotoffset; |
| 2312 | slotoffset += sizeof(PyObject *); |
| 2313 | } |
| 2314 | type->tp_basicsize = slotoffset; |
| 2315 | type->tp_itemsize = base->tp_itemsize; |
| 2316 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2319 | type->tp_getset = subtype_getsets_full; |
| 2320 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2321 | type->tp_getset = subtype_getsets_weakref_only; |
| 2322 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2323 | type->tp_getset = subtype_getsets_dict_only; |
| 2324 | else |
| 2325 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | /* Special case some slots */ |
| 2328 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2329 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2330 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2331 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2332 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2333 | } |
| 2334 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | /* Enable GC unless there are really no instance variables possible */ |
| 2337 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2338 | type->tp_itemsize == 0)) |
| 2339 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | /* Always override allocation strategy to use regular heap */ |
| 2342 | type->tp_alloc = PyType_GenericAlloc; |
| 2343 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2344 | type->tp_free = PyObject_GC_Del; |
| 2345 | type->tp_traverse = subtype_traverse; |
| 2346 | type->tp_clear = subtype_clear; |
| 2347 | } |
| 2348 | else |
| 2349 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2352 | if (PyType_Ready(type) < 0) |
| 2353 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | /* Put the proper slots in place */ |
| 2356 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2357 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2358 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2360 | |
| 2361 | error: |
| 2362 | Py_XDECREF(dict); |
| 2363 | Py_XDECREF(bases); |
| 2364 | Py_XDECREF(slots); |
| 2365 | Py_XDECREF(type); |
| 2366 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2369 | static short slotoffsets[] = { |
| 2370 | -1, /* invalid slot */ |
| 2371 | #include "typeslots.inc" |
| 2372 | }; |
| 2373 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2374 | PyObject * |
| 2375 | PyType_FromSpec(PyType_Spec *spec) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2376 | { |
| 2377 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
| 2378 | char *res_start = (char*)res; |
| 2379 | PyType_Slot *slot; |
| 2380 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2381 | if (res == NULL) |
| 2382 | return NULL; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2383 | res->ht_name = PyUnicode_FromString(spec->name); |
| 2384 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2385 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2386 | res->ht_qualname = res->ht_name; |
| 2387 | Py_INCREF(res->ht_qualname); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2388 | res->ht_type.tp_name = _PyUnicode_AsString(res->ht_name); |
| 2389 | if (!res->ht_type.tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2390 | goto fail; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2391 | |
| 2392 | res->ht_type.tp_basicsize = spec->basicsize; |
| 2393 | res->ht_type.tp_itemsize = spec->itemsize; |
| 2394 | res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2395 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2396 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2397 | if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2398 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2399 | goto fail; |
| 2400 | } |
| 2401 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* need to make a copy of the docstring slot, which usually |
| 2404 | points to a static string literal */ |
| 2405 | if (slot->slot == Py_tp_doc) { |
| 2406 | ssize_t len = strlen(slot->pfunc)+1; |
| 2407 | char *tp_doc = PyObject_MALLOC(len); |
| 2408 | if (tp_doc == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2409 | goto fail; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2410 | memcpy(tp_doc, slot->pfunc, len); |
| 2411 | res->ht_type.tp_doc = tp_doc; |
| 2412 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2415 | if (PyType_Ready(&res->ht_type) < 0) |
| 2416 | goto fail; |
| 2417 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2418 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2419 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2420 | fail: |
| 2421 | Py_DECREF(res); |
| 2422 | return NULL; |
| 2423 | } |
| 2424 | |
| 2425 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2426 | /* Internal API to look for a name through the MRO. |
| 2427 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2428 | PyObject * |
| 2429 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | Py_ssize_t i, n; |
| 2432 | PyObject *mro, *res, *base, *dict; |
| 2433 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2436 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2437 | /* fast path */ |
| 2438 | h = MCACHE_HASH_METHOD(type, name); |
| 2439 | if (method_cache[h].version == type->tp_version_tag && |
| 2440 | method_cache[h].name == name) |
| 2441 | return method_cache[h].value; |
| 2442 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | /* Look in tp_dict of types in MRO */ |
| 2445 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | /* If mro is NULL, the type is either not yet initialized |
| 2448 | by PyType_Ready(), or already cleared by type_clear(). |
| 2449 | Either way the safest thing to do is to return NULL. */ |
| 2450 | if (mro == NULL) |
| 2451 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2454 | /* keep a strong reference to mro because type->tp_mro can be replaced |
| 2455 | during PyDict_GetItem(dict, name) */ |
| 2456 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | assert(PyTuple_Check(mro)); |
| 2458 | n = PyTuple_GET_SIZE(mro); |
| 2459 | for (i = 0; i < n; i++) { |
| 2460 | base = PyTuple_GET_ITEM(mro, i); |
| 2461 | assert(PyType_Check(base)); |
| 2462 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2463 | assert(dict && PyDict_Check(dict)); |
| 2464 | res = PyDict_GetItem(dict, name); |
| 2465 | if (res != NULL) |
| 2466 | break; |
| 2467 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2468 | Py_DECREF(mro); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2471 | h = MCACHE_HASH_METHOD(type, name); |
| 2472 | method_cache[h].version = type->tp_version_tag; |
| 2473 | method_cache[h].value = res; /* borrowed */ |
| 2474 | Py_INCREF(name); |
| 2475 | Py_DECREF(method_cache[h].name); |
| 2476 | method_cache[h].name = name; |
| 2477 | } |
| 2478 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2481 | static PyObject * |
| 2482 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 2483 | { |
| 2484 | PyObject *oname; |
| 2485 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 2486 | if (oname == NULL) |
| 2487 | return NULL; |
| 2488 | return _PyType_Lookup(type, oname); |
| 2489 | } |
| 2490 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2491 | /* This is similar to PyObject_GenericGetAttr(), |
| 2492 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2493 | static PyObject * |
| 2494 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | PyTypeObject *metatype = Py_TYPE(type); |
| 2497 | PyObject *meta_attribute, *attribute; |
| 2498 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2499 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 2500 | if (!PyUnicode_Check(name)) { |
| 2501 | PyErr_Format(PyExc_TypeError, |
| 2502 | "attribute name must be string, not '%.200s'", |
| 2503 | name->ob_type->tp_name); |
| 2504 | return NULL; |
| 2505 | } |
| 2506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2508 | if (type->tp_dict == NULL) { |
| 2509 | if (PyType_Ready(type) < 0) |
| 2510 | return NULL; |
| 2511 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 | /* No readable descriptor found yet */ |
| 2514 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | /* Look for the attribute in the metatype */ |
| 2517 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | if (meta_attribute != NULL) { |
| 2520 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2523 | /* Data descriptors implement tp_descr_set to intercept |
| 2524 | * writes. Assume the attribute is not overridden in |
| 2525 | * type's tp_dict (and bases): call the descriptor now. |
| 2526 | */ |
| 2527 | return meta_get(meta_attribute, (PyObject *)type, |
| 2528 | (PyObject *)metatype); |
| 2529 | } |
| 2530 | Py_INCREF(meta_attribute); |
| 2531 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2534 | * type and its bases */ |
| 2535 | attribute = _PyType_Lookup(type, name); |
| 2536 | if (attribute != NULL) { |
| 2537 | /* Implement descriptor functionality, if any */ |
| 2538 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | if (local_get != NULL) { |
| 2543 | /* NULL 2nd argument indicates the descriptor was |
| 2544 | * found on the target object itself (or a base) */ |
| 2545 | return local_get(attribute, (PyObject *)NULL, |
| 2546 | (PyObject *)type); |
| 2547 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | Py_INCREF(attribute); |
| 2550 | return attribute; |
| 2551 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | /* No attribute found in local __dict__ (or bases): use the |
| 2554 | * descriptor from the metatype, if any */ |
| 2555 | if (meta_get != NULL) { |
| 2556 | PyObject *res; |
| 2557 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2558 | (PyObject *)metatype); |
| 2559 | Py_DECREF(meta_attribute); |
| 2560 | return res; |
| 2561 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2564 | if (meta_attribute != NULL) { |
| 2565 | return meta_attribute; |
| 2566 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | /* Give up */ |
| 2569 | PyErr_Format(PyExc_AttributeError, |
| 2570 | "type object '%.50s' has no attribute '%U'", |
| 2571 | type->tp_name, name); |
| 2572 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | static int |
| 2576 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2579 | PyErr_Format( |
| 2580 | PyExc_TypeError, |
| 2581 | "can't set attributes of built-in/extension type '%s'", |
| 2582 | type->tp_name); |
| 2583 | return -1; |
| 2584 | } |
| 2585 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2586 | return -1; |
| 2587 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | static void |
| 2591 | type_dealloc(PyTypeObject *type) |
| 2592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | /* Assert this is a heap-allocated type object */ |
| 2596 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2597 | _PyObject_GC_UNTRACK(type); |
| 2598 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2599 | et = (PyHeapTypeObject *)type; |
| 2600 | Py_XDECREF(type->tp_base); |
| 2601 | Py_XDECREF(type->tp_dict); |
| 2602 | Py_XDECREF(type->tp_bases); |
| 2603 | Py_XDECREF(type->tp_mro); |
| 2604 | Py_XDECREF(type->tp_cache); |
| 2605 | Py_XDECREF(type->tp_subclasses); |
| 2606 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2607 | * of most other objects. It's okay to cast it to char *. |
| 2608 | */ |
| 2609 | PyObject_Free((char *)type->tp_doc); |
| 2610 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2611 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | Py_XDECREF(et->ht_slots); |
| 2613 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2616 | static PyObject * |
| 2617 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2619 | PyObject *list, *raw, *ref; |
| 2620 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | list = PyList_New(0); |
| 2623 | if (list == NULL) |
| 2624 | return NULL; |
| 2625 | raw = type->tp_subclasses; |
| 2626 | if (raw == NULL) |
| 2627 | return list; |
| 2628 | assert(PyList_Check(raw)); |
| 2629 | n = PyList_GET_SIZE(raw); |
| 2630 | for (i = 0; i < n; i++) { |
| 2631 | ref = PyList_GET_ITEM(raw, i); |
| 2632 | assert(PyWeakref_CheckRef(ref)); |
| 2633 | ref = PyWeakref_GET_OBJECT(ref); |
| 2634 | if (ref != Py_None) { |
| 2635 | if (PyList_Append(list, ref) < 0) { |
| 2636 | Py_DECREF(list); |
| 2637 | return NULL; |
| 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2644 | static PyObject * |
| 2645 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2650 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2651 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2652 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2653 | defined, as it's expected that only the final set of dict keys is |
| 2654 | interesting. |
| 2655 | Return 0 on success, -1 on error. |
| 2656 | */ |
| 2657 | |
| 2658 | static int |
| 2659 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2660 | { |
| 2661 | PyObject *classdict; |
| 2662 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2663 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2664 | |
| 2665 | assert(PyDict_Check(dict)); |
| 2666 | assert(aclass); |
| 2667 | |
| 2668 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2669 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2670 | if (classdict == NULL) |
| 2671 | PyErr_Clear(); |
| 2672 | else { |
| 2673 | int status = PyDict_Update(dict, classdict); |
| 2674 | Py_DECREF(classdict); |
| 2675 | if (status < 0) |
| 2676 | return -1; |
| 2677 | } |
| 2678 | |
| 2679 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2680 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2681 | if (bases == NULL) |
| 2682 | PyErr_Clear(); |
| 2683 | else { |
| 2684 | /* We have no guarantee that bases is a real tuple */ |
| 2685 | Py_ssize_t i, n; |
| 2686 | n = PySequence_Size(bases); /* This better be right */ |
| 2687 | if (n < 0) |
| 2688 | PyErr_Clear(); |
| 2689 | else { |
| 2690 | for (i = 0; i < n; i++) { |
| 2691 | int status; |
| 2692 | PyObject *base = PySequence_GetItem(bases, i); |
| 2693 | if (base == NULL) { |
| 2694 | Py_DECREF(bases); |
| 2695 | return -1; |
| 2696 | } |
| 2697 | status = merge_class_dict(dict, base); |
| 2698 | Py_DECREF(base); |
| 2699 | if (status < 0) { |
| 2700 | Py_DECREF(bases); |
| 2701 | return -1; |
| 2702 | } |
| 2703 | } |
| 2704 | } |
| 2705 | Py_DECREF(bases); |
| 2706 | } |
| 2707 | return 0; |
| 2708 | } |
| 2709 | |
| 2710 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2711 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2712 | metaclass would probably be more confusing than helpful. |
| 2713 | */ |
| 2714 | static PyObject * |
| 2715 | type_dir(PyObject *self, PyObject *args) |
| 2716 | { |
| 2717 | PyObject *result = NULL; |
| 2718 | PyObject *dict = PyDict_New(); |
| 2719 | |
| 2720 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 2721 | result = PyDict_Keys(dict); |
| 2722 | |
| 2723 | Py_XDECREF(dict); |
| 2724 | return result; |
| 2725 | } |
| 2726 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2727 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2729 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2730 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2731 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2732 | {"__prepare__", (PyCFunction)type_prepare, |
| 2733 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2734 | PyDoc_STR("__prepare__() -> dict\n" |
| 2735 | "used to create the namespace for the class statement")}, |
| 2736 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2737 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2739 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2740 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 2741 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2743 | }; |
| 2744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2745 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2746 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2747 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2748 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2749 | static int |
| 2750 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | /* Because of type_is_gc(), the collector only calls this |
| 2753 | for heaptypes. */ |
| 2754 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | Py_VISIT(type->tp_dict); |
| 2757 | Py_VISIT(type->tp_cache); |
| 2758 | Py_VISIT(type->tp_mro); |
| 2759 | Py_VISIT(type->tp_bases); |
| 2760 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | /* There's no need to visit type->tp_subclasses or |
| 2763 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2764 | in cycles; tp_subclasses is a list of weak references, |
| 2765 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | static int |
| 2771 | type_clear(PyTypeObject *type) |
| 2772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | /* Because of type_is_gc(), the collector only calls this |
| 2774 | for heaptypes. */ |
| 2775 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2777 | /* We need to invalidate the method cache carefully before clearing |
| 2778 | the dict, so that other objects caught in a reference cycle |
| 2779 | don't start calling destroyed methods. |
| 2780 | |
| 2781 | Otherwise, the only field we need to clear is tp_mro, which is |
| 2782 | part of a hard cycle (its first element is the class itself) that |
| 2783 | won't be broken otherwise (it's a tuple and tuples don't have a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | tp_clear handler). None of the other fields need to be |
| 2785 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | tp_cache: |
| 2788 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | tp_bases, tp_base: |
| 2791 | If these are involved in a cycle, there must be at least |
| 2792 | one other, mutable object in the cycle, e.g. a base |
| 2793 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | tp_subclasses: |
| 2796 | A list of weak references can't be part of a cycle; and |
| 2797 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | slots (in PyHeapTypeObject): |
| 2800 | A tuple of strings can't be part of a cycle. |
| 2801 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2803 | PyType_Modified(type); |
| 2804 | if (type->tp_dict) |
| 2805 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | static int |
| 2812 | type_is_gc(PyTypeObject *type) |
| 2813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2817 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2819 | "type", /* tp_name */ |
| 2820 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2821 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2822 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2823 | 0, /* tp_print */ |
| 2824 | 0, /* tp_getattr */ |
| 2825 | 0, /* tp_setattr */ |
| 2826 | 0, /* tp_reserved */ |
| 2827 | (reprfunc)type_repr, /* tp_repr */ |
| 2828 | 0, /* tp_as_number */ |
| 2829 | 0, /* tp_as_sequence */ |
| 2830 | 0, /* tp_as_mapping */ |
| 2831 | 0, /* tp_hash */ |
| 2832 | (ternaryfunc)type_call, /* tp_call */ |
| 2833 | 0, /* tp_str */ |
| 2834 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2835 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2836 | 0, /* tp_as_buffer */ |
| 2837 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2838 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2839 | type_doc, /* tp_doc */ |
| 2840 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2841 | (inquiry)type_clear, /* tp_clear */ |
| 2842 | 0, /* tp_richcompare */ |
| 2843 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2844 | 0, /* tp_iter */ |
| 2845 | 0, /* tp_iternext */ |
| 2846 | type_methods, /* tp_methods */ |
| 2847 | type_members, /* tp_members */ |
| 2848 | type_getsets, /* tp_getset */ |
| 2849 | 0, /* tp_base */ |
| 2850 | 0, /* tp_dict */ |
| 2851 | 0, /* tp_descr_get */ |
| 2852 | 0, /* tp_descr_set */ |
| 2853 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2854 | type_init, /* tp_init */ |
| 2855 | 0, /* tp_alloc */ |
| 2856 | type_new, /* tp_new */ |
| 2857 | PyObject_GC_Del, /* tp_free */ |
| 2858 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2859 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2860 | |
| 2861 | |
| 2862 | /* The base type of all types (eventually)... except itself. */ |
| 2863 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2864 | /* You may wonder why object.__new__() only complains about arguments |
| 2865 | when object.__init__() is not overridden, and vice versa. |
| 2866 | |
| 2867 | Consider the use cases: |
| 2868 | |
| 2869 | 1. When neither is overridden, we want to hear complaints about |
| 2870 | excess (i.e., any) arguments, since their presence could |
| 2871 | indicate there's a bug. |
| 2872 | |
| 2873 | 2. When defining an Immutable type, we are likely to override only |
| 2874 | __new__(), since __init__() is called too late to initialize an |
| 2875 | Immutable object. Since __new__() defines the signature for the |
| 2876 | type, it would be a pain to have to override __init__() just to |
| 2877 | stop it from complaining about excess arguments. |
| 2878 | |
| 2879 | 3. When defining a Mutable type, we are likely to override only |
| 2880 | __init__(). So here the converse reasoning applies: we don't |
| 2881 | want to have to override __new__() just to stop it from |
| 2882 | complaining. |
| 2883 | |
| 2884 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2885 | object.__init__(), the latter should complain about excess |
| 2886 | arguments; ditto for __new__(). |
| 2887 | |
| 2888 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2889 | excess arguments. The best solution that addresses all four use |
| 2890 | cases is as follows: __init__() complains about excess arguments |
| 2891 | unless __new__() is overridden and __init__() is not overridden |
| 2892 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2893 | symmetrically, __new__() complains about excess arguments unless |
| 2894 | __init__() is overridden and __new__() is not overridden |
| 2895 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2896 | |
| 2897 | However, for backwards compatibility, this breaks too much code. |
| 2898 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2899 | methods are overridden; for all other cases we'll use the above |
| 2900 | rules. |
| 2901 | |
| 2902 | */ |
| 2903 | |
| 2904 | /* Forward */ |
| 2905 | static PyObject * |
| 2906 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2907 | |
| 2908 | static int |
| 2909 | excess_args(PyObject *args, PyObject *kwds) |
| 2910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | return PyTuple_GET_SIZE(args) || |
| 2912 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2915 | static int |
| 2916 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | int err = 0; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 2919 | PyTypeObject *type = Py_TYPE(self); |
| 2920 | if (excess_args(args, kwds) && |
| 2921 | (type->tp_new == object_new || type->tp_init != object_init)) { |
| 2922 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
| 2923 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | } |
| 2925 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2928 | static PyObject * |
| 2929 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2930 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 2931 | if (excess_args(args, kwds) && |
| 2932 | (type->tp_init == object_init || type->tp_new != object_new)) { |
| 2933 | PyErr_SetString(PyExc_TypeError, "object.__new__() takes no parameters"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | return NULL; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 2935 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | PyObject *abstract_methods = NULL; |
| 2939 | PyObject *builtins; |
| 2940 | PyObject *sorted; |
| 2941 | PyObject *sorted_methods = NULL; |
| 2942 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 2943 | PyObject *comma; |
| 2944 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2945 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2948 | into joined. */ |
| 2949 | abstract_methods = type_abstractmethods(type, NULL); |
| 2950 | if (abstract_methods == NULL) |
| 2951 | goto error; |
| 2952 | builtins = PyEval_GetBuiltins(); |
| 2953 | if (builtins == NULL) |
| 2954 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2955 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | if (sorted == NULL) |
| 2957 | goto error; |
| 2958 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2959 | abstract_methods, |
| 2960 | NULL); |
| 2961 | if (sorted_methods == NULL) |
| 2962 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 2963 | comma = _PyUnicode_FromId(&comma_id); |
| 2964 | if (comma == NULL) |
| 2965 | goto error; |
| 2966 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | if (joined == NULL) |
| 2968 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | PyErr_Format(PyExc_TypeError, |
| 2971 | "Can't instantiate abstract class %s " |
| 2972 | "with abstract methods %U", |
| 2973 | type->tp_name, |
| 2974 | joined); |
| 2975 | error: |
| 2976 | Py_XDECREF(joined); |
| 2977 | Py_XDECREF(sorted_methods); |
| 2978 | Py_XDECREF(abstract_methods); |
| 2979 | return NULL; |
| 2980 | } |
| 2981 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2984 | static void |
| 2985 | object_dealloc(PyObject *self) |
| 2986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2990 | static PyObject * |
| 2991 | object_repr(PyObject *self) |
| 2992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | PyTypeObject *type; |
| 2994 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | type = Py_TYPE(self); |
| 2997 | mod = type_module(type, NULL); |
| 2998 | if (mod == NULL) |
| 2999 | PyErr_Clear(); |
| 3000 | else if (!PyUnicode_Check(mod)) { |
| 3001 | Py_DECREF(mod); |
| 3002 | mod = NULL; |
| 3003 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3004 | name = type_qualname(type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | if (name == NULL) |
| 3006 | return NULL; |
| 3007 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 3008 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3009 | else |
| 3010 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3011 | type->tp_name, self); |
| 3012 | Py_XDECREF(mod); |
| 3013 | Py_DECREF(name); |
| 3014 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3017 | static PyObject * |
| 3018 | object_str(PyObject *self) |
| 3019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | f = Py_TYPE(self)->tp_repr; |
Antoine Pitrou | 8cdc40e | 2011-07-15 21:15:07 +0200 | [diff] [blame] | 3023 | if (f == NULL || f == object_str) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | f = object_repr; |
| 3025 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3028 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3029 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3030 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | case Py_EQ: |
| 3036 | /* Return NotImplemented instead of False, so if two |
| 3037 | objects are compared, both get a chance at the |
| 3038 | comparison. See issue #1393. */ |
| 3039 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3040 | Py_INCREF(res); |
| 3041 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | case Py_NE: |
| 3044 | /* By default, != returns the opposite of ==, |
| 3045 | unless the latter returns NotImplemented. */ |
| 3046 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3047 | if (res != NULL && res != Py_NotImplemented) { |
| 3048 | int ok = PyObject_IsTrue(res); |
| 3049 | Py_DECREF(res); |
| 3050 | if (ok < 0) |
| 3051 | res = NULL; |
| 3052 | else { |
| 3053 | if (ok) |
| 3054 | res = Py_False; |
| 3055 | else |
| 3056 | res = Py_True; |
| 3057 | Py_INCREF(res); |
| 3058 | } |
| 3059 | } |
| 3060 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | default: |
| 3063 | res = Py_NotImplemented; |
| 3064 | Py_INCREF(res); |
| 3065 | break; |
| 3066 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3072 | object_get_class(PyObject *self, void *closure) |
| 3073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | Py_INCREF(Py_TYPE(self)); |
| 3075 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | static int |
| 3079 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | return a == b || |
| 3082 | (a != NULL && |
| 3083 | b != NULL && |
| 3084 | a->tp_basicsize == b->tp_basicsize && |
| 3085 | a->tp_itemsize == b->tp_itemsize && |
| 3086 | a->tp_dictoffset == b->tp_dictoffset && |
| 3087 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3088 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3089 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | static int |
| 3093 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | PyTypeObject *base = a->tp_base; |
| 3096 | Py_ssize_t size; |
| 3097 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3098 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3099 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | size = base->tp_basicsize; |
| 3101 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3102 | size += sizeof(PyObject *); |
| 3103 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3104 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | /* Check slots compliance */ |
| 3107 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3108 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3109 | if (slots_a && slots_b) { |
| 3110 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3111 | return 0; |
| 3112 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3113 | } |
| 3114 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3118 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3123 | newto->tp_free != oldto->tp_free) |
| 3124 | { |
| 3125 | PyErr_Format(PyExc_TypeError, |
| 3126 | "%s assignment: " |
| 3127 | "'%s' deallocator differs from '%s'", |
| 3128 | attr, |
| 3129 | newto->tp_name, |
| 3130 | oldto->tp_name); |
| 3131 | return 0; |
| 3132 | } |
| 3133 | newbase = newto; |
| 3134 | oldbase = oldto; |
| 3135 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3136 | newbase = newbase->tp_base; |
| 3137 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3138 | oldbase = oldbase->tp_base; |
| 3139 | if (newbase != oldbase && |
| 3140 | (newbase->tp_base != oldbase->tp_base || |
| 3141 | !same_slots_added(newbase, oldbase))) { |
| 3142 | PyErr_Format(PyExc_TypeError, |
| 3143 | "%s assignment: " |
| 3144 | "'%s' object layout differs from '%s'", |
| 3145 | attr, |
| 3146 | newto->tp_name, |
| 3147 | oldto->tp_name); |
| 3148 | return 0; |
| 3149 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3155 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | PyTypeObject *oldto = Py_TYPE(self); |
| 3158 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | if (value == NULL) { |
| 3161 | PyErr_SetString(PyExc_TypeError, |
| 3162 | "can't delete __class__ attribute"); |
| 3163 | return -1; |
| 3164 | } |
| 3165 | if (!PyType_Check(value)) { |
| 3166 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3167 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | Py_TYPE(value)->tp_name); |
| 3169 | return -1; |
| 3170 | } |
| 3171 | newto = (PyTypeObject *)value; |
| 3172 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3173 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3174 | { |
| 3175 | PyErr_Format(PyExc_TypeError, |
| 3176 | "__class__ assignment: only for heap types"); |
| 3177 | return -1; |
| 3178 | } |
| 3179 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3180 | Py_INCREF(newto); |
| 3181 | Py_TYPE(self) = newto; |
| 3182 | Py_DECREF(oldto); |
| 3183 | return 0; |
| 3184 | } |
| 3185 | else { |
| 3186 | return -1; |
| 3187 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | {"__class__", object_get_class, object_set_class, |
| 3192 | PyDoc_STR("the object's class")}, |
| 3193 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3194 | }; |
| 3195 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3196 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3197 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3198 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3199 | - pickle protocols < 2 |
| 3200 | - calculating the list of slot names (done only once per class) |
| 3201 | - the __newobj__ function (which is used as a token but never called) |
| 3202 | */ |
| 3203 | |
| 3204 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3205 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | static PyObject *copyreg_str; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3208 | static PyObject *mod_copyreg = NULL; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | if (!copyreg_str) { |
| 3211 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3212 | if (copyreg_str == NULL) |
| 3213 | return NULL; |
| 3214 | } |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3215 | if (!mod_copyreg) { |
| 3216 | mod_copyreg = PyImport_Import(copyreg_str); |
| 3217 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3218 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3219 | Py_XINCREF(mod_copyreg); |
| 3220 | return mod_copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | static PyObject * |
| 3224 | slotnames(PyObject *cls) |
| 3225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | PyObject *clsdict; |
| 3227 | PyObject *copyreg; |
| 3228 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3229 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3230 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3233 | slotnames = _PyDict_GetItemId(clsdict, &PyId___slotnames__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3235 | Py_INCREF(slotnames); |
| 3236 | return slotnames; |
| 3237 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | copyreg = import_copyreg(); |
| 3240 | if (copyreg == NULL) |
| 3241 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3242 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3243 | slotnames = _PyObject_CallMethodId(copyreg, &PyId__slotnames, "O", cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | Py_DECREF(copyreg); |
| 3245 | if (slotnames != NULL && |
| 3246 | slotnames != Py_None && |
| 3247 | !PyList_Check(slotnames)) |
| 3248 | { |
| 3249 | PyErr_SetString(PyExc_TypeError, |
| 3250 | "copyreg._slotnames didn't return a list or None"); |
| 3251 | Py_DECREF(slotnames); |
| 3252 | slotnames = NULL; |
| 3253 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | static PyObject * |
| 3259 | reduce_2(PyObject *obj) |
| 3260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | PyObject *cls, *getnewargs; |
| 3262 | PyObject *args = NULL, *args2 = NULL; |
| 3263 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3264 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3265 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3266 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3267 | _Py_IDENTIFIER(__getnewargs__); |
| 3268 | _Py_IDENTIFIER(__getstate__); |
| 3269 | _Py_IDENTIFIER(__newobj__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3270 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3271 | cls = (PyObject *) Py_TYPE(obj); |
| 3272 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3273 | getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | if (getnewargs != NULL) { |
| 3275 | args = PyObject_CallObject(getnewargs, NULL); |
| 3276 | Py_DECREF(getnewargs); |
| 3277 | if (args != NULL && !PyTuple_Check(args)) { |
| 3278 | PyErr_Format(PyExc_TypeError, |
| 3279 | "__getnewargs__ should return a tuple, " |
| 3280 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3281 | goto end; |
| 3282 | } |
| 3283 | } |
| 3284 | else { |
| 3285 | PyErr_Clear(); |
| 3286 | args = PyTuple_New(0); |
| 3287 | } |
| 3288 | if (args == NULL) |
| 3289 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3290 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3291 | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | if (getstate != NULL) { |
| 3293 | state = PyObject_CallObject(getstate, NULL); |
| 3294 | Py_DECREF(getstate); |
| 3295 | if (state == NULL) |
| 3296 | goto end; |
| 3297 | } |
| 3298 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3299 | PyObject **dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | PyErr_Clear(); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3301 | dict = _PyObject_GetDictPtr(obj); |
| 3302 | if (dict && *dict) |
| 3303 | state = *dict; |
| 3304 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | state = Py_None; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3306 | Py_INCREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | names = slotnames(cls); |
| 3308 | if (names == NULL) |
| 3309 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3310 | if (names != Py_None && PyList_GET_SIZE(names) > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | assert(PyList_Check(names)); |
| 3312 | slots = PyDict_New(); |
| 3313 | if (slots == NULL) |
| 3314 | goto end; |
| 3315 | n = 0; |
| 3316 | /* Can't pre-compute the list size; the list |
| 3317 | is stored on the class so accessible to other |
| 3318 | threads, which may be run by DECREF */ |
| 3319 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3320 | PyObject *name, *value; |
| 3321 | name = PyList_GET_ITEM(names, i); |
| 3322 | value = PyObject_GetAttr(obj, name); |
| 3323 | if (value == NULL) |
| 3324 | PyErr_Clear(); |
| 3325 | else { |
| 3326 | int err = PyDict_SetItem(slots, name, |
| 3327 | value); |
| 3328 | Py_DECREF(value); |
| 3329 | if (err) |
| 3330 | goto end; |
| 3331 | n++; |
| 3332 | } |
| 3333 | } |
| 3334 | if (n) { |
| 3335 | state = Py_BuildValue("(NO)", state, slots); |
| 3336 | if (state == NULL) |
| 3337 | goto end; |
| 3338 | } |
| 3339 | } |
| 3340 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | if (!PyList_Check(obj)) { |
| 3343 | listitems = Py_None; |
| 3344 | Py_INCREF(listitems); |
| 3345 | } |
| 3346 | else { |
| 3347 | listitems = PyObject_GetIter(obj); |
| 3348 | if (listitems == NULL) |
| 3349 | goto end; |
| 3350 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | if (!PyDict_Check(obj)) { |
| 3353 | dictitems = Py_None; |
| 3354 | Py_INCREF(dictitems); |
| 3355 | } |
| 3356 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3357 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3358 | PyObject *items = _PyObject_CallMethodId(obj, &PyId_items, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | if (items == NULL) |
| 3360 | goto end; |
| 3361 | dictitems = PyObject_GetIter(items); |
| 3362 | Py_DECREF(items); |
| 3363 | if (dictitems == NULL) |
| 3364 | goto end; |
| 3365 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | copyreg = import_copyreg(); |
| 3368 | if (copyreg == NULL) |
| 3369 | goto end; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3370 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | if (newobj == NULL) |
| 3372 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3374 | n = PyTuple_GET_SIZE(args); |
| 3375 | args2 = PyTuple_New(n+1); |
| 3376 | if (args2 == NULL) |
| 3377 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3378 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | for (i = 0; i < n; i++) { |
| 3381 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3382 | Py_INCREF(v); |
| 3383 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3384 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3387 | |
| 3388 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | Py_XDECREF(args); |
| 3390 | Py_XDECREF(args2); |
| 3391 | Py_XDECREF(slots); |
| 3392 | Py_XDECREF(state); |
| 3393 | Py_XDECREF(names); |
| 3394 | Py_XDECREF(listitems); |
| 3395 | Py_XDECREF(dictitems); |
| 3396 | Py_XDECREF(copyreg); |
| 3397 | Py_XDECREF(newobj); |
| 3398 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3401 | /* |
| 3402 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3403 | * were implemented in the same function: |
| 3404 | * - trying to pickle an object with a custom __reduce__ method that |
| 3405 | * fell back to object.__reduce__ in certain circumstances led to |
| 3406 | * infinite recursion at Python level and eventual RuntimeError. |
| 3407 | * - Pickling objects that lied about their type by overwriting the |
| 3408 | * __class__ descriptor could lead to infinite recursion at C level |
| 3409 | * and eventual segfault. |
| 3410 | * |
| 3411 | * Because of backwards compatibility, the two methods still have to |
| 3412 | * behave in the same way, even if this is not required by the pickle |
| 3413 | * protocol. This common functionality was moved to the _common_reduce |
| 3414 | * function. |
| 3415 | */ |
| 3416 | static PyObject * |
| 3417 | _common_reduce(PyObject *self, int proto) |
| 3418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | if (proto >= 2) |
| 3422 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | copyreg = import_copyreg(); |
| 3425 | if (!copyreg) |
| 3426 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3429 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | static PyObject * |
| 3435 | object_reduce(PyObject *self, PyObject *args) |
| 3436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3440 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3445 | static PyObject * |
| 3446 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3447 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3448 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | PyObject *reduce, *res; |
| 3450 | int proto = 0; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3451 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3454 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3455 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3456 | if (objreduce == NULL) { |
| 3457 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 3458 | &PyId___reduce__); |
| 3459 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3460 | return NULL; |
| 3461 | } |
| 3462 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3463 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | if (reduce == NULL) |
| 3465 | PyErr_Clear(); |
| 3466 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3467 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3469 | |
| 3470 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3471 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | if (clsreduce == NULL) { |
| 3473 | Py_DECREF(reduce); |
| 3474 | return NULL; |
| 3475 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | override = (clsreduce != objreduce); |
| 3477 | Py_DECREF(clsreduce); |
| 3478 | if (override) { |
| 3479 | res = PyObject_CallObject(reduce, NULL); |
| 3480 | Py_DECREF(reduce); |
| 3481 | return res; |
| 3482 | } |
| 3483 | else |
| 3484 | Py_DECREF(reduce); |
| 3485 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3490 | static PyObject * |
| 3491 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3492 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3493 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3497 | "Abstract classes can override this to customize issubclass().\n" |
| 3498 | "\n" |
| 3499 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3500 | "It should return True, False or NotImplemented. If it returns\n" |
| 3501 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3502 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3503 | |
| 3504 | /* |
| 3505 | from PEP 3101, this code implements: |
| 3506 | |
| 3507 | class object: |
| 3508 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3510 | */ |
| 3511 | static PyObject * |
| 3512 | object_format(PyObject *self, PyObject *args) |
| 3513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | PyObject *format_spec; |
| 3515 | PyObject *self_as_str = NULL; |
| 3516 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3519 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3522 | if (self_as_str != NULL) { |
| 3523 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3524 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3525 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3526 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3527 | "object.__format__ with a non-empty format " |
| 3528 | "string is deprecated", 1) < 0) { |
| 3529 | goto done; |
| 3530 | } |
| 3531 | /* Eventually this will become an error: |
| 3532 | PyErr_Format(PyExc_TypeError, |
| 3533 | "non-empty format string passed to object.__format__"); |
| 3534 | goto done; |
| 3535 | */ |
| 3536 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3537 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3538 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3547 | static PyObject * |
| 3548 | object_sizeof(PyObject *self, PyObject *args) |
| 3549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | res = 0; |
| 3553 | isize = self->ob_type->tp_itemsize; |
| 3554 | if (isize > 0) |
| 3555 | res = Py_SIZE(self->ob_type) * isize; |
| 3556 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3561 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 3562 | and recursively up the __class__.__bases__ chain. |
| 3563 | */ |
| 3564 | static PyObject * |
| 3565 | object_dir(PyObject *self, PyObject *args) |
| 3566 | { |
| 3567 | PyObject *result = NULL; |
| 3568 | PyObject *dict = NULL; |
| 3569 | PyObject *itsclass = NULL; |
| 3570 | |
| 3571 | /* Get __dict__ (which may or may not be a real dict...) */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3572 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3573 | if (dict == NULL) { |
| 3574 | PyErr_Clear(); |
| 3575 | dict = PyDict_New(); |
| 3576 | } |
| 3577 | else if (!PyDict_Check(dict)) { |
| 3578 | Py_DECREF(dict); |
| 3579 | dict = PyDict_New(); |
| 3580 | } |
| 3581 | else { |
| 3582 | /* Copy __dict__ to avoid mutating it. */ |
| 3583 | PyObject *temp = PyDict_Copy(dict); |
| 3584 | Py_DECREF(dict); |
| 3585 | dict = temp; |
| 3586 | } |
| 3587 | |
| 3588 | if (dict == NULL) |
| 3589 | goto error; |
| 3590 | |
| 3591 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3592 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3593 | if (itsclass == NULL) |
| 3594 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 3595 | __class__ exists? */ |
| 3596 | PyErr_Clear(); |
| 3597 | else if (merge_class_dict(dict, itsclass) != 0) |
| 3598 | goto error; |
| 3599 | |
| 3600 | result = PyDict_Keys(dict); |
| 3601 | /* fall through */ |
| 3602 | error: |
| 3603 | Py_XDECREF(itsclass); |
| 3604 | Py_XDECREF(dict); |
| 3605 | return result; |
| 3606 | } |
| 3607 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3608 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3610 | PyDoc_STR("helper for pickle")}, |
| 3611 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3612 | PyDoc_STR("helper for pickle")}, |
| 3613 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3614 | object_subclasshook_doc}, |
| 3615 | {"__format__", object_format, METH_VARARGS, |
| 3616 | PyDoc_STR("default object formatter")}, |
| 3617 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3618 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3619 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3620 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3622 | }; |
| 3623 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3624 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3625 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3627 | "object", /* tp_name */ |
| 3628 | sizeof(PyObject), /* tp_basicsize */ |
| 3629 | 0, /* tp_itemsize */ |
| 3630 | object_dealloc, /* tp_dealloc */ |
| 3631 | 0, /* tp_print */ |
| 3632 | 0, /* tp_getattr */ |
| 3633 | 0, /* tp_setattr */ |
| 3634 | 0, /* tp_reserved */ |
| 3635 | object_repr, /* tp_repr */ |
| 3636 | 0, /* tp_as_number */ |
| 3637 | 0, /* tp_as_sequence */ |
| 3638 | 0, /* tp_as_mapping */ |
| 3639 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3640 | 0, /* tp_call */ |
| 3641 | object_str, /* tp_str */ |
| 3642 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3643 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3644 | 0, /* tp_as_buffer */ |
| 3645 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3646 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3647 | 0, /* tp_traverse */ |
| 3648 | 0, /* tp_clear */ |
| 3649 | object_richcompare, /* tp_richcompare */ |
| 3650 | 0, /* tp_weaklistoffset */ |
| 3651 | 0, /* tp_iter */ |
| 3652 | 0, /* tp_iternext */ |
| 3653 | object_methods, /* tp_methods */ |
| 3654 | 0, /* tp_members */ |
| 3655 | object_getsets, /* tp_getset */ |
| 3656 | 0, /* tp_base */ |
| 3657 | 0, /* tp_dict */ |
| 3658 | 0, /* tp_descr_get */ |
| 3659 | 0, /* tp_descr_set */ |
| 3660 | 0, /* tp_dictoffset */ |
| 3661 | object_init, /* tp_init */ |
| 3662 | PyType_GenericAlloc, /* tp_alloc */ |
| 3663 | object_new, /* tp_new */ |
| 3664 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3665 | }; |
| 3666 | |
| 3667 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3668 | /* Add the methods from tp_methods to the __dict__ in a type object */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3669 | |
| 3670 | static int |
| 3671 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3673 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | for (; meth->ml_name != NULL; meth++) { |
| 3676 | PyObject *descr; |
| 3677 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3678 | !(meth->ml_flags & METH_COEXIST)) |
| 3679 | continue; |
| 3680 | if (meth->ml_flags & METH_CLASS) { |
| 3681 | if (meth->ml_flags & METH_STATIC) { |
| 3682 | PyErr_SetString(PyExc_ValueError, |
| 3683 | "method cannot be both class and static"); |
| 3684 | return -1; |
| 3685 | } |
| 3686 | descr = PyDescr_NewClassMethod(type, meth); |
| 3687 | } |
| 3688 | else if (meth->ml_flags & METH_STATIC) { |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 3689 | PyObject *cfunc = PyCFunction_New(meth, (PyObject*)type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | if (cfunc == NULL) |
| 3691 | return -1; |
| 3692 | descr = PyStaticMethod_New(cfunc); |
| 3693 | Py_DECREF(cfunc); |
| 3694 | } |
| 3695 | else { |
| 3696 | descr = PyDescr_NewMethod(type, meth); |
| 3697 | } |
| 3698 | if (descr == NULL) |
| 3699 | return -1; |
| 3700 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
| 3701 | return -1; |
| 3702 | Py_DECREF(descr); |
| 3703 | } |
| 3704 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3708 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3710 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3712 | for (; memb->name != NULL; memb++) { |
| 3713 | PyObject *descr; |
| 3714 | if (PyDict_GetItemString(dict, memb->name)) |
| 3715 | continue; |
| 3716 | descr = PyDescr_NewMember(type, memb); |
| 3717 | if (descr == NULL) |
| 3718 | return -1; |
| 3719 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3720 | return -1; |
| 3721 | Py_DECREF(descr); |
| 3722 | } |
| 3723 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3727 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | for (; gsp->name != NULL; gsp++) { |
| 3732 | PyObject *descr; |
| 3733 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3734 | continue; |
| 3735 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | if (descr == NULL) |
| 3738 | return -1; |
| 3739 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3740 | return -1; |
| 3741 | Py_DECREF(descr); |
| 3742 | } |
| 3743 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3746 | static void |
| 3747 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3748 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3751 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3752 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3753 | (!type->tp_traverse && !type->tp_clear)) { |
| 3754 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3755 | if (type->tp_traverse == NULL) |
| 3756 | type->tp_traverse = base->tp_traverse; |
| 3757 | if (type->tp_clear == NULL) |
| 3758 | type->tp_clear = base->tp_clear; |
| 3759 | } |
| 3760 | { |
| 3761 | /* The condition below could use some explanation. |
| 3762 | It appears that tp_new is not inherited for static types |
| 3763 | whose base class is 'object'; this seems to be a precaution |
| 3764 | so that old extension types don't suddenly become |
| 3765 | callable (object.__new__ wouldn't insure the invariants |
| 3766 | that the extension type's own factory function ensures). |
| 3767 | Heap types, of course, are under our control, so they do |
| 3768 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3769 | other built-in type as the default also |
| 3770 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | if (base != &PyBaseObject_Type || |
| 3772 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3773 | if (type->tp_new == NULL) |
| 3774 | type->tp_new = base->tp_new; |
| 3775 | } |
| 3776 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3777 | if (type->tp_basicsize == 0) |
| 3778 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3781 | |
| 3782 | #undef COPYVAL |
| 3783 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | COPYVAL(tp_itemsize); |
| 3787 | COPYVAL(tp_weaklistoffset); |
| 3788 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | /* Setup fast subclass flags */ |
| 3791 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3792 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3793 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3794 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3795 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3796 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3797 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3798 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3799 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3800 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3801 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3802 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3803 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3804 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3805 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3806 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3809 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3810 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3813 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3816 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 3817 | return 1; |
| 3818 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 3819 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3823 | static void |
| 3824 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3827 | |
| 3828 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3829 | #undef COPYSLOT |
| 3830 | #undef COPYNUM |
| 3831 | #undef COPYSEQ |
| 3832 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3833 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3834 | |
| 3835 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | (base->SLOT != 0 && \ |
| 3837 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3838 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3839 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3840 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3841 | |
| 3842 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3843 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3844 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3845 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3848 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3850 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3851 | basebase = base->tp_base; |
| 3852 | if (basebase->tp_as_number == NULL) |
| 3853 | basebase = NULL; |
| 3854 | COPYNUM(nb_add); |
| 3855 | COPYNUM(nb_subtract); |
| 3856 | COPYNUM(nb_multiply); |
| 3857 | COPYNUM(nb_remainder); |
| 3858 | COPYNUM(nb_divmod); |
| 3859 | COPYNUM(nb_power); |
| 3860 | COPYNUM(nb_negative); |
| 3861 | COPYNUM(nb_positive); |
| 3862 | COPYNUM(nb_absolute); |
| 3863 | COPYNUM(nb_bool); |
| 3864 | COPYNUM(nb_invert); |
| 3865 | COPYNUM(nb_lshift); |
| 3866 | COPYNUM(nb_rshift); |
| 3867 | COPYNUM(nb_and); |
| 3868 | COPYNUM(nb_xor); |
| 3869 | COPYNUM(nb_or); |
| 3870 | COPYNUM(nb_int); |
| 3871 | COPYNUM(nb_float); |
| 3872 | COPYNUM(nb_inplace_add); |
| 3873 | COPYNUM(nb_inplace_subtract); |
| 3874 | COPYNUM(nb_inplace_multiply); |
| 3875 | COPYNUM(nb_inplace_remainder); |
| 3876 | COPYNUM(nb_inplace_power); |
| 3877 | COPYNUM(nb_inplace_lshift); |
| 3878 | COPYNUM(nb_inplace_rshift); |
| 3879 | COPYNUM(nb_inplace_and); |
| 3880 | COPYNUM(nb_inplace_xor); |
| 3881 | COPYNUM(nb_inplace_or); |
| 3882 | COPYNUM(nb_true_divide); |
| 3883 | COPYNUM(nb_floor_divide); |
| 3884 | COPYNUM(nb_inplace_true_divide); |
| 3885 | COPYNUM(nb_inplace_floor_divide); |
| 3886 | COPYNUM(nb_index); |
| 3887 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3890 | basebase = base->tp_base; |
| 3891 | if (basebase->tp_as_sequence == NULL) |
| 3892 | basebase = NULL; |
| 3893 | COPYSEQ(sq_length); |
| 3894 | COPYSEQ(sq_concat); |
| 3895 | COPYSEQ(sq_repeat); |
| 3896 | COPYSEQ(sq_item); |
| 3897 | COPYSEQ(sq_ass_item); |
| 3898 | COPYSEQ(sq_contains); |
| 3899 | COPYSEQ(sq_inplace_concat); |
| 3900 | COPYSEQ(sq_inplace_repeat); |
| 3901 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3904 | basebase = base->tp_base; |
| 3905 | if (basebase->tp_as_mapping == NULL) |
| 3906 | basebase = NULL; |
| 3907 | COPYMAP(mp_length); |
| 3908 | COPYMAP(mp_subscript); |
| 3909 | COPYMAP(mp_ass_subscript); |
| 3910 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3913 | basebase = base->tp_base; |
| 3914 | if (basebase->tp_as_buffer == NULL) |
| 3915 | basebase = NULL; |
| 3916 | COPYBUF(bf_getbuffer); |
| 3917 | COPYBUF(bf_releasebuffer); |
| 3918 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | COPYSLOT(tp_dealloc); |
| 3923 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3924 | type->tp_getattr = base->tp_getattr; |
| 3925 | type->tp_getattro = base->tp_getattro; |
| 3926 | } |
| 3927 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3928 | type->tp_setattr = base->tp_setattr; |
| 3929 | type->tp_setattro = base->tp_setattro; |
| 3930 | } |
| 3931 | /* tp_reserved is ignored */ |
| 3932 | COPYSLOT(tp_repr); |
| 3933 | /* tp_hash see tp_richcompare */ |
| 3934 | COPYSLOT(tp_call); |
| 3935 | COPYSLOT(tp_str); |
| 3936 | { |
| 3937 | /* Copy comparison-related slots only when |
| 3938 | not overriding them anywhere */ |
| 3939 | if (type->tp_richcompare == NULL && |
| 3940 | type->tp_hash == NULL && |
| 3941 | !overrides_hash(type)) |
| 3942 | { |
| 3943 | type->tp_richcompare = base->tp_richcompare; |
| 3944 | type->tp_hash = base->tp_hash; |
| 3945 | } |
| 3946 | } |
| 3947 | { |
| 3948 | COPYSLOT(tp_iter); |
| 3949 | COPYSLOT(tp_iternext); |
| 3950 | } |
| 3951 | { |
| 3952 | COPYSLOT(tp_descr_get); |
| 3953 | COPYSLOT(tp_descr_set); |
| 3954 | COPYSLOT(tp_dictoffset); |
| 3955 | COPYSLOT(tp_init); |
| 3956 | COPYSLOT(tp_alloc); |
| 3957 | COPYSLOT(tp_is_gc); |
| 3958 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3959 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3960 | /* They agree about gc. */ |
| 3961 | COPYSLOT(tp_free); |
| 3962 | } |
| 3963 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3964 | type->tp_free == NULL && |
| 3965 | base->tp_free == PyObject_Free) { |
| 3966 | /* A bit of magic to plug in the correct default |
| 3967 | * tp_free function when a derived class adds gc, |
| 3968 | * didn't define tp_free, and the base uses the |
| 3969 | * default non-gc tp_free. |
| 3970 | */ |
| 3971 | type->tp_free = PyObject_GC_Del; |
| 3972 | } |
| 3973 | /* else they didn't agree about gc, and there isn't something |
| 3974 | * obvious to be done -- the type is on its own. |
| 3975 | */ |
| 3976 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3979 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3980 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3981 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3982 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | PyObject *dict, *bases; |
| 3985 | PyTypeObject *base; |
| 3986 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3989 | assert(type->tp_dict != NULL); |
| 3990 | return 0; |
| 3991 | } |
| 3992 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3994 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3995 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3996 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3997 | /* PyType_Ready is the closest thing we have to a choke point |
| 3998 | * for type objects, so is the best place I can think of to try |
| 3999 | * to get type objects into the doubly-linked list of all objects. |
| 4000 | * Still, not all type objects go thru PyType_Ready. |
| 4001 | */ |
| 4002 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4003 | #endif |
| 4004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4006 | base = type->tp_base; |
| 4007 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4008 | base = type->tp_base = &PyBaseObject_Type; |
| 4009 | Py_INCREF(base); |
| 4010 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4012 | /* Now the only way base can still be NULL is if type is |
| 4013 | * &PyBaseObject_Type. |
| 4014 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4016 | /* Initialize the base class */ |
| 4017 | if (base != NULL && base->tp_dict == NULL) { |
| 4018 | if (PyType_Ready(base) < 0) |
| 4019 | goto error; |
| 4020 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4023 | compilable separately on Windows can call PyType_Ready() instead of |
| 4024 | initializing the ob_type field of their type objects. */ |
| 4025 | /* The test for base != NULL is really unnecessary, since base is only |
| 4026 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4027 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4028 | know that. */ |
| 4029 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4030 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | /* Initialize tp_bases */ |
| 4033 | bases = type->tp_bases; |
| 4034 | if (bases == NULL) { |
| 4035 | if (base == NULL) |
| 4036 | bases = PyTuple_New(0); |
| 4037 | else |
| 4038 | bases = PyTuple_Pack(1, base); |
| 4039 | if (bases == NULL) |
| 4040 | goto error; |
| 4041 | type->tp_bases = bases; |
| 4042 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4044 | /* Initialize tp_dict */ |
| 4045 | dict = type->tp_dict; |
| 4046 | if (dict == NULL) { |
| 4047 | dict = PyDict_New(); |
| 4048 | if (dict == NULL) |
| 4049 | goto error; |
| 4050 | type->tp_dict = dict; |
| 4051 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | /* Add type-specific descriptors to tp_dict */ |
| 4054 | if (add_operators(type) < 0) |
| 4055 | goto error; |
| 4056 | if (type->tp_methods != NULL) { |
| 4057 | if (add_methods(type, type->tp_methods) < 0) |
| 4058 | goto error; |
| 4059 | } |
| 4060 | if (type->tp_members != NULL) { |
| 4061 | if (add_members(type, type->tp_members) < 0) |
| 4062 | goto error; |
| 4063 | } |
| 4064 | if (type->tp_getset != NULL) { |
| 4065 | if (add_getset(type, type->tp_getset) < 0) |
| 4066 | goto error; |
| 4067 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | /* Calculate method resolution order */ |
| 4070 | if (mro_internal(type) < 0) { |
| 4071 | goto error; |
| 4072 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4074 | /* Inherit special flags from dominant base */ |
| 4075 | if (type->tp_base != NULL) |
| 4076 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4078 | /* Initialize tp_dict properly */ |
| 4079 | bases = type->tp_mro; |
| 4080 | assert(bases != NULL); |
| 4081 | assert(PyTuple_Check(bases)); |
| 4082 | n = PyTuple_GET_SIZE(bases); |
| 4083 | for (i = 1; i < n; i++) { |
| 4084 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4085 | if (PyType_Check(b)) |
| 4086 | inherit_slots(type, (PyTypeObject *)b); |
| 4087 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | /* Sanity check for tp_free. */ |
| 4090 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4091 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4092 | /* This base class needs to call tp_free, but doesn't have |
| 4093 | * one, or its tp_free is for non-gc'ed objects. |
| 4094 | */ |
| 4095 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4096 | "gc and is a base type but has inappropriate " |
| 4097 | "tp_free slot", |
| 4098 | type->tp_name); |
| 4099 | goto error; |
| 4100 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4102 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4103 | the tp_doc slot. |
| 4104 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4105 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4106 | if (type->tp_doc != NULL) { |
| 4107 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 4108 | if (doc == NULL) |
| 4109 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4110 | _PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4111 | Py_DECREF(doc); |
| 4112 | } else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4113 | _PyDict_SetItemId(type->tp_dict, |
| 4114 | &PyId___doc__, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | } |
| 4116 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4118 | /* Hack for tp_hash and __hash__. |
| 4119 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4120 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4121 | tp_dict['__hash__'] equal to None. |
| 4122 | This signals that __hash__ is not inherited. |
| 4123 | */ |
| 4124 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4125 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 4126 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4127 | goto error; |
| 4128 | type->tp_hash = PyObject_HashNotImplemented; |
| 4129 | } |
| 4130 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | /* Some more special stuff */ |
| 4133 | base = type->tp_base; |
| 4134 | if (base != NULL) { |
| 4135 | if (type->tp_as_number == NULL) |
| 4136 | type->tp_as_number = base->tp_as_number; |
| 4137 | if (type->tp_as_sequence == NULL) |
| 4138 | type->tp_as_sequence = base->tp_as_sequence; |
| 4139 | if (type->tp_as_mapping == NULL) |
| 4140 | type->tp_as_mapping = base->tp_as_mapping; |
| 4141 | if (type->tp_as_buffer == NULL) |
| 4142 | type->tp_as_buffer = base->tp_as_buffer; |
| 4143 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 | /* Link into each base class's list of subclasses */ |
| 4146 | bases = type->tp_bases; |
| 4147 | n = PyTuple_GET_SIZE(bases); |
| 4148 | for (i = 0; i < n; i++) { |
| 4149 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4150 | if (PyType_Check(b) && |
| 4151 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4152 | goto error; |
| 4153 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | /* Warn for a type that implements tp_compare (now known as |
| 4156 | tp_reserved) but not tp_richcompare. */ |
| 4157 | if (type->tp_reserved && !type->tp_richcompare) { |
| 4158 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4159 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 4160 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4161 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4162 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4163 | if (error == -1) |
| 4164 | goto error; |
| 4165 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | /* All done -- set the ready flag */ |
| 4168 | assert(type->tp_dict != NULL); |
| 4169 | type->tp_flags = |
| 4170 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4171 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4172 | |
| 4173 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4174 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4175 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4178 | static int |
| 4179 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | Py_ssize_t i; |
| 4182 | int result; |
| 4183 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | list = base->tp_subclasses; |
| 4186 | if (list == NULL) { |
| 4187 | base->tp_subclasses = list = PyList_New(0); |
| 4188 | if (list == NULL) |
| 4189 | return -1; |
| 4190 | } |
| 4191 | assert(PyList_Check(list)); |
| 4192 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 4193 | i = PyList_GET_SIZE(list); |
| 4194 | while (--i >= 0) { |
| 4195 | ref = PyList_GET_ITEM(list, i); |
| 4196 | assert(PyWeakref_CheckRef(ref)); |
| 4197 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 4198 | return PyList_SetItem(list, i, newobj); |
| 4199 | } |
| 4200 | result = PyList_Append(list, newobj); |
| 4201 | Py_DECREF(newobj); |
| 4202 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4205 | static void |
| 4206 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | Py_ssize_t i; |
| 4209 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | list = base->tp_subclasses; |
| 4212 | if (list == NULL) { |
| 4213 | return; |
| 4214 | } |
| 4215 | assert(PyList_Check(list)); |
| 4216 | i = PyList_GET_SIZE(list); |
| 4217 | while (--i >= 0) { |
| 4218 | ref = PyList_GET_ITEM(list, i); |
| 4219 | assert(PyWeakref_CheckRef(ref)); |
| 4220 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4221 | /* this can't fail, right? */ |
| 4222 | PySequence_DelItem(list, i); |
| 4223 | return; |
| 4224 | } |
| 4225 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4226 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4227 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4228 | static int |
| 4229 | check_num_args(PyObject *ob, int n) |
| 4230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | if (!PyTuple_CheckExact(ob)) { |
| 4232 | PyErr_SetString(PyExc_SystemError, |
| 4233 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4234 | return 0; |
| 4235 | } |
| 4236 | if (n == PyTuple_GET_SIZE(ob)) |
| 4237 | return 1; |
| 4238 | PyErr_Format( |
| 4239 | PyExc_TypeError, |
| 4240 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4241 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4244 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4245 | |
| 4246 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4248 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4249 | Most tables have only one entry; the tables for binary operators have two |
| 4250 | entries, one regular and one with reversed arguments. */ |
| 4251 | |
| 4252 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4253 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | lenfunc func = (lenfunc)wrapped; |
| 4256 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | if (!check_num_args(args, 0)) |
| 4259 | return NULL; |
| 4260 | res = (*func)(self); |
| 4261 | if (res == -1 && PyErr_Occurred()) |
| 4262 | return NULL; |
| 4263 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4266 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4267 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | inquiry func = (inquiry)wrapped; |
| 4270 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | if (!check_num_args(args, 0)) |
| 4273 | return NULL; |
| 4274 | res = (*func)(self); |
| 4275 | if (res == -1 && PyErr_Occurred()) |
| 4276 | return NULL; |
| 4277 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4281 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | binaryfunc func = (binaryfunc)wrapped; |
| 4284 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | if (!check_num_args(args, 1)) |
| 4287 | return NULL; |
| 4288 | other = PyTuple_GET_ITEM(args, 0); |
| 4289 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4293 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | binaryfunc func = (binaryfunc)wrapped; |
| 4296 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | if (!check_num_args(args, 1)) |
| 4299 | return NULL; |
| 4300 | other = PyTuple_GET_ITEM(args, 0); |
| 4301 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4305 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | binaryfunc func = (binaryfunc)wrapped; |
| 4308 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | if (!check_num_args(args, 1)) |
| 4311 | return NULL; |
| 4312 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4316 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4317 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4320 | PyObject *other; |
| 4321 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4326 | return NULL; |
| 4327 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4330 | static PyObject * |
| 4331 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4333 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4334 | PyObject *other; |
| 4335 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4340 | return NULL; |
| 4341 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4344 | static PyObject * |
| 4345 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | if (!check_num_args(args, 0)) |
| 4350 | return NULL; |
| 4351 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4354 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4355 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4358 | PyObject* o; |
| 4359 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4362 | return NULL; |
| 4363 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4364 | if (i == -1 && PyErr_Occurred()) |
| 4365 | return NULL; |
| 4366 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4369 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4370 | getindex(PyObject *self, PyObject *arg) |
| 4371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4375 | if (i == -1 && PyErr_Occurred()) |
| 4376 | return -1; |
| 4377 | if (i < 0) { |
| 4378 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4379 | if (sq && sq->sq_length) { |
| 4380 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4381 | if (n < 0) |
| 4382 | return -1; |
| 4383 | i += n; |
| 4384 | } |
| 4385 | } |
| 4386 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | static PyObject * |
| 4390 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4392 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4393 | PyObject *arg; |
| 4394 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4397 | arg = PyTuple_GET_ITEM(args, 0); |
| 4398 | i = getindex(self, arg); |
| 4399 | if (i == -1 && PyErr_Occurred()) |
| 4400 | return NULL; |
| 4401 | return (*func)(self, i); |
| 4402 | } |
| 4403 | check_num_args(args, 1); |
| 4404 | assert(PyErr_Occurred()); |
| 4405 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4408 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4409 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4412 | Py_ssize_t i; |
| 4413 | int res; |
| 4414 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4417 | return NULL; |
| 4418 | i = getindex(self, arg); |
| 4419 | if (i == -1 && PyErr_Occurred()) |
| 4420 | return NULL; |
| 4421 | res = (*func)(self, i, value); |
| 4422 | if (res == -1 && PyErr_Occurred()) |
| 4423 | return NULL; |
| 4424 | Py_INCREF(Py_None); |
| 4425 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4428 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4429 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4432 | Py_ssize_t i; |
| 4433 | int res; |
| 4434 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | if (!check_num_args(args, 1)) |
| 4437 | return NULL; |
| 4438 | arg = PyTuple_GET_ITEM(args, 0); |
| 4439 | i = getindex(self, arg); |
| 4440 | if (i == -1 && PyErr_Occurred()) |
| 4441 | return NULL; |
| 4442 | res = (*func)(self, i, NULL); |
| 4443 | if (res == -1 && PyErr_Occurred()) |
| 4444 | return NULL; |
| 4445 | Py_INCREF(Py_None); |
| 4446 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4449 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4450 | static PyObject * |
| 4451 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | objobjproc func = (objobjproc)wrapped; |
| 4454 | int res; |
| 4455 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | if (!check_num_args(args, 1)) |
| 4458 | return NULL; |
| 4459 | value = PyTuple_GET_ITEM(args, 0); |
| 4460 | res = (*func)(self, value); |
| 4461 | if (res == -1 && PyErr_Occurred()) |
| 4462 | return NULL; |
| 4463 | else |
| 4464 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4467 | static PyObject * |
| 4468 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | objobjargproc func = (objobjargproc)wrapped; |
| 4471 | int res; |
| 4472 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4475 | return NULL; |
| 4476 | res = (*func)(self, key, value); |
| 4477 | if (res == -1 && PyErr_Occurred()) |
| 4478 | return NULL; |
| 4479 | Py_INCREF(Py_None); |
| 4480 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4483 | static PyObject * |
| 4484 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 | objobjargproc func = (objobjargproc)wrapped; |
| 4487 | int res; |
| 4488 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4490 | if (!check_num_args(args, 1)) |
| 4491 | return NULL; |
| 4492 | key = PyTuple_GET_ITEM(args, 0); |
| 4493 | res = (*func)(self, key, NULL); |
| 4494 | if (res == -1 && PyErr_Occurred()) |
| 4495 | return NULL; |
| 4496 | Py_INCREF(Py_None); |
| 4497 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4500 | /* Helper to check for object.__setattr__ or __delattr__ applied to a type. |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 4501 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4502 | static int |
| 4503 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | PyTypeObject *type = Py_TYPE(self); |
| 4506 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4507 | type = type->tp_base; |
| 4508 | /* If type is NULL now, this is a really weird type. |
| 4509 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4510 | if (type && type->tp_setattro != func) { |
| 4511 | PyErr_Format(PyExc_TypeError, |
| 4512 | "can't apply this %s to %s object", |
| 4513 | what, |
| 4514 | type->tp_name); |
| 4515 | return 0; |
| 4516 | } |
| 4517 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4520 | static PyObject * |
| 4521 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | setattrofunc func = (setattrofunc)wrapped; |
| 4524 | int res; |
| 4525 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4527 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4528 | return NULL; |
| 4529 | if (!hackcheck(self, func, "__setattr__")) |
| 4530 | return NULL; |
| 4531 | res = (*func)(self, name, value); |
| 4532 | if (res < 0) |
| 4533 | return NULL; |
| 4534 | Py_INCREF(Py_None); |
| 4535 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | static PyObject * |
| 4539 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | setattrofunc func = (setattrofunc)wrapped; |
| 4542 | int res; |
| 4543 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | if (!check_num_args(args, 1)) |
| 4546 | return NULL; |
| 4547 | name = PyTuple_GET_ITEM(args, 0); |
| 4548 | if (!hackcheck(self, func, "__delattr__")) |
| 4549 | return NULL; |
| 4550 | res = (*func)(self, name, NULL); |
| 4551 | if (res < 0) |
| 4552 | return NULL; |
| 4553 | Py_INCREF(Py_None); |
| 4554 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4557 | static PyObject * |
| 4558 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4561 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | if (!check_num_args(args, 0)) |
| 4564 | return NULL; |
| 4565 | res = (*func)(self); |
| 4566 | if (res == -1 && PyErr_Occurred()) |
| 4567 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4568 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4571 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4572 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4579 | static PyObject * |
| 4580 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4583 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | if (!check_num_args(args, 1)) |
| 4586 | return NULL; |
| 4587 | other = PyTuple_GET_ITEM(args, 0); |
| 4588 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | #undef RICHCMP_WRAPPER |
| 4592 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4593 | static PyObject * \ |
| 4594 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4595 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4599 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4600 | RICHCMP_WRAPPER(le, Py_LE) |
| 4601 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4602 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4603 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4604 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4605 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4606 | static PyObject * |
| 4607 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4609 | unaryfunc func = (unaryfunc)wrapped; |
| 4610 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 | if (!check_num_args(args, 0)) |
| 4613 | return NULL; |
| 4614 | res = (*func)(self); |
| 4615 | if (res == NULL && !PyErr_Occurred()) |
| 4616 | PyErr_SetNone(PyExc_StopIteration); |
| 4617 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4620 | static PyObject * |
| 4621 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4623 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4624 | PyObject *obj; |
| 4625 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4628 | return NULL; |
| 4629 | if (obj == Py_None) |
| 4630 | obj = NULL; |
| 4631 | if (type == Py_None) |
| 4632 | type = NULL; |
| 4633 | if (type == NULL &&obj == NULL) { |
| 4634 | PyErr_SetString(PyExc_TypeError, |
| 4635 | "__get__(None, None) is invalid"); |
| 4636 | return NULL; |
| 4637 | } |
| 4638 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4641 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4642 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4645 | PyObject *obj, *value; |
| 4646 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4649 | return NULL; |
| 4650 | ret = (*func)(self, obj, value); |
| 4651 | if (ret < 0) |
| 4652 | return NULL; |
| 4653 | Py_INCREF(Py_None); |
| 4654 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4655 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4656 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4657 | static PyObject * |
| 4658 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4661 | PyObject *obj; |
| 4662 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | if (!check_num_args(args, 1)) |
| 4665 | return NULL; |
| 4666 | obj = PyTuple_GET_ITEM(args, 0); |
| 4667 | ret = (*func)(self, obj, NULL); |
| 4668 | if (ret < 0) |
| 4669 | return NULL; |
| 4670 | Py_INCREF(Py_None); |
| 4671 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4672 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4673 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4674 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4675 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | if (func(self, args, kwds) < 0) |
| 4680 | return NULL; |
| 4681 | Py_INCREF(Py_None); |
| 4682 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4685 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4686 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | PyTypeObject *type, *subtype, *staticbase; |
| 4689 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | if (self == NULL || !PyType_Check(self)) |
| 4692 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4693 | type = (PyTypeObject *)self; |
| 4694 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4695 | PyErr_Format(PyExc_TypeError, |
| 4696 | "%s.__new__(): not enough arguments", |
| 4697 | type->tp_name); |
| 4698 | return NULL; |
| 4699 | } |
| 4700 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4701 | if (!PyType_Check(arg0)) { |
| 4702 | PyErr_Format(PyExc_TypeError, |
| 4703 | "%s.__new__(X): X is not a type object (%s)", |
| 4704 | type->tp_name, |
| 4705 | Py_TYPE(arg0)->tp_name); |
| 4706 | return NULL; |
| 4707 | } |
| 4708 | subtype = (PyTypeObject *)arg0; |
| 4709 | if (!PyType_IsSubtype(subtype, type)) { |
| 4710 | PyErr_Format(PyExc_TypeError, |
| 4711 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4712 | type->tp_name, |
| 4713 | subtype->tp_name, |
| 4714 | subtype->tp_name, |
| 4715 | type->tp_name); |
| 4716 | return NULL; |
| 4717 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | /* Check that the use doesn't do something silly and unsafe like |
| 4720 | object.__new__(dict). To do this, we check that the |
| 4721 | most derived base that's not a heap type is this type. */ |
| 4722 | staticbase = subtype; |
| 4723 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4724 | staticbase = staticbase->tp_base; |
| 4725 | /* If staticbase is NULL now, it is a really weird type. |
| 4726 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4727 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4728 | PyErr_Format(PyExc_TypeError, |
| 4729 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4730 | type->tp_name, |
| 4731 | subtype->tp_name, |
| 4732 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4733 | return NULL; |
| 4734 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4737 | if (args == NULL) |
| 4738 | return NULL; |
| 4739 | res = type->tp_new(subtype, args, kwds); |
| 4740 | Py_DECREF(args); |
| 4741 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4744 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4746 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4747 | "a new object with type S, a subtype of T")}, |
| 4748 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4749 | }; |
| 4750 | |
| 4751 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4752 | add_tp_new_wrapper(PyTypeObject *type) |
| 4753 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4755 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4756 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | return 0; |
| 4758 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4759 | if (func == NULL) |
| 4760 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4761 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | Py_DECREF(func); |
| 4763 | return -1; |
| 4764 | } |
| 4765 | Py_DECREF(func); |
| 4766 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4769 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4770 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4771 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4772 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4773 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4774 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4775 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4776 | _Py_static_string(id, OPSTR); \ |
| 4777 | return call_method(self, &id, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4780 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4781 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4782 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4783 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4784 | _Py_static_string(id, OPSTR); \ |
| 4785 | return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4788 | /* Boolean helper for SLOT1BINFULL(). |
| 4789 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4790 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4791 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | PyObject *a, *b; |
| 4794 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4795 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4796 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | if (b == NULL) { |
| 4798 | PyErr_Clear(); |
| 4799 | /* If right doesn't have it, it's not overloaded */ |
| 4800 | return 0; |
| 4801 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4802 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4803 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | if (a == NULL) { |
| 4805 | PyErr_Clear(); |
| 4806 | Py_DECREF(b); |
| 4807 | /* If right has it but left doesn't, it's overloaded */ |
| 4808 | return 1; |
| 4809 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4811 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4812 | Py_DECREF(a); |
| 4813 | Py_DECREF(b); |
| 4814 | if (ok < 0) { |
| 4815 | PyErr_Clear(); |
| 4816 | return 0; |
| 4817 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4822 | |
| 4823 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4824 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4825 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4826 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4827 | _Py_static_string(op_id, OPSTR); \ |
| 4828 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4829 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4830 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4831 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4832 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4833 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4834 | PyObject *r; \ |
| 4835 | if (do_other && \ |
| 4836 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4837 | method_is_overloaded(self, other, &rop_id)) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4838 | r = call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | if (r != Py_NotImplemented) \ |
| 4840 | return r; \ |
| 4841 | Py_DECREF(r); \ |
| 4842 | do_other = 0; \ |
| 4843 | } \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4844 | r = call_maybe(self, &op_id, "(O)", other); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | if (r != Py_NotImplemented || \ |
| 4846 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4847 | return r; \ |
| 4848 | Py_DECREF(r); \ |
| 4849 | } \ |
| 4850 | if (do_other) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4851 | return call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4852 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4853 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4858 | |
| 4859 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4860 | static PyObject * \ |
| 4861 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4862 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4863 | _Py_static_string(id, #OPSTR); \ |
| 4864 | return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4867 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4868 | slot_sq_length(PyObject *self) |
| 4869 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4870 | _Py_IDENTIFIER(__len__); |
| 4871 | PyObject *res = call_method(self, &PyId___len__, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | if (res == NULL) |
| 4875 | return -1; |
| 4876 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 4877 | Py_DECREF(res); |
| 4878 | if (len < 0) { |
| 4879 | if (!PyErr_Occurred()) |
| 4880 | PyErr_SetString(PyExc_ValueError, |
| 4881 | "__len__() should return >= 0"); |
| 4882 | return -1; |
| 4883 | } |
| 4884 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4887 | /* Super-optimized version of slot_sq_item. |
| 4888 | Other slots could do the same... */ |
| 4889 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4890 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4891 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4892 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4893 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4894 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4895 | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4896 | if (func != NULL) { |
| 4897 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 4898 | Py_INCREF(func); |
| 4899 | else { |
| 4900 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 4901 | if (func == NULL) { |
| 4902 | return NULL; |
| 4903 | } |
| 4904 | } |
| 4905 | ival = PyLong_FromSsize_t(i); |
| 4906 | if (ival != NULL) { |
| 4907 | args = PyTuple_New(1); |
| 4908 | if (args != NULL) { |
| 4909 | PyTuple_SET_ITEM(args, 0, ival); |
| 4910 | retval = PyObject_Call(func, args, NULL); |
| 4911 | Py_XDECREF(args); |
| 4912 | Py_XDECREF(func); |
| 4913 | return retval; |
| 4914 | } |
| 4915 | } |
| 4916 | } |
| 4917 | else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4918 | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4919 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4920 | } |
| 4921 | Py_XDECREF(args); |
| 4922 | Py_XDECREF(ival); |
| 4923 | Py_XDECREF(func); |
| 4924 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4927 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4928 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4930 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4931 | _Py_IDENTIFIER(__delitem__); |
| 4932 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4934 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4935 | res = call_method(self, &PyId___delitem__, "(n)", index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4936 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4937 | res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4938 | if (res == NULL) |
| 4939 | return -1; |
| 4940 | Py_DECREF(res); |
| 4941 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4945 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | PyObject *func, *res, *args; |
| 4948 | int result = -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4949 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4950 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4951 | func = lookup_maybe(self, &PyId___contains__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | if (func != NULL) { |
| 4953 | args = PyTuple_Pack(1, value); |
| 4954 | if (args == NULL) |
| 4955 | res = NULL; |
| 4956 | else { |
| 4957 | res = PyObject_Call(func, args, NULL); |
| 4958 | Py_DECREF(args); |
| 4959 | } |
| 4960 | Py_DECREF(func); |
| 4961 | if (res != NULL) { |
| 4962 | result = PyObject_IsTrue(res); |
| 4963 | Py_DECREF(res); |
| 4964 | } |
| 4965 | } |
| 4966 | else if (! PyErr_Occurred()) { |
| 4967 | /* Possible results: -1 and 1 */ |
| 4968 | result = (int)_PySequence_IterSearch(self, value, |
| 4969 | PY_ITERSEARCH_CONTAINS); |
| 4970 | } |
| 4971 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4974 | #define slot_mp_length slot_sq_length |
| 4975 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4976 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4977 | |
| 4978 | static int |
| 4979 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4982 | _Py_IDENTIFIER(__delitem__); |
| 4983 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4986 | res = call_method(self, &PyId___delitem__, "(O)", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4987 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4988 | res = call_method(self, &PyId___setitem__, "(OO)", key, value); |
| 4989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4990 | if (res == NULL) |
| 4991 | return -1; |
| 4992 | Py_DECREF(res); |
| 4993 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4996 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4997 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4998 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4999 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5000 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5001 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5002 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5003 | |
| 5004 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5005 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5006 | |
| 5007 | static PyObject * |
| 5008 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5009 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5010 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | if (modulus == Py_None) |
| 5013 | return slot_nb_power_binary(self, other); |
| 5014 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5015 | can call this when the second argument's type uses |
| 5016 | slot_nb_power, so check before calling self.__pow__. */ |
| 5017 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5018 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5019 | return call_method(self, &PyId___pow__, "(OO)", other, modulus); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5021 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | SLOT0(slot_nb_negative, "__neg__") |
| 5025 | SLOT0(slot_nb_positive, "__pos__") |
| 5026 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5027 | |
| 5028 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5029 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5030 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5031 | PyObject *func, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | int result = -1; |
| 5033 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5034 | _Py_IDENTIFIER(__len__); |
| 5035 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5036 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5037 | func = lookup_maybe(self, &PyId___bool__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | if (func == NULL) { |
| 5039 | if (PyErr_Occurred()) |
| 5040 | return -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5041 | func = lookup_maybe(self, &PyId___len__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | if (func == NULL) |
| 5043 | return PyErr_Occurred() ? -1 : 1; |
| 5044 | using_len = 1; |
| 5045 | } |
| 5046 | args = PyTuple_New(0); |
| 5047 | if (args != NULL) { |
| 5048 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5049 | Py_DECREF(args); |
| 5050 | if (temp != NULL) { |
| 5051 | if (using_len) { |
| 5052 | /* enforced by slot_nb_len */ |
| 5053 | result = PyObject_IsTrue(temp); |
| 5054 | } |
| 5055 | else if (PyBool_Check(temp)) { |
| 5056 | result = PyObject_IsTrue(temp); |
| 5057 | } |
| 5058 | else { |
| 5059 | PyErr_Format(PyExc_TypeError, |
| 5060 | "__bool__ should return " |
| 5061 | "bool, returned %s", |
| 5062 | Py_TYPE(temp)->tp_name); |
| 5063 | result = -1; |
| 5064 | } |
| 5065 | Py_DECREF(temp); |
| 5066 | } |
| 5067 | } |
| 5068 | Py_DECREF(func); |
| 5069 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5072 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5073 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5074 | slot_nb_index(PyObject *self) |
| 5075 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5076 | _Py_IDENTIFIER(__index__); |
| 5077 | return call_method(self, &PyId___index__, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5081 | SLOT0(slot_nb_invert, "__invert__") |
| 5082 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5083 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5084 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5085 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5086 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5087 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5088 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5089 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5090 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5091 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5092 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5093 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5094 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5095 | static PyObject * |
| 5096 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5097 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5098 | _Py_IDENTIFIER(__ipow__); |
| 5099 | return call_method(self, &PyId___ipow__, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5100 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5101 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5102 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5103 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5104 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5105 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5106 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5107 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5108 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5109 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5110 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5111 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5112 | static PyObject * |
| 5113 | slot_tp_repr(PyObject *self) |
| 5114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5116 | _Py_IDENTIFIER(__repr__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5117 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5118 | func = lookup_method(self, &PyId___repr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | if (func != NULL) { |
| 5120 | res = PyEval_CallObject(func, NULL); |
| 5121 | Py_DECREF(func); |
| 5122 | return res; |
| 5123 | } |
| 5124 | PyErr_Clear(); |
| 5125 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5126 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | static PyObject * |
| 5130 | slot_tp_str(PyObject *self) |
| 5131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5132 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5133 | _Py_IDENTIFIER(__str__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5134 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5135 | func = lookup_method(self, &PyId___str__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5136 | if (func != NULL) { |
| 5137 | res = PyEval_CallObject(func, NULL); |
| 5138 | Py_DECREF(func); |
| 5139 | return res; |
| 5140 | } |
| 5141 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5142 | /* PyObject *ress; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | PyErr_Clear(); |
| 5144 | res = slot_tp_repr(self); |
| 5145 | if (!res) |
| 5146 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5147 | /* XXX this is non-sensical. Why should we return |
| 5148 | a bytes object from __str__. Is this code even |
| 5149 | used? - mvl */ |
| 5150 | assert(0); |
| 5151 | return res; |
| 5152 | /* |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 5153 | ress = _PyUnicode_AsDefaultEncodedString(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5154 | Py_DECREF(res); |
| 5155 | return ress; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5156 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5157 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5158 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5159 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5160 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5161 | slot_tp_hash(PyObject *self) |
| 5162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5163 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5164 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5165 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5166 | func = lookup_method(self, &PyId___hash__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | if (func == Py_None) { |
| 5169 | Py_DECREF(func); |
| 5170 | func = NULL; |
| 5171 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | if (func == NULL) { |
| 5174 | return PyObject_HashNotImplemented(self); |
| 5175 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5177 | res = PyEval_CallObject(func, NULL); |
| 5178 | Py_DECREF(func); |
| 5179 | if (res == NULL) |
| 5180 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5181 | |
| 5182 | if (!PyLong_Check(res)) { |
| 5183 | PyErr_SetString(PyExc_TypeError, |
| 5184 | "__hash__ method should return an integer"); |
| 5185 | return -1; |
| 5186 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5187 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5188 | hashable Python object x, hash(x) will always lie within the range of |
| 5189 | Py_hash_t. Therefore our transformation must preserve values that |
| 5190 | already lie within this range, to ensure that if x.__hash__() returns |
| 5191 | hash(y) then hash(x) == hash(y). */ |
| 5192 | h = PyLong_AsSsize_t(res); |
| 5193 | if (h == -1 && PyErr_Occurred()) { |
| 5194 | /* res was not within the range of a Py_hash_t, so we're free to |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5195 | use any sufficiently bit-mixing transformation; |
| 5196 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5197 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5199 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5200 | /* -1 is reserved for errors. */ |
| 5201 | if (h == -1) |
| 5202 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5204 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | static PyObject * |
| 5208 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5209 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5210 | _Py_IDENTIFIER(__call__); |
| 5211 | PyObject *meth = lookup_method(self, &PyId___call__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | if (meth == NULL) |
| 5215 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5219 | Py_DECREF(meth); |
| 5220 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5223 | /* There are two slot dispatch functions for tp_getattro. |
| 5224 | |
| 5225 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5226 | but no __getattr__ hook is present; |
| 5227 | |
| 5228 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5229 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5230 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5231 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5232 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5233 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5234 | static PyObject * |
| 5235 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5236 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5237 | return call_method(self, &PyId___getattribute__, "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5240 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5241 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5243 | PyObject *res, *descr = NULL; |
| 5244 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5246 | if (f != NULL) { |
| 5247 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5248 | if (descr == NULL) |
| 5249 | return NULL; |
| 5250 | else |
| 5251 | attr = descr; |
| 5252 | } |
| 5253 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5254 | Py_XDECREF(descr); |
| 5255 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5259 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5261 | PyTypeObject *tp = Py_TYPE(self); |
| 5262 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5263 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5265 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5266 | method fully for each attribute lookup for classes with |
| 5267 | __getattr__, even when the attribute is present. So we use |
| 5268 | _PyType_Lookup and create the method only when needed, with |
| 5269 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5270 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | if (getattr == NULL) { |
| 5272 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5273 | tp->tp_getattro = slot_tp_getattro; |
| 5274 | return slot_tp_getattro(self, name); |
| 5275 | } |
| 5276 | Py_INCREF(getattr); |
| 5277 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5278 | method fully for each attribute lookup for classes with |
| 5279 | __getattr__, even when self has the default __getattribute__ |
| 5280 | method. So we use _PyType_Lookup and create the method only when |
| 5281 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5282 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | if (getattribute == NULL || |
| 5284 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5285 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5286 | (void *)PyObject_GenericGetAttr)) |
| 5287 | res = PyObject_GenericGetAttr(self, name); |
| 5288 | else { |
| 5289 | Py_INCREF(getattribute); |
| 5290 | res = call_attribute(self, getattribute, name); |
| 5291 | Py_DECREF(getattribute); |
| 5292 | } |
| 5293 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5294 | PyErr_Clear(); |
| 5295 | res = call_attribute(self, getattr, name); |
| 5296 | } |
| 5297 | Py_DECREF(getattr); |
| 5298 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5301 | static int |
| 5302 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5305 | _Py_IDENTIFIER(__delattr__); |
| 5306 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5309 | res = call_method(self, &PyId___delattr__, "(O)", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5311 | res = call_method(self, &PyId___setattr__, "(OO)", name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | if (res == NULL) |
| 5313 | return -1; |
| 5314 | Py_DECREF(res); |
| 5315 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5318 | static _Py_Identifier name_op[] = { |
| 5319 | {0, "__lt__", 0}, |
| 5320 | {0, "__le__", 0}, |
| 5321 | {0, "__eq__", 0}, |
| 5322 | {0, "__ne__", 0}, |
| 5323 | {0, "__gt__", 0}, |
| 5324 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5325 | }; |
| 5326 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5327 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5328 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | PyObject *func, *args, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5331 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5332 | func = lookup_method(self, &name_op[op]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | if (func == NULL) { |
| 5334 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5335 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | } |
| 5337 | args = PyTuple_Pack(1, other); |
| 5338 | if (args == NULL) |
| 5339 | res = NULL; |
| 5340 | else { |
| 5341 | res = PyObject_Call(func, args, NULL); |
| 5342 | Py_DECREF(args); |
| 5343 | } |
| 5344 | Py_DECREF(func); |
| 5345 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5348 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5349 | slot_tp_iter(PyObject *self) |
| 5350 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5351 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5352 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5353 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5354 | func = lookup_method(self, &PyId___iter__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5355 | if (func != NULL) { |
| 5356 | PyObject *args; |
| 5357 | args = res = PyTuple_New(0); |
| 5358 | if (args != NULL) { |
| 5359 | res = PyObject_Call(func, args, NULL); |
| 5360 | Py_DECREF(args); |
| 5361 | } |
| 5362 | Py_DECREF(func); |
| 5363 | return res; |
| 5364 | } |
| 5365 | PyErr_Clear(); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5366 | func = lookup_method(self, &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | if (func == NULL) { |
| 5368 | PyErr_Format(PyExc_TypeError, |
| 5369 | "'%.200s' object is not iterable", |
| 5370 | Py_TYPE(self)->tp_name); |
| 5371 | return NULL; |
| 5372 | } |
| 5373 | Py_DECREF(func); |
| 5374 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5375 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5376 | |
| 5377 | static PyObject * |
| 5378 | slot_tp_iternext(PyObject *self) |
| 5379 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5380 | _Py_IDENTIFIER(__next__); |
| 5381 | return call_method(self, &PyId___next__, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5384 | static PyObject * |
| 5385 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | PyTypeObject *tp = Py_TYPE(self); |
| 5388 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5389 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5390 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5391 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | if (get == NULL) { |
| 5393 | /* Avoid further slowdowns */ |
| 5394 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5395 | tp->tp_descr_get = NULL; |
| 5396 | Py_INCREF(self); |
| 5397 | return self; |
| 5398 | } |
| 5399 | if (obj == NULL) |
| 5400 | obj = Py_None; |
| 5401 | if (type == NULL) |
| 5402 | type = Py_None; |
| 5403 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5404 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5405 | |
| 5406 | static int |
| 5407 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5410 | _Py_IDENTIFIER(__delete__); |
| 5411 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5414 | res = call_method(self, &PyId___delete__, "(O)", target); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5416 | res = call_method(self, &PyId___set__, "(OO)", target, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | if (res == NULL) |
| 5418 | return -1; |
| 5419 | Py_DECREF(res); |
| 5420 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | static int |
| 5424 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5425 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5426 | _Py_IDENTIFIER(__init__); |
| 5427 | PyObject *meth = lookup_method(self, &PyId___init__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | if (meth == NULL) |
| 5431 | return -1; |
| 5432 | res = PyObject_Call(meth, args, kwds); |
| 5433 | Py_DECREF(meth); |
| 5434 | if (res == NULL) |
| 5435 | return -1; |
| 5436 | if (res != Py_None) { |
| 5437 | PyErr_Format(PyExc_TypeError, |
| 5438 | "__init__() should return None, not '%.200s'", |
| 5439 | Py_TYPE(res)->tp_name); |
| 5440 | Py_DECREF(res); |
| 5441 | return -1; |
| 5442 | } |
| 5443 | Py_DECREF(res); |
| 5444 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | static PyObject * |
| 5448 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5450 | PyObject *func; |
| 5451 | PyObject *newargs, *x; |
| 5452 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5453 | _Py_IDENTIFIER(__new__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5454 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5455 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5456 | if (func == NULL) |
| 5457 | return NULL; |
| 5458 | assert(PyTuple_Check(args)); |
| 5459 | n = PyTuple_GET_SIZE(args); |
| 5460 | newargs = PyTuple_New(n+1); |
| 5461 | if (newargs == NULL) |
| 5462 | return NULL; |
| 5463 | Py_INCREF(type); |
| 5464 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5465 | for (i = 0; i < n; i++) { |
| 5466 | x = PyTuple_GET_ITEM(args, i); |
| 5467 | Py_INCREF(x); |
| 5468 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5469 | } |
| 5470 | x = PyObject_Call(func, newargs, kwds); |
| 5471 | Py_DECREF(newargs); |
| 5472 | Py_DECREF(func); |
| 5473 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5476 | static void |
| 5477 | slot_tp_del(PyObject *self) |
| 5478 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5479 | _Py_IDENTIFIER(__del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5480 | PyObject *del, *res; |
| 5481 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5483 | /* Temporarily resurrect the object. */ |
| 5484 | assert(self->ob_refcnt == 0); |
| 5485 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | /* Save the current exception, if any. */ |
| 5488 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5490 | /* Execute __del__ method, if any. */ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5491 | del = lookup_maybe(self, &PyId___del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | if (del != NULL) { |
| 5493 | res = PyEval_CallObject(del, NULL); |
| 5494 | if (res == NULL) |
| 5495 | PyErr_WriteUnraisable(del); |
| 5496 | else |
| 5497 | Py_DECREF(res); |
| 5498 | Py_DECREF(del); |
| 5499 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5501 | /* Restore the saved exception. */ |
| 5502 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5504 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5505 | * cause a recursive call. |
| 5506 | */ |
| 5507 | assert(self->ob_refcnt > 0); |
| 5508 | if (--self->ob_refcnt == 0) |
| 5509 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5511 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5512 | * never happened. |
| 5513 | */ |
| 5514 | { |
| 5515 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5516 | _Py_NewReference(self); |
| 5517 | self->ob_refcnt = refcnt; |
| 5518 | } |
| 5519 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5520 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5521 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5522 | * we need to undo that. */ |
| 5523 | _Py_DEC_REFTOTAL; |
| 5524 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5525 | * chain, so no more to do there. |
| 5526 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5527 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5528 | * undone. |
| 5529 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5530 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | --Py_TYPE(self)->tp_frees; |
| 5532 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5533 | #endif |
| 5534 | } |
| 5535 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5536 | |
| 5537 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5538 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5539 | structure, which incorporates the additional structures used for numbers, |
| 5540 | sequences and mappings. |
| 5541 | Note that multiple names may map to the same slot (e.g. __eq__, |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5542 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5543 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5544 | terminated with an all-zero entry. (This table is further initialized and |
| 5545 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5546 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5547 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5548 | |
| 5549 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5550 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5551 | #undef ETSLOT |
| 5552 | #undef SQSLOT |
| 5553 | #undef MPSLOT |
| 5554 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5555 | #undef UNSLOT |
| 5556 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5557 | #undef BINSLOT |
| 5558 | #undef RBINSLOT |
| 5559 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5560 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5562 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5563 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5565 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5566 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5568 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5569 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5571 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5573 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5575 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5577 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5578 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5580 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5581 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5583 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5584 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5586 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5587 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5589 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5590 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5591 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5592 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5593 | |
| 5594 | static slotdef slotdefs[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5596 | "x.__len__() <==> len(x)"), |
| 5597 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5598 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5599 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5600 | user-defined methods has unexpected side-effects, as shown by |
| 5601 | test_descr.notimplemented() */ |
| 5602 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5603 | "x.__add__(y) <==> x+y"), |
| 5604 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5605 | "x.__mul__(n) <==> x*n"), |
| 5606 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5607 | "x.__rmul__(n) <==> n*x"), |
| 5608 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5609 | "x.__getitem__(y) <==> x[y]"), |
| 5610 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5611 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5612 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5613 | "x.__delitem__(y) <==> del x[y]"), |
| 5614 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5615 | "x.__contains__(y) <==> y in x"), |
| 5616 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5617 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5618 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5619 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5621 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5622 | "x.__len__() <==> len(x)"), |
| 5623 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5624 | wrap_binaryfunc, |
| 5625 | "x.__getitem__(y) <==> x[y]"), |
| 5626 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5627 | wrap_objobjargproc, |
| 5628 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5629 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5630 | wrap_delitem, |
| 5631 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5634 | "+"), |
| 5635 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5636 | "+"), |
| 5637 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5638 | "-"), |
| 5639 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5640 | "-"), |
| 5641 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5642 | "*"), |
| 5643 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5644 | "*"), |
| 5645 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5646 | "%"), |
| 5647 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5648 | "%"), |
| 5649 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5650 | "divmod(x, y)"), |
| 5651 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5652 | "divmod(y, x)"), |
| 5653 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5654 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5655 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5656 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5657 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5658 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5659 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5660 | "abs(x)"), |
| 5661 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5662 | "x != 0"), |
| 5663 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5664 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5665 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5666 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5667 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5668 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5669 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5670 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5671 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5672 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5673 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5674 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5675 | "int(x)"), |
| 5676 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5677 | "float(x)"), |
| 5678 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5679 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
| 5680 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5681 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5682 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5683 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5685 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5687 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5688 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5689 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5691 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5693 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5695 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5697 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5698 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5699 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5701 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5702 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5703 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5704 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5705 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5706 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5707 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5710 | "x.__str__() <==> str(x)"), |
| 5711 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5712 | "x.__repr__() <==> repr(x)"), |
| 5713 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5714 | "x.__hash__() <==> hash(x)"), |
| 5715 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5716 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5717 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5718 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5719 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5720 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5721 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5722 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5723 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5724 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5725 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5726 | "x.__delattr__('name') <==> del x.name"), |
| 5727 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5728 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5729 | "x.__lt__(y) <==> x<y"), |
| 5730 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5731 | "x.__le__(y) <==> x<=y"), |
| 5732 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5733 | "x.__eq__(y) <==> x==y"), |
| 5734 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5735 | "x.__ne__(y) <==> x!=y"), |
| 5736 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5737 | "x.__gt__(y) <==> x>y"), |
| 5738 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5739 | "x.__ge__(y) <==> x>=y"), |
| 5740 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5741 | "x.__iter__() <==> iter(x)"), |
| 5742 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5743 | "x.__next__() <==> next(x)"), |
| 5744 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5745 | "descr.__get__(obj[, type]) -> value"), |
| 5746 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5747 | "descr.__set__(obj, value)"), |
| 5748 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5749 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5750 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5751 | "x.__init__(...) initializes x; " |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 5752 | "see help(type(x)) for signature", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | PyWrapperFlag_KEYWORDS), |
| 5754 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5755 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
| 5756 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5757 | }; |
| 5758 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5759 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5760 | pointer to the actual slot. This is not quite the same as simply adding |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5761 | the offset to the type pointer, since it takes care to indirect through the |
| 5762 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5763 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5764 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5765 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5767 | char *ptr; |
| 5768 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5770 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5771 | assert(offset >= 0); |
| 5772 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5773 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5774 | ptr = (char *)type->tp_as_sequence; |
| 5775 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5776 | } |
| 5777 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5778 | ptr = (char *)type->tp_as_mapping; |
| 5779 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5780 | } |
| 5781 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5782 | ptr = (char *)type->tp_as_number; |
| 5783 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5784 | } |
| 5785 | else { |
| 5786 | ptr = (char *)type; |
| 5787 | } |
| 5788 | if (ptr != NULL) |
| 5789 | ptr += offset; |
| 5790 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5791 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5792 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5793 | /* Length of array of slotdef pointers used to store slots with the |
| 5794 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5795 | the same __name__, for any __name__. Since that's a static property, it is |
| 5796 | appropriate to declare fixed-size arrays for this. */ |
| 5797 | #define MAX_EQUIV 10 |
| 5798 | |
| 5799 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5800 | exactly one slot function. The name must be an interned string. */ |
| 5801 | static void ** |
| 5802 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5804 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | /* pname and ptrs act as a little cache */ |
| 5807 | static PyObject *pname; |
| 5808 | static slotdef *ptrs[MAX_EQUIV]; |
| 5809 | slotdef *p, **pp; |
| 5810 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | if (pname != name) { |
| 5813 | /* Collect all slotdefs that match name into ptrs. */ |
| 5814 | pname = name; |
| 5815 | pp = ptrs; |
| 5816 | for (p = slotdefs; p->name_strobj; p++) { |
| 5817 | if (p->name_strobj == name) |
| 5818 | *pp++ = p; |
| 5819 | } |
| 5820 | *pp = NULL; |
| 5821 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5823 | /* Look in all matching slots of the type; if exactly one of these has |
| 5824 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5825 | res = NULL; |
| 5826 | for (pp = ptrs; *pp; pp++) { |
| 5827 | ptr = slotptr(type, (*pp)->offset); |
| 5828 | if (ptr == NULL || *ptr == NULL) |
| 5829 | continue; |
| 5830 | if (res != NULL) |
| 5831 | return NULL; |
| 5832 | res = ptr; |
| 5833 | } |
| 5834 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5837 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5838 | does some incredibly complex thinking and then sticks something into the |
| 5839 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5840 | interests, and then stores a generic wrapper or a specific function into |
| 5841 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5842 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5843 | static slotdef * |
| 5844 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | PyObject *descr; |
| 5847 | PyWrapperDescrObject *d; |
| 5848 | void *generic = NULL, *specific = NULL; |
| 5849 | int use_generic = 0; |
| 5850 | int offset = p->offset; |
| 5851 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | if (ptr == NULL) { |
| 5854 | do { |
| 5855 | ++p; |
| 5856 | } while (p->offset == offset); |
| 5857 | return p; |
| 5858 | } |
| 5859 | do { |
| 5860 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5861 | if (descr == NULL) { |
| 5862 | if (ptr == (void**)&type->tp_iternext) { |
| 5863 | specific = _PyObject_NextNotImplemented; |
| 5864 | } |
| 5865 | continue; |
| 5866 | } |
| 5867 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
| 5868 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5869 | if (tptr == NULL || tptr == ptr) |
| 5870 | generic = p->function; |
| 5871 | d = (PyWrapperDescrObject *)descr; |
| 5872 | if (d->d_base->wrapper == p->wrapper && |
| 5873 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 5874 | { |
| 5875 | if (specific == NULL || |
| 5876 | specific == d->d_wrapped) |
| 5877 | specific = d->d_wrapped; |
| 5878 | else |
| 5879 | use_generic = 1; |
| 5880 | } |
| 5881 | } |
| 5882 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 5883 | PyCFunction_GET_FUNCTION(descr) == |
| 5884 | (PyCFunction)tp_new_wrapper && |
| 5885 | ptr == (void**)&type->tp_new) |
| 5886 | { |
| 5887 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5888 | so must be special-cased differently. |
| 5889 | If we don't do this, creating an instance will |
| 5890 | always use slot_tp_new which will look up |
| 5891 | __new__ in the MRO which will call tp_new_wrapper |
| 5892 | which will look through the base classes looking |
| 5893 | for a static base and call its tp_new (usually |
| 5894 | PyType_GenericNew), after performing various |
| 5895 | sanity checks and constructing a new argument |
| 5896 | list. Cut all that nonsense short -- this speeds |
| 5897 | up instance creation tremendously. */ |
| 5898 | specific = (void *)type->tp_new; |
| 5899 | /* XXX I'm not 100% sure that there isn't a hole |
| 5900 | in this reasoning that requires additional |
| 5901 | sanity checks. I'll buy the first person to |
| 5902 | point out a bug in this reasoning a beer. */ |
| 5903 | } |
| 5904 | else if (descr == Py_None && |
| 5905 | ptr == (void**)&type->tp_hash) { |
| 5906 | /* We specifically allow __hash__ to be set to None |
| 5907 | to prevent inheritance of the default |
| 5908 | implementation from object.__hash__ */ |
| 5909 | specific = PyObject_HashNotImplemented; |
| 5910 | } |
| 5911 | else { |
| 5912 | use_generic = 1; |
| 5913 | generic = p->function; |
| 5914 | } |
| 5915 | } while ((++p)->offset == offset); |
| 5916 | if (specific && !use_generic) |
| 5917 | *ptr = specific; |
| 5918 | else |
| 5919 | *ptr = generic; |
| 5920 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5923 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5924 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5925 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5926 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5927 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5928 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | for (; *pp; pp++) |
| 5931 | update_one_slot(type, *pp); |
| 5932 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5933 | } |
| 5934 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5935 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 5936 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5937 | static int |
| 5938 | slotdef_cmp(const void *aa, const void *bb) |
| 5939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5940 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 5941 | int c = a->offset - b->offset; |
| 5942 | if (c != 0) |
| 5943 | return c; |
| 5944 | else |
| 5945 | /* Cannot use a-b, as this gives off_t, |
| 5946 | which may lose precision when converted to int. */ |
| 5947 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5950 | /* Initialize the slotdefs table by adding interned string objects for the |
| 5951 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5952 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5953 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5955 | slotdef *p; |
| 5956 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5958 | if (initialized) |
| 5959 | return; |
| 5960 | for (p = slotdefs; p->name; p++) { |
| 5961 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 5962 | if (!p->name_strobj) |
| 5963 | Py_FatalError("Out of memory interning slotdef names"); |
| 5964 | } |
| 5965 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 5966 | slotdef_cmp); |
| 5967 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5968 | } |
| 5969 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5970 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5971 | static int |
| 5972 | update_slot(PyTypeObject *type, PyObject *name) |
| 5973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5974 | slotdef *ptrs[MAX_EQUIV]; |
| 5975 | slotdef *p; |
| 5976 | slotdef **pp; |
| 5977 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 5980 | subclasses. This could possibly be unified with the |
| 5981 | update_subclasses() recursion below, but carefully: |
| 5982 | they each have their own conditions on which to stop |
| 5983 | recursing into subclasses. */ |
| 5984 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 5985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | init_slotdefs(); |
| 5987 | pp = ptrs; |
| 5988 | for (p = slotdefs; p->name; p++) { |
| 5989 | /* XXX assume name is interned! */ |
| 5990 | if (p->name_strobj == name) |
| 5991 | *pp++ = p; |
| 5992 | } |
| 5993 | *pp = NULL; |
| 5994 | for (pp = ptrs; *pp; pp++) { |
| 5995 | p = *pp; |
| 5996 | offset = p->offset; |
| 5997 | while (p > slotdefs && (p-1)->offset == offset) |
| 5998 | --p; |
| 5999 | *pp = p; |
| 6000 | } |
| 6001 | if (ptrs[0] == NULL) |
| 6002 | return 0; /* Not an attribute that affects any slots */ |
| 6003 | return update_subclasses(type, name, |
| 6004 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6007 | /* Store the proper functions in the slot dispatches at class (type) |
| 6008 | definition time, based upon which operations the class overrides in its |
| 6009 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6010 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6011 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6012 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6013 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | init_slotdefs(); |
| 6016 | for (p = slotdefs; p->name; ) |
| 6017 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6018 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6019 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6020 | static void |
| 6021 | update_all_slots(PyTypeObject* type) |
| 6022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6023 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6025 | init_slotdefs(); |
| 6026 | for (p = slotdefs; p->name; p++) { |
| 6027 | /* update_slot returns int but can't actually fail */ |
| 6028 | update_slot(type, p->name_strobj); |
| 6029 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6032 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6033 | recursive functions to call a callback for all subclasses, |
| 6034 | but refraining from recursing into subclasses that define 'name'. */ |
| 6035 | |
| 6036 | static int |
| 6037 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6038 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6040 | if (callback(type, data) < 0) |
| 6041 | return -1; |
| 6042 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | static int |
| 6046 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6047 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6048 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6049 | PyTypeObject *subclass; |
| 6050 | PyObject *ref, *subclasses, *dict; |
| 6051 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6053 | subclasses = type->tp_subclasses; |
| 6054 | if (subclasses == NULL) |
| 6055 | return 0; |
| 6056 | assert(PyList_Check(subclasses)); |
| 6057 | n = PyList_GET_SIZE(subclasses); |
| 6058 | for (i = 0; i < n; i++) { |
| 6059 | ref = PyList_GET_ITEM(subclasses, i); |
| 6060 | assert(PyWeakref_CheckRef(ref)); |
| 6061 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6062 | assert(subclass != NULL); |
| 6063 | if ((PyObject *)subclass == Py_None) |
| 6064 | continue; |
| 6065 | assert(PyType_Check(subclass)); |
| 6066 | /* Avoid recursing down into unaffected classes */ |
| 6067 | dict = subclass->tp_dict; |
| 6068 | if (dict != NULL && PyDict_Check(dict) && |
| 6069 | PyDict_GetItem(dict, name) != NULL) |
| 6070 | continue; |
| 6071 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6072 | return -1; |
| 6073 | } |
| 6074 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6077 | /* This function is called by PyType_Ready() to populate the type's |
| 6078 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6079 | function slot (like tp_repr) that's defined in the type, one or more |
| 6080 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6081 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6082 | cause more than one descriptor to be added (for example, the nb_add |
| 6083 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6084 | slots compete for the same descriptor (for example both sq_item and |
| 6085 | mp_subscript generate a __getitem__ descriptor). |
| 6086 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6087 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6088 | slotdef entries are sorted by the offset of the slot in the |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6089 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6090 | between competing slots: the members of PyHeapTypeObject are listed |
| 6091 | from most general to least general, so the most general slot is |
| 6092 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6093 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6094 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6095 | |
| 6096 | This only adds new descriptors and doesn't overwrite entries in |
| 6097 | tp_dict that were previously defined. The descriptors contain a |
| 6098 | reference to the C function they must call, so that it's safe if they |
| 6099 | are copied into a subtype's __dict__ and the subtype has a different |
| 6100 | C function in its slot -- calling the method defined by the |
| 6101 | descriptor will call the C function that was used to create it, |
| 6102 | rather than the C function present in the slot when it is called. |
| 6103 | (This is important because a subtype may have a C function in the |
| 6104 | slot that calls the method from the dictionary, and we want to avoid |
| 6105 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6106 | |
| 6107 | static int |
| 6108 | add_operators(PyTypeObject *type) |
| 6109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6110 | PyObject *dict = type->tp_dict; |
| 6111 | slotdef *p; |
| 6112 | PyObject *descr; |
| 6113 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6115 | init_slotdefs(); |
| 6116 | for (p = slotdefs; p->name; p++) { |
| 6117 | if (p->wrapper == NULL) |
| 6118 | continue; |
| 6119 | ptr = slotptr(type, p->offset); |
| 6120 | if (!ptr || !*ptr) |
| 6121 | continue; |
| 6122 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6123 | continue; |
| 6124 | if (*ptr == PyObject_HashNotImplemented) { |
| 6125 | /* Classes may prevent the inheritance of the tp_hash |
| 6126 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6127 | visible as a None value for the __hash__ attribute. */ |
| 6128 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6129 | return -1; |
| 6130 | } |
| 6131 | else { |
| 6132 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6133 | if (descr == NULL) |
| 6134 | return -1; |
| 6135 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6136 | return -1; |
| 6137 | Py_DECREF(descr); |
| 6138 | } |
| 6139 | } |
| 6140 | if (type->tp_new != NULL) { |
| 6141 | if (add_tp_new_wrapper(type) < 0) |
| 6142 | return -1; |
| 6143 | } |
| 6144 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6147 | |
| 6148 | /* Cooperative 'super' */ |
| 6149 | |
| 6150 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6151 | PyObject_HEAD |
| 6152 | PyTypeObject *type; |
| 6153 | PyObject *obj; |
| 6154 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6155 | } superobject; |
| 6156 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6157 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6158 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6159 | "the class invoking super()"}, |
| 6160 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6161 | "the instance invoking super(); may be None"}, |
| 6162 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6163 | "the type of the instance invoking super(); may be None"}, |
| 6164 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6165 | }; |
| 6166 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6167 | static void |
| 6168 | super_dealloc(PyObject *self) |
| 6169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6170 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6172 | _PyObject_GC_UNTRACK(self); |
| 6173 | Py_XDECREF(su->obj); |
| 6174 | Py_XDECREF(su->type); |
| 6175 | Py_XDECREF(su->obj_type); |
| 6176 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6180 | super_repr(PyObject *self) |
| 6181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6182 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6184 | if (su->obj_type) |
| 6185 | return PyUnicode_FromFormat( |
| 6186 | "<super: <class '%s'>, <%s object>>", |
| 6187 | su->type ? su->type->tp_name : "NULL", |
| 6188 | su->obj_type->tp_name); |
| 6189 | else |
| 6190 | return PyUnicode_FromFormat( |
| 6191 | "<super: <class '%s'>, NULL>", |
| 6192 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6196 | super_getattro(PyObject *self, PyObject *name) |
| 6197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6198 | superobject *su = (superobject *)self; |
| 6199 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6201 | if (!skip) { |
| 6202 | /* We want __class__ to return the class of the super object |
| 6203 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6204 | skip = (PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6205 | PyUnicode_GET_LENGTH(name) == 9 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6206 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6207 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6209 | if (!skip) { |
| 6210 | PyObject *mro, *res, *tmp, *dict; |
| 6211 | PyTypeObject *starttype; |
| 6212 | descrgetfunc f; |
| 6213 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6215 | starttype = su->obj_type; |
| 6216 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6218 | if (mro == NULL) |
| 6219 | n = 0; |
| 6220 | else { |
| 6221 | assert(PyTuple_Check(mro)); |
| 6222 | n = PyTuple_GET_SIZE(mro); |
| 6223 | } |
| 6224 | for (i = 0; i < n; i++) { |
| 6225 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6226 | break; |
| 6227 | } |
| 6228 | i++; |
| 6229 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6230 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 6231 | replaced during PyDict_GetItem(dict, name) */ |
| 6232 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | for (; i < n; i++) { |
| 6234 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6235 | if (PyType_Check(tmp)) |
| 6236 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6237 | else |
| 6238 | continue; |
| 6239 | res = PyDict_GetItem(dict, name); |
| 6240 | if (res != NULL) { |
| 6241 | Py_INCREF(res); |
| 6242 | f = Py_TYPE(res)->tp_descr_get; |
| 6243 | if (f != NULL) { |
| 6244 | tmp = f(res, |
| 6245 | /* Only pass 'obj' param if |
| 6246 | this is instance-mode super |
| 6247 | (See SF ID #743627) |
| 6248 | */ |
| 6249 | (su->obj == (PyObject *) |
| 6250 | su->obj_type |
| 6251 | ? (PyObject *)NULL |
| 6252 | : su->obj), |
| 6253 | (PyObject *)starttype); |
| 6254 | Py_DECREF(res); |
| 6255 | res = tmp; |
| 6256 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6257 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | return res; |
| 6259 | } |
| 6260 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6261 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6262 | } |
| 6263 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6266 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6267 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6269 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6270 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 6271 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6273 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6274 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6276 | - If it is an instance, it must be an instance of 'type'. This is |
| 6277 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6279 | But... when obj is an instance, we want to allow for the case where |
| 6280 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6281 | This will allow using super() with a proxy for obj. |
| 6282 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6284 | /* Check for first bullet above (special case) */ |
| 6285 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6286 | Py_INCREF(obj); |
| 6287 | return (PyTypeObject *)obj; |
| 6288 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | /* Normal case */ |
| 6291 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6292 | Py_INCREF(Py_TYPE(obj)); |
| 6293 | return Py_TYPE(obj); |
| 6294 | } |
| 6295 | else { |
| 6296 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6297 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6298 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6299 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6300 | if (class_attr != NULL && |
| 6301 | PyType_Check(class_attr) && |
| 6302 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6303 | { |
| 6304 | int ok = PyType_IsSubtype( |
| 6305 | (PyTypeObject *)class_attr, type); |
| 6306 | if (ok) |
| 6307 | return (PyTypeObject *)class_attr; |
| 6308 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6310 | if (class_attr == NULL) |
| 6311 | PyErr_Clear(); |
| 6312 | else |
| 6313 | Py_DECREF(class_attr); |
| 6314 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6316 | PyErr_SetString(PyExc_TypeError, |
| 6317 | "super(type, obj): " |
| 6318 | "obj must be an instance or subtype of type"); |
| 6319 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6320 | } |
| 6321 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6322 | static PyObject * |
| 6323 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6325 | superobject *su = (superobject *)self; |
| 6326 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6328 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6329 | /* Not binding to an object, or already bound */ |
| 6330 | Py_INCREF(self); |
| 6331 | return self; |
| 6332 | } |
| 6333 | if (Py_TYPE(su) != &PySuper_Type) |
| 6334 | /* If su is an instance of a (strict) subclass of super, |
| 6335 | call its type */ |
| 6336 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6337 | su->type, obj, NULL); |
| 6338 | else { |
| 6339 | /* Inline the common case */ |
| 6340 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6341 | if (obj_type == NULL) |
| 6342 | return NULL; |
| 6343 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6344 | NULL, NULL); |
| 6345 | if (newobj == NULL) |
| 6346 | return NULL; |
| 6347 | Py_INCREF(su->type); |
| 6348 | Py_INCREF(obj); |
| 6349 | newobj->type = su->type; |
| 6350 | newobj->obj = obj; |
| 6351 | newobj->obj_type = obj_type; |
| 6352 | return (PyObject *)newobj; |
| 6353 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | static int |
| 6357 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | superobject *su = (superobject *)self; |
| 6360 | PyTypeObject *type = NULL; |
| 6361 | PyObject *obj = NULL; |
| 6362 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6364 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6365 | return -1; |
| 6366 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6367 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | if (type == NULL) { |
| 6370 | /* Call super(), without args -- fill in from __class__ |
| 6371 | and first local variable on the stack. */ |
| 6372 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6373 | PyCodeObject *co = f->f_code; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6374 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6375 | if (co == NULL) { |
| 6376 | PyErr_SetString(PyExc_SystemError, |
| 6377 | "super(): no code object"); |
| 6378 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6379 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | if (co->co_argcount == 0) { |
| 6381 | PyErr_SetString(PyExc_SystemError, |
| 6382 | "super(): no arguments"); |
| 6383 | return -1; |
| 6384 | } |
| 6385 | obj = f->f_localsplus[0]; |
| 6386 | if (obj == NULL) { |
| 6387 | PyErr_SetString(PyExc_SystemError, |
| 6388 | "super(): arg[0] deleted"); |
| 6389 | return -1; |
| 6390 | } |
| 6391 | if (co->co_freevars == NULL) |
| 6392 | n = 0; |
| 6393 | else { |
| 6394 | assert(PyTuple_Check(co->co_freevars)); |
| 6395 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6396 | } |
| 6397 | for (i = 0; i < n; i++) { |
| 6398 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6399 | assert(PyUnicode_Check(name)); |
| 6400 | if (!PyUnicode_CompareWithASCIIString(name, |
Benjamin Peterson | f5ff223 | 2011-06-19 19:42:22 -0500 | [diff] [blame] | 6401 | "@__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | Py_ssize_t index = co->co_nlocals + |
| 6403 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6404 | PyObject *cell = f->f_localsplus[index]; |
| 6405 | if (cell == NULL || !PyCell_Check(cell)) { |
| 6406 | PyErr_SetString(PyExc_SystemError, |
| 6407 | "super(): bad __class__ cell"); |
| 6408 | return -1; |
| 6409 | } |
| 6410 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6411 | if (type == NULL) { |
| 6412 | PyErr_SetString(PyExc_SystemError, |
| 6413 | "super(): empty __class__ cell"); |
| 6414 | return -1; |
| 6415 | } |
| 6416 | if (!PyType_Check(type)) { |
| 6417 | PyErr_Format(PyExc_SystemError, |
| 6418 | "super(): __class__ is not a type (%s)", |
| 6419 | Py_TYPE(type)->tp_name); |
| 6420 | return -1; |
| 6421 | } |
| 6422 | break; |
| 6423 | } |
| 6424 | } |
| 6425 | if (type == NULL) { |
| 6426 | PyErr_SetString(PyExc_SystemError, |
| 6427 | "super(): __class__ cell not found"); |
| 6428 | return -1; |
| 6429 | } |
| 6430 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6432 | if (obj == Py_None) |
| 6433 | obj = NULL; |
| 6434 | if (obj != NULL) { |
| 6435 | obj_type = supercheck(type, obj); |
| 6436 | if (obj_type == NULL) |
| 6437 | return -1; |
| 6438 | Py_INCREF(obj); |
| 6439 | } |
| 6440 | Py_INCREF(type); |
| 6441 | su->type = type; |
| 6442 | su->obj = obj; |
| 6443 | su->obj_type = obj_type; |
| 6444 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6447 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6448 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6449 | "super(type) -> unbound super object\n" |
| 6450 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6451 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6452 | "Typical use to call a cooperative superclass method:\n" |
| 6453 | "class C(B):\n" |
| 6454 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6455 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6456 | "This works for class methods too:\n" |
| 6457 | "class C(B):\n" |
| 6458 | " @classmethod\n" |
| 6459 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6460 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6461 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6462 | static int |
| 6463 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6465 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6467 | Py_VISIT(su->obj); |
| 6468 | Py_VISIT(su->type); |
| 6469 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6471 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6474 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6475 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6476 | "super", /* tp_name */ |
| 6477 | sizeof(superobject), /* tp_basicsize */ |
| 6478 | 0, /* tp_itemsize */ |
| 6479 | /* methods */ |
| 6480 | super_dealloc, /* tp_dealloc */ |
| 6481 | 0, /* tp_print */ |
| 6482 | 0, /* tp_getattr */ |
| 6483 | 0, /* tp_setattr */ |
| 6484 | 0, /* tp_reserved */ |
| 6485 | super_repr, /* tp_repr */ |
| 6486 | 0, /* tp_as_number */ |
| 6487 | 0, /* tp_as_sequence */ |
| 6488 | 0, /* tp_as_mapping */ |
| 6489 | 0, /* tp_hash */ |
| 6490 | 0, /* tp_call */ |
| 6491 | 0, /* tp_str */ |
| 6492 | super_getattro, /* tp_getattro */ |
| 6493 | 0, /* tp_setattro */ |
| 6494 | 0, /* tp_as_buffer */ |
| 6495 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6496 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6497 | super_doc, /* tp_doc */ |
| 6498 | super_traverse, /* tp_traverse */ |
| 6499 | 0, /* tp_clear */ |
| 6500 | 0, /* tp_richcompare */ |
| 6501 | 0, /* tp_weaklistoffset */ |
| 6502 | 0, /* tp_iter */ |
| 6503 | 0, /* tp_iternext */ |
| 6504 | 0, /* tp_methods */ |
| 6505 | super_members, /* tp_members */ |
| 6506 | 0, /* tp_getset */ |
| 6507 | 0, /* tp_base */ |
| 6508 | 0, /* tp_dict */ |
| 6509 | super_descr_get, /* tp_descr_get */ |
| 6510 | 0, /* tp_descr_set */ |
| 6511 | 0, /* tp_dictoffset */ |
| 6512 | super_init, /* tp_init */ |
| 6513 | PyType_GenericAlloc, /* tp_alloc */ |
| 6514 | PyType_GenericNew, /* tp_new */ |
| 6515 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6516 | }; |