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 | |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 10 | /* Cached lookup of the copyreg module, for faster __reduce__ calls */ |
| 11 | |
| 12 | static PyObject *cached_copyreg_module = NULL; |
| 13 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 14 | /* Support type attribute cache */ |
| 15 | |
| 16 | /* The cache can keep references to the names alive for longer than |
| 17 | they normally would. This is why the maximum size is limited to |
| 18 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 19 | strings are used as attribute names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | #define MCACHE_MAX_ATTR_SIZE 100 |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 21 | #define MCACHE_SIZE_EXP 9 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | #define MCACHE_HASH(version, name_hash) \ |
| 23 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 24 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 25 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 27 | ((PyASCIIObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 28 | #define MCACHE_CACHEABLE_NAME(name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | PyUnicode_CheckExact(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 30 | PyUnicode_READY(name) != -1 && \ |
| 31 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 32 | |
| 33 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | unsigned int version; |
| 35 | PyObject *name; /* reference to exactly a str or None */ |
| 36 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 40 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 42 | _Py_IDENTIFIER(__class__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 43 | _Py_IDENTIFIER(__dict__); |
| 44 | _Py_IDENTIFIER(__doc__); |
| 45 | _Py_IDENTIFIER(__getitem__); |
| 46 | _Py_IDENTIFIER(__getattribute__); |
| 47 | _Py_IDENTIFIER(__hash__); |
| 48 | _Py_IDENTIFIER(__module__); |
| 49 | _Py_IDENTIFIER(__name__); |
| 50 | _Py_IDENTIFIER(__new__); |
| 51 | |
| 52 | static PyObject * |
| 53 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 54 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 55 | static PyObject * |
| 56 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 57 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 58 | unsigned int |
| 59 | PyType_ClearCache(void) |
| 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | Py_ssize_t i; |
| 62 | unsigned int cur_version_tag = next_version_tag - 1; |
| 63 | |
| 64 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 65 | method_cache[i].version = 0; |
| 66 | Py_CLEAR(method_cache[i].name); |
| 67 | method_cache[i].value = NULL; |
| 68 | } |
| 69 | next_version_tag = 0; |
| 70 | /* mark all version tags as invalid */ |
| 71 | PyType_Modified(&PyBaseObject_Type); |
| 72 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 73 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 74 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 75 | void |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 76 | _PyType_Fini(void) |
| 77 | { |
| 78 | PyType_ClearCache(); |
| 79 | /* Need to forget our obsolete instance of the copyreg module at |
| 80 | * interpreter shutdown (issue #17408). */ |
| 81 | Py_CLEAR(cached_copyreg_module); |
| 82 | } |
| 83 | |
| 84 | void |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 85 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | /* Invalidate any cached data for the specified type and all |
| 88 | subclasses. This function is called after the base |
| 89 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | Invariants: |
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 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 94 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 95 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 98 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 101 | type (so it must first clear it on all subclasses). The |
| 102 | tp_version_tag value is meaningless unless this flag is set. |
| 103 | We don't assign new version tags eagerly, but only as |
| 104 | needed. |
| 105 | */ |
| 106 | PyObject *raw, *ref; |
| 107 | Py_ssize_t i, n; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 110 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | raw = type->tp_subclasses; |
| 113 | if (raw != NULL) { |
| 114 | n = PyList_GET_SIZE(raw); |
| 115 | for (i = 0; i < n; i++) { |
| 116 | ref = PyList_GET_ITEM(raw, i); |
| 117 | ref = PyWeakref_GET_OBJECT(ref); |
| 118 | if (ref != Py_None) { |
| 119 | PyType_Modified((PyTypeObject *)ref); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void |
| 127 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | /* |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 129 | 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] | 130 | able to be cached. This function is called after the base |
| 131 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 134 | has a custom MRO that includes a type which is not officially |
| 135 | super type. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | Called from mro_internal, which will subsequently be called on |
| 138 | each subclass when their mro is recursively updated. |
| 139 | */ |
| 140 | Py_ssize_t i, n; |
| 141 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 144 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | n = PyTuple_GET_SIZE(bases); |
| 147 | for (i = 0; i < n; i++) { |
| 148 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 149 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 150 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 151 | assert(PyType_Check(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 155 | !PyType_IsSubtype(type, cls)) { |
| 156 | clear = 1; |
| 157 | break; |
| 158 | } |
| 159 | } |
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 (clear) |
| 162 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 163 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int |
| 167 | assign_version_tag(PyTypeObject *type) |
| 168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | /* Ensure that the tp_version_tag is valid and set |
| 170 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 171 | must first be done on all super classes. Return 0 if this |
| 172 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 173 | */ |
| 174 | Py_ssize_t i, n; |
| 175 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 178 | return 1; |
| 179 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 180 | return 0; |
| 181 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 182 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | type->tp_version_tag = next_version_tag++; |
| 185 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | if (type->tp_version_tag == 0) { |
| 188 | /* wrap-around or just starting Python - clear the whole |
| 189 | cache by filling names with references to Py_None. |
| 190 | Values are also set to NULL for added protection, as they |
| 191 | are borrowed reference */ |
| 192 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 193 | method_cache[i].value = NULL; |
| 194 | Py_XDECREF(method_cache[i].name); |
| 195 | method_cache[i].name = Py_None; |
| 196 | Py_INCREF(Py_None); |
| 197 | } |
| 198 | /* mark all version tags as invalid */ |
| 199 | PyType_Modified(&PyBaseObject_Type); |
| 200 | return 1; |
| 201 | } |
| 202 | bases = type->tp_bases; |
| 203 | n = PyTuple_GET_SIZE(bases); |
| 204 | for (i = 0; i < n; i++) { |
| 205 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 206 | assert(PyType_Check(b)); |
| 207 | if (!assign_version_tag((PyTypeObject *)b)) |
| 208 | return 0; |
| 209 | } |
| 210 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 211 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 215 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 216 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 217 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 219 | {"__weakrefoffset__", T_LONG, |
| 220 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 221 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 222 | {"__dictoffset__", T_LONG, |
| 223 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 224 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 225 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 226 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 227 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 228 | static int |
| 229 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 230 | { |
| 231 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 232 | PyErr_Format(PyExc_TypeError, |
| 233 | "can't set %s.%s", type->tp_name, name); |
| 234 | return 0; |
| 235 | } |
| 236 | if (!value) { |
| 237 | PyErr_Format(PyExc_TypeError, |
| 238 | "can't delete %s.%s", type->tp_name, name); |
| 239 | return 0; |
| 240 | } |
| 241 | return 1; |
| 242 | } |
| 243 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 244 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 245 | type_name(PyTypeObject *type, void *context) |
| 246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 250 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | Py_INCREF(et->ht_name); |
| 253 | return et->ht_name; |
| 254 | } |
| 255 | else { |
| 256 | s = strrchr(type->tp_name, '.'); |
| 257 | if (s == NULL) |
| 258 | s = type->tp_name; |
| 259 | else |
| 260 | s++; |
| 261 | return PyUnicode_FromString(s); |
| 262 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 265 | static PyObject * |
| 266 | type_qualname(PyTypeObject *type, void *context) |
| 267 | { |
| 268 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 269 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 270 | Py_INCREF(et->ht_qualname); |
| 271 | return et->ht_qualname; |
| 272 | } |
| 273 | else { |
| 274 | return type_name(type, context); |
| 275 | } |
| 276 | } |
| 277 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 278 | static int |
| 279 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | PyHeapTypeObject* et; |
| 282 | char *tp_name; |
| 283 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 284 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 285 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | if (!PyUnicode_Check(value)) { |
| 288 | PyErr_Format(PyExc_TypeError, |
| 289 | "can only assign string to %s.__name__, not '%s'", |
| 290 | type->tp_name, Py_TYPE(value)->tp_name); |
| 291 | return -1; |
| 292 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | /* Check absence of null characters */ |
| 295 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 296 | if (tmp == NULL) |
| 297 | return -1; |
| 298 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 299 | Py_DECREF(tmp); |
| 300 | PyErr_Format(PyExc_ValueError, |
| 301 | "__name__ must not contain null bytes"); |
| 302 | return -1; |
| 303 | } |
| 304 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | tp_name = _PyUnicode_AsString(value); |
| 307 | if (tp_name == NULL) |
| 308 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 313 | |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame] | 314 | /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name |
| 315 | value. (Bug #16447.) */ |
| 316 | tmp = et->ht_name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | type->tp_name = tp_name; |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame] | 320 | Py_DECREF(tmp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 325 | static int |
| 326 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 327 | { |
| 328 | PyHeapTypeObject* et; |
| 329 | |
Benjamin Peterson | 2c05a2e | 2012-10-31 00:01:15 -0400 | [diff] [blame] | 330 | if (!check_set_special_type_attr(type, value, "__qualname__")) |
| 331 | return -1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 332 | if (!PyUnicode_Check(value)) { |
| 333 | PyErr_Format(PyExc_TypeError, |
| 334 | "can only assign string to %s.__qualname__, not '%s'", |
| 335 | type->tp_name, Py_TYPE(value)->tp_name); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | et = (PyHeapTypeObject*)type; |
| 340 | Py_INCREF(value); |
| 341 | Py_DECREF(et->ht_qualname); |
| 342 | et->ht_qualname = value; |
| 343 | return 0; |
| 344 | } |
| 345 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 346 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 347 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | PyObject *mod; |
| 350 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 353 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | if (!mod) { |
| 355 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 356 | return 0; |
| 357 | } |
| 358 | Py_XINCREF(mod); |
| 359 | return mod; |
| 360 | } |
| 361 | else { |
| 362 | s = strrchr(type->tp_name, '.'); |
| 363 | if (s != NULL) |
| 364 | return PyUnicode_FromStringAndSize( |
| 365 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 366 | return PyUnicode_FromString("builtins"); |
| 367 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 370 | static int |
| 371 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 372 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 373 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 377 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 378 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 381 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 382 | type_abstractmethods(PyTypeObject *type, void *context) |
| 383 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 384 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 385 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 386 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 387 | if (type != &PyType_Type) |
| 388 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (!mod) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | Py_XINCREF(mod); |
| 394 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int |
| 398 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | /* __abstractmethods__ should only be set once on a type, in |
| 401 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 402 | special to update subclasses. |
| 403 | */ |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 404 | int abstract, res; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 405 | if (value != NULL) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 406 | abstract = PyObject_IsTrue(value); |
| 407 | if (abstract < 0) |
| 408 | return -1; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 409 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value); |
| 410 | } |
| 411 | else { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 412 | abstract = 0; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 413 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__"); |
| 414 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 415 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 416 | return -1; |
| 417 | } |
| 418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (res == 0) { |
| 420 | PyType_Modified(type); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 421 | if (abstract) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 423 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | } |
| 426 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 430 | type_get_bases(PyTypeObject *type, void *context) |
| 431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | Py_INCREF(type->tp_bases); |
| 433 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static PyTypeObject *best_base(PyObject *); |
| 437 | static int mro_internal(PyTypeObject *); |
| 438 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 439 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 440 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 441 | static void update_all_slots(PyTypeObject *); |
| 442 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 443 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 444 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 446 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 448 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 449 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 450 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | PyTypeObject *subclass; |
| 453 | PyObject *ref, *subclasses, *old_mro; |
| 454 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | subclasses = type->tp_subclasses; |
| 457 | if (subclasses == NULL) |
| 458 | return 0; |
| 459 | assert(PyList_Check(subclasses)); |
| 460 | n = PyList_GET_SIZE(subclasses); |
| 461 | for (i = 0; i < n; i++) { |
| 462 | ref = PyList_GET_ITEM(subclasses, i); |
| 463 | assert(PyWeakref_CheckRef(ref)); |
| 464 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 465 | assert(subclass != NULL); |
| 466 | if ((PyObject *)subclass == Py_None) |
| 467 | continue; |
| 468 | assert(PyType_Check(subclass)); |
| 469 | old_mro = subclass->tp_mro; |
| 470 | if (mro_internal(subclass) < 0) { |
| 471 | subclass->tp_mro = old_mro; |
| 472 | return -1; |
| 473 | } |
| 474 | else { |
| 475 | PyObject* tuple; |
| 476 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 477 | Py_DECREF(old_mro); |
| 478 | if (!tuple) |
| 479 | return -1; |
| 480 | if (PyList_Append(temp, tuple) < 0) |
| 481 | return -1; |
| 482 | Py_DECREF(tuple); |
| 483 | } |
| 484 | if (mro_subclasses(subclass, temp) < 0) |
| 485 | return -1; |
| 486 | } |
| 487 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static int |
| 491 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | Py_ssize_t i; |
| 494 | int r = 0; |
| 495 | PyObject *ob, *temp; |
| 496 | PyTypeObject *new_base, *old_base; |
| 497 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 498 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 499 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | if (!PyTuple_Check(value)) { |
| 502 | PyErr_Format(PyExc_TypeError, |
| 503 | "can only assign tuple to %s.__bases__, not %s", |
| 504 | type->tp_name, Py_TYPE(value)->tp_name); |
| 505 | return -1; |
| 506 | } |
| 507 | if (PyTuple_GET_SIZE(value) == 0) { |
| 508 | PyErr_Format(PyExc_TypeError, |
| 509 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 510 | type->tp_name); |
| 511 | return -1; |
| 512 | } |
| 513 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 514 | ob = PyTuple_GET_ITEM(value, i); |
| 515 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 516 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 517 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 518 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 519 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | } |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 521 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 522 | PyErr_SetString(PyExc_TypeError, |
| 523 | "a __bases__ item causes an inheritance cycle"); |
| 524 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 529 | |
Benjamin Peterson | ab3c1c1 | 2012-04-01 18:48:02 -0400 | [diff] [blame] | 530 | if (!new_base) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 534 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | Py_INCREF(new_base); |
| 537 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | old_bases = type->tp_bases; |
| 540 | old_base = type->tp_base; |
| 541 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | type->tp_bases = value; |
| 544 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | if (mro_internal(type) < 0) { |
| 547 | goto bail; |
| 548 | } |
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 | temp = PyList_New(0); |
| 551 | if (!temp) |
| 552 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | if (r < 0) { |
| 557 | for (i = 0; i < PyList_Size(temp); i++) { |
| 558 | PyTypeObject* cls; |
| 559 | PyObject* mro; |
| 560 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 561 | "", 2, 2, &cls, &mro); |
| 562 | Py_INCREF(mro); |
| 563 | ob = cls->tp_mro; |
| 564 | cls->tp_mro = mro; |
| 565 | Py_DECREF(ob); |
| 566 | } |
| 567 | Py_DECREF(temp); |
| 568 | goto bail; |
| 569 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | /* any base that was in __bases__ but now isn't, we |
| 574 | need to remove |type| from its tp_subclasses. |
| 575 | conversely, any class now in __bases__ that wasn't |
| 576 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | /* for now, sod that: just remove from all old_bases, |
| 579 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 582 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 583 | if (PyType_Check(ob)) { |
| 584 | remove_subclass( |
| 585 | (PyTypeObject*)ob, type); |
| 586 | } |
| 587 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 590 | ob = PyTuple_GET_ITEM(value, i); |
| 591 | if (PyType_Check(ob)) { |
| 592 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 593 | r = -1; |
| 594 | } |
| 595 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | Py_DECREF(old_bases); |
| 600 | Py_DECREF(old_base); |
| 601 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 604 | |
| 605 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | Py_DECREF(type->tp_bases); |
| 607 | Py_DECREF(type->tp_base); |
| 608 | if (type->tp_mro != old_mro) { |
| 609 | Py_DECREF(type->tp_mro); |
| 610 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | type->tp_bases = old_bases; |
| 613 | type->tp_base = old_base; |
| 614 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 620 | type_dict(PyTypeObject *type, void *context) |
| 621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | if (type->tp_dict == NULL) { |
| 623 | Py_INCREF(Py_None); |
| 624 | return Py_None; |
| 625 | } |
| 626 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 629 | static PyObject * |
| 630 | type_get_doc(PyTypeObject *type, void *context) |
| 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | PyObject *result; |
| 633 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 634 | return PyUnicode_FromString(type->tp_doc); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 635 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (result == NULL) { |
| 637 | result = Py_None; |
| 638 | Py_INCREF(result); |
| 639 | } |
| 640 | else if (Py_TYPE(result)->tp_descr_get) { |
| 641 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 642 | (PyObject *)type); |
| 643 | } |
| 644 | else { |
| 645 | Py_INCREF(result); |
| 646 | } |
| 647 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 650 | static int |
| 651 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 652 | { |
| 653 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 654 | return -1; |
| 655 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 656 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 657 | } |
| 658 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 659 | static PyObject * |
| 660 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 663 | case -1: |
| 664 | return NULL; |
| 665 | case 0: |
| 666 | Py_RETURN_FALSE; |
| 667 | default: |
| 668 | Py_RETURN_TRUE; |
| 669 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 674 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 677 | case -1: |
| 678 | return NULL; |
| 679 | case 0: |
| 680 | Py_RETURN_FALSE; |
| 681 | default: |
| 682 | Py_RETURN_TRUE; |
| 683 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 686 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 687 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 689 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 691 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 692 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 693 | (setter)type_set_abstractmethods, NULL}, |
| 694 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 695 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 699 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 700 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | mod = type_module(type, NULL); |
| 705 | if (mod == NULL) |
| 706 | PyErr_Clear(); |
| 707 | else if (!PyUnicode_Check(mod)) { |
| 708 | Py_DECREF(mod); |
| 709 | mod = NULL; |
| 710 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 711 | name = type_qualname(type, NULL); |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 712 | if (name == NULL) { |
| 713 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | return NULL; |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 715 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 718 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 719 | else |
| 720 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | Py_XDECREF(mod); |
| 723 | Py_DECREF(name); |
| 724 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 727 | static PyObject * |
| 728 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | if (type->tp_new == NULL) { |
| 733 | PyErr_Format(PyExc_TypeError, |
| 734 | "cannot create '%.100s' instances", |
| 735 | type->tp_name); |
| 736 | return NULL; |
| 737 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | obj = type->tp_new(type, args, kwds); |
| 740 | if (obj != NULL) { |
| 741 | /* Ugly exception: when the call was type(something), |
| 742 | don't call tp_init on the result. */ |
| 743 | if (type == &PyType_Type && |
| 744 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 745 | (kwds == NULL || |
| 746 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 747 | return obj; |
| 748 | /* If the returned object is not an instance of type, |
| 749 | it won't be initialized. */ |
| 750 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 751 | return obj; |
| 752 | type = Py_TYPE(obj); |
Victor Stinner | 3997cfd | 2013-07-16 22:51:21 +0200 | [diff] [blame^] | 753 | if (type->tp_init != NULL) { |
| 754 | int res = type->tp_init(obj, args, kwds); |
| 755 | if (res < 0) { |
| 756 | Py_DECREF(obj); |
| 757 | obj = NULL; |
| 758 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 765 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | PyObject *obj; |
| 768 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 769 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (PyType_IS_GC(type)) |
| 772 | obj = _PyObject_GC_Malloc(size); |
| 773 | else |
| 774 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | if (obj == NULL) |
| 777 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 782 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | if (type->tp_itemsize == 0) |
| 785 | PyObject_INIT(obj, type); |
| 786 | else |
| 787 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | if (PyType_IS_GC(type)) |
| 790 | _PyObject_GC_TRACK(obj); |
| 791 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | PyObject * |
| 795 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 800 | /* Helpers for subtyping */ |
| 801 | |
| 802 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 803 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | Py_ssize_t i, n; |
| 806 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | n = Py_SIZE(type); |
| 809 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 810 | for (i = 0; i < n; i++, mp++) { |
| 811 | if (mp->type == T_OBJECT_EX) { |
| 812 | char *addr = (char *)self + mp->offset; |
| 813 | PyObject *obj = *(PyObject **)addr; |
| 814 | if (obj != NULL) { |
| 815 | int err = visit(obj, arg); |
| 816 | if (err) |
| 817 | return err; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 825 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 826 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | PyTypeObject *type, *base; |
| 828 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | /* Find the nearest base with a different tp_traverse, |
| 831 | and traverse slots while we're at it */ |
| 832 | type = Py_TYPE(self); |
| 833 | base = type; |
| 834 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 835 | if (Py_SIZE(base)) { |
| 836 | int err = traverse_slots(base, self, visit, arg); |
| 837 | if (err) |
| 838 | return err; |
| 839 | } |
| 840 | base = base->tp_base; |
| 841 | assert(base); |
| 842 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 845 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 846 | if (dictptr && *dictptr) |
| 847 | Py_VISIT(*dictptr); |
| 848 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 851 | /* For a heaptype, the instances count as references |
| 852 | to the type. Traverse the type so the collector |
| 853 | can find cycles involving this link. */ |
| 854 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | if (basetraverse) |
| 857 | return basetraverse(self, visit, arg); |
| 858 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static void |
| 862 | clear_slots(PyTypeObject *type, PyObject *self) |
| 863 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | Py_ssize_t i, n; |
| 865 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | n = Py_SIZE(type); |
| 868 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 869 | for (i = 0; i < n; i++, mp++) { |
| 870 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 871 | char *addr = (char *)self + mp->offset; |
| 872 | PyObject *obj = *(PyObject **)addr; |
| 873 | if (obj != NULL) { |
| 874 | *(PyObject **)addr = NULL; |
| 875 | Py_DECREF(obj); |
| 876 | } |
| 877 | } |
| 878 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static int |
| 882 | subtype_clear(PyObject *self) |
| 883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | PyTypeObject *type, *base; |
| 885 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | /* Find the nearest base with a different tp_clear |
| 888 | and clear slots while we're at it */ |
| 889 | type = Py_TYPE(self); |
| 890 | base = type; |
| 891 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 892 | if (Py_SIZE(base)) |
| 893 | clear_slots(base, self); |
| 894 | base = base->tp_base; |
| 895 | assert(base); |
| 896 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 897 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 898 | /* Clear the instance dict (if any), to break cycles involving only |
| 899 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 900 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 901 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 902 | if (dictptr && *dictptr) |
| 903 | Py_CLEAR(*dictptr); |
| 904 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | if (baseclear) |
| 907 | return baseclear(self); |
| 908 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 909 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 910 | |
| 911 | static void |
| 912 | subtype_dealloc(PyObject *self) |
| 913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | PyTypeObject *type, *base; |
| 915 | destructor basedealloc; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 916 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | /* Extract the type; we expect it to be a heap type */ |
| 919 | type = Py_TYPE(self); |
| 920 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | if (!PyType_IS_GC(type)) { |
| 925 | /* It's really rare to find a dynamic type that doesn't have |
| 926 | GC; it can only happen when deriving from 'object' and not |
| 927 | adding any slots or instance variables. This allows |
| 928 | certain simplifications: there's no need to call |
| 929 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | /* Maybe call finalizer; exit early if resurrected */ |
| 932 | if (type->tp_del) { |
| 933 | type->tp_del(self); |
| 934 | if (self->ob_refcnt > 0) |
| 935 | return; |
| 936 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | /* Find the nearest base with a different tp_dealloc */ |
| 939 | base = type; |
| 940 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 941 | assert(Py_SIZE(base) == 0); |
| 942 | base = base->tp_base; |
| 943 | assert(base); |
| 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 | /* Extract the type again; tp_del may have changed it */ |
| 947 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | /* Call the base tp_dealloc() */ |
| 950 | assert(basedealloc); |
| 951 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | /* Can't reference self beyond this point */ |
| 954 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | /* Done */ |
| 957 | return; |
| 958 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 963 | /* See explanation at end of function for full disclosure */ |
| 964 | PyObject_GC_UnTrack(self); |
| 965 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 966 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 968 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 969 | -- tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 971 | * (if any, and whether directly here or indirectly in something we |
| 972 | * call) may trigger GC, and if self is tracked at that point, it |
| 973 | * will look like trash to GC and GC will try to delete self again. |
| 974 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | /* Find the nearest base with a different tp_dealloc */ |
| 977 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 978 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | base = base->tp_base; |
| 980 | assert(base); |
| 981 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 984 | the finalizer (__del__), clearing slots, or clearing the instance |
| 985 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 988 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | /* Maybe call finalizer; exit early if resurrected */ |
| 991 | if (type->tp_del) { |
| 992 | _PyObject_GC_TRACK(self); |
| 993 | type->tp_del(self); |
| 994 | if (self->ob_refcnt > 0) |
| 995 | goto endlabel; /* resurrected */ |
| 996 | else |
| 997 | _PyObject_GC_UNTRACK(self); |
| 998 | /* New weakrefs could be created during the finalizer call. |
| 999 | If this occurs, clear them out without calling their |
| 1000 | finalizers since they might rely on part of the object |
| 1001 | being finalized that has already been destroyed. */ |
| 1002 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 1003 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 1004 | PyWeakReference **list = (PyWeakReference **) \ |
| 1005 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 1006 | while (*list) |
| 1007 | _PyWeakref_ClearRef(*list); |
| 1008 | } |
| 1009 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 1012 | base = type; |
| 1013 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 1014 | if (Py_SIZE(base)) |
| 1015 | clear_slots(base, self); |
| 1016 | base = base->tp_base; |
| 1017 | assert(base); |
| 1018 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | /* If we added a dict, DECREF it */ |
| 1021 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1022 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1023 | if (dictptr != NULL) { |
| 1024 | PyObject *dict = *dictptr; |
| 1025 | if (dict != NULL) { |
| 1026 | Py_DECREF(dict); |
| 1027 | *dictptr = NULL; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | /* Extract the type again; tp_del may have changed it */ |
| 1033 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | /* Call the base tp_dealloc(); first retrack self if |
| 1036 | * basedealloc knows about gc. |
| 1037 | */ |
| 1038 | if (PyType_IS_GC(base)) |
| 1039 | _PyObject_GC_TRACK(self); |
| 1040 | assert(basedealloc); |
| 1041 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | /* Can't reference self beyond this point */ |
| 1044 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1045 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1046 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1048 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | Py_TRASHCAN_SAFE_END(self); |
| 1050 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1051 | -- tstate->trash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1058 | For one, this explains why there must be a call to GC-untrack |
| 1059 | before the trashcan begin macro. Without understanding the |
| 1060 | trashcan code, the answers to the following questions don't make |
| 1061 | sense. |
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 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1064 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | A. In the case that the base class is GC-aware, the base class |
| 1067 | probably GC-untracks the object. If it does that using the |
| 1068 | UNTRACK macro, this will crash when the object is already |
| 1069 | untracked. Because we don't know what the base class does, the |
| 1070 | only safe thing is to make sure the object is tracked when we |
| 1071 | call the base class dealloc. But... The trashcan begin macro |
| 1072 | requires that the object is *untracked* before it is called. So |
| 1073 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | GC untrack |
| 1076 | trashcan begin |
| 1077 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | Q. Why did the last question say "immediately GC-track again"? |
| 1080 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1083 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1084 | (which can happen if any weakref callback gets invoked), it |
| 1085 | 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] | 1086 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | Q. Why the bizarre (net-zero) manipulation of |
| 1090 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1093 | The following scenario used to be possible: |
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 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | - the trashcan limit is not yet reached, so the trashcan level |
| 1100 | is incremented and the code between trashcan begin and end is |
| 1101 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | - this destroys much of the object's contents, including its |
| 1104 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | - basedealloc() is called; this is really list_dealloc(), or |
| 1107 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | - the trashcan limit is now reached, so the object is put on the |
| 1110 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | - later, the trashcan code starts deleting the objects from its |
| 1119 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | - at the very least (if the destroyed slots and __dict__ don't |
| 1124 | cause problems) the object's type gets decref'ed a second |
| 1125 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | The remedy is to make sure that if the code between trashcan |
| 1128 | begin and end in subtype_dealloc() is called, the code between |
| 1129 | trashcan begin and end in basedealloc() will also be called. |
| 1130 | This is done by decrementing the level after passing into the |
| 1131 | trashcan block, and incrementing it just before leaving the |
| 1132 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | But now it's possible that a chain of objects consisting solely |
| 1135 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1136 | the trashcan mechanism completely: the decremented level means |
| 1137 | that the effective level never reaches the limit. Therefore, we |
| 1138 | *increment* the level *before* entering the trashcan block, and |
| 1139 | matchingly decrement it after leaving. This means the trashcan |
| 1140 | 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] | 1141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | Q. Are there any live examples of code in need of all this |
| 1143 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1146 | compiled in debug mode) before the trashcan level manipulations |
| 1147 | were added. For more discussion, see SF patches 581742, 575073 |
| 1148 | and bug 574207. |
| 1149 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1152 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1153 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1154 | /* type test with subclassing support */ |
| 1155 | |
| 1156 | int |
| 1157 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | mro = a->tp_mro; |
| 1162 | if (mro != NULL) { |
| 1163 | /* Deal with multiple inheritance without recursion |
| 1164 | by walking the MRO tuple */ |
| 1165 | Py_ssize_t i, n; |
| 1166 | assert(PyTuple_Check(mro)); |
| 1167 | n = PyTuple_GET_SIZE(mro); |
| 1168 | for (i = 0; i < n; i++) { |
| 1169 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1170 | return 1; |
| 1171 | } |
| 1172 | return 0; |
| 1173 | } |
| 1174 | else { |
| 1175 | /* a is not completely initilized yet; follow tp_base */ |
| 1176 | do { |
| 1177 | if (a == b) |
| 1178 | return 1; |
| 1179 | a = a->tp_base; |
| 1180 | } while (a != NULL); |
| 1181 | return b == &PyBaseObject_Type; |
| 1182 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1185 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1186 | without looking in the instance dictionary |
| 1187 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1189 | 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] | 1190 | static variable used to cache the interned Python string. |
| 1191 | |
| 1192 | Two variants: |
| 1193 | |
| 1194 | - lookup_maybe() returns NULL without raising an exception |
| 1195 | when the _PyType_Lookup() call fails; |
| 1196 | |
| 1197 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1198 | |
| 1199 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1200 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1201 | |
| 1202 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1203 | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1204 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1205 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1206 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1207 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if (res != NULL) { |
| 1209 | descrgetfunc f; |
| 1210 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1211 | Py_INCREF(res); |
| 1212 | else |
| 1213 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1214 | } |
| 1215 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1218 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1219 | lookup_method(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1220 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1221 | PyObject *res = lookup_maybe(self, attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | if (res == NULL && !PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1223 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1227 | PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1228 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1229 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1230 | return lookup_maybe(self, attrid); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1233 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1235 | as lookup_method to cache the interned name string object. */ |
| 1236 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1237 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1238 | call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | va_list va; |
| 1241 | PyObject *args, *func = 0, *retval; |
| 1242 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1243 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1244 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | if (func == NULL) { |
| 1246 | va_end(va); |
| 1247 | if (!PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1248 | PyErr_SetObject(PyExc_AttributeError, nameid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | return NULL; |
| 1250 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | if (format && *format) |
| 1253 | args = Py_VaBuildValue(format, va); |
| 1254 | else |
| 1255 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | if (args == NULL) |
| 1260 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | assert(PyTuple_Check(args)); |
| 1263 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | Py_DECREF(args); |
| 1266 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1272 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1273 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1274 | call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | va_list va; |
| 1277 | PyObject *args, *func = 0, *retval; |
| 1278 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1279 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1280 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | if (func == NULL) { |
| 1282 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1283 | if (!PyErr_Occurred()) |
| 1284 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | return NULL; |
| 1286 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | if (format && *format) |
| 1289 | args = Py_VaBuildValue(format, va); |
| 1290 | else |
| 1291 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | if (args == NULL) |
| 1296 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | assert(PyTuple_Check(args)); |
| 1299 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | Py_DECREF(args); |
| 1302 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1307 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1308 | Method resolution order algorithm C3 described in |
| 1309 | "A Monotonic Superclass Linearization for Dylan", |
| 1310 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1311 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1312 | (OOPSLA 1996) |
| 1313 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1314 | Some notes about the rules implied by C3: |
| 1315 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1316 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1317 | It isn't legal to repeat a class in a list of base classes. |
| 1318 | |
| 1319 | The next three properties are the 3 constraints in "C3". |
| 1320 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1321 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1322 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1323 | subclasses of C. |
| 1324 | |
| 1325 | Monotonicity. |
| 1326 | The MRO of a class must be an extension without reordering of the |
| 1327 | MRO of each of its superclasses. |
| 1328 | |
| 1329 | Extended Precedence Graph (EPG). |
| 1330 | Linearization is consistent if there is a path in the EPG from |
| 1331 | each class to all its successors in the linearization. See |
| 1332 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1333 | */ |
| 1334 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1335 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1336 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | Py_ssize_t j, size; |
| 1338 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | for (j = whence+1; j < size; j++) { |
| 1341 | if (PyList_GET_ITEM(list, j) == o) |
| 1342 | return 1; |
| 1343 | } |
| 1344 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1347 | static PyObject * |
| 1348 | class_name(PyObject *cls) |
| 1349 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1350 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | if (name == NULL) { |
| 1352 | PyErr_Clear(); |
| 1353 | Py_XDECREF(name); |
| 1354 | name = PyObject_Repr(cls); |
| 1355 | } |
| 1356 | if (name == NULL) |
| 1357 | return NULL; |
| 1358 | if (!PyUnicode_Check(name)) { |
| 1359 | Py_DECREF(name); |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static int |
| 1366 | check_duplicates(PyObject *list) |
| 1367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | Py_ssize_t i, j, n; |
| 1369 | /* Let's use a quadratic time algorithm, |
| 1370 | assuming that the bases lists is short. |
| 1371 | */ |
| 1372 | n = PyList_GET_SIZE(list); |
| 1373 | for (i = 0; i < n; i++) { |
| 1374 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1375 | for (j = i + 1; j < n; j++) { |
| 1376 | if (PyList_GET_ITEM(list, j) == o) { |
| 1377 | o = class_name(o); |
| 1378 | if (o != NULL) { |
| 1379 | PyErr_Format(PyExc_TypeError, |
| 1380 | "duplicate base class %U", |
| 1381 | o); |
| 1382 | Py_DECREF(o); |
| 1383 | } else { |
| 1384 | PyErr_SetString(PyExc_TypeError, |
| 1385 | "duplicate base class"); |
| 1386 | } |
| 1387 | return -1; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* Raise a TypeError for an MRO order disagreement. |
| 1395 | |
| 1396 | It's hard to produce a good error message. In the absence of better |
| 1397 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | 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] | 1399 | order in which they should be put in the MRO, but it's hard to |
| 1400 | diagnose what constraint can't be satisfied. |
| 1401 | */ |
| 1402 | |
| 1403 | static void |
| 1404 | set_mro_error(PyObject *to_merge, int *remain) |
| 1405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | Py_ssize_t i, n, off, to_merge_size; |
| 1407 | char buf[1000]; |
| 1408 | PyObject *k, *v; |
| 1409 | PyObject *set = PyDict_New(); |
| 1410 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1413 | for (i = 0; i < to_merge_size; i++) { |
| 1414 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1415 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1416 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1417 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1418 | Py_DECREF(set); |
| 1419 | return; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1426 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | i = 0; |
| 1428 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1429 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1430 | char *name_str; |
| 1431 | if (name != NULL) { |
| 1432 | name_str = _PyUnicode_AsString(name); |
| 1433 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1434 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1435 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1436 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1437 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | Py_XDECREF(name); |
| 1439 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1440 | buf[off++] = ','; |
| 1441 | buf[off] = '\0'; |
| 1442 | } |
| 1443 | } |
| 1444 | PyErr_SetString(PyExc_TypeError, buf); |
| 1445 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1448 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1449 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1451 | int *remain; |
| 1452 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | /* remain stores an index into each sublist of to_merge. |
| 1457 | remain[i] is the index of the next base in to_merge[i] |
| 1458 | that is not included in acc. |
| 1459 | */ |
| 1460 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1461 | if (remain == NULL) { |
| 1462 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | return -1; |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1464 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | for (i = 0; i < to_merge_size; i++) |
| 1466 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1467 | |
| 1468 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | empty_cnt = 0; |
| 1470 | for (i = 0; i < to_merge_size; i++) { |
| 1471 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1476 | empty_cnt++; |
| 1477 | continue; |
| 1478 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | The input sequences alone can determine the choice. |
| 1483 | If not, choose the class which appears in the MRO |
| 1484 | of the earliest direct superclass of the new class. |
| 1485 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1488 | for (j = 0; j < to_merge_size; j++) { |
| 1489 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1490 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1491 | goto skip; /* continue outer loop */ |
| 1492 | } |
| 1493 | } |
| 1494 | ok = PyList_Append(acc, candidate); |
| 1495 | if (ok < 0) { |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1496 | PyMem_FREE(remain); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | return -1; |
| 1498 | } |
| 1499 | for (j = 0; j < to_merge_size; j++) { |
| 1500 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1501 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1502 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1503 | remain[j]++; |
| 1504 | } |
| 1505 | } |
| 1506 | goto again; |
| 1507 | skip: ; |
| 1508 | } |
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 | if (empty_cnt == to_merge_size) { |
| 1511 | PyMem_FREE(remain); |
| 1512 | return 0; |
| 1513 | } |
| 1514 | set_mro_error(to_merge, remain); |
| 1515 | PyMem_FREE(remain); |
| 1516 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1519 | static PyObject * |
| 1520 | mro_implementation(PyTypeObject *type) |
| 1521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | Py_ssize_t i, n; |
| 1523 | int ok; |
| 1524 | PyObject *bases, *result; |
| 1525 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | if (type->tp_dict == NULL) { |
| 1528 | if (PyType_Ready(type) < 0) |
| 1529 | return NULL; |
| 1530 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | /* Find a superclass linearization that honors the constraints |
| 1533 | of the explicit lists of bases and the constraints implied by |
| 1534 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | to_merge is a list of lists, where each list is a superclass |
| 1537 | linearization implied by a base class. The last element of |
| 1538 | to_merge is the declared list of bases. |
| 1539 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | bases = type->tp_bases; |
| 1542 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | to_merge = PyList_New(n+1); |
| 1545 | if (to_merge == NULL) |
| 1546 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | for (i = 0; i < n; i++) { |
| 1549 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1550 | PyObject *parentMRO; |
| 1551 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1552 | if (parentMRO == NULL) { |
| 1553 | Py_DECREF(to_merge); |
| 1554 | return NULL; |
| 1555 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1558 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | bases_aslist = PySequence_List(bases); |
| 1561 | if (bases_aslist == NULL) { |
| 1562 | Py_DECREF(to_merge); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | /* This is just a basic sanity check. */ |
| 1566 | if (check_duplicates(bases_aslist) < 0) { |
| 1567 | Py_DECREF(to_merge); |
| 1568 | Py_DECREF(bases_aslist); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1574 | if (result == NULL) { |
| 1575 | Py_DECREF(to_merge); |
| 1576 | return NULL; |
| 1577 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | ok = pmerge(result, to_merge); |
| 1580 | Py_DECREF(to_merge); |
| 1581 | if (ok < 0) { |
| 1582 | Py_DECREF(result); |
| 1583 | return NULL; |
| 1584 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1590 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static int |
| 1598 | mro_internal(PyTypeObject *type) |
| 1599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | PyObject *mro, *result, *tuple; |
| 1601 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | if (Py_TYPE(type) == &PyType_Type) { |
| 1604 | result = mro_implementation(type); |
| 1605 | } |
| 1606 | else { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1607 | _Py_IDENTIFIER(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | checkit = 1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1609 | mro = lookup_method((PyObject *)type, &PyId_mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | if (mro == NULL) |
| 1611 | return -1; |
| 1612 | result = PyObject_CallObject(mro, NULL); |
| 1613 | Py_DECREF(mro); |
| 1614 | } |
| 1615 | if (result == NULL) |
| 1616 | return -1; |
| 1617 | tuple = PySequence_Tuple(result); |
| 1618 | Py_DECREF(result); |
| 1619 | if (tuple == NULL) |
| 1620 | return -1; |
| 1621 | if (checkit) { |
| 1622 | Py_ssize_t i, len; |
| 1623 | PyObject *cls; |
| 1624 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | for (i = 0; i < len; i++) { |
| 1631 | PyTypeObject *t; |
| 1632 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1633 | if (!PyType_Check(cls)) { |
| 1634 | PyErr_Format(PyExc_TypeError, |
| 1635 | "mro() returned a non-class ('%.500s')", |
| 1636 | Py_TYPE(cls)->tp_name); |
| 1637 | Py_DECREF(tuple); |
| 1638 | return -1; |
| 1639 | } |
| 1640 | t = (PyTypeObject*)cls; |
| 1641 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1642 | PyErr_Format(PyExc_TypeError, |
| 1643 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1644 | t->tp_name); |
| 1645 | Py_DECREF(tuple); |
| 1646 | return -1; |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1653 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | from the custom MRO */ |
| 1655 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | /* Calculate the best base amongst multiple base classes. |
| 1664 | This is the first one that's on the path to the "solid base". */ |
| 1665 | |
| 1666 | static PyTypeObject * |
| 1667 | best_base(PyObject *bases) |
| 1668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | Py_ssize_t i, n; |
| 1670 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1671 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | assert(PyTuple_Check(bases)); |
| 1674 | n = PyTuple_GET_SIZE(bases); |
| 1675 | assert(n > 0); |
| 1676 | base = NULL; |
| 1677 | winner = NULL; |
| 1678 | for (i = 0; i < n; i++) { |
| 1679 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1680 | if (!PyType_Check(base_proto)) { |
| 1681 | PyErr_SetString( |
| 1682 | PyExc_TypeError, |
| 1683 | "bases must be types"); |
| 1684 | return NULL; |
| 1685 | } |
| 1686 | base_i = (PyTypeObject *)base_proto; |
| 1687 | if (base_i->tp_dict == NULL) { |
| 1688 | if (PyType_Ready(base_i) < 0) |
| 1689 | return NULL; |
| 1690 | } |
| 1691 | candidate = solid_base(base_i); |
| 1692 | if (winner == NULL) { |
| 1693 | winner = candidate; |
| 1694 | base = base_i; |
| 1695 | } |
| 1696 | else if (PyType_IsSubtype(winner, candidate)) |
| 1697 | ; |
| 1698 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1699 | winner = candidate; |
| 1700 | base = base_i; |
| 1701 | } |
| 1702 | else { |
| 1703 | PyErr_SetString( |
| 1704 | PyExc_TypeError, |
| 1705 | "multiple bases have " |
| 1706 | "instance lay-out conflict"); |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1710 | assert (base != NULL); |
| 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | static int |
| 1716 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | size_t t_size = type->tp_basicsize; |
| 1719 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1722 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1723 | /* If itemsize is involved, stricter rules */ |
| 1724 | return t_size != b_size || |
| 1725 | type->tp_itemsize != base->tp_itemsize; |
| 1726 | } |
| 1727 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1728 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1729 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1730 | t_size -= sizeof(PyObject *); |
| 1731 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1732 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1733 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1734 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | static PyTypeObject * |
| 1740 | solid_base(PyTypeObject *type) |
| 1741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | if (type->tp_base) |
| 1745 | base = solid_base(type->tp_base); |
| 1746 | else |
| 1747 | base = &PyBaseObject_Type; |
| 1748 | if (extra_ivars(type, base)) |
| 1749 | return type; |
| 1750 | else |
| 1751 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1754 | static void object_dealloc(PyObject *); |
| 1755 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1756 | static int update_slot(PyTypeObject *, PyObject *); |
| 1757 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1758 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1759 | /* |
| 1760 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1761 | * inherited from various builtin types. The builtin base usually provides |
| 1762 | * its own __dict__ descriptor, so we use that when we can. |
| 1763 | */ |
| 1764 | static PyTypeObject * |
| 1765 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | while (type->tp_base != NULL) { |
| 1768 | if (type->tp_dictoffset != 0 && |
| 1769 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1770 | return type; |
| 1771 | type = type->tp_base; |
| 1772 | } |
| 1773 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | static PyObject * |
| 1777 | get_dict_descriptor(PyTypeObject *type) |
| 1778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1780 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1781 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1783 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | static void |
| 1789 | raise_dict_descr_error(PyObject *obj) |
| 1790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | PyErr_Format(PyExc_TypeError, |
| 1792 | "this __dict__ descriptor does not support " |
| 1793 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1796 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1797 | subtype_dict(PyObject *obj, void *context) |
| 1798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1802 | if (base != NULL) { |
| 1803 | descrgetfunc func; |
| 1804 | PyObject *descr = get_dict_descriptor(base); |
| 1805 | if (descr == NULL) { |
| 1806 | raise_dict_descr_error(obj); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | func = Py_TYPE(descr)->tp_descr_get; |
| 1810 | if (func == NULL) { |
| 1811 | raise_dict_descr_error(obj); |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1815 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1816 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1819 | static int |
| 1820 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1821 | { |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1822 | PyObject *dict, **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1826 | if (base != NULL) { |
| 1827 | descrsetfunc func; |
| 1828 | PyObject *descr = get_dict_descriptor(base); |
| 1829 | if (descr == NULL) { |
| 1830 | raise_dict_descr_error(obj); |
| 1831 | return -1; |
| 1832 | } |
| 1833 | func = Py_TYPE(descr)->tp_descr_set; |
| 1834 | if (func == NULL) { |
| 1835 | raise_dict_descr_error(obj); |
| 1836 | return -1; |
| 1837 | } |
| 1838 | return func(descr, obj, value); |
| 1839 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1840 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | dictptr = _PyObject_GetDictPtr(obj); |
| 1842 | if (dictptr == NULL) { |
| 1843 | PyErr_SetString(PyExc_AttributeError, |
| 1844 | "This object has no __dict__"); |
| 1845 | return -1; |
| 1846 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 1847 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | PyErr_Format(PyExc_TypeError, |
| 1849 | "__dict__ must be set to a dictionary, " |
| 1850 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1851 | return -1; |
| 1852 | } |
| 1853 | dict = *dictptr; |
| 1854 | Py_XINCREF(value); |
| 1855 | *dictptr = value; |
| 1856 | Py_XDECREF(dict); |
| 1857 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1860 | static PyObject * |
| 1861 | subtype_getweakref(PyObject *obj, void *context) |
| 1862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | PyObject **weaklistptr; |
| 1864 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1867 | PyErr_SetString(PyExc_AttributeError, |
| 1868 | "This object has no __weakref__"); |
| 1869 | return NULL; |
| 1870 | } |
| 1871 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1872 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1873 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1874 | weaklistptr = (PyObject **) |
| 1875 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1876 | if (*weaklistptr == NULL) |
| 1877 | result = Py_None; |
| 1878 | else |
| 1879 | result = *weaklistptr; |
| 1880 | Py_INCREF(result); |
| 1881 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1884 | /* Three variants on the subtype_getsets list. */ |
| 1885 | |
| 1886 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | {"__dict__", subtype_dict, subtype_setdict, |
| 1888 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1889 | {"__weakref__", subtype_getweakref, NULL, |
| 1890 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1891 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1894 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | {"__dict__", subtype_dict, subtype_setdict, |
| 1896 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1897 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1898 | }; |
| 1899 | |
| 1900 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | {"__weakref__", subtype_getweakref, NULL, |
| 1902 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1903 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1904 | }; |
| 1905 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1906 | static int |
| 1907 | valid_identifier(PyObject *s) |
| 1908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | if (!PyUnicode_Check(s)) { |
| 1910 | PyErr_Format(PyExc_TypeError, |
| 1911 | "__slots__ items must be strings, not '%.200s'", |
| 1912 | Py_TYPE(s)->tp_name); |
| 1913 | return 0; |
| 1914 | } |
| 1915 | if (!PyUnicode_IsIdentifier(s)) { |
| 1916 | PyErr_SetString(PyExc_TypeError, |
| 1917 | "__slots__ must be identifiers"); |
| 1918 | return 0; |
| 1919 | } |
| 1920 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1923 | /* Forward */ |
| 1924 | static int |
| 1925 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1926 | |
| 1927 | static int |
| 1928 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | assert(args != NULL && PyTuple_Check(args)); |
| 1933 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1936 | PyErr_SetString(PyExc_TypeError, |
| 1937 | "type.__init__() takes no keyword arguments"); |
| 1938 | return -1; |
| 1939 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | if (args != NULL && PyTuple_Check(args) && |
| 1942 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1943 | PyErr_SetString(PyExc_TypeError, |
| 1944 | "type.__init__() takes 1 or 3 arguments"); |
| 1945 | return -1; |
| 1946 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | /* Call object.__init__(self) now. */ |
| 1949 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1950 | args = PyTuple_GetSlice(args, 0, 0); |
| 1951 | res = object_init(cls, args, NULL); |
| 1952 | Py_DECREF(args); |
| 1953 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 1956 | unsigned long |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 1957 | PyType_GetFlags(PyTypeObject *type) |
| 1958 | { |
| 1959 | return type->tp_flags; |
| 1960 | } |
| 1961 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 1962 | /* Determine the most derived metatype. */ |
| 1963 | PyTypeObject * |
| 1964 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 1965 | { |
| 1966 | Py_ssize_t i, nbases; |
| 1967 | PyTypeObject *winner; |
| 1968 | PyObject *tmp; |
| 1969 | PyTypeObject *tmptype; |
| 1970 | |
| 1971 | /* Determine the proper metatype to deal with this, |
| 1972 | and check for metatype conflicts while we're at it. |
| 1973 | Note that if some other metatype wins to contract, |
| 1974 | it's possible that its instances are not types. */ |
| 1975 | |
| 1976 | nbases = PyTuple_GET_SIZE(bases); |
| 1977 | winner = metatype; |
| 1978 | for (i = 0; i < nbases; i++) { |
| 1979 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1980 | tmptype = Py_TYPE(tmp); |
| 1981 | if (PyType_IsSubtype(winner, tmptype)) |
| 1982 | continue; |
| 1983 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1984 | winner = tmptype; |
| 1985 | continue; |
| 1986 | } |
| 1987 | /* else: */ |
| 1988 | PyErr_SetString(PyExc_TypeError, |
| 1989 | "metaclass conflict: " |
| 1990 | "the metaclass of a derived class " |
| 1991 | "must be a (non-strict) subclass " |
| 1992 | "of the metaclasses of all its bases"); |
| 1993 | return NULL; |
| 1994 | } |
| 1995 | return winner; |
| 1996 | } |
| 1997 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1998 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1999 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 2000 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2001 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2003 | PyObject *qualname, *slots = NULL, *tmp, *newslots; |
| 2004 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | PyHeapTypeObject *et; |
| 2006 | PyMemberDef *mp; |
| 2007 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 2008 | int j, may_add_dict, may_add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2009 | _Py_IDENTIFIER(__qualname__); |
| 2010 | _Py_IDENTIFIER(__slots__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | assert(args != NULL && PyTuple_Check(args)); |
| 2013 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | /* Special case: type(x) should return x->ob_type */ |
| 2016 | { |
| 2017 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2018 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2021 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2022 | Py_INCREF(Py_TYPE(x)); |
| 2023 | return (PyObject *) Py_TYPE(x); |
| 2024 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2027 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2028 | a msg saying type() needs exactly 3. */ |
| 2029 | if (nargs + nkwds != 3) { |
| 2030 | PyErr_SetString(PyExc_TypeError, |
| 2031 | "type() takes 1 or 3 arguments"); |
| 2032 | return NULL; |
| 2033 | } |
| 2034 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | /* Check arguments: (name, bases, dict) */ |
| 2037 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2038 | &name, |
| 2039 | &PyTuple_Type, &bases, |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2040 | &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2042 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2043 | /* Determine the proper metatype to deal with this: */ |
| 2044 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2045 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | return NULL; |
| 2047 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | if (winner != metatype) { |
| 2050 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2051 | return winner->tp_new(winner, args, kwds); |
| 2052 | metatype = winner; |
| 2053 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2056 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | if (nbases == 0) { |
| 2058 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2059 | if (bases == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2060 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | nbases = 1; |
| 2062 | } |
| 2063 | else |
| 2064 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | /* Calculate best base, and check that all bases are type objects */ |
| 2067 | base = best_base(bases); |
| 2068 | if (base == NULL) { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2069 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | } |
| 2071 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2072 | PyErr_Format(PyExc_TypeError, |
| 2073 | "type '%.100s' is not an acceptable base type", |
| 2074 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2075 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2077 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2078 | dict = PyDict_Copy(orig_dict); |
| 2079 | if (dict == NULL) |
| 2080 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2083 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | nslots = 0; |
| 2085 | add_dict = 0; |
| 2086 | add_weak = 0; |
| 2087 | may_add_dict = base->tp_dictoffset == 0; |
| 2088 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2089 | if (slots == NULL) { |
| 2090 | if (may_add_dict) { |
| 2091 | add_dict++; |
| 2092 | } |
| 2093 | if (may_add_weak) { |
| 2094 | add_weak++; |
| 2095 | } |
| 2096 | } |
| 2097 | else { |
| 2098 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | /* Make it into a tuple */ |
| 2101 | if (PyUnicode_Check(slots)) |
| 2102 | slots = PyTuple_Pack(1, slots); |
| 2103 | else |
| 2104 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2105 | if (slots == NULL) |
| 2106 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | /* Are slots allowed? */ |
| 2110 | nslots = PyTuple_GET_SIZE(slots); |
| 2111 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2112 | PyErr_Format(PyExc_TypeError, |
| 2113 | "nonempty __slots__ " |
| 2114 | "not supported for subtype of '%s'", |
| 2115 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2116 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | /* Check for valid slot names and two special cases */ |
| 2120 | for (i = 0; i < nslots; i++) { |
| 2121 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2122 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2123 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | assert(PyUnicode_Check(tmp)); |
| 2125 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2126 | if (!may_add_dict || add_dict) { |
| 2127 | PyErr_SetString(PyExc_TypeError, |
| 2128 | "__dict__ slot disallowed: " |
| 2129 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2130 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | } |
| 2132 | add_dict++; |
| 2133 | } |
| 2134 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2135 | if (!may_add_weak || add_weak) { |
| 2136 | PyErr_SetString(PyExc_TypeError, |
| 2137 | "__weakref__ slot disallowed: " |
| 2138 | "either we already got one, " |
| 2139 | "or __itemsize__ != 0"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2140 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | } |
| 2142 | add_weak++; |
| 2143 | } |
| 2144 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | /* Copy slots into a list, mangle names and sort them. |
| 2147 | Sorted names are needed for __class__ assignment. |
| 2148 | Convert them back to tuple at the end. |
| 2149 | */ |
| 2150 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2151 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2152 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | for (i = j = 0; i < nslots; i++) { |
| 2154 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2155 | if ((add_dict && |
| 2156 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2157 | (add_weak && |
| 2158 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2159 | continue; |
| 2160 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2161 | if (!tmp) { |
| 2162 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2163 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2164 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2166 | if (PyDict_GetItem(dict, tmp)) { |
| 2167 | PyErr_Format(PyExc_ValueError, |
| 2168 | "%R in __slots__ conflicts with class variable", |
| 2169 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2170 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2171 | goto error; |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2172 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | j++; |
| 2174 | } |
| 2175 | assert(j == nslots - add_dict - add_weak); |
| 2176 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2177 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2180 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | } |
| 2182 | slots = PyList_AsTuple(newslots); |
| 2183 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2184 | if (slots == NULL) |
| 2185 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | /* Secondary bases may provide weakrefs or dict */ |
| 2188 | if (nbases > 1 && |
| 2189 | ((may_add_dict && !add_dict) || |
| 2190 | (may_add_weak && !add_weak))) { |
| 2191 | for (i = 0; i < nbases; i++) { |
| 2192 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2193 | if (tmp == (PyObject *)base) |
| 2194 | continue; /* Skip primary base */ |
| 2195 | assert(PyType_Check(tmp)); |
| 2196 | tmptype = (PyTypeObject *)tmp; |
| 2197 | if (may_add_dict && !add_dict && |
| 2198 | tmptype->tp_dictoffset != 0) |
| 2199 | add_dict++; |
| 2200 | if (may_add_weak && !add_weak && |
| 2201 | tmptype->tp_weaklistoffset != 0) |
| 2202 | add_weak++; |
| 2203 | if (may_add_dict && !add_dict) |
| 2204 | continue; |
| 2205 | if (may_add_weak && !add_weak) |
| 2206 | continue; |
| 2207 | /* Nothing more to check */ |
| 2208 | break; |
| 2209 | } |
| 2210 | } |
| 2211 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | /* Allocate the type object */ |
| 2214 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2215 | if (type == NULL) |
| 2216 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | /* Keep name and slots alive in the extended type object */ |
| 2219 | et = (PyHeapTypeObject *)type; |
| 2220 | Py_INCREF(name); |
| 2221 | et->ht_name = name; |
| 2222 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2223 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | /* Initialize tp_flags */ |
| 2226 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2227 | Py_TPFLAGS_BASETYPE; |
| 2228 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2229 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | /* Initialize essential fields */ |
| 2232 | type->tp_as_number = &et->as_number; |
| 2233 | type->tp_as_sequence = &et->as_sequence; |
| 2234 | type->tp_as_mapping = &et->as_mapping; |
| 2235 | type->tp_as_buffer = &et->as_buffer; |
| 2236 | type->tp_name = _PyUnicode_AsString(name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2237 | if (!type->tp_name) |
| 2238 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | /* Set tp_base and tp_bases */ |
| 2241 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2242 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | Py_INCREF(base); |
| 2244 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2247 | Py_INCREF(dict); |
| 2248 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2251 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | tmp = PyEval_GetGlobals(); |
| 2253 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2254 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2256 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2257 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2258 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2262 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2263 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2264 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2265 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2266 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2267 | if (qualname != NULL) { |
| 2268 | if (!PyUnicode_Check(qualname)) { |
| 2269 | PyErr_Format(PyExc_TypeError, |
| 2270 | "type __qualname__ must be a str, not %s", |
| 2271 | Py_TYPE(qualname)->tp_name); |
| 2272 | goto error; |
| 2273 | } |
| 2274 | } |
Benjamin Peterson | 8afa7fa | 2012-10-30 23:51:03 -0400 | [diff] [blame] | 2275 | et->ht_qualname = qualname ? qualname : et->ht_name; |
| 2276 | Py_INCREF(et->ht_qualname); |
| 2277 | if (qualname != NULL && PyDict_DelItem(dict, PyId___qualname__.object) < 0) |
| 2278 | goto error; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2281 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2282 | if that fails, it will still look into __dict__. |
| 2283 | */ |
| 2284 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2285 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2287 | Py_ssize_t len; |
| 2288 | char *doc_str; |
| 2289 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | doc_str = _PyUnicode_AsString(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2292 | if (doc_str == NULL) |
| 2293 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2295 | len = strlen(doc_str); |
| 2296 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2297 | if (tp_doc == NULL) { |
| 2298 | PyErr_NoMemory(); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2299 | goto error; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2300 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | memcpy(tp_doc, doc_str, len + 1); |
| 2302 | type->tp_doc = tp_doc; |
| 2303 | } |
| 2304 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | /* Special-case __new__: if it's a plain function, |
| 2307 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2308 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2310 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2311 | if (tmp == NULL) |
| 2312 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2313 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) |
| 2314 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | Py_DECREF(tmp); |
| 2316 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2319 | mp = PyHeapType_GET_MEMBERS(et); |
| 2320 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2321 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | for (i = 0; i < nslots; i++, mp++) { |
| 2323 | mp->name = _PyUnicode_AsString( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2324 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2325 | if (mp->name == NULL) |
| 2326 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | mp->type = T_OBJECT_EX; |
| 2328 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | /* __dict__ and __weakref__ are already filtered out */ |
| 2331 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2332 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | slotoffset += sizeof(PyObject *); |
| 2335 | } |
| 2336 | } |
| 2337 | if (add_dict) { |
| 2338 | if (base->tp_itemsize) |
| 2339 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2340 | else |
| 2341 | type->tp_dictoffset = slotoffset; |
| 2342 | slotoffset += sizeof(PyObject *); |
| 2343 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2344 | if (type->tp_dictoffset) { |
| 2345 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2346 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | if (add_weak) { |
| 2348 | assert(!base->tp_itemsize); |
| 2349 | type->tp_weaklistoffset = slotoffset; |
| 2350 | slotoffset += sizeof(PyObject *); |
| 2351 | } |
| 2352 | type->tp_basicsize = slotoffset; |
| 2353 | type->tp_itemsize = base->tp_itemsize; |
| 2354 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2357 | type->tp_getset = subtype_getsets_full; |
| 2358 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2359 | type->tp_getset = subtype_getsets_weakref_only; |
| 2360 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2361 | type->tp_getset = subtype_getsets_dict_only; |
| 2362 | else |
| 2363 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | /* Special case some slots */ |
| 2366 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2367 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2368 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2369 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2370 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2371 | } |
| 2372 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | /* Enable GC unless there are really no instance variables possible */ |
| 2375 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2376 | type->tp_itemsize == 0)) |
| 2377 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | /* Always override allocation strategy to use regular heap */ |
| 2380 | type->tp_alloc = PyType_GenericAlloc; |
| 2381 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2382 | type->tp_free = PyObject_GC_Del; |
| 2383 | type->tp_traverse = subtype_traverse; |
| 2384 | type->tp_clear = subtype_clear; |
| 2385 | } |
| 2386 | else |
| 2387 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2390 | if (PyType_Ready(type) < 0) |
| 2391 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | /* Put the proper slots in place */ |
| 2394 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2395 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2396 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2398 | |
| 2399 | error: |
| 2400 | Py_XDECREF(dict); |
| 2401 | Py_XDECREF(bases); |
| 2402 | Py_XDECREF(slots); |
| 2403 | Py_XDECREF(type); |
| 2404 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2407 | static short slotoffsets[] = { |
| 2408 | -1, /* invalid slot */ |
| 2409 | #include "typeslots.inc" |
| 2410 | }; |
| 2411 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2412 | PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2413 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2414 | { |
| 2415 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2416 | PyTypeObject *type, *base; |
| 2417 | char *s; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2418 | char *res_start = (char*)res; |
| 2419 | PyType_Slot *slot; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2420 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2421 | /* Set the type name and qualname */ |
| 2422 | s = strrchr(spec->name, '.'); |
| 2423 | if (s == NULL) |
| 2424 | s = (char*)spec->name; |
| 2425 | else |
| 2426 | s++; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2427 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2428 | if (res == NULL) |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2429 | return NULL; |
| 2430 | type = &res->ht_type; |
Antoine Pitrou | 66a3a7e | 2012-06-24 00:42:59 +0200 | [diff] [blame] | 2431 | /* The flags must be initialized early, before the GC traverses us */ |
| 2432 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2433 | res->ht_name = PyUnicode_FromString(s); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2434 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2435 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2436 | res->ht_qualname = res->ht_name; |
| 2437 | Py_INCREF(res->ht_qualname); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2438 | type->tp_name = spec->name; |
| 2439 | if (!type->tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2440 | goto fail; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2441 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2442 | /* Adjust for empty tuple bases */ |
| 2443 | if (!bases) { |
| 2444 | base = &PyBaseObject_Type; |
| 2445 | /* See whether Py_tp_base(s) was specified */ |
| 2446 | for (slot = spec->slots; slot->slot; slot++) { |
| 2447 | if (slot->slot == Py_tp_base) |
| 2448 | base = slot->pfunc; |
| 2449 | else if (slot->slot == Py_tp_bases) { |
| 2450 | bases = slot->pfunc; |
| 2451 | Py_INCREF(bases); |
| 2452 | } |
| 2453 | } |
| 2454 | if (!bases) |
| 2455 | bases = PyTuple_Pack(1, base); |
| 2456 | if (!bases) |
| 2457 | goto fail; |
| 2458 | } |
| 2459 | else |
| 2460 | Py_INCREF(bases); |
| 2461 | |
| 2462 | /* Calculate best base, and check that all bases are type objects */ |
| 2463 | base = best_base(bases); |
| 2464 | if (base == NULL) { |
| 2465 | goto fail; |
| 2466 | } |
| 2467 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2468 | PyErr_Format(PyExc_TypeError, |
| 2469 | "type '%.100s' is not an acceptable base type", |
| 2470 | base->tp_name); |
| 2471 | goto fail; |
| 2472 | } |
| 2473 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2474 | /* Initialize essential fields */ |
| 2475 | type->tp_as_number = &res->as_number; |
| 2476 | type->tp_as_sequence = &res->as_sequence; |
| 2477 | type->tp_as_mapping = &res->as_mapping; |
| 2478 | type->tp_as_buffer = &res->as_buffer; |
| 2479 | /* Set tp_base and tp_bases */ |
| 2480 | type->tp_bases = bases; |
| 2481 | bases = NULL; |
| 2482 | Py_INCREF(base); |
| 2483 | type->tp_base = base; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2484 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2485 | type->tp_basicsize = spec->basicsize; |
| 2486 | type->tp_itemsize = spec->itemsize; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2487 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2488 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2489 | if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2490 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2491 | goto fail; |
| 2492 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2493 | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
| 2494 | /* Processed above */ |
| 2495 | continue; |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2496 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2497 | |
| 2498 | /* need to make a copy of the docstring slot, which usually |
| 2499 | points to a static string literal */ |
| 2500 | if (slot->slot == Py_tp_doc) { |
Antoine Pitrou | 682d94c | 2012-05-14 14:43:03 +0200 | [diff] [blame] | 2501 | size_t len = strlen(slot->pfunc)+1; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2502 | char *tp_doc = PyObject_MALLOC(len); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2503 | if (tp_doc == NULL) { |
| 2504 | PyErr_NoMemory(); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2505 | goto fail; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2506 | } |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2507 | memcpy(tp_doc, slot->pfunc, len); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2508 | type->tp_doc = tp_doc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2509 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2510 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2511 | if (type->tp_dictoffset) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2512 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2513 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2514 | if (type->tp_dealloc == NULL) { |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2515 | /* It's a heap type, so needs the heap types' dealloc. |
| 2516 | subtype_dealloc will call the base type's tp_dealloc, if |
| 2517 | necessary. */ |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2518 | type->tp_dealloc = subtype_dealloc; |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2519 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2520 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2521 | if (PyType_Ready(type) < 0) |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2522 | goto fail; |
| 2523 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2524 | /* Set type.__module__ */ |
| 2525 | s = strrchr(spec->name, '.'); |
| 2526 | if (s != NULL) |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2527 | _PyDict_SetItemId(type->tp_dict, &PyId___module__, |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2528 | PyUnicode_FromStringAndSize( |
| 2529 | spec->name, (Py_ssize_t)(s - spec->name))); |
| 2530 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2531 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2532 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2533 | fail: |
| 2534 | Py_DECREF(res); |
| 2535 | return NULL; |
| 2536 | } |
| 2537 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2538 | PyObject * |
| 2539 | PyType_FromSpec(PyType_Spec *spec) |
| 2540 | { |
| 2541 | return PyType_FromSpecWithBases(spec, NULL); |
| 2542 | } |
| 2543 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2544 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2545 | /* Internal API to look for a name through the MRO. |
| 2546 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2547 | PyObject * |
| 2548 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | Py_ssize_t i, n; |
| 2551 | PyObject *mro, *res, *base, *dict; |
| 2552 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2555 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2556 | /* fast path */ |
| 2557 | h = MCACHE_HASH_METHOD(type, name); |
| 2558 | if (method_cache[h].version == type->tp_version_tag && |
| 2559 | method_cache[h].name == name) |
| 2560 | return method_cache[h].value; |
| 2561 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | /* Look in tp_dict of types in MRO */ |
| 2564 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | /* If mro is NULL, the type is either not yet initialized |
| 2567 | by PyType_Ready(), or already cleared by type_clear(). |
| 2568 | Either way the safest thing to do is to return NULL. */ |
| 2569 | if (mro == NULL) |
| 2570 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2572 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2573 | /* keep a strong reference to mro because type->tp_mro can be replaced |
| 2574 | during PyDict_GetItem(dict, name) */ |
| 2575 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2576 | assert(PyTuple_Check(mro)); |
| 2577 | n = PyTuple_GET_SIZE(mro); |
| 2578 | for (i = 0; i < n; i++) { |
| 2579 | base = PyTuple_GET_ITEM(mro, i); |
| 2580 | assert(PyType_Check(base)); |
| 2581 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2582 | assert(dict && PyDict_Check(dict)); |
| 2583 | res = PyDict_GetItem(dict, name); |
| 2584 | if (res != NULL) |
| 2585 | break; |
| 2586 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2587 | Py_DECREF(mro); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2590 | h = MCACHE_HASH_METHOD(type, name); |
| 2591 | method_cache[h].version = type->tp_version_tag; |
| 2592 | method_cache[h].value = res; /* borrowed */ |
| 2593 | Py_INCREF(name); |
| 2594 | Py_DECREF(method_cache[h].name); |
| 2595 | method_cache[h].name = name; |
| 2596 | } |
| 2597 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2600 | static PyObject * |
| 2601 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 2602 | { |
| 2603 | PyObject *oname; |
| 2604 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 2605 | if (oname == NULL) |
| 2606 | return NULL; |
| 2607 | return _PyType_Lookup(type, oname); |
| 2608 | } |
| 2609 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2610 | /* This is similar to PyObject_GenericGetAttr(), |
| 2611 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2612 | static PyObject * |
| 2613 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 | PyTypeObject *metatype = Py_TYPE(type); |
| 2616 | PyObject *meta_attribute, *attribute; |
| 2617 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2618 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 2619 | if (!PyUnicode_Check(name)) { |
| 2620 | PyErr_Format(PyExc_TypeError, |
| 2621 | "attribute name must be string, not '%.200s'", |
| 2622 | name->ob_type->tp_name); |
| 2623 | return NULL; |
| 2624 | } |
| 2625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2627 | if (type->tp_dict == NULL) { |
| 2628 | if (PyType_Ready(type) < 0) |
| 2629 | return NULL; |
| 2630 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | /* No readable descriptor found yet */ |
| 2633 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | /* Look for the attribute in the metatype */ |
| 2636 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | if (meta_attribute != NULL) { |
| 2639 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2642 | /* Data descriptors implement tp_descr_set to intercept |
| 2643 | * writes. Assume the attribute is not overridden in |
| 2644 | * type's tp_dict (and bases): call the descriptor now. |
| 2645 | */ |
| 2646 | return meta_get(meta_attribute, (PyObject *)type, |
| 2647 | (PyObject *)metatype); |
| 2648 | } |
| 2649 | Py_INCREF(meta_attribute); |
| 2650 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2653 | * type and its bases */ |
| 2654 | attribute = _PyType_Lookup(type, name); |
| 2655 | if (attribute != NULL) { |
| 2656 | /* Implement descriptor functionality, if any */ |
| 2657 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | if (local_get != NULL) { |
| 2662 | /* NULL 2nd argument indicates the descriptor was |
| 2663 | * found on the target object itself (or a base) */ |
| 2664 | return local_get(attribute, (PyObject *)NULL, |
| 2665 | (PyObject *)type); |
| 2666 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | Py_INCREF(attribute); |
| 2669 | return attribute; |
| 2670 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | /* No attribute found in local __dict__ (or bases): use the |
| 2673 | * descriptor from the metatype, if any */ |
| 2674 | if (meta_get != NULL) { |
| 2675 | PyObject *res; |
| 2676 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2677 | (PyObject *)metatype); |
| 2678 | Py_DECREF(meta_attribute); |
| 2679 | return res; |
| 2680 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2683 | if (meta_attribute != NULL) { |
| 2684 | return meta_attribute; |
| 2685 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | /* Give up */ |
| 2688 | PyErr_Format(PyExc_AttributeError, |
| 2689 | "type object '%.50s' has no attribute '%U'", |
| 2690 | type->tp_name, name); |
| 2691 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | static int |
| 2695 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2698 | PyErr_Format( |
| 2699 | PyExc_TypeError, |
| 2700 | "can't set attributes of built-in/extension type '%s'", |
| 2701 | type->tp_name); |
| 2702 | return -1; |
| 2703 | } |
| 2704 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2705 | return -1; |
| 2706 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2709 | extern void |
| 2710 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
| 2711 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2712 | static void |
| 2713 | type_dealloc(PyTypeObject *type) |
| 2714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | /* Assert this is a heap-allocated type object */ |
| 2718 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2719 | _PyObject_GC_UNTRACK(type); |
| 2720 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2721 | et = (PyHeapTypeObject *)type; |
| 2722 | Py_XDECREF(type->tp_base); |
| 2723 | Py_XDECREF(type->tp_dict); |
| 2724 | Py_XDECREF(type->tp_bases); |
| 2725 | Py_XDECREF(type->tp_mro); |
| 2726 | Py_XDECREF(type->tp_cache); |
| 2727 | Py_XDECREF(type->tp_subclasses); |
| 2728 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2729 | * of most other objects. It's okay to cast it to char *. |
| 2730 | */ |
| 2731 | PyObject_Free((char *)type->tp_doc); |
| 2732 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2733 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | Py_XDECREF(et->ht_slots); |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2735 | if (et->ht_cached_keys) |
| 2736 | _PyDictKeys_DecRef(et->ht_cached_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2740 | static PyObject * |
| 2741 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | PyObject *list, *raw, *ref; |
| 2744 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | list = PyList_New(0); |
| 2747 | if (list == NULL) |
| 2748 | return NULL; |
| 2749 | raw = type->tp_subclasses; |
| 2750 | if (raw == NULL) |
| 2751 | return list; |
| 2752 | assert(PyList_Check(raw)); |
| 2753 | n = PyList_GET_SIZE(raw); |
| 2754 | for (i = 0; i < n; i++) { |
| 2755 | ref = PyList_GET_ITEM(raw, i); |
| 2756 | assert(PyWeakref_CheckRef(ref)); |
| 2757 | ref = PyWeakref_GET_OBJECT(ref); |
| 2758 | if (ref != Py_None) { |
| 2759 | if (PyList_Append(list, ref) < 0) { |
| 2760 | Py_DECREF(list); |
| 2761 | return NULL; |
| 2762 | } |
| 2763 | } |
| 2764 | } |
| 2765 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2768 | static PyObject * |
| 2769 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2774 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2775 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2776 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2777 | defined, as it's expected that only the final set of dict keys is |
| 2778 | interesting. |
| 2779 | Return 0 on success, -1 on error. |
| 2780 | */ |
| 2781 | |
| 2782 | static int |
| 2783 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2784 | { |
| 2785 | PyObject *classdict; |
| 2786 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2787 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2788 | |
| 2789 | assert(PyDict_Check(dict)); |
| 2790 | assert(aclass); |
| 2791 | |
| 2792 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2793 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2794 | if (classdict == NULL) |
| 2795 | PyErr_Clear(); |
| 2796 | else { |
| 2797 | int status = PyDict_Update(dict, classdict); |
| 2798 | Py_DECREF(classdict); |
| 2799 | if (status < 0) |
| 2800 | return -1; |
| 2801 | } |
| 2802 | |
| 2803 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2804 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2805 | if (bases == NULL) |
| 2806 | PyErr_Clear(); |
| 2807 | else { |
| 2808 | /* We have no guarantee that bases is a real tuple */ |
| 2809 | Py_ssize_t i, n; |
| 2810 | n = PySequence_Size(bases); /* This better be right */ |
| 2811 | if (n < 0) |
| 2812 | PyErr_Clear(); |
| 2813 | else { |
| 2814 | for (i = 0; i < n; i++) { |
| 2815 | int status; |
| 2816 | PyObject *base = PySequence_GetItem(bases, i); |
| 2817 | if (base == NULL) { |
| 2818 | Py_DECREF(bases); |
| 2819 | return -1; |
| 2820 | } |
| 2821 | status = merge_class_dict(dict, base); |
| 2822 | Py_DECREF(base); |
| 2823 | if (status < 0) { |
| 2824 | Py_DECREF(bases); |
| 2825 | return -1; |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | Py_DECREF(bases); |
| 2830 | } |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2835 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2836 | metaclass would probably be more confusing than helpful. |
| 2837 | */ |
| 2838 | static PyObject * |
| 2839 | type_dir(PyObject *self, PyObject *args) |
| 2840 | { |
| 2841 | PyObject *result = NULL; |
| 2842 | PyObject *dict = PyDict_New(); |
| 2843 | |
| 2844 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 2845 | result = PyDict_Keys(dict); |
| 2846 | |
| 2847 | Py_XDECREF(dict); |
| 2848 | return result; |
| 2849 | } |
| 2850 | |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2851 | static PyObject* |
| 2852 | type_sizeof(PyObject *self, PyObject *args_unused) |
| 2853 | { |
| 2854 | Py_ssize_t size; |
| 2855 | PyTypeObject *type = (PyTypeObject*)self; |
| 2856 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 2857 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 2858 | size = sizeof(PyHeapTypeObject); |
| 2859 | if (et->ht_cached_keys) |
| 2860 | size += _PyDict_KeysSize(et->ht_cached_keys); |
| 2861 | } |
| 2862 | else |
| 2863 | size = sizeof(PyTypeObject); |
| 2864 | return PyLong_FromSsize_t(size); |
| 2865 | } |
| 2866 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2867 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2869 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2870 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2871 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2872 | {"__prepare__", (PyCFunction)type_prepare, |
| 2873 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2874 | PyDoc_STR("__prepare__() -> dict\n" |
| 2875 | "used to create the namespace for the class statement")}, |
| 2876 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2877 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2879 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2880 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 2881 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2882 | {"__sizeof__", type_sizeof, METH_NOARGS, |
| 2883 | "__sizeof__() -> int\nreturn memory consumption of the type object"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2885 | }; |
| 2886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2887 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2888 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2889 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2890 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2891 | static int |
| 2892 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | /* Because of type_is_gc(), the collector only calls this |
| 2895 | for heaptypes. */ |
Antoine Pitrou | 1351ca6 | 2012-06-24 00:30:12 +0200 | [diff] [blame] | 2896 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2897 | char msg[200]; |
| 2898 | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
| 2899 | type->tp_name); |
| 2900 | Py_FatalError(msg); |
| 2901 | } |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | Py_VISIT(type->tp_dict); |
| 2904 | Py_VISIT(type->tp_cache); |
| 2905 | Py_VISIT(type->tp_mro); |
| 2906 | Py_VISIT(type->tp_bases); |
| 2907 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | /* There's no need to visit type->tp_subclasses or |
| 2910 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2911 | in cycles; tp_subclasses is a list of weak references, |
| 2912 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | static int |
| 2918 | type_clear(PyTypeObject *type) |
| 2919 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2920 | PyDictKeysObject *cached_keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | /* Because of type_is_gc(), the collector only calls this |
| 2922 | for heaptypes. */ |
| 2923 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2924 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2925 | /* We need to invalidate the method cache carefully before clearing |
| 2926 | the dict, so that other objects caught in a reference cycle |
| 2927 | don't start calling destroyed methods. |
| 2928 | |
| 2929 | Otherwise, the only field we need to clear is tp_mro, which is |
| 2930 | part of a hard cycle (its first element is the class itself) that |
| 2931 | 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] | 2932 | tp_clear handler). None of the other fields need to be |
| 2933 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | tp_cache: |
| 2936 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | tp_bases, tp_base: |
| 2939 | If these are involved in a cycle, there must be at least |
| 2940 | one other, mutable object in the cycle, e.g. a base |
| 2941 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | tp_subclasses: |
| 2944 | A list of weak references can't be part of a cycle; and |
| 2945 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | slots (in PyHeapTypeObject): |
| 2948 | A tuple of strings can't be part of a cycle. |
| 2949 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2950 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2951 | PyType_Modified(type); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2952 | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
| 2953 | if (cached_keys != NULL) { |
| 2954 | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
| 2955 | _PyDictKeys_DecRef(cached_keys); |
| 2956 | } |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2957 | if (type->tp_dict) |
| 2958 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | static int |
| 2965 | type_is_gc(PyTypeObject *type) |
| 2966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2970 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2972 | "type", /* tp_name */ |
| 2973 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2974 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2975 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2976 | 0, /* tp_print */ |
| 2977 | 0, /* tp_getattr */ |
| 2978 | 0, /* tp_setattr */ |
| 2979 | 0, /* tp_reserved */ |
| 2980 | (reprfunc)type_repr, /* tp_repr */ |
| 2981 | 0, /* tp_as_number */ |
| 2982 | 0, /* tp_as_sequence */ |
| 2983 | 0, /* tp_as_mapping */ |
| 2984 | 0, /* tp_hash */ |
| 2985 | (ternaryfunc)type_call, /* tp_call */ |
| 2986 | 0, /* tp_str */ |
| 2987 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2988 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2989 | 0, /* tp_as_buffer */ |
| 2990 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2991 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2992 | type_doc, /* tp_doc */ |
| 2993 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2994 | (inquiry)type_clear, /* tp_clear */ |
| 2995 | 0, /* tp_richcompare */ |
| 2996 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2997 | 0, /* tp_iter */ |
| 2998 | 0, /* tp_iternext */ |
| 2999 | type_methods, /* tp_methods */ |
| 3000 | type_members, /* tp_members */ |
| 3001 | type_getsets, /* tp_getset */ |
| 3002 | 0, /* tp_base */ |
| 3003 | 0, /* tp_dict */ |
| 3004 | 0, /* tp_descr_get */ |
| 3005 | 0, /* tp_descr_set */ |
| 3006 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 3007 | type_init, /* tp_init */ |
| 3008 | 0, /* tp_alloc */ |
| 3009 | type_new, /* tp_new */ |
| 3010 | PyObject_GC_Del, /* tp_free */ |
| 3011 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3012 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3013 | |
| 3014 | |
| 3015 | /* The base type of all types (eventually)... except itself. */ |
| 3016 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3017 | /* You may wonder why object.__new__() only complains about arguments |
| 3018 | when object.__init__() is not overridden, and vice versa. |
| 3019 | |
| 3020 | Consider the use cases: |
| 3021 | |
| 3022 | 1. When neither is overridden, we want to hear complaints about |
| 3023 | excess (i.e., any) arguments, since their presence could |
| 3024 | indicate there's a bug. |
| 3025 | |
| 3026 | 2. When defining an Immutable type, we are likely to override only |
| 3027 | __new__(), since __init__() is called too late to initialize an |
| 3028 | Immutable object. Since __new__() defines the signature for the |
| 3029 | type, it would be a pain to have to override __init__() just to |
| 3030 | stop it from complaining about excess arguments. |
| 3031 | |
| 3032 | 3. When defining a Mutable type, we are likely to override only |
| 3033 | __init__(). So here the converse reasoning applies: we don't |
| 3034 | want to have to override __new__() just to stop it from |
| 3035 | complaining. |
| 3036 | |
| 3037 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 3038 | object.__init__(), the latter should complain about excess |
| 3039 | arguments; ditto for __new__(). |
| 3040 | |
| 3041 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 3042 | excess arguments. The best solution that addresses all four use |
| 3043 | cases is as follows: __init__() complains about excess arguments |
| 3044 | unless __new__() is overridden and __init__() is not overridden |
| 3045 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 3046 | symmetrically, __new__() complains about excess arguments unless |
| 3047 | __init__() is overridden and __new__() is not overridden |
| 3048 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 3049 | |
| 3050 | However, for backwards compatibility, this breaks too much code. |
| 3051 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 3052 | methods are overridden; for all other cases we'll use the above |
| 3053 | rules. |
| 3054 | |
| 3055 | */ |
| 3056 | |
| 3057 | /* Forward */ |
| 3058 | static PyObject * |
| 3059 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3060 | |
| 3061 | static int |
| 3062 | excess_args(PyObject *args, PyObject *kwds) |
| 3063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | return PyTuple_GET_SIZE(args) || |
| 3065 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3068 | static int |
| 3069 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | int err = 0; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3072 | PyTypeObject *type = Py_TYPE(self); |
| 3073 | if (excess_args(args, kwds) && |
| 3074 | (type->tp_new == object_new || type->tp_init != object_init)) { |
| 3075 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
| 3076 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | } |
| 3078 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3081 | static PyObject * |
| 3082 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3083 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3084 | if (excess_args(args, kwds) && |
| 3085 | (type->tp_init == object_init || type->tp_new != object_new)) { |
R David Murray | 702a5dc | 2013-02-18 21:39:18 -0500 | [diff] [blame] | 3086 | PyErr_SetString(PyExc_TypeError, "object() takes no parameters"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | return NULL; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3088 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | PyObject *abstract_methods = NULL; |
| 3092 | PyObject *builtins; |
| 3093 | PyObject *sorted; |
| 3094 | PyObject *sorted_methods = NULL; |
| 3095 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3096 | PyObject *comma; |
| 3097 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3098 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 3101 | into joined. */ |
| 3102 | abstract_methods = type_abstractmethods(type, NULL); |
| 3103 | if (abstract_methods == NULL) |
| 3104 | goto error; |
| 3105 | builtins = PyEval_GetBuiltins(); |
| 3106 | if (builtins == NULL) |
| 3107 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3108 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | if (sorted == NULL) |
| 3110 | goto error; |
| 3111 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 3112 | abstract_methods, |
| 3113 | NULL); |
| 3114 | if (sorted_methods == NULL) |
| 3115 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3116 | comma = _PyUnicode_FromId(&comma_id); |
| 3117 | if (comma == NULL) |
| 3118 | goto error; |
| 3119 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | if (joined == NULL) |
| 3121 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | PyErr_Format(PyExc_TypeError, |
| 3124 | "Can't instantiate abstract class %s " |
| 3125 | "with abstract methods %U", |
| 3126 | type->tp_name, |
| 3127 | joined); |
| 3128 | error: |
| 3129 | Py_XDECREF(joined); |
| 3130 | Py_XDECREF(sorted_methods); |
| 3131 | Py_XDECREF(abstract_methods); |
| 3132 | return NULL; |
| 3133 | } |
| 3134 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3137 | static void |
| 3138 | object_dealloc(PyObject *self) |
| 3139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3143 | static PyObject * |
| 3144 | object_repr(PyObject *self) |
| 3145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | PyTypeObject *type; |
| 3147 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | type = Py_TYPE(self); |
| 3150 | mod = type_module(type, NULL); |
| 3151 | if (mod == NULL) |
| 3152 | PyErr_Clear(); |
| 3153 | else if (!PyUnicode_Check(mod)) { |
| 3154 | Py_DECREF(mod); |
| 3155 | mod = NULL; |
| 3156 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3157 | name = type_qualname(type, NULL); |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3158 | if (name == NULL) { |
| 3159 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | return NULL; |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3161 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 3163 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3164 | else |
| 3165 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3166 | type->tp_name, self); |
| 3167 | Py_XDECREF(mod); |
| 3168 | Py_DECREF(name); |
| 3169 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3172 | static PyObject * |
| 3173 | object_str(PyObject *self) |
| 3174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | f = Py_TYPE(self)->tp_repr; |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 3178 | if (f == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | f = object_repr; |
| 3180 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3183 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3184 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | case Py_EQ: |
| 3191 | /* Return NotImplemented instead of False, so if two |
| 3192 | objects are compared, both get a chance at the |
| 3193 | comparison. See issue #1393. */ |
| 3194 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3195 | Py_INCREF(res); |
| 3196 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | case Py_NE: |
| 3199 | /* By default, != returns the opposite of ==, |
| 3200 | unless the latter returns NotImplemented. */ |
| 3201 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3202 | if (res != NULL && res != Py_NotImplemented) { |
| 3203 | int ok = PyObject_IsTrue(res); |
| 3204 | Py_DECREF(res); |
| 3205 | if (ok < 0) |
| 3206 | res = NULL; |
| 3207 | else { |
| 3208 | if (ok) |
| 3209 | res = Py_False; |
| 3210 | else |
| 3211 | res = Py_True; |
| 3212 | Py_INCREF(res); |
| 3213 | } |
| 3214 | } |
| 3215 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | default: |
| 3218 | res = Py_NotImplemented; |
| 3219 | Py_INCREF(res); |
| 3220 | break; |
| 3221 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3227 | object_get_class(PyObject *self, void *closure) |
| 3228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | Py_INCREF(Py_TYPE(self)); |
| 3230 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | static int |
| 3234 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | return a == b || |
| 3237 | (a != NULL && |
| 3238 | b != NULL && |
| 3239 | a->tp_basicsize == b->tp_basicsize && |
| 3240 | a->tp_itemsize == b->tp_itemsize && |
| 3241 | a->tp_dictoffset == b->tp_dictoffset && |
| 3242 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3243 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3244 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | static int |
| 3248 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | PyTypeObject *base = a->tp_base; |
| 3251 | Py_ssize_t size; |
| 3252 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3253 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3254 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | size = base->tp_basicsize; |
| 3256 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3257 | size += sizeof(PyObject *); |
| 3258 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3259 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | /* Check slots compliance */ |
| 3262 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3263 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3264 | if (slots_a && slots_b) { |
| 3265 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3266 | return 0; |
| 3267 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3268 | } |
| 3269 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3273 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3278 | newto->tp_free != oldto->tp_free) |
| 3279 | { |
| 3280 | PyErr_Format(PyExc_TypeError, |
| 3281 | "%s assignment: " |
| 3282 | "'%s' deallocator differs from '%s'", |
| 3283 | attr, |
| 3284 | newto->tp_name, |
| 3285 | oldto->tp_name); |
| 3286 | return 0; |
| 3287 | } |
| 3288 | newbase = newto; |
| 3289 | oldbase = oldto; |
| 3290 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3291 | newbase = newbase->tp_base; |
| 3292 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3293 | oldbase = oldbase->tp_base; |
| 3294 | if (newbase != oldbase && |
| 3295 | (newbase->tp_base != oldbase->tp_base || |
| 3296 | !same_slots_added(newbase, oldbase))) { |
| 3297 | PyErr_Format(PyExc_TypeError, |
| 3298 | "%s assignment: " |
| 3299 | "'%s' object layout differs from '%s'", |
| 3300 | attr, |
| 3301 | newto->tp_name, |
| 3302 | oldto->tp_name); |
| 3303 | return 0; |
| 3304 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3310 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | PyTypeObject *oldto = Py_TYPE(self); |
| 3313 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | if (value == NULL) { |
| 3316 | PyErr_SetString(PyExc_TypeError, |
| 3317 | "can't delete __class__ attribute"); |
| 3318 | return -1; |
| 3319 | } |
| 3320 | if (!PyType_Check(value)) { |
| 3321 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3322 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | Py_TYPE(value)->tp_name); |
| 3324 | return -1; |
| 3325 | } |
| 3326 | newto = (PyTypeObject *)value; |
| 3327 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3328 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3329 | { |
| 3330 | PyErr_Format(PyExc_TypeError, |
| 3331 | "__class__ assignment: only for heap types"); |
| 3332 | return -1; |
| 3333 | } |
| 3334 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3335 | Py_INCREF(newto); |
| 3336 | Py_TYPE(self) = newto; |
| 3337 | Py_DECREF(oldto); |
| 3338 | return 0; |
| 3339 | } |
| 3340 | else { |
| 3341 | return -1; |
| 3342 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | {"__class__", object_get_class, object_set_class, |
| 3347 | PyDoc_STR("the object's class")}, |
| 3348 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3349 | }; |
| 3350 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3351 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3352 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3353 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3354 | - pickle protocols < 2 |
| 3355 | - calculating the list of slot names (done only once per class) |
| 3356 | - the __newobj__ function (which is used as a token but never called) |
| 3357 | */ |
| 3358 | |
| 3359 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3360 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | if (!copyreg_str) { |
| 3365 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3366 | if (copyreg_str == NULL) |
| 3367 | return NULL; |
| 3368 | } |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 3369 | if (!cached_copyreg_module) { |
| 3370 | cached_copyreg_module = PyImport_Import(copyreg_str); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3371 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3372 | |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 3373 | Py_XINCREF(cached_copyreg_module); |
| 3374 | return cached_copyreg_module; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | static PyObject * |
| 3378 | slotnames(PyObject *cls) |
| 3379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | PyObject *clsdict; |
| 3381 | PyObject *copyreg; |
| 3382 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3383 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3384 | _Py_IDENTIFIER(_slotnames); |
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 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3387 | slotnames = _PyDict_GetItemId(clsdict, &PyId___slotnames__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3389 | Py_INCREF(slotnames); |
| 3390 | return slotnames; |
| 3391 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | copyreg = import_copyreg(); |
| 3394 | if (copyreg == NULL) |
| 3395 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3396 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3397 | slotnames = _PyObject_CallMethodId(copyreg, &PyId__slotnames, "O", cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | Py_DECREF(copyreg); |
| 3399 | if (slotnames != NULL && |
| 3400 | slotnames != Py_None && |
| 3401 | !PyList_Check(slotnames)) |
| 3402 | { |
| 3403 | PyErr_SetString(PyExc_TypeError, |
| 3404 | "copyreg._slotnames didn't return a list or None"); |
| 3405 | Py_DECREF(slotnames); |
| 3406 | slotnames = NULL; |
| 3407 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | static PyObject * |
| 3413 | reduce_2(PyObject *obj) |
| 3414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3415 | PyObject *cls, *getnewargs; |
| 3416 | PyObject *args = NULL, *args2 = NULL; |
| 3417 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3418 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3419 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3420 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3421 | _Py_IDENTIFIER(__getnewargs__); |
| 3422 | _Py_IDENTIFIER(__getstate__); |
| 3423 | _Py_IDENTIFIER(__newobj__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3424 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3425 | cls = (PyObject *) Py_TYPE(obj); |
| 3426 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3427 | getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | if (getnewargs != NULL) { |
| 3429 | args = PyObject_CallObject(getnewargs, NULL); |
| 3430 | Py_DECREF(getnewargs); |
| 3431 | if (args != NULL && !PyTuple_Check(args)) { |
| 3432 | PyErr_Format(PyExc_TypeError, |
| 3433 | "__getnewargs__ should return a tuple, " |
| 3434 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3435 | goto end; |
| 3436 | } |
| 3437 | } |
| 3438 | else { |
| 3439 | PyErr_Clear(); |
| 3440 | args = PyTuple_New(0); |
| 3441 | } |
| 3442 | if (args == NULL) |
| 3443 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3444 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3445 | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | if (getstate != NULL) { |
| 3447 | state = PyObject_CallObject(getstate, NULL); |
| 3448 | Py_DECREF(getstate); |
| 3449 | if (state == NULL) |
| 3450 | goto end; |
| 3451 | } |
| 3452 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3453 | PyObject **dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | PyErr_Clear(); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3455 | dict = _PyObject_GetDictPtr(obj); |
| 3456 | if (dict && *dict) |
| 3457 | state = *dict; |
| 3458 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | state = Py_None; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3460 | Py_INCREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | names = slotnames(cls); |
| 3462 | if (names == NULL) |
| 3463 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3464 | if (names != Py_None && PyList_GET_SIZE(names) > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | assert(PyList_Check(names)); |
| 3466 | slots = PyDict_New(); |
| 3467 | if (slots == NULL) |
| 3468 | goto end; |
| 3469 | n = 0; |
| 3470 | /* Can't pre-compute the list size; the list |
| 3471 | is stored on the class so accessible to other |
| 3472 | threads, which may be run by DECREF */ |
| 3473 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3474 | PyObject *name, *value; |
| 3475 | name = PyList_GET_ITEM(names, i); |
| 3476 | value = PyObject_GetAttr(obj, name); |
| 3477 | if (value == NULL) |
| 3478 | PyErr_Clear(); |
| 3479 | else { |
| 3480 | int err = PyDict_SetItem(slots, name, |
| 3481 | value); |
| 3482 | Py_DECREF(value); |
| 3483 | if (err) |
| 3484 | goto end; |
| 3485 | n++; |
| 3486 | } |
| 3487 | } |
| 3488 | if (n) { |
| 3489 | state = Py_BuildValue("(NO)", state, slots); |
| 3490 | if (state == NULL) |
| 3491 | goto end; |
| 3492 | } |
| 3493 | } |
| 3494 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | if (!PyList_Check(obj)) { |
| 3497 | listitems = Py_None; |
| 3498 | Py_INCREF(listitems); |
| 3499 | } |
| 3500 | else { |
| 3501 | listitems = PyObject_GetIter(obj); |
| 3502 | if (listitems == NULL) |
| 3503 | goto end; |
| 3504 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | if (!PyDict_Check(obj)) { |
| 3507 | dictitems = Py_None; |
| 3508 | Py_INCREF(dictitems); |
| 3509 | } |
| 3510 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3511 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3512 | PyObject *items = _PyObject_CallMethodId(obj, &PyId_items, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | if (items == NULL) |
| 3514 | goto end; |
| 3515 | dictitems = PyObject_GetIter(items); |
| 3516 | Py_DECREF(items); |
| 3517 | if (dictitems == NULL) |
| 3518 | goto end; |
| 3519 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | copyreg = import_copyreg(); |
| 3522 | if (copyreg == NULL) |
| 3523 | goto end; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3524 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3525 | if (newobj == NULL) |
| 3526 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | n = PyTuple_GET_SIZE(args); |
| 3529 | args2 = PyTuple_New(n+1); |
| 3530 | if (args2 == NULL) |
| 3531 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3532 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3534 | for (i = 0; i < n; i++) { |
| 3535 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3536 | Py_INCREF(v); |
| 3537 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3538 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3541 | |
| 3542 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | Py_XDECREF(args); |
| 3544 | Py_XDECREF(args2); |
| 3545 | Py_XDECREF(slots); |
| 3546 | Py_XDECREF(state); |
| 3547 | Py_XDECREF(names); |
| 3548 | Py_XDECREF(listitems); |
| 3549 | Py_XDECREF(dictitems); |
| 3550 | Py_XDECREF(copyreg); |
| 3551 | Py_XDECREF(newobj); |
| 3552 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3555 | /* |
| 3556 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3557 | * were implemented in the same function: |
| 3558 | * - trying to pickle an object with a custom __reduce__ method that |
| 3559 | * fell back to object.__reduce__ in certain circumstances led to |
| 3560 | * infinite recursion at Python level and eventual RuntimeError. |
| 3561 | * - Pickling objects that lied about their type by overwriting the |
| 3562 | * __class__ descriptor could lead to infinite recursion at C level |
| 3563 | * and eventual segfault. |
| 3564 | * |
| 3565 | * Because of backwards compatibility, the two methods still have to |
| 3566 | * behave in the same way, even if this is not required by the pickle |
| 3567 | * protocol. This common functionality was moved to the _common_reduce |
| 3568 | * function. |
| 3569 | */ |
| 3570 | static PyObject * |
| 3571 | _common_reduce(PyObject *self, int proto) |
| 3572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | if (proto >= 2) |
| 3576 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | copyreg = import_copyreg(); |
| 3579 | if (!copyreg) |
| 3580 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3583 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | static PyObject * |
| 3589 | object_reduce(PyObject *self, PyObject *args) |
| 3590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3593 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3594 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3599 | static PyObject * |
| 3600 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3601 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3602 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | PyObject *reduce, *res; |
| 3604 | int proto = 0; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3605 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3607 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3608 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3609 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3610 | if (objreduce == NULL) { |
| 3611 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 3612 | &PyId___reduce__); |
| 3613 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3614 | return NULL; |
| 3615 | } |
| 3616 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3617 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | if (reduce == NULL) |
| 3619 | PyErr_Clear(); |
| 3620 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3621 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3623 | |
| 3624 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3625 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | if (clsreduce == NULL) { |
| 3627 | Py_DECREF(reduce); |
| 3628 | return NULL; |
| 3629 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | override = (clsreduce != objreduce); |
| 3631 | Py_DECREF(clsreduce); |
| 3632 | if (override) { |
| 3633 | res = PyObject_CallObject(reduce, NULL); |
| 3634 | Py_DECREF(reduce); |
| 3635 | return res; |
| 3636 | } |
| 3637 | else |
| 3638 | Py_DECREF(reduce); |
| 3639 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3641 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3644 | static PyObject * |
| 3645 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3646 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3647 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3651 | "Abstract classes can override this to customize issubclass().\n" |
| 3652 | "\n" |
| 3653 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3654 | "It should return True, False or NotImplemented. If it returns\n" |
| 3655 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3656 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3657 | |
| 3658 | /* |
| 3659 | from PEP 3101, this code implements: |
| 3660 | |
| 3661 | class object: |
| 3662 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3663 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3664 | */ |
| 3665 | static PyObject * |
| 3666 | object_format(PyObject *self, PyObject *args) |
| 3667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | PyObject *format_spec; |
| 3669 | PyObject *self_as_str = NULL; |
| 3670 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3673 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3676 | if (self_as_str != NULL) { |
| 3677 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3678 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3679 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 3680 | PyErr_SetString(PyExc_TypeError, |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3681 | "non-empty format string passed to object.__format__"); |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 3682 | goto done; |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3683 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3684 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3685 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3689 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3691 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3694 | static PyObject * |
| 3695 | object_sizeof(PyObject *self, PyObject *args) |
| 3696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3697 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | res = 0; |
| 3700 | isize = self->ob_type->tp_itemsize; |
| 3701 | if (isize > 0) |
| 3702 | res = Py_SIZE(self->ob_type) * isize; |
| 3703 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3705 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3708 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 3709 | and recursively up the __class__.__bases__ chain. |
| 3710 | */ |
| 3711 | static PyObject * |
| 3712 | object_dir(PyObject *self, PyObject *args) |
| 3713 | { |
| 3714 | PyObject *result = NULL; |
| 3715 | PyObject *dict = NULL; |
| 3716 | PyObject *itsclass = NULL; |
| 3717 | |
| 3718 | /* 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] | 3719 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3720 | if (dict == NULL) { |
| 3721 | PyErr_Clear(); |
| 3722 | dict = PyDict_New(); |
| 3723 | } |
| 3724 | else if (!PyDict_Check(dict)) { |
| 3725 | Py_DECREF(dict); |
| 3726 | dict = PyDict_New(); |
| 3727 | } |
| 3728 | else { |
| 3729 | /* Copy __dict__ to avoid mutating it. */ |
| 3730 | PyObject *temp = PyDict_Copy(dict); |
| 3731 | Py_DECREF(dict); |
| 3732 | dict = temp; |
| 3733 | } |
| 3734 | |
| 3735 | if (dict == NULL) |
| 3736 | goto error; |
| 3737 | |
| 3738 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3739 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3740 | if (itsclass == NULL) |
| 3741 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 3742 | __class__ exists? */ |
| 3743 | PyErr_Clear(); |
| 3744 | else if (merge_class_dict(dict, itsclass) != 0) |
| 3745 | goto error; |
| 3746 | |
| 3747 | result = PyDict_Keys(dict); |
| 3748 | /* fall through */ |
| 3749 | error: |
| 3750 | Py_XDECREF(itsclass); |
| 3751 | Py_XDECREF(dict); |
| 3752 | return result; |
| 3753 | } |
| 3754 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3755 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3757 | PyDoc_STR("helper for pickle")}, |
| 3758 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3759 | PyDoc_STR("helper for pickle")}, |
| 3760 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3761 | object_subclasshook_doc}, |
| 3762 | {"__format__", object_format, METH_VARARGS, |
| 3763 | PyDoc_STR("default object formatter")}, |
| 3764 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3765 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3766 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3767 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3769 | }; |
| 3770 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3771 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3772 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3773 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3774 | "object", /* tp_name */ |
| 3775 | sizeof(PyObject), /* tp_basicsize */ |
| 3776 | 0, /* tp_itemsize */ |
| 3777 | object_dealloc, /* tp_dealloc */ |
| 3778 | 0, /* tp_print */ |
| 3779 | 0, /* tp_getattr */ |
| 3780 | 0, /* tp_setattr */ |
| 3781 | 0, /* tp_reserved */ |
| 3782 | object_repr, /* tp_repr */ |
| 3783 | 0, /* tp_as_number */ |
| 3784 | 0, /* tp_as_sequence */ |
| 3785 | 0, /* tp_as_mapping */ |
| 3786 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3787 | 0, /* tp_call */ |
| 3788 | object_str, /* tp_str */ |
| 3789 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3790 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3791 | 0, /* tp_as_buffer */ |
| 3792 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3793 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3794 | 0, /* tp_traverse */ |
| 3795 | 0, /* tp_clear */ |
| 3796 | object_richcompare, /* tp_richcompare */ |
| 3797 | 0, /* tp_weaklistoffset */ |
| 3798 | 0, /* tp_iter */ |
| 3799 | 0, /* tp_iternext */ |
| 3800 | object_methods, /* tp_methods */ |
| 3801 | 0, /* tp_members */ |
| 3802 | object_getsets, /* tp_getset */ |
| 3803 | 0, /* tp_base */ |
| 3804 | 0, /* tp_dict */ |
| 3805 | 0, /* tp_descr_get */ |
| 3806 | 0, /* tp_descr_set */ |
| 3807 | 0, /* tp_dictoffset */ |
| 3808 | object_init, /* tp_init */ |
| 3809 | PyType_GenericAlloc, /* tp_alloc */ |
| 3810 | object_new, /* tp_new */ |
| 3811 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3812 | }; |
| 3813 | |
| 3814 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3815 | /* 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] | 3816 | |
| 3817 | static int |
| 3818 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | for (; meth->ml_name != NULL; meth++) { |
| 3823 | PyObject *descr; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3824 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3826 | !(meth->ml_flags & METH_COEXIST)) |
| 3827 | continue; |
| 3828 | if (meth->ml_flags & METH_CLASS) { |
| 3829 | if (meth->ml_flags & METH_STATIC) { |
| 3830 | PyErr_SetString(PyExc_ValueError, |
| 3831 | "method cannot be both class and static"); |
| 3832 | return -1; |
| 3833 | } |
| 3834 | descr = PyDescr_NewClassMethod(type, meth); |
| 3835 | } |
| 3836 | else if (meth->ml_flags & METH_STATIC) { |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 3837 | PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | if (cfunc == NULL) |
| 3839 | return -1; |
| 3840 | descr = PyStaticMethod_New(cfunc); |
| 3841 | Py_DECREF(cfunc); |
| 3842 | } |
| 3843 | else { |
| 3844 | descr = PyDescr_NewMethod(type, meth); |
| 3845 | } |
| 3846 | if (descr == NULL) |
| 3847 | return -1; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3848 | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3849 | Py_DECREF(descr); |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3850 | if (err < 0) |
| 3851 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3852 | } |
| 3853 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3857 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3859 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | for (; memb->name != NULL; memb++) { |
| 3862 | PyObject *descr; |
| 3863 | if (PyDict_GetItemString(dict, memb->name)) |
| 3864 | continue; |
| 3865 | descr = PyDescr_NewMember(type, memb); |
| 3866 | if (descr == NULL) |
| 3867 | return -1; |
| 3868 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3869 | return -1; |
| 3870 | Py_DECREF(descr); |
| 3871 | } |
| 3872 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3876 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | for (; gsp->name != NULL; gsp++) { |
| 3881 | PyObject *descr; |
| 3882 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3883 | continue; |
| 3884 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | if (descr == NULL) |
| 3887 | return -1; |
| 3888 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3889 | return -1; |
| 3890 | Py_DECREF(descr); |
| 3891 | } |
| 3892 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3895 | static void |
| 3896 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3897 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3901 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3902 | (!type->tp_traverse && !type->tp_clear)) { |
| 3903 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3904 | if (type->tp_traverse == NULL) |
| 3905 | type->tp_traverse = base->tp_traverse; |
| 3906 | if (type->tp_clear == NULL) |
| 3907 | type->tp_clear = base->tp_clear; |
| 3908 | } |
| 3909 | { |
| 3910 | /* The condition below could use some explanation. |
| 3911 | It appears that tp_new is not inherited for static types |
| 3912 | whose base class is 'object'; this seems to be a precaution |
| 3913 | so that old extension types don't suddenly become |
| 3914 | callable (object.__new__ wouldn't insure the invariants |
| 3915 | that the extension type's own factory function ensures). |
| 3916 | Heap types, of course, are under our control, so they do |
| 3917 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3918 | other built-in type as the default also |
| 3919 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | if (base != &PyBaseObject_Type || |
| 3921 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3922 | if (type->tp_new == NULL) |
| 3923 | type->tp_new = base->tp_new; |
| 3924 | } |
| 3925 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3926 | if (type->tp_basicsize == 0) |
| 3927 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3930 | |
| 3931 | #undef COPYVAL |
| 3932 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3933 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | COPYVAL(tp_itemsize); |
| 3936 | COPYVAL(tp_weaklistoffset); |
| 3937 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | /* Setup fast subclass flags */ |
| 3940 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3941 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3942 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3943 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3944 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3945 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3946 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3947 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3948 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3949 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3950 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3951 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3952 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3953 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3954 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3955 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3958 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3959 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3962 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3965 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 3966 | return 1; |
| 3967 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 3968 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3972 | static void |
| 3973 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3976 | |
| 3977 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3978 | #undef COPYSLOT |
| 3979 | #undef COPYNUM |
| 3980 | #undef COPYSEQ |
| 3981 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3982 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3983 | |
| 3984 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3985 | (base->SLOT != 0 && \ |
| 3986 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3987 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3988 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3990 | |
| 3991 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3992 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3993 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3994 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3996 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3997 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3999 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 4000 | basebase = base->tp_base; |
| 4001 | if (basebase->tp_as_number == NULL) |
| 4002 | basebase = NULL; |
| 4003 | COPYNUM(nb_add); |
| 4004 | COPYNUM(nb_subtract); |
| 4005 | COPYNUM(nb_multiply); |
| 4006 | COPYNUM(nb_remainder); |
| 4007 | COPYNUM(nb_divmod); |
| 4008 | COPYNUM(nb_power); |
| 4009 | COPYNUM(nb_negative); |
| 4010 | COPYNUM(nb_positive); |
| 4011 | COPYNUM(nb_absolute); |
| 4012 | COPYNUM(nb_bool); |
| 4013 | COPYNUM(nb_invert); |
| 4014 | COPYNUM(nb_lshift); |
| 4015 | COPYNUM(nb_rshift); |
| 4016 | COPYNUM(nb_and); |
| 4017 | COPYNUM(nb_xor); |
| 4018 | COPYNUM(nb_or); |
| 4019 | COPYNUM(nb_int); |
| 4020 | COPYNUM(nb_float); |
| 4021 | COPYNUM(nb_inplace_add); |
| 4022 | COPYNUM(nb_inplace_subtract); |
| 4023 | COPYNUM(nb_inplace_multiply); |
| 4024 | COPYNUM(nb_inplace_remainder); |
| 4025 | COPYNUM(nb_inplace_power); |
| 4026 | COPYNUM(nb_inplace_lshift); |
| 4027 | COPYNUM(nb_inplace_rshift); |
| 4028 | COPYNUM(nb_inplace_and); |
| 4029 | COPYNUM(nb_inplace_xor); |
| 4030 | COPYNUM(nb_inplace_or); |
| 4031 | COPYNUM(nb_true_divide); |
| 4032 | COPYNUM(nb_floor_divide); |
| 4033 | COPYNUM(nb_inplace_true_divide); |
| 4034 | COPYNUM(nb_inplace_floor_divide); |
| 4035 | COPYNUM(nb_index); |
| 4036 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 4039 | basebase = base->tp_base; |
| 4040 | if (basebase->tp_as_sequence == NULL) |
| 4041 | basebase = NULL; |
| 4042 | COPYSEQ(sq_length); |
| 4043 | COPYSEQ(sq_concat); |
| 4044 | COPYSEQ(sq_repeat); |
| 4045 | COPYSEQ(sq_item); |
| 4046 | COPYSEQ(sq_ass_item); |
| 4047 | COPYSEQ(sq_contains); |
| 4048 | COPYSEQ(sq_inplace_concat); |
| 4049 | COPYSEQ(sq_inplace_repeat); |
| 4050 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 4053 | basebase = base->tp_base; |
| 4054 | if (basebase->tp_as_mapping == NULL) |
| 4055 | basebase = NULL; |
| 4056 | COPYMAP(mp_length); |
| 4057 | COPYMAP(mp_subscript); |
| 4058 | COPYMAP(mp_ass_subscript); |
| 4059 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4061 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 4062 | basebase = base->tp_base; |
| 4063 | if (basebase->tp_as_buffer == NULL) |
| 4064 | basebase = NULL; |
| 4065 | COPYBUF(bf_getbuffer); |
| 4066 | COPYBUF(bf_releasebuffer); |
| 4067 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | COPYSLOT(tp_dealloc); |
| 4072 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 4073 | type->tp_getattr = base->tp_getattr; |
| 4074 | type->tp_getattro = base->tp_getattro; |
| 4075 | } |
| 4076 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 4077 | type->tp_setattr = base->tp_setattr; |
| 4078 | type->tp_setattro = base->tp_setattro; |
| 4079 | } |
| 4080 | /* tp_reserved is ignored */ |
| 4081 | COPYSLOT(tp_repr); |
| 4082 | /* tp_hash see tp_richcompare */ |
| 4083 | COPYSLOT(tp_call); |
| 4084 | COPYSLOT(tp_str); |
| 4085 | { |
| 4086 | /* Copy comparison-related slots only when |
| 4087 | not overriding them anywhere */ |
| 4088 | if (type->tp_richcompare == NULL && |
| 4089 | type->tp_hash == NULL && |
| 4090 | !overrides_hash(type)) |
| 4091 | { |
| 4092 | type->tp_richcompare = base->tp_richcompare; |
| 4093 | type->tp_hash = base->tp_hash; |
| 4094 | } |
| 4095 | } |
| 4096 | { |
| 4097 | COPYSLOT(tp_iter); |
| 4098 | COPYSLOT(tp_iternext); |
| 4099 | } |
| 4100 | { |
| 4101 | COPYSLOT(tp_descr_get); |
| 4102 | COPYSLOT(tp_descr_set); |
| 4103 | COPYSLOT(tp_dictoffset); |
| 4104 | COPYSLOT(tp_init); |
| 4105 | COPYSLOT(tp_alloc); |
| 4106 | COPYSLOT(tp_is_gc); |
| 4107 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 4108 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 4109 | /* They agree about gc. */ |
| 4110 | COPYSLOT(tp_free); |
| 4111 | } |
| 4112 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4113 | type->tp_free == NULL && |
| 4114 | base->tp_free == PyObject_Free) { |
| 4115 | /* A bit of magic to plug in the correct default |
| 4116 | * tp_free function when a derived class adds gc, |
| 4117 | * didn't define tp_free, and the base uses the |
| 4118 | * default non-gc tp_free. |
| 4119 | */ |
| 4120 | type->tp_free = PyObject_GC_Del; |
| 4121 | } |
| 4122 | /* else they didn't agree about gc, and there isn't something |
| 4123 | * obvious to be done -- the type is on its own. |
| 4124 | */ |
| 4125 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4128 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4129 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4130 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 4131 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4133 | PyObject *dict, *bases; |
| 4134 | PyTypeObject *base; |
| 4135 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 4138 | assert(type->tp_dict != NULL); |
| 4139 | return 0; |
| 4140 | } |
| 4141 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4144 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4145 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 | /* PyType_Ready is the closest thing we have to a choke point |
| 4147 | * for type objects, so is the best place I can think of to try |
| 4148 | * to get type objects into the doubly-linked list of all objects. |
| 4149 | * Still, not all type objects go thru PyType_Ready. |
| 4150 | */ |
| 4151 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4152 | #endif |
| 4153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4154 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4155 | base = type->tp_base; |
| 4156 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4157 | base = type->tp_base = &PyBaseObject_Type; |
| 4158 | Py_INCREF(base); |
| 4159 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4161 | /* Now the only way base can still be NULL is if type is |
| 4162 | * &PyBaseObject_Type. |
| 4163 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4165 | /* Initialize the base class */ |
| 4166 | if (base != NULL && base->tp_dict == NULL) { |
| 4167 | if (PyType_Ready(base) < 0) |
| 4168 | goto error; |
| 4169 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4172 | compilable separately on Windows can call PyType_Ready() instead of |
| 4173 | initializing the ob_type field of their type objects. */ |
| 4174 | /* The test for base != NULL is really unnecessary, since base is only |
| 4175 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4176 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4177 | know that. */ |
| 4178 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4179 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | /* Initialize tp_bases */ |
| 4182 | bases = type->tp_bases; |
| 4183 | if (bases == NULL) { |
| 4184 | if (base == NULL) |
| 4185 | bases = PyTuple_New(0); |
| 4186 | else |
| 4187 | bases = PyTuple_Pack(1, base); |
| 4188 | if (bases == NULL) |
| 4189 | goto error; |
| 4190 | type->tp_bases = bases; |
| 4191 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | /* Initialize tp_dict */ |
| 4194 | dict = type->tp_dict; |
| 4195 | if (dict == NULL) { |
| 4196 | dict = PyDict_New(); |
| 4197 | if (dict == NULL) |
| 4198 | goto error; |
| 4199 | type->tp_dict = dict; |
| 4200 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | /* Add type-specific descriptors to tp_dict */ |
| 4203 | if (add_operators(type) < 0) |
| 4204 | goto error; |
| 4205 | if (type->tp_methods != NULL) { |
| 4206 | if (add_methods(type, type->tp_methods) < 0) |
| 4207 | goto error; |
| 4208 | } |
| 4209 | if (type->tp_members != NULL) { |
| 4210 | if (add_members(type, type->tp_members) < 0) |
| 4211 | goto error; |
| 4212 | } |
| 4213 | if (type->tp_getset != NULL) { |
| 4214 | if (add_getset(type, type->tp_getset) < 0) |
| 4215 | goto error; |
| 4216 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4218 | /* Calculate method resolution order */ |
| 4219 | if (mro_internal(type) < 0) { |
| 4220 | goto error; |
| 4221 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | /* Inherit special flags from dominant base */ |
| 4224 | if (type->tp_base != NULL) |
| 4225 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | /* Initialize tp_dict properly */ |
| 4228 | bases = type->tp_mro; |
| 4229 | assert(bases != NULL); |
| 4230 | assert(PyTuple_Check(bases)); |
| 4231 | n = PyTuple_GET_SIZE(bases); |
| 4232 | for (i = 1; i < n; i++) { |
| 4233 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4234 | if (PyType_Check(b)) |
| 4235 | inherit_slots(type, (PyTypeObject *)b); |
| 4236 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | /* Sanity check for tp_free. */ |
| 4239 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4240 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4241 | /* This base class needs to call tp_free, but doesn't have |
| 4242 | * one, or its tp_free is for non-gc'ed objects. |
| 4243 | */ |
| 4244 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4245 | "gc and is a base type but has inappropriate " |
| 4246 | "tp_free slot", |
| 4247 | type->tp_name); |
| 4248 | goto error; |
| 4249 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4252 | the tp_doc slot. |
| 4253 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4254 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | if (type->tp_doc != NULL) { |
| 4256 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 4257 | if (doc == NULL) |
| 4258 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4259 | _PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | Py_DECREF(doc); |
| 4261 | } else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4262 | _PyDict_SetItemId(type->tp_dict, |
| 4263 | &PyId___doc__, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | } |
| 4265 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | /* Hack for tp_hash and __hash__. |
| 4268 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4269 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4270 | tp_dict['__hash__'] equal to None. |
| 4271 | This signals that __hash__ is not inherited. |
| 4272 | */ |
| 4273 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4274 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 4275 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4276 | goto error; |
| 4277 | type->tp_hash = PyObject_HashNotImplemented; |
| 4278 | } |
| 4279 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | /* Some more special stuff */ |
| 4282 | base = type->tp_base; |
| 4283 | if (base != NULL) { |
| 4284 | if (type->tp_as_number == NULL) |
| 4285 | type->tp_as_number = base->tp_as_number; |
| 4286 | if (type->tp_as_sequence == NULL) |
| 4287 | type->tp_as_sequence = base->tp_as_sequence; |
| 4288 | if (type->tp_as_mapping == NULL) |
| 4289 | type->tp_as_mapping = base->tp_as_mapping; |
| 4290 | if (type->tp_as_buffer == NULL) |
| 4291 | type->tp_as_buffer = base->tp_as_buffer; |
| 4292 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4294 | /* Link into each base class's list of subclasses */ |
| 4295 | bases = type->tp_bases; |
| 4296 | n = PyTuple_GET_SIZE(bases); |
| 4297 | for (i = 0; i < n; i++) { |
| 4298 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4299 | if (PyType_Check(b) && |
| 4300 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4301 | goto error; |
| 4302 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | /* Warn for a type that implements tp_compare (now known as |
| 4305 | tp_reserved) but not tp_richcompare. */ |
| 4306 | if (type->tp_reserved && !type->tp_richcompare) { |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4307 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4308 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4309 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4310 | type->tp_name); |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4311 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4312 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | /* All done -- set the ready flag */ |
| 4315 | assert(type->tp_dict != NULL); |
| 4316 | type->tp_flags = |
| 4317 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4318 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4319 | |
| 4320 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4322 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4325 | static int |
| 4326 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | Py_ssize_t i; |
| 4329 | int result; |
| 4330 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | list = base->tp_subclasses; |
| 4333 | if (list == NULL) { |
| 4334 | base->tp_subclasses = list = PyList_New(0); |
| 4335 | if (list == NULL) |
| 4336 | return -1; |
| 4337 | } |
| 4338 | assert(PyList_Check(list)); |
| 4339 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Victor Stinner | 9812af8 | 2013-07-08 22:25:48 +0200 | [diff] [blame] | 4340 | if (newobj == NULL) |
| 4341 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | i = PyList_GET_SIZE(list); |
| 4343 | while (--i >= 0) { |
| 4344 | ref = PyList_GET_ITEM(list, i); |
| 4345 | assert(PyWeakref_CheckRef(ref)); |
| 4346 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 4347 | return PyList_SetItem(list, i, newobj); |
| 4348 | } |
| 4349 | result = PyList_Append(list, newobj); |
| 4350 | Py_DECREF(newobj); |
| 4351 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4354 | static void |
| 4355 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | Py_ssize_t i; |
| 4358 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4360 | list = base->tp_subclasses; |
| 4361 | if (list == NULL) { |
| 4362 | return; |
| 4363 | } |
| 4364 | assert(PyList_Check(list)); |
| 4365 | i = PyList_GET_SIZE(list); |
| 4366 | while (--i >= 0) { |
| 4367 | ref = PyList_GET_ITEM(list, i); |
| 4368 | assert(PyWeakref_CheckRef(ref)); |
| 4369 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4370 | /* this can't fail, right? */ |
| 4371 | PySequence_DelItem(list, i); |
| 4372 | return; |
| 4373 | } |
| 4374 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4375 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4376 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4377 | static int |
| 4378 | check_num_args(PyObject *ob, int n) |
| 4379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | if (!PyTuple_CheckExact(ob)) { |
| 4381 | PyErr_SetString(PyExc_SystemError, |
| 4382 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4383 | return 0; |
| 4384 | } |
| 4385 | if (n == PyTuple_GET_SIZE(ob)) |
| 4386 | return 1; |
| 4387 | PyErr_Format( |
| 4388 | PyExc_TypeError, |
| 4389 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4390 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4393 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4394 | |
| 4395 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4397 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4398 | Most tables have only one entry; the tables for binary operators have two |
| 4399 | entries, one regular and one with reversed arguments. */ |
| 4400 | |
| 4401 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4402 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | lenfunc func = (lenfunc)wrapped; |
| 4405 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | if (!check_num_args(args, 0)) |
| 4408 | return NULL; |
| 4409 | res = (*func)(self); |
| 4410 | if (res == -1 && PyErr_Occurred()) |
| 4411 | return NULL; |
| 4412 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4415 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4416 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | inquiry func = (inquiry)wrapped; |
| 4419 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | if (!check_num_args(args, 0)) |
| 4422 | return NULL; |
| 4423 | res = (*func)(self); |
| 4424 | if (res == -1 && PyErr_Occurred()) |
| 4425 | return NULL; |
| 4426 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4430 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | binaryfunc func = (binaryfunc)wrapped; |
| 4433 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | if (!check_num_args(args, 1)) |
| 4436 | return NULL; |
| 4437 | other = PyTuple_GET_ITEM(args, 0); |
| 4438 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4442 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | binaryfunc func = (binaryfunc)wrapped; |
| 4445 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | if (!check_num_args(args, 1)) |
| 4448 | return NULL; |
| 4449 | other = PyTuple_GET_ITEM(args, 0); |
| 4450 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4454 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | binaryfunc func = (binaryfunc)wrapped; |
| 4457 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | if (!check_num_args(args, 1)) |
| 4460 | return NULL; |
| 4461 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4465 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4466 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4469 | PyObject *other; |
| 4470 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | /* Note: This wrapper only works for __pow__() */ |
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, "", 1, 2, &other, &third)) |
| 4475 | return NULL; |
| 4476 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4479 | static PyObject * |
| 4480 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4483 | PyObject *other; |
| 4484 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4488 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4489 | return NULL; |
| 4490 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4493 | static PyObject * |
| 4494 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | if (!check_num_args(args, 0)) |
| 4499 | return NULL; |
| 4500 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4503 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4504 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4507 | PyObject* o; |
| 4508 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4511 | return NULL; |
| 4512 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4513 | if (i == -1 && PyErr_Occurred()) |
| 4514 | return NULL; |
| 4515 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4518 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4519 | getindex(PyObject *self, PyObject *arg) |
| 4520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4524 | if (i == -1 && PyErr_Occurred()) |
| 4525 | return -1; |
| 4526 | if (i < 0) { |
| 4527 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4528 | if (sq && sq->sq_length) { |
| 4529 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4530 | if (n < 0) |
| 4531 | return -1; |
| 4532 | i += n; |
| 4533 | } |
| 4534 | } |
| 4535 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | static PyObject * |
| 4539 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4542 | PyObject *arg; |
| 4543 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4546 | arg = PyTuple_GET_ITEM(args, 0); |
| 4547 | i = getindex(self, arg); |
| 4548 | if (i == -1 && PyErr_Occurred()) |
| 4549 | return NULL; |
| 4550 | return (*func)(self, i); |
| 4551 | } |
| 4552 | check_num_args(args, 1); |
| 4553 | assert(PyErr_Occurred()); |
| 4554 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4557 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4558 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4561 | Py_ssize_t i; |
| 4562 | int res; |
| 4563 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4565 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4566 | return NULL; |
| 4567 | i = getindex(self, arg); |
| 4568 | if (i == -1 && PyErr_Occurred()) |
| 4569 | return NULL; |
| 4570 | res = (*func)(self, i, value); |
| 4571 | if (res == -1 && PyErr_Occurred()) |
| 4572 | return NULL; |
| 4573 | Py_INCREF(Py_None); |
| 4574 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4577 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4578 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4581 | Py_ssize_t i; |
| 4582 | int res; |
| 4583 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +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 | arg = PyTuple_GET_ITEM(args, 0); |
| 4588 | i = getindex(self, arg); |
| 4589 | if (i == -1 && PyErr_Occurred()) |
| 4590 | return NULL; |
| 4591 | res = (*func)(self, i, NULL); |
| 4592 | if (res == -1 && PyErr_Occurred()) |
| 4593 | return NULL; |
| 4594 | Py_INCREF(Py_None); |
| 4595 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4598 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4599 | static PyObject * |
| 4600 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | objobjproc func = (objobjproc)wrapped; |
| 4603 | int res; |
| 4604 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | if (!check_num_args(args, 1)) |
| 4607 | return NULL; |
| 4608 | value = PyTuple_GET_ITEM(args, 0); |
| 4609 | res = (*func)(self, value); |
| 4610 | if (res == -1 && PyErr_Occurred()) |
| 4611 | return NULL; |
| 4612 | else |
| 4613 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4616 | static PyObject * |
| 4617 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | objobjargproc func = (objobjargproc)wrapped; |
| 4620 | int res; |
| 4621 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4623 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4624 | return NULL; |
| 4625 | res = (*func)(self, key, value); |
| 4626 | if (res == -1 && PyErr_Occurred()) |
| 4627 | return NULL; |
| 4628 | Py_INCREF(Py_None); |
| 4629 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4632 | static PyObject * |
| 4633 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | objobjargproc func = (objobjargproc)wrapped; |
| 4636 | int res; |
| 4637 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | if (!check_num_args(args, 1)) |
| 4640 | return NULL; |
| 4641 | key = PyTuple_GET_ITEM(args, 0); |
| 4642 | res = (*func)(self, key, NULL); |
| 4643 | if (res == -1 && PyErr_Occurred()) |
| 4644 | return NULL; |
| 4645 | Py_INCREF(Py_None); |
| 4646 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4649 | /* 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] | 4650 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4651 | static int |
| 4652 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | PyTypeObject *type = Py_TYPE(self); |
| 4655 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4656 | type = type->tp_base; |
| 4657 | /* If type is NULL now, this is a really weird type. |
| 4658 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4659 | if (type && type->tp_setattro != func) { |
| 4660 | PyErr_Format(PyExc_TypeError, |
| 4661 | "can't apply this %s to %s object", |
| 4662 | what, |
| 4663 | type->tp_name); |
| 4664 | return 0; |
| 4665 | } |
| 4666 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4669 | static PyObject * |
| 4670 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | setattrofunc func = (setattrofunc)wrapped; |
| 4673 | int res; |
| 4674 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4677 | return NULL; |
| 4678 | if (!hackcheck(self, func, "__setattr__")) |
| 4679 | return NULL; |
| 4680 | res = (*func)(self, name, value); |
| 4681 | if (res < 0) |
| 4682 | return NULL; |
| 4683 | Py_INCREF(Py_None); |
| 4684 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | static PyObject * |
| 4688 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | setattrofunc func = (setattrofunc)wrapped; |
| 4691 | int res; |
| 4692 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | if (!check_num_args(args, 1)) |
| 4695 | return NULL; |
| 4696 | name = PyTuple_GET_ITEM(args, 0); |
| 4697 | if (!hackcheck(self, func, "__delattr__")) |
| 4698 | return NULL; |
| 4699 | res = (*func)(self, name, NULL); |
| 4700 | if (res < 0) |
| 4701 | return NULL; |
| 4702 | Py_INCREF(Py_None); |
| 4703 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4706 | static PyObject * |
| 4707 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4710 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | if (!check_num_args(args, 0)) |
| 4713 | return NULL; |
| 4714 | res = (*func)(self); |
| 4715 | if (res == -1 && PyErr_Occurred()) |
| 4716 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4717 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4720 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4721 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4723 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4725 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4728 | static PyObject * |
| 4729 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4732 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 | if (!check_num_args(args, 1)) |
| 4735 | return NULL; |
| 4736 | other = PyTuple_GET_ITEM(args, 0); |
| 4737 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | #undef RICHCMP_WRAPPER |
| 4741 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4742 | static PyObject * \ |
| 4743 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4744 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4748 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4749 | RICHCMP_WRAPPER(le, Py_LE) |
| 4750 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4751 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4752 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4753 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4754 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4755 | static PyObject * |
| 4756 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4758 | unaryfunc func = (unaryfunc)wrapped; |
| 4759 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | if (!check_num_args(args, 0)) |
| 4762 | return NULL; |
| 4763 | res = (*func)(self); |
| 4764 | if (res == NULL && !PyErr_Occurred()) |
| 4765 | PyErr_SetNone(PyExc_StopIteration); |
| 4766 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4769 | static PyObject * |
| 4770 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4773 | PyObject *obj; |
| 4774 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4777 | return NULL; |
| 4778 | if (obj == Py_None) |
| 4779 | obj = NULL; |
| 4780 | if (type == Py_None) |
| 4781 | type = NULL; |
| 4782 | if (type == NULL &&obj == NULL) { |
| 4783 | PyErr_SetString(PyExc_TypeError, |
| 4784 | "__get__(None, None) is invalid"); |
| 4785 | return NULL; |
| 4786 | } |
| 4787 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4790 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4791 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4794 | PyObject *obj, *value; |
| 4795 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4798 | return NULL; |
| 4799 | ret = (*func)(self, obj, value); |
| 4800 | if (ret < 0) |
| 4801 | return NULL; |
| 4802 | Py_INCREF(Py_None); |
| 4803 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4804 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4805 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4806 | static PyObject * |
| 4807 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4810 | PyObject *obj; |
| 4811 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | if (!check_num_args(args, 1)) |
| 4814 | return NULL; |
| 4815 | obj = PyTuple_GET_ITEM(args, 0); |
| 4816 | ret = (*func)(self, obj, NULL); |
| 4817 | if (ret < 0) |
| 4818 | return NULL; |
| 4819 | Py_INCREF(Py_None); |
| 4820 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4821 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4822 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4823 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4824 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4826 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | if (func(self, args, kwds) < 0) |
| 4829 | return NULL; |
| 4830 | Py_INCREF(Py_None); |
| 4831 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4834 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4835 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4837 | PyTypeObject *type, *subtype, *staticbase; |
| 4838 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | if (self == NULL || !PyType_Check(self)) |
| 4841 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4842 | type = (PyTypeObject *)self; |
| 4843 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4844 | PyErr_Format(PyExc_TypeError, |
| 4845 | "%s.__new__(): not enough arguments", |
| 4846 | type->tp_name); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4850 | if (!PyType_Check(arg0)) { |
| 4851 | PyErr_Format(PyExc_TypeError, |
| 4852 | "%s.__new__(X): X is not a type object (%s)", |
| 4853 | type->tp_name, |
| 4854 | Py_TYPE(arg0)->tp_name); |
| 4855 | return NULL; |
| 4856 | } |
| 4857 | subtype = (PyTypeObject *)arg0; |
| 4858 | if (!PyType_IsSubtype(subtype, type)) { |
| 4859 | PyErr_Format(PyExc_TypeError, |
| 4860 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4861 | type->tp_name, |
| 4862 | subtype->tp_name, |
| 4863 | subtype->tp_name, |
| 4864 | type->tp_name); |
| 4865 | return NULL; |
| 4866 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | /* Check that the use doesn't do something silly and unsafe like |
| 4869 | object.__new__(dict). To do this, we check that the |
| 4870 | most derived base that's not a heap type is this type. */ |
| 4871 | staticbase = subtype; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 4872 | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4873 | staticbase = staticbase->tp_base; |
| 4874 | /* If staticbase is NULL now, it is a really weird type. |
| 4875 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4876 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4877 | PyErr_Format(PyExc_TypeError, |
| 4878 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4879 | type->tp_name, |
| 4880 | subtype->tp_name, |
| 4881 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4882 | return NULL; |
| 4883 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4886 | if (args == NULL) |
| 4887 | return NULL; |
| 4888 | res = type->tp_new(subtype, args, kwds); |
| 4889 | Py_DECREF(args); |
| 4890 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4893 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4895 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4896 | "a new object with type S, a subtype of T")}, |
| 4897 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4898 | }; |
| 4899 | |
| 4900 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4901 | add_tp_new_wrapper(PyTypeObject *type) |
| 4902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4904 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4905 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | return 0; |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 4907 | func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | if (func == NULL) |
| 4909 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4910 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | Py_DECREF(func); |
| 4912 | return -1; |
| 4913 | } |
| 4914 | Py_DECREF(func); |
| 4915 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4918 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4919 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4920 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4921 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4922 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4923 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4924 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4925 | _Py_static_string(id, OPSTR); \ |
| 4926 | return call_method(self, &id, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4929 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4930 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4931 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4932 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4933 | _Py_static_string(id, OPSTR); \ |
| 4934 | return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4937 | /* Boolean helper for SLOT1BINFULL(). |
| 4938 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4939 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4940 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4942 | PyObject *a, *b; |
| 4943 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4944 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4945 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | if (b == NULL) { |
| 4947 | PyErr_Clear(); |
| 4948 | /* If right doesn't have it, it's not overloaded */ |
| 4949 | return 0; |
| 4950 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4951 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4952 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4953 | if (a == NULL) { |
| 4954 | PyErr_Clear(); |
| 4955 | Py_DECREF(b); |
| 4956 | /* If right has it but left doesn't, it's overloaded */ |
| 4957 | return 1; |
| 4958 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4961 | Py_DECREF(a); |
| 4962 | Py_DECREF(b); |
| 4963 | if (ok < 0) { |
| 4964 | PyErr_Clear(); |
| 4965 | return 0; |
| 4966 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4971 | |
| 4972 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4973 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4974 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4975 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4976 | _Py_static_string(op_id, OPSTR); \ |
| 4977 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4978 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4979 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4980 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4981 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4982 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4983 | PyObject *r; \ |
| 4984 | if (do_other && \ |
| 4985 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4986 | method_is_overloaded(self, other, &rop_id)) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4987 | r = call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | if (r != Py_NotImplemented) \ |
| 4989 | return r; \ |
| 4990 | Py_DECREF(r); \ |
| 4991 | do_other = 0; \ |
| 4992 | } \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4993 | r = call_maybe(self, &op_id, "(O)", other); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | if (r != Py_NotImplemented || \ |
| 4995 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4996 | return r; \ |
| 4997 | Py_DECREF(r); \ |
| 4998 | } \ |
| 4999 | if (do_other) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5000 | return call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5002 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5007 | |
| 5008 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 5009 | static PyObject * \ |
| 5010 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 5011 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5012 | _Py_static_string(id, #OPSTR); \ |
| 5013 | return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5016 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5017 | slot_sq_length(PyObject *self) |
| 5018 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5019 | _Py_IDENTIFIER(__len__); |
| 5020 | PyObject *res = call_method(self, &PyId___len__, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | if (res == NULL) |
| 5024 | return -1; |
| 5025 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 5026 | Py_DECREF(res); |
| 5027 | if (len < 0) { |
| 5028 | if (!PyErr_Occurred()) |
| 5029 | PyErr_SetString(PyExc_ValueError, |
| 5030 | "__len__() should return >= 0"); |
| 5031 | return -1; |
| 5032 | } |
| 5033 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5036 | /* Super-optimized version of slot_sq_item. |
| 5037 | Other slots could do the same... */ |
| 5038 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5039 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 5042 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5043 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5044 | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | if (func != NULL) { |
| 5046 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 5047 | Py_INCREF(func); |
| 5048 | else { |
| 5049 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 5050 | if (func == NULL) { |
| 5051 | return NULL; |
| 5052 | } |
| 5053 | } |
| 5054 | ival = PyLong_FromSsize_t(i); |
| 5055 | if (ival != NULL) { |
| 5056 | args = PyTuple_New(1); |
| 5057 | if (args != NULL) { |
| 5058 | PyTuple_SET_ITEM(args, 0, ival); |
| 5059 | retval = PyObject_Call(func, args, NULL); |
| 5060 | Py_XDECREF(args); |
| 5061 | Py_XDECREF(func); |
| 5062 | return retval; |
| 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5067 | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 5069 | } |
| 5070 | Py_XDECREF(args); |
| 5071 | Py_XDECREF(ival); |
| 5072 | Py_XDECREF(func); |
| 5073 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5076 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5077 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5080 | _Py_IDENTIFIER(__delitem__); |
| 5081 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5084 | res = call_method(self, &PyId___delitem__, "(n)", index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5086 | res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | if (res == NULL) |
| 5088 | return -1; |
| 5089 | Py_DECREF(res); |
| 5090 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5094 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | PyObject *func, *res, *args; |
| 5097 | int result = -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5098 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5099 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5100 | func = lookup_maybe(self, &PyId___contains__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | if (func != NULL) { |
| 5102 | args = PyTuple_Pack(1, value); |
| 5103 | if (args == NULL) |
| 5104 | res = NULL; |
| 5105 | else { |
| 5106 | res = PyObject_Call(func, args, NULL); |
| 5107 | Py_DECREF(args); |
| 5108 | } |
| 5109 | Py_DECREF(func); |
| 5110 | if (res != NULL) { |
| 5111 | result = PyObject_IsTrue(res); |
| 5112 | Py_DECREF(res); |
| 5113 | } |
| 5114 | } |
| 5115 | else if (! PyErr_Occurred()) { |
| 5116 | /* Possible results: -1 and 1 */ |
| 5117 | result = (int)_PySequence_IterSearch(self, value, |
| 5118 | PY_ITERSEARCH_CONTAINS); |
| 5119 | } |
| 5120 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5123 | #define slot_mp_length slot_sq_length |
| 5124 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5125 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5126 | |
| 5127 | static int |
| 5128 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5130 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5131 | _Py_IDENTIFIER(__delitem__); |
| 5132 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5135 | res = call_method(self, &PyId___delitem__, "(O)", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5136 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5137 | res = call_method(self, &PyId___setitem__, "(OO)", key, value); |
| 5138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | if (res == NULL) |
| 5140 | return -1; |
| 5141 | Py_DECREF(res); |
| 5142 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5145 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5146 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5147 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5148 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5149 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5150 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5151 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5152 | |
| 5153 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5154 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5155 | |
| 5156 | static PyObject * |
| 5157 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5158 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5159 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5161 | if (modulus == Py_None) |
| 5162 | return slot_nb_power_binary(self, other); |
| 5163 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5164 | can call this when the second argument's type uses |
| 5165 | slot_nb_power, so check before calling self.__pow__. */ |
| 5166 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5167 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5168 | return call_method(self, &PyId___pow__, "(OO)", other, modulus); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5170 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | SLOT0(slot_nb_negative, "__neg__") |
| 5174 | SLOT0(slot_nb_positive, "__pos__") |
| 5175 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5176 | |
| 5177 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5178 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | PyObject *func, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | int result = -1; |
| 5182 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5183 | _Py_IDENTIFIER(__len__); |
| 5184 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5185 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5186 | func = lookup_maybe(self, &PyId___bool__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | if (func == NULL) { |
| 5188 | if (PyErr_Occurred()) |
| 5189 | return -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5190 | func = lookup_maybe(self, &PyId___len__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | if (func == NULL) |
| 5192 | return PyErr_Occurred() ? -1 : 1; |
| 5193 | using_len = 1; |
| 5194 | } |
| 5195 | args = PyTuple_New(0); |
| 5196 | if (args != NULL) { |
| 5197 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5198 | Py_DECREF(args); |
| 5199 | if (temp != NULL) { |
| 5200 | if (using_len) { |
| 5201 | /* enforced by slot_nb_len */ |
| 5202 | result = PyObject_IsTrue(temp); |
| 5203 | } |
| 5204 | else if (PyBool_Check(temp)) { |
| 5205 | result = PyObject_IsTrue(temp); |
| 5206 | } |
| 5207 | else { |
| 5208 | PyErr_Format(PyExc_TypeError, |
| 5209 | "__bool__ should return " |
| 5210 | "bool, returned %s", |
| 5211 | Py_TYPE(temp)->tp_name); |
| 5212 | result = -1; |
| 5213 | } |
| 5214 | Py_DECREF(temp); |
| 5215 | } |
| 5216 | } |
| 5217 | Py_DECREF(func); |
| 5218 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5221 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5222 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5223 | slot_nb_index(PyObject *self) |
| 5224 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5225 | _Py_IDENTIFIER(__index__); |
| 5226 | return call_method(self, &PyId___index__, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5230 | SLOT0(slot_nb_invert, "__invert__") |
| 5231 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5232 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5233 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5234 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5235 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5236 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5237 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5238 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5239 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5240 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5241 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5242 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5243 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5244 | static PyObject * |
| 5245 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5246 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5247 | _Py_IDENTIFIER(__ipow__); |
| 5248 | return call_method(self, &PyId___ipow__, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5249 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5250 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5251 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5252 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5253 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5254 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5255 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5256 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5257 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5258 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5259 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5260 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5261 | static PyObject * |
| 5262 | slot_tp_repr(PyObject *self) |
| 5263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5265 | _Py_IDENTIFIER(__repr__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5266 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5267 | func = lookup_method(self, &PyId___repr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | if (func != NULL) { |
| 5269 | res = PyEval_CallObject(func, NULL); |
| 5270 | Py_DECREF(func); |
| 5271 | return res; |
| 5272 | } |
| 5273 | PyErr_Clear(); |
| 5274 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5275 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | static PyObject * |
| 5279 | slot_tp_str(PyObject *self) |
| 5280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5282 | _Py_IDENTIFIER(__str__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5283 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5284 | func = lookup_method(self, &PyId___str__); |
Victor Stinner | 2e8474d | 2013-07-11 22:46:11 +0200 | [diff] [blame] | 5285 | if (func == NULL) |
| 5286 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | res = PyEval_CallObject(func, NULL); |
| 5288 | Py_DECREF(func); |
| 5289 | return res; |
| 5290 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5291 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5292 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5293 | slot_tp_hash(PyObject *self) |
| 5294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5295 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5296 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5297 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5298 | func = lookup_method(self, &PyId___hash__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | if (func == Py_None) { |
| 5301 | Py_DECREF(func); |
| 5302 | func = NULL; |
| 5303 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | if (func == NULL) { |
| 5306 | return PyObject_HashNotImplemented(self); |
| 5307 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5309 | res = PyEval_CallObject(func, NULL); |
| 5310 | Py_DECREF(func); |
| 5311 | if (res == NULL) |
| 5312 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5313 | |
| 5314 | if (!PyLong_Check(res)) { |
| 5315 | PyErr_SetString(PyExc_TypeError, |
| 5316 | "__hash__ method should return an integer"); |
| 5317 | return -1; |
| 5318 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5319 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5320 | hashable Python object x, hash(x) will always lie within the range of |
| 5321 | Py_hash_t. Therefore our transformation must preserve values that |
| 5322 | already lie within this range, to ensure that if x.__hash__() returns |
| 5323 | hash(y) then hash(x) == hash(y). */ |
| 5324 | h = PyLong_AsSsize_t(res); |
| 5325 | if (h == -1 && PyErr_Occurred()) { |
| 5326 | /* 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] | 5327 | use any sufficiently bit-mixing transformation; |
| 5328 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5329 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5331 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5332 | /* -1 is reserved for errors. */ |
| 5333 | if (h == -1) |
| 5334 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5336 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | static PyObject * |
| 5340 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5341 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5342 | _Py_IDENTIFIER(__call__); |
| 5343 | PyObject *meth = lookup_method(self, &PyId___call__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | if (meth == NULL) |
| 5347 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5349 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5351 | Py_DECREF(meth); |
| 5352 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5355 | /* There are two slot dispatch functions for tp_getattro. |
| 5356 | |
| 5357 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5358 | but no __getattr__ hook is present; |
| 5359 | |
| 5360 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5361 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5362 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5363 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5364 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5365 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5366 | static PyObject * |
| 5367 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5368 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5369 | return call_method(self, &PyId___getattribute__, "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5372 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5373 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | PyObject *res, *descr = NULL; |
| 5376 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | if (f != NULL) { |
| 5379 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5380 | if (descr == NULL) |
| 5381 | return NULL; |
| 5382 | else |
| 5383 | attr = descr; |
| 5384 | } |
| 5385 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5386 | Py_XDECREF(descr); |
| 5387 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5391 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | PyTypeObject *tp = Py_TYPE(self); |
| 5394 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5395 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5398 | method fully for each attribute lookup for classes with |
| 5399 | __getattr__, even when the attribute is present. So we use |
| 5400 | _PyType_Lookup and create the method only when needed, with |
| 5401 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5402 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | if (getattr == NULL) { |
| 5404 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5405 | tp->tp_getattro = slot_tp_getattro; |
| 5406 | return slot_tp_getattro(self, name); |
| 5407 | } |
| 5408 | Py_INCREF(getattr); |
| 5409 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5410 | method fully for each attribute lookup for classes with |
| 5411 | __getattr__, even when self has the default __getattribute__ |
| 5412 | method. So we use _PyType_Lookup and create the method only when |
| 5413 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5414 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | if (getattribute == NULL || |
| 5416 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5417 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5418 | (void *)PyObject_GenericGetAttr)) |
| 5419 | res = PyObject_GenericGetAttr(self, name); |
| 5420 | else { |
| 5421 | Py_INCREF(getattribute); |
| 5422 | res = call_attribute(self, getattribute, name); |
| 5423 | Py_DECREF(getattribute); |
| 5424 | } |
| 5425 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5426 | PyErr_Clear(); |
| 5427 | res = call_attribute(self, getattr, name); |
| 5428 | } |
| 5429 | Py_DECREF(getattr); |
| 5430 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5433 | static int |
| 5434 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5437 | _Py_IDENTIFIER(__delattr__); |
| 5438 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5441 | res = call_method(self, &PyId___delattr__, "(O)", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5443 | res = call_method(self, &PyId___setattr__, "(OO)", name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | if (res == NULL) |
| 5445 | return -1; |
| 5446 | Py_DECREF(res); |
| 5447 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5450 | static _Py_Identifier name_op[] = { |
| 5451 | {0, "__lt__", 0}, |
| 5452 | {0, "__le__", 0}, |
| 5453 | {0, "__eq__", 0}, |
| 5454 | {0, "__ne__", 0}, |
| 5455 | {0, "__gt__", 0}, |
| 5456 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5457 | }; |
| 5458 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5459 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5460 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | PyObject *func, *args, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5463 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5464 | func = lookup_method(self, &name_op[op]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | if (func == NULL) { |
| 5466 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5467 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5468 | } |
| 5469 | args = PyTuple_Pack(1, other); |
| 5470 | if (args == NULL) |
| 5471 | res = NULL; |
| 5472 | else { |
| 5473 | res = PyObject_Call(func, args, NULL); |
| 5474 | Py_DECREF(args); |
| 5475 | } |
| 5476 | Py_DECREF(func); |
| 5477 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5480 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5481 | slot_tp_iter(PyObject *self) |
| 5482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5483 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5484 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5485 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5486 | func = lookup_method(self, &PyId___iter__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | if (func != NULL) { |
| 5488 | PyObject *args; |
| 5489 | args = res = PyTuple_New(0); |
| 5490 | if (args != NULL) { |
| 5491 | res = PyObject_Call(func, args, NULL); |
| 5492 | Py_DECREF(args); |
| 5493 | } |
| 5494 | Py_DECREF(func); |
| 5495 | return res; |
| 5496 | } |
| 5497 | PyErr_Clear(); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5498 | func = lookup_method(self, &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5499 | if (func == NULL) { |
| 5500 | PyErr_Format(PyExc_TypeError, |
| 5501 | "'%.200s' object is not iterable", |
| 5502 | Py_TYPE(self)->tp_name); |
| 5503 | return NULL; |
| 5504 | } |
| 5505 | Py_DECREF(func); |
| 5506 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5507 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5508 | |
| 5509 | static PyObject * |
| 5510 | slot_tp_iternext(PyObject *self) |
| 5511 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5512 | _Py_IDENTIFIER(__next__); |
| 5513 | return call_method(self, &PyId___next__, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5516 | static PyObject * |
| 5517 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | PyTypeObject *tp = Py_TYPE(self); |
| 5520 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5521 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5522 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5523 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | if (get == NULL) { |
| 5525 | /* Avoid further slowdowns */ |
| 5526 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5527 | tp->tp_descr_get = NULL; |
| 5528 | Py_INCREF(self); |
| 5529 | return self; |
| 5530 | } |
| 5531 | if (obj == NULL) |
| 5532 | obj = Py_None; |
| 5533 | if (type == NULL) |
| 5534 | type = Py_None; |
| 5535 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5536 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5537 | |
| 5538 | static int |
| 5539 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5542 | _Py_IDENTIFIER(__delete__); |
| 5543 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5546 | res = call_method(self, &PyId___delete__, "(O)", target); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5548 | res = call_method(self, &PyId___set__, "(OO)", target, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | if (res == NULL) |
| 5550 | return -1; |
| 5551 | Py_DECREF(res); |
| 5552 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
| 5555 | static int |
| 5556 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5557 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5558 | _Py_IDENTIFIER(__init__); |
| 5559 | PyObject *meth = lookup_method(self, &PyId___init__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | if (meth == NULL) |
| 5563 | return -1; |
| 5564 | res = PyObject_Call(meth, args, kwds); |
| 5565 | Py_DECREF(meth); |
| 5566 | if (res == NULL) |
| 5567 | return -1; |
| 5568 | if (res != Py_None) { |
| 5569 | PyErr_Format(PyExc_TypeError, |
| 5570 | "__init__() should return None, not '%.200s'", |
| 5571 | Py_TYPE(res)->tp_name); |
| 5572 | Py_DECREF(res); |
| 5573 | return -1; |
| 5574 | } |
| 5575 | Py_DECREF(res); |
| 5576 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | static PyObject * |
| 5580 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | PyObject *func; |
| 5583 | PyObject *newargs, *x; |
| 5584 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5585 | _Py_IDENTIFIER(__new__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5586 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5587 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | if (func == NULL) |
| 5589 | return NULL; |
| 5590 | assert(PyTuple_Check(args)); |
| 5591 | n = PyTuple_GET_SIZE(args); |
| 5592 | newargs = PyTuple_New(n+1); |
| 5593 | if (newargs == NULL) |
| 5594 | return NULL; |
| 5595 | Py_INCREF(type); |
| 5596 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5597 | for (i = 0; i < n; i++) { |
| 5598 | x = PyTuple_GET_ITEM(args, i); |
| 5599 | Py_INCREF(x); |
| 5600 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5601 | } |
| 5602 | x = PyObject_Call(func, newargs, kwds); |
| 5603 | Py_DECREF(newargs); |
| 5604 | Py_DECREF(func); |
| 5605 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5608 | static void |
| 5609 | slot_tp_del(PyObject *self) |
| 5610 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5611 | _Py_IDENTIFIER(__del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | PyObject *del, *res; |
| 5613 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | /* Temporarily resurrect the object. */ |
| 5616 | assert(self->ob_refcnt == 0); |
| 5617 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5619 | /* Save the current exception, if any. */ |
| 5620 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5622 | /* Execute __del__ method, if any. */ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5623 | del = lookup_maybe(self, &PyId___del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5624 | if (del != NULL) { |
| 5625 | res = PyEval_CallObject(del, NULL); |
| 5626 | if (res == NULL) |
| 5627 | PyErr_WriteUnraisable(del); |
| 5628 | else |
| 5629 | Py_DECREF(res); |
| 5630 | Py_DECREF(del); |
| 5631 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | /* Restore the saved exception. */ |
| 5634 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5636 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5637 | * cause a recursive call. |
| 5638 | */ |
| 5639 | assert(self->ob_refcnt > 0); |
| 5640 | if (--self->ob_refcnt == 0) |
| 5641 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5643 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5644 | * never happened. |
| 5645 | */ |
| 5646 | { |
| 5647 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5648 | _Py_NewReference(self); |
| 5649 | self->ob_refcnt = refcnt; |
| 5650 | } |
| 5651 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5652 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5653 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5654 | * we need to undo that. */ |
| 5655 | _Py_DEC_REFTOTAL; |
| 5656 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5657 | * chain, so no more to do there. |
| 5658 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5659 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5660 | * undone. |
| 5661 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5662 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | --Py_TYPE(self)->tp_frees; |
| 5664 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5665 | #endif |
| 5666 | } |
| 5667 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5668 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5669 | /* |
| 5670 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
| 5671 | |
| 5672 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
| 5673 | which incorporates the additional structures used for numbers, sequences and |
| 5674 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 5675 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
| 5676 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
| 5677 | an all-zero entry. (This table is further initialized in init_slotdefs().) |
| 5678 | */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5679 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5680 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5681 | |
| 5682 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5683 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5684 | #undef ETSLOT |
| 5685 | #undef SQSLOT |
| 5686 | #undef MPSLOT |
| 5687 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5688 | #undef UNSLOT |
| 5689 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5690 | #undef BINSLOT |
| 5691 | #undef RBINSLOT |
| 5692 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5693 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5695 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5696 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5698 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5699 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5701 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5702 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5703 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5704 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5705 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5706 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5708 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5710 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5711 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5713 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5714 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5716 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5717 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5719 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5720 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5721 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5722 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5723 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5725 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5726 | |
| 5727 | static slotdef slotdefs[] = { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5728 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5729 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5730 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5731 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5732 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5733 | "x.__repr__() <==> repr(x)"), |
| 5734 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5735 | "x.__hash__() <==> hash(x)"), |
| 5736 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5737 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5738 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5739 | "x.__str__() <==> str(x)"), |
| 5740 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5741 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5742 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5743 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5744 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5745 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5746 | "x.__delattr__('name') <==> del x.name"), |
| 5747 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5748 | "x.__lt__(y) <==> x<y"), |
| 5749 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5750 | "x.__le__(y) <==> x<=y"), |
| 5751 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5752 | "x.__eq__(y) <==> x==y"), |
| 5753 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5754 | "x.__ne__(y) <==> x!=y"), |
| 5755 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5756 | "x.__gt__(y) <==> x>y"), |
| 5757 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5758 | "x.__ge__(y) <==> x>=y"), |
| 5759 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5760 | "x.__iter__() <==> iter(x)"), |
| 5761 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5762 | "x.__next__() <==> next(x)"), |
| 5763 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5764 | "descr.__get__(obj[, type]) -> value"), |
| 5765 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5766 | "descr.__set__(obj, value)"), |
| 5767 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5768 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5769 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5770 | "x.__init__(...) initializes x; " |
| 5771 | "see help(type(x)) for signature", |
| 5772 | PyWrapperFlag_KEYWORDS), |
| 5773 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5774 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5776 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5777 | "+"), |
| 5778 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5779 | "+"), |
| 5780 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5781 | "-"), |
| 5782 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5783 | "-"), |
| 5784 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5785 | "*"), |
| 5786 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5787 | "*"), |
| 5788 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5789 | "%"), |
| 5790 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5791 | "%"), |
| 5792 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5793 | "divmod(x, y)"), |
| 5794 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5795 | "divmod(y, x)"), |
| 5796 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5797 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5798 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5799 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5800 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5801 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5802 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5803 | "abs(x)"), |
| 5804 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5805 | "x != 0"), |
| 5806 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5807 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5808 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5809 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5810 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5811 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5812 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5813 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5814 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5815 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5816 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5817 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5818 | "int(x)"), |
| 5819 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5820 | "float(x)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5822 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5823 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5824 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5825 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5826 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5828 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5829 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5830 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5831 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5832 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5833 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5834 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5835 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5836 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5837 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5838 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5839 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5840 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5842 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5843 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5844 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5845 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5846 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5847 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5848 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5849 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5850 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5851 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5852 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5853 | "x.__len__() <==> len(x)"), |
| 5854 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5855 | wrap_binaryfunc, |
| 5856 | "x.__getitem__(y) <==> x[y]"), |
| 5857 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5858 | wrap_objobjargproc, |
| 5859 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5860 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5861 | wrap_delitem, |
| 5862 | "x.__delitem__(y) <==> del x[y]"), |
| 5863 | |
| 5864 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5865 | "x.__len__() <==> len(x)"), |
| 5866 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5867 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5868 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5869 | user-defined methods has unexpected side-effects, as shown by |
| 5870 | test_descr.notimplemented() */ |
| 5871 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5872 | "x.__add__(y) <==> x+y"), |
| 5873 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5874 | "x.__mul__(n) <==> x*n"), |
| 5875 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5876 | "x.__rmul__(n) <==> n*x"), |
| 5877 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5878 | "x.__getitem__(y) <==> x[y]"), |
| 5879 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5880 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5881 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5882 | "x.__delitem__(y) <==> del x[y]"), |
| 5883 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5884 | "x.__contains__(y) <==> y in x"), |
| 5885 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5886 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5887 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5888 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
| 5889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5890 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5891 | }; |
| 5892 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5893 | /* 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] | 5894 | 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] | 5895 | the offset to the type pointer, since it takes care to indirect through the |
| 5896 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5897 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5898 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5899 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5901 | char *ptr; |
| 5902 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5904 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5905 | assert(offset >= 0); |
| 5906 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5907 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5908 | ptr = (char *)type->tp_as_sequence; |
| 5909 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5910 | } |
| 5911 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5912 | ptr = (char *)type->tp_as_mapping; |
| 5913 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5914 | } |
| 5915 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5916 | ptr = (char *)type->tp_as_number; |
| 5917 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5918 | } |
| 5919 | else { |
| 5920 | ptr = (char *)type; |
| 5921 | } |
| 5922 | if (ptr != NULL) |
| 5923 | ptr += offset; |
| 5924 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5925 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5926 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5927 | /* Length of array of slotdef pointers used to store slots with the |
| 5928 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5929 | the same __name__, for any __name__. Since that's a static property, it is |
| 5930 | appropriate to declare fixed-size arrays for this. */ |
| 5931 | #define MAX_EQUIV 10 |
| 5932 | |
| 5933 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5934 | exactly one slot function. The name must be an interned string. */ |
| 5935 | static void ** |
| 5936 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | /* 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] | 5939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5940 | /* pname and ptrs act as a little cache */ |
| 5941 | static PyObject *pname; |
| 5942 | static slotdef *ptrs[MAX_EQUIV]; |
| 5943 | slotdef *p, **pp; |
| 5944 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5946 | if (pname != name) { |
| 5947 | /* Collect all slotdefs that match name into ptrs. */ |
| 5948 | pname = name; |
| 5949 | pp = ptrs; |
| 5950 | for (p = slotdefs; p->name_strobj; p++) { |
| 5951 | if (p->name_strobj == name) |
| 5952 | *pp++ = p; |
| 5953 | } |
| 5954 | *pp = NULL; |
| 5955 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | /* Look in all matching slots of the type; if exactly one of these has |
| 5958 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5959 | res = NULL; |
| 5960 | for (pp = ptrs; *pp; pp++) { |
| 5961 | ptr = slotptr(type, (*pp)->offset); |
| 5962 | if (ptr == NULL || *ptr == NULL) |
| 5963 | continue; |
| 5964 | if (res != NULL) |
| 5965 | return NULL; |
| 5966 | res = ptr; |
| 5967 | } |
| 5968 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5971 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5972 | does some incredibly complex thinking and then sticks something into the |
| 5973 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5974 | interests, and then stores a generic wrapper or a specific function into |
| 5975 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5976 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5977 | static slotdef * |
| 5978 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5980 | PyObject *descr; |
| 5981 | PyWrapperDescrObject *d; |
| 5982 | void *generic = NULL, *specific = NULL; |
| 5983 | int use_generic = 0; |
| 5984 | int offset = p->offset; |
| 5985 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | if (ptr == NULL) { |
| 5988 | do { |
| 5989 | ++p; |
| 5990 | } while (p->offset == offset); |
| 5991 | return p; |
| 5992 | } |
| 5993 | do { |
| 5994 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5995 | if (descr == NULL) { |
| 5996 | if (ptr == (void**)&type->tp_iternext) { |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 5997 | specific = (void *)_PyObject_NextNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5998 | } |
| 5999 | continue; |
| 6000 | } |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 6001 | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
| 6002 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6003 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6004 | if (tptr == NULL || tptr == ptr) |
| 6005 | generic = p->function; |
| 6006 | d = (PyWrapperDescrObject *)descr; |
| 6007 | if (d->d_base->wrapper == p->wrapper && |
| 6008 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 6009 | { |
| 6010 | if (specific == NULL || |
| 6011 | specific == d->d_wrapped) |
| 6012 | specific = d->d_wrapped; |
| 6013 | else |
| 6014 | use_generic = 1; |
| 6015 | } |
| 6016 | } |
| 6017 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 6018 | PyCFunction_GET_FUNCTION(descr) == |
| 6019 | (PyCFunction)tp_new_wrapper && |
| 6020 | ptr == (void**)&type->tp_new) |
| 6021 | { |
| 6022 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6023 | so must be special-cased differently. |
| 6024 | If we don't do this, creating an instance will |
| 6025 | always use slot_tp_new which will look up |
| 6026 | __new__ in the MRO which will call tp_new_wrapper |
| 6027 | which will look through the base classes looking |
| 6028 | for a static base and call its tp_new (usually |
| 6029 | PyType_GenericNew), after performing various |
| 6030 | sanity checks and constructing a new argument |
| 6031 | list. Cut all that nonsense short -- this speeds |
| 6032 | up instance creation tremendously. */ |
| 6033 | specific = (void *)type->tp_new; |
| 6034 | /* XXX I'm not 100% sure that there isn't a hole |
| 6035 | in this reasoning that requires additional |
| 6036 | sanity checks. I'll buy the first person to |
| 6037 | point out a bug in this reasoning a beer. */ |
| 6038 | } |
| 6039 | else if (descr == Py_None && |
| 6040 | ptr == (void**)&type->tp_hash) { |
| 6041 | /* We specifically allow __hash__ to be set to None |
| 6042 | to prevent inheritance of the default |
| 6043 | implementation from object.__hash__ */ |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6044 | specific = (void *)PyObject_HashNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6045 | } |
| 6046 | else { |
| 6047 | use_generic = 1; |
| 6048 | generic = p->function; |
| 6049 | } |
| 6050 | } while ((++p)->offset == offset); |
| 6051 | if (specific && !use_generic) |
| 6052 | *ptr = specific; |
| 6053 | else |
| 6054 | *ptr = generic; |
| 6055 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6056 | } |
| 6057 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6058 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6059 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6060 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6061 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6065 | for (; *pp; pp++) |
| 6066 | update_one_slot(type, *pp); |
| 6067 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6070 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6071 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6072 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6073 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6074 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6075 | slotdef *p; |
| 6076 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6078 | if (initialized) |
| 6079 | return; |
| 6080 | for (p = slotdefs; p->name; p++) { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6081 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
| 6082 | assert(!p[1].name || p->offset <= p[1].offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6083 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 6084 | if (!p->name_strobj) |
| 6085 | Py_FatalError("Out of memory interning slotdef names"); |
| 6086 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6087 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6088 | } |
| 6089 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6090 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6091 | static int |
| 6092 | update_slot(PyTypeObject *type, PyObject *name) |
| 6093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6094 | slotdef *ptrs[MAX_EQUIV]; |
| 6095 | slotdef *p; |
| 6096 | slotdef **pp; |
| 6097 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6099 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6100 | subclasses. This could possibly be unified with the |
| 6101 | update_subclasses() recursion below, but carefully: |
| 6102 | they each have their own conditions on which to stop |
| 6103 | recursing into subclasses. */ |
| 6104 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 6105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6106 | init_slotdefs(); |
| 6107 | pp = ptrs; |
| 6108 | for (p = slotdefs; p->name; p++) { |
| 6109 | /* XXX assume name is interned! */ |
| 6110 | if (p->name_strobj == name) |
| 6111 | *pp++ = p; |
| 6112 | } |
| 6113 | *pp = NULL; |
| 6114 | for (pp = ptrs; *pp; pp++) { |
| 6115 | p = *pp; |
| 6116 | offset = p->offset; |
| 6117 | while (p > slotdefs && (p-1)->offset == offset) |
| 6118 | --p; |
| 6119 | *pp = p; |
| 6120 | } |
| 6121 | if (ptrs[0] == NULL) |
| 6122 | return 0; /* Not an attribute that affects any slots */ |
| 6123 | return update_subclasses(type, name, |
| 6124 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6125 | } |
| 6126 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6127 | /* Store the proper functions in the slot dispatches at class (type) |
| 6128 | definition time, based upon which operations the class overrides in its |
| 6129 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6130 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6131 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6133 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6135 | init_slotdefs(); |
| 6136 | for (p = slotdefs; p->name; ) |
| 6137 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6138 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6139 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6140 | static void |
| 6141 | update_all_slots(PyTypeObject* type) |
| 6142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6143 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6145 | init_slotdefs(); |
| 6146 | for (p = slotdefs; p->name; p++) { |
| 6147 | /* update_slot returns int but can't actually fail */ |
| 6148 | update_slot(type, p->name_strobj); |
| 6149 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6150 | } |
| 6151 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6152 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6153 | recursive functions to call a callback for all subclasses, |
| 6154 | but refraining from recursing into subclasses that define 'name'. */ |
| 6155 | |
| 6156 | static int |
| 6157 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6158 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6160 | if (callback(type, data) < 0) |
| 6161 | return -1; |
| 6162 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | static int |
| 6166 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6167 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6169 | PyTypeObject *subclass; |
| 6170 | PyObject *ref, *subclasses, *dict; |
| 6171 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6173 | subclasses = type->tp_subclasses; |
| 6174 | if (subclasses == NULL) |
| 6175 | return 0; |
| 6176 | assert(PyList_Check(subclasses)); |
| 6177 | n = PyList_GET_SIZE(subclasses); |
| 6178 | for (i = 0; i < n; i++) { |
| 6179 | ref = PyList_GET_ITEM(subclasses, i); |
| 6180 | assert(PyWeakref_CheckRef(ref)); |
| 6181 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6182 | assert(subclass != NULL); |
| 6183 | if ((PyObject *)subclass == Py_None) |
| 6184 | continue; |
| 6185 | assert(PyType_Check(subclass)); |
| 6186 | /* Avoid recursing down into unaffected classes */ |
| 6187 | dict = subclass->tp_dict; |
| 6188 | if (dict != NULL && PyDict_Check(dict) && |
| 6189 | PyDict_GetItem(dict, name) != NULL) |
| 6190 | continue; |
| 6191 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6192 | return -1; |
| 6193 | } |
| 6194 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6195 | } |
| 6196 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6197 | /* This function is called by PyType_Ready() to populate the type's |
| 6198 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6199 | function slot (like tp_repr) that's defined in the type, one or more |
| 6200 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6201 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6202 | cause more than one descriptor to be added (for example, the nb_add |
| 6203 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6204 | slots compete for the same descriptor (for example both sq_item and |
| 6205 | mp_subscript generate a __getitem__ descriptor). |
| 6206 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6207 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6208 | 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] | 6209 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6210 | between competing slots: the members of PyHeapTypeObject are listed |
| 6211 | from most general to least general, so the most general slot is |
| 6212 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6213 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6214 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6215 | |
| 6216 | This only adds new descriptors and doesn't overwrite entries in |
| 6217 | tp_dict that were previously defined. The descriptors contain a |
| 6218 | reference to the C function they must call, so that it's safe if they |
| 6219 | are copied into a subtype's __dict__ and the subtype has a different |
| 6220 | C function in its slot -- calling the method defined by the |
| 6221 | descriptor will call the C function that was used to create it, |
| 6222 | rather than the C function present in the slot when it is called. |
| 6223 | (This is important because a subtype may have a C function in the |
| 6224 | slot that calls the method from the dictionary, and we want to avoid |
| 6225 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6226 | |
| 6227 | static int |
| 6228 | add_operators(PyTypeObject *type) |
| 6229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6230 | PyObject *dict = type->tp_dict; |
| 6231 | slotdef *p; |
| 6232 | PyObject *descr; |
| 6233 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6235 | init_slotdefs(); |
| 6236 | for (p = slotdefs; p->name; p++) { |
| 6237 | if (p->wrapper == NULL) |
| 6238 | continue; |
| 6239 | ptr = slotptr(type, p->offset); |
| 6240 | if (!ptr || !*ptr) |
| 6241 | continue; |
| 6242 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6243 | continue; |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6244 | if (*ptr == (void *)PyObject_HashNotImplemented) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6245 | /* Classes may prevent the inheritance of the tp_hash |
| 6246 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6247 | visible as a None value for the __hash__ attribute. */ |
| 6248 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6249 | return -1; |
| 6250 | } |
| 6251 | else { |
| 6252 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6253 | if (descr == NULL) |
| 6254 | return -1; |
| 6255 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6256 | return -1; |
| 6257 | Py_DECREF(descr); |
| 6258 | } |
| 6259 | } |
| 6260 | if (type->tp_new != NULL) { |
| 6261 | if (add_tp_new_wrapper(type) < 0) |
| 6262 | return -1; |
| 6263 | } |
| 6264 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6267 | |
| 6268 | /* Cooperative 'super' */ |
| 6269 | |
| 6270 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6271 | PyObject_HEAD |
| 6272 | PyTypeObject *type; |
| 6273 | PyObject *obj; |
| 6274 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6275 | } superobject; |
| 6276 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6277 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6278 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6279 | "the class invoking super()"}, |
| 6280 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6281 | "the instance invoking super(); may be None"}, |
| 6282 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6283 | "the type of the instance invoking super(); may be None"}, |
| 6284 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6285 | }; |
| 6286 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6287 | static void |
| 6288 | super_dealloc(PyObject *self) |
| 6289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6292 | _PyObject_GC_UNTRACK(self); |
| 6293 | Py_XDECREF(su->obj); |
| 6294 | Py_XDECREF(su->type); |
| 6295 | Py_XDECREF(su->obj_type); |
| 6296 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6300 | super_repr(PyObject *self) |
| 6301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6302 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6304 | if (su->obj_type) |
| 6305 | return PyUnicode_FromFormat( |
| 6306 | "<super: <class '%s'>, <%s object>>", |
| 6307 | su->type ? su->type->tp_name : "NULL", |
| 6308 | su->obj_type->tp_name); |
| 6309 | else |
| 6310 | return PyUnicode_FromFormat( |
| 6311 | "<super: <class '%s'>, NULL>", |
| 6312 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
| 6315 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6316 | super_getattro(PyObject *self, PyObject *name) |
| 6317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6318 | superobject *su = (superobject *)self; |
| 6319 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6321 | if (!skip) { |
| 6322 | /* We want __class__ to return the class of the super object |
| 6323 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6324 | skip = (PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6325 | PyUnicode_GET_LENGTH(name) == 9 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6326 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6327 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6329 | if (!skip) { |
| 6330 | PyObject *mro, *res, *tmp, *dict; |
| 6331 | PyTypeObject *starttype; |
| 6332 | descrgetfunc f; |
| 6333 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6335 | starttype = su->obj_type; |
| 6336 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6338 | if (mro == NULL) |
| 6339 | n = 0; |
| 6340 | else { |
| 6341 | assert(PyTuple_Check(mro)); |
| 6342 | n = PyTuple_GET_SIZE(mro); |
| 6343 | } |
| 6344 | for (i = 0; i < n; i++) { |
| 6345 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6346 | break; |
| 6347 | } |
| 6348 | i++; |
| 6349 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6350 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 6351 | replaced during PyDict_GetItem(dict, name) */ |
| 6352 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6353 | for (; i < n; i++) { |
| 6354 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6355 | if (PyType_Check(tmp)) |
| 6356 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6357 | else |
| 6358 | continue; |
| 6359 | res = PyDict_GetItem(dict, name); |
| 6360 | if (res != NULL) { |
| 6361 | Py_INCREF(res); |
| 6362 | f = Py_TYPE(res)->tp_descr_get; |
| 6363 | if (f != NULL) { |
| 6364 | tmp = f(res, |
| 6365 | /* Only pass 'obj' param if |
| 6366 | this is instance-mode super |
| 6367 | (See SF ID #743627) |
| 6368 | */ |
| 6369 | (su->obj == (PyObject *) |
| 6370 | su->obj_type |
| 6371 | ? (PyObject *)NULL |
| 6372 | : su->obj), |
| 6373 | (PyObject *)starttype); |
| 6374 | Py_DECREF(res); |
| 6375 | res = tmp; |
| 6376 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6377 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6378 | return res; |
| 6379 | } |
| 6380 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6381 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6382 | } |
| 6383 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6386 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6387 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6390 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 6391 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6393 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6394 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6396 | - If it is an instance, it must be an instance of 'type'. This is |
| 6397 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6399 | But... when obj is an instance, we want to allow for the case where |
| 6400 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6401 | This will allow using super() with a proxy for obj. |
| 6402 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6404 | /* Check for first bullet above (special case) */ |
| 6405 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6406 | Py_INCREF(obj); |
| 6407 | return (PyTypeObject *)obj; |
| 6408 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6410 | /* Normal case */ |
| 6411 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6412 | Py_INCREF(Py_TYPE(obj)); |
| 6413 | return Py_TYPE(obj); |
| 6414 | } |
| 6415 | else { |
| 6416 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6417 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6418 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6419 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6420 | if (class_attr != NULL && |
| 6421 | PyType_Check(class_attr) && |
| 6422 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6423 | { |
| 6424 | int ok = PyType_IsSubtype( |
| 6425 | (PyTypeObject *)class_attr, type); |
| 6426 | if (ok) |
| 6427 | return (PyTypeObject *)class_attr; |
| 6428 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6430 | if (class_attr == NULL) |
| 6431 | PyErr_Clear(); |
| 6432 | else |
| 6433 | Py_DECREF(class_attr); |
| 6434 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6436 | PyErr_SetString(PyExc_TypeError, |
| 6437 | "super(type, obj): " |
| 6438 | "obj must be an instance or subtype of type"); |
| 6439 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6442 | static PyObject * |
| 6443 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6445 | superobject *su = (superobject *)self; |
| 6446 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6448 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6449 | /* Not binding to an object, or already bound */ |
| 6450 | Py_INCREF(self); |
| 6451 | return self; |
| 6452 | } |
| 6453 | if (Py_TYPE(su) != &PySuper_Type) |
| 6454 | /* If su is an instance of a (strict) subclass of super, |
| 6455 | call its type */ |
| 6456 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6457 | su->type, obj, NULL); |
| 6458 | else { |
| 6459 | /* Inline the common case */ |
| 6460 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6461 | if (obj_type == NULL) |
| 6462 | return NULL; |
| 6463 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6464 | NULL, NULL); |
| 6465 | if (newobj == NULL) |
| 6466 | return NULL; |
| 6467 | Py_INCREF(su->type); |
| 6468 | Py_INCREF(obj); |
| 6469 | newobj->type = su->type; |
| 6470 | newobj->obj = obj; |
| 6471 | newobj->obj_type = obj_type; |
| 6472 | return (PyObject *)newobj; |
| 6473 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | static int |
| 6477 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6479 | superobject *su = (superobject *)self; |
| 6480 | PyTypeObject *type = NULL; |
| 6481 | PyObject *obj = NULL; |
| 6482 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6484 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6485 | return -1; |
| 6486 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6487 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6489 | if (type == NULL) { |
| 6490 | /* Call super(), without args -- fill in from __class__ |
| 6491 | and first local variable on the stack. */ |
| 6492 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6493 | PyCodeObject *co = f->f_code; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6494 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | if (co == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6496 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | "super(): no code object"); |
| 6498 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6499 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6500 | if (co->co_argcount == 0) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6501 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | "super(): no arguments"); |
| 6503 | return -1; |
| 6504 | } |
| 6505 | obj = f->f_localsplus[0]; |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 6506 | if (obj == NULL && co->co_cell2arg) { |
| 6507 | /* The first argument might be a cell. */ |
| 6508 | n = PyTuple_GET_SIZE(co->co_cellvars); |
| 6509 | for (i = 0; i < n; i++) { |
| 6510 | if (co->co_cell2arg[i] == 0) { |
| 6511 | PyObject *cell = f->f_localsplus[co->co_nlocals + i]; |
| 6512 | assert(PyCell_Check(cell)); |
| 6513 | obj = PyCell_GET(cell); |
| 6514 | break; |
| 6515 | } |
| 6516 | } |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 6517 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6518 | if (obj == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6519 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6520 | "super(): arg[0] deleted"); |
| 6521 | return -1; |
| 6522 | } |
| 6523 | if (co->co_freevars == NULL) |
| 6524 | n = 0; |
| 6525 | else { |
| 6526 | assert(PyTuple_Check(co->co_freevars)); |
| 6527 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6528 | } |
| 6529 | for (i = 0; i < n; i++) { |
| 6530 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6531 | assert(PyUnicode_Check(name)); |
| 6532 | if (!PyUnicode_CompareWithASCIIString(name, |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 6533 | "__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6534 | Py_ssize_t index = co->co_nlocals + |
| 6535 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6536 | PyObject *cell = f->f_localsplus[index]; |
| 6537 | if (cell == NULL || !PyCell_Check(cell)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6538 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6539 | "super(): bad __class__ cell"); |
| 6540 | return -1; |
| 6541 | } |
| 6542 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6543 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6544 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6545 | "super(): empty __class__ cell"); |
| 6546 | return -1; |
| 6547 | } |
| 6548 | if (!PyType_Check(type)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6549 | PyErr_Format(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | "super(): __class__ is not a type (%s)", |
| 6551 | Py_TYPE(type)->tp_name); |
| 6552 | return -1; |
| 6553 | } |
| 6554 | break; |
| 6555 | } |
| 6556 | } |
| 6557 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6558 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6559 | "super(): __class__ cell not found"); |
| 6560 | return -1; |
| 6561 | } |
| 6562 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6564 | if (obj == Py_None) |
| 6565 | obj = NULL; |
| 6566 | if (obj != NULL) { |
| 6567 | obj_type = supercheck(type, obj); |
| 6568 | if (obj_type == NULL) |
| 6569 | return -1; |
| 6570 | Py_INCREF(obj); |
| 6571 | } |
| 6572 | Py_INCREF(type); |
| 6573 | su->type = type; |
| 6574 | su->obj = obj; |
| 6575 | su->obj_type = obj_type; |
| 6576 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6579 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6580 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6581 | "super(type) -> unbound super object\n" |
| 6582 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6583 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6584 | "Typical use to call a cooperative superclass method:\n" |
| 6585 | "class C(B):\n" |
| 6586 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6587 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6588 | "This works for class methods too:\n" |
| 6589 | "class C(B):\n" |
| 6590 | " @classmethod\n" |
| 6591 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6593 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6594 | static int |
| 6595 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6597 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6599 | Py_VISIT(su->obj); |
| 6600 | Py_VISIT(su->type); |
| 6601 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6603 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6606 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6607 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6608 | "super", /* tp_name */ |
| 6609 | sizeof(superobject), /* tp_basicsize */ |
| 6610 | 0, /* tp_itemsize */ |
| 6611 | /* methods */ |
| 6612 | super_dealloc, /* tp_dealloc */ |
| 6613 | 0, /* tp_print */ |
| 6614 | 0, /* tp_getattr */ |
| 6615 | 0, /* tp_setattr */ |
| 6616 | 0, /* tp_reserved */ |
| 6617 | super_repr, /* tp_repr */ |
| 6618 | 0, /* tp_as_number */ |
| 6619 | 0, /* tp_as_sequence */ |
| 6620 | 0, /* tp_as_mapping */ |
| 6621 | 0, /* tp_hash */ |
| 6622 | 0, /* tp_call */ |
| 6623 | 0, /* tp_str */ |
| 6624 | super_getattro, /* tp_getattro */ |
| 6625 | 0, /* tp_setattro */ |
| 6626 | 0, /* tp_as_buffer */ |
| 6627 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6628 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6629 | super_doc, /* tp_doc */ |
| 6630 | super_traverse, /* tp_traverse */ |
| 6631 | 0, /* tp_clear */ |
| 6632 | 0, /* tp_richcompare */ |
| 6633 | 0, /* tp_weaklistoffset */ |
| 6634 | 0, /* tp_iter */ |
| 6635 | 0, /* tp_iternext */ |
| 6636 | 0, /* tp_methods */ |
| 6637 | super_members, /* tp_members */ |
| 6638 | 0, /* tp_getset */ |
| 6639 | 0, /* tp_base */ |
| 6640 | 0, /* tp_dict */ |
| 6641 | super_descr_get, /* tp_descr_get */ |
| 6642 | 0, /* tp_descr_set */ |
| 6643 | 0, /* tp_dictoffset */ |
| 6644 | super_init, /* tp_init */ |
| 6645 | PyType_GenericAlloc, /* tp_alloc */ |
| 6646 | PyType_GenericNew, /* tp_new */ |
| 6647 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6648 | }; |