Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 9 | |
| 10 | /* Support type attribute cache */ |
| 11 | |
| 12 | /* The cache can keep references to the names alive for longer than |
| 13 | they normally would. This is why the maximum size is limited to |
| 14 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 15 | strings are used as attribute names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | #define MCACHE_MAX_ATTR_SIZE 100 |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 17 | #define MCACHE_SIZE_EXP 9 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | #define MCACHE_HASH(version, name_hash) \ |
| 19 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 20 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 21 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 23 | ((PyASCIIObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 24 | #define MCACHE_CACHEABLE_NAME(name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | PyUnicode_CheckExact(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 26 | PyUnicode_READY(name) != -1 && \ |
| 27 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 28 | |
| 29 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | unsigned int version; |
| 31 | PyObject *name; /* reference to exactly a str or None */ |
| 32 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 36 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 38 | _Py_IDENTIFIER(__class__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 39 | _Py_IDENTIFIER(__dict__); |
| 40 | _Py_IDENTIFIER(__doc__); |
| 41 | _Py_IDENTIFIER(__getitem__); |
| 42 | _Py_IDENTIFIER(__getattribute__); |
| 43 | _Py_IDENTIFIER(__hash__); |
| 44 | _Py_IDENTIFIER(__module__); |
| 45 | _Py_IDENTIFIER(__name__); |
| 46 | _Py_IDENTIFIER(__new__); |
| 47 | |
| 48 | static PyObject * |
| 49 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 50 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 51 | static PyObject * |
| 52 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 53 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 54 | unsigned int |
| 55 | PyType_ClearCache(void) |
| 56 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | Py_ssize_t i; |
| 58 | unsigned int cur_version_tag = next_version_tag - 1; |
| 59 | |
| 60 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 61 | method_cache[i].version = 0; |
| 62 | Py_CLEAR(method_cache[i].name); |
| 63 | method_cache[i].value = NULL; |
| 64 | } |
| 65 | next_version_tag = 0; |
| 66 | /* mark all version tags as invalid */ |
| 67 | PyType_Modified(&PyBaseObject_Type); |
| 68 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 69 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 70 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 71 | void |
| 72 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 73 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | /* Invalidate any cached data for the specified type and all |
| 75 | subclasses. This function is called after the base |
| 76 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 81 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 82 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 85 | it must first be set on all super types. |
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 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 88 | type (so it must first clear it on all subclasses). The |
| 89 | tp_version_tag value is meaningless unless this flag is set. |
| 90 | We don't assign new version tags eagerly, but only as |
| 91 | needed. |
| 92 | */ |
| 93 | PyObject *raw, *ref; |
| 94 | Py_ssize_t i, n; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 97 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | raw = type->tp_subclasses; |
| 100 | if (raw != NULL) { |
| 101 | n = PyList_GET_SIZE(raw); |
| 102 | for (i = 0; i < n; i++) { |
| 103 | ref = PyList_GET_ITEM(raw, i); |
| 104 | ref = PyWeakref_GET_OBJECT(ref); |
| 105 | if (ref != Py_None) { |
| 106 | PyType_Modified((PyTypeObject *)ref); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
| 114 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | /* |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 116 | 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] | 117 | able to be cached. This function is called after the base |
| 118 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 121 | has a custom MRO that includes a type which is not officially |
| 122 | super type. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | Called from mro_internal, which will subsequently be called on |
| 125 | each subclass when their mro is recursively updated. |
| 126 | */ |
| 127 | Py_ssize_t i, n; |
| 128 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 131 | return; |
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 | n = PyTuple_GET_SIZE(bases); |
| 134 | for (i = 0; i < n; i++) { |
| 135 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 136 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 137 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 138 | assert(PyType_Check(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 142 | !PyType_IsSubtype(type, cls)) { |
| 143 | clear = 1; |
| 144 | break; |
| 145 | } |
| 146 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (clear) |
| 149 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 150 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int |
| 154 | assign_version_tag(PyTypeObject *type) |
| 155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | /* Ensure that the tp_version_tag is valid and set |
| 157 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 158 | must first be done on all super classes. Return 0 if this |
| 159 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 160 | */ |
| 161 | Py_ssize_t i, n; |
| 162 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 165 | return 1; |
| 166 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 167 | return 0; |
| 168 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 169 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | type->tp_version_tag = next_version_tag++; |
| 172 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | if (type->tp_version_tag == 0) { |
| 175 | /* wrap-around or just starting Python - clear the whole |
| 176 | cache by filling names with references to Py_None. |
| 177 | Values are also set to NULL for added protection, as they |
| 178 | are borrowed reference */ |
| 179 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 180 | method_cache[i].value = NULL; |
| 181 | Py_XDECREF(method_cache[i].name); |
| 182 | method_cache[i].name = Py_None; |
| 183 | Py_INCREF(Py_None); |
| 184 | } |
| 185 | /* mark all version tags as invalid */ |
| 186 | PyType_Modified(&PyBaseObject_Type); |
| 187 | return 1; |
| 188 | } |
| 189 | bases = type->tp_bases; |
| 190 | n = PyTuple_GET_SIZE(bases); |
| 191 | for (i = 0; i < n; i++) { |
| 192 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 193 | assert(PyType_Check(b)); |
| 194 | if (!assign_version_tag((PyTypeObject *)b)) |
| 195 | return 0; |
| 196 | } |
| 197 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 198 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 202 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 203 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 204 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 206 | {"__weakrefoffset__", T_LONG, |
| 207 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 208 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 209 | {"__dictoffset__", T_LONG, |
| 210 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 211 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 212 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 213 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 215 | static int |
| 216 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 217 | { |
| 218 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 219 | PyErr_Format(PyExc_TypeError, |
| 220 | "can't set %s.%s", type->tp_name, name); |
| 221 | return 0; |
| 222 | } |
| 223 | if (!value) { |
| 224 | PyErr_Format(PyExc_TypeError, |
| 225 | "can't delete %s.%s", type->tp_name, name); |
| 226 | return 0; |
| 227 | } |
| 228 | return 1; |
| 229 | } |
| 230 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 231 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 232 | type_name(PyTypeObject *type, void *context) |
| 233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 237 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | Py_INCREF(et->ht_name); |
| 240 | return et->ht_name; |
| 241 | } |
| 242 | else { |
| 243 | s = strrchr(type->tp_name, '.'); |
| 244 | if (s == NULL) |
| 245 | s = type->tp_name; |
| 246 | else |
| 247 | s++; |
| 248 | return PyUnicode_FromString(s); |
| 249 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 252 | static PyObject * |
| 253 | type_qualname(PyTypeObject *type, void *context) |
| 254 | { |
| 255 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 256 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 257 | Py_INCREF(et->ht_qualname); |
| 258 | return et->ht_qualname; |
| 259 | } |
| 260 | else { |
| 261 | return type_name(type, context); |
| 262 | } |
| 263 | } |
| 264 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 265 | static int |
| 266 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | PyHeapTypeObject* et; |
| 269 | char *tp_name; |
| 270 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 271 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 272 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (!PyUnicode_Check(value)) { |
| 275 | PyErr_Format(PyExc_TypeError, |
| 276 | "can only assign string to %s.__name__, not '%s'", |
| 277 | type->tp_name, Py_TYPE(value)->tp_name); |
| 278 | return -1; |
| 279 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | /* Check absence of null characters */ |
| 282 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 283 | if (tmp == NULL) |
| 284 | return -1; |
| 285 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 286 | Py_DECREF(tmp); |
| 287 | PyErr_Format(PyExc_ValueError, |
| 288 | "__name__ must not contain null bytes"); |
| 289 | return -1; |
| 290 | } |
| 291 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | tp_name = _PyUnicode_AsString(value); |
| 294 | if (tp_name == NULL) |
| 295 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 300 | |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame^] | 301 | /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name |
| 302 | value. (Bug #16447.) */ |
| 303 | tmp = et->ht_name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | type->tp_name = tp_name; |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame^] | 307 | Py_DECREF(tmp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 312 | static int |
| 313 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 314 | { |
| 315 | PyHeapTypeObject* et; |
| 316 | |
Benjamin Peterson | 2c05a2e | 2012-10-31 00:01:15 -0400 | [diff] [blame] | 317 | if (!check_set_special_type_attr(type, value, "__qualname__")) |
| 318 | return -1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 319 | if (!PyUnicode_Check(value)) { |
| 320 | PyErr_Format(PyExc_TypeError, |
| 321 | "can only assign string to %s.__qualname__, not '%s'", |
| 322 | type->tp_name, Py_TYPE(value)->tp_name); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | et = (PyHeapTypeObject*)type; |
| 327 | Py_INCREF(value); |
| 328 | Py_DECREF(et->ht_qualname); |
| 329 | et->ht_qualname = value; |
| 330 | return 0; |
| 331 | } |
| 332 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 333 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 334 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | PyObject *mod; |
| 337 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 340 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (!mod) { |
| 342 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 343 | return 0; |
| 344 | } |
| 345 | Py_XINCREF(mod); |
| 346 | return mod; |
| 347 | } |
| 348 | else { |
| 349 | s = strrchr(type->tp_name, '.'); |
| 350 | if (s != NULL) |
| 351 | return PyUnicode_FromStringAndSize( |
| 352 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 353 | return PyUnicode_FromString("builtins"); |
| 354 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 357 | static int |
| 358 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 359 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 360 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 364 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 365 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 368 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 369 | type_abstractmethods(PyTypeObject *type, void *context) |
| 370 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 371 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 372 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 373 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 374 | if (type != &PyType_Type) |
| 375 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | if (!mod) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 377 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | Py_XINCREF(mod); |
| 381 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int |
| 385 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | /* __abstractmethods__ should only be set once on a type, in |
| 388 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 389 | special to update subclasses. |
| 390 | */ |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 391 | int abstract, res; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 392 | if (value != NULL) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 393 | abstract = PyObject_IsTrue(value); |
| 394 | if (abstract < 0) |
| 395 | return -1; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 396 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value); |
| 397 | } |
| 398 | else { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 399 | abstract = 0; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 400 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__"); |
| 401 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 402 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | if (res == 0) { |
| 407 | PyType_Modified(type); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 408 | if (abstract) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 410 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | } |
| 413 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 417 | type_get_bases(PyTypeObject *type, void *context) |
| 418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | Py_INCREF(type->tp_bases); |
| 420 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static PyTypeObject *best_base(PyObject *); |
| 424 | static int mro_internal(PyTypeObject *); |
| 425 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 426 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 427 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 428 | static void update_all_slots(PyTypeObject *); |
| 429 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 430 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 431 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 433 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 435 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 436 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 437 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | PyTypeObject *subclass; |
| 440 | PyObject *ref, *subclasses, *old_mro; |
| 441 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | subclasses = type->tp_subclasses; |
| 444 | if (subclasses == NULL) |
| 445 | return 0; |
| 446 | assert(PyList_Check(subclasses)); |
| 447 | n = PyList_GET_SIZE(subclasses); |
| 448 | for (i = 0; i < n; i++) { |
| 449 | ref = PyList_GET_ITEM(subclasses, i); |
| 450 | assert(PyWeakref_CheckRef(ref)); |
| 451 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 452 | assert(subclass != NULL); |
| 453 | if ((PyObject *)subclass == Py_None) |
| 454 | continue; |
| 455 | assert(PyType_Check(subclass)); |
| 456 | old_mro = subclass->tp_mro; |
| 457 | if (mro_internal(subclass) < 0) { |
| 458 | subclass->tp_mro = old_mro; |
| 459 | return -1; |
| 460 | } |
| 461 | else { |
| 462 | PyObject* tuple; |
| 463 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 464 | Py_DECREF(old_mro); |
| 465 | if (!tuple) |
| 466 | return -1; |
| 467 | if (PyList_Append(temp, tuple) < 0) |
| 468 | return -1; |
| 469 | Py_DECREF(tuple); |
| 470 | } |
| 471 | if (mro_subclasses(subclass, temp) < 0) |
| 472 | return -1; |
| 473 | } |
| 474 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int |
| 478 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | Py_ssize_t i; |
| 481 | int r = 0; |
| 482 | PyObject *ob, *temp; |
| 483 | PyTypeObject *new_base, *old_base; |
| 484 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 485 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 486 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (!PyTuple_Check(value)) { |
| 489 | PyErr_Format(PyExc_TypeError, |
| 490 | "can only assign tuple to %s.__bases__, not %s", |
| 491 | type->tp_name, Py_TYPE(value)->tp_name); |
| 492 | return -1; |
| 493 | } |
| 494 | if (PyTuple_GET_SIZE(value) == 0) { |
| 495 | PyErr_Format(PyExc_TypeError, |
| 496 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 497 | type->tp_name); |
| 498 | return -1; |
| 499 | } |
| 500 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 501 | ob = PyTuple_GET_ITEM(value, i); |
| 502 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 503 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 504 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 505 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 506 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | } |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 508 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 509 | PyErr_SetString(PyExc_TypeError, |
| 510 | "a __bases__ item causes an inheritance cycle"); |
| 511 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 516 | |
Benjamin Peterson | ab3c1c1 | 2012-04-01 18:48:02 -0400 | [diff] [blame] | 517 | if (!new_base) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 521 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | Py_INCREF(new_base); |
| 524 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | old_bases = type->tp_bases; |
| 527 | old_base = type->tp_base; |
| 528 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | type->tp_bases = value; |
| 531 | type->tp_base = new_base; |
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 (mro_internal(type) < 0) { |
| 534 | goto bail; |
| 535 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | temp = PyList_New(0); |
| 538 | if (!temp) |
| 539 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | if (r < 0) { |
| 544 | for (i = 0; i < PyList_Size(temp); i++) { |
| 545 | PyTypeObject* cls; |
| 546 | PyObject* mro; |
| 547 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 548 | "", 2, 2, &cls, &mro); |
| 549 | Py_INCREF(mro); |
| 550 | ob = cls->tp_mro; |
| 551 | cls->tp_mro = mro; |
| 552 | Py_DECREF(ob); |
| 553 | } |
| 554 | Py_DECREF(temp); |
| 555 | goto bail; |
| 556 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | /* any base that was in __bases__ but now isn't, we |
| 561 | need to remove |type| from its tp_subclasses. |
| 562 | conversely, any class now in __bases__ that wasn't |
| 563 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | /* for now, sod that: just remove from all old_bases, |
| 566 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 569 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 570 | if (PyType_Check(ob)) { |
| 571 | remove_subclass( |
| 572 | (PyTypeObject*)ob, type); |
| 573 | } |
| 574 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 577 | ob = PyTuple_GET_ITEM(value, i); |
| 578 | if (PyType_Check(ob)) { |
| 579 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 580 | r = -1; |
| 581 | } |
| 582 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | Py_DECREF(old_bases); |
| 587 | Py_DECREF(old_base); |
| 588 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 591 | |
| 592 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | Py_DECREF(type->tp_bases); |
| 594 | Py_DECREF(type->tp_base); |
| 595 | if (type->tp_mro != old_mro) { |
| 596 | Py_DECREF(type->tp_mro); |
| 597 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | type->tp_bases = old_bases; |
| 600 | type->tp_base = old_base; |
| 601 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 607 | type_dict(PyTypeObject *type, void *context) |
| 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | if (type->tp_dict == NULL) { |
| 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
| 612 | } |
| 613 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 616 | static PyObject * |
| 617 | type_get_doc(PyTypeObject *type, void *context) |
| 618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | PyObject *result; |
| 620 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 621 | return PyUnicode_FromString(type->tp_doc); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 622 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | if (result == NULL) { |
| 624 | result = Py_None; |
| 625 | Py_INCREF(result); |
| 626 | } |
| 627 | else if (Py_TYPE(result)->tp_descr_get) { |
| 628 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 629 | (PyObject *)type); |
| 630 | } |
| 631 | else { |
| 632 | Py_INCREF(result); |
| 633 | } |
| 634 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 637 | static int |
| 638 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 639 | { |
| 640 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 641 | return -1; |
| 642 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 643 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 644 | } |
| 645 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 646 | static PyObject * |
| 647 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 650 | case -1: |
| 651 | return NULL; |
| 652 | case 0: |
| 653 | Py_RETURN_FALSE; |
| 654 | default: |
| 655 | Py_RETURN_TRUE; |
| 656 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 661 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 664 | case -1: |
| 665 | return NULL; |
| 666 | case 0: |
| 667 | Py_RETURN_FALSE; |
| 668 | default: |
| 669 | Py_RETURN_TRUE; |
| 670 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 673 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 674 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 676 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 678 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 679 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 680 | (setter)type_set_abstractmethods, NULL}, |
| 681 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 682 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 686 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 687 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | mod = type_module(type, NULL); |
| 692 | if (mod == NULL) |
| 693 | PyErr_Clear(); |
| 694 | else if (!PyUnicode_Check(mod)) { |
| 695 | Py_DECREF(mod); |
| 696 | mod = NULL; |
| 697 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 698 | name = type_qualname(type, NULL); |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 699 | if (name == NULL) { |
| 700 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | return NULL; |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 702 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 705 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 706 | else |
| 707 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | Py_XDECREF(mod); |
| 710 | Py_DECREF(name); |
| 711 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 714 | static PyObject * |
| 715 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 716 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | if (type->tp_new == NULL) { |
| 720 | PyErr_Format(PyExc_TypeError, |
| 721 | "cannot create '%.100s' instances", |
| 722 | type->tp_name); |
| 723 | return NULL; |
| 724 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | obj = type->tp_new(type, args, kwds); |
| 727 | if (obj != NULL) { |
| 728 | /* Ugly exception: when the call was type(something), |
| 729 | don't call tp_init on the result. */ |
| 730 | if (type == &PyType_Type && |
| 731 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 732 | (kwds == NULL || |
| 733 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 734 | return obj; |
| 735 | /* If the returned object is not an instance of type, |
| 736 | it won't be initialized. */ |
| 737 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 738 | return obj; |
| 739 | type = Py_TYPE(obj); |
| 740 | if (type->tp_init != NULL && |
| 741 | type->tp_init(obj, args, kwds) < 0) { |
| 742 | Py_DECREF(obj); |
| 743 | obj = NULL; |
| 744 | } |
| 745 | } |
| 746 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 750 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | PyObject *obj; |
| 753 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 754 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | if (PyType_IS_GC(type)) |
| 757 | obj = _PyObject_GC_Malloc(size); |
| 758 | else |
| 759 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | if (obj == NULL) |
| 762 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 767 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | if (type->tp_itemsize == 0) |
| 770 | PyObject_INIT(obj, type); |
| 771 | else |
| 772 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | if (PyType_IS_GC(type)) |
| 775 | _PyObject_GC_TRACK(obj); |
| 776 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | PyObject * |
| 780 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 785 | /* Helpers for subtyping */ |
| 786 | |
| 787 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 788 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | Py_ssize_t i, n; |
| 791 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | n = Py_SIZE(type); |
| 794 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 795 | for (i = 0; i < n; i++, mp++) { |
| 796 | if (mp->type == T_OBJECT_EX) { |
| 797 | char *addr = (char *)self + mp->offset; |
| 798 | PyObject *obj = *(PyObject **)addr; |
| 799 | if (obj != NULL) { |
| 800 | int err = visit(obj, arg); |
| 801 | if (err) |
| 802 | return err; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 810 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | PyTypeObject *type, *base; |
| 813 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | /* Find the nearest base with a different tp_traverse, |
| 816 | and traverse slots while we're at it */ |
| 817 | type = Py_TYPE(self); |
| 818 | base = type; |
| 819 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 820 | if (Py_SIZE(base)) { |
| 821 | int err = traverse_slots(base, self, visit, arg); |
| 822 | if (err) |
| 823 | return err; |
| 824 | } |
| 825 | base = base->tp_base; |
| 826 | assert(base); |
| 827 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 830 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 831 | if (dictptr && *dictptr) |
| 832 | Py_VISIT(*dictptr); |
| 833 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 836 | /* For a heaptype, the instances count as references |
| 837 | to the type. Traverse the type so the collector |
| 838 | can find cycles involving this link. */ |
| 839 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | if (basetraverse) |
| 842 | return basetraverse(self, visit, arg); |
| 843 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static void |
| 847 | clear_slots(PyTypeObject *type, PyObject *self) |
| 848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | Py_ssize_t i, n; |
| 850 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | n = Py_SIZE(type); |
| 853 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 854 | for (i = 0; i < n; i++, mp++) { |
| 855 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 856 | char *addr = (char *)self + mp->offset; |
| 857 | PyObject *obj = *(PyObject **)addr; |
| 858 | if (obj != NULL) { |
| 859 | *(PyObject **)addr = NULL; |
| 860 | Py_DECREF(obj); |
| 861 | } |
| 862 | } |
| 863 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static int |
| 867 | subtype_clear(PyObject *self) |
| 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | PyTypeObject *type, *base; |
| 870 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | /* Find the nearest base with a different tp_clear |
| 873 | and clear slots while we're at it */ |
| 874 | type = Py_TYPE(self); |
| 875 | base = type; |
| 876 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 877 | if (Py_SIZE(base)) |
| 878 | clear_slots(base, self); |
| 879 | base = base->tp_base; |
| 880 | assert(base); |
| 881 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 882 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 883 | /* Clear the instance dict (if any), to break cycles involving only |
| 884 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 885 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 886 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 887 | if (dictptr && *dictptr) |
| 888 | Py_CLEAR(*dictptr); |
| 889 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | if (baseclear) |
| 892 | return baseclear(self); |
| 893 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 894 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 895 | |
| 896 | static void |
| 897 | subtype_dealloc(PyObject *self) |
| 898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | PyTypeObject *type, *base; |
| 900 | destructor basedealloc; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 901 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | /* Extract the type; we expect it to be a heap type */ |
| 904 | type = Py_TYPE(self); |
| 905 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | if (!PyType_IS_GC(type)) { |
| 910 | /* It's really rare to find a dynamic type that doesn't have |
| 911 | GC; it can only happen when deriving from 'object' and not |
| 912 | adding any slots or instance variables. This allows |
| 913 | certain simplifications: there's no need to call |
| 914 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | /* Maybe call finalizer; exit early if resurrected */ |
| 917 | if (type->tp_del) { |
| 918 | type->tp_del(self); |
| 919 | if (self->ob_refcnt > 0) |
| 920 | return; |
| 921 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | /* Find the nearest base with a different tp_dealloc */ |
| 924 | base = type; |
| 925 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 926 | assert(Py_SIZE(base) == 0); |
| 927 | base = base->tp_base; |
| 928 | assert(base); |
| 929 | } |
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 | /* Extract the type again; tp_del may have changed it */ |
| 932 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | /* Call the base tp_dealloc() */ |
| 935 | assert(basedealloc); |
| 936 | basedealloc(self); |
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 | /* Can't reference self beyond this point */ |
| 939 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | /* Done */ |
| 942 | return; |
| 943 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 948 | /* See explanation at end of function for full disclosure */ |
| 949 | PyObject_GC_UnTrack(self); |
| 950 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 951 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 953 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 954 | -- tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 956 | * (if any, and whether directly here or indirectly in something we |
| 957 | * call) may trigger GC, and if self is tracked at that point, it |
| 958 | * will look like trash to GC and GC will try to delete self again. |
| 959 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | /* Find the nearest base with a different tp_dealloc */ |
| 962 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 963 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | base = base->tp_base; |
| 965 | assert(base); |
| 966 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 969 | the finalizer (__del__), clearing slots, or clearing the instance |
| 970 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 973 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | /* Maybe call finalizer; exit early if resurrected */ |
| 976 | if (type->tp_del) { |
| 977 | _PyObject_GC_TRACK(self); |
| 978 | type->tp_del(self); |
| 979 | if (self->ob_refcnt > 0) |
| 980 | goto endlabel; /* resurrected */ |
| 981 | else |
| 982 | _PyObject_GC_UNTRACK(self); |
| 983 | /* New weakrefs could be created during the finalizer call. |
| 984 | If this occurs, clear them out without calling their |
| 985 | finalizers since they might rely on part of the object |
| 986 | being finalized that has already been destroyed. */ |
| 987 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 988 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 989 | PyWeakReference **list = (PyWeakReference **) \ |
| 990 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 991 | while (*list) |
| 992 | _PyWeakref_ClearRef(*list); |
| 993 | } |
| 994 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 997 | base = type; |
| 998 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 999 | if (Py_SIZE(base)) |
| 1000 | clear_slots(base, self); |
| 1001 | base = base->tp_base; |
| 1002 | assert(base); |
| 1003 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | /* If we added a dict, DECREF it */ |
| 1006 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1007 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1008 | if (dictptr != NULL) { |
| 1009 | PyObject *dict = *dictptr; |
| 1010 | if (dict != NULL) { |
| 1011 | Py_DECREF(dict); |
| 1012 | *dictptr = NULL; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | /* Extract the type again; tp_del may have changed it */ |
| 1018 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | /* Call the base tp_dealloc(); first retrack self if |
| 1021 | * basedealloc knows about gc. |
| 1022 | */ |
| 1023 | if (PyType_IS_GC(base)) |
| 1024 | _PyObject_GC_TRACK(self); |
| 1025 | assert(basedealloc); |
| 1026 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | /* Can't reference self beyond this point */ |
| 1029 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1030 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1031 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1033 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | Py_TRASHCAN_SAFE_END(self); |
| 1035 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1036 | -- tstate->trash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1043 | For one, this explains why there must be a call to GC-untrack |
| 1044 | before the trashcan begin macro. Without understanding the |
| 1045 | trashcan code, the answers to the following questions don't make |
| 1046 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1049 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | A. In the case that the base class is GC-aware, the base class |
| 1052 | probably GC-untracks the object. If it does that using the |
| 1053 | UNTRACK macro, this will crash when the object is already |
| 1054 | untracked. Because we don't know what the base class does, the |
| 1055 | only safe thing is to make sure the object is tracked when we |
| 1056 | call the base class dealloc. But... The trashcan begin macro |
| 1057 | requires that the object is *untracked* before it is called. So |
| 1058 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | GC untrack |
| 1061 | trashcan begin |
| 1062 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | Q. Why did the last question say "immediately GC-track again"? |
| 1065 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1068 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1069 | (which can happen if any weakref callback gets invoked), it |
| 1070 | 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] | 1071 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | Q. Why the bizarre (net-zero) manipulation of |
| 1075 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1078 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | - the trashcan limit is not yet reached, so the trashcan level |
| 1085 | is incremented and the code between trashcan begin and end is |
| 1086 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | - this destroys much of the object's contents, including its |
| 1089 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | - basedealloc() is called; this is really list_dealloc(), or |
| 1092 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | - the trashcan limit is now reached, so the object is put on the |
| 1095 | trashcan's to-be-deleted-later list |
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 | - basedealloc() returns |
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 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | - subtype_dealloc() returns |
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 | - later, the trashcan code starts deleting the objects from its |
| 1104 | to-be-deleted-later list |
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 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | - at the very least (if the destroyed slots and __dict__ don't |
| 1109 | cause problems) the object's type gets decref'ed a second |
| 1110 | time, which is *BAD*!!! |
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 | The remedy is to make sure that if the code between trashcan |
| 1113 | begin and end in subtype_dealloc() is called, the code between |
| 1114 | trashcan begin and end in basedealloc() will also be called. |
| 1115 | This is done by decrementing the level after passing into the |
| 1116 | trashcan block, and incrementing it just before leaving the |
| 1117 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | But now it's possible that a chain of objects consisting solely |
| 1120 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1121 | the trashcan mechanism completely: the decremented level means |
| 1122 | that the effective level never reaches the limit. Therefore, we |
| 1123 | *increment* the level *before* entering the trashcan block, and |
| 1124 | matchingly decrement it after leaving. This means the trashcan |
| 1125 | 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] | 1126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | Q. Are there any live examples of code in need of all this |
| 1128 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1131 | compiled in debug mode) before the trashcan level manipulations |
| 1132 | were added. For more discussion, see SF patches 581742, 575073 |
| 1133 | and bug 574207. |
| 1134 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1137 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1138 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1139 | /* type test with subclassing support */ |
| 1140 | |
| 1141 | int |
| 1142 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | mro = a->tp_mro; |
| 1147 | if (mro != NULL) { |
| 1148 | /* Deal with multiple inheritance without recursion |
| 1149 | by walking the MRO tuple */ |
| 1150 | Py_ssize_t i, n; |
| 1151 | assert(PyTuple_Check(mro)); |
| 1152 | n = PyTuple_GET_SIZE(mro); |
| 1153 | for (i = 0; i < n; i++) { |
| 1154 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1155 | return 1; |
| 1156 | } |
| 1157 | return 0; |
| 1158 | } |
| 1159 | else { |
| 1160 | /* a is not completely initilized yet; follow tp_base */ |
| 1161 | do { |
| 1162 | if (a == b) |
| 1163 | return 1; |
| 1164 | a = a->tp_base; |
| 1165 | } while (a != NULL); |
| 1166 | return b == &PyBaseObject_Type; |
| 1167 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1170 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1171 | without looking in the instance dictionary |
| 1172 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1174 | 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] | 1175 | static variable used to cache the interned Python string. |
| 1176 | |
| 1177 | Two variants: |
| 1178 | |
| 1179 | - lookup_maybe() returns NULL without raising an exception |
| 1180 | when the _PyType_Lookup() call fails; |
| 1181 | |
| 1182 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1183 | |
| 1184 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1185 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1186 | |
| 1187 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1188 | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1189 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1190 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1191 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1192 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | if (res != NULL) { |
| 1194 | descrgetfunc f; |
| 1195 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1196 | Py_INCREF(res); |
| 1197 | else |
| 1198 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1199 | } |
| 1200 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1203 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1204 | lookup_method(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1205 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1206 | PyObject *res = lookup_maybe(self, attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | if (res == NULL && !PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1208 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1212 | PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1213 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1214 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1215 | return lookup_maybe(self, attrid); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1218 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1220 | as lookup_method to cache the interned name string object. */ |
| 1221 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1222 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1223 | call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | va_list va; |
| 1226 | PyObject *args, *func = 0, *retval; |
| 1227 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1228 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1229 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | if (func == NULL) { |
| 1231 | va_end(va); |
| 1232 | if (!PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1233 | PyErr_SetObject(PyExc_AttributeError, nameid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | return NULL; |
| 1235 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | if (format && *format) |
| 1238 | args = Py_VaBuildValue(format, va); |
| 1239 | else |
| 1240 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | if (args == NULL) |
| 1245 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | assert(PyTuple_Check(args)); |
| 1248 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | Py_DECREF(args); |
| 1251 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1257 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1258 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1259 | call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | va_list va; |
| 1262 | PyObject *args, *func = 0, *retval; |
| 1263 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1264 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1265 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | if (func == NULL) { |
| 1267 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1268 | if (!PyErr_Occurred()) |
| 1269 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | return NULL; |
| 1271 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | if (format && *format) |
| 1274 | args = Py_VaBuildValue(format, va); |
| 1275 | else |
| 1276 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | if (args == NULL) |
| 1281 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | assert(PyTuple_Check(args)); |
| 1284 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | Py_DECREF(args); |
| 1287 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1292 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1293 | Method resolution order algorithm C3 described in |
| 1294 | "A Monotonic Superclass Linearization for Dylan", |
| 1295 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1296 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1297 | (OOPSLA 1996) |
| 1298 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1299 | Some notes about the rules implied by C3: |
| 1300 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1301 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1302 | It isn't legal to repeat a class in a list of base classes. |
| 1303 | |
| 1304 | The next three properties are the 3 constraints in "C3". |
| 1305 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1306 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1307 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1308 | subclasses of C. |
| 1309 | |
| 1310 | Monotonicity. |
| 1311 | The MRO of a class must be an extension without reordering of the |
| 1312 | MRO of each of its superclasses. |
| 1313 | |
| 1314 | Extended Precedence Graph (EPG). |
| 1315 | Linearization is consistent if there is a path in the EPG from |
| 1316 | each class to all its successors in the linearization. See |
| 1317 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1318 | */ |
| 1319 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1320 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1321 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | Py_ssize_t j, size; |
| 1323 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | for (j = whence+1; j < size; j++) { |
| 1326 | if (PyList_GET_ITEM(list, j) == o) |
| 1327 | return 1; |
| 1328 | } |
| 1329 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1332 | static PyObject * |
| 1333 | class_name(PyObject *cls) |
| 1334 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1335 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | if (name == NULL) { |
| 1337 | PyErr_Clear(); |
| 1338 | Py_XDECREF(name); |
| 1339 | name = PyObject_Repr(cls); |
| 1340 | } |
| 1341 | if (name == NULL) |
| 1342 | return NULL; |
| 1343 | if (!PyUnicode_Check(name)) { |
| 1344 | Py_DECREF(name); |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | static int |
| 1351 | check_duplicates(PyObject *list) |
| 1352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | Py_ssize_t i, j, n; |
| 1354 | /* Let's use a quadratic time algorithm, |
| 1355 | assuming that the bases lists is short. |
| 1356 | */ |
| 1357 | n = PyList_GET_SIZE(list); |
| 1358 | for (i = 0; i < n; i++) { |
| 1359 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1360 | for (j = i + 1; j < n; j++) { |
| 1361 | if (PyList_GET_ITEM(list, j) == o) { |
| 1362 | o = class_name(o); |
| 1363 | if (o != NULL) { |
| 1364 | PyErr_Format(PyExc_TypeError, |
| 1365 | "duplicate base class %U", |
| 1366 | o); |
| 1367 | Py_DECREF(o); |
| 1368 | } else { |
| 1369 | PyErr_SetString(PyExc_TypeError, |
| 1370 | "duplicate base class"); |
| 1371 | } |
| 1372 | return -1; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* Raise a TypeError for an MRO order disagreement. |
| 1380 | |
| 1381 | It's hard to produce a good error message. In the absence of better |
| 1382 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | 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] | 1384 | order in which they should be put in the MRO, but it's hard to |
| 1385 | diagnose what constraint can't be satisfied. |
| 1386 | */ |
| 1387 | |
| 1388 | static void |
| 1389 | set_mro_error(PyObject *to_merge, int *remain) |
| 1390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | Py_ssize_t i, n, off, to_merge_size; |
| 1392 | char buf[1000]; |
| 1393 | PyObject *k, *v; |
| 1394 | PyObject *set = PyDict_New(); |
| 1395 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1398 | for (i = 0; i < to_merge_size; i++) { |
| 1399 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1400 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1401 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1402 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1403 | Py_DECREF(set); |
| 1404 | return; |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1411 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | i = 0; |
| 1413 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1414 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1415 | char *name_str; |
| 1416 | if (name != NULL) { |
| 1417 | name_str = _PyUnicode_AsString(name); |
| 1418 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1419 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1420 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1421 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1422 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | Py_XDECREF(name); |
| 1424 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1425 | buf[off++] = ','; |
| 1426 | buf[off] = '\0'; |
| 1427 | } |
| 1428 | } |
| 1429 | PyErr_SetString(PyExc_TypeError, buf); |
| 1430 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1433 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1434 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1436 | int *remain; |
| 1437 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | /* remain stores an index into each sublist of to_merge. |
| 1442 | remain[i] is the index of the next base in to_merge[i] |
| 1443 | that is not included in acc. |
| 1444 | */ |
| 1445 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1446 | if (remain == NULL) |
| 1447 | return -1; |
| 1448 | for (i = 0; i < to_merge_size; i++) |
| 1449 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1450 | |
| 1451 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | empty_cnt = 0; |
| 1453 | for (i = 0; i < to_merge_size; i++) { |
| 1454 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1459 | empty_cnt++; |
| 1460 | continue; |
| 1461 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | The input sequences alone can determine the choice. |
| 1466 | If not, choose the class which appears in the MRO |
| 1467 | of the earliest direct superclass of the new class. |
| 1468 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1471 | for (j = 0; j < to_merge_size; j++) { |
| 1472 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1473 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1474 | goto skip; /* continue outer loop */ |
| 1475 | } |
| 1476 | } |
| 1477 | ok = PyList_Append(acc, candidate); |
| 1478 | if (ok < 0) { |
| 1479 | PyMem_Free(remain); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | for (j = 0; j < to_merge_size; j++) { |
| 1483 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1484 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1485 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1486 | remain[j]++; |
| 1487 | } |
| 1488 | } |
| 1489 | goto again; |
| 1490 | skip: ; |
| 1491 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | if (empty_cnt == to_merge_size) { |
| 1494 | PyMem_FREE(remain); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | set_mro_error(to_merge, remain); |
| 1498 | PyMem_FREE(remain); |
| 1499 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1502 | static PyObject * |
| 1503 | mro_implementation(PyTypeObject *type) |
| 1504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | Py_ssize_t i, n; |
| 1506 | int ok; |
| 1507 | PyObject *bases, *result; |
| 1508 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (type->tp_dict == NULL) { |
| 1511 | if (PyType_Ready(type) < 0) |
| 1512 | return NULL; |
| 1513 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | /* Find a superclass linearization that honors the constraints |
| 1516 | of the explicit lists of bases and the constraints implied by |
| 1517 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | to_merge is a list of lists, where each list is a superclass |
| 1520 | linearization implied by a base class. The last element of |
| 1521 | to_merge is the declared list of bases. |
| 1522 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | bases = type->tp_bases; |
| 1525 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | to_merge = PyList_New(n+1); |
| 1528 | if (to_merge == NULL) |
| 1529 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | for (i = 0; i < n; i++) { |
| 1532 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1533 | PyObject *parentMRO; |
| 1534 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1535 | if (parentMRO == NULL) { |
| 1536 | Py_DECREF(to_merge); |
| 1537 | return NULL; |
| 1538 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1541 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | bases_aslist = PySequence_List(bases); |
| 1544 | if (bases_aslist == NULL) { |
| 1545 | Py_DECREF(to_merge); |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | /* This is just a basic sanity check. */ |
| 1549 | if (check_duplicates(bases_aslist) < 0) { |
| 1550 | Py_DECREF(to_merge); |
| 1551 | Py_DECREF(bases_aslist); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1557 | if (result == NULL) { |
| 1558 | Py_DECREF(to_merge); |
| 1559 | return NULL; |
| 1560 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | ok = pmerge(result, to_merge); |
| 1563 | Py_DECREF(to_merge); |
| 1564 | if (ok < 0) { |
| 1565 | Py_DECREF(result); |
| 1566 | return NULL; |
| 1567 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1573 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static int |
| 1581 | mro_internal(PyTypeObject *type) |
| 1582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | PyObject *mro, *result, *tuple; |
| 1584 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | if (Py_TYPE(type) == &PyType_Type) { |
| 1587 | result = mro_implementation(type); |
| 1588 | } |
| 1589 | else { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1590 | _Py_IDENTIFIER(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | checkit = 1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1592 | mro = lookup_method((PyObject *)type, &PyId_mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | if (mro == NULL) |
| 1594 | return -1; |
| 1595 | result = PyObject_CallObject(mro, NULL); |
| 1596 | Py_DECREF(mro); |
| 1597 | } |
| 1598 | if (result == NULL) |
| 1599 | return -1; |
| 1600 | tuple = PySequence_Tuple(result); |
| 1601 | Py_DECREF(result); |
| 1602 | if (tuple == NULL) |
| 1603 | return -1; |
| 1604 | if (checkit) { |
| 1605 | Py_ssize_t i, len; |
| 1606 | PyObject *cls; |
| 1607 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | for (i = 0; i < len; i++) { |
| 1614 | PyTypeObject *t; |
| 1615 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1616 | if (!PyType_Check(cls)) { |
| 1617 | PyErr_Format(PyExc_TypeError, |
| 1618 | "mro() returned a non-class ('%.500s')", |
| 1619 | Py_TYPE(cls)->tp_name); |
| 1620 | Py_DECREF(tuple); |
| 1621 | return -1; |
| 1622 | } |
| 1623 | t = (PyTypeObject*)cls; |
| 1624 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1625 | PyErr_Format(PyExc_TypeError, |
| 1626 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1627 | t->tp_name); |
| 1628 | Py_DECREF(tuple); |
| 1629 | return -1; |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1636 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | from the custom MRO */ |
| 1638 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | |
| 1646 | /* Calculate the best base amongst multiple base classes. |
| 1647 | This is the first one that's on the path to the "solid base". */ |
| 1648 | |
| 1649 | static PyTypeObject * |
| 1650 | best_base(PyObject *bases) |
| 1651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | Py_ssize_t i, n; |
| 1653 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1654 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | assert(PyTuple_Check(bases)); |
| 1657 | n = PyTuple_GET_SIZE(bases); |
| 1658 | assert(n > 0); |
| 1659 | base = NULL; |
| 1660 | winner = NULL; |
| 1661 | for (i = 0; i < n; i++) { |
| 1662 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1663 | if (!PyType_Check(base_proto)) { |
| 1664 | PyErr_SetString( |
| 1665 | PyExc_TypeError, |
| 1666 | "bases must be types"); |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | base_i = (PyTypeObject *)base_proto; |
| 1670 | if (base_i->tp_dict == NULL) { |
| 1671 | if (PyType_Ready(base_i) < 0) |
| 1672 | return NULL; |
| 1673 | } |
| 1674 | candidate = solid_base(base_i); |
| 1675 | if (winner == NULL) { |
| 1676 | winner = candidate; |
| 1677 | base = base_i; |
| 1678 | } |
| 1679 | else if (PyType_IsSubtype(winner, candidate)) |
| 1680 | ; |
| 1681 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1682 | winner = candidate; |
| 1683 | base = base_i; |
| 1684 | } |
| 1685 | else { |
| 1686 | PyErr_SetString( |
| 1687 | PyExc_TypeError, |
| 1688 | "multiple bases have " |
| 1689 | "instance lay-out conflict"); |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1693 | assert (base != NULL); |
| 1694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static int |
| 1699 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1700 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | size_t t_size = type->tp_basicsize; |
| 1702 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1705 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1706 | /* If itemsize is involved, stricter rules */ |
| 1707 | return t_size != b_size || |
| 1708 | type->tp_itemsize != base->tp_itemsize; |
| 1709 | } |
| 1710 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1711 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1712 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1713 | t_size -= sizeof(PyObject *); |
| 1714 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1715 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1716 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1717 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | static PyTypeObject * |
| 1723 | solid_base(PyTypeObject *type) |
| 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | if (type->tp_base) |
| 1728 | base = solid_base(type->tp_base); |
| 1729 | else |
| 1730 | base = &PyBaseObject_Type; |
| 1731 | if (extra_ivars(type, base)) |
| 1732 | return type; |
| 1733 | else |
| 1734 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1737 | static void object_dealloc(PyObject *); |
| 1738 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1739 | static int update_slot(PyTypeObject *, PyObject *); |
| 1740 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1741 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1742 | /* |
| 1743 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1744 | * inherited from various builtin types. The builtin base usually provides |
| 1745 | * its own __dict__ descriptor, so we use that when we can. |
| 1746 | */ |
| 1747 | static PyTypeObject * |
| 1748 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | while (type->tp_base != NULL) { |
| 1751 | if (type->tp_dictoffset != 0 && |
| 1752 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1753 | return type; |
| 1754 | type = type->tp_base; |
| 1755 | } |
| 1756 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | static PyObject * |
| 1760 | get_dict_descriptor(PyTypeObject *type) |
| 1761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1763 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1764 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1766 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | static void |
| 1772 | raise_dict_descr_error(PyObject *obj) |
| 1773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | PyErr_Format(PyExc_TypeError, |
| 1775 | "this __dict__ descriptor does not support " |
| 1776 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1779 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1780 | subtype_dict(PyObject *obj, void *context) |
| 1781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1785 | if (base != NULL) { |
| 1786 | descrgetfunc func; |
| 1787 | PyObject *descr = get_dict_descriptor(base); |
| 1788 | if (descr == NULL) { |
| 1789 | raise_dict_descr_error(obj); |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | func = Py_TYPE(descr)->tp_descr_get; |
| 1793 | if (func == NULL) { |
| 1794 | raise_dict_descr_error(obj); |
| 1795 | return NULL; |
| 1796 | } |
| 1797 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1798 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1799 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1802 | static int |
| 1803 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1804 | { |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1805 | PyObject *dict, **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1809 | if (base != NULL) { |
| 1810 | descrsetfunc func; |
| 1811 | PyObject *descr = get_dict_descriptor(base); |
| 1812 | if (descr == NULL) { |
| 1813 | raise_dict_descr_error(obj); |
| 1814 | return -1; |
| 1815 | } |
| 1816 | func = Py_TYPE(descr)->tp_descr_set; |
| 1817 | if (func == NULL) { |
| 1818 | raise_dict_descr_error(obj); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | return func(descr, obj, value); |
| 1822 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1823 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | dictptr = _PyObject_GetDictPtr(obj); |
| 1825 | if (dictptr == NULL) { |
| 1826 | PyErr_SetString(PyExc_AttributeError, |
| 1827 | "This object has no __dict__"); |
| 1828 | return -1; |
| 1829 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 1830 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1831 | PyErr_Format(PyExc_TypeError, |
| 1832 | "__dict__ must be set to a dictionary, " |
| 1833 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1834 | return -1; |
| 1835 | } |
| 1836 | dict = *dictptr; |
| 1837 | Py_XINCREF(value); |
| 1838 | *dictptr = value; |
| 1839 | Py_XDECREF(dict); |
| 1840 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1843 | static PyObject * |
| 1844 | subtype_getweakref(PyObject *obj, void *context) |
| 1845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | PyObject **weaklistptr; |
| 1847 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1850 | PyErr_SetString(PyExc_AttributeError, |
| 1851 | "This object has no __weakref__"); |
| 1852 | return NULL; |
| 1853 | } |
| 1854 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1855 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1856 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1857 | weaklistptr = (PyObject **) |
| 1858 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1859 | if (*weaklistptr == NULL) |
| 1860 | result = Py_None; |
| 1861 | else |
| 1862 | result = *weaklistptr; |
| 1863 | Py_INCREF(result); |
| 1864 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1867 | /* Three variants on the subtype_getsets list. */ |
| 1868 | |
| 1869 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | {"__dict__", subtype_dict, subtype_setdict, |
| 1871 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1872 | {"__weakref__", subtype_getweakref, NULL, |
| 1873 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1874 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1875 | }; |
| 1876 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1877 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | {"__dict__", subtype_dict, subtype_setdict, |
| 1879 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1880 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1881 | }; |
| 1882 | |
| 1883 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | {"__weakref__", subtype_getweakref, NULL, |
| 1885 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1886 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1887 | }; |
| 1888 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1889 | static int |
| 1890 | valid_identifier(PyObject *s) |
| 1891 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | if (!PyUnicode_Check(s)) { |
| 1893 | PyErr_Format(PyExc_TypeError, |
| 1894 | "__slots__ items must be strings, not '%.200s'", |
| 1895 | Py_TYPE(s)->tp_name); |
| 1896 | return 0; |
| 1897 | } |
| 1898 | if (!PyUnicode_IsIdentifier(s)) { |
| 1899 | PyErr_SetString(PyExc_TypeError, |
| 1900 | "__slots__ must be identifiers"); |
| 1901 | return 0; |
| 1902 | } |
| 1903 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1906 | /* Forward */ |
| 1907 | static int |
| 1908 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1909 | |
| 1910 | static int |
| 1911 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | assert(args != NULL && PyTuple_Check(args)); |
| 1916 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1919 | PyErr_SetString(PyExc_TypeError, |
| 1920 | "type.__init__() takes no keyword arguments"); |
| 1921 | return -1; |
| 1922 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | if (args != NULL && PyTuple_Check(args) && |
| 1925 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1926 | PyErr_SetString(PyExc_TypeError, |
| 1927 | "type.__init__() takes 1 or 3 arguments"); |
| 1928 | return -1; |
| 1929 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1931 | /* Call object.__init__(self) now. */ |
| 1932 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1933 | args = PyTuple_GetSlice(args, 0, 0); |
| 1934 | res = object_init(cls, args, NULL); |
| 1935 | Py_DECREF(args); |
| 1936 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 1939 | long |
| 1940 | PyType_GetFlags(PyTypeObject *type) |
| 1941 | { |
| 1942 | return type->tp_flags; |
| 1943 | } |
| 1944 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 1945 | /* Determine the most derived metatype. */ |
| 1946 | PyTypeObject * |
| 1947 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 1948 | { |
| 1949 | Py_ssize_t i, nbases; |
| 1950 | PyTypeObject *winner; |
| 1951 | PyObject *tmp; |
| 1952 | PyTypeObject *tmptype; |
| 1953 | |
| 1954 | /* Determine the proper metatype to deal with this, |
| 1955 | and check for metatype conflicts while we're at it. |
| 1956 | Note that if some other metatype wins to contract, |
| 1957 | it's possible that its instances are not types. */ |
| 1958 | |
| 1959 | nbases = PyTuple_GET_SIZE(bases); |
| 1960 | winner = metatype; |
| 1961 | for (i = 0; i < nbases; i++) { |
| 1962 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1963 | tmptype = Py_TYPE(tmp); |
| 1964 | if (PyType_IsSubtype(winner, tmptype)) |
| 1965 | continue; |
| 1966 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1967 | winner = tmptype; |
| 1968 | continue; |
| 1969 | } |
| 1970 | /* else: */ |
| 1971 | PyErr_SetString(PyExc_TypeError, |
| 1972 | "metaclass conflict: " |
| 1973 | "the metaclass of a derived class " |
| 1974 | "must be a (non-strict) subclass " |
| 1975 | "of the metaclasses of all its bases"); |
| 1976 | return NULL; |
| 1977 | } |
| 1978 | return winner; |
| 1979 | } |
| 1980 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1981 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1982 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1983 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1984 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1986 | PyObject *qualname, *slots = NULL, *tmp, *newslots; |
| 1987 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | PyHeapTypeObject *et; |
| 1989 | PyMemberDef *mp; |
| 1990 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1991 | int j, may_add_dict, may_add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1992 | _Py_IDENTIFIER(__qualname__); |
| 1993 | _Py_IDENTIFIER(__slots__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | assert(args != NULL && PyTuple_Check(args)); |
| 1996 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | /* Special case: type(x) should return x->ob_type */ |
| 1999 | { |
| 2000 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2001 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2004 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2005 | Py_INCREF(Py_TYPE(x)); |
| 2006 | return (PyObject *) Py_TYPE(x); |
| 2007 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2010 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2011 | a msg saying type() needs exactly 3. */ |
| 2012 | if (nargs + nkwds != 3) { |
| 2013 | PyErr_SetString(PyExc_TypeError, |
| 2014 | "type() takes 1 or 3 arguments"); |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | /* Check arguments: (name, bases, dict) */ |
| 2020 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2021 | &name, |
| 2022 | &PyTuple_Type, &bases, |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2023 | &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2025 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2026 | /* Determine the proper metatype to deal with this: */ |
| 2027 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2028 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | return NULL; |
| 2030 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | if (winner != metatype) { |
| 2033 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2034 | return winner->tp_new(winner, args, kwds); |
| 2035 | metatype = winner; |
| 2036 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2039 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | if (nbases == 0) { |
| 2041 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2042 | if (bases == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2043 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | nbases = 1; |
| 2045 | } |
| 2046 | else |
| 2047 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | /* Calculate best base, and check that all bases are type objects */ |
| 2050 | base = best_base(bases); |
| 2051 | if (base == NULL) { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2052 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | } |
| 2054 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2055 | PyErr_Format(PyExc_TypeError, |
| 2056 | "type '%.100s' is not an acceptable base type", |
| 2057 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2058 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2060 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2061 | dict = PyDict_Copy(orig_dict); |
| 2062 | if (dict == NULL) |
| 2063 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2066 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | nslots = 0; |
| 2068 | add_dict = 0; |
| 2069 | add_weak = 0; |
| 2070 | may_add_dict = base->tp_dictoffset == 0; |
| 2071 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2072 | if (slots == NULL) { |
| 2073 | if (may_add_dict) { |
| 2074 | add_dict++; |
| 2075 | } |
| 2076 | if (may_add_weak) { |
| 2077 | add_weak++; |
| 2078 | } |
| 2079 | } |
| 2080 | else { |
| 2081 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | /* Make it into a tuple */ |
| 2084 | if (PyUnicode_Check(slots)) |
| 2085 | slots = PyTuple_Pack(1, slots); |
| 2086 | else |
| 2087 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2088 | if (slots == NULL) |
| 2089 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | /* Are slots allowed? */ |
| 2093 | nslots = PyTuple_GET_SIZE(slots); |
| 2094 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2095 | PyErr_Format(PyExc_TypeError, |
| 2096 | "nonempty __slots__ " |
| 2097 | "not supported for subtype of '%s'", |
| 2098 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2099 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | /* Check for valid slot names and two special cases */ |
| 2103 | for (i = 0; i < nslots; i++) { |
| 2104 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2105 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2106 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | assert(PyUnicode_Check(tmp)); |
| 2108 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2109 | if (!may_add_dict || add_dict) { |
| 2110 | PyErr_SetString(PyExc_TypeError, |
| 2111 | "__dict__ slot disallowed: " |
| 2112 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2113 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | } |
| 2115 | add_dict++; |
| 2116 | } |
| 2117 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2118 | if (!may_add_weak || add_weak) { |
| 2119 | PyErr_SetString(PyExc_TypeError, |
| 2120 | "__weakref__ slot disallowed: " |
| 2121 | "either we already got one, " |
| 2122 | "or __itemsize__ != 0"); |
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 | } |
| 2125 | add_weak++; |
| 2126 | } |
| 2127 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | /* Copy slots into a list, mangle names and sort them. |
| 2130 | Sorted names are needed for __class__ assignment. |
| 2131 | Convert them back to tuple at the end. |
| 2132 | */ |
| 2133 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2134 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2135 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | for (i = j = 0; i < nslots; i++) { |
| 2137 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2138 | if ((add_dict && |
| 2139 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2140 | (add_weak && |
| 2141 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2142 | continue; |
| 2143 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2144 | if (!tmp) { |
| 2145 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2146 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2147 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2149 | if (PyDict_GetItem(dict, tmp)) { |
| 2150 | PyErr_Format(PyExc_ValueError, |
| 2151 | "%R in __slots__ conflicts with class variable", |
| 2152 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2153 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2154 | goto error; |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2155 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | j++; |
| 2157 | } |
| 2158 | assert(j == nslots - add_dict - add_weak); |
| 2159 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2160 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2163 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | } |
| 2165 | slots = PyList_AsTuple(newslots); |
| 2166 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2167 | if (slots == NULL) |
| 2168 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | /* Secondary bases may provide weakrefs or dict */ |
| 2171 | if (nbases > 1 && |
| 2172 | ((may_add_dict && !add_dict) || |
| 2173 | (may_add_weak && !add_weak))) { |
| 2174 | for (i = 0; i < nbases; i++) { |
| 2175 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2176 | if (tmp == (PyObject *)base) |
| 2177 | continue; /* Skip primary base */ |
| 2178 | assert(PyType_Check(tmp)); |
| 2179 | tmptype = (PyTypeObject *)tmp; |
| 2180 | if (may_add_dict && !add_dict && |
| 2181 | tmptype->tp_dictoffset != 0) |
| 2182 | add_dict++; |
| 2183 | if (may_add_weak && !add_weak && |
| 2184 | tmptype->tp_weaklistoffset != 0) |
| 2185 | add_weak++; |
| 2186 | if (may_add_dict && !add_dict) |
| 2187 | continue; |
| 2188 | if (may_add_weak && !add_weak) |
| 2189 | continue; |
| 2190 | /* Nothing more to check */ |
| 2191 | break; |
| 2192 | } |
| 2193 | } |
| 2194 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | /* Allocate the type object */ |
| 2197 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2198 | if (type == NULL) |
| 2199 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | /* Keep name and slots alive in the extended type object */ |
| 2202 | et = (PyHeapTypeObject *)type; |
| 2203 | Py_INCREF(name); |
| 2204 | et->ht_name = name; |
| 2205 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2206 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | /* Initialize tp_flags */ |
| 2209 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2210 | Py_TPFLAGS_BASETYPE; |
| 2211 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2212 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | /* Initialize essential fields */ |
| 2215 | type->tp_as_number = &et->as_number; |
| 2216 | type->tp_as_sequence = &et->as_sequence; |
| 2217 | type->tp_as_mapping = &et->as_mapping; |
| 2218 | type->tp_as_buffer = &et->as_buffer; |
| 2219 | type->tp_name = _PyUnicode_AsString(name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2220 | if (!type->tp_name) |
| 2221 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | /* Set tp_base and tp_bases */ |
| 2224 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2225 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | Py_INCREF(base); |
| 2227 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2230 | Py_INCREF(dict); |
| 2231 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2234 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | tmp = PyEval_GetGlobals(); |
| 2236 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2237 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2239 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2240 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2241 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2245 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2246 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2247 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2248 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2249 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2250 | if (qualname != NULL) { |
| 2251 | if (!PyUnicode_Check(qualname)) { |
| 2252 | PyErr_Format(PyExc_TypeError, |
| 2253 | "type __qualname__ must be a str, not %s", |
| 2254 | Py_TYPE(qualname)->tp_name); |
| 2255 | goto error; |
| 2256 | } |
| 2257 | } |
Benjamin Peterson | 8afa7fa | 2012-10-30 23:51:03 -0400 | [diff] [blame] | 2258 | et->ht_qualname = qualname ? qualname : et->ht_name; |
| 2259 | Py_INCREF(et->ht_qualname); |
| 2260 | if (qualname != NULL && PyDict_DelItem(dict, PyId___qualname__.object) < 0) |
| 2261 | goto error; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2264 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2265 | if that fails, it will still look into __dict__. |
| 2266 | */ |
| 2267 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2268 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2270 | Py_ssize_t len; |
| 2271 | char *doc_str; |
| 2272 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | doc_str = _PyUnicode_AsString(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2275 | if (doc_str == NULL) |
| 2276 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2278 | len = strlen(doc_str); |
| 2279 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2280 | if (tp_doc == NULL) |
| 2281 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | memcpy(tp_doc, doc_str, len + 1); |
| 2283 | type->tp_doc = tp_doc; |
| 2284 | } |
| 2285 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | /* Special-case __new__: if it's a plain function, |
| 2288 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2289 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2291 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2292 | if (tmp == NULL) |
| 2293 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2294 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) |
| 2295 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | Py_DECREF(tmp); |
| 2297 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2300 | mp = PyHeapType_GET_MEMBERS(et); |
| 2301 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2302 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | for (i = 0; i < nslots; i++, mp++) { |
| 2304 | mp->name = _PyUnicode_AsString( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2305 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2306 | if (mp->name == NULL) |
| 2307 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | mp->type = T_OBJECT_EX; |
| 2309 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | /* __dict__ and __weakref__ are already filtered out */ |
| 2312 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2313 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | slotoffset += sizeof(PyObject *); |
| 2316 | } |
| 2317 | } |
| 2318 | if (add_dict) { |
| 2319 | if (base->tp_itemsize) |
| 2320 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2321 | else |
| 2322 | type->tp_dictoffset = slotoffset; |
| 2323 | slotoffset += sizeof(PyObject *); |
| 2324 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2325 | if (type->tp_dictoffset) { |
| 2326 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2327 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | if (add_weak) { |
| 2329 | assert(!base->tp_itemsize); |
| 2330 | type->tp_weaklistoffset = slotoffset; |
| 2331 | slotoffset += sizeof(PyObject *); |
| 2332 | } |
| 2333 | type->tp_basicsize = slotoffset; |
| 2334 | type->tp_itemsize = base->tp_itemsize; |
| 2335 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2338 | type->tp_getset = subtype_getsets_full; |
| 2339 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2340 | type->tp_getset = subtype_getsets_weakref_only; |
| 2341 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2342 | type->tp_getset = subtype_getsets_dict_only; |
| 2343 | else |
| 2344 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | /* Special case some slots */ |
| 2347 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2348 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2349 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2350 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2351 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2352 | } |
| 2353 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | /* Enable GC unless there are really no instance variables possible */ |
| 2356 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2357 | type->tp_itemsize == 0)) |
| 2358 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* Always override allocation strategy to use regular heap */ |
| 2361 | type->tp_alloc = PyType_GenericAlloc; |
| 2362 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2363 | type->tp_free = PyObject_GC_Del; |
| 2364 | type->tp_traverse = subtype_traverse; |
| 2365 | type->tp_clear = subtype_clear; |
| 2366 | } |
| 2367 | else |
| 2368 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2371 | if (PyType_Ready(type) < 0) |
| 2372 | goto error; |
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 | /* Put the proper slots in place */ |
| 2375 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2376 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2377 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2379 | |
| 2380 | error: |
| 2381 | Py_XDECREF(dict); |
| 2382 | Py_XDECREF(bases); |
| 2383 | Py_XDECREF(slots); |
| 2384 | Py_XDECREF(type); |
| 2385 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2388 | static short slotoffsets[] = { |
| 2389 | -1, /* invalid slot */ |
| 2390 | #include "typeslots.inc" |
| 2391 | }; |
| 2392 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2393 | PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2394 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2395 | { |
| 2396 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2397 | PyTypeObject *type, *base; |
| 2398 | char *s; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2399 | char *res_start = (char*)res; |
| 2400 | PyType_Slot *slot; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2401 | |
| 2402 | /* Set the type name and qualname */ |
| 2403 | s = strrchr(spec->name, '.'); |
| 2404 | if (s == NULL) |
| 2405 | s = (char*)spec->name; |
| 2406 | else |
| 2407 | s++; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2408 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2409 | if (res == NULL) |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2410 | return NULL; |
| 2411 | type = &res->ht_type; |
Antoine Pitrou | 66a3a7e | 2012-06-24 00:42:59 +0200 | [diff] [blame] | 2412 | /* The flags must be initialized early, before the GC traverses us */ |
| 2413 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2414 | res->ht_name = PyUnicode_FromString(s); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2415 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2416 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2417 | res->ht_qualname = res->ht_name; |
| 2418 | Py_INCREF(res->ht_qualname); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2419 | type->tp_name = spec->name; |
| 2420 | if (!type->tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2421 | goto fail; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2422 | |
| 2423 | /* Adjust for empty tuple bases */ |
| 2424 | if (!bases) { |
| 2425 | base = &PyBaseObject_Type; |
| 2426 | /* See whether Py_tp_base(s) was specified */ |
| 2427 | for (slot = spec->slots; slot->slot; slot++) { |
| 2428 | if (slot->slot == Py_tp_base) |
| 2429 | base = slot->pfunc; |
| 2430 | else if (slot->slot == Py_tp_bases) { |
| 2431 | bases = slot->pfunc; |
| 2432 | Py_INCREF(bases); |
| 2433 | } |
| 2434 | } |
| 2435 | if (!bases) |
| 2436 | bases = PyTuple_Pack(1, base); |
| 2437 | if (!bases) |
| 2438 | goto fail; |
| 2439 | } |
| 2440 | else |
| 2441 | Py_INCREF(bases); |
| 2442 | |
| 2443 | /* Calculate best base, and check that all bases are type objects */ |
| 2444 | base = best_base(bases); |
| 2445 | if (base == NULL) { |
| 2446 | goto fail; |
| 2447 | } |
| 2448 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2449 | PyErr_Format(PyExc_TypeError, |
| 2450 | "type '%.100s' is not an acceptable base type", |
| 2451 | base->tp_name); |
| 2452 | goto fail; |
| 2453 | } |
| 2454 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2455 | /* Initialize essential fields */ |
| 2456 | type->tp_as_number = &res->as_number; |
| 2457 | type->tp_as_sequence = &res->as_sequence; |
| 2458 | type->tp_as_mapping = &res->as_mapping; |
| 2459 | type->tp_as_buffer = &res->as_buffer; |
| 2460 | /* Set tp_base and tp_bases */ |
| 2461 | type->tp_bases = bases; |
| 2462 | bases = NULL; |
| 2463 | Py_INCREF(base); |
| 2464 | type->tp_base = base; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2465 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2466 | type->tp_basicsize = spec->basicsize; |
| 2467 | type->tp_itemsize = spec->itemsize; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2468 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2469 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2470 | if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2471 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2472 | goto fail; |
| 2473 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2474 | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
| 2475 | /* Processed above */ |
| 2476 | continue; |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2477 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2478 | |
| 2479 | /* need to make a copy of the docstring slot, which usually |
| 2480 | points to a static string literal */ |
| 2481 | if (slot->slot == Py_tp_doc) { |
Antoine Pitrou | 682d94c | 2012-05-14 14:43:03 +0200 | [diff] [blame] | 2482 | size_t len = strlen(slot->pfunc)+1; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2483 | char *tp_doc = PyObject_MALLOC(len); |
| 2484 | if (tp_doc == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2485 | goto fail; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2486 | memcpy(tp_doc, slot->pfunc, len); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2487 | type->tp_doc = tp_doc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2488 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2489 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2490 | if (type->tp_dictoffset) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2491 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2492 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2493 | if (type->tp_dealloc == NULL) { |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2494 | /* It's a heap type, so needs the heap types' dealloc. |
| 2495 | subtype_dealloc will call the base type's tp_dealloc, if |
| 2496 | necessary. */ |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2497 | type->tp_dealloc = subtype_dealloc; |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2498 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2500 | if (PyType_Ready(type) < 0) |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2501 | goto fail; |
| 2502 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2503 | /* Set type.__module__ */ |
| 2504 | s = strrchr(spec->name, '.'); |
| 2505 | if (s != NULL) |
| 2506 | _PyDict_SetItemId(type->tp_dict, &PyId___module__, |
| 2507 | PyUnicode_FromStringAndSize( |
| 2508 | spec->name, (Py_ssize_t)(s - spec->name))); |
| 2509 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2510 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2511 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2512 | fail: |
| 2513 | Py_DECREF(res); |
| 2514 | return NULL; |
| 2515 | } |
| 2516 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2517 | PyObject * |
| 2518 | PyType_FromSpec(PyType_Spec *spec) |
| 2519 | { |
| 2520 | return PyType_FromSpecWithBases(spec, NULL); |
| 2521 | } |
| 2522 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2523 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2524 | /* Internal API to look for a name through the MRO. |
| 2525 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2526 | PyObject * |
| 2527 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | Py_ssize_t i, n; |
| 2530 | PyObject *mro, *res, *base, *dict; |
| 2531 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2534 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2535 | /* fast path */ |
| 2536 | h = MCACHE_HASH_METHOD(type, name); |
| 2537 | if (method_cache[h].version == type->tp_version_tag && |
| 2538 | method_cache[h].name == name) |
| 2539 | return method_cache[h].value; |
| 2540 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | /* Look in tp_dict of types in MRO */ |
| 2543 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | /* If mro is NULL, the type is either not yet initialized |
| 2546 | by PyType_Ready(), or already cleared by type_clear(). |
| 2547 | Either way the safest thing to do is to return NULL. */ |
| 2548 | if (mro == NULL) |
| 2549 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2552 | /* keep a strong reference to mro because type->tp_mro can be replaced |
| 2553 | during PyDict_GetItem(dict, name) */ |
| 2554 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | assert(PyTuple_Check(mro)); |
| 2556 | n = PyTuple_GET_SIZE(mro); |
| 2557 | for (i = 0; i < n; i++) { |
| 2558 | base = PyTuple_GET_ITEM(mro, i); |
| 2559 | assert(PyType_Check(base)); |
| 2560 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2561 | assert(dict && PyDict_Check(dict)); |
| 2562 | res = PyDict_GetItem(dict, name); |
| 2563 | if (res != NULL) |
| 2564 | break; |
| 2565 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2566 | Py_DECREF(mro); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2569 | h = MCACHE_HASH_METHOD(type, name); |
| 2570 | method_cache[h].version = type->tp_version_tag; |
| 2571 | method_cache[h].value = res; /* borrowed */ |
| 2572 | Py_INCREF(name); |
| 2573 | Py_DECREF(method_cache[h].name); |
| 2574 | method_cache[h].name = name; |
| 2575 | } |
| 2576 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2579 | static PyObject * |
| 2580 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 2581 | { |
| 2582 | PyObject *oname; |
| 2583 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 2584 | if (oname == NULL) |
| 2585 | return NULL; |
| 2586 | return _PyType_Lookup(type, oname); |
| 2587 | } |
| 2588 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2589 | /* This is similar to PyObject_GenericGetAttr(), |
| 2590 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2591 | static PyObject * |
| 2592 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | PyTypeObject *metatype = Py_TYPE(type); |
| 2595 | PyObject *meta_attribute, *attribute; |
| 2596 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2597 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 2598 | if (!PyUnicode_Check(name)) { |
| 2599 | PyErr_Format(PyExc_TypeError, |
| 2600 | "attribute name must be string, not '%.200s'", |
| 2601 | name->ob_type->tp_name); |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2606 | if (type->tp_dict == NULL) { |
| 2607 | if (PyType_Ready(type) < 0) |
| 2608 | return NULL; |
| 2609 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 | /* No readable descriptor found yet */ |
| 2612 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | /* Look for the attribute in the metatype */ |
| 2615 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | if (meta_attribute != NULL) { |
| 2618 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2621 | /* Data descriptors implement tp_descr_set to intercept |
| 2622 | * writes. Assume the attribute is not overridden in |
| 2623 | * type's tp_dict (and bases): call the descriptor now. |
| 2624 | */ |
| 2625 | return meta_get(meta_attribute, (PyObject *)type, |
| 2626 | (PyObject *)metatype); |
| 2627 | } |
| 2628 | Py_INCREF(meta_attribute); |
| 2629 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2632 | * type and its bases */ |
| 2633 | attribute = _PyType_Lookup(type, name); |
| 2634 | if (attribute != NULL) { |
| 2635 | /* Implement descriptor functionality, if any */ |
| 2636 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | if (local_get != NULL) { |
| 2641 | /* NULL 2nd argument indicates the descriptor was |
| 2642 | * found on the target object itself (or a base) */ |
| 2643 | return local_get(attribute, (PyObject *)NULL, |
| 2644 | (PyObject *)type); |
| 2645 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | Py_INCREF(attribute); |
| 2648 | return attribute; |
| 2649 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | /* No attribute found in local __dict__ (or bases): use the |
| 2652 | * descriptor from the metatype, if any */ |
| 2653 | if (meta_get != NULL) { |
| 2654 | PyObject *res; |
| 2655 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2656 | (PyObject *)metatype); |
| 2657 | Py_DECREF(meta_attribute); |
| 2658 | return res; |
| 2659 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2662 | if (meta_attribute != NULL) { |
| 2663 | return meta_attribute; |
| 2664 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | /* Give up */ |
| 2667 | PyErr_Format(PyExc_AttributeError, |
| 2668 | "type object '%.50s' has no attribute '%U'", |
| 2669 | type->tp_name, name); |
| 2670 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | static int |
| 2674 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2677 | PyErr_Format( |
| 2678 | PyExc_TypeError, |
| 2679 | "can't set attributes of built-in/extension type '%s'", |
| 2680 | type->tp_name); |
| 2681 | return -1; |
| 2682 | } |
| 2683 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2684 | return -1; |
| 2685 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2688 | extern void |
| 2689 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
| 2690 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2691 | static void |
| 2692 | type_dealloc(PyTypeObject *type) |
| 2693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | /* Assert this is a heap-allocated type object */ |
| 2697 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2698 | _PyObject_GC_UNTRACK(type); |
| 2699 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2700 | et = (PyHeapTypeObject *)type; |
| 2701 | Py_XDECREF(type->tp_base); |
| 2702 | Py_XDECREF(type->tp_dict); |
| 2703 | Py_XDECREF(type->tp_bases); |
| 2704 | Py_XDECREF(type->tp_mro); |
| 2705 | Py_XDECREF(type->tp_cache); |
| 2706 | Py_XDECREF(type->tp_subclasses); |
| 2707 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2708 | * of most other objects. It's okay to cast it to char *. |
| 2709 | */ |
| 2710 | PyObject_Free((char *)type->tp_doc); |
| 2711 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2712 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | Py_XDECREF(et->ht_slots); |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2714 | if (et->ht_cached_keys) |
| 2715 | _PyDictKeys_DecRef(et->ht_cached_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2719 | static PyObject * |
| 2720 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | PyObject *list, *raw, *ref; |
| 2723 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | list = PyList_New(0); |
| 2726 | if (list == NULL) |
| 2727 | return NULL; |
| 2728 | raw = type->tp_subclasses; |
| 2729 | if (raw == NULL) |
| 2730 | return list; |
| 2731 | assert(PyList_Check(raw)); |
| 2732 | n = PyList_GET_SIZE(raw); |
| 2733 | for (i = 0; i < n; i++) { |
| 2734 | ref = PyList_GET_ITEM(raw, i); |
| 2735 | assert(PyWeakref_CheckRef(ref)); |
| 2736 | ref = PyWeakref_GET_OBJECT(ref); |
| 2737 | if (ref != Py_None) { |
| 2738 | if (PyList_Append(list, ref) < 0) { |
| 2739 | Py_DECREF(list); |
| 2740 | return NULL; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2747 | static PyObject * |
| 2748 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2753 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2754 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2755 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2756 | defined, as it's expected that only the final set of dict keys is |
| 2757 | interesting. |
| 2758 | Return 0 on success, -1 on error. |
| 2759 | */ |
| 2760 | |
| 2761 | static int |
| 2762 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2763 | { |
| 2764 | PyObject *classdict; |
| 2765 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2766 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2767 | |
| 2768 | assert(PyDict_Check(dict)); |
| 2769 | assert(aclass); |
| 2770 | |
| 2771 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2772 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2773 | if (classdict == NULL) |
| 2774 | PyErr_Clear(); |
| 2775 | else { |
| 2776 | int status = PyDict_Update(dict, classdict); |
| 2777 | Py_DECREF(classdict); |
| 2778 | if (status < 0) |
| 2779 | return -1; |
| 2780 | } |
| 2781 | |
| 2782 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2783 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2784 | if (bases == NULL) |
| 2785 | PyErr_Clear(); |
| 2786 | else { |
| 2787 | /* We have no guarantee that bases is a real tuple */ |
| 2788 | Py_ssize_t i, n; |
| 2789 | n = PySequence_Size(bases); /* This better be right */ |
| 2790 | if (n < 0) |
| 2791 | PyErr_Clear(); |
| 2792 | else { |
| 2793 | for (i = 0; i < n; i++) { |
| 2794 | int status; |
| 2795 | PyObject *base = PySequence_GetItem(bases, i); |
| 2796 | if (base == NULL) { |
| 2797 | Py_DECREF(bases); |
| 2798 | return -1; |
| 2799 | } |
| 2800 | status = merge_class_dict(dict, base); |
| 2801 | Py_DECREF(base); |
| 2802 | if (status < 0) { |
| 2803 | Py_DECREF(bases); |
| 2804 | return -1; |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | Py_DECREF(bases); |
| 2809 | } |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2814 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2815 | metaclass would probably be more confusing than helpful. |
| 2816 | */ |
| 2817 | static PyObject * |
| 2818 | type_dir(PyObject *self, PyObject *args) |
| 2819 | { |
| 2820 | PyObject *result = NULL; |
| 2821 | PyObject *dict = PyDict_New(); |
| 2822 | |
| 2823 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 2824 | result = PyDict_Keys(dict); |
| 2825 | |
| 2826 | Py_XDECREF(dict); |
| 2827 | return result; |
| 2828 | } |
| 2829 | |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2830 | static PyObject* |
| 2831 | type_sizeof(PyObject *self, PyObject *args_unused) |
| 2832 | { |
| 2833 | Py_ssize_t size; |
| 2834 | PyTypeObject *type = (PyTypeObject*)self; |
| 2835 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 2836 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 2837 | size = sizeof(PyHeapTypeObject); |
| 2838 | if (et->ht_cached_keys) |
| 2839 | size += _PyDict_KeysSize(et->ht_cached_keys); |
| 2840 | } |
| 2841 | else |
| 2842 | size = sizeof(PyTypeObject); |
| 2843 | return PyLong_FromSsize_t(size); |
| 2844 | } |
| 2845 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2846 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2848 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2849 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2850 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2851 | {"__prepare__", (PyCFunction)type_prepare, |
| 2852 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2853 | PyDoc_STR("__prepare__() -> dict\n" |
| 2854 | "used to create the namespace for the class statement")}, |
| 2855 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2856 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2858 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2859 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 2860 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2861 | {"__sizeof__", type_sizeof, METH_NOARGS, |
| 2862 | "__sizeof__() -> int\nreturn memory consumption of the type object"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2864 | }; |
| 2865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2866 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2867 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2868 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2869 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2870 | static int |
| 2871 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | /* Because of type_is_gc(), the collector only calls this |
| 2874 | for heaptypes. */ |
Antoine Pitrou | 1351ca6 | 2012-06-24 00:30:12 +0200 | [diff] [blame] | 2875 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2876 | char msg[200]; |
| 2877 | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
| 2878 | type->tp_name); |
| 2879 | Py_FatalError(msg); |
| 2880 | } |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | Py_VISIT(type->tp_dict); |
| 2883 | Py_VISIT(type->tp_cache); |
| 2884 | Py_VISIT(type->tp_mro); |
| 2885 | Py_VISIT(type->tp_bases); |
| 2886 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | /* There's no need to visit type->tp_subclasses or |
| 2889 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2890 | in cycles; tp_subclasses is a list of weak references, |
| 2891 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | static int |
| 2897 | type_clear(PyTypeObject *type) |
| 2898 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2899 | PyDictKeysObject *cached_keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | /* Because of type_is_gc(), the collector only calls this |
| 2901 | for heaptypes. */ |
| 2902 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2904 | /* We need to invalidate the method cache carefully before clearing |
| 2905 | the dict, so that other objects caught in a reference cycle |
| 2906 | don't start calling destroyed methods. |
| 2907 | |
| 2908 | Otherwise, the only field we need to clear is tp_mro, which is |
| 2909 | part of a hard cycle (its first element is the class itself) that |
| 2910 | 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] | 2911 | tp_clear handler). None of the other fields need to be |
| 2912 | cleared, and here's why: |
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 | tp_cache: |
| 2915 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | tp_bases, tp_base: |
| 2918 | If these are involved in a cycle, there must be at least |
| 2919 | one other, mutable object in the cycle, e.g. a base |
| 2920 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | tp_subclasses: |
| 2923 | A list of weak references can't be part of a cycle; and |
| 2924 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | slots (in PyHeapTypeObject): |
| 2927 | A tuple of strings can't be part of a cycle. |
| 2928 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2930 | PyType_Modified(type); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2931 | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
| 2932 | if (cached_keys != NULL) { |
| 2933 | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
| 2934 | _PyDictKeys_DecRef(cached_keys); |
| 2935 | } |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2936 | if (type->tp_dict) |
| 2937 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | static int |
| 2944 | type_is_gc(PyTypeObject *type) |
| 2945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2949 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2951 | "type", /* tp_name */ |
| 2952 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2953 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2954 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2955 | 0, /* tp_print */ |
| 2956 | 0, /* tp_getattr */ |
| 2957 | 0, /* tp_setattr */ |
| 2958 | 0, /* tp_reserved */ |
| 2959 | (reprfunc)type_repr, /* tp_repr */ |
| 2960 | 0, /* tp_as_number */ |
| 2961 | 0, /* tp_as_sequence */ |
| 2962 | 0, /* tp_as_mapping */ |
| 2963 | 0, /* tp_hash */ |
| 2964 | (ternaryfunc)type_call, /* tp_call */ |
| 2965 | 0, /* tp_str */ |
| 2966 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2967 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2968 | 0, /* tp_as_buffer */ |
| 2969 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2970 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2971 | type_doc, /* tp_doc */ |
| 2972 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2973 | (inquiry)type_clear, /* tp_clear */ |
| 2974 | 0, /* tp_richcompare */ |
| 2975 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2976 | 0, /* tp_iter */ |
| 2977 | 0, /* tp_iternext */ |
| 2978 | type_methods, /* tp_methods */ |
| 2979 | type_members, /* tp_members */ |
| 2980 | type_getsets, /* tp_getset */ |
| 2981 | 0, /* tp_base */ |
| 2982 | 0, /* tp_dict */ |
| 2983 | 0, /* tp_descr_get */ |
| 2984 | 0, /* tp_descr_set */ |
| 2985 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2986 | type_init, /* tp_init */ |
| 2987 | 0, /* tp_alloc */ |
| 2988 | type_new, /* tp_new */ |
| 2989 | PyObject_GC_Del, /* tp_free */ |
| 2990 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2991 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2992 | |
| 2993 | |
| 2994 | /* The base type of all types (eventually)... except itself. */ |
| 2995 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2996 | /* You may wonder why object.__new__() only complains about arguments |
| 2997 | when object.__init__() is not overridden, and vice versa. |
| 2998 | |
| 2999 | Consider the use cases: |
| 3000 | |
| 3001 | 1. When neither is overridden, we want to hear complaints about |
| 3002 | excess (i.e., any) arguments, since their presence could |
| 3003 | indicate there's a bug. |
| 3004 | |
| 3005 | 2. When defining an Immutable type, we are likely to override only |
| 3006 | __new__(), since __init__() is called too late to initialize an |
| 3007 | Immutable object. Since __new__() defines the signature for the |
| 3008 | type, it would be a pain to have to override __init__() just to |
| 3009 | stop it from complaining about excess arguments. |
| 3010 | |
| 3011 | 3. When defining a Mutable type, we are likely to override only |
| 3012 | __init__(). So here the converse reasoning applies: we don't |
| 3013 | want to have to override __new__() just to stop it from |
| 3014 | complaining. |
| 3015 | |
| 3016 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 3017 | object.__init__(), the latter should complain about excess |
| 3018 | arguments; ditto for __new__(). |
| 3019 | |
| 3020 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 3021 | excess arguments. The best solution that addresses all four use |
| 3022 | cases is as follows: __init__() complains about excess arguments |
| 3023 | unless __new__() is overridden and __init__() is not overridden |
| 3024 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 3025 | symmetrically, __new__() complains about excess arguments unless |
| 3026 | __init__() is overridden and __new__() is not overridden |
| 3027 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 3028 | |
| 3029 | However, for backwards compatibility, this breaks too much code. |
| 3030 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 3031 | methods are overridden; for all other cases we'll use the above |
| 3032 | rules. |
| 3033 | |
| 3034 | */ |
| 3035 | |
| 3036 | /* Forward */ |
| 3037 | static PyObject * |
| 3038 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3039 | |
| 3040 | static int |
| 3041 | excess_args(PyObject *args, PyObject *kwds) |
| 3042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | return PyTuple_GET_SIZE(args) || |
| 3044 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3047 | static int |
| 3048 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | int err = 0; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3051 | PyTypeObject *type = Py_TYPE(self); |
| 3052 | if (excess_args(args, kwds) && |
| 3053 | (type->tp_new == object_new || type->tp_init != object_init)) { |
| 3054 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
| 3055 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | } |
| 3057 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3060 | static PyObject * |
| 3061 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3062 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3063 | if (excess_args(args, kwds) && |
| 3064 | (type->tp_init == object_init || type->tp_new != object_new)) { |
R David Murray | 702a5dc | 2013-02-18 21:39:18 -0500 | [diff] [blame] | 3065 | PyErr_SetString(PyExc_TypeError, "object() takes no parameters"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | return NULL; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3067 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | PyObject *abstract_methods = NULL; |
| 3071 | PyObject *builtins; |
| 3072 | PyObject *sorted; |
| 3073 | PyObject *sorted_methods = NULL; |
| 3074 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3075 | PyObject *comma; |
| 3076 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3077 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 3080 | into joined. */ |
| 3081 | abstract_methods = type_abstractmethods(type, NULL); |
| 3082 | if (abstract_methods == NULL) |
| 3083 | goto error; |
| 3084 | builtins = PyEval_GetBuiltins(); |
| 3085 | if (builtins == NULL) |
| 3086 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3087 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | if (sorted == NULL) |
| 3089 | goto error; |
| 3090 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 3091 | abstract_methods, |
| 3092 | NULL); |
| 3093 | if (sorted_methods == NULL) |
| 3094 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3095 | comma = _PyUnicode_FromId(&comma_id); |
| 3096 | if (comma == NULL) |
| 3097 | goto error; |
| 3098 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | if (joined == NULL) |
| 3100 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3102 | PyErr_Format(PyExc_TypeError, |
| 3103 | "Can't instantiate abstract class %s " |
| 3104 | "with abstract methods %U", |
| 3105 | type->tp_name, |
| 3106 | joined); |
| 3107 | error: |
| 3108 | Py_XDECREF(joined); |
| 3109 | Py_XDECREF(sorted_methods); |
| 3110 | Py_XDECREF(abstract_methods); |
| 3111 | return NULL; |
| 3112 | } |
| 3113 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3116 | static void |
| 3117 | object_dealloc(PyObject *self) |
| 3118 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3119 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3122 | static PyObject * |
| 3123 | object_repr(PyObject *self) |
| 3124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | PyTypeObject *type; |
| 3126 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | type = Py_TYPE(self); |
| 3129 | mod = type_module(type, NULL); |
| 3130 | if (mod == NULL) |
| 3131 | PyErr_Clear(); |
| 3132 | else if (!PyUnicode_Check(mod)) { |
| 3133 | Py_DECREF(mod); |
| 3134 | mod = NULL; |
| 3135 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3136 | name = type_qualname(type, NULL); |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3137 | if (name == NULL) { |
| 3138 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | return NULL; |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3140 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 3142 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3143 | else |
| 3144 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3145 | type->tp_name, self); |
| 3146 | Py_XDECREF(mod); |
| 3147 | Py_DECREF(name); |
| 3148 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3151 | static PyObject * |
| 3152 | object_str(PyObject *self) |
| 3153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | f = Py_TYPE(self)->tp_repr; |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 3157 | if (f == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | f = object_repr; |
| 3159 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3162 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3163 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3167 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | case Py_EQ: |
| 3170 | /* Return NotImplemented instead of False, so if two |
| 3171 | objects are compared, both get a chance at the |
| 3172 | comparison. See issue #1393. */ |
| 3173 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3174 | Py_INCREF(res); |
| 3175 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | case Py_NE: |
| 3178 | /* By default, != returns the opposite of ==, |
| 3179 | unless the latter returns NotImplemented. */ |
| 3180 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3181 | if (res != NULL && res != Py_NotImplemented) { |
| 3182 | int ok = PyObject_IsTrue(res); |
| 3183 | Py_DECREF(res); |
| 3184 | if (ok < 0) |
| 3185 | res = NULL; |
| 3186 | else { |
| 3187 | if (ok) |
| 3188 | res = Py_False; |
| 3189 | else |
| 3190 | res = Py_True; |
| 3191 | Py_INCREF(res); |
| 3192 | } |
| 3193 | } |
| 3194 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3196 | default: |
| 3197 | res = Py_NotImplemented; |
| 3198 | Py_INCREF(res); |
| 3199 | break; |
| 3200 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3206 | object_get_class(PyObject *self, void *closure) |
| 3207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | Py_INCREF(Py_TYPE(self)); |
| 3209 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | static int |
| 3213 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3215 | return a == b || |
| 3216 | (a != NULL && |
| 3217 | b != NULL && |
| 3218 | a->tp_basicsize == b->tp_basicsize && |
| 3219 | a->tp_itemsize == b->tp_itemsize && |
| 3220 | a->tp_dictoffset == b->tp_dictoffset && |
| 3221 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3222 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3223 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | static int |
| 3227 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | PyTypeObject *base = a->tp_base; |
| 3230 | Py_ssize_t size; |
| 3231 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3232 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3233 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | size = base->tp_basicsize; |
| 3235 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3236 | size += sizeof(PyObject *); |
| 3237 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3238 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | /* Check slots compliance */ |
| 3241 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3242 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3243 | if (slots_a && slots_b) { |
| 3244 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3245 | return 0; |
| 3246 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3247 | } |
| 3248 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3252 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3257 | newto->tp_free != oldto->tp_free) |
| 3258 | { |
| 3259 | PyErr_Format(PyExc_TypeError, |
| 3260 | "%s assignment: " |
| 3261 | "'%s' deallocator differs from '%s'", |
| 3262 | attr, |
| 3263 | newto->tp_name, |
| 3264 | oldto->tp_name); |
| 3265 | return 0; |
| 3266 | } |
| 3267 | newbase = newto; |
| 3268 | oldbase = oldto; |
| 3269 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3270 | newbase = newbase->tp_base; |
| 3271 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3272 | oldbase = oldbase->tp_base; |
| 3273 | if (newbase != oldbase && |
| 3274 | (newbase->tp_base != oldbase->tp_base || |
| 3275 | !same_slots_added(newbase, oldbase))) { |
| 3276 | PyErr_Format(PyExc_TypeError, |
| 3277 | "%s assignment: " |
| 3278 | "'%s' object layout differs from '%s'", |
| 3279 | attr, |
| 3280 | newto->tp_name, |
| 3281 | oldto->tp_name); |
| 3282 | return 0; |
| 3283 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3289 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | PyTypeObject *oldto = Py_TYPE(self); |
| 3292 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | if (value == NULL) { |
| 3295 | PyErr_SetString(PyExc_TypeError, |
| 3296 | "can't delete __class__ attribute"); |
| 3297 | return -1; |
| 3298 | } |
| 3299 | if (!PyType_Check(value)) { |
| 3300 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3301 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3302 | Py_TYPE(value)->tp_name); |
| 3303 | return -1; |
| 3304 | } |
| 3305 | newto = (PyTypeObject *)value; |
| 3306 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3307 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3308 | { |
| 3309 | PyErr_Format(PyExc_TypeError, |
| 3310 | "__class__ assignment: only for heap types"); |
| 3311 | return -1; |
| 3312 | } |
| 3313 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3314 | Py_INCREF(newto); |
| 3315 | Py_TYPE(self) = newto; |
| 3316 | Py_DECREF(oldto); |
| 3317 | return 0; |
| 3318 | } |
| 3319 | else { |
| 3320 | return -1; |
| 3321 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | {"__class__", object_get_class, object_set_class, |
| 3326 | PyDoc_STR("the object's class")}, |
| 3327 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3328 | }; |
| 3329 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3330 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3331 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3332 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3333 | - pickle protocols < 2 |
| 3334 | - calculating the list of slot names (done only once per class) |
| 3335 | - the __newobj__ function (which is used as a token but never called) |
| 3336 | */ |
| 3337 | |
| 3338 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3339 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | static PyObject *copyreg_str; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3342 | static PyObject *mod_copyreg = NULL; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | if (!copyreg_str) { |
| 3345 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3346 | if (copyreg_str == NULL) |
| 3347 | return NULL; |
| 3348 | } |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3349 | if (!mod_copyreg) { |
| 3350 | mod_copyreg = PyImport_Import(copyreg_str); |
| 3351 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3352 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3353 | Py_XINCREF(mod_copyreg); |
| 3354 | return mod_copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | static PyObject * |
| 3358 | slotnames(PyObject *cls) |
| 3359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | PyObject *clsdict; |
| 3361 | PyObject *copyreg; |
| 3362 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3363 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3364 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3366 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3367 | slotnames = _PyDict_GetItemId(clsdict, &PyId___slotnames__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3369 | Py_INCREF(slotnames); |
| 3370 | return slotnames; |
| 3371 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | copyreg = import_copyreg(); |
| 3374 | if (copyreg == NULL) |
| 3375 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3376 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3377 | slotnames = _PyObject_CallMethodId(copyreg, &PyId__slotnames, "O", cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | Py_DECREF(copyreg); |
| 3379 | if (slotnames != NULL && |
| 3380 | slotnames != Py_None && |
| 3381 | !PyList_Check(slotnames)) |
| 3382 | { |
| 3383 | PyErr_SetString(PyExc_TypeError, |
| 3384 | "copyreg._slotnames didn't return a list or None"); |
| 3385 | Py_DECREF(slotnames); |
| 3386 | slotnames = NULL; |
| 3387 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | static PyObject * |
| 3393 | reduce_2(PyObject *obj) |
| 3394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | PyObject *cls, *getnewargs; |
| 3396 | PyObject *args = NULL, *args2 = NULL; |
| 3397 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3398 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3399 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3400 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3401 | _Py_IDENTIFIER(__getnewargs__); |
| 3402 | _Py_IDENTIFIER(__getstate__); |
| 3403 | _Py_IDENTIFIER(__newobj__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3405 | cls = (PyObject *) Py_TYPE(obj); |
| 3406 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3407 | getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | if (getnewargs != NULL) { |
| 3409 | args = PyObject_CallObject(getnewargs, NULL); |
| 3410 | Py_DECREF(getnewargs); |
| 3411 | if (args != NULL && !PyTuple_Check(args)) { |
| 3412 | PyErr_Format(PyExc_TypeError, |
| 3413 | "__getnewargs__ should return a tuple, " |
| 3414 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3415 | goto end; |
| 3416 | } |
| 3417 | } |
| 3418 | else { |
| 3419 | PyErr_Clear(); |
| 3420 | args = PyTuple_New(0); |
| 3421 | } |
| 3422 | if (args == NULL) |
| 3423 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3424 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3425 | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | if (getstate != NULL) { |
| 3427 | state = PyObject_CallObject(getstate, NULL); |
| 3428 | Py_DECREF(getstate); |
| 3429 | if (state == NULL) |
| 3430 | goto end; |
| 3431 | } |
| 3432 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3433 | PyObject **dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | PyErr_Clear(); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3435 | dict = _PyObject_GetDictPtr(obj); |
| 3436 | if (dict && *dict) |
| 3437 | state = *dict; |
| 3438 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | state = Py_None; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3440 | Py_INCREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | names = slotnames(cls); |
| 3442 | if (names == NULL) |
| 3443 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3444 | if (names != Py_None && PyList_GET_SIZE(names) > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | assert(PyList_Check(names)); |
| 3446 | slots = PyDict_New(); |
| 3447 | if (slots == NULL) |
| 3448 | goto end; |
| 3449 | n = 0; |
| 3450 | /* Can't pre-compute the list size; the list |
| 3451 | is stored on the class so accessible to other |
| 3452 | threads, which may be run by DECREF */ |
| 3453 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3454 | PyObject *name, *value; |
| 3455 | name = PyList_GET_ITEM(names, i); |
| 3456 | value = PyObject_GetAttr(obj, name); |
| 3457 | if (value == NULL) |
| 3458 | PyErr_Clear(); |
| 3459 | else { |
| 3460 | int err = PyDict_SetItem(slots, name, |
| 3461 | value); |
| 3462 | Py_DECREF(value); |
| 3463 | if (err) |
| 3464 | goto end; |
| 3465 | n++; |
| 3466 | } |
| 3467 | } |
| 3468 | if (n) { |
| 3469 | state = Py_BuildValue("(NO)", state, slots); |
| 3470 | if (state == NULL) |
| 3471 | goto end; |
| 3472 | } |
| 3473 | } |
| 3474 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | if (!PyList_Check(obj)) { |
| 3477 | listitems = Py_None; |
| 3478 | Py_INCREF(listitems); |
| 3479 | } |
| 3480 | else { |
| 3481 | listitems = PyObject_GetIter(obj); |
| 3482 | if (listitems == NULL) |
| 3483 | goto end; |
| 3484 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | if (!PyDict_Check(obj)) { |
| 3487 | dictitems = Py_None; |
| 3488 | Py_INCREF(dictitems); |
| 3489 | } |
| 3490 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3491 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3492 | PyObject *items = _PyObject_CallMethodId(obj, &PyId_items, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | if (items == NULL) |
| 3494 | goto end; |
| 3495 | dictitems = PyObject_GetIter(items); |
| 3496 | Py_DECREF(items); |
| 3497 | if (dictitems == NULL) |
| 3498 | goto end; |
| 3499 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | copyreg = import_copyreg(); |
| 3502 | if (copyreg == NULL) |
| 3503 | goto end; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3504 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | if (newobj == NULL) |
| 3506 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | n = PyTuple_GET_SIZE(args); |
| 3509 | args2 = PyTuple_New(n+1); |
| 3510 | if (args2 == NULL) |
| 3511 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3512 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | for (i = 0; i < n; i++) { |
| 3515 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3516 | Py_INCREF(v); |
| 3517 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3518 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3521 | |
| 3522 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | Py_XDECREF(args); |
| 3524 | Py_XDECREF(args2); |
| 3525 | Py_XDECREF(slots); |
| 3526 | Py_XDECREF(state); |
| 3527 | Py_XDECREF(names); |
| 3528 | Py_XDECREF(listitems); |
| 3529 | Py_XDECREF(dictitems); |
| 3530 | Py_XDECREF(copyreg); |
| 3531 | Py_XDECREF(newobj); |
| 3532 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3535 | /* |
| 3536 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3537 | * were implemented in the same function: |
| 3538 | * - trying to pickle an object with a custom __reduce__ method that |
| 3539 | * fell back to object.__reduce__ in certain circumstances led to |
| 3540 | * infinite recursion at Python level and eventual RuntimeError. |
| 3541 | * - Pickling objects that lied about their type by overwriting the |
| 3542 | * __class__ descriptor could lead to infinite recursion at C level |
| 3543 | * and eventual segfault. |
| 3544 | * |
| 3545 | * Because of backwards compatibility, the two methods still have to |
| 3546 | * behave in the same way, even if this is not required by the pickle |
| 3547 | * protocol. This common functionality was moved to the _common_reduce |
| 3548 | * function. |
| 3549 | */ |
| 3550 | static PyObject * |
| 3551 | _common_reduce(PyObject *self, int proto) |
| 3552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3555 | if (proto >= 2) |
| 3556 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | copyreg = import_copyreg(); |
| 3559 | if (!copyreg) |
| 3560 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3563 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3565 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | static PyObject * |
| 3569 | object_reduce(PyObject *self, PyObject *args) |
| 3570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3574 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3579 | static PyObject * |
| 3580 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3581 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3582 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | PyObject *reduce, *res; |
| 3584 | int proto = 0; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3585 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3588 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3589 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3590 | if (objreduce == NULL) { |
| 3591 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 3592 | &PyId___reduce__); |
| 3593 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3594 | return NULL; |
| 3595 | } |
| 3596 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3597 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | if (reduce == NULL) |
| 3599 | PyErr_Clear(); |
| 3600 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3601 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3603 | |
| 3604 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3605 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | if (clsreduce == NULL) { |
| 3607 | Py_DECREF(reduce); |
| 3608 | return NULL; |
| 3609 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3610 | override = (clsreduce != objreduce); |
| 3611 | Py_DECREF(clsreduce); |
| 3612 | if (override) { |
| 3613 | res = PyObject_CallObject(reduce, NULL); |
| 3614 | Py_DECREF(reduce); |
| 3615 | return res; |
| 3616 | } |
| 3617 | else |
| 3618 | Py_DECREF(reduce); |
| 3619 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3624 | static PyObject * |
| 3625 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3626 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3627 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3631 | "Abstract classes can override this to customize issubclass().\n" |
| 3632 | "\n" |
| 3633 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3634 | "It should return True, False or NotImplemented. If it returns\n" |
| 3635 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3636 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3637 | |
| 3638 | /* |
| 3639 | from PEP 3101, this code implements: |
| 3640 | |
| 3641 | class object: |
| 3642 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3644 | */ |
| 3645 | static PyObject * |
| 3646 | object_format(PyObject *self, PyObject *args) |
| 3647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | PyObject *format_spec; |
| 3649 | PyObject *self_as_str = NULL; |
| 3650 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3652 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3653 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3655 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3656 | if (self_as_str != NULL) { |
| 3657 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3658 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3659 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3660 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3661 | "object.__format__ with a non-empty format " |
| 3662 | "string is deprecated", 1) < 0) { |
| 3663 | goto done; |
| 3664 | } |
| 3665 | /* Eventually this will become an error: |
| 3666 | PyErr_Format(PyExc_TypeError, |
| 3667 | "non-empty format string passed to object.__format__"); |
| 3668 | goto done; |
| 3669 | */ |
| 3670 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3671 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3672 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3681 | static PyObject * |
| 3682 | object_sizeof(PyObject *self, PyObject *args) |
| 3683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3684 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3686 | res = 0; |
| 3687 | isize = self->ob_type->tp_itemsize; |
| 3688 | if (isize > 0) |
| 3689 | res = Py_SIZE(self->ob_type) * isize; |
| 3690 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3692 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3695 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 3696 | and recursively up the __class__.__bases__ chain. |
| 3697 | */ |
| 3698 | static PyObject * |
| 3699 | object_dir(PyObject *self, PyObject *args) |
| 3700 | { |
| 3701 | PyObject *result = NULL; |
| 3702 | PyObject *dict = NULL; |
| 3703 | PyObject *itsclass = NULL; |
| 3704 | |
| 3705 | /* 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] | 3706 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3707 | if (dict == NULL) { |
| 3708 | PyErr_Clear(); |
| 3709 | dict = PyDict_New(); |
| 3710 | } |
| 3711 | else if (!PyDict_Check(dict)) { |
| 3712 | Py_DECREF(dict); |
| 3713 | dict = PyDict_New(); |
| 3714 | } |
| 3715 | else { |
| 3716 | /* Copy __dict__ to avoid mutating it. */ |
| 3717 | PyObject *temp = PyDict_Copy(dict); |
| 3718 | Py_DECREF(dict); |
| 3719 | dict = temp; |
| 3720 | } |
| 3721 | |
| 3722 | if (dict == NULL) |
| 3723 | goto error; |
| 3724 | |
| 3725 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3726 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3727 | if (itsclass == NULL) |
| 3728 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 3729 | __class__ exists? */ |
| 3730 | PyErr_Clear(); |
| 3731 | else if (merge_class_dict(dict, itsclass) != 0) |
| 3732 | goto error; |
| 3733 | |
| 3734 | result = PyDict_Keys(dict); |
| 3735 | /* fall through */ |
| 3736 | error: |
| 3737 | Py_XDECREF(itsclass); |
| 3738 | Py_XDECREF(dict); |
| 3739 | return result; |
| 3740 | } |
| 3741 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3742 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3744 | PyDoc_STR("helper for pickle")}, |
| 3745 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3746 | PyDoc_STR("helper for pickle")}, |
| 3747 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3748 | object_subclasshook_doc}, |
| 3749 | {"__format__", object_format, METH_VARARGS, |
| 3750 | PyDoc_STR("default object formatter")}, |
| 3751 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3752 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3753 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3754 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3756 | }; |
| 3757 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3758 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3759 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3761 | "object", /* tp_name */ |
| 3762 | sizeof(PyObject), /* tp_basicsize */ |
| 3763 | 0, /* tp_itemsize */ |
| 3764 | object_dealloc, /* tp_dealloc */ |
| 3765 | 0, /* tp_print */ |
| 3766 | 0, /* tp_getattr */ |
| 3767 | 0, /* tp_setattr */ |
| 3768 | 0, /* tp_reserved */ |
| 3769 | object_repr, /* tp_repr */ |
| 3770 | 0, /* tp_as_number */ |
| 3771 | 0, /* tp_as_sequence */ |
| 3772 | 0, /* tp_as_mapping */ |
| 3773 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3774 | 0, /* tp_call */ |
| 3775 | object_str, /* tp_str */ |
| 3776 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3777 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3778 | 0, /* tp_as_buffer */ |
| 3779 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3780 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3781 | 0, /* tp_traverse */ |
| 3782 | 0, /* tp_clear */ |
| 3783 | object_richcompare, /* tp_richcompare */ |
| 3784 | 0, /* tp_weaklistoffset */ |
| 3785 | 0, /* tp_iter */ |
| 3786 | 0, /* tp_iternext */ |
| 3787 | object_methods, /* tp_methods */ |
| 3788 | 0, /* tp_members */ |
| 3789 | object_getsets, /* tp_getset */ |
| 3790 | 0, /* tp_base */ |
| 3791 | 0, /* tp_dict */ |
| 3792 | 0, /* tp_descr_get */ |
| 3793 | 0, /* tp_descr_set */ |
| 3794 | 0, /* tp_dictoffset */ |
| 3795 | object_init, /* tp_init */ |
| 3796 | PyType_GenericAlloc, /* tp_alloc */ |
| 3797 | object_new, /* tp_new */ |
| 3798 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3799 | }; |
| 3800 | |
| 3801 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3802 | /* 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] | 3803 | |
| 3804 | static int |
| 3805 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | for (; meth->ml_name != NULL; meth++) { |
| 3810 | PyObject *descr; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3811 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3813 | !(meth->ml_flags & METH_COEXIST)) |
| 3814 | continue; |
| 3815 | if (meth->ml_flags & METH_CLASS) { |
| 3816 | if (meth->ml_flags & METH_STATIC) { |
| 3817 | PyErr_SetString(PyExc_ValueError, |
| 3818 | "method cannot be both class and static"); |
| 3819 | return -1; |
| 3820 | } |
| 3821 | descr = PyDescr_NewClassMethod(type, meth); |
| 3822 | } |
| 3823 | else if (meth->ml_flags & METH_STATIC) { |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 3824 | PyObject *cfunc = PyCFunction_New(meth, (PyObject*)type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | if (cfunc == NULL) |
| 3826 | return -1; |
| 3827 | descr = PyStaticMethod_New(cfunc); |
| 3828 | Py_DECREF(cfunc); |
| 3829 | } |
| 3830 | else { |
| 3831 | descr = PyDescr_NewMethod(type, meth); |
| 3832 | } |
| 3833 | if (descr == NULL) |
| 3834 | return -1; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3835 | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | Py_DECREF(descr); |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3837 | if (err < 0) |
| 3838 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | } |
| 3840 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3844 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3846 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | for (; memb->name != NULL; memb++) { |
| 3849 | PyObject *descr; |
| 3850 | if (PyDict_GetItemString(dict, memb->name)) |
| 3851 | continue; |
| 3852 | descr = PyDescr_NewMember(type, memb); |
| 3853 | if (descr == NULL) |
| 3854 | return -1; |
| 3855 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3856 | return -1; |
| 3857 | Py_DECREF(descr); |
| 3858 | } |
| 3859 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3863 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | for (; gsp->name != NULL; gsp++) { |
| 3868 | PyObject *descr; |
| 3869 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3870 | continue; |
| 3871 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | if (descr == NULL) |
| 3874 | return -1; |
| 3875 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3876 | return -1; |
| 3877 | Py_DECREF(descr); |
| 3878 | } |
| 3879 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3882 | static void |
| 3883 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3884 | { |
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 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3888 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3889 | (!type->tp_traverse && !type->tp_clear)) { |
| 3890 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3891 | if (type->tp_traverse == NULL) |
| 3892 | type->tp_traverse = base->tp_traverse; |
| 3893 | if (type->tp_clear == NULL) |
| 3894 | type->tp_clear = base->tp_clear; |
| 3895 | } |
| 3896 | { |
| 3897 | /* The condition below could use some explanation. |
| 3898 | It appears that tp_new is not inherited for static types |
| 3899 | whose base class is 'object'; this seems to be a precaution |
| 3900 | so that old extension types don't suddenly become |
| 3901 | callable (object.__new__ wouldn't insure the invariants |
| 3902 | that the extension type's own factory function ensures). |
| 3903 | Heap types, of course, are under our control, so they do |
| 3904 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3905 | other built-in type as the default also |
| 3906 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3907 | if (base != &PyBaseObject_Type || |
| 3908 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3909 | if (type->tp_new == NULL) |
| 3910 | type->tp_new = base->tp_new; |
| 3911 | } |
| 3912 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3913 | if (type->tp_basicsize == 0) |
| 3914 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3917 | |
| 3918 | #undef COPYVAL |
| 3919 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | COPYVAL(tp_itemsize); |
| 3923 | COPYVAL(tp_weaklistoffset); |
| 3924 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | /* Setup fast subclass flags */ |
| 3927 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3928 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3929 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3930 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3931 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3932 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3933 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3934 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3935 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3936 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3937 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3938 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3939 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3940 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3941 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3942 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3945 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3946 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3947 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3949 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3952 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 3953 | return 1; |
| 3954 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 3955 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3956 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3959 | static void |
| 3960 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3962 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3963 | |
| 3964 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3965 | #undef COPYSLOT |
| 3966 | #undef COPYNUM |
| 3967 | #undef COPYSEQ |
| 3968 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3969 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3970 | |
| 3971 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | (base->SLOT != 0 && \ |
| 3973 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3974 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3975 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3977 | |
| 3978 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3979 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3980 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3981 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3984 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3987 | basebase = base->tp_base; |
| 3988 | if (basebase->tp_as_number == NULL) |
| 3989 | basebase = NULL; |
| 3990 | COPYNUM(nb_add); |
| 3991 | COPYNUM(nb_subtract); |
| 3992 | COPYNUM(nb_multiply); |
| 3993 | COPYNUM(nb_remainder); |
| 3994 | COPYNUM(nb_divmod); |
| 3995 | COPYNUM(nb_power); |
| 3996 | COPYNUM(nb_negative); |
| 3997 | COPYNUM(nb_positive); |
| 3998 | COPYNUM(nb_absolute); |
| 3999 | COPYNUM(nb_bool); |
| 4000 | COPYNUM(nb_invert); |
| 4001 | COPYNUM(nb_lshift); |
| 4002 | COPYNUM(nb_rshift); |
| 4003 | COPYNUM(nb_and); |
| 4004 | COPYNUM(nb_xor); |
| 4005 | COPYNUM(nb_or); |
| 4006 | COPYNUM(nb_int); |
| 4007 | COPYNUM(nb_float); |
| 4008 | COPYNUM(nb_inplace_add); |
| 4009 | COPYNUM(nb_inplace_subtract); |
| 4010 | COPYNUM(nb_inplace_multiply); |
| 4011 | COPYNUM(nb_inplace_remainder); |
| 4012 | COPYNUM(nb_inplace_power); |
| 4013 | COPYNUM(nb_inplace_lshift); |
| 4014 | COPYNUM(nb_inplace_rshift); |
| 4015 | COPYNUM(nb_inplace_and); |
| 4016 | COPYNUM(nb_inplace_xor); |
| 4017 | COPYNUM(nb_inplace_or); |
| 4018 | COPYNUM(nb_true_divide); |
| 4019 | COPYNUM(nb_floor_divide); |
| 4020 | COPYNUM(nb_inplace_true_divide); |
| 4021 | COPYNUM(nb_inplace_floor_divide); |
| 4022 | COPYNUM(nb_index); |
| 4023 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 4026 | basebase = base->tp_base; |
| 4027 | if (basebase->tp_as_sequence == NULL) |
| 4028 | basebase = NULL; |
| 4029 | COPYSEQ(sq_length); |
| 4030 | COPYSEQ(sq_concat); |
| 4031 | COPYSEQ(sq_repeat); |
| 4032 | COPYSEQ(sq_item); |
| 4033 | COPYSEQ(sq_ass_item); |
| 4034 | COPYSEQ(sq_contains); |
| 4035 | COPYSEQ(sq_inplace_concat); |
| 4036 | COPYSEQ(sq_inplace_repeat); |
| 4037 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4039 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 4040 | basebase = base->tp_base; |
| 4041 | if (basebase->tp_as_mapping == NULL) |
| 4042 | basebase = NULL; |
| 4043 | COPYMAP(mp_length); |
| 4044 | COPYMAP(mp_subscript); |
| 4045 | COPYMAP(mp_ass_subscript); |
| 4046 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 4049 | basebase = base->tp_base; |
| 4050 | if (basebase->tp_as_buffer == NULL) |
| 4051 | basebase = NULL; |
| 4052 | COPYBUF(bf_getbuffer); |
| 4053 | COPYBUF(bf_releasebuffer); |
| 4054 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | COPYSLOT(tp_dealloc); |
| 4059 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 4060 | type->tp_getattr = base->tp_getattr; |
| 4061 | type->tp_getattro = base->tp_getattro; |
| 4062 | } |
| 4063 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 4064 | type->tp_setattr = base->tp_setattr; |
| 4065 | type->tp_setattro = base->tp_setattro; |
| 4066 | } |
| 4067 | /* tp_reserved is ignored */ |
| 4068 | COPYSLOT(tp_repr); |
| 4069 | /* tp_hash see tp_richcompare */ |
| 4070 | COPYSLOT(tp_call); |
| 4071 | COPYSLOT(tp_str); |
| 4072 | { |
| 4073 | /* Copy comparison-related slots only when |
| 4074 | not overriding them anywhere */ |
| 4075 | if (type->tp_richcompare == NULL && |
| 4076 | type->tp_hash == NULL && |
| 4077 | !overrides_hash(type)) |
| 4078 | { |
| 4079 | type->tp_richcompare = base->tp_richcompare; |
| 4080 | type->tp_hash = base->tp_hash; |
| 4081 | } |
| 4082 | } |
| 4083 | { |
| 4084 | COPYSLOT(tp_iter); |
| 4085 | COPYSLOT(tp_iternext); |
| 4086 | } |
| 4087 | { |
| 4088 | COPYSLOT(tp_descr_get); |
| 4089 | COPYSLOT(tp_descr_set); |
| 4090 | COPYSLOT(tp_dictoffset); |
| 4091 | COPYSLOT(tp_init); |
| 4092 | COPYSLOT(tp_alloc); |
| 4093 | COPYSLOT(tp_is_gc); |
| 4094 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 4095 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 4096 | /* They agree about gc. */ |
| 4097 | COPYSLOT(tp_free); |
| 4098 | } |
| 4099 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4100 | type->tp_free == NULL && |
| 4101 | base->tp_free == PyObject_Free) { |
| 4102 | /* A bit of magic to plug in the correct default |
| 4103 | * tp_free function when a derived class adds gc, |
| 4104 | * didn't define tp_free, and the base uses the |
| 4105 | * default non-gc tp_free. |
| 4106 | */ |
| 4107 | type->tp_free = PyObject_GC_Del; |
| 4108 | } |
| 4109 | /* else they didn't agree about gc, and there isn't something |
| 4110 | * obvious to be done -- the type is on its own. |
| 4111 | */ |
| 4112 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4115 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4116 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4117 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 4118 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | PyObject *dict, *bases; |
| 4121 | PyTypeObject *base; |
| 4122 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4124 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 4125 | assert(type->tp_dict != NULL); |
| 4126 | return 0; |
| 4127 | } |
| 4128 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4131 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4132 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4133 | /* PyType_Ready is the closest thing we have to a choke point |
| 4134 | * for type objects, so is the best place I can think of to try |
| 4135 | * to get type objects into the doubly-linked list of all objects. |
| 4136 | * Still, not all type objects go thru PyType_Ready. |
| 4137 | */ |
| 4138 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4139 | #endif |
| 4140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4142 | base = type->tp_base; |
| 4143 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4144 | base = type->tp_base = &PyBaseObject_Type; |
| 4145 | Py_INCREF(base); |
| 4146 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4148 | /* Now the only way base can still be NULL is if type is |
| 4149 | * &PyBaseObject_Type. |
| 4150 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4152 | /* Initialize the base class */ |
| 4153 | if (base != NULL && base->tp_dict == NULL) { |
| 4154 | if (PyType_Ready(base) < 0) |
| 4155 | goto error; |
| 4156 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4159 | compilable separately on Windows can call PyType_Ready() instead of |
| 4160 | initializing the ob_type field of their type objects. */ |
| 4161 | /* The test for base != NULL is really unnecessary, since base is only |
| 4162 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4163 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4164 | know that. */ |
| 4165 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4166 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 | /* Initialize tp_bases */ |
| 4169 | bases = type->tp_bases; |
| 4170 | if (bases == NULL) { |
| 4171 | if (base == NULL) |
| 4172 | bases = PyTuple_New(0); |
| 4173 | else |
| 4174 | bases = PyTuple_Pack(1, base); |
| 4175 | if (bases == NULL) |
| 4176 | goto error; |
| 4177 | type->tp_bases = bases; |
| 4178 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | /* Initialize tp_dict */ |
| 4181 | dict = type->tp_dict; |
| 4182 | if (dict == NULL) { |
| 4183 | dict = PyDict_New(); |
| 4184 | if (dict == NULL) |
| 4185 | goto error; |
| 4186 | type->tp_dict = dict; |
| 4187 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4189 | /* Add type-specific descriptors to tp_dict */ |
| 4190 | if (add_operators(type) < 0) |
| 4191 | goto error; |
| 4192 | if (type->tp_methods != NULL) { |
| 4193 | if (add_methods(type, type->tp_methods) < 0) |
| 4194 | goto error; |
| 4195 | } |
| 4196 | if (type->tp_members != NULL) { |
| 4197 | if (add_members(type, type->tp_members) < 0) |
| 4198 | goto error; |
| 4199 | } |
| 4200 | if (type->tp_getset != NULL) { |
| 4201 | if (add_getset(type, type->tp_getset) < 0) |
| 4202 | goto error; |
| 4203 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | /* Calculate method resolution order */ |
| 4206 | if (mro_internal(type) < 0) { |
| 4207 | goto error; |
| 4208 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | /* Inherit special flags from dominant base */ |
| 4211 | if (type->tp_base != NULL) |
| 4212 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | /* Initialize tp_dict properly */ |
| 4215 | bases = type->tp_mro; |
| 4216 | assert(bases != NULL); |
| 4217 | assert(PyTuple_Check(bases)); |
| 4218 | n = PyTuple_GET_SIZE(bases); |
| 4219 | for (i = 1; i < n; i++) { |
| 4220 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4221 | if (PyType_Check(b)) |
| 4222 | inherit_slots(type, (PyTypeObject *)b); |
| 4223 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4225 | /* Sanity check for tp_free. */ |
| 4226 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4227 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4228 | /* This base class needs to call tp_free, but doesn't have |
| 4229 | * one, or its tp_free is for non-gc'ed objects. |
| 4230 | */ |
| 4231 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4232 | "gc and is a base type but has inappropriate " |
| 4233 | "tp_free slot", |
| 4234 | type->tp_name); |
| 4235 | goto error; |
| 4236 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4239 | the tp_doc slot. |
| 4240 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4241 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4242 | if (type->tp_doc != NULL) { |
| 4243 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 4244 | if (doc == NULL) |
| 4245 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4246 | _PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | Py_DECREF(doc); |
| 4248 | } else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4249 | _PyDict_SetItemId(type->tp_dict, |
| 4250 | &PyId___doc__, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4254 | /* Hack for tp_hash and __hash__. |
| 4255 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4256 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4257 | tp_dict['__hash__'] equal to None. |
| 4258 | This signals that __hash__ is not inherited. |
| 4259 | */ |
| 4260 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4261 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 4262 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | goto error; |
| 4264 | type->tp_hash = PyObject_HashNotImplemented; |
| 4265 | } |
| 4266 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | /* Some more special stuff */ |
| 4269 | base = type->tp_base; |
| 4270 | if (base != NULL) { |
| 4271 | if (type->tp_as_number == NULL) |
| 4272 | type->tp_as_number = base->tp_as_number; |
| 4273 | if (type->tp_as_sequence == NULL) |
| 4274 | type->tp_as_sequence = base->tp_as_sequence; |
| 4275 | if (type->tp_as_mapping == NULL) |
| 4276 | type->tp_as_mapping = base->tp_as_mapping; |
| 4277 | if (type->tp_as_buffer == NULL) |
| 4278 | type->tp_as_buffer = base->tp_as_buffer; |
| 4279 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | /* Link into each base class's list of subclasses */ |
| 4282 | bases = type->tp_bases; |
| 4283 | n = PyTuple_GET_SIZE(bases); |
| 4284 | for (i = 0; i < n; i++) { |
| 4285 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4286 | if (PyType_Check(b) && |
| 4287 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4288 | goto error; |
| 4289 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4291 | /* Warn for a type that implements tp_compare (now known as |
| 4292 | tp_reserved) but not tp_richcompare. */ |
| 4293 | if (type->tp_reserved && !type->tp_richcompare) { |
| 4294 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4295 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 4296 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4297 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4298 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | if (error == -1) |
| 4300 | goto error; |
| 4301 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | /* All done -- set the ready flag */ |
| 4304 | assert(type->tp_dict != NULL); |
| 4305 | type->tp_flags = |
| 4306 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4307 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4308 | |
| 4309 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4311 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4314 | static int |
| 4315 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | Py_ssize_t i; |
| 4318 | int result; |
| 4319 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | list = base->tp_subclasses; |
| 4322 | if (list == NULL) { |
| 4323 | base->tp_subclasses = list = PyList_New(0); |
| 4324 | if (list == NULL) |
| 4325 | return -1; |
| 4326 | } |
| 4327 | assert(PyList_Check(list)); |
| 4328 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 4329 | i = PyList_GET_SIZE(list); |
| 4330 | while (--i >= 0) { |
| 4331 | ref = PyList_GET_ITEM(list, i); |
| 4332 | assert(PyWeakref_CheckRef(ref)); |
| 4333 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 4334 | return PyList_SetItem(list, i, newobj); |
| 4335 | } |
| 4336 | result = PyList_Append(list, newobj); |
| 4337 | Py_DECREF(newobj); |
| 4338 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4341 | static void |
| 4342 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4344 | Py_ssize_t i; |
| 4345 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | list = base->tp_subclasses; |
| 4348 | if (list == NULL) { |
| 4349 | return; |
| 4350 | } |
| 4351 | assert(PyList_Check(list)); |
| 4352 | i = PyList_GET_SIZE(list); |
| 4353 | while (--i >= 0) { |
| 4354 | ref = PyList_GET_ITEM(list, i); |
| 4355 | assert(PyWeakref_CheckRef(ref)); |
| 4356 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4357 | /* this can't fail, right? */ |
| 4358 | PySequence_DelItem(list, i); |
| 4359 | return; |
| 4360 | } |
| 4361 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4362 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4363 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4364 | static int |
| 4365 | check_num_args(PyObject *ob, int n) |
| 4366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4367 | if (!PyTuple_CheckExact(ob)) { |
| 4368 | PyErr_SetString(PyExc_SystemError, |
| 4369 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4370 | return 0; |
| 4371 | } |
| 4372 | if (n == PyTuple_GET_SIZE(ob)) |
| 4373 | return 1; |
| 4374 | PyErr_Format( |
| 4375 | PyExc_TypeError, |
| 4376 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4377 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4380 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4381 | |
| 4382 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4384 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4385 | Most tables have only one entry; the tables for binary operators have two |
| 4386 | entries, one regular and one with reversed arguments. */ |
| 4387 | |
| 4388 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4389 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | lenfunc func = (lenfunc)wrapped; |
| 4392 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | if (!check_num_args(args, 0)) |
| 4395 | return NULL; |
| 4396 | res = (*func)(self); |
| 4397 | if (res == -1 && PyErr_Occurred()) |
| 4398 | return NULL; |
| 4399 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4402 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4403 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | inquiry func = (inquiry)wrapped; |
| 4406 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | if (!check_num_args(args, 0)) |
| 4409 | return NULL; |
| 4410 | res = (*func)(self); |
| 4411 | if (res == -1 && PyErr_Occurred()) |
| 4412 | return NULL; |
| 4413 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4417 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | binaryfunc func = (binaryfunc)wrapped; |
| 4420 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | if (!check_num_args(args, 1)) |
| 4423 | return NULL; |
| 4424 | other = PyTuple_GET_ITEM(args, 0); |
| 4425 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4429 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | binaryfunc func = (binaryfunc)wrapped; |
| 4432 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | if (!check_num_args(args, 1)) |
| 4435 | return NULL; |
| 4436 | other = PyTuple_GET_ITEM(args, 0); |
| 4437 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4441 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4443 | binaryfunc func = (binaryfunc)wrapped; |
| 4444 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | if (!check_num_args(args, 1)) |
| 4447 | return NULL; |
| 4448 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4449 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4452 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4453 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4455 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4456 | PyObject *other; |
| 4457 | PyObject *third = Py_None; |
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 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4462 | return NULL; |
| 4463 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4466 | static PyObject * |
| 4467 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4470 | PyObject *other; |
| 4471 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4476 | return NULL; |
| 4477 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4480 | static PyObject * |
| 4481 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | if (!check_num_args(args, 0)) |
| 4486 | return NULL; |
| 4487 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4490 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4491 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4494 | PyObject* o; |
| 4495 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4498 | return NULL; |
| 4499 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4500 | if (i == -1 && PyErr_Occurred()) |
| 4501 | return NULL; |
| 4502 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4505 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4506 | getindex(PyObject *self, PyObject *arg) |
| 4507 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4511 | if (i == -1 && PyErr_Occurred()) |
| 4512 | return -1; |
| 4513 | if (i < 0) { |
| 4514 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4515 | if (sq && sq->sq_length) { |
| 4516 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4517 | if (n < 0) |
| 4518 | return -1; |
| 4519 | i += n; |
| 4520 | } |
| 4521 | } |
| 4522 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | static PyObject * |
| 4526 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4527 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4529 | PyObject *arg; |
| 4530 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4533 | arg = PyTuple_GET_ITEM(args, 0); |
| 4534 | i = getindex(self, arg); |
| 4535 | if (i == -1 && PyErr_Occurred()) |
| 4536 | return NULL; |
| 4537 | return (*func)(self, i); |
| 4538 | } |
| 4539 | check_num_args(args, 1); |
| 4540 | assert(PyErr_Occurred()); |
| 4541 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4544 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4545 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4548 | Py_ssize_t i; |
| 4549 | int res; |
| 4550 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4553 | return NULL; |
| 4554 | i = getindex(self, arg); |
| 4555 | if (i == -1 && PyErr_Occurred()) |
| 4556 | return NULL; |
| 4557 | res = (*func)(self, i, value); |
| 4558 | if (res == -1 && PyErr_Occurred()) |
| 4559 | return NULL; |
| 4560 | Py_INCREF(Py_None); |
| 4561 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4564 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4565 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4568 | Py_ssize_t i; |
| 4569 | int res; |
| 4570 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4572 | if (!check_num_args(args, 1)) |
| 4573 | return NULL; |
| 4574 | arg = PyTuple_GET_ITEM(args, 0); |
| 4575 | i = getindex(self, arg); |
| 4576 | if (i == -1 && PyErr_Occurred()) |
| 4577 | return NULL; |
| 4578 | res = (*func)(self, i, NULL); |
| 4579 | if (res == -1 && PyErr_Occurred()) |
| 4580 | return NULL; |
| 4581 | Py_INCREF(Py_None); |
| 4582 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4585 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4586 | static PyObject * |
| 4587 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | objobjproc func = (objobjproc)wrapped; |
| 4590 | int res; |
| 4591 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4593 | if (!check_num_args(args, 1)) |
| 4594 | return NULL; |
| 4595 | value = PyTuple_GET_ITEM(args, 0); |
| 4596 | res = (*func)(self, value); |
| 4597 | if (res == -1 && PyErr_Occurred()) |
| 4598 | return NULL; |
| 4599 | else |
| 4600 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4603 | static PyObject * |
| 4604 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | objobjargproc func = (objobjargproc)wrapped; |
| 4607 | int res; |
| 4608 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4611 | return NULL; |
| 4612 | res = (*func)(self, key, value); |
| 4613 | if (res == -1 && PyErr_Occurred()) |
| 4614 | return NULL; |
| 4615 | Py_INCREF(Py_None); |
| 4616 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4619 | static PyObject * |
| 4620 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | objobjargproc func = (objobjargproc)wrapped; |
| 4623 | int res; |
| 4624 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | if (!check_num_args(args, 1)) |
| 4627 | return NULL; |
| 4628 | key = PyTuple_GET_ITEM(args, 0); |
| 4629 | res = (*func)(self, key, NULL); |
| 4630 | if (res == -1 && PyErr_Occurred()) |
| 4631 | return NULL; |
| 4632 | Py_INCREF(Py_None); |
| 4633 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4636 | /* 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] | 4637 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4638 | static int |
| 4639 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | PyTypeObject *type = Py_TYPE(self); |
| 4642 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4643 | type = type->tp_base; |
| 4644 | /* If type is NULL now, this is a really weird type. |
| 4645 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4646 | if (type && type->tp_setattro != func) { |
| 4647 | PyErr_Format(PyExc_TypeError, |
| 4648 | "can't apply this %s to %s object", |
| 4649 | what, |
| 4650 | type->tp_name); |
| 4651 | return 0; |
| 4652 | } |
| 4653 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4656 | static PyObject * |
| 4657 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | setattrofunc func = (setattrofunc)wrapped; |
| 4660 | int res; |
| 4661 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4664 | return NULL; |
| 4665 | if (!hackcheck(self, func, "__setattr__")) |
| 4666 | return NULL; |
| 4667 | res = (*func)(self, name, value); |
| 4668 | if (res < 0) |
| 4669 | return NULL; |
| 4670 | Py_INCREF(Py_None); |
| 4671 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | static PyObject * |
| 4675 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | setattrofunc func = (setattrofunc)wrapped; |
| 4678 | int res; |
| 4679 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | if (!check_num_args(args, 1)) |
| 4682 | return NULL; |
| 4683 | name = PyTuple_GET_ITEM(args, 0); |
| 4684 | if (!hackcheck(self, func, "__delattr__")) |
| 4685 | return NULL; |
| 4686 | res = (*func)(self, name, NULL); |
| 4687 | if (res < 0) |
| 4688 | return NULL; |
| 4689 | Py_INCREF(Py_None); |
| 4690 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4693 | static PyObject * |
| 4694 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4697 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | if (!check_num_args(args, 0)) |
| 4700 | return NULL; |
| 4701 | res = (*func)(self); |
| 4702 | if (res == -1 && PyErr_Occurred()) |
| 4703 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4704 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4707 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4708 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4710 | ternaryfunc func = (ternaryfunc)wrapped; |
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 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4715 | static PyObject * |
| 4716 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4719 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | if (!check_num_args(args, 1)) |
| 4722 | return NULL; |
| 4723 | other = PyTuple_GET_ITEM(args, 0); |
| 4724 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | #undef RICHCMP_WRAPPER |
| 4728 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4729 | static PyObject * \ |
| 4730 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4731 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4732 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4735 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4736 | RICHCMP_WRAPPER(le, Py_LE) |
| 4737 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4738 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4739 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4740 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4741 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4742 | static PyObject * |
| 4743 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | unaryfunc func = (unaryfunc)wrapped; |
| 4746 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4748 | if (!check_num_args(args, 0)) |
| 4749 | return NULL; |
| 4750 | res = (*func)(self); |
| 4751 | if (res == NULL && !PyErr_Occurred()) |
| 4752 | PyErr_SetNone(PyExc_StopIteration); |
| 4753 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4756 | static PyObject * |
| 4757 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4760 | PyObject *obj; |
| 4761 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4764 | return NULL; |
| 4765 | if (obj == Py_None) |
| 4766 | obj = NULL; |
| 4767 | if (type == Py_None) |
| 4768 | type = NULL; |
| 4769 | if (type == NULL &&obj == NULL) { |
| 4770 | PyErr_SetString(PyExc_TypeError, |
| 4771 | "__get__(None, None) is invalid"); |
| 4772 | return NULL; |
| 4773 | } |
| 4774 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4777 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4778 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4781 | PyObject *obj, *value; |
| 4782 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4785 | return NULL; |
| 4786 | ret = (*func)(self, obj, value); |
| 4787 | if (ret < 0) |
| 4788 | return NULL; |
| 4789 | Py_INCREF(Py_None); |
| 4790 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4791 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4792 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4793 | static PyObject * |
| 4794 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4797 | PyObject *obj; |
| 4798 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | if (!check_num_args(args, 1)) |
| 4801 | return NULL; |
| 4802 | obj = PyTuple_GET_ITEM(args, 0); |
| 4803 | ret = (*func)(self, obj, NULL); |
| 4804 | if (ret < 0) |
| 4805 | return NULL; |
| 4806 | Py_INCREF(Py_None); |
| 4807 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4808 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4809 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4810 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4811 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | if (func(self, args, kwds) < 0) |
| 4816 | return NULL; |
| 4817 | Py_INCREF(Py_None); |
| 4818 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4821 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4822 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4824 | PyTypeObject *type, *subtype, *staticbase; |
| 4825 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | if (self == NULL || !PyType_Check(self)) |
| 4828 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4829 | type = (PyTypeObject *)self; |
| 4830 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4831 | PyErr_Format(PyExc_TypeError, |
| 4832 | "%s.__new__(): not enough arguments", |
| 4833 | type->tp_name); |
| 4834 | return NULL; |
| 4835 | } |
| 4836 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4837 | if (!PyType_Check(arg0)) { |
| 4838 | PyErr_Format(PyExc_TypeError, |
| 4839 | "%s.__new__(X): X is not a type object (%s)", |
| 4840 | type->tp_name, |
| 4841 | Py_TYPE(arg0)->tp_name); |
| 4842 | return NULL; |
| 4843 | } |
| 4844 | subtype = (PyTypeObject *)arg0; |
| 4845 | if (!PyType_IsSubtype(subtype, type)) { |
| 4846 | PyErr_Format(PyExc_TypeError, |
| 4847 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4848 | type->tp_name, |
| 4849 | subtype->tp_name, |
| 4850 | subtype->tp_name, |
| 4851 | type->tp_name); |
| 4852 | return NULL; |
| 4853 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | /* Check that the use doesn't do something silly and unsafe like |
| 4856 | object.__new__(dict). To do this, we check that the |
| 4857 | most derived base that's not a heap type is this type. */ |
| 4858 | staticbase = subtype; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 4859 | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4860 | staticbase = staticbase->tp_base; |
| 4861 | /* If staticbase is NULL now, it is a really weird type. |
| 4862 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4863 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4864 | PyErr_Format(PyExc_TypeError, |
| 4865 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4866 | type->tp_name, |
| 4867 | subtype->tp_name, |
| 4868 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4869 | return NULL; |
| 4870 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4873 | if (args == NULL) |
| 4874 | return NULL; |
| 4875 | res = type->tp_new(subtype, args, kwds); |
| 4876 | Py_DECREF(args); |
| 4877 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4880 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4882 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4883 | "a new object with type S, a subtype of T")}, |
| 4884 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4885 | }; |
| 4886 | |
| 4887 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4888 | add_tp_new_wrapper(PyTypeObject *type) |
| 4889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4890 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4891 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4892 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | return 0; |
| 4894 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4895 | if (func == NULL) |
| 4896 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4897 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4898 | Py_DECREF(func); |
| 4899 | return -1; |
| 4900 | } |
| 4901 | Py_DECREF(func); |
| 4902 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4905 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4906 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4907 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4908 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4909 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4910 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4911 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4912 | _Py_static_string(id, OPSTR); \ |
| 4913 | return call_method(self, &id, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4916 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4917 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4918 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4919 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4920 | _Py_static_string(id, OPSTR); \ |
| 4921 | return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4924 | /* Boolean helper for SLOT1BINFULL(). |
| 4925 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4926 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4927 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4928 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4929 | PyObject *a, *b; |
| 4930 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4931 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4932 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4933 | if (b == NULL) { |
| 4934 | PyErr_Clear(); |
| 4935 | /* If right doesn't have it, it's not overloaded */ |
| 4936 | return 0; |
| 4937 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4938 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4939 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4940 | if (a == NULL) { |
| 4941 | PyErr_Clear(); |
| 4942 | Py_DECREF(b); |
| 4943 | /* If right has it but left doesn't, it's overloaded */ |
| 4944 | return 1; |
| 4945 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4948 | Py_DECREF(a); |
| 4949 | Py_DECREF(b); |
| 4950 | if (ok < 0) { |
| 4951 | PyErr_Clear(); |
| 4952 | return 0; |
| 4953 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4955 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4958 | |
| 4959 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4960 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4961 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4962 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4963 | _Py_static_string(op_id, OPSTR); \ |
| 4964 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4965 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4966 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4967 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4968 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4969 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4970 | PyObject *r; \ |
| 4971 | if (do_other && \ |
| 4972 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4973 | method_is_overloaded(self, other, &rop_id)) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4974 | r = call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | if (r != Py_NotImplemented) \ |
| 4976 | return r; \ |
| 4977 | Py_DECREF(r); \ |
| 4978 | do_other = 0; \ |
| 4979 | } \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4980 | r = call_maybe(self, &op_id, "(O)", other); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | if (r != Py_NotImplemented || \ |
| 4982 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4983 | return r; \ |
| 4984 | Py_DECREF(r); \ |
| 4985 | } \ |
| 4986 | if (do_other) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4987 | return call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4989 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4994 | |
| 4995 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4996 | static PyObject * \ |
| 4997 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4998 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4999 | _Py_static_string(id, #OPSTR); \ |
| 5000 | return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5003 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5004 | slot_sq_length(PyObject *self) |
| 5005 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5006 | _Py_IDENTIFIER(__len__); |
| 5007 | PyObject *res = call_method(self, &PyId___len__, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | if (res == NULL) |
| 5011 | return -1; |
| 5012 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 5013 | Py_DECREF(res); |
| 5014 | if (len < 0) { |
| 5015 | if (!PyErr_Occurred()) |
| 5016 | PyErr_SetString(PyExc_ValueError, |
| 5017 | "__len__() should return >= 0"); |
| 5018 | return -1; |
| 5019 | } |
| 5020 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5023 | /* Super-optimized version of slot_sq_item. |
| 5024 | Other slots could do the same... */ |
| 5025 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5026 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 5029 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5030 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5031 | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | if (func != NULL) { |
| 5033 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 5034 | Py_INCREF(func); |
| 5035 | else { |
| 5036 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 5037 | if (func == NULL) { |
| 5038 | return NULL; |
| 5039 | } |
| 5040 | } |
| 5041 | ival = PyLong_FromSsize_t(i); |
| 5042 | if (ival != NULL) { |
| 5043 | args = PyTuple_New(1); |
| 5044 | if (args != NULL) { |
| 5045 | PyTuple_SET_ITEM(args, 0, ival); |
| 5046 | retval = PyObject_Call(func, args, NULL); |
| 5047 | Py_XDECREF(args); |
| 5048 | Py_XDECREF(func); |
| 5049 | return retval; |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5054 | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 5056 | } |
| 5057 | Py_XDECREF(args); |
| 5058 | Py_XDECREF(ival); |
| 5059 | Py_XDECREF(func); |
| 5060 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5063 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5064 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5066 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5067 | _Py_IDENTIFIER(__delitem__); |
| 5068 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5071 | res = call_method(self, &PyId___delitem__, "(n)", index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5072 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5073 | res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | if (res == NULL) |
| 5075 | return -1; |
| 5076 | Py_DECREF(res); |
| 5077 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5081 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | PyObject *func, *res, *args; |
| 5084 | int result = -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5085 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5086 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5087 | func = lookup_maybe(self, &PyId___contains__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | if (func != NULL) { |
| 5089 | args = PyTuple_Pack(1, value); |
| 5090 | if (args == NULL) |
| 5091 | res = NULL; |
| 5092 | else { |
| 5093 | res = PyObject_Call(func, args, NULL); |
| 5094 | Py_DECREF(args); |
| 5095 | } |
| 5096 | Py_DECREF(func); |
| 5097 | if (res != NULL) { |
| 5098 | result = PyObject_IsTrue(res); |
| 5099 | Py_DECREF(res); |
| 5100 | } |
| 5101 | } |
| 5102 | else if (! PyErr_Occurred()) { |
| 5103 | /* Possible results: -1 and 1 */ |
| 5104 | result = (int)_PySequence_IterSearch(self, value, |
| 5105 | PY_ITERSEARCH_CONTAINS); |
| 5106 | } |
| 5107 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5110 | #define slot_mp_length slot_sq_length |
| 5111 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5112 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5113 | |
| 5114 | static int |
| 5115 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5118 | _Py_IDENTIFIER(__delitem__); |
| 5119 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5122 | res = call_method(self, &PyId___delitem__, "(O)", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5123 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5124 | res = call_method(self, &PyId___setitem__, "(OO)", key, value); |
| 5125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | if (res == NULL) |
| 5127 | return -1; |
| 5128 | Py_DECREF(res); |
| 5129 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5132 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5133 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5134 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5135 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5136 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5137 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5138 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5139 | |
| 5140 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5141 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5142 | |
| 5143 | static PyObject * |
| 5144 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5145 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5146 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | if (modulus == Py_None) |
| 5149 | return slot_nb_power_binary(self, other); |
| 5150 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5151 | can call this when the second argument's type uses |
| 5152 | slot_nb_power, so check before calling self.__pow__. */ |
| 5153 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5154 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5155 | return call_method(self, &PyId___pow__, "(OO)", other, modulus); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5156 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5157 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | SLOT0(slot_nb_negative, "__neg__") |
| 5161 | SLOT0(slot_nb_positive, "__pos__") |
| 5162 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5163 | |
| 5164 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5165 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5167 | PyObject *func, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | int result = -1; |
| 5169 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5170 | _Py_IDENTIFIER(__len__); |
| 5171 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5172 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5173 | func = lookup_maybe(self, &PyId___bool__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | if (func == NULL) { |
| 5175 | if (PyErr_Occurred()) |
| 5176 | return -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5177 | func = lookup_maybe(self, &PyId___len__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | if (func == NULL) |
| 5179 | return PyErr_Occurred() ? -1 : 1; |
| 5180 | using_len = 1; |
| 5181 | } |
| 5182 | args = PyTuple_New(0); |
| 5183 | if (args != NULL) { |
| 5184 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5185 | Py_DECREF(args); |
| 5186 | if (temp != NULL) { |
| 5187 | if (using_len) { |
| 5188 | /* enforced by slot_nb_len */ |
| 5189 | result = PyObject_IsTrue(temp); |
| 5190 | } |
| 5191 | else if (PyBool_Check(temp)) { |
| 5192 | result = PyObject_IsTrue(temp); |
| 5193 | } |
| 5194 | else { |
| 5195 | PyErr_Format(PyExc_TypeError, |
| 5196 | "__bool__ should return " |
| 5197 | "bool, returned %s", |
| 5198 | Py_TYPE(temp)->tp_name); |
| 5199 | result = -1; |
| 5200 | } |
| 5201 | Py_DECREF(temp); |
| 5202 | } |
| 5203 | } |
| 5204 | Py_DECREF(func); |
| 5205 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5208 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5209 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5210 | slot_nb_index(PyObject *self) |
| 5211 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5212 | _Py_IDENTIFIER(__index__); |
| 5213 | return call_method(self, &PyId___index__, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5217 | SLOT0(slot_nb_invert, "__invert__") |
| 5218 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5219 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5220 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5221 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5222 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5223 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5224 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5225 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5226 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5227 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5228 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5229 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5230 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5231 | static PyObject * |
| 5232 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5233 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5234 | _Py_IDENTIFIER(__ipow__); |
| 5235 | return call_method(self, &PyId___ipow__, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5236 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5237 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5238 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5239 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5240 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5241 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5242 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5243 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5244 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5245 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5246 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5247 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5248 | static PyObject * |
| 5249 | slot_tp_repr(PyObject *self) |
| 5250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5252 | _Py_IDENTIFIER(__repr__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5253 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5254 | func = lookup_method(self, &PyId___repr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | if (func != NULL) { |
| 5256 | res = PyEval_CallObject(func, NULL); |
| 5257 | Py_DECREF(func); |
| 5258 | return res; |
| 5259 | } |
| 5260 | PyErr_Clear(); |
| 5261 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5262 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | static PyObject * |
| 5266 | slot_tp_str(PyObject *self) |
| 5267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5269 | _Py_IDENTIFIER(__str__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5270 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5271 | func = lookup_method(self, &PyId___str__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | if (func != NULL) { |
| 5273 | res = PyEval_CallObject(func, NULL); |
| 5274 | Py_DECREF(func); |
| 5275 | return res; |
| 5276 | } |
| 5277 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5278 | /* PyObject *ress; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | PyErr_Clear(); |
| 5280 | res = slot_tp_repr(self); |
| 5281 | if (!res) |
| 5282 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5283 | /* XXX this is non-sensical. Why should we return |
| 5284 | a bytes object from __str__. Is this code even |
| 5285 | used? - mvl */ |
| 5286 | assert(0); |
| 5287 | return res; |
| 5288 | /* |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 5289 | ress = _PyUnicode_AsDefaultEncodedString(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | Py_DECREF(res); |
| 5291 | return ress; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5292 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5293 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5294 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5295 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5296 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5297 | slot_tp_hash(PyObject *self) |
| 5298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5299 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5300 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5301 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5302 | func = lookup_method(self, &PyId___hash__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | if (func == Py_None) { |
| 5305 | Py_DECREF(func); |
| 5306 | func = NULL; |
| 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 | if (func == NULL) { |
| 5310 | return PyObject_HashNotImplemented(self); |
| 5311 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5313 | res = PyEval_CallObject(func, NULL); |
| 5314 | Py_DECREF(func); |
| 5315 | if (res == NULL) |
| 5316 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5317 | |
| 5318 | if (!PyLong_Check(res)) { |
| 5319 | PyErr_SetString(PyExc_TypeError, |
| 5320 | "__hash__ method should return an integer"); |
| 5321 | return -1; |
| 5322 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5323 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5324 | hashable Python object x, hash(x) will always lie within the range of |
| 5325 | Py_hash_t. Therefore our transformation must preserve values that |
| 5326 | already lie within this range, to ensure that if x.__hash__() returns |
| 5327 | hash(y) then hash(x) == hash(y). */ |
| 5328 | h = PyLong_AsSsize_t(res); |
| 5329 | if (h == -1 && PyErr_Occurred()) { |
| 5330 | /* 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] | 5331 | use any sufficiently bit-mixing transformation; |
| 5332 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5333 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5334 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5335 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5336 | /* -1 is reserved for errors. */ |
| 5337 | if (h == -1) |
| 5338 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5340 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | static PyObject * |
| 5344 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5345 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5346 | _Py_IDENTIFIER(__call__); |
| 5347 | PyObject *meth = lookup_method(self, &PyId___call__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5350 | if (meth == NULL) |
| 5351 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5355 | Py_DECREF(meth); |
| 5356 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5359 | /* There are two slot dispatch functions for tp_getattro. |
| 5360 | |
| 5361 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5362 | but no __getattr__ hook is present; |
| 5363 | |
| 5364 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5365 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5366 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5367 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5368 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5369 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5370 | static PyObject * |
| 5371 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5372 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5373 | return call_method(self, &PyId___getattribute__, "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5376 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5377 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5379 | PyObject *res, *descr = NULL; |
| 5380 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5382 | if (f != NULL) { |
| 5383 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5384 | if (descr == NULL) |
| 5385 | return NULL; |
| 5386 | else |
| 5387 | attr = descr; |
| 5388 | } |
| 5389 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5390 | Py_XDECREF(descr); |
| 5391 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5395 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | PyTypeObject *tp = Py_TYPE(self); |
| 5398 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5399 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5401 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5402 | method fully for each attribute lookup for classes with |
| 5403 | __getattr__, even when the attribute is present. So we use |
| 5404 | _PyType_Lookup and create the method only when needed, with |
| 5405 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5406 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | if (getattr == NULL) { |
| 5408 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5409 | tp->tp_getattro = slot_tp_getattro; |
| 5410 | return slot_tp_getattro(self, name); |
| 5411 | } |
| 5412 | Py_INCREF(getattr); |
| 5413 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5414 | method fully for each attribute lookup for classes with |
| 5415 | __getattr__, even when self has the default __getattribute__ |
| 5416 | method. So we use _PyType_Lookup and create the method only when |
| 5417 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5418 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | if (getattribute == NULL || |
| 5420 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5421 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5422 | (void *)PyObject_GenericGetAttr)) |
| 5423 | res = PyObject_GenericGetAttr(self, name); |
| 5424 | else { |
| 5425 | Py_INCREF(getattribute); |
| 5426 | res = call_attribute(self, getattribute, name); |
| 5427 | Py_DECREF(getattribute); |
| 5428 | } |
| 5429 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5430 | PyErr_Clear(); |
| 5431 | res = call_attribute(self, getattr, name); |
| 5432 | } |
| 5433 | Py_DECREF(getattr); |
| 5434 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5437 | static int |
| 5438 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5441 | _Py_IDENTIFIER(__delattr__); |
| 5442 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5445 | res = call_method(self, &PyId___delattr__, "(O)", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5446 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5447 | res = call_method(self, &PyId___setattr__, "(OO)", name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5448 | if (res == NULL) |
| 5449 | return -1; |
| 5450 | Py_DECREF(res); |
| 5451 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5454 | static _Py_Identifier name_op[] = { |
| 5455 | {0, "__lt__", 0}, |
| 5456 | {0, "__le__", 0}, |
| 5457 | {0, "__eq__", 0}, |
| 5458 | {0, "__ne__", 0}, |
| 5459 | {0, "__gt__", 0}, |
| 5460 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5461 | }; |
| 5462 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5463 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5464 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | PyObject *func, *args, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5467 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5468 | func = lookup_method(self, &name_op[op]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | if (func == NULL) { |
| 5470 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5471 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5472 | } |
| 5473 | args = PyTuple_Pack(1, other); |
| 5474 | if (args == NULL) |
| 5475 | res = NULL; |
| 5476 | else { |
| 5477 | res = PyObject_Call(func, args, NULL); |
| 5478 | Py_DECREF(args); |
| 5479 | } |
| 5480 | Py_DECREF(func); |
| 5481 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5484 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5485 | slot_tp_iter(PyObject *self) |
| 5486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5488 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5489 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5490 | func = lookup_method(self, &PyId___iter__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5491 | if (func != NULL) { |
| 5492 | PyObject *args; |
| 5493 | args = res = PyTuple_New(0); |
| 5494 | if (args != NULL) { |
| 5495 | res = PyObject_Call(func, args, NULL); |
| 5496 | Py_DECREF(args); |
| 5497 | } |
| 5498 | Py_DECREF(func); |
| 5499 | return res; |
| 5500 | } |
| 5501 | PyErr_Clear(); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5502 | func = lookup_method(self, &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | if (func == NULL) { |
| 5504 | PyErr_Format(PyExc_TypeError, |
| 5505 | "'%.200s' object is not iterable", |
| 5506 | Py_TYPE(self)->tp_name); |
| 5507 | return NULL; |
| 5508 | } |
| 5509 | Py_DECREF(func); |
| 5510 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5511 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5512 | |
| 5513 | static PyObject * |
| 5514 | slot_tp_iternext(PyObject *self) |
| 5515 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5516 | _Py_IDENTIFIER(__next__); |
| 5517 | return call_method(self, &PyId___next__, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5520 | static PyObject * |
| 5521 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5523 | PyTypeObject *tp = Py_TYPE(self); |
| 5524 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5525 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5526 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5527 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5528 | if (get == NULL) { |
| 5529 | /* Avoid further slowdowns */ |
| 5530 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5531 | tp->tp_descr_get = NULL; |
| 5532 | Py_INCREF(self); |
| 5533 | return self; |
| 5534 | } |
| 5535 | if (obj == NULL) |
| 5536 | obj = Py_None; |
| 5537 | if (type == NULL) |
| 5538 | type = Py_None; |
| 5539 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5540 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5541 | |
| 5542 | static int |
| 5543 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5546 | _Py_IDENTIFIER(__delete__); |
| 5547 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5550 | res = call_method(self, &PyId___delete__, "(O)", target); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5552 | res = call_method(self, &PyId___set__, "(OO)", target, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | if (res == NULL) |
| 5554 | return -1; |
| 5555 | Py_DECREF(res); |
| 5556 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | static int |
| 5560 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5561 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5562 | _Py_IDENTIFIER(__init__); |
| 5563 | PyObject *meth = lookup_method(self, &PyId___init__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | if (meth == NULL) |
| 5567 | return -1; |
| 5568 | res = PyObject_Call(meth, args, kwds); |
| 5569 | Py_DECREF(meth); |
| 5570 | if (res == NULL) |
| 5571 | return -1; |
| 5572 | if (res != Py_None) { |
| 5573 | PyErr_Format(PyExc_TypeError, |
| 5574 | "__init__() should return None, not '%.200s'", |
| 5575 | Py_TYPE(res)->tp_name); |
| 5576 | Py_DECREF(res); |
| 5577 | return -1; |
| 5578 | } |
| 5579 | Py_DECREF(res); |
| 5580 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | static PyObject * |
| 5584 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5586 | PyObject *func; |
| 5587 | PyObject *newargs, *x; |
| 5588 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5589 | _Py_IDENTIFIER(__new__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5590 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5591 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | if (func == NULL) |
| 5593 | return NULL; |
| 5594 | assert(PyTuple_Check(args)); |
| 5595 | n = PyTuple_GET_SIZE(args); |
| 5596 | newargs = PyTuple_New(n+1); |
| 5597 | if (newargs == NULL) |
| 5598 | return NULL; |
| 5599 | Py_INCREF(type); |
| 5600 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5601 | for (i = 0; i < n; i++) { |
| 5602 | x = PyTuple_GET_ITEM(args, i); |
| 5603 | Py_INCREF(x); |
| 5604 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5605 | } |
| 5606 | x = PyObject_Call(func, newargs, kwds); |
| 5607 | Py_DECREF(newargs); |
| 5608 | Py_DECREF(func); |
| 5609 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5612 | static void |
| 5613 | slot_tp_del(PyObject *self) |
| 5614 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5615 | _Py_IDENTIFIER(__del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5616 | PyObject *del, *res; |
| 5617 | PyObject *error_type, *error_value, *error_traceback; |
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 | /* Temporarily resurrect the object. */ |
| 5620 | assert(self->ob_refcnt == 0); |
| 5621 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5623 | /* Save the current exception, if any. */ |
| 5624 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5626 | /* Execute __del__ method, if any. */ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5627 | del = lookup_maybe(self, &PyId___del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5628 | if (del != NULL) { |
| 5629 | res = PyEval_CallObject(del, NULL); |
| 5630 | if (res == NULL) |
| 5631 | PyErr_WriteUnraisable(del); |
| 5632 | else |
| 5633 | Py_DECREF(res); |
| 5634 | Py_DECREF(del); |
| 5635 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5637 | /* Restore the saved exception. */ |
| 5638 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5640 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5641 | * cause a recursive call. |
| 5642 | */ |
| 5643 | assert(self->ob_refcnt > 0); |
| 5644 | if (--self->ob_refcnt == 0) |
| 5645 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5647 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5648 | * never happened. |
| 5649 | */ |
| 5650 | { |
| 5651 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5652 | _Py_NewReference(self); |
| 5653 | self->ob_refcnt = refcnt; |
| 5654 | } |
| 5655 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5656 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5657 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5658 | * we need to undo that. */ |
| 5659 | _Py_DEC_REFTOTAL; |
| 5660 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5661 | * chain, so no more to do there. |
| 5662 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5663 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5664 | * undone. |
| 5665 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5666 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5667 | --Py_TYPE(self)->tp_frees; |
| 5668 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5669 | #endif |
| 5670 | } |
| 5671 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5672 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5673 | /* |
| 5674 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
| 5675 | |
| 5676 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
| 5677 | which incorporates the additional structures used for numbers, sequences and |
| 5678 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 5679 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
| 5680 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
| 5681 | an all-zero entry. (This table is further initialized in init_slotdefs().) |
| 5682 | */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5683 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5684 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5685 | |
| 5686 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5687 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5688 | #undef ETSLOT |
| 5689 | #undef SQSLOT |
| 5690 | #undef MPSLOT |
| 5691 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5692 | #undef UNSLOT |
| 5693 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5694 | #undef BINSLOT |
| 5695 | #undef RBINSLOT |
| 5696 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5697 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5698 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5699 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5700 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5701 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5702 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5703 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5704 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5705 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5706 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5708 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5710 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5711 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5712 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5713 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5714 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5715 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5716 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5717 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5718 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5719 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5720 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5721 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5723 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5724 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5725 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5726 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5727 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5728 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5729 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5730 | |
| 5731 | static slotdef slotdefs[] = { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5732 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5733 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5734 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5735 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5736 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5737 | "x.__repr__() <==> repr(x)"), |
| 5738 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5739 | "x.__hash__() <==> hash(x)"), |
| 5740 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5741 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5742 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5743 | "x.__str__() <==> str(x)"), |
| 5744 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5745 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5746 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5747 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5748 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5749 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5750 | "x.__delattr__('name') <==> del x.name"), |
| 5751 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5752 | "x.__lt__(y) <==> x<y"), |
| 5753 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5754 | "x.__le__(y) <==> x<=y"), |
| 5755 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5756 | "x.__eq__(y) <==> x==y"), |
| 5757 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5758 | "x.__ne__(y) <==> x!=y"), |
| 5759 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5760 | "x.__gt__(y) <==> x>y"), |
| 5761 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5762 | "x.__ge__(y) <==> x>=y"), |
| 5763 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5764 | "x.__iter__() <==> iter(x)"), |
| 5765 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5766 | "x.__next__() <==> next(x)"), |
| 5767 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5768 | "descr.__get__(obj[, type]) -> value"), |
| 5769 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5770 | "descr.__set__(obj, value)"), |
| 5771 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5772 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5773 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5774 | "x.__init__(...) initializes x; " |
| 5775 | "see help(type(x)) for signature", |
| 5776 | PyWrapperFlag_KEYWORDS), |
| 5777 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5778 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5780 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5781 | "+"), |
| 5782 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5783 | "+"), |
| 5784 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5785 | "-"), |
| 5786 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5787 | "-"), |
| 5788 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5789 | "*"), |
| 5790 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5791 | "*"), |
| 5792 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5793 | "%"), |
| 5794 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5795 | "%"), |
| 5796 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5797 | "divmod(x, y)"), |
| 5798 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5799 | "divmod(y, x)"), |
| 5800 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5801 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5802 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5803 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5804 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5805 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5806 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5807 | "abs(x)"), |
| 5808 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5809 | "x != 0"), |
| 5810 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5811 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5812 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5813 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5814 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5815 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5816 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5817 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5818 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5819 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5820 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5821 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5822 | "int(x)"), |
| 5823 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5824 | "float(x)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5825 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
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("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
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("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
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("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
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("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
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("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
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("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
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("__iand__", nb_inplace_and, slot_nb_inplace_and, |
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 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5842 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5844 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5846 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5847 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5848 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5849 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5850 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5851 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5852 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5853 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5854 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5855 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 5856 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5857 | "x.__len__() <==> len(x)"), |
| 5858 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5859 | wrap_binaryfunc, |
| 5860 | "x.__getitem__(y) <==> x[y]"), |
| 5861 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5862 | wrap_objobjargproc, |
| 5863 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5864 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5865 | wrap_delitem, |
| 5866 | "x.__delitem__(y) <==> del x[y]"), |
| 5867 | |
| 5868 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5869 | "x.__len__() <==> len(x)"), |
| 5870 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5871 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5872 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5873 | user-defined methods has unexpected side-effects, as shown by |
| 5874 | test_descr.notimplemented() */ |
| 5875 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5876 | "x.__add__(y) <==> x+y"), |
| 5877 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5878 | "x.__mul__(n) <==> x*n"), |
| 5879 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5880 | "x.__rmul__(n) <==> n*x"), |
| 5881 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5882 | "x.__getitem__(y) <==> x[y]"), |
| 5883 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5884 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5885 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5886 | "x.__delitem__(y) <==> del x[y]"), |
| 5887 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5888 | "x.__contains__(y) <==> y in x"), |
| 5889 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5890 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5891 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5892 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
| 5893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5894 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5895 | }; |
| 5896 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5897 | /* 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] | 5898 | 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] | 5899 | the offset to the type pointer, since it takes care to indirect through the |
| 5900 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5901 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5902 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5903 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5904 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5905 | char *ptr; |
| 5906 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5908 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5909 | assert(offset >= 0); |
| 5910 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5911 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5912 | ptr = (char *)type->tp_as_sequence; |
| 5913 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5914 | } |
| 5915 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5916 | ptr = (char *)type->tp_as_mapping; |
| 5917 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5918 | } |
| 5919 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5920 | ptr = (char *)type->tp_as_number; |
| 5921 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5922 | } |
| 5923 | else { |
| 5924 | ptr = (char *)type; |
| 5925 | } |
| 5926 | if (ptr != NULL) |
| 5927 | ptr += offset; |
| 5928 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5929 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5930 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5931 | /* Length of array of slotdef pointers used to store slots with the |
| 5932 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5933 | the same __name__, for any __name__. Since that's a static property, it is |
| 5934 | appropriate to declare fixed-size arrays for this. */ |
| 5935 | #define MAX_EQUIV 10 |
| 5936 | |
| 5937 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5938 | exactly one slot function. The name must be an interned string. */ |
| 5939 | static void ** |
| 5940 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | /* 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] | 5943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5944 | /* pname and ptrs act as a little cache */ |
| 5945 | static PyObject *pname; |
| 5946 | static slotdef *ptrs[MAX_EQUIV]; |
| 5947 | slotdef *p, **pp; |
| 5948 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5950 | if (pname != name) { |
| 5951 | /* Collect all slotdefs that match name into ptrs. */ |
| 5952 | pname = name; |
| 5953 | pp = ptrs; |
| 5954 | for (p = slotdefs; p->name_strobj; p++) { |
| 5955 | if (p->name_strobj == name) |
| 5956 | *pp++ = p; |
| 5957 | } |
| 5958 | *pp = NULL; |
| 5959 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | /* Look in all matching slots of the type; if exactly one of these has |
| 5962 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5963 | res = NULL; |
| 5964 | for (pp = ptrs; *pp; pp++) { |
| 5965 | ptr = slotptr(type, (*pp)->offset); |
| 5966 | if (ptr == NULL || *ptr == NULL) |
| 5967 | continue; |
| 5968 | if (res != NULL) |
| 5969 | return NULL; |
| 5970 | res = ptr; |
| 5971 | } |
| 5972 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5975 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5976 | does some incredibly complex thinking and then sticks something into the |
| 5977 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5978 | interests, and then stores a generic wrapper or a specific function into |
| 5979 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5980 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5981 | static slotdef * |
| 5982 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | PyObject *descr; |
| 5985 | PyWrapperDescrObject *d; |
| 5986 | void *generic = NULL, *specific = NULL; |
| 5987 | int use_generic = 0; |
| 5988 | int offset = p->offset; |
| 5989 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5991 | if (ptr == NULL) { |
| 5992 | do { |
| 5993 | ++p; |
| 5994 | } while (p->offset == offset); |
| 5995 | return p; |
| 5996 | } |
| 5997 | do { |
| 5998 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5999 | if (descr == NULL) { |
| 6000 | if (ptr == (void**)&type->tp_iternext) { |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6001 | specific = (void *)_PyObject_NextNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6002 | } |
| 6003 | continue; |
| 6004 | } |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 6005 | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
| 6006 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6008 | if (tptr == NULL || tptr == ptr) |
| 6009 | generic = p->function; |
| 6010 | d = (PyWrapperDescrObject *)descr; |
| 6011 | if (d->d_base->wrapper == p->wrapper && |
| 6012 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 6013 | { |
| 6014 | if (specific == NULL || |
| 6015 | specific == d->d_wrapped) |
| 6016 | specific = d->d_wrapped; |
| 6017 | else |
| 6018 | use_generic = 1; |
| 6019 | } |
| 6020 | } |
| 6021 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 6022 | PyCFunction_GET_FUNCTION(descr) == |
| 6023 | (PyCFunction)tp_new_wrapper && |
| 6024 | ptr == (void**)&type->tp_new) |
| 6025 | { |
| 6026 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6027 | so must be special-cased differently. |
| 6028 | If we don't do this, creating an instance will |
| 6029 | always use slot_tp_new which will look up |
| 6030 | __new__ in the MRO which will call tp_new_wrapper |
| 6031 | which will look through the base classes looking |
| 6032 | for a static base and call its tp_new (usually |
| 6033 | PyType_GenericNew), after performing various |
| 6034 | sanity checks and constructing a new argument |
| 6035 | list. Cut all that nonsense short -- this speeds |
| 6036 | up instance creation tremendously. */ |
| 6037 | specific = (void *)type->tp_new; |
| 6038 | /* XXX I'm not 100% sure that there isn't a hole |
| 6039 | in this reasoning that requires additional |
| 6040 | sanity checks. I'll buy the first person to |
| 6041 | point out a bug in this reasoning a beer. */ |
| 6042 | } |
| 6043 | else if (descr == Py_None && |
| 6044 | ptr == (void**)&type->tp_hash) { |
| 6045 | /* We specifically allow __hash__ to be set to None |
| 6046 | to prevent inheritance of the default |
| 6047 | implementation from object.__hash__ */ |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6048 | specific = (void *)PyObject_HashNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6049 | } |
| 6050 | else { |
| 6051 | use_generic = 1; |
| 6052 | generic = p->function; |
| 6053 | } |
| 6054 | } while ((++p)->offset == offset); |
| 6055 | if (specific && !use_generic) |
| 6056 | *ptr = specific; |
| 6057 | else |
| 6058 | *ptr = generic; |
| 6059 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6060 | } |
| 6061 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6062 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6063 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6064 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6065 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6066 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6067 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6069 | for (; *pp; pp++) |
| 6070 | update_one_slot(type, *pp); |
| 6071 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6072 | } |
| 6073 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6074 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6075 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6076 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6077 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6079 | slotdef *p; |
| 6080 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6082 | if (initialized) |
| 6083 | return; |
| 6084 | for (p = slotdefs; p->name; p++) { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6085 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
| 6086 | assert(!p[1].name || p->offset <= p[1].offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6087 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 6088 | if (!p->name_strobj) |
| 6089 | Py_FatalError("Out of memory interning slotdef names"); |
| 6090 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6091 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6094 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6095 | static int |
| 6096 | update_slot(PyTypeObject *type, PyObject *name) |
| 6097 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6098 | slotdef *ptrs[MAX_EQUIV]; |
| 6099 | slotdef *p; |
| 6100 | slotdef **pp; |
| 6101 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6103 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6104 | subclasses. This could possibly be unified with the |
| 6105 | update_subclasses() recursion below, but carefully: |
| 6106 | they each have their own conditions on which to stop |
| 6107 | recursing into subclasses. */ |
| 6108 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 6109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6110 | init_slotdefs(); |
| 6111 | pp = ptrs; |
| 6112 | for (p = slotdefs; p->name; p++) { |
| 6113 | /* XXX assume name is interned! */ |
| 6114 | if (p->name_strobj == name) |
| 6115 | *pp++ = p; |
| 6116 | } |
| 6117 | *pp = NULL; |
| 6118 | for (pp = ptrs; *pp; pp++) { |
| 6119 | p = *pp; |
| 6120 | offset = p->offset; |
| 6121 | while (p > slotdefs && (p-1)->offset == offset) |
| 6122 | --p; |
| 6123 | *pp = p; |
| 6124 | } |
| 6125 | if (ptrs[0] == NULL) |
| 6126 | return 0; /* Not an attribute that affects any slots */ |
| 6127 | return update_subclasses(type, name, |
| 6128 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6131 | /* Store the proper functions in the slot dispatches at class (type) |
| 6132 | definition time, based upon which operations the class overrides in its |
| 6133 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6134 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6135 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6137 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6139 | init_slotdefs(); |
| 6140 | for (p = slotdefs; p->name; ) |
| 6141 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6142 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6143 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6144 | static void |
| 6145 | update_all_slots(PyTypeObject* type) |
| 6146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6147 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6149 | init_slotdefs(); |
| 6150 | for (p = slotdefs; p->name; p++) { |
| 6151 | /* update_slot returns int but can't actually fail */ |
| 6152 | update_slot(type, p->name_strobj); |
| 6153 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6156 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6157 | recursive functions to call a callback for all subclasses, |
| 6158 | but refraining from recursing into subclasses that define 'name'. */ |
| 6159 | |
| 6160 | static int |
| 6161 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6162 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6164 | if (callback(type, data) < 0) |
| 6165 | return -1; |
| 6166 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
| 6169 | static int |
| 6170 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6171 | update_callback callback, void *data) |
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 | PyTypeObject *subclass; |
| 6174 | PyObject *ref, *subclasses, *dict; |
| 6175 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6177 | subclasses = type->tp_subclasses; |
| 6178 | if (subclasses == NULL) |
| 6179 | return 0; |
| 6180 | assert(PyList_Check(subclasses)); |
| 6181 | n = PyList_GET_SIZE(subclasses); |
| 6182 | for (i = 0; i < n; i++) { |
| 6183 | ref = PyList_GET_ITEM(subclasses, i); |
| 6184 | assert(PyWeakref_CheckRef(ref)); |
| 6185 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6186 | assert(subclass != NULL); |
| 6187 | if ((PyObject *)subclass == Py_None) |
| 6188 | continue; |
| 6189 | assert(PyType_Check(subclass)); |
| 6190 | /* Avoid recursing down into unaffected classes */ |
| 6191 | dict = subclass->tp_dict; |
| 6192 | if (dict != NULL && PyDict_Check(dict) && |
| 6193 | PyDict_GetItem(dict, name) != NULL) |
| 6194 | continue; |
| 6195 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6196 | return -1; |
| 6197 | } |
| 6198 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6199 | } |
| 6200 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6201 | /* This function is called by PyType_Ready() to populate the type's |
| 6202 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6203 | function slot (like tp_repr) that's defined in the type, one or more |
| 6204 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6205 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6206 | cause more than one descriptor to be added (for example, the nb_add |
| 6207 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6208 | slots compete for the same descriptor (for example both sq_item and |
| 6209 | mp_subscript generate a __getitem__ descriptor). |
| 6210 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6211 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6212 | 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] | 6213 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6214 | between competing slots: the members of PyHeapTypeObject are listed |
| 6215 | from most general to least general, so the most general slot is |
| 6216 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6217 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6218 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6219 | |
| 6220 | This only adds new descriptors and doesn't overwrite entries in |
| 6221 | tp_dict that were previously defined. The descriptors contain a |
| 6222 | reference to the C function they must call, so that it's safe if they |
| 6223 | are copied into a subtype's __dict__ and the subtype has a different |
| 6224 | C function in its slot -- calling the method defined by the |
| 6225 | descriptor will call the C function that was used to create it, |
| 6226 | rather than the C function present in the slot when it is called. |
| 6227 | (This is important because a subtype may have a C function in the |
| 6228 | slot that calls the method from the dictionary, and we want to avoid |
| 6229 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6230 | |
| 6231 | static int |
| 6232 | add_operators(PyTypeObject *type) |
| 6233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6234 | PyObject *dict = type->tp_dict; |
| 6235 | slotdef *p; |
| 6236 | PyObject *descr; |
| 6237 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6239 | init_slotdefs(); |
| 6240 | for (p = slotdefs; p->name; p++) { |
| 6241 | if (p->wrapper == NULL) |
| 6242 | continue; |
| 6243 | ptr = slotptr(type, p->offset); |
| 6244 | if (!ptr || !*ptr) |
| 6245 | continue; |
| 6246 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6247 | continue; |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6248 | if (*ptr == (void *)PyObject_HashNotImplemented) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6249 | /* Classes may prevent the inheritance of the tp_hash |
| 6250 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6251 | visible as a None value for the __hash__ attribute. */ |
| 6252 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6253 | return -1; |
| 6254 | } |
| 6255 | else { |
| 6256 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6257 | if (descr == NULL) |
| 6258 | return -1; |
| 6259 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6260 | return -1; |
| 6261 | Py_DECREF(descr); |
| 6262 | } |
| 6263 | } |
| 6264 | if (type->tp_new != NULL) { |
| 6265 | if (add_tp_new_wrapper(type) < 0) |
| 6266 | return -1; |
| 6267 | } |
| 6268 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6271 | |
| 6272 | /* Cooperative 'super' */ |
| 6273 | |
| 6274 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | PyObject_HEAD |
| 6276 | PyTypeObject *type; |
| 6277 | PyObject *obj; |
| 6278 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6279 | } superobject; |
| 6280 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6281 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6282 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6283 | "the class invoking super()"}, |
| 6284 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6285 | "the instance invoking super(); may be None"}, |
| 6286 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6287 | "the type of the instance invoking super(); may be None"}, |
| 6288 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6289 | }; |
| 6290 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6291 | static void |
| 6292 | super_dealloc(PyObject *self) |
| 6293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6294 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6296 | _PyObject_GC_UNTRACK(self); |
| 6297 | Py_XDECREF(su->obj); |
| 6298 | Py_XDECREF(su->type); |
| 6299 | Py_XDECREF(su->obj_type); |
| 6300 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6304 | super_repr(PyObject *self) |
| 6305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6308 | if (su->obj_type) |
| 6309 | return PyUnicode_FromFormat( |
| 6310 | "<super: <class '%s'>, <%s object>>", |
| 6311 | su->type ? su->type->tp_name : "NULL", |
| 6312 | su->obj_type->tp_name); |
| 6313 | else |
| 6314 | return PyUnicode_FromFormat( |
| 6315 | "<super: <class '%s'>, NULL>", |
| 6316 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6320 | super_getattro(PyObject *self, PyObject *name) |
| 6321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6322 | superobject *su = (superobject *)self; |
| 6323 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6325 | if (!skip) { |
| 6326 | /* We want __class__ to return the class of the super object |
| 6327 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6328 | skip = (PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6329 | PyUnicode_GET_LENGTH(name) == 9 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6330 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6331 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6333 | if (!skip) { |
| 6334 | PyObject *mro, *res, *tmp, *dict; |
| 6335 | PyTypeObject *starttype; |
| 6336 | descrgetfunc f; |
| 6337 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6339 | starttype = su->obj_type; |
| 6340 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | if (mro == NULL) |
| 6343 | n = 0; |
| 6344 | else { |
| 6345 | assert(PyTuple_Check(mro)); |
| 6346 | n = PyTuple_GET_SIZE(mro); |
| 6347 | } |
| 6348 | for (i = 0; i < n; i++) { |
| 6349 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6350 | break; |
| 6351 | } |
| 6352 | i++; |
| 6353 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6354 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 6355 | replaced during PyDict_GetItem(dict, name) */ |
| 6356 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | for (; i < n; i++) { |
| 6358 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6359 | if (PyType_Check(tmp)) |
| 6360 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6361 | else |
| 6362 | continue; |
| 6363 | res = PyDict_GetItem(dict, name); |
| 6364 | if (res != NULL) { |
| 6365 | Py_INCREF(res); |
| 6366 | f = Py_TYPE(res)->tp_descr_get; |
| 6367 | if (f != NULL) { |
| 6368 | tmp = f(res, |
| 6369 | /* Only pass 'obj' param if |
| 6370 | this is instance-mode super |
| 6371 | (See SF ID #743627) |
| 6372 | */ |
| 6373 | (su->obj == (PyObject *) |
| 6374 | su->obj_type |
| 6375 | ? (PyObject *)NULL |
| 6376 | : su->obj), |
| 6377 | (PyObject *)starttype); |
| 6378 | Py_DECREF(res); |
| 6379 | res = tmp; |
| 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 | return res; |
| 6383 | } |
| 6384 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6385 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | } |
| 6387 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6390 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6391 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6393 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6394 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 6395 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6398 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | - If it is an instance, it must be an instance of 'type'. This is |
| 6401 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6403 | But... when obj is an instance, we want to allow for the case where |
| 6404 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6405 | This will allow using super() with a proxy for obj. |
| 6406 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6408 | /* Check for first bullet above (special case) */ |
| 6409 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6410 | Py_INCREF(obj); |
| 6411 | return (PyTypeObject *)obj; |
| 6412 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6414 | /* Normal case */ |
| 6415 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6416 | Py_INCREF(Py_TYPE(obj)); |
| 6417 | return Py_TYPE(obj); |
| 6418 | } |
| 6419 | else { |
| 6420 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6421 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6422 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6423 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6424 | if (class_attr != NULL && |
| 6425 | PyType_Check(class_attr) && |
| 6426 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6427 | { |
| 6428 | int ok = PyType_IsSubtype( |
| 6429 | (PyTypeObject *)class_attr, type); |
| 6430 | if (ok) |
| 6431 | return (PyTypeObject *)class_attr; |
| 6432 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6434 | if (class_attr == NULL) |
| 6435 | PyErr_Clear(); |
| 6436 | else |
| 6437 | Py_DECREF(class_attr); |
| 6438 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6440 | PyErr_SetString(PyExc_TypeError, |
| 6441 | "super(type, obj): " |
| 6442 | "obj must be an instance or subtype of type"); |
| 6443 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6444 | } |
| 6445 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6446 | static PyObject * |
| 6447 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6448 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | superobject *su = (superobject *)self; |
| 6450 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6452 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6453 | /* Not binding to an object, or already bound */ |
| 6454 | Py_INCREF(self); |
| 6455 | return self; |
| 6456 | } |
| 6457 | if (Py_TYPE(su) != &PySuper_Type) |
| 6458 | /* If su is an instance of a (strict) subclass of super, |
| 6459 | call its type */ |
| 6460 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6461 | su->type, obj, NULL); |
| 6462 | else { |
| 6463 | /* Inline the common case */ |
| 6464 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6465 | if (obj_type == NULL) |
| 6466 | return NULL; |
| 6467 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6468 | NULL, NULL); |
| 6469 | if (newobj == NULL) |
| 6470 | return NULL; |
| 6471 | Py_INCREF(su->type); |
| 6472 | Py_INCREF(obj); |
| 6473 | newobj->type = su->type; |
| 6474 | newobj->obj = obj; |
| 6475 | newobj->obj_type = obj_type; |
| 6476 | return (PyObject *)newobj; |
| 6477 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6478 | } |
| 6479 | |
| 6480 | static int |
| 6481 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6483 | superobject *su = (superobject *)self; |
| 6484 | PyTypeObject *type = NULL; |
| 6485 | PyObject *obj = NULL; |
| 6486 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6488 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6489 | return -1; |
| 6490 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6491 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | if (type == NULL) { |
| 6494 | /* Call super(), without args -- fill in from __class__ |
| 6495 | and first local variable on the stack. */ |
| 6496 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6497 | PyCodeObject *co = f->f_code; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6498 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6499 | if (co == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6500 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6501 | "super(): no code object"); |
| 6502 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6503 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6504 | if (co->co_argcount == 0) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6505 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6506 | "super(): no arguments"); |
| 6507 | return -1; |
| 6508 | } |
| 6509 | obj = f->f_localsplus[0]; |
| 6510 | if (obj == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6511 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6512 | "super(): arg[0] deleted"); |
| 6513 | return -1; |
| 6514 | } |
| 6515 | if (co->co_freevars == NULL) |
| 6516 | n = 0; |
| 6517 | else { |
| 6518 | assert(PyTuple_Check(co->co_freevars)); |
| 6519 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6520 | } |
| 6521 | for (i = 0; i < n; i++) { |
| 6522 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6523 | assert(PyUnicode_Check(name)); |
| 6524 | if (!PyUnicode_CompareWithASCIIString(name, |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 6525 | "__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6526 | Py_ssize_t index = co->co_nlocals + |
| 6527 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6528 | PyObject *cell = f->f_localsplus[index]; |
| 6529 | if (cell == NULL || !PyCell_Check(cell)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6530 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | "super(): bad __class__ cell"); |
| 6532 | return -1; |
| 6533 | } |
| 6534 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6535 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6536 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6537 | "super(): empty __class__ cell"); |
| 6538 | return -1; |
| 6539 | } |
| 6540 | if (!PyType_Check(type)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6541 | PyErr_Format(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6542 | "super(): __class__ is not a type (%s)", |
| 6543 | Py_TYPE(type)->tp_name); |
| 6544 | return -1; |
| 6545 | } |
| 6546 | break; |
| 6547 | } |
| 6548 | } |
| 6549 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6550 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | "super(): __class__ cell not found"); |
| 6552 | return -1; |
| 6553 | } |
| 6554 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6556 | if (obj == Py_None) |
| 6557 | obj = NULL; |
| 6558 | if (obj != NULL) { |
| 6559 | obj_type = supercheck(type, obj); |
| 6560 | if (obj_type == NULL) |
| 6561 | return -1; |
| 6562 | Py_INCREF(obj); |
| 6563 | } |
| 6564 | Py_INCREF(type); |
| 6565 | su->type = type; |
| 6566 | su->obj = obj; |
| 6567 | su->obj_type = obj_type; |
| 6568 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6572 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6573 | "super(type) -> unbound super object\n" |
| 6574 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6575 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6576 | "Typical use to call a cooperative superclass method:\n" |
| 6577 | "class C(B):\n" |
| 6578 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6579 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6580 | "This works for class methods too:\n" |
| 6581 | "class C(B):\n" |
| 6582 | " @classmethod\n" |
| 6583 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6585 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6586 | static int |
| 6587 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6589 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6591 | Py_VISIT(su->obj); |
| 6592 | Py_VISIT(su->type); |
| 6593 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6598 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6599 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6600 | "super", /* tp_name */ |
| 6601 | sizeof(superobject), /* tp_basicsize */ |
| 6602 | 0, /* tp_itemsize */ |
| 6603 | /* methods */ |
| 6604 | super_dealloc, /* tp_dealloc */ |
| 6605 | 0, /* tp_print */ |
| 6606 | 0, /* tp_getattr */ |
| 6607 | 0, /* tp_setattr */ |
| 6608 | 0, /* tp_reserved */ |
| 6609 | super_repr, /* tp_repr */ |
| 6610 | 0, /* tp_as_number */ |
| 6611 | 0, /* tp_as_sequence */ |
| 6612 | 0, /* tp_as_mapping */ |
| 6613 | 0, /* tp_hash */ |
| 6614 | 0, /* tp_call */ |
| 6615 | 0, /* tp_str */ |
| 6616 | super_getattro, /* tp_getattro */ |
| 6617 | 0, /* tp_setattro */ |
| 6618 | 0, /* tp_as_buffer */ |
| 6619 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6620 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6621 | super_doc, /* tp_doc */ |
| 6622 | super_traverse, /* tp_traverse */ |
| 6623 | 0, /* tp_clear */ |
| 6624 | 0, /* tp_richcompare */ |
| 6625 | 0, /* tp_weaklistoffset */ |
| 6626 | 0, /* tp_iter */ |
| 6627 | 0, /* tp_iternext */ |
| 6628 | 0, /* tp_methods */ |
| 6629 | super_members, /* tp_members */ |
| 6630 | 0, /* tp_getset */ |
| 6631 | 0, /* tp_base */ |
| 6632 | 0, /* tp_dict */ |
| 6633 | super_descr_get, /* tp_descr_get */ |
| 6634 | 0, /* tp_descr_set */ |
| 6635 | 0, /* tp_dictoffset */ |
| 6636 | super_init, /* tp_init */ |
| 6637 | PyType_GenericAlloc, /* tp_alloc */ |
| 6638 | PyType_GenericNew, /* tp_new */ |
| 6639 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6640 | }; |