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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | Py_DECREF(et->ht_name); |
| 302 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | type->tp_name = tp_name; |
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 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 309 | static int |
| 310 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 311 | { |
| 312 | PyHeapTypeObject* et; |
| 313 | |
| 314 | if (!PyUnicode_Check(value)) { |
| 315 | PyErr_Format(PyExc_TypeError, |
| 316 | "can only assign string to %s.__qualname__, not '%s'", |
| 317 | type->tp_name, Py_TYPE(value)->tp_name); |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | et = (PyHeapTypeObject*)type; |
| 322 | Py_INCREF(value); |
| 323 | Py_DECREF(et->ht_qualname); |
| 324 | et->ht_qualname = value; |
| 325 | return 0; |
| 326 | } |
| 327 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 328 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 329 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | PyObject *mod; |
| 332 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 335 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | if (!mod) { |
| 337 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 338 | return 0; |
| 339 | } |
| 340 | Py_XINCREF(mod); |
| 341 | return mod; |
| 342 | } |
| 343 | else { |
| 344 | s = strrchr(type->tp_name, '.'); |
| 345 | if (s != NULL) |
| 346 | return PyUnicode_FromStringAndSize( |
| 347 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 348 | return PyUnicode_FromString("builtins"); |
| 349 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 352 | static int |
| 353 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 354 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 355 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 359 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 360 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 363 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 364 | type_abstractmethods(PyTypeObject *type, void *context) |
| 365 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 366 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 367 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 368 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 369 | if (type != &PyType_Type) |
| 370 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | if (!mod) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 372 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
| 375 | Py_XINCREF(mod); |
| 376 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static int |
| 380 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | /* __abstractmethods__ should only be set once on a type, in |
| 383 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 384 | special to update subclasses. |
| 385 | */ |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 386 | int abstract, res; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 387 | if (value != NULL) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 388 | abstract = PyObject_IsTrue(value); |
| 389 | if (abstract < 0) |
| 390 | return -1; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 391 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value); |
| 392 | } |
| 393 | else { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 394 | abstract = 0; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 395 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__"); |
| 396 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 397 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | if (res == 0) { |
| 402 | PyType_Modified(type); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 403 | if (abstract) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 405 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | } |
| 408 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 412 | type_get_bases(PyTypeObject *type, void *context) |
| 413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | Py_INCREF(type->tp_bases); |
| 415 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static PyTypeObject *best_base(PyObject *); |
| 419 | static int mro_internal(PyTypeObject *); |
| 420 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 421 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 422 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 423 | static void update_all_slots(PyTypeObject *); |
| 424 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 425 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 426 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 428 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 430 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 431 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 432 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | PyTypeObject *subclass; |
| 435 | PyObject *ref, *subclasses, *old_mro; |
| 436 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | subclasses = type->tp_subclasses; |
| 439 | if (subclasses == NULL) |
| 440 | return 0; |
| 441 | assert(PyList_Check(subclasses)); |
| 442 | n = PyList_GET_SIZE(subclasses); |
| 443 | for (i = 0; i < n; i++) { |
| 444 | ref = PyList_GET_ITEM(subclasses, i); |
| 445 | assert(PyWeakref_CheckRef(ref)); |
| 446 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 447 | assert(subclass != NULL); |
| 448 | if ((PyObject *)subclass == Py_None) |
| 449 | continue; |
| 450 | assert(PyType_Check(subclass)); |
| 451 | old_mro = subclass->tp_mro; |
| 452 | if (mro_internal(subclass) < 0) { |
| 453 | subclass->tp_mro = old_mro; |
| 454 | return -1; |
| 455 | } |
| 456 | else { |
| 457 | PyObject* tuple; |
| 458 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 459 | Py_DECREF(old_mro); |
| 460 | if (!tuple) |
| 461 | return -1; |
| 462 | if (PyList_Append(temp, tuple) < 0) |
| 463 | return -1; |
| 464 | Py_DECREF(tuple); |
| 465 | } |
| 466 | if (mro_subclasses(subclass, temp) < 0) |
| 467 | return -1; |
| 468 | } |
| 469 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int |
| 473 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | Py_ssize_t i; |
| 476 | int r = 0; |
| 477 | PyObject *ob, *temp; |
| 478 | PyTypeObject *new_base, *old_base; |
| 479 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 480 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 481 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | if (!PyTuple_Check(value)) { |
| 484 | PyErr_Format(PyExc_TypeError, |
| 485 | "can only assign tuple to %s.__bases__, not %s", |
| 486 | type->tp_name, Py_TYPE(value)->tp_name); |
| 487 | return -1; |
| 488 | } |
| 489 | if (PyTuple_GET_SIZE(value) == 0) { |
| 490 | PyErr_Format(PyExc_TypeError, |
| 491 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 492 | type->tp_name); |
| 493 | return -1; |
| 494 | } |
| 495 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 496 | ob = PyTuple_GET_ITEM(value, i); |
| 497 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 498 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 499 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 500 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 501 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | } |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 503 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 504 | PyErr_SetString(PyExc_TypeError, |
| 505 | "a __bases__ item causes an inheritance cycle"); |
| 506 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 511 | |
Benjamin Peterson | ab3c1c1 | 2012-04-01 18:48:02 -0400 | [diff] [blame] | 512 | if (!new_base) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | return -1; |
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 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 516 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | Py_INCREF(new_base); |
| 519 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | old_bases = type->tp_bases; |
| 522 | old_base = type->tp_base; |
| 523 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | type->tp_bases = value; |
| 526 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | if (mro_internal(type) < 0) { |
| 529 | goto bail; |
| 530 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | temp = PyList_New(0); |
| 533 | if (!temp) |
| 534 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | if (r < 0) { |
| 539 | for (i = 0; i < PyList_Size(temp); i++) { |
| 540 | PyTypeObject* cls; |
| 541 | PyObject* mro; |
| 542 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 543 | "", 2, 2, &cls, &mro); |
| 544 | Py_INCREF(mro); |
| 545 | ob = cls->tp_mro; |
| 546 | cls->tp_mro = mro; |
| 547 | Py_DECREF(ob); |
| 548 | } |
| 549 | Py_DECREF(temp); |
| 550 | goto bail; |
| 551 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* any base that was in __bases__ but now isn't, we |
| 556 | need to remove |type| from its tp_subclasses. |
| 557 | conversely, any class now in __bases__ that wasn't |
| 558 | needs to have |type| added to its subclasses. */ |
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 | /* for now, sod that: just remove from all old_bases, |
| 561 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 564 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 565 | if (PyType_Check(ob)) { |
| 566 | remove_subclass( |
| 567 | (PyTypeObject*)ob, type); |
| 568 | } |
| 569 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 572 | ob = PyTuple_GET_ITEM(value, i); |
| 573 | if (PyType_Check(ob)) { |
| 574 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 575 | r = -1; |
| 576 | } |
| 577 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | Py_DECREF(old_bases); |
| 582 | Py_DECREF(old_base); |
| 583 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 586 | |
| 587 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | Py_DECREF(type->tp_bases); |
| 589 | Py_DECREF(type->tp_base); |
| 590 | if (type->tp_mro != old_mro) { |
| 591 | Py_DECREF(type->tp_mro); |
| 592 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | type->tp_bases = old_bases; |
| 595 | type->tp_base = old_base; |
| 596 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 602 | type_dict(PyTypeObject *type, void *context) |
| 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | if (type->tp_dict == NULL) { |
| 605 | Py_INCREF(Py_None); |
| 606 | return Py_None; |
| 607 | } |
| 608 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 611 | static PyObject * |
| 612 | type_get_doc(PyTypeObject *type, void *context) |
| 613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | PyObject *result; |
| 615 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 616 | return PyUnicode_FromString(type->tp_doc); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 617 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | if (result == NULL) { |
| 619 | result = Py_None; |
| 620 | Py_INCREF(result); |
| 621 | } |
| 622 | else if (Py_TYPE(result)->tp_descr_get) { |
| 623 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 624 | (PyObject *)type); |
| 625 | } |
| 626 | else { |
| 627 | Py_INCREF(result); |
| 628 | } |
| 629 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 632 | static int |
| 633 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 634 | { |
| 635 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 636 | return -1; |
| 637 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 638 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 639 | } |
| 640 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 641 | static PyObject * |
| 642 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 645 | case -1: |
| 646 | return NULL; |
| 647 | case 0: |
| 648 | Py_RETURN_FALSE; |
| 649 | default: |
| 650 | Py_RETURN_TRUE; |
| 651 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 656 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 659 | case -1: |
| 660 | return NULL; |
| 661 | case 0: |
| 662 | Py_RETURN_FALSE; |
| 663 | default: |
| 664 | Py_RETURN_TRUE; |
| 665 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 668 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 669 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 671 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 673 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 674 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 675 | (setter)type_set_abstractmethods, NULL}, |
| 676 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 677 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 681 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 682 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | mod = type_module(type, NULL); |
| 687 | if (mod == NULL) |
| 688 | PyErr_Clear(); |
| 689 | else if (!PyUnicode_Check(mod)) { |
| 690 | Py_DECREF(mod); |
| 691 | mod = NULL; |
| 692 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 693 | name = type_qualname(type, NULL); |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 694 | if (name == NULL) { |
| 695 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | return NULL; |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 697 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 700 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 701 | else |
| 702 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
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 | Py_XDECREF(mod); |
| 705 | Py_DECREF(name); |
| 706 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 709 | static PyObject * |
| 710 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | if (type->tp_new == NULL) { |
| 715 | PyErr_Format(PyExc_TypeError, |
| 716 | "cannot create '%.100s' instances", |
| 717 | type->tp_name); |
| 718 | return NULL; |
| 719 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | obj = type->tp_new(type, args, kwds); |
| 722 | if (obj != NULL) { |
| 723 | /* Ugly exception: when the call was type(something), |
| 724 | don't call tp_init on the result. */ |
| 725 | if (type == &PyType_Type && |
| 726 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 727 | (kwds == NULL || |
| 728 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 729 | return obj; |
| 730 | /* If the returned object is not an instance of type, |
| 731 | it won't be initialized. */ |
| 732 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 733 | return obj; |
| 734 | type = Py_TYPE(obj); |
| 735 | if (type->tp_init != NULL && |
| 736 | type->tp_init(obj, args, kwds) < 0) { |
| 737 | Py_DECREF(obj); |
| 738 | obj = NULL; |
| 739 | } |
| 740 | } |
| 741 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 745 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | PyObject *obj; |
| 748 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 749 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | if (PyType_IS_GC(type)) |
| 752 | obj = _PyObject_GC_Malloc(size); |
| 753 | else |
| 754 | obj = (PyObject *)PyObject_MALLOC(size); |
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 (obj == NULL) |
| 757 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | memset(obj, '\0', 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 (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 762 | Py_INCREF(type); |
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 | if (type->tp_itemsize == 0) |
| 765 | PyObject_INIT(obj, type); |
| 766 | else |
| 767 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
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 (PyType_IS_GC(type)) |
| 770 | _PyObject_GC_TRACK(obj); |
| 771 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | PyObject * |
| 775 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 780 | /* Helpers for subtyping */ |
| 781 | |
| 782 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 783 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | Py_ssize_t i, n; |
| 786 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | n = Py_SIZE(type); |
| 789 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 790 | for (i = 0; i < n; i++, mp++) { |
| 791 | if (mp->type == T_OBJECT_EX) { |
| 792 | char *addr = (char *)self + mp->offset; |
| 793 | PyObject *obj = *(PyObject **)addr; |
| 794 | if (obj != NULL) { |
| 795 | int err = visit(obj, arg); |
| 796 | if (err) |
| 797 | return err; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 805 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | PyTypeObject *type, *base; |
| 808 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | /* Find the nearest base with a different tp_traverse, |
| 811 | and traverse slots while we're at it */ |
| 812 | type = Py_TYPE(self); |
| 813 | base = type; |
| 814 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 815 | if (Py_SIZE(base)) { |
| 816 | int err = traverse_slots(base, self, visit, arg); |
| 817 | if (err) |
| 818 | return err; |
| 819 | } |
| 820 | base = base->tp_base; |
| 821 | assert(base); |
| 822 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 825 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 826 | if (dictptr && *dictptr) |
| 827 | Py_VISIT(*dictptr); |
| 828 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 831 | /* For a heaptype, the instances count as references |
| 832 | to the type. Traverse the type so the collector |
| 833 | can find cycles involving this link. */ |
| 834 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | if (basetraverse) |
| 837 | return basetraverse(self, visit, arg); |
| 838 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void |
| 842 | clear_slots(PyTypeObject *type, PyObject *self) |
| 843 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | Py_ssize_t i, n; |
| 845 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | n = Py_SIZE(type); |
| 848 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 849 | for (i = 0; i < n; i++, mp++) { |
| 850 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 851 | char *addr = (char *)self + mp->offset; |
| 852 | PyObject *obj = *(PyObject **)addr; |
| 853 | if (obj != NULL) { |
| 854 | *(PyObject **)addr = NULL; |
| 855 | Py_DECREF(obj); |
| 856 | } |
| 857 | } |
| 858 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static int |
| 862 | subtype_clear(PyObject *self) |
| 863 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | PyTypeObject *type, *base; |
| 865 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | /* Find the nearest base with a different tp_clear |
| 868 | and clear slots while we're at it */ |
| 869 | type = Py_TYPE(self); |
| 870 | base = type; |
| 871 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 872 | if (Py_SIZE(base)) |
| 873 | clear_slots(base, self); |
| 874 | base = base->tp_base; |
| 875 | assert(base); |
| 876 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 877 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 878 | /* Clear the instance dict (if any), to break cycles involving only |
| 879 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 880 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 881 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 882 | if (dictptr && *dictptr) |
| 883 | Py_CLEAR(*dictptr); |
| 884 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if (baseclear) |
| 887 | return baseclear(self); |
| 888 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 889 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 890 | |
| 891 | static void |
| 892 | subtype_dealloc(PyObject *self) |
| 893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | PyTypeObject *type, *base; |
| 895 | destructor basedealloc; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 896 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | /* Extract the type; we expect it to be a heap type */ |
| 899 | type = Py_TYPE(self); |
| 900 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | if (!PyType_IS_GC(type)) { |
| 905 | /* It's really rare to find a dynamic type that doesn't have |
| 906 | GC; it can only happen when deriving from 'object' and not |
| 907 | adding any slots or instance variables. This allows |
| 908 | certain simplifications: there's no need to call |
| 909 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | /* Maybe call finalizer; exit early if resurrected */ |
| 912 | if (type->tp_del) { |
| 913 | type->tp_del(self); |
| 914 | if (self->ob_refcnt > 0) |
| 915 | return; |
| 916 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | /* Find the nearest base with a different tp_dealloc */ |
| 919 | base = type; |
| 920 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 921 | assert(Py_SIZE(base) == 0); |
| 922 | base = base->tp_base; |
| 923 | assert(base); |
| 924 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | /* Extract the type again; tp_del may have changed it */ |
| 927 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | /* Call the base tp_dealloc() */ |
| 930 | assert(basedealloc); |
| 931 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | /* Can't reference self beyond this point */ |
| 934 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | /* Done */ |
| 937 | return; |
| 938 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 943 | /* See explanation at end of function for full disclosure */ |
| 944 | PyObject_GC_UnTrack(self); |
| 945 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 946 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 948 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 949 | -- tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 951 | * (if any, and whether directly here or indirectly in something we |
| 952 | * call) may trigger GC, and if self is tracked at that point, it |
| 953 | * will look like trash to GC and GC will try to delete self again. |
| 954 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | /* Find the nearest base with a different tp_dealloc */ |
| 957 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 958 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | base = base->tp_base; |
| 960 | assert(base); |
| 961 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 964 | the finalizer (__del__), clearing slots, or clearing the instance |
| 965 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 968 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | /* Maybe call finalizer; exit early if resurrected */ |
| 971 | if (type->tp_del) { |
| 972 | _PyObject_GC_TRACK(self); |
| 973 | type->tp_del(self); |
| 974 | if (self->ob_refcnt > 0) |
| 975 | goto endlabel; /* resurrected */ |
| 976 | else |
| 977 | _PyObject_GC_UNTRACK(self); |
| 978 | /* New weakrefs could be created during the finalizer call. |
| 979 | If this occurs, clear them out without calling their |
| 980 | finalizers since they might rely on part of the object |
| 981 | being finalized that has already been destroyed. */ |
| 982 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 983 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 984 | PyWeakReference **list = (PyWeakReference **) \ |
| 985 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 986 | while (*list) |
| 987 | _PyWeakref_ClearRef(*list); |
| 988 | } |
| 989 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 992 | base = type; |
| 993 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 994 | if (Py_SIZE(base)) |
| 995 | clear_slots(base, self); |
| 996 | base = base->tp_base; |
| 997 | assert(base); |
| 998 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | /* If we added a dict, DECREF it */ |
| 1001 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1002 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1003 | if (dictptr != NULL) { |
| 1004 | PyObject *dict = *dictptr; |
| 1005 | if (dict != NULL) { |
| 1006 | Py_DECREF(dict); |
| 1007 | *dictptr = NULL; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | /* Extract the type again; tp_del may have changed it */ |
| 1013 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | /* Call the base tp_dealloc(); first retrack self if |
| 1016 | * basedealloc knows about gc. |
| 1017 | */ |
| 1018 | if (PyType_IS_GC(base)) |
| 1019 | _PyObject_GC_TRACK(self); |
| 1020 | assert(basedealloc); |
| 1021 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | /* Can't reference self beyond this point */ |
| 1024 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1025 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1026 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1028 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | Py_TRASHCAN_SAFE_END(self); |
| 1030 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1031 | -- tstate->trash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1038 | For one, this explains why there must be a call to GC-untrack |
| 1039 | before the trashcan begin macro. Without understanding the |
| 1040 | trashcan code, the answers to the following questions don't make |
| 1041 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1044 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | A. In the case that the base class is GC-aware, the base class |
| 1047 | probably GC-untracks the object. If it does that using the |
| 1048 | UNTRACK macro, this will crash when the object is already |
| 1049 | untracked. Because we don't know what the base class does, the |
| 1050 | only safe thing is to make sure the object is tracked when we |
| 1051 | call the base class dealloc. But... The trashcan begin macro |
| 1052 | requires that the object is *untracked* before it is called. So |
| 1053 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | GC untrack |
| 1056 | trashcan begin |
| 1057 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | Q. Why did the last question say "immediately GC-track again"? |
| 1060 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1063 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1064 | (which can happen if any weakref callback gets invoked), it |
| 1065 | 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] | 1066 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | Q. Why the bizarre (net-zero) manipulation of |
| 1070 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1073 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | - suppose the trashcan level is one below the trashcan limit |
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 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | - the trashcan limit is not yet reached, so the trashcan level |
| 1080 | is incremented and the code between trashcan begin and end is |
| 1081 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | - this destroys much of the object's contents, including its |
| 1084 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | - basedealloc() is called; this is really list_dealloc(), or |
| 1087 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | - the trashcan limit is now reached, so the object is put on the |
| 1090 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | - basedealloc() returns |
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 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | - later, the trashcan code starts deleting the objects from its |
| 1099 | to-be-deleted-later list |
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() is called *AGAIN* for the same object |
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 | - at the very least (if the destroyed slots and __dict__ don't |
| 1104 | cause problems) the object's type gets decref'ed a second |
| 1105 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | The remedy is to make sure that if the code between trashcan |
| 1108 | begin and end in subtype_dealloc() is called, the code between |
| 1109 | trashcan begin and end in basedealloc() will also be called. |
| 1110 | This is done by decrementing the level after passing into the |
| 1111 | trashcan block, and incrementing it just before leaving the |
| 1112 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | But now it's possible that a chain of objects consisting solely |
| 1115 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1116 | the trashcan mechanism completely: the decremented level means |
| 1117 | that the effective level never reaches the limit. Therefore, we |
| 1118 | *increment* the level *before* entering the trashcan block, and |
| 1119 | matchingly decrement it after leaving. This means the trashcan |
| 1120 | 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] | 1121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | Q. Are there any live examples of code in need of all this |
| 1123 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1126 | compiled in debug mode) before the trashcan level manipulations |
| 1127 | were added. For more discussion, see SF patches 581742, 575073 |
| 1128 | and bug 574207. |
| 1129 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1132 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1133 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1134 | /* type test with subclassing support */ |
| 1135 | |
| 1136 | int |
| 1137 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | mro = a->tp_mro; |
| 1142 | if (mro != NULL) { |
| 1143 | /* Deal with multiple inheritance without recursion |
| 1144 | by walking the MRO tuple */ |
| 1145 | Py_ssize_t i, n; |
| 1146 | assert(PyTuple_Check(mro)); |
| 1147 | n = PyTuple_GET_SIZE(mro); |
| 1148 | for (i = 0; i < n; i++) { |
| 1149 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1150 | return 1; |
| 1151 | } |
| 1152 | return 0; |
| 1153 | } |
| 1154 | else { |
| 1155 | /* a is not completely initilized yet; follow tp_base */ |
| 1156 | do { |
| 1157 | if (a == b) |
| 1158 | return 1; |
| 1159 | a = a->tp_base; |
| 1160 | } while (a != NULL); |
| 1161 | return b == &PyBaseObject_Type; |
| 1162 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1165 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1166 | without looking in the instance dictionary |
| 1167 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1169 | 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] | 1170 | static variable used to cache the interned Python string. |
| 1171 | |
| 1172 | Two variants: |
| 1173 | |
| 1174 | - lookup_maybe() returns NULL without raising an exception |
| 1175 | when the _PyType_Lookup() call fails; |
| 1176 | |
| 1177 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1178 | |
| 1179 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1180 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1181 | |
| 1182 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1183 | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1184 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1185 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1186 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1187 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | if (res != NULL) { |
| 1189 | descrgetfunc f; |
| 1190 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1191 | Py_INCREF(res); |
| 1192 | else |
| 1193 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1194 | } |
| 1195 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1198 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1199 | lookup_method(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1200 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1201 | PyObject *res = lookup_maybe(self, attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | if (res == NULL && !PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1203 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1207 | PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1208 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1209 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1210 | return lookup_maybe(self, attrid); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1213 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1215 | as lookup_method to cache the interned name string object. */ |
| 1216 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1217 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1218 | call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | va_list va; |
| 1221 | PyObject *args, *func = 0, *retval; |
| 1222 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1223 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1224 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | if (func == NULL) { |
| 1226 | va_end(va); |
| 1227 | if (!PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1228 | PyErr_SetObject(PyExc_AttributeError, nameid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | if (format && *format) |
| 1233 | args = Py_VaBuildValue(format, va); |
| 1234 | else |
| 1235 | args = PyTuple_New(0); |
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 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | if (args == NULL) |
| 1240 | return NULL; |
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 | assert(PyTuple_Check(args)); |
| 1243 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | Py_DECREF(args); |
| 1246 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1252 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1253 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1254 | call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | va_list va; |
| 1257 | PyObject *args, *func = 0, *retval; |
| 1258 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1259 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1260 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | if (func == NULL) { |
| 1262 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1263 | if (!PyErr_Occurred()) |
| 1264 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | return NULL; |
| 1266 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | if (format && *format) |
| 1269 | args = Py_VaBuildValue(format, va); |
| 1270 | else |
| 1271 | args = PyTuple_New(0); |
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 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | if (args == NULL) |
| 1276 | return NULL; |
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 | assert(PyTuple_Check(args)); |
| 1279 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | Py_DECREF(args); |
| 1282 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1287 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1288 | Method resolution order algorithm C3 described in |
| 1289 | "A Monotonic Superclass Linearization for Dylan", |
| 1290 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1291 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1292 | (OOPSLA 1996) |
| 1293 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1294 | Some notes about the rules implied by C3: |
| 1295 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1296 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1297 | It isn't legal to repeat a class in a list of base classes. |
| 1298 | |
| 1299 | The next three properties are the 3 constraints in "C3". |
| 1300 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1301 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1302 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1303 | subclasses of C. |
| 1304 | |
| 1305 | Monotonicity. |
| 1306 | The MRO of a class must be an extension without reordering of the |
| 1307 | MRO of each of its superclasses. |
| 1308 | |
| 1309 | Extended Precedence Graph (EPG). |
| 1310 | Linearization is consistent if there is a path in the EPG from |
| 1311 | each class to all its successors in the linearization. See |
| 1312 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1313 | */ |
| 1314 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1315 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1316 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | Py_ssize_t j, size; |
| 1318 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | for (j = whence+1; j < size; j++) { |
| 1321 | if (PyList_GET_ITEM(list, j) == o) |
| 1322 | return 1; |
| 1323 | } |
| 1324 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1327 | static PyObject * |
| 1328 | class_name(PyObject *cls) |
| 1329 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1330 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | if (name == NULL) { |
| 1332 | PyErr_Clear(); |
| 1333 | Py_XDECREF(name); |
| 1334 | name = PyObject_Repr(cls); |
| 1335 | } |
| 1336 | if (name == NULL) |
| 1337 | return NULL; |
| 1338 | if (!PyUnicode_Check(name)) { |
| 1339 | Py_DECREF(name); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static int |
| 1346 | check_duplicates(PyObject *list) |
| 1347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | Py_ssize_t i, j, n; |
| 1349 | /* Let's use a quadratic time algorithm, |
| 1350 | assuming that the bases lists is short. |
| 1351 | */ |
| 1352 | n = PyList_GET_SIZE(list); |
| 1353 | for (i = 0; i < n; i++) { |
| 1354 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1355 | for (j = i + 1; j < n; j++) { |
| 1356 | if (PyList_GET_ITEM(list, j) == o) { |
| 1357 | o = class_name(o); |
| 1358 | if (o != NULL) { |
| 1359 | PyErr_Format(PyExc_TypeError, |
| 1360 | "duplicate base class %U", |
| 1361 | o); |
| 1362 | Py_DECREF(o); |
| 1363 | } else { |
| 1364 | PyErr_SetString(PyExc_TypeError, |
| 1365 | "duplicate base class"); |
| 1366 | } |
| 1367 | return -1; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /* Raise a TypeError for an MRO order disagreement. |
| 1375 | |
| 1376 | It's hard to produce a good error message. In the absence of better |
| 1377 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | 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] | 1379 | order in which they should be put in the MRO, but it's hard to |
| 1380 | diagnose what constraint can't be satisfied. |
| 1381 | */ |
| 1382 | |
| 1383 | static void |
| 1384 | set_mro_error(PyObject *to_merge, int *remain) |
| 1385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | Py_ssize_t i, n, off, to_merge_size; |
| 1387 | char buf[1000]; |
| 1388 | PyObject *k, *v; |
| 1389 | PyObject *set = PyDict_New(); |
| 1390 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1393 | for (i = 0; i < to_merge_size; i++) { |
| 1394 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1395 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1396 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1397 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1398 | Py_DECREF(set); |
| 1399 | return; |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1406 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | i = 0; |
| 1408 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1409 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1410 | char *name_str; |
| 1411 | if (name != NULL) { |
| 1412 | name_str = _PyUnicode_AsString(name); |
| 1413 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1414 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1415 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1416 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1417 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | Py_XDECREF(name); |
| 1419 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1420 | buf[off++] = ','; |
| 1421 | buf[off] = '\0'; |
| 1422 | } |
| 1423 | } |
| 1424 | PyErr_SetString(PyExc_TypeError, buf); |
| 1425 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1428 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1429 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1431 | int *remain; |
| 1432 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | /* remain stores an index into each sublist of to_merge. |
| 1437 | remain[i] is the index of the next base in to_merge[i] |
| 1438 | that is not included in acc. |
| 1439 | */ |
| 1440 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1441 | if (remain == NULL) |
| 1442 | return -1; |
| 1443 | for (i = 0; i < to_merge_size; i++) |
| 1444 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1445 | |
| 1446 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | empty_cnt = 0; |
| 1448 | for (i = 0; i < to_merge_size; i++) { |
| 1449 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1454 | empty_cnt++; |
| 1455 | continue; |
| 1456 | } |
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 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | The input sequences alone can determine the choice. |
| 1461 | If not, choose the class which appears in the MRO |
| 1462 | of the earliest direct superclass of the new class. |
| 1463 | */ |
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 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1466 | for (j = 0; j < to_merge_size; j++) { |
| 1467 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1468 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1469 | goto skip; /* continue outer loop */ |
| 1470 | } |
| 1471 | } |
| 1472 | ok = PyList_Append(acc, candidate); |
| 1473 | if (ok < 0) { |
| 1474 | PyMem_Free(remain); |
| 1475 | return -1; |
| 1476 | } |
| 1477 | for (j = 0; j < to_merge_size; j++) { |
| 1478 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1479 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1480 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1481 | remain[j]++; |
| 1482 | } |
| 1483 | } |
| 1484 | goto again; |
| 1485 | skip: ; |
| 1486 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | if (empty_cnt == to_merge_size) { |
| 1489 | PyMem_FREE(remain); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | set_mro_error(to_merge, remain); |
| 1493 | PyMem_FREE(remain); |
| 1494 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1497 | static PyObject * |
| 1498 | mro_implementation(PyTypeObject *type) |
| 1499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | Py_ssize_t i, n; |
| 1501 | int ok; |
| 1502 | PyObject *bases, *result; |
| 1503 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | if (type->tp_dict == NULL) { |
| 1506 | if (PyType_Ready(type) < 0) |
| 1507 | return NULL; |
| 1508 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | /* Find a superclass linearization that honors the constraints |
| 1511 | of the explicit lists of bases and the constraints implied by |
| 1512 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | to_merge is a list of lists, where each list is a superclass |
| 1515 | linearization implied by a base class. The last element of |
| 1516 | to_merge is the declared list of bases. |
| 1517 | */ |
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 | bases = type->tp_bases; |
| 1520 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | to_merge = PyList_New(n+1); |
| 1523 | if (to_merge == NULL) |
| 1524 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | for (i = 0; i < n; i++) { |
| 1527 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1528 | PyObject *parentMRO; |
| 1529 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1530 | if (parentMRO == NULL) { |
| 1531 | Py_DECREF(to_merge); |
| 1532 | return NULL; |
| 1533 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1536 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | bases_aslist = PySequence_List(bases); |
| 1539 | if (bases_aslist == NULL) { |
| 1540 | Py_DECREF(to_merge); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | /* This is just a basic sanity check. */ |
| 1544 | if (check_duplicates(bases_aslist) < 0) { |
| 1545 | Py_DECREF(to_merge); |
| 1546 | Py_DECREF(bases_aslist); |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1552 | if (result == NULL) { |
| 1553 | Py_DECREF(to_merge); |
| 1554 | return NULL; |
| 1555 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | ok = pmerge(result, to_merge); |
| 1558 | Py_DECREF(to_merge); |
| 1559 | if (ok < 0) { |
| 1560 | Py_DECREF(result); |
| 1561 | return NULL; |
| 1562 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1568 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | static int |
| 1576 | mro_internal(PyTypeObject *type) |
| 1577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | PyObject *mro, *result, *tuple; |
| 1579 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | if (Py_TYPE(type) == &PyType_Type) { |
| 1582 | result = mro_implementation(type); |
| 1583 | } |
| 1584 | else { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1585 | _Py_IDENTIFIER(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | checkit = 1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1587 | mro = lookup_method((PyObject *)type, &PyId_mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | if (mro == NULL) |
| 1589 | return -1; |
| 1590 | result = PyObject_CallObject(mro, NULL); |
| 1591 | Py_DECREF(mro); |
| 1592 | } |
| 1593 | if (result == NULL) |
| 1594 | return -1; |
| 1595 | tuple = PySequence_Tuple(result); |
| 1596 | Py_DECREF(result); |
| 1597 | if (tuple == NULL) |
| 1598 | return -1; |
| 1599 | if (checkit) { |
| 1600 | Py_ssize_t i, len; |
| 1601 | PyObject *cls; |
| 1602 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | for (i = 0; i < len; i++) { |
| 1609 | PyTypeObject *t; |
| 1610 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1611 | if (!PyType_Check(cls)) { |
| 1612 | PyErr_Format(PyExc_TypeError, |
| 1613 | "mro() returned a non-class ('%.500s')", |
| 1614 | Py_TYPE(cls)->tp_name); |
| 1615 | Py_DECREF(tuple); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | t = (PyTypeObject*)cls; |
| 1619 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1620 | PyErr_Format(PyExc_TypeError, |
| 1621 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1622 | t->tp_name); |
| 1623 | Py_DECREF(tuple); |
| 1624 | return -1; |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1631 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | from the custom MRO */ |
| 1633 | type_mro_modified(type, type->tp_bases); |
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 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | |
| 1641 | /* Calculate the best base amongst multiple base classes. |
| 1642 | This is the first one that's on the path to the "solid base". */ |
| 1643 | |
| 1644 | static PyTypeObject * |
| 1645 | best_base(PyObject *bases) |
| 1646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | Py_ssize_t i, n; |
| 1648 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1649 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | assert(PyTuple_Check(bases)); |
| 1652 | n = PyTuple_GET_SIZE(bases); |
| 1653 | assert(n > 0); |
| 1654 | base = NULL; |
| 1655 | winner = NULL; |
| 1656 | for (i = 0; i < n; i++) { |
| 1657 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1658 | if (!PyType_Check(base_proto)) { |
| 1659 | PyErr_SetString( |
| 1660 | PyExc_TypeError, |
| 1661 | "bases must be types"); |
| 1662 | return NULL; |
| 1663 | } |
| 1664 | base_i = (PyTypeObject *)base_proto; |
| 1665 | if (base_i->tp_dict == NULL) { |
| 1666 | if (PyType_Ready(base_i) < 0) |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | candidate = solid_base(base_i); |
| 1670 | if (winner == NULL) { |
| 1671 | winner = candidate; |
| 1672 | base = base_i; |
| 1673 | } |
| 1674 | else if (PyType_IsSubtype(winner, candidate)) |
| 1675 | ; |
| 1676 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1677 | winner = candidate; |
| 1678 | base = base_i; |
| 1679 | } |
| 1680 | else { |
| 1681 | PyErr_SetString( |
| 1682 | PyExc_TypeError, |
| 1683 | "multiple bases have " |
| 1684 | "instance lay-out conflict"); |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1688 | assert (base != NULL); |
| 1689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | static int |
| 1694 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | size_t t_size = type->tp_basicsize; |
| 1697 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1700 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1701 | /* If itemsize is involved, stricter rules */ |
| 1702 | return t_size != b_size || |
| 1703 | type->tp_itemsize != base->tp_itemsize; |
| 1704 | } |
| 1705 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1706 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1707 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1708 | t_size -= sizeof(PyObject *); |
| 1709 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1710 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1711 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1712 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | static PyTypeObject * |
| 1718 | solid_base(PyTypeObject *type) |
| 1719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | if (type->tp_base) |
| 1723 | base = solid_base(type->tp_base); |
| 1724 | else |
| 1725 | base = &PyBaseObject_Type; |
| 1726 | if (extra_ivars(type, base)) |
| 1727 | return type; |
| 1728 | else |
| 1729 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1732 | static void object_dealloc(PyObject *); |
| 1733 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1734 | static int update_slot(PyTypeObject *, PyObject *); |
| 1735 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1737 | /* |
| 1738 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1739 | * inherited from various builtin types. The builtin base usually provides |
| 1740 | * its own __dict__ descriptor, so we use that when we can. |
| 1741 | */ |
| 1742 | static PyTypeObject * |
| 1743 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | while (type->tp_base != NULL) { |
| 1746 | if (type->tp_dictoffset != 0 && |
| 1747 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1748 | return type; |
| 1749 | type = type->tp_base; |
| 1750 | } |
| 1751 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | static PyObject * |
| 1755 | get_dict_descriptor(PyTypeObject *type) |
| 1756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1758 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1759 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1761 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | static void |
| 1767 | raise_dict_descr_error(PyObject *obj) |
| 1768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | PyErr_Format(PyExc_TypeError, |
| 1770 | "this __dict__ descriptor does not support " |
| 1771 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1774 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1775 | subtype_dict(PyObject *obj, void *context) |
| 1776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1780 | if (base != NULL) { |
| 1781 | descrgetfunc func; |
| 1782 | PyObject *descr = get_dict_descriptor(base); |
| 1783 | if (descr == NULL) { |
| 1784 | raise_dict_descr_error(obj); |
| 1785 | return NULL; |
| 1786 | } |
| 1787 | func = Py_TYPE(descr)->tp_descr_get; |
| 1788 | if (func == NULL) { |
| 1789 | raise_dict_descr_error(obj); |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1793 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1794 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1797 | static int |
| 1798 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1799 | { |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1800 | PyObject *dict, **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1804 | if (base != NULL) { |
| 1805 | descrsetfunc func; |
| 1806 | PyObject *descr = get_dict_descriptor(base); |
| 1807 | if (descr == NULL) { |
| 1808 | raise_dict_descr_error(obj); |
| 1809 | return -1; |
| 1810 | } |
| 1811 | func = Py_TYPE(descr)->tp_descr_set; |
| 1812 | if (func == NULL) { |
| 1813 | raise_dict_descr_error(obj); |
| 1814 | return -1; |
| 1815 | } |
| 1816 | return func(descr, obj, value); |
| 1817 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1818 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | dictptr = _PyObject_GetDictPtr(obj); |
| 1820 | if (dictptr == NULL) { |
| 1821 | PyErr_SetString(PyExc_AttributeError, |
| 1822 | "This object has no __dict__"); |
| 1823 | return -1; |
| 1824 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 1825 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | PyErr_Format(PyExc_TypeError, |
| 1827 | "__dict__ must be set to a dictionary, " |
| 1828 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1829 | return -1; |
| 1830 | } |
| 1831 | dict = *dictptr; |
| 1832 | Py_XINCREF(value); |
| 1833 | *dictptr = value; |
| 1834 | Py_XDECREF(dict); |
| 1835 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1838 | static PyObject * |
| 1839 | subtype_getweakref(PyObject *obj, void *context) |
| 1840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | PyObject **weaklistptr; |
| 1842 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1845 | PyErr_SetString(PyExc_AttributeError, |
| 1846 | "This object has no __weakref__"); |
| 1847 | return NULL; |
| 1848 | } |
| 1849 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1850 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1851 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1852 | weaklistptr = (PyObject **) |
| 1853 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1854 | if (*weaklistptr == NULL) |
| 1855 | result = Py_None; |
| 1856 | else |
| 1857 | result = *weaklistptr; |
| 1858 | Py_INCREF(result); |
| 1859 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1862 | /* Three variants on the subtype_getsets list. */ |
| 1863 | |
| 1864 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | {"__dict__", subtype_dict, subtype_setdict, |
| 1866 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1867 | {"__weakref__", subtype_getweakref, NULL, |
| 1868 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1869 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1870 | }; |
| 1871 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1872 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | {"__dict__", subtype_dict, subtype_setdict, |
| 1874 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1875 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1876 | }; |
| 1877 | |
| 1878 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | {"__weakref__", subtype_getweakref, NULL, |
| 1880 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1881 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1882 | }; |
| 1883 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1884 | static int |
| 1885 | valid_identifier(PyObject *s) |
| 1886 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | if (!PyUnicode_Check(s)) { |
| 1888 | PyErr_Format(PyExc_TypeError, |
| 1889 | "__slots__ items must be strings, not '%.200s'", |
| 1890 | Py_TYPE(s)->tp_name); |
| 1891 | return 0; |
| 1892 | } |
| 1893 | if (!PyUnicode_IsIdentifier(s)) { |
| 1894 | PyErr_SetString(PyExc_TypeError, |
| 1895 | "__slots__ must be identifiers"); |
| 1896 | return 0; |
| 1897 | } |
| 1898 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1901 | /* Forward */ |
| 1902 | static int |
| 1903 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1904 | |
| 1905 | static int |
| 1906 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | assert(args != NULL && PyTuple_Check(args)); |
| 1911 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1914 | PyErr_SetString(PyExc_TypeError, |
| 1915 | "type.__init__() takes no keyword arguments"); |
| 1916 | return -1; |
| 1917 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | if (args != NULL && PyTuple_Check(args) && |
| 1920 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1921 | PyErr_SetString(PyExc_TypeError, |
| 1922 | "type.__init__() takes 1 or 3 arguments"); |
| 1923 | return -1; |
| 1924 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | /* Call object.__init__(self) now. */ |
| 1927 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1928 | args = PyTuple_GetSlice(args, 0, 0); |
| 1929 | res = object_init(cls, args, NULL); |
| 1930 | Py_DECREF(args); |
| 1931 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 1934 | long |
| 1935 | PyType_GetFlags(PyTypeObject *type) |
| 1936 | { |
| 1937 | return type->tp_flags; |
| 1938 | } |
| 1939 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 1940 | /* Determine the most derived metatype. */ |
| 1941 | PyTypeObject * |
| 1942 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 1943 | { |
| 1944 | Py_ssize_t i, nbases; |
| 1945 | PyTypeObject *winner; |
| 1946 | PyObject *tmp; |
| 1947 | PyTypeObject *tmptype; |
| 1948 | |
| 1949 | /* Determine the proper metatype to deal with this, |
| 1950 | and check for metatype conflicts while we're at it. |
| 1951 | Note that if some other metatype wins to contract, |
| 1952 | it's possible that its instances are not types. */ |
| 1953 | |
| 1954 | nbases = PyTuple_GET_SIZE(bases); |
| 1955 | winner = metatype; |
| 1956 | for (i = 0; i < nbases; i++) { |
| 1957 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1958 | tmptype = Py_TYPE(tmp); |
| 1959 | if (PyType_IsSubtype(winner, tmptype)) |
| 1960 | continue; |
| 1961 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1962 | winner = tmptype; |
| 1963 | continue; |
| 1964 | } |
| 1965 | /* else: */ |
| 1966 | PyErr_SetString(PyExc_TypeError, |
| 1967 | "metaclass conflict: " |
| 1968 | "the metaclass of a derived class " |
| 1969 | "must be a (non-strict) subclass " |
| 1970 | "of the metaclasses of all its bases"); |
| 1971 | return NULL; |
| 1972 | } |
| 1973 | return winner; |
| 1974 | } |
| 1975 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1976 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1977 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1978 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1979 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 1981 | PyObject *qualname, *slots = NULL, *tmp, *newslots; |
| 1982 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | PyHeapTypeObject *et; |
| 1984 | PyMemberDef *mp; |
| 1985 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1986 | int j, may_add_dict, may_add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1987 | _Py_IDENTIFIER(__qualname__); |
| 1988 | _Py_IDENTIFIER(__slots__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | assert(args != NULL && PyTuple_Check(args)); |
| 1991 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | /* Special case: type(x) should return x->ob_type */ |
| 1994 | { |
| 1995 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1996 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(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 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1999 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2000 | Py_INCREF(Py_TYPE(x)); |
| 2001 | return (PyObject *) Py_TYPE(x); |
| 2002 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2005 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2006 | a msg saying type() needs exactly 3. */ |
| 2007 | if (nargs + nkwds != 3) { |
| 2008 | PyErr_SetString(PyExc_TypeError, |
| 2009 | "type() takes 1 or 3 arguments"); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | /* Check arguments: (name, bases, dict) */ |
| 2015 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2016 | &name, |
| 2017 | &PyTuple_Type, &bases, |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2018 | &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2020 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2021 | /* Determine the proper metatype to deal with this: */ |
| 2022 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2023 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | return NULL; |
| 2025 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (winner != metatype) { |
| 2028 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2029 | return winner->tp_new(winner, args, kwds); |
| 2030 | metatype = winner; |
| 2031 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2034 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | if (nbases == 0) { |
| 2036 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2037 | if (bases == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2038 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | nbases = 1; |
| 2040 | } |
| 2041 | else |
| 2042 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | /* Calculate best base, and check that all bases are type objects */ |
| 2045 | base = best_base(bases); |
| 2046 | if (base == NULL) { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2047 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | } |
| 2049 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2050 | PyErr_Format(PyExc_TypeError, |
| 2051 | "type '%.100s' is not an acceptable base type", |
| 2052 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2053 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2055 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2056 | dict = PyDict_Copy(orig_dict); |
| 2057 | if (dict == NULL) |
| 2058 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2061 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | nslots = 0; |
| 2063 | add_dict = 0; |
| 2064 | add_weak = 0; |
| 2065 | may_add_dict = base->tp_dictoffset == 0; |
| 2066 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2067 | if (slots == NULL) { |
| 2068 | if (may_add_dict) { |
| 2069 | add_dict++; |
| 2070 | } |
| 2071 | if (may_add_weak) { |
| 2072 | add_weak++; |
| 2073 | } |
| 2074 | } |
| 2075 | else { |
| 2076 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | /* Make it into a tuple */ |
| 2079 | if (PyUnicode_Check(slots)) |
| 2080 | slots = PyTuple_Pack(1, slots); |
| 2081 | else |
| 2082 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2083 | if (slots == NULL) |
| 2084 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | /* Are slots allowed? */ |
| 2088 | nslots = PyTuple_GET_SIZE(slots); |
| 2089 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2090 | PyErr_Format(PyExc_TypeError, |
| 2091 | "nonempty __slots__ " |
| 2092 | "not supported for subtype of '%s'", |
| 2093 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2094 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | /* Check for valid slot names and two special cases */ |
| 2098 | for (i = 0; i < nslots; i++) { |
| 2099 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2100 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2101 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | assert(PyUnicode_Check(tmp)); |
| 2103 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2104 | if (!may_add_dict || add_dict) { |
| 2105 | PyErr_SetString(PyExc_TypeError, |
| 2106 | "__dict__ slot disallowed: " |
| 2107 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2108 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | } |
| 2110 | add_dict++; |
| 2111 | } |
| 2112 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2113 | if (!may_add_weak || add_weak) { |
| 2114 | PyErr_SetString(PyExc_TypeError, |
| 2115 | "__weakref__ slot disallowed: " |
| 2116 | "either we already got one, " |
| 2117 | "or __itemsize__ != 0"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2118 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | } |
| 2120 | add_weak++; |
| 2121 | } |
| 2122 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | /* Copy slots into a list, mangle names and sort them. |
| 2125 | Sorted names are needed for __class__ assignment. |
| 2126 | Convert them back to tuple at the end. |
| 2127 | */ |
| 2128 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2129 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2130 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | for (i = j = 0; i < nslots; i++) { |
| 2132 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2133 | if ((add_dict && |
| 2134 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2135 | (add_weak && |
| 2136 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2137 | continue; |
| 2138 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2139 | if (!tmp) { |
| 2140 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2141 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2142 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2144 | if (PyDict_GetItem(dict, tmp)) { |
| 2145 | PyErr_Format(PyExc_ValueError, |
| 2146 | "%R in __slots__ conflicts with class variable", |
| 2147 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2148 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2149 | goto error; |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2150 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2151 | j++; |
| 2152 | } |
| 2153 | assert(j == nslots - add_dict - add_weak); |
| 2154 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2155 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2158 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | } |
| 2160 | slots = PyList_AsTuple(newslots); |
| 2161 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2162 | if (slots == NULL) |
| 2163 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | /* Secondary bases may provide weakrefs or dict */ |
| 2166 | if (nbases > 1 && |
| 2167 | ((may_add_dict && !add_dict) || |
| 2168 | (may_add_weak && !add_weak))) { |
| 2169 | for (i = 0; i < nbases; i++) { |
| 2170 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2171 | if (tmp == (PyObject *)base) |
| 2172 | continue; /* Skip primary base */ |
| 2173 | assert(PyType_Check(tmp)); |
| 2174 | tmptype = (PyTypeObject *)tmp; |
| 2175 | if (may_add_dict && !add_dict && |
| 2176 | tmptype->tp_dictoffset != 0) |
| 2177 | add_dict++; |
| 2178 | if (may_add_weak && !add_weak && |
| 2179 | tmptype->tp_weaklistoffset != 0) |
| 2180 | add_weak++; |
| 2181 | if (may_add_dict && !add_dict) |
| 2182 | continue; |
| 2183 | if (may_add_weak && !add_weak) |
| 2184 | continue; |
| 2185 | /* Nothing more to check */ |
| 2186 | break; |
| 2187 | } |
| 2188 | } |
| 2189 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | /* Allocate the type object */ |
| 2192 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2193 | if (type == NULL) |
| 2194 | goto error; |
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 | /* Keep name and slots alive in the extended type object */ |
| 2197 | et = (PyHeapTypeObject *)type; |
| 2198 | Py_INCREF(name); |
| 2199 | et->ht_name = name; |
| 2200 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2201 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | /* Initialize tp_flags */ |
| 2204 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2205 | Py_TPFLAGS_BASETYPE; |
| 2206 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2207 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | /* Initialize essential fields */ |
| 2210 | type->tp_as_number = &et->as_number; |
| 2211 | type->tp_as_sequence = &et->as_sequence; |
| 2212 | type->tp_as_mapping = &et->as_mapping; |
| 2213 | type->tp_as_buffer = &et->as_buffer; |
| 2214 | type->tp_name = _PyUnicode_AsString(name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2215 | if (!type->tp_name) |
| 2216 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | /* Set tp_base and tp_bases */ |
| 2219 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2220 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | Py_INCREF(base); |
| 2222 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2225 | Py_INCREF(dict); |
| 2226 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2229 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | tmp = PyEval_GetGlobals(); |
| 2231 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2232 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2234 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2235 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2236 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2240 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2241 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2242 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2243 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2244 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2245 | if (qualname != NULL) { |
| 2246 | if (!PyUnicode_Check(qualname)) { |
| 2247 | PyErr_Format(PyExc_TypeError, |
| 2248 | "type __qualname__ must be a str, not %s", |
| 2249 | Py_TYPE(qualname)->tp_name); |
| 2250 | goto error; |
| 2251 | } |
| 2252 | } |
| 2253 | else { |
| 2254 | qualname = et->ht_name; |
| 2255 | } |
| 2256 | Py_INCREF(qualname); |
| 2257 | et->ht_qualname = qualname; |
| 2258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2260 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2261 | if that fails, it will still look into __dict__. |
| 2262 | */ |
| 2263 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2264 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2266 | Py_ssize_t len; |
| 2267 | char *doc_str; |
| 2268 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | doc_str = _PyUnicode_AsString(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2271 | if (doc_str == NULL) |
| 2272 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2274 | len = strlen(doc_str); |
| 2275 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2276 | if (tp_doc == NULL) |
| 2277 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | memcpy(tp_doc, doc_str, len + 1); |
| 2279 | type->tp_doc = tp_doc; |
| 2280 | } |
| 2281 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | /* Special-case __new__: if it's a plain function, |
| 2284 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2285 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2287 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2288 | if (tmp == NULL) |
| 2289 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2290 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) |
| 2291 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | Py_DECREF(tmp); |
| 2293 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2296 | mp = PyHeapType_GET_MEMBERS(et); |
| 2297 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2298 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | for (i = 0; i < nslots; i++, mp++) { |
| 2300 | mp->name = _PyUnicode_AsString( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2301 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2302 | if (mp->name == NULL) |
| 2303 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | mp->type = T_OBJECT_EX; |
| 2305 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | /* __dict__ and __weakref__ are already filtered out */ |
| 2308 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2309 | assert(strcmp(mp->name, "__weakref__") != 0); |
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 | slotoffset += sizeof(PyObject *); |
| 2312 | } |
| 2313 | } |
| 2314 | if (add_dict) { |
| 2315 | if (base->tp_itemsize) |
| 2316 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2317 | else |
| 2318 | type->tp_dictoffset = slotoffset; |
| 2319 | slotoffset += sizeof(PyObject *); |
| 2320 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2321 | if (type->tp_dictoffset) { |
| 2322 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2323 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | if (add_weak) { |
| 2325 | assert(!base->tp_itemsize); |
| 2326 | type->tp_weaklistoffset = slotoffset; |
| 2327 | slotoffset += sizeof(PyObject *); |
| 2328 | } |
| 2329 | type->tp_basicsize = slotoffset; |
| 2330 | type->tp_itemsize = base->tp_itemsize; |
| 2331 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2334 | type->tp_getset = subtype_getsets_full; |
| 2335 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2336 | type->tp_getset = subtype_getsets_weakref_only; |
| 2337 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2338 | type->tp_getset = subtype_getsets_dict_only; |
| 2339 | else |
| 2340 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | /* Special case some slots */ |
| 2343 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2344 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2345 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2346 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2347 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2348 | } |
| 2349 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | /* Enable GC unless there are really no instance variables possible */ |
| 2352 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2353 | type->tp_itemsize == 0)) |
| 2354 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | /* Always override allocation strategy to use regular heap */ |
| 2357 | type->tp_alloc = PyType_GenericAlloc; |
| 2358 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2359 | type->tp_free = PyObject_GC_Del; |
| 2360 | type->tp_traverse = subtype_traverse; |
| 2361 | type->tp_clear = subtype_clear; |
| 2362 | } |
| 2363 | else |
| 2364 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2367 | if (PyType_Ready(type) < 0) |
| 2368 | goto error; |
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 | /* Put the proper slots in place */ |
| 2371 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2372 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2373 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2375 | |
| 2376 | error: |
| 2377 | Py_XDECREF(dict); |
| 2378 | Py_XDECREF(bases); |
| 2379 | Py_XDECREF(slots); |
| 2380 | Py_XDECREF(type); |
| 2381 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2384 | static short slotoffsets[] = { |
| 2385 | -1, /* invalid slot */ |
| 2386 | #include "typeslots.inc" |
| 2387 | }; |
| 2388 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2389 | PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2390 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2391 | { |
| 2392 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2393 | PyTypeObject *type, *base; |
| 2394 | char *s; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2395 | char *res_start = (char*)res; |
| 2396 | PyType_Slot *slot; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2397 | |
| 2398 | /* Set the type name and qualname */ |
| 2399 | s = strrchr(spec->name, '.'); |
| 2400 | if (s == NULL) |
| 2401 | s = (char*)spec->name; |
| 2402 | else |
| 2403 | s++; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2404 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2405 | if (res == NULL) |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2406 | return NULL; |
| 2407 | type = &res->ht_type; |
Antoine Pitrou | 66a3a7e | 2012-06-24 00:42:59 +0200 | [diff] [blame] | 2408 | /* The flags must be initialized early, before the GC traverses us */ |
| 2409 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2410 | res->ht_name = PyUnicode_FromString(s); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2411 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2412 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2413 | res->ht_qualname = res->ht_name; |
| 2414 | Py_INCREF(res->ht_qualname); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2415 | type->tp_name = spec->name; |
| 2416 | if (!type->tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2417 | goto fail; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* Adjust for empty tuple bases */ |
| 2420 | if (!bases) { |
| 2421 | base = &PyBaseObject_Type; |
| 2422 | /* See whether Py_tp_base(s) was specified */ |
| 2423 | for (slot = spec->slots; slot->slot; slot++) { |
| 2424 | if (slot->slot == Py_tp_base) |
| 2425 | base = slot->pfunc; |
| 2426 | else if (slot->slot == Py_tp_bases) { |
| 2427 | bases = slot->pfunc; |
| 2428 | Py_INCREF(bases); |
| 2429 | } |
| 2430 | } |
| 2431 | if (!bases) |
| 2432 | bases = PyTuple_Pack(1, base); |
| 2433 | if (!bases) |
| 2434 | goto fail; |
| 2435 | } |
| 2436 | else |
| 2437 | Py_INCREF(bases); |
| 2438 | |
| 2439 | /* Calculate best base, and check that all bases are type objects */ |
| 2440 | base = best_base(bases); |
| 2441 | if (base == NULL) { |
| 2442 | goto fail; |
| 2443 | } |
| 2444 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2445 | PyErr_Format(PyExc_TypeError, |
| 2446 | "type '%.100s' is not an acceptable base type", |
| 2447 | base->tp_name); |
| 2448 | goto fail; |
| 2449 | } |
| 2450 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2451 | /* Initialize essential fields */ |
| 2452 | type->tp_as_number = &res->as_number; |
| 2453 | type->tp_as_sequence = &res->as_sequence; |
| 2454 | type->tp_as_mapping = &res->as_mapping; |
| 2455 | type->tp_as_buffer = &res->as_buffer; |
| 2456 | /* Set tp_base and tp_bases */ |
| 2457 | type->tp_bases = bases; |
| 2458 | bases = NULL; |
| 2459 | Py_INCREF(base); |
| 2460 | type->tp_base = base; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2461 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2462 | type->tp_basicsize = spec->basicsize; |
| 2463 | type->tp_itemsize = spec->itemsize; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2464 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2465 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2466 | if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2467 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2468 | goto fail; |
| 2469 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2470 | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
| 2471 | /* Processed above */ |
| 2472 | continue; |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2473 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2474 | |
| 2475 | /* need to make a copy of the docstring slot, which usually |
| 2476 | points to a static string literal */ |
| 2477 | if (slot->slot == Py_tp_doc) { |
Antoine Pitrou | 682d94c | 2012-05-14 14:43:03 +0200 | [diff] [blame] | 2478 | size_t len = strlen(slot->pfunc)+1; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2479 | char *tp_doc = PyObject_MALLOC(len); |
| 2480 | if (tp_doc == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2481 | goto fail; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2482 | memcpy(tp_doc, slot->pfunc, len); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2483 | type->tp_doc = tp_doc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2484 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2485 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2486 | if (type->tp_dictoffset) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2487 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2488 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2489 | if (type->tp_dealloc == NULL) { |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2490 | /* It's a heap type, so needs the heap types' dealloc. |
| 2491 | subtype_dealloc will call the base type's tp_dealloc, if |
| 2492 | necessary. */ |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2493 | type->tp_dealloc = subtype_dealloc; |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2494 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2496 | if (PyType_Ready(type) < 0) |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2497 | goto fail; |
| 2498 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2499 | /* Set type.__module__ */ |
| 2500 | s = strrchr(spec->name, '.'); |
| 2501 | if (s != NULL) |
| 2502 | _PyDict_SetItemId(type->tp_dict, &PyId___module__, |
| 2503 | PyUnicode_FromStringAndSize( |
| 2504 | spec->name, (Py_ssize_t)(s - spec->name))); |
| 2505 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2506 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2507 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2508 | fail: |
| 2509 | Py_DECREF(res); |
| 2510 | return NULL; |
| 2511 | } |
| 2512 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2513 | PyObject * |
| 2514 | PyType_FromSpec(PyType_Spec *spec) |
| 2515 | { |
| 2516 | return PyType_FromSpecWithBases(spec, NULL); |
| 2517 | } |
| 2518 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2519 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2520 | /* Internal API to look for a name through the MRO. |
| 2521 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2522 | PyObject * |
| 2523 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2524 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | Py_ssize_t i, n; |
| 2526 | PyObject *mro, *res, *base, *dict; |
| 2527 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2530 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2531 | /* fast path */ |
| 2532 | h = MCACHE_HASH_METHOD(type, name); |
| 2533 | if (method_cache[h].version == type->tp_version_tag && |
| 2534 | method_cache[h].name == name) |
| 2535 | return method_cache[h].value; |
| 2536 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | /* Look in tp_dict of types in MRO */ |
| 2539 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | /* If mro is NULL, the type is either not yet initialized |
| 2542 | by PyType_Ready(), or already cleared by type_clear(). |
| 2543 | Either way the safest thing to do is to return NULL. */ |
| 2544 | if (mro == NULL) |
| 2545 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2548 | /* keep a strong reference to mro because type->tp_mro can be replaced |
| 2549 | during PyDict_GetItem(dict, name) */ |
| 2550 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | assert(PyTuple_Check(mro)); |
| 2552 | n = PyTuple_GET_SIZE(mro); |
| 2553 | for (i = 0; i < n; i++) { |
| 2554 | base = PyTuple_GET_ITEM(mro, i); |
| 2555 | assert(PyType_Check(base)); |
| 2556 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2557 | assert(dict && PyDict_Check(dict)); |
| 2558 | res = PyDict_GetItem(dict, name); |
| 2559 | if (res != NULL) |
| 2560 | break; |
| 2561 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2562 | Py_DECREF(mro); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2565 | h = MCACHE_HASH_METHOD(type, name); |
| 2566 | method_cache[h].version = type->tp_version_tag; |
| 2567 | method_cache[h].value = res; /* borrowed */ |
| 2568 | Py_INCREF(name); |
| 2569 | Py_DECREF(method_cache[h].name); |
| 2570 | method_cache[h].name = name; |
| 2571 | } |
| 2572 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2575 | static PyObject * |
| 2576 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 2577 | { |
| 2578 | PyObject *oname; |
| 2579 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 2580 | if (oname == NULL) |
| 2581 | return NULL; |
| 2582 | return _PyType_Lookup(type, oname); |
| 2583 | } |
| 2584 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2585 | /* This is similar to PyObject_GenericGetAttr(), |
| 2586 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2587 | static PyObject * |
| 2588 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | PyTypeObject *metatype = Py_TYPE(type); |
| 2591 | PyObject *meta_attribute, *attribute; |
| 2592 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2593 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 2594 | if (!PyUnicode_Check(name)) { |
| 2595 | PyErr_Format(PyExc_TypeError, |
| 2596 | "attribute name must be string, not '%.200s'", |
| 2597 | name->ob_type->tp_name); |
| 2598 | return NULL; |
| 2599 | } |
| 2600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2602 | if (type->tp_dict == NULL) { |
| 2603 | if (PyType_Ready(type) < 0) |
| 2604 | return NULL; |
| 2605 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | /* No readable descriptor found yet */ |
| 2608 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | /* Look for the attribute in the metatype */ |
| 2611 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | if (meta_attribute != NULL) { |
| 2614 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2617 | /* Data descriptors implement tp_descr_set to intercept |
| 2618 | * writes. Assume the attribute is not overridden in |
| 2619 | * type's tp_dict (and bases): call the descriptor now. |
| 2620 | */ |
| 2621 | return meta_get(meta_attribute, (PyObject *)type, |
| 2622 | (PyObject *)metatype); |
| 2623 | } |
| 2624 | Py_INCREF(meta_attribute); |
| 2625 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2628 | * type and its bases */ |
| 2629 | attribute = _PyType_Lookup(type, name); |
| 2630 | if (attribute != NULL) { |
| 2631 | /* Implement descriptor functionality, if any */ |
| 2632 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | if (local_get != NULL) { |
| 2637 | /* NULL 2nd argument indicates the descriptor was |
| 2638 | * found on the target object itself (or a base) */ |
| 2639 | return local_get(attribute, (PyObject *)NULL, |
| 2640 | (PyObject *)type); |
| 2641 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | Py_INCREF(attribute); |
| 2644 | return attribute; |
| 2645 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | /* No attribute found in local __dict__ (or bases): use the |
| 2648 | * descriptor from the metatype, if any */ |
| 2649 | if (meta_get != NULL) { |
| 2650 | PyObject *res; |
| 2651 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2652 | (PyObject *)metatype); |
| 2653 | Py_DECREF(meta_attribute); |
| 2654 | return res; |
| 2655 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2658 | if (meta_attribute != NULL) { |
| 2659 | return meta_attribute; |
| 2660 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | /* Give up */ |
| 2663 | PyErr_Format(PyExc_AttributeError, |
| 2664 | "type object '%.50s' has no attribute '%U'", |
| 2665 | type->tp_name, name); |
| 2666 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | static int |
| 2670 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2673 | PyErr_Format( |
| 2674 | PyExc_TypeError, |
| 2675 | "can't set attributes of built-in/extension type '%s'", |
| 2676 | type->tp_name); |
| 2677 | return -1; |
| 2678 | } |
| 2679 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2680 | return -1; |
| 2681 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2684 | extern void |
| 2685 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
| 2686 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2687 | static void |
| 2688 | type_dealloc(PyTypeObject *type) |
| 2689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | /* Assert this is a heap-allocated type object */ |
| 2693 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2694 | _PyObject_GC_UNTRACK(type); |
| 2695 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2696 | et = (PyHeapTypeObject *)type; |
| 2697 | Py_XDECREF(type->tp_base); |
| 2698 | Py_XDECREF(type->tp_dict); |
| 2699 | Py_XDECREF(type->tp_bases); |
| 2700 | Py_XDECREF(type->tp_mro); |
| 2701 | Py_XDECREF(type->tp_cache); |
| 2702 | Py_XDECREF(type->tp_subclasses); |
| 2703 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2704 | * of most other objects. It's okay to cast it to char *. |
| 2705 | */ |
| 2706 | PyObject_Free((char *)type->tp_doc); |
| 2707 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2708 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | Py_XDECREF(et->ht_slots); |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2710 | if (et->ht_cached_keys) |
| 2711 | _PyDictKeys_DecRef(et->ht_cached_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2715 | static PyObject * |
| 2716 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | PyObject *list, *raw, *ref; |
| 2719 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | list = PyList_New(0); |
| 2722 | if (list == NULL) |
| 2723 | return NULL; |
| 2724 | raw = type->tp_subclasses; |
| 2725 | if (raw == NULL) |
| 2726 | return list; |
| 2727 | assert(PyList_Check(raw)); |
| 2728 | n = PyList_GET_SIZE(raw); |
| 2729 | for (i = 0; i < n; i++) { |
| 2730 | ref = PyList_GET_ITEM(raw, i); |
| 2731 | assert(PyWeakref_CheckRef(ref)); |
| 2732 | ref = PyWeakref_GET_OBJECT(ref); |
| 2733 | if (ref != Py_None) { |
| 2734 | if (PyList_Append(list, ref) < 0) { |
| 2735 | Py_DECREF(list); |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2743 | static PyObject * |
| 2744 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2749 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2750 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2751 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2752 | defined, as it's expected that only the final set of dict keys is |
| 2753 | interesting. |
| 2754 | Return 0 on success, -1 on error. |
| 2755 | */ |
| 2756 | |
| 2757 | static int |
| 2758 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2759 | { |
| 2760 | PyObject *classdict; |
| 2761 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2762 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2763 | |
| 2764 | assert(PyDict_Check(dict)); |
| 2765 | assert(aclass); |
| 2766 | |
| 2767 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2768 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2769 | if (classdict == NULL) |
| 2770 | PyErr_Clear(); |
| 2771 | else { |
| 2772 | int status = PyDict_Update(dict, classdict); |
| 2773 | Py_DECREF(classdict); |
| 2774 | if (status < 0) |
| 2775 | return -1; |
| 2776 | } |
| 2777 | |
| 2778 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2779 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2780 | if (bases == NULL) |
| 2781 | PyErr_Clear(); |
| 2782 | else { |
| 2783 | /* We have no guarantee that bases is a real tuple */ |
| 2784 | Py_ssize_t i, n; |
| 2785 | n = PySequence_Size(bases); /* This better be right */ |
| 2786 | if (n < 0) |
| 2787 | PyErr_Clear(); |
| 2788 | else { |
| 2789 | for (i = 0; i < n; i++) { |
| 2790 | int status; |
| 2791 | PyObject *base = PySequence_GetItem(bases, i); |
| 2792 | if (base == NULL) { |
| 2793 | Py_DECREF(bases); |
| 2794 | return -1; |
| 2795 | } |
| 2796 | status = merge_class_dict(dict, base); |
| 2797 | Py_DECREF(base); |
| 2798 | if (status < 0) { |
| 2799 | Py_DECREF(bases); |
| 2800 | return -1; |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | Py_DECREF(bases); |
| 2805 | } |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
| 2809 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2810 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2811 | metaclass would probably be more confusing than helpful. |
| 2812 | */ |
| 2813 | static PyObject * |
| 2814 | type_dir(PyObject *self, PyObject *args) |
| 2815 | { |
| 2816 | PyObject *result = NULL; |
| 2817 | PyObject *dict = PyDict_New(); |
| 2818 | |
| 2819 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 2820 | result = PyDict_Keys(dict); |
| 2821 | |
| 2822 | Py_XDECREF(dict); |
| 2823 | return result; |
| 2824 | } |
| 2825 | |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2826 | static PyObject* |
| 2827 | type_sizeof(PyObject *self, PyObject *args_unused) |
| 2828 | { |
| 2829 | Py_ssize_t size; |
| 2830 | PyTypeObject *type = (PyTypeObject*)self; |
| 2831 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 2832 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 2833 | size = sizeof(PyHeapTypeObject); |
| 2834 | if (et->ht_cached_keys) |
| 2835 | size += _PyDict_KeysSize(et->ht_cached_keys); |
| 2836 | } |
| 2837 | else |
| 2838 | size = sizeof(PyTypeObject); |
| 2839 | return PyLong_FromSsize_t(size); |
| 2840 | } |
| 2841 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2842 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2844 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2845 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2846 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2847 | {"__prepare__", (PyCFunction)type_prepare, |
| 2848 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2849 | PyDoc_STR("__prepare__() -> dict\n" |
| 2850 | "used to create the namespace for the class statement")}, |
| 2851 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2852 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2854 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2855 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 2856 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2857 | {"__sizeof__", type_sizeof, METH_NOARGS, |
| 2858 | "__sizeof__() -> int\nreturn memory consumption of the type object"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2860 | }; |
| 2861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2862 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2863 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2864 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2865 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2866 | static int |
| 2867 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | /* Because of type_is_gc(), the collector only calls this |
| 2870 | for heaptypes. */ |
Antoine Pitrou | 1351ca6 | 2012-06-24 00:30:12 +0200 | [diff] [blame] | 2871 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2872 | char msg[200]; |
| 2873 | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
| 2874 | type->tp_name); |
| 2875 | Py_FatalError(msg); |
| 2876 | } |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | Py_VISIT(type->tp_dict); |
| 2879 | Py_VISIT(type->tp_cache); |
| 2880 | Py_VISIT(type->tp_mro); |
| 2881 | Py_VISIT(type->tp_bases); |
| 2882 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | /* There's no need to visit type->tp_subclasses or |
| 2885 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2886 | in cycles; tp_subclasses is a list of weak references, |
| 2887 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | static int |
| 2893 | type_clear(PyTypeObject *type) |
| 2894 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2895 | PyDictKeysObject *cached_keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | /* Because of type_is_gc(), the collector only calls this |
| 2897 | for heaptypes. */ |
| 2898 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2900 | /* We need to invalidate the method cache carefully before clearing |
| 2901 | the dict, so that other objects caught in a reference cycle |
| 2902 | don't start calling destroyed methods. |
| 2903 | |
| 2904 | Otherwise, the only field we need to clear is tp_mro, which is |
| 2905 | part of a hard cycle (its first element is the class itself) that |
| 2906 | 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] | 2907 | tp_clear handler). None of the other fields need to be |
| 2908 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | tp_cache: |
| 2911 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | tp_bases, tp_base: |
| 2914 | If these are involved in a cycle, there must be at least |
| 2915 | one other, mutable object in the cycle, e.g. a base |
| 2916 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | tp_subclasses: |
| 2919 | A list of weak references can't be part of a cycle; and |
| 2920 | lists have their own tp_clear. |
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 | slots (in PyHeapTypeObject): |
| 2923 | A tuple of strings can't be part of a cycle. |
| 2924 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2926 | PyType_Modified(type); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2927 | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
| 2928 | if (cached_keys != NULL) { |
| 2929 | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
| 2930 | _PyDictKeys_DecRef(cached_keys); |
| 2931 | } |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 2932 | if (type->tp_dict) |
| 2933 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | static int |
| 2940 | type_is_gc(PyTypeObject *type) |
| 2941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2945 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2947 | "type", /* tp_name */ |
| 2948 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2949 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2950 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2951 | 0, /* tp_print */ |
| 2952 | 0, /* tp_getattr */ |
| 2953 | 0, /* tp_setattr */ |
| 2954 | 0, /* tp_reserved */ |
| 2955 | (reprfunc)type_repr, /* tp_repr */ |
| 2956 | 0, /* tp_as_number */ |
| 2957 | 0, /* tp_as_sequence */ |
| 2958 | 0, /* tp_as_mapping */ |
| 2959 | 0, /* tp_hash */ |
| 2960 | (ternaryfunc)type_call, /* tp_call */ |
| 2961 | 0, /* tp_str */ |
| 2962 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2963 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2964 | 0, /* tp_as_buffer */ |
| 2965 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2966 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2967 | type_doc, /* tp_doc */ |
| 2968 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2969 | (inquiry)type_clear, /* tp_clear */ |
| 2970 | 0, /* tp_richcompare */ |
| 2971 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2972 | 0, /* tp_iter */ |
| 2973 | 0, /* tp_iternext */ |
| 2974 | type_methods, /* tp_methods */ |
| 2975 | type_members, /* tp_members */ |
| 2976 | type_getsets, /* tp_getset */ |
| 2977 | 0, /* tp_base */ |
| 2978 | 0, /* tp_dict */ |
| 2979 | 0, /* tp_descr_get */ |
| 2980 | 0, /* tp_descr_set */ |
| 2981 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2982 | type_init, /* tp_init */ |
| 2983 | 0, /* tp_alloc */ |
| 2984 | type_new, /* tp_new */ |
| 2985 | PyObject_GC_Del, /* tp_free */ |
| 2986 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2987 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2988 | |
| 2989 | |
| 2990 | /* The base type of all types (eventually)... except itself. */ |
| 2991 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2992 | /* You may wonder why object.__new__() only complains about arguments |
| 2993 | when object.__init__() is not overridden, and vice versa. |
| 2994 | |
| 2995 | Consider the use cases: |
| 2996 | |
| 2997 | 1. When neither is overridden, we want to hear complaints about |
| 2998 | excess (i.e., any) arguments, since their presence could |
| 2999 | indicate there's a bug. |
| 3000 | |
| 3001 | 2. When defining an Immutable type, we are likely to override only |
| 3002 | __new__(), since __init__() is called too late to initialize an |
| 3003 | Immutable object. Since __new__() defines the signature for the |
| 3004 | type, it would be a pain to have to override __init__() just to |
| 3005 | stop it from complaining about excess arguments. |
| 3006 | |
| 3007 | 3. When defining a Mutable type, we are likely to override only |
| 3008 | __init__(). So here the converse reasoning applies: we don't |
| 3009 | want to have to override __new__() just to stop it from |
| 3010 | complaining. |
| 3011 | |
| 3012 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 3013 | object.__init__(), the latter should complain about excess |
| 3014 | arguments; ditto for __new__(). |
| 3015 | |
| 3016 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 3017 | excess arguments. The best solution that addresses all four use |
| 3018 | cases is as follows: __init__() complains about excess arguments |
| 3019 | unless __new__() is overridden and __init__() is not overridden |
| 3020 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 3021 | symmetrically, __new__() complains about excess arguments unless |
| 3022 | __init__() is overridden and __new__() is not overridden |
| 3023 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 3024 | |
| 3025 | However, for backwards compatibility, this breaks too much code. |
| 3026 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 3027 | methods are overridden; for all other cases we'll use the above |
| 3028 | rules. |
| 3029 | |
| 3030 | */ |
| 3031 | |
| 3032 | /* Forward */ |
| 3033 | static PyObject * |
| 3034 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3035 | |
| 3036 | static int |
| 3037 | excess_args(PyObject *args, PyObject *kwds) |
| 3038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | return PyTuple_GET_SIZE(args) || |
| 3040 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3043 | static int |
| 3044 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3045 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | int err = 0; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3047 | PyTypeObject *type = Py_TYPE(self); |
| 3048 | if (excess_args(args, kwds) && |
| 3049 | (type->tp_new == object_new || type->tp_init != object_init)) { |
| 3050 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
| 3051 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | } |
| 3053 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3056 | static PyObject * |
| 3057 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3058 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3059 | if (excess_args(args, kwds) && |
| 3060 | (type->tp_init == object_init || type->tp_new != object_new)) { |
| 3061 | PyErr_SetString(PyExc_TypeError, "object.__new__() takes no parameters"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | return NULL; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3063 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | PyObject *abstract_methods = NULL; |
| 3067 | PyObject *builtins; |
| 3068 | PyObject *sorted; |
| 3069 | PyObject *sorted_methods = NULL; |
| 3070 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3071 | PyObject *comma; |
| 3072 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3073 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 3076 | into joined. */ |
| 3077 | abstract_methods = type_abstractmethods(type, NULL); |
| 3078 | if (abstract_methods == NULL) |
| 3079 | goto error; |
| 3080 | builtins = PyEval_GetBuiltins(); |
| 3081 | if (builtins == NULL) |
| 3082 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3083 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | if (sorted == NULL) |
| 3085 | goto error; |
| 3086 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 3087 | abstract_methods, |
| 3088 | NULL); |
| 3089 | if (sorted_methods == NULL) |
| 3090 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3091 | comma = _PyUnicode_FromId(&comma_id); |
| 3092 | if (comma == NULL) |
| 3093 | goto error; |
| 3094 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | if (joined == NULL) |
| 3096 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | PyErr_Format(PyExc_TypeError, |
| 3099 | "Can't instantiate abstract class %s " |
| 3100 | "with abstract methods %U", |
| 3101 | type->tp_name, |
| 3102 | joined); |
| 3103 | error: |
| 3104 | Py_XDECREF(joined); |
| 3105 | Py_XDECREF(sorted_methods); |
| 3106 | Py_XDECREF(abstract_methods); |
| 3107 | return NULL; |
| 3108 | } |
| 3109 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3112 | static void |
| 3113 | object_dealloc(PyObject *self) |
| 3114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3118 | static PyObject * |
| 3119 | object_repr(PyObject *self) |
| 3120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | PyTypeObject *type; |
| 3122 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | type = Py_TYPE(self); |
| 3125 | mod = type_module(type, NULL); |
| 3126 | if (mod == NULL) |
| 3127 | PyErr_Clear(); |
| 3128 | else if (!PyUnicode_Check(mod)) { |
| 3129 | Py_DECREF(mod); |
| 3130 | mod = NULL; |
| 3131 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3132 | name = type_qualname(type, NULL); |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3133 | if (name == NULL) { |
| 3134 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | return NULL; |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3136 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 3138 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3139 | else |
| 3140 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3141 | type->tp_name, self); |
| 3142 | Py_XDECREF(mod); |
| 3143 | Py_DECREF(name); |
| 3144 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3147 | static PyObject * |
| 3148 | object_str(PyObject *self) |
| 3149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | f = Py_TYPE(self)->tp_repr; |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 3153 | if (f == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | f = object_repr; |
| 3155 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3158 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3159 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | case Py_EQ: |
| 3166 | /* Return NotImplemented instead of False, so if two |
| 3167 | objects are compared, both get a chance at the |
| 3168 | comparison. See issue #1393. */ |
| 3169 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3170 | Py_INCREF(res); |
| 3171 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | case Py_NE: |
| 3174 | /* By default, != returns the opposite of ==, |
| 3175 | unless the latter returns NotImplemented. */ |
| 3176 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3177 | if (res != NULL && res != Py_NotImplemented) { |
| 3178 | int ok = PyObject_IsTrue(res); |
| 3179 | Py_DECREF(res); |
| 3180 | if (ok < 0) |
| 3181 | res = NULL; |
| 3182 | else { |
| 3183 | if (ok) |
| 3184 | res = Py_False; |
| 3185 | else |
| 3186 | res = Py_True; |
| 3187 | Py_INCREF(res); |
| 3188 | } |
| 3189 | } |
| 3190 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | default: |
| 3193 | res = Py_NotImplemented; |
| 3194 | Py_INCREF(res); |
| 3195 | break; |
| 3196 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3202 | object_get_class(PyObject *self, void *closure) |
| 3203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | Py_INCREF(Py_TYPE(self)); |
| 3205 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | static int |
| 3209 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | return a == b || |
| 3212 | (a != NULL && |
| 3213 | b != NULL && |
| 3214 | a->tp_basicsize == b->tp_basicsize && |
| 3215 | a->tp_itemsize == b->tp_itemsize && |
| 3216 | a->tp_dictoffset == b->tp_dictoffset && |
| 3217 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3218 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3219 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | static int |
| 3223 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | PyTypeObject *base = a->tp_base; |
| 3226 | Py_ssize_t size; |
| 3227 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3228 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3229 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | size = base->tp_basicsize; |
| 3231 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3232 | size += sizeof(PyObject *); |
| 3233 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3234 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | /* Check slots compliance */ |
| 3237 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3238 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3239 | if (slots_a && slots_b) { |
| 3240 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3241 | return 0; |
| 3242 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3243 | } |
| 3244 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3248 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3253 | newto->tp_free != oldto->tp_free) |
| 3254 | { |
| 3255 | PyErr_Format(PyExc_TypeError, |
| 3256 | "%s assignment: " |
| 3257 | "'%s' deallocator differs from '%s'", |
| 3258 | attr, |
| 3259 | newto->tp_name, |
| 3260 | oldto->tp_name); |
| 3261 | return 0; |
| 3262 | } |
| 3263 | newbase = newto; |
| 3264 | oldbase = oldto; |
| 3265 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3266 | newbase = newbase->tp_base; |
| 3267 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3268 | oldbase = oldbase->tp_base; |
| 3269 | if (newbase != oldbase && |
| 3270 | (newbase->tp_base != oldbase->tp_base || |
| 3271 | !same_slots_added(newbase, oldbase))) { |
| 3272 | PyErr_Format(PyExc_TypeError, |
| 3273 | "%s assignment: " |
| 3274 | "'%s' object layout differs from '%s'", |
| 3275 | attr, |
| 3276 | newto->tp_name, |
| 3277 | oldto->tp_name); |
| 3278 | return 0; |
| 3279 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3285 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | PyTypeObject *oldto = Py_TYPE(self); |
| 3288 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | if (value == NULL) { |
| 3291 | PyErr_SetString(PyExc_TypeError, |
| 3292 | "can't delete __class__ attribute"); |
| 3293 | return -1; |
| 3294 | } |
| 3295 | if (!PyType_Check(value)) { |
| 3296 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3297 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | Py_TYPE(value)->tp_name); |
| 3299 | return -1; |
| 3300 | } |
| 3301 | newto = (PyTypeObject *)value; |
| 3302 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3303 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3304 | { |
| 3305 | PyErr_Format(PyExc_TypeError, |
| 3306 | "__class__ assignment: only for heap types"); |
| 3307 | return -1; |
| 3308 | } |
| 3309 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3310 | Py_INCREF(newto); |
| 3311 | Py_TYPE(self) = newto; |
| 3312 | Py_DECREF(oldto); |
| 3313 | return 0; |
| 3314 | } |
| 3315 | else { |
| 3316 | return -1; |
| 3317 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | {"__class__", object_get_class, object_set_class, |
| 3322 | PyDoc_STR("the object's class")}, |
| 3323 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3324 | }; |
| 3325 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3326 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3327 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3328 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3329 | - pickle protocols < 2 |
| 3330 | - calculating the list of slot names (done only once per class) |
| 3331 | - the __newobj__ function (which is used as a token but never called) |
| 3332 | */ |
| 3333 | |
| 3334 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3335 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | static PyObject *copyreg_str; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3338 | static PyObject *mod_copyreg = NULL; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | if (!copyreg_str) { |
| 3341 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3342 | if (copyreg_str == NULL) |
| 3343 | return NULL; |
| 3344 | } |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3345 | if (!mod_copyreg) { |
| 3346 | mod_copyreg = PyImport_Import(copyreg_str); |
| 3347 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3348 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3349 | Py_XINCREF(mod_copyreg); |
| 3350 | return mod_copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | static PyObject * |
| 3354 | slotnames(PyObject *cls) |
| 3355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | PyObject *clsdict; |
| 3357 | PyObject *copyreg; |
| 3358 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3359 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3360 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3363 | slotnames = _PyDict_GetItemId(clsdict, &PyId___slotnames__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3365 | Py_INCREF(slotnames); |
| 3366 | return slotnames; |
| 3367 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | copyreg = import_copyreg(); |
| 3370 | if (copyreg == NULL) |
| 3371 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3372 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3373 | slotnames = _PyObject_CallMethodId(copyreg, &PyId__slotnames, "O", cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3374 | Py_DECREF(copyreg); |
| 3375 | if (slotnames != NULL && |
| 3376 | slotnames != Py_None && |
| 3377 | !PyList_Check(slotnames)) |
| 3378 | { |
| 3379 | PyErr_SetString(PyExc_TypeError, |
| 3380 | "copyreg._slotnames didn't return a list or None"); |
| 3381 | Py_DECREF(slotnames); |
| 3382 | slotnames = NULL; |
| 3383 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3385 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | static PyObject * |
| 3389 | reduce_2(PyObject *obj) |
| 3390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3391 | PyObject *cls, *getnewargs; |
| 3392 | PyObject *args = NULL, *args2 = NULL; |
| 3393 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3394 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3395 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3396 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3397 | _Py_IDENTIFIER(__getnewargs__); |
| 3398 | _Py_IDENTIFIER(__getstate__); |
| 3399 | _Py_IDENTIFIER(__newobj__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3400 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3401 | cls = (PyObject *) Py_TYPE(obj); |
| 3402 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3403 | getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | if (getnewargs != NULL) { |
| 3405 | args = PyObject_CallObject(getnewargs, NULL); |
| 3406 | Py_DECREF(getnewargs); |
| 3407 | if (args != NULL && !PyTuple_Check(args)) { |
| 3408 | PyErr_Format(PyExc_TypeError, |
| 3409 | "__getnewargs__ should return a tuple, " |
| 3410 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3411 | goto end; |
| 3412 | } |
| 3413 | } |
| 3414 | else { |
| 3415 | PyErr_Clear(); |
| 3416 | args = PyTuple_New(0); |
| 3417 | } |
| 3418 | if (args == NULL) |
| 3419 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3420 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3421 | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3422 | if (getstate != NULL) { |
| 3423 | state = PyObject_CallObject(getstate, NULL); |
| 3424 | Py_DECREF(getstate); |
| 3425 | if (state == NULL) |
| 3426 | goto end; |
| 3427 | } |
| 3428 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3429 | PyObject **dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | PyErr_Clear(); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3431 | dict = _PyObject_GetDictPtr(obj); |
| 3432 | if (dict && *dict) |
| 3433 | state = *dict; |
| 3434 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | state = Py_None; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3436 | Py_INCREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | names = slotnames(cls); |
| 3438 | if (names == NULL) |
| 3439 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3440 | if (names != Py_None && PyList_GET_SIZE(names) > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | assert(PyList_Check(names)); |
| 3442 | slots = PyDict_New(); |
| 3443 | if (slots == NULL) |
| 3444 | goto end; |
| 3445 | n = 0; |
| 3446 | /* Can't pre-compute the list size; the list |
| 3447 | is stored on the class so accessible to other |
| 3448 | threads, which may be run by DECREF */ |
| 3449 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3450 | PyObject *name, *value; |
| 3451 | name = PyList_GET_ITEM(names, i); |
| 3452 | value = PyObject_GetAttr(obj, name); |
| 3453 | if (value == NULL) |
| 3454 | PyErr_Clear(); |
| 3455 | else { |
| 3456 | int err = PyDict_SetItem(slots, name, |
| 3457 | value); |
| 3458 | Py_DECREF(value); |
| 3459 | if (err) |
| 3460 | goto end; |
| 3461 | n++; |
| 3462 | } |
| 3463 | } |
| 3464 | if (n) { |
| 3465 | state = Py_BuildValue("(NO)", state, slots); |
| 3466 | if (state == NULL) |
| 3467 | goto end; |
| 3468 | } |
| 3469 | } |
| 3470 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | if (!PyList_Check(obj)) { |
| 3473 | listitems = Py_None; |
| 3474 | Py_INCREF(listitems); |
| 3475 | } |
| 3476 | else { |
| 3477 | listitems = PyObject_GetIter(obj); |
| 3478 | if (listitems == NULL) |
| 3479 | goto end; |
| 3480 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | if (!PyDict_Check(obj)) { |
| 3483 | dictitems = Py_None; |
| 3484 | Py_INCREF(dictitems); |
| 3485 | } |
| 3486 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3487 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3488 | PyObject *items = _PyObject_CallMethodId(obj, &PyId_items, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | if (items == NULL) |
| 3490 | goto end; |
| 3491 | dictitems = PyObject_GetIter(items); |
| 3492 | Py_DECREF(items); |
| 3493 | if (dictitems == NULL) |
| 3494 | goto end; |
| 3495 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3497 | copyreg = import_copyreg(); |
| 3498 | if (copyreg == NULL) |
| 3499 | goto end; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3500 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | if (newobj == NULL) |
| 3502 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | n = PyTuple_GET_SIZE(args); |
| 3505 | args2 = PyTuple_New(n+1); |
| 3506 | if (args2 == NULL) |
| 3507 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3508 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | for (i = 0; i < n; i++) { |
| 3511 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3512 | Py_INCREF(v); |
| 3513 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3514 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3517 | |
| 3518 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | Py_XDECREF(args); |
| 3520 | Py_XDECREF(args2); |
| 3521 | Py_XDECREF(slots); |
| 3522 | Py_XDECREF(state); |
| 3523 | Py_XDECREF(names); |
| 3524 | Py_XDECREF(listitems); |
| 3525 | Py_XDECREF(dictitems); |
| 3526 | Py_XDECREF(copyreg); |
| 3527 | Py_XDECREF(newobj); |
| 3528 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3531 | /* |
| 3532 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3533 | * were implemented in the same function: |
| 3534 | * - trying to pickle an object with a custom __reduce__ method that |
| 3535 | * fell back to object.__reduce__ in certain circumstances led to |
| 3536 | * infinite recursion at Python level and eventual RuntimeError. |
| 3537 | * - Pickling objects that lied about their type by overwriting the |
| 3538 | * __class__ descriptor could lead to infinite recursion at C level |
| 3539 | * and eventual segfault. |
| 3540 | * |
| 3541 | * Because of backwards compatibility, the two methods still have to |
| 3542 | * behave in the same way, even if this is not required by the pickle |
| 3543 | * protocol. This common functionality was moved to the _common_reduce |
| 3544 | * function. |
| 3545 | */ |
| 3546 | static PyObject * |
| 3547 | _common_reduce(PyObject *self, int proto) |
| 3548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | if (proto >= 2) |
| 3552 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | copyreg = import_copyreg(); |
| 3555 | if (!copyreg) |
| 3556 | return NULL; |
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 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3559 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | static PyObject * |
| 3565 | object_reduce(PyObject *self, PyObject *args) |
| 3566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3570 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3575 | static PyObject * |
| 3576 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3577 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3578 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3579 | PyObject *reduce, *res; |
| 3580 | int proto = 0; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3581 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3584 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3585 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3586 | if (objreduce == NULL) { |
| 3587 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 3588 | &PyId___reduce__); |
| 3589 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3590 | return NULL; |
| 3591 | } |
| 3592 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3593 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | if (reduce == NULL) |
| 3595 | PyErr_Clear(); |
| 3596 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3597 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3599 | |
| 3600 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3601 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | if (clsreduce == NULL) { |
| 3603 | Py_DECREF(reduce); |
| 3604 | return NULL; |
| 3605 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | override = (clsreduce != objreduce); |
| 3607 | Py_DECREF(clsreduce); |
| 3608 | if (override) { |
| 3609 | res = PyObject_CallObject(reduce, NULL); |
| 3610 | Py_DECREF(reduce); |
| 3611 | return res; |
| 3612 | } |
| 3613 | else |
| 3614 | Py_DECREF(reduce); |
| 3615 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3620 | static PyObject * |
| 3621 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3622 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3623 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3627 | "Abstract classes can override this to customize issubclass().\n" |
| 3628 | "\n" |
| 3629 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3630 | "It should return True, False or NotImplemented. If it returns\n" |
| 3631 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3632 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3633 | |
| 3634 | /* |
| 3635 | from PEP 3101, this code implements: |
| 3636 | |
| 3637 | class object: |
| 3638 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3639 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3640 | */ |
| 3641 | static PyObject * |
| 3642 | object_format(PyObject *self, PyObject *args) |
| 3643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | PyObject *format_spec; |
| 3645 | PyObject *self_as_str = NULL; |
| 3646 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3649 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3652 | if (self_as_str != NULL) { |
| 3653 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3654 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3655 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3656 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3657 | "object.__format__ with a non-empty format " |
| 3658 | "string is deprecated", 1) < 0) { |
| 3659 | goto done; |
| 3660 | } |
| 3661 | /* Eventually this will become an error: |
| 3662 | PyErr_Format(PyExc_TypeError, |
| 3663 | "non-empty format string passed to object.__format__"); |
| 3664 | goto done; |
| 3665 | */ |
| 3666 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3667 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3668 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3677 | static PyObject * |
| 3678 | object_sizeof(PyObject *self, PyObject *args) |
| 3679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3682 | res = 0; |
| 3683 | isize = self->ob_type->tp_itemsize; |
| 3684 | if (isize > 0) |
| 3685 | res = Py_SIZE(self->ob_type) * isize; |
| 3686 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3688 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3691 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 3692 | and recursively up the __class__.__bases__ chain. |
| 3693 | */ |
| 3694 | static PyObject * |
| 3695 | object_dir(PyObject *self, PyObject *args) |
| 3696 | { |
| 3697 | PyObject *result = NULL; |
| 3698 | PyObject *dict = NULL; |
| 3699 | PyObject *itsclass = NULL; |
| 3700 | |
| 3701 | /* 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] | 3702 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3703 | if (dict == NULL) { |
| 3704 | PyErr_Clear(); |
| 3705 | dict = PyDict_New(); |
| 3706 | } |
| 3707 | else if (!PyDict_Check(dict)) { |
| 3708 | Py_DECREF(dict); |
| 3709 | dict = PyDict_New(); |
| 3710 | } |
| 3711 | else { |
| 3712 | /* Copy __dict__ to avoid mutating it. */ |
| 3713 | PyObject *temp = PyDict_Copy(dict); |
| 3714 | Py_DECREF(dict); |
| 3715 | dict = temp; |
| 3716 | } |
| 3717 | |
| 3718 | if (dict == NULL) |
| 3719 | goto error; |
| 3720 | |
| 3721 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3722 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3723 | if (itsclass == NULL) |
| 3724 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 3725 | __class__ exists? */ |
| 3726 | PyErr_Clear(); |
| 3727 | else if (merge_class_dict(dict, itsclass) != 0) |
| 3728 | goto error; |
| 3729 | |
| 3730 | result = PyDict_Keys(dict); |
| 3731 | /* fall through */ |
| 3732 | error: |
| 3733 | Py_XDECREF(itsclass); |
| 3734 | Py_XDECREF(dict); |
| 3735 | return result; |
| 3736 | } |
| 3737 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3738 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3740 | PyDoc_STR("helper for pickle")}, |
| 3741 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3742 | PyDoc_STR("helper for pickle")}, |
| 3743 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3744 | object_subclasshook_doc}, |
| 3745 | {"__format__", object_format, METH_VARARGS, |
| 3746 | PyDoc_STR("default object formatter")}, |
| 3747 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3748 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3749 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3750 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3751 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3752 | }; |
| 3753 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3754 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3755 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3757 | "object", /* tp_name */ |
| 3758 | sizeof(PyObject), /* tp_basicsize */ |
| 3759 | 0, /* tp_itemsize */ |
| 3760 | object_dealloc, /* tp_dealloc */ |
| 3761 | 0, /* tp_print */ |
| 3762 | 0, /* tp_getattr */ |
| 3763 | 0, /* tp_setattr */ |
| 3764 | 0, /* tp_reserved */ |
| 3765 | object_repr, /* tp_repr */ |
| 3766 | 0, /* tp_as_number */ |
| 3767 | 0, /* tp_as_sequence */ |
| 3768 | 0, /* tp_as_mapping */ |
| 3769 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3770 | 0, /* tp_call */ |
| 3771 | object_str, /* tp_str */ |
| 3772 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3773 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3774 | 0, /* tp_as_buffer */ |
| 3775 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3776 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3777 | 0, /* tp_traverse */ |
| 3778 | 0, /* tp_clear */ |
| 3779 | object_richcompare, /* tp_richcompare */ |
| 3780 | 0, /* tp_weaklistoffset */ |
| 3781 | 0, /* tp_iter */ |
| 3782 | 0, /* tp_iternext */ |
| 3783 | object_methods, /* tp_methods */ |
| 3784 | 0, /* tp_members */ |
| 3785 | object_getsets, /* tp_getset */ |
| 3786 | 0, /* tp_base */ |
| 3787 | 0, /* tp_dict */ |
| 3788 | 0, /* tp_descr_get */ |
| 3789 | 0, /* tp_descr_set */ |
| 3790 | 0, /* tp_dictoffset */ |
| 3791 | object_init, /* tp_init */ |
| 3792 | PyType_GenericAlloc, /* tp_alloc */ |
| 3793 | object_new, /* tp_new */ |
| 3794 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3795 | }; |
| 3796 | |
| 3797 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3798 | /* 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] | 3799 | |
| 3800 | static int |
| 3801 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | for (; meth->ml_name != NULL; meth++) { |
| 3806 | PyObject *descr; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3807 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3809 | !(meth->ml_flags & METH_COEXIST)) |
| 3810 | continue; |
| 3811 | if (meth->ml_flags & METH_CLASS) { |
| 3812 | if (meth->ml_flags & METH_STATIC) { |
| 3813 | PyErr_SetString(PyExc_ValueError, |
| 3814 | "method cannot be both class and static"); |
| 3815 | return -1; |
| 3816 | } |
| 3817 | descr = PyDescr_NewClassMethod(type, meth); |
| 3818 | } |
| 3819 | else if (meth->ml_flags & METH_STATIC) { |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 3820 | PyObject *cfunc = PyCFunction_New(meth, (PyObject*)type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3821 | if (cfunc == NULL) |
| 3822 | return -1; |
| 3823 | descr = PyStaticMethod_New(cfunc); |
| 3824 | Py_DECREF(cfunc); |
| 3825 | } |
| 3826 | else { |
| 3827 | descr = PyDescr_NewMethod(type, meth); |
| 3828 | } |
| 3829 | if (descr == NULL) |
| 3830 | return -1; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3831 | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | Py_DECREF(descr); |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 3833 | if (err < 0) |
| 3834 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | } |
| 3836 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3840 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3844 | for (; memb->name != NULL; memb++) { |
| 3845 | PyObject *descr; |
| 3846 | if (PyDict_GetItemString(dict, memb->name)) |
| 3847 | continue; |
| 3848 | descr = PyDescr_NewMember(type, memb); |
| 3849 | if (descr == NULL) |
| 3850 | return -1; |
| 3851 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3852 | return -1; |
| 3853 | Py_DECREF(descr); |
| 3854 | } |
| 3855 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3859 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3863 | for (; gsp->name != NULL; gsp++) { |
| 3864 | PyObject *descr; |
| 3865 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3866 | continue; |
| 3867 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3869 | if (descr == NULL) |
| 3870 | return -1; |
| 3871 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3872 | return -1; |
| 3873 | Py_DECREF(descr); |
| 3874 | } |
| 3875 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3878 | static void |
| 3879 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3880 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3884 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3885 | (!type->tp_traverse && !type->tp_clear)) { |
| 3886 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3887 | if (type->tp_traverse == NULL) |
| 3888 | type->tp_traverse = base->tp_traverse; |
| 3889 | if (type->tp_clear == NULL) |
| 3890 | type->tp_clear = base->tp_clear; |
| 3891 | } |
| 3892 | { |
| 3893 | /* The condition below could use some explanation. |
| 3894 | It appears that tp_new is not inherited for static types |
| 3895 | whose base class is 'object'; this seems to be a precaution |
| 3896 | so that old extension types don't suddenly become |
| 3897 | callable (object.__new__ wouldn't insure the invariants |
| 3898 | that the extension type's own factory function ensures). |
| 3899 | Heap types, of course, are under our control, so they do |
| 3900 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3901 | other built-in type as the default also |
| 3902 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | if (base != &PyBaseObject_Type || |
| 3904 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3905 | if (type->tp_new == NULL) |
| 3906 | type->tp_new = base->tp_new; |
| 3907 | } |
| 3908 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3909 | if (type->tp_basicsize == 0) |
| 3910 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3913 | |
| 3914 | #undef COPYVAL |
| 3915 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | COPYVAL(tp_itemsize); |
| 3919 | COPYVAL(tp_weaklistoffset); |
| 3920 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | /* Setup fast subclass flags */ |
| 3923 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3924 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3925 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3926 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3927 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3928 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3929 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3930 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3931 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3932 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3933 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3934 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3935 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3936 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3937 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3938 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3941 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3942 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3945 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3948 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 3949 | return 1; |
| 3950 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 3951 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3955 | static void |
| 3956 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3959 | |
| 3960 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3961 | #undef COPYSLOT |
| 3962 | #undef COPYNUM |
| 3963 | #undef COPYSEQ |
| 3964 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3965 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3966 | |
| 3967 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | (base->SLOT != 0 && \ |
| 3969 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3970 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3971 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3973 | |
| 3974 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3975 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3976 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3977 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3980 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3982 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3983 | basebase = base->tp_base; |
| 3984 | if (basebase->tp_as_number == NULL) |
| 3985 | basebase = NULL; |
| 3986 | COPYNUM(nb_add); |
| 3987 | COPYNUM(nb_subtract); |
| 3988 | COPYNUM(nb_multiply); |
| 3989 | COPYNUM(nb_remainder); |
| 3990 | COPYNUM(nb_divmod); |
| 3991 | COPYNUM(nb_power); |
| 3992 | COPYNUM(nb_negative); |
| 3993 | COPYNUM(nb_positive); |
| 3994 | COPYNUM(nb_absolute); |
| 3995 | COPYNUM(nb_bool); |
| 3996 | COPYNUM(nb_invert); |
| 3997 | COPYNUM(nb_lshift); |
| 3998 | COPYNUM(nb_rshift); |
| 3999 | COPYNUM(nb_and); |
| 4000 | COPYNUM(nb_xor); |
| 4001 | COPYNUM(nb_or); |
| 4002 | COPYNUM(nb_int); |
| 4003 | COPYNUM(nb_float); |
| 4004 | COPYNUM(nb_inplace_add); |
| 4005 | COPYNUM(nb_inplace_subtract); |
| 4006 | COPYNUM(nb_inplace_multiply); |
| 4007 | COPYNUM(nb_inplace_remainder); |
| 4008 | COPYNUM(nb_inplace_power); |
| 4009 | COPYNUM(nb_inplace_lshift); |
| 4010 | COPYNUM(nb_inplace_rshift); |
| 4011 | COPYNUM(nb_inplace_and); |
| 4012 | COPYNUM(nb_inplace_xor); |
| 4013 | COPYNUM(nb_inplace_or); |
| 4014 | COPYNUM(nb_true_divide); |
| 4015 | COPYNUM(nb_floor_divide); |
| 4016 | COPYNUM(nb_inplace_true_divide); |
| 4017 | COPYNUM(nb_inplace_floor_divide); |
| 4018 | COPYNUM(nb_index); |
| 4019 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4021 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 4022 | basebase = base->tp_base; |
| 4023 | if (basebase->tp_as_sequence == NULL) |
| 4024 | basebase = NULL; |
| 4025 | COPYSEQ(sq_length); |
| 4026 | COPYSEQ(sq_concat); |
| 4027 | COPYSEQ(sq_repeat); |
| 4028 | COPYSEQ(sq_item); |
| 4029 | COPYSEQ(sq_ass_item); |
| 4030 | COPYSEQ(sq_contains); |
| 4031 | COPYSEQ(sq_inplace_concat); |
| 4032 | COPYSEQ(sq_inplace_repeat); |
| 4033 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4035 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 4036 | basebase = base->tp_base; |
| 4037 | if (basebase->tp_as_mapping == NULL) |
| 4038 | basebase = NULL; |
| 4039 | COPYMAP(mp_length); |
| 4040 | COPYMAP(mp_subscript); |
| 4041 | COPYMAP(mp_ass_subscript); |
| 4042 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4044 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 4045 | basebase = base->tp_base; |
| 4046 | if (basebase->tp_as_buffer == NULL) |
| 4047 | basebase = NULL; |
| 4048 | COPYBUF(bf_getbuffer); |
| 4049 | COPYBUF(bf_releasebuffer); |
| 4050 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | COPYSLOT(tp_dealloc); |
| 4055 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 4056 | type->tp_getattr = base->tp_getattr; |
| 4057 | type->tp_getattro = base->tp_getattro; |
| 4058 | } |
| 4059 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 4060 | type->tp_setattr = base->tp_setattr; |
| 4061 | type->tp_setattro = base->tp_setattro; |
| 4062 | } |
| 4063 | /* tp_reserved is ignored */ |
| 4064 | COPYSLOT(tp_repr); |
| 4065 | /* tp_hash see tp_richcompare */ |
| 4066 | COPYSLOT(tp_call); |
| 4067 | COPYSLOT(tp_str); |
| 4068 | { |
| 4069 | /* Copy comparison-related slots only when |
| 4070 | not overriding them anywhere */ |
| 4071 | if (type->tp_richcompare == NULL && |
| 4072 | type->tp_hash == NULL && |
| 4073 | !overrides_hash(type)) |
| 4074 | { |
| 4075 | type->tp_richcompare = base->tp_richcompare; |
| 4076 | type->tp_hash = base->tp_hash; |
| 4077 | } |
| 4078 | } |
| 4079 | { |
| 4080 | COPYSLOT(tp_iter); |
| 4081 | COPYSLOT(tp_iternext); |
| 4082 | } |
| 4083 | { |
| 4084 | COPYSLOT(tp_descr_get); |
| 4085 | COPYSLOT(tp_descr_set); |
| 4086 | COPYSLOT(tp_dictoffset); |
| 4087 | COPYSLOT(tp_init); |
| 4088 | COPYSLOT(tp_alloc); |
| 4089 | COPYSLOT(tp_is_gc); |
| 4090 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 4091 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 4092 | /* They agree about gc. */ |
| 4093 | COPYSLOT(tp_free); |
| 4094 | } |
| 4095 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4096 | type->tp_free == NULL && |
| 4097 | base->tp_free == PyObject_Free) { |
| 4098 | /* A bit of magic to plug in the correct default |
| 4099 | * tp_free function when a derived class adds gc, |
| 4100 | * didn't define tp_free, and the base uses the |
| 4101 | * default non-gc tp_free. |
| 4102 | */ |
| 4103 | type->tp_free = PyObject_GC_Del; |
| 4104 | } |
| 4105 | /* else they didn't agree about gc, and there isn't something |
| 4106 | * obvious to be done -- the type is on its own. |
| 4107 | */ |
| 4108 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4111 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4112 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4113 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 4114 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | PyObject *dict, *bases; |
| 4117 | PyTypeObject *base; |
| 4118 | Py_ssize_t i, n; |
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 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 4121 | assert(type->tp_dict != NULL); |
| 4122 | return 0; |
| 4123 | } |
| 4124 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4127 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4128 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | /* PyType_Ready is the closest thing we have to a choke point |
| 4130 | * for type objects, so is the best place I can think of to try |
| 4131 | * to get type objects into the doubly-linked list of all objects. |
| 4132 | * Still, not all type objects go thru PyType_Ready. |
| 4133 | */ |
| 4134 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4135 | #endif |
| 4136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4138 | base = type->tp_base; |
| 4139 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4140 | base = type->tp_base = &PyBaseObject_Type; |
| 4141 | Py_INCREF(base); |
| 4142 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | /* Now the only way base can still be NULL is if type is |
| 4145 | * &PyBaseObject_Type. |
| 4146 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4148 | /* Initialize the base class */ |
| 4149 | if (base != NULL && base->tp_dict == NULL) { |
| 4150 | if (PyType_Ready(base) < 0) |
| 4151 | goto error; |
| 4152 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4154 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4155 | compilable separately on Windows can call PyType_Ready() instead of |
| 4156 | initializing the ob_type field of their type objects. */ |
| 4157 | /* The test for base != NULL is really unnecessary, since base is only |
| 4158 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4159 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4160 | know that. */ |
| 4161 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4162 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4164 | /* Initialize tp_bases */ |
| 4165 | bases = type->tp_bases; |
| 4166 | if (bases == NULL) { |
| 4167 | if (base == NULL) |
| 4168 | bases = PyTuple_New(0); |
| 4169 | else |
| 4170 | bases = PyTuple_Pack(1, base); |
| 4171 | if (bases == NULL) |
| 4172 | goto error; |
| 4173 | type->tp_bases = bases; |
| 4174 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4176 | /* Initialize tp_dict */ |
| 4177 | dict = type->tp_dict; |
| 4178 | if (dict == NULL) { |
| 4179 | dict = PyDict_New(); |
| 4180 | if (dict == NULL) |
| 4181 | goto error; |
| 4182 | type->tp_dict = dict; |
| 4183 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | /* Add type-specific descriptors to tp_dict */ |
| 4186 | if (add_operators(type) < 0) |
| 4187 | goto error; |
| 4188 | if (type->tp_methods != NULL) { |
| 4189 | if (add_methods(type, type->tp_methods) < 0) |
| 4190 | goto error; |
| 4191 | } |
| 4192 | if (type->tp_members != NULL) { |
| 4193 | if (add_members(type, type->tp_members) < 0) |
| 4194 | goto error; |
| 4195 | } |
| 4196 | if (type->tp_getset != NULL) { |
| 4197 | if (add_getset(type, type->tp_getset) < 0) |
| 4198 | goto error; |
| 4199 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4201 | /* Calculate method resolution order */ |
| 4202 | if (mro_internal(type) < 0) { |
| 4203 | goto error; |
| 4204 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | /* Inherit special flags from dominant base */ |
| 4207 | if (type->tp_base != NULL) |
| 4208 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | /* Initialize tp_dict properly */ |
| 4211 | bases = type->tp_mro; |
| 4212 | assert(bases != NULL); |
| 4213 | assert(PyTuple_Check(bases)); |
| 4214 | n = PyTuple_GET_SIZE(bases); |
| 4215 | for (i = 1; i < n; i++) { |
| 4216 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4217 | if (PyType_Check(b)) |
| 4218 | inherit_slots(type, (PyTypeObject *)b); |
| 4219 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | /* Sanity check for tp_free. */ |
| 4222 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4223 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4224 | /* This base class needs to call tp_free, but doesn't have |
| 4225 | * one, or its tp_free is for non-gc'ed objects. |
| 4226 | */ |
| 4227 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4228 | "gc and is a base type but has inappropriate " |
| 4229 | "tp_free slot", |
| 4230 | type->tp_name); |
| 4231 | goto error; |
| 4232 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4235 | the tp_doc slot. |
| 4236 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4237 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | if (type->tp_doc != NULL) { |
| 4239 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 4240 | if (doc == NULL) |
| 4241 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4242 | _PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | Py_DECREF(doc); |
| 4244 | } else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4245 | _PyDict_SetItemId(type->tp_dict, |
| 4246 | &PyId___doc__, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4250 | /* Hack for tp_hash and __hash__. |
| 4251 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4252 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4253 | tp_dict['__hash__'] equal to None. |
| 4254 | This signals that __hash__ is not inherited. |
| 4255 | */ |
| 4256 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4257 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 4258 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | goto error; |
| 4260 | type->tp_hash = PyObject_HashNotImplemented; |
| 4261 | } |
| 4262 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | /* Some more special stuff */ |
| 4265 | base = type->tp_base; |
| 4266 | if (base != NULL) { |
| 4267 | if (type->tp_as_number == NULL) |
| 4268 | type->tp_as_number = base->tp_as_number; |
| 4269 | if (type->tp_as_sequence == NULL) |
| 4270 | type->tp_as_sequence = base->tp_as_sequence; |
| 4271 | if (type->tp_as_mapping == NULL) |
| 4272 | type->tp_as_mapping = base->tp_as_mapping; |
| 4273 | if (type->tp_as_buffer == NULL) |
| 4274 | type->tp_as_buffer = base->tp_as_buffer; |
| 4275 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | /* Link into each base class's list of subclasses */ |
| 4278 | bases = type->tp_bases; |
| 4279 | n = PyTuple_GET_SIZE(bases); |
| 4280 | for (i = 0; i < n; i++) { |
| 4281 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4282 | if (PyType_Check(b) && |
| 4283 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4284 | goto error; |
| 4285 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4287 | /* Warn for a type that implements tp_compare (now known as |
| 4288 | tp_reserved) but not tp_richcompare. */ |
| 4289 | if (type->tp_reserved && !type->tp_richcompare) { |
| 4290 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4291 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 4292 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4293 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4294 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | if (error == -1) |
| 4296 | goto error; |
| 4297 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | /* All done -- set the ready flag */ |
| 4300 | assert(type->tp_dict != NULL); |
| 4301 | type->tp_flags = |
| 4302 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4303 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4304 | |
| 4305 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4306 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4307 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4310 | static int |
| 4311 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | Py_ssize_t i; |
| 4314 | int result; |
| 4315 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | list = base->tp_subclasses; |
| 4318 | if (list == NULL) { |
| 4319 | base->tp_subclasses = list = PyList_New(0); |
| 4320 | if (list == NULL) |
| 4321 | return -1; |
| 4322 | } |
| 4323 | assert(PyList_Check(list)); |
| 4324 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 4325 | i = PyList_GET_SIZE(list); |
| 4326 | while (--i >= 0) { |
| 4327 | ref = PyList_GET_ITEM(list, i); |
| 4328 | assert(PyWeakref_CheckRef(ref)); |
| 4329 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 4330 | return PyList_SetItem(list, i, newobj); |
| 4331 | } |
| 4332 | result = PyList_Append(list, newobj); |
| 4333 | Py_DECREF(newobj); |
| 4334 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4337 | static void |
| 4338 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4339 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | Py_ssize_t i; |
| 4341 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4343 | list = base->tp_subclasses; |
| 4344 | if (list == NULL) { |
| 4345 | return; |
| 4346 | } |
| 4347 | assert(PyList_Check(list)); |
| 4348 | i = PyList_GET_SIZE(list); |
| 4349 | while (--i >= 0) { |
| 4350 | ref = PyList_GET_ITEM(list, i); |
| 4351 | assert(PyWeakref_CheckRef(ref)); |
| 4352 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4353 | /* this can't fail, right? */ |
| 4354 | PySequence_DelItem(list, i); |
| 4355 | return; |
| 4356 | } |
| 4357 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4358 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4359 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4360 | static int |
| 4361 | check_num_args(PyObject *ob, int n) |
| 4362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | if (!PyTuple_CheckExact(ob)) { |
| 4364 | PyErr_SetString(PyExc_SystemError, |
| 4365 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4366 | return 0; |
| 4367 | } |
| 4368 | if (n == PyTuple_GET_SIZE(ob)) |
| 4369 | return 1; |
| 4370 | PyErr_Format( |
| 4371 | PyExc_TypeError, |
| 4372 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4373 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4376 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4377 | |
| 4378 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4380 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4381 | Most tables have only one entry; the tables for binary operators have two |
| 4382 | entries, one regular and one with reversed arguments. */ |
| 4383 | |
| 4384 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4385 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | lenfunc func = (lenfunc)wrapped; |
| 4388 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | if (!check_num_args(args, 0)) |
| 4391 | return NULL; |
| 4392 | res = (*func)(self); |
| 4393 | if (res == -1 && PyErr_Occurred()) |
| 4394 | return NULL; |
| 4395 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4398 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4399 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | inquiry func = (inquiry)wrapped; |
| 4402 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | if (!check_num_args(args, 0)) |
| 4405 | return NULL; |
| 4406 | res = (*func)(self); |
| 4407 | if (res == -1 && PyErr_Occurred()) |
| 4408 | return NULL; |
| 4409 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4413 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | binaryfunc func = (binaryfunc)wrapped; |
| 4416 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | if (!check_num_args(args, 1)) |
| 4419 | return NULL; |
| 4420 | other = PyTuple_GET_ITEM(args, 0); |
| 4421 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4425 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | binaryfunc func = (binaryfunc)wrapped; |
| 4428 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | if (!check_num_args(args, 1)) |
| 4431 | return NULL; |
| 4432 | other = PyTuple_GET_ITEM(args, 0); |
| 4433 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4437 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | binaryfunc func = (binaryfunc)wrapped; |
| 4440 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | if (!check_num_args(args, 1)) |
| 4443 | return NULL; |
| 4444 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4448 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4449 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4452 | PyObject *other; |
| 4453 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4455 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4458 | return NULL; |
| 4459 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4462 | static PyObject * |
| 4463 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4466 | PyObject *other; |
| 4467 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4472 | return NULL; |
| 4473 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4476 | static PyObject * |
| 4477 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | if (!check_num_args(args, 0)) |
| 4482 | return NULL; |
| 4483 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4486 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4487 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4490 | PyObject* o; |
| 4491 | Py_ssize_t i; |
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 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4494 | return NULL; |
| 4495 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4496 | if (i == -1 && PyErr_Occurred()) |
| 4497 | return NULL; |
| 4498 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4501 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4502 | getindex(PyObject *self, PyObject *arg) |
| 4503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4507 | if (i == -1 && PyErr_Occurred()) |
| 4508 | return -1; |
| 4509 | if (i < 0) { |
| 4510 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4511 | if (sq && sq->sq_length) { |
| 4512 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4513 | if (n < 0) |
| 4514 | return -1; |
| 4515 | i += n; |
| 4516 | } |
| 4517 | } |
| 4518 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | static PyObject * |
| 4522 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4525 | PyObject *arg; |
| 4526 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4529 | arg = PyTuple_GET_ITEM(args, 0); |
| 4530 | i = getindex(self, arg); |
| 4531 | if (i == -1 && PyErr_Occurred()) |
| 4532 | return NULL; |
| 4533 | return (*func)(self, i); |
| 4534 | } |
| 4535 | check_num_args(args, 1); |
| 4536 | assert(PyErr_Occurred()); |
| 4537 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4540 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4541 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4544 | Py_ssize_t i; |
| 4545 | int res; |
| 4546 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4548 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4549 | return NULL; |
| 4550 | i = getindex(self, arg); |
| 4551 | if (i == -1 && PyErr_Occurred()) |
| 4552 | return NULL; |
| 4553 | res = (*func)(self, i, value); |
| 4554 | if (res == -1 && PyErr_Occurred()) |
| 4555 | return NULL; |
| 4556 | Py_INCREF(Py_None); |
| 4557 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4560 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4561 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4564 | Py_ssize_t i; |
| 4565 | int res; |
| 4566 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | if (!check_num_args(args, 1)) |
| 4569 | return NULL; |
| 4570 | arg = PyTuple_GET_ITEM(args, 0); |
| 4571 | i = getindex(self, arg); |
| 4572 | if (i == -1 && PyErr_Occurred()) |
| 4573 | return NULL; |
| 4574 | res = (*func)(self, i, NULL); |
| 4575 | if (res == -1 && PyErr_Occurred()) |
| 4576 | return NULL; |
| 4577 | Py_INCREF(Py_None); |
| 4578 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4581 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4582 | static PyObject * |
| 4583 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4584 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | objobjproc func = (objobjproc)wrapped; |
| 4586 | int res; |
| 4587 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | if (!check_num_args(args, 1)) |
| 4590 | return NULL; |
| 4591 | value = PyTuple_GET_ITEM(args, 0); |
| 4592 | res = (*func)(self, value); |
| 4593 | if (res == -1 && PyErr_Occurred()) |
| 4594 | return NULL; |
| 4595 | else |
| 4596 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4599 | static PyObject * |
| 4600 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | objobjargproc func = (objobjargproc)wrapped; |
| 4603 | int res; |
| 4604 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4607 | return NULL; |
| 4608 | res = (*func)(self, key, value); |
| 4609 | if (res == -1 && PyErr_Occurred()) |
| 4610 | return NULL; |
| 4611 | Py_INCREF(Py_None); |
| 4612 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4615 | static PyObject * |
| 4616 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | objobjargproc func = (objobjargproc)wrapped; |
| 4619 | int res; |
| 4620 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | if (!check_num_args(args, 1)) |
| 4623 | return NULL; |
| 4624 | key = PyTuple_GET_ITEM(args, 0); |
| 4625 | res = (*func)(self, key, NULL); |
| 4626 | if (res == -1 && PyErr_Occurred()) |
| 4627 | return NULL; |
| 4628 | Py_INCREF(Py_None); |
| 4629 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4632 | /* 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] | 4633 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4634 | static int |
| 4635 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | PyTypeObject *type = Py_TYPE(self); |
| 4638 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4639 | type = type->tp_base; |
| 4640 | /* If type is NULL now, this is a really weird type. |
| 4641 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4642 | if (type && type->tp_setattro != func) { |
| 4643 | PyErr_Format(PyExc_TypeError, |
| 4644 | "can't apply this %s to %s object", |
| 4645 | what, |
| 4646 | type->tp_name); |
| 4647 | return 0; |
| 4648 | } |
| 4649 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4652 | static PyObject * |
| 4653 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4655 | setattrofunc func = (setattrofunc)wrapped; |
| 4656 | int res; |
| 4657 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4660 | return NULL; |
| 4661 | if (!hackcheck(self, func, "__setattr__")) |
| 4662 | return NULL; |
| 4663 | res = (*func)(self, name, value); |
| 4664 | if (res < 0) |
| 4665 | return NULL; |
| 4666 | Py_INCREF(Py_None); |
| 4667 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | static PyObject * |
| 4671 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | setattrofunc func = (setattrofunc)wrapped; |
| 4674 | int res; |
| 4675 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | if (!check_num_args(args, 1)) |
| 4678 | return NULL; |
| 4679 | name = PyTuple_GET_ITEM(args, 0); |
| 4680 | if (!hackcheck(self, func, "__delattr__")) |
| 4681 | return NULL; |
| 4682 | res = (*func)(self, name, NULL); |
| 4683 | if (res < 0) |
| 4684 | return NULL; |
| 4685 | Py_INCREF(Py_None); |
| 4686 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4689 | static PyObject * |
| 4690 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4693 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4695 | if (!check_num_args(args, 0)) |
| 4696 | return NULL; |
| 4697 | res = (*func)(self); |
| 4698 | if (res == -1 && PyErr_Occurred()) |
| 4699 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4700 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4703 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4704 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4711 | static PyObject * |
| 4712 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4715 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | if (!check_num_args(args, 1)) |
| 4718 | return NULL; |
| 4719 | other = PyTuple_GET_ITEM(args, 0); |
| 4720 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | #undef RICHCMP_WRAPPER |
| 4724 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4725 | static PyObject * \ |
| 4726 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4727 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4731 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4732 | RICHCMP_WRAPPER(le, Py_LE) |
| 4733 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4734 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4735 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4736 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4737 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4738 | static PyObject * |
| 4739 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | unaryfunc func = (unaryfunc)wrapped; |
| 4742 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4744 | if (!check_num_args(args, 0)) |
| 4745 | return NULL; |
| 4746 | res = (*func)(self); |
| 4747 | if (res == NULL && !PyErr_Occurred()) |
| 4748 | PyErr_SetNone(PyExc_StopIteration); |
| 4749 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4752 | static PyObject * |
| 4753 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4756 | PyObject *obj; |
| 4757 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4760 | return NULL; |
| 4761 | if (obj == Py_None) |
| 4762 | obj = NULL; |
| 4763 | if (type == Py_None) |
| 4764 | type = NULL; |
| 4765 | if (type == NULL &&obj == NULL) { |
| 4766 | PyErr_SetString(PyExc_TypeError, |
| 4767 | "__get__(None, None) is invalid"); |
| 4768 | return NULL; |
| 4769 | } |
| 4770 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4773 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4774 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4777 | PyObject *obj, *value; |
| 4778 | int ret; |
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 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4781 | return NULL; |
| 4782 | ret = (*func)(self, obj, value); |
| 4783 | if (ret < 0) |
| 4784 | return NULL; |
| 4785 | Py_INCREF(Py_None); |
| 4786 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4787 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4788 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4789 | static PyObject * |
| 4790 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4793 | PyObject *obj; |
| 4794 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | if (!check_num_args(args, 1)) |
| 4797 | return NULL; |
| 4798 | obj = PyTuple_GET_ITEM(args, 0); |
| 4799 | ret = (*func)(self, obj, NULL); |
| 4800 | if (ret < 0) |
| 4801 | return NULL; |
| 4802 | Py_INCREF(Py_None); |
| 4803 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4804 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4805 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4806 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4807 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4811 | if (func(self, args, kwds) < 0) |
| 4812 | return NULL; |
| 4813 | Py_INCREF(Py_None); |
| 4814 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4817 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4818 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | PyTypeObject *type, *subtype, *staticbase; |
| 4821 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4823 | if (self == NULL || !PyType_Check(self)) |
| 4824 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4825 | type = (PyTypeObject *)self; |
| 4826 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4827 | PyErr_Format(PyExc_TypeError, |
| 4828 | "%s.__new__(): not enough arguments", |
| 4829 | type->tp_name); |
| 4830 | return NULL; |
| 4831 | } |
| 4832 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4833 | if (!PyType_Check(arg0)) { |
| 4834 | PyErr_Format(PyExc_TypeError, |
| 4835 | "%s.__new__(X): X is not a type object (%s)", |
| 4836 | type->tp_name, |
| 4837 | Py_TYPE(arg0)->tp_name); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | subtype = (PyTypeObject *)arg0; |
| 4841 | if (!PyType_IsSubtype(subtype, type)) { |
| 4842 | PyErr_Format(PyExc_TypeError, |
| 4843 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4844 | type->tp_name, |
| 4845 | subtype->tp_name, |
| 4846 | subtype->tp_name, |
| 4847 | type->tp_name); |
| 4848 | return NULL; |
| 4849 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | /* Check that the use doesn't do something silly and unsafe like |
| 4852 | object.__new__(dict). To do this, we check that the |
| 4853 | most derived base that's not a heap type is this type. */ |
| 4854 | staticbase = subtype; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 4855 | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4856 | staticbase = staticbase->tp_base; |
| 4857 | /* If staticbase is NULL now, it is a really weird type. |
| 4858 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4859 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4860 | PyErr_Format(PyExc_TypeError, |
| 4861 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4862 | type->tp_name, |
| 4863 | subtype->tp_name, |
| 4864 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4865 | return NULL; |
| 4866 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4869 | if (args == NULL) |
| 4870 | return NULL; |
| 4871 | res = type->tp_new(subtype, args, kwds); |
| 4872 | Py_DECREF(args); |
| 4873 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4876 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4878 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4879 | "a new object with type S, a subtype of T")}, |
| 4880 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4881 | }; |
| 4882 | |
| 4883 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4884 | add_tp_new_wrapper(PyTypeObject *type) |
| 4885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4886 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4887 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4888 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | return 0; |
| 4890 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4891 | if (func == NULL) |
| 4892 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4893 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | Py_DECREF(func); |
| 4895 | return -1; |
| 4896 | } |
| 4897 | Py_DECREF(func); |
| 4898 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4901 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4902 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4903 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4904 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4905 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4906 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4907 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4908 | _Py_static_string(id, OPSTR); \ |
| 4909 | return call_method(self, &id, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4912 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4913 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4914 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4915 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4916 | _Py_static_string(id, OPSTR); \ |
| 4917 | return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4920 | /* Boolean helper for SLOT1BINFULL(). |
| 4921 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4922 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4923 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | PyObject *a, *b; |
| 4926 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4927 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4928 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4929 | if (b == NULL) { |
| 4930 | PyErr_Clear(); |
| 4931 | /* If right doesn't have it, it's not overloaded */ |
| 4932 | return 0; |
| 4933 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4934 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4935 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4936 | if (a == NULL) { |
| 4937 | PyErr_Clear(); |
| 4938 | Py_DECREF(b); |
| 4939 | /* If right has it but left doesn't, it's overloaded */ |
| 4940 | return 1; |
| 4941 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4944 | Py_DECREF(a); |
| 4945 | Py_DECREF(b); |
| 4946 | if (ok < 0) { |
| 4947 | PyErr_Clear(); |
| 4948 | return 0; |
| 4949 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4954 | |
| 4955 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4956 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4957 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4958 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4959 | _Py_static_string(op_id, OPSTR); \ |
| 4960 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4962 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4963 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4964 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4965 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4966 | PyObject *r; \ |
| 4967 | if (do_other && \ |
| 4968 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4969 | method_is_overloaded(self, other, &rop_id)) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4970 | r = call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | if (r != Py_NotImplemented) \ |
| 4972 | return r; \ |
| 4973 | Py_DECREF(r); \ |
| 4974 | do_other = 0; \ |
| 4975 | } \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4976 | r = call_maybe(self, &op_id, "(O)", other); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | if (r != Py_NotImplemented || \ |
| 4978 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4979 | return r; \ |
| 4980 | Py_DECREF(r); \ |
| 4981 | } \ |
| 4982 | if (do_other) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4983 | return call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4984 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4985 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4990 | |
| 4991 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4992 | static PyObject * \ |
| 4993 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4994 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4995 | _Py_static_string(id, #OPSTR); \ |
| 4996 | return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4999 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5000 | slot_sq_length(PyObject *self) |
| 5001 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5002 | _Py_IDENTIFIER(__len__); |
| 5003 | PyObject *res = call_method(self, &PyId___len__, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | if (res == NULL) |
| 5007 | return -1; |
| 5008 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 5009 | Py_DECREF(res); |
| 5010 | if (len < 0) { |
| 5011 | if (!PyErr_Occurred()) |
| 5012 | PyErr_SetString(PyExc_ValueError, |
| 5013 | "__len__() should return >= 0"); |
| 5014 | return -1; |
| 5015 | } |
| 5016 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5019 | /* Super-optimized version of slot_sq_item. |
| 5020 | Other slots could do the same... */ |
| 5021 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5022 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5023 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 5025 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5026 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5027 | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | if (func != NULL) { |
| 5029 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 5030 | Py_INCREF(func); |
| 5031 | else { |
| 5032 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 5033 | if (func == NULL) { |
| 5034 | return NULL; |
| 5035 | } |
| 5036 | } |
| 5037 | ival = PyLong_FromSsize_t(i); |
| 5038 | if (ival != NULL) { |
| 5039 | args = PyTuple_New(1); |
| 5040 | if (args != NULL) { |
| 5041 | PyTuple_SET_ITEM(args, 0, ival); |
| 5042 | retval = PyObject_Call(func, args, NULL); |
| 5043 | Py_XDECREF(args); |
| 5044 | Py_XDECREF(func); |
| 5045 | return retval; |
| 5046 | } |
| 5047 | } |
| 5048 | } |
| 5049 | else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5050 | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5051 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 5052 | } |
| 5053 | Py_XDECREF(args); |
| 5054 | Py_XDECREF(ival); |
| 5055 | Py_XDECREF(func); |
| 5056 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5059 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5060 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5063 | _Py_IDENTIFIER(__delitem__); |
| 5064 | _Py_IDENTIFIER(__setitem__); |
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 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5067 | res = call_method(self, &PyId___delitem__, "(n)", index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5069 | res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | if (res == NULL) |
| 5071 | return -1; |
| 5072 | Py_DECREF(res); |
| 5073 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5077 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | PyObject *func, *res, *args; |
| 5080 | int result = -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5081 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5082 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5083 | func = lookup_maybe(self, &PyId___contains__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | if (func != NULL) { |
| 5085 | args = PyTuple_Pack(1, value); |
| 5086 | if (args == NULL) |
| 5087 | res = NULL; |
| 5088 | else { |
| 5089 | res = PyObject_Call(func, args, NULL); |
| 5090 | Py_DECREF(args); |
| 5091 | } |
| 5092 | Py_DECREF(func); |
| 5093 | if (res != NULL) { |
| 5094 | result = PyObject_IsTrue(res); |
| 5095 | Py_DECREF(res); |
| 5096 | } |
| 5097 | } |
| 5098 | else if (! PyErr_Occurred()) { |
| 5099 | /* Possible results: -1 and 1 */ |
| 5100 | result = (int)_PySequence_IterSearch(self, value, |
| 5101 | PY_ITERSEARCH_CONTAINS); |
| 5102 | } |
| 5103 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5106 | #define slot_mp_length slot_sq_length |
| 5107 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5108 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5109 | |
| 5110 | static int |
| 5111 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5114 | _Py_IDENTIFIER(__delitem__); |
| 5115 | _Py_IDENTIFIER(__setitem__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5118 | res = call_method(self, &PyId___delitem__, "(O)", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5120 | res = call_method(self, &PyId___setitem__, "(OO)", key, value); |
| 5121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | if (res == NULL) |
| 5123 | return -1; |
| 5124 | Py_DECREF(res); |
| 5125 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5128 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5129 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5130 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5131 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5132 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5133 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5134 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5135 | |
| 5136 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5138 | |
| 5139 | static PyObject * |
| 5140 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5141 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5142 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5144 | if (modulus == Py_None) |
| 5145 | return slot_nb_power_binary(self, other); |
| 5146 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5147 | can call this when the second argument's type uses |
| 5148 | slot_nb_power, so check before calling self.__pow__. */ |
| 5149 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5150 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5151 | return call_method(self, &PyId___pow__, "(OO)", other, modulus); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5153 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | SLOT0(slot_nb_negative, "__neg__") |
| 5157 | SLOT0(slot_nb_positive, "__pos__") |
| 5158 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5159 | |
| 5160 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5161 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5163 | PyObject *func, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | int result = -1; |
| 5165 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5166 | _Py_IDENTIFIER(__len__); |
| 5167 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5168 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5169 | func = lookup_maybe(self, &PyId___bool__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5170 | if (func == NULL) { |
| 5171 | if (PyErr_Occurred()) |
| 5172 | return -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5173 | func = lookup_maybe(self, &PyId___len__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | if (func == NULL) |
| 5175 | return PyErr_Occurred() ? -1 : 1; |
| 5176 | using_len = 1; |
| 5177 | } |
| 5178 | args = PyTuple_New(0); |
| 5179 | if (args != NULL) { |
| 5180 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5181 | Py_DECREF(args); |
| 5182 | if (temp != NULL) { |
| 5183 | if (using_len) { |
| 5184 | /* enforced by slot_nb_len */ |
| 5185 | result = PyObject_IsTrue(temp); |
| 5186 | } |
| 5187 | else if (PyBool_Check(temp)) { |
| 5188 | result = PyObject_IsTrue(temp); |
| 5189 | } |
| 5190 | else { |
| 5191 | PyErr_Format(PyExc_TypeError, |
| 5192 | "__bool__ should return " |
| 5193 | "bool, returned %s", |
| 5194 | Py_TYPE(temp)->tp_name); |
| 5195 | result = -1; |
| 5196 | } |
| 5197 | Py_DECREF(temp); |
| 5198 | } |
| 5199 | } |
| 5200 | Py_DECREF(func); |
| 5201 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5204 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5205 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5206 | slot_nb_index(PyObject *self) |
| 5207 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5208 | _Py_IDENTIFIER(__index__); |
| 5209 | return call_method(self, &PyId___index__, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5213 | SLOT0(slot_nb_invert, "__invert__") |
| 5214 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5215 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5216 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5217 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5218 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5219 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5220 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5221 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5222 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5223 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5224 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5225 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5226 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5227 | static PyObject * |
| 5228 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5229 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5230 | _Py_IDENTIFIER(__ipow__); |
| 5231 | return call_method(self, &PyId___ipow__, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5232 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5233 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5234 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5235 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5236 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5237 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5238 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5240 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5241 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5242 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5243 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5244 | static PyObject * |
| 5245 | slot_tp_repr(PyObject *self) |
| 5246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5248 | _Py_IDENTIFIER(__repr__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5249 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5250 | func = lookup_method(self, &PyId___repr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | if (func != NULL) { |
| 5252 | res = PyEval_CallObject(func, NULL); |
| 5253 | Py_DECREF(func); |
| 5254 | return res; |
| 5255 | } |
| 5256 | PyErr_Clear(); |
| 5257 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5258 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | static PyObject * |
| 5262 | slot_tp_str(PyObject *self) |
| 5263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5265 | _Py_IDENTIFIER(__str__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5266 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5267 | func = lookup_method(self, &PyId___str__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | if (func != NULL) { |
| 5269 | res = PyEval_CallObject(func, NULL); |
| 5270 | Py_DECREF(func); |
| 5271 | return res; |
| 5272 | } |
| 5273 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5274 | /* PyObject *ress; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | PyErr_Clear(); |
| 5276 | res = slot_tp_repr(self); |
| 5277 | if (!res) |
| 5278 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5279 | /* XXX this is non-sensical. Why should we return |
| 5280 | a bytes object from __str__. Is this code even |
| 5281 | used? - mvl */ |
| 5282 | assert(0); |
| 5283 | return res; |
| 5284 | /* |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 5285 | ress = _PyUnicode_AsDefaultEncodedString(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5286 | Py_DECREF(res); |
| 5287 | return ress; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5288 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5290 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5291 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5292 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5293 | slot_tp_hash(PyObject *self) |
| 5294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5295 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5296 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5297 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5298 | func = lookup_method(self, &PyId___hash__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | if (func == Py_None) { |
| 5301 | Py_DECREF(func); |
| 5302 | func = NULL; |
| 5303 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | if (func == NULL) { |
| 5306 | return PyObject_HashNotImplemented(self); |
| 5307 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5309 | res = PyEval_CallObject(func, NULL); |
| 5310 | Py_DECREF(func); |
| 5311 | if (res == NULL) |
| 5312 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5313 | |
| 5314 | if (!PyLong_Check(res)) { |
| 5315 | PyErr_SetString(PyExc_TypeError, |
| 5316 | "__hash__ method should return an integer"); |
| 5317 | return -1; |
| 5318 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5319 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5320 | hashable Python object x, hash(x) will always lie within the range of |
| 5321 | Py_hash_t. Therefore our transformation must preserve values that |
| 5322 | already lie within this range, to ensure that if x.__hash__() returns |
| 5323 | hash(y) then hash(x) == hash(y). */ |
| 5324 | h = PyLong_AsSsize_t(res); |
| 5325 | if (h == -1 && PyErr_Occurred()) { |
| 5326 | /* res was not within the range of a Py_hash_t, so we're free to |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5327 | use any sufficiently bit-mixing transformation; |
| 5328 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5329 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5331 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5332 | /* -1 is reserved for errors. */ |
| 5333 | if (h == -1) |
| 5334 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5336 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | static PyObject * |
| 5340 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5341 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5342 | _Py_IDENTIFIER(__call__); |
| 5343 | PyObject *meth = lookup_method(self, &PyId___call__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | if (meth == NULL) |
| 5347 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5349 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5351 | Py_DECREF(meth); |
| 5352 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5355 | /* There are two slot dispatch functions for tp_getattro. |
| 5356 | |
| 5357 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5358 | but no __getattr__ hook is present; |
| 5359 | |
| 5360 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5361 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5362 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5363 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5364 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5365 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5366 | static PyObject * |
| 5367 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5368 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5369 | return call_method(self, &PyId___getattribute__, "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5372 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5373 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | PyObject *res, *descr = NULL; |
| 5376 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | if (f != NULL) { |
| 5379 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5380 | if (descr == NULL) |
| 5381 | return NULL; |
| 5382 | else |
| 5383 | attr = descr; |
| 5384 | } |
| 5385 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5386 | Py_XDECREF(descr); |
| 5387 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5391 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | PyTypeObject *tp = Py_TYPE(self); |
| 5394 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5395 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5398 | method fully for each attribute lookup for classes with |
| 5399 | __getattr__, even when the attribute is present. So we use |
| 5400 | _PyType_Lookup and create the method only when needed, with |
| 5401 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5402 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | if (getattr == NULL) { |
| 5404 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5405 | tp->tp_getattro = slot_tp_getattro; |
| 5406 | return slot_tp_getattro(self, name); |
| 5407 | } |
| 5408 | Py_INCREF(getattr); |
| 5409 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5410 | method fully for each attribute lookup for classes with |
| 5411 | __getattr__, even when self has the default __getattribute__ |
| 5412 | method. So we use _PyType_Lookup and create the method only when |
| 5413 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5414 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | if (getattribute == NULL || |
| 5416 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5417 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5418 | (void *)PyObject_GenericGetAttr)) |
| 5419 | res = PyObject_GenericGetAttr(self, name); |
| 5420 | else { |
| 5421 | Py_INCREF(getattribute); |
| 5422 | res = call_attribute(self, getattribute, name); |
| 5423 | Py_DECREF(getattribute); |
| 5424 | } |
| 5425 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5426 | PyErr_Clear(); |
| 5427 | res = call_attribute(self, getattr, name); |
| 5428 | } |
| 5429 | Py_DECREF(getattr); |
| 5430 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5433 | static int |
| 5434 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5437 | _Py_IDENTIFIER(__delattr__); |
| 5438 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5441 | res = call_method(self, &PyId___delattr__, "(O)", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5443 | res = call_method(self, &PyId___setattr__, "(OO)", name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | if (res == NULL) |
| 5445 | return -1; |
| 5446 | Py_DECREF(res); |
| 5447 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5450 | static _Py_Identifier name_op[] = { |
| 5451 | {0, "__lt__", 0}, |
| 5452 | {0, "__le__", 0}, |
| 5453 | {0, "__eq__", 0}, |
| 5454 | {0, "__ne__", 0}, |
| 5455 | {0, "__gt__", 0}, |
| 5456 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5457 | }; |
| 5458 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5459 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5460 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | PyObject *func, *args, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5463 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5464 | func = lookup_method(self, &name_op[op]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | if (func == NULL) { |
| 5466 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5467 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5468 | } |
| 5469 | args = PyTuple_Pack(1, other); |
| 5470 | if (args == NULL) |
| 5471 | res = NULL; |
| 5472 | else { |
| 5473 | res = PyObject_Call(func, args, NULL); |
| 5474 | Py_DECREF(args); |
| 5475 | } |
| 5476 | Py_DECREF(func); |
| 5477 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5480 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5481 | slot_tp_iter(PyObject *self) |
| 5482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5483 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5484 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5485 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5486 | func = lookup_method(self, &PyId___iter__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | if (func != NULL) { |
| 5488 | PyObject *args; |
| 5489 | args = res = PyTuple_New(0); |
| 5490 | if (args != NULL) { |
| 5491 | res = PyObject_Call(func, args, NULL); |
| 5492 | Py_DECREF(args); |
| 5493 | } |
| 5494 | Py_DECREF(func); |
| 5495 | return res; |
| 5496 | } |
| 5497 | PyErr_Clear(); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5498 | func = lookup_method(self, &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5499 | if (func == NULL) { |
| 5500 | PyErr_Format(PyExc_TypeError, |
| 5501 | "'%.200s' object is not iterable", |
| 5502 | Py_TYPE(self)->tp_name); |
| 5503 | return NULL; |
| 5504 | } |
| 5505 | Py_DECREF(func); |
| 5506 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5507 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5508 | |
| 5509 | static PyObject * |
| 5510 | slot_tp_iternext(PyObject *self) |
| 5511 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5512 | _Py_IDENTIFIER(__next__); |
| 5513 | return call_method(self, &PyId___next__, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5516 | static PyObject * |
| 5517 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | PyTypeObject *tp = Py_TYPE(self); |
| 5520 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5521 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5522 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5523 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | if (get == NULL) { |
| 5525 | /* Avoid further slowdowns */ |
| 5526 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5527 | tp->tp_descr_get = NULL; |
| 5528 | Py_INCREF(self); |
| 5529 | return self; |
| 5530 | } |
| 5531 | if (obj == NULL) |
| 5532 | obj = Py_None; |
| 5533 | if (type == NULL) |
| 5534 | type = Py_None; |
| 5535 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5536 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5537 | |
| 5538 | static int |
| 5539 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5542 | _Py_IDENTIFIER(__delete__); |
| 5543 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5546 | res = call_method(self, &PyId___delete__, "(O)", target); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5548 | res = call_method(self, &PyId___set__, "(OO)", target, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | if (res == NULL) |
| 5550 | return -1; |
| 5551 | Py_DECREF(res); |
| 5552 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
| 5555 | static int |
| 5556 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5557 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5558 | _Py_IDENTIFIER(__init__); |
| 5559 | PyObject *meth = lookup_method(self, &PyId___init__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | if (meth == NULL) |
| 5563 | return -1; |
| 5564 | res = PyObject_Call(meth, args, kwds); |
| 5565 | Py_DECREF(meth); |
| 5566 | if (res == NULL) |
| 5567 | return -1; |
| 5568 | if (res != Py_None) { |
| 5569 | PyErr_Format(PyExc_TypeError, |
| 5570 | "__init__() should return None, not '%.200s'", |
| 5571 | Py_TYPE(res)->tp_name); |
| 5572 | Py_DECREF(res); |
| 5573 | return -1; |
| 5574 | } |
| 5575 | Py_DECREF(res); |
| 5576 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | static PyObject * |
| 5580 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | PyObject *func; |
| 5583 | PyObject *newargs, *x; |
| 5584 | Py_ssize_t i, n; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5585 | _Py_IDENTIFIER(__new__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5586 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5587 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | if (func == NULL) |
| 5589 | return NULL; |
| 5590 | assert(PyTuple_Check(args)); |
| 5591 | n = PyTuple_GET_SIZE(args); |
| 5592 | newargs = PyTuple_New(n+1); |
| 5593 | if (newargs == NULL) |
| 5594 | return NULL; |
| 5595 | Py_INCREF(type); |
| 5596 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5597 | for (i = 0; i < n; i++) { |
| 5598 | x = PyTuple_GET_ITEM(args, i); |
| 5599 | Py_INCREF(x); |
| 5600 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5601 | } |
| 5602 | x = PyObject_Call(func, newargs, kwds); |
| 5603 | Py_DECREF(newargs); |
| 5604 | Py_DECREF(func); |
| 5605 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5608 | static void |
| 5609 | slot_tp_del(PyObject *self) |
| 5610 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5611 | _Py_IDENTIFIER(__del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | PyObject *del, *res; |
| 5613 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | /* Temporarily resurrect the object. */ |
| 5616 | assert(self->ob_refcnt == 0); |
| 5617 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5619 | /* Save the current exception, if any. */ |
| 5620 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5622 | /* Execute __del__ method, if any. */ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5623 | del = lookup_maybe(self, &PyId___del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5624 | if (del != NULL) { |
| 5625 | res = PyEval_CallObject(del, NULL); |
| 5626 | if (res == NULL) |
| 5627 | PyErr_WriteUnraisable(del); |
| 5628 | else |
| 5629 | Py_DECREF(res); |
| 5630 | Py_DECREF(del); |
| 5631 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | /* Restore the saved exception. */ |
| 5634 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5636 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5637 | * cause a recursive call. |
| 5638 | */ |
| 5639 | assert(self->ob_refcnt > 0); |
| 5640 | if (--self->ob_refcnt == 0) |
| 5641 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5643 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5644 | * never happened. |
| 5645 | */ |
| 5646 | { |
| 5647 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5648 | _Py_NewReference(self); |
| 5649 | self->ob_refcnt = refcnt; |
| 5650 | } |
| 5651 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5652 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5653 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5654 | * we need to undo that. */ |
| 5655 | _Py_DEC_REFTOTAL; |
| 5656 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5657 | * chain, so no more to do there. |
| 5658 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5659 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5660 | * undone. |
| 5661 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5662 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | --Py_TYPE(self)->tp_frees; |
| 5664 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5665 | #endif |
| 5666 | } |
| 5667 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5668 | |
| 5669 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5670 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5671 | structure, which incorporates the additional structures used for numbers, |
| 5672 | sequences and mappings. |
| 5673 | Note that multiple names may map to the same slot (e.g. __eq__, |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5674 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5675 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5676 | terminated with an all-zero entry. (This table is further initialized and |
| 5677 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5678 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5679 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5680 | |
| 5681 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5682 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5683 | #undef ETSLOT |
| 5684 | #undef SQSLOT |
| 5685 | #undef MPSLOT |
| 5686 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5687 | #undef UNSLOT |
| 5688 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5689 | #undef BINSLOT |
| 5690 | #undef RBINSLOT |
| 5691 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5692 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5693 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5694 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5695 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5697 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5698 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5699 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5700 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5701 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5703 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5704 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5705 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5707 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5708 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5709 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5710 | #define IBSLOT(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, \ |
| 5712 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5713 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5714 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5715 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5716 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5717 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5718 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5719 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5720 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5721 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5722 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5723 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5724 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5725 | |
| 5726 | static slotdef slotdefs[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5728 | "x.__len__() <==> len(x)"), |
| 5729 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5730 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5731 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5732 | user-defined methods has unexpected side-effects, as shown by |
| 5733 | test_descr.notimplemented() */ |
| 5734 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5735 | "x.__add__(y) <==> x+y"), |
| 5736 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5737 | "x.__mul__(n) <==> x*n"), |
| 5738 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5739 | "x.__rmul__(n) <==> n*x"), |
| 5740 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5741 | "x.__getitem__(y) <==> x[y]"), |
| 5742 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5743 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5744 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5745 | "x.__delitem__(y) <==> del x[y]"), |
| 5746 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5747 | "x.__contains__(y) <==> y in x"), |
| 5748 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5749 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5750 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5751 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5754 | "x.__len__() <==> len(x)"), |
| 5755 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5756 | wrap_binaryfunc, |
| 5757 | "x.__getitem__(y) <==> x[y]"), |
| 5758 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5759 | wrap_objobjargproc, |
| 5760 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5761 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5762 | wrap_delitem, |
| 5763 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5766 | "+"), |
| 5767 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5768 | "+"), |
| 5769 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5770 | "-"), |
| 5771 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5772 | "-"), |
| 5773 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5774 | "*"), |
| 5775 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5776 | "*"), |
| 5777 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5778 | "%"), |
| 5779 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5780 | "%"), |
| 5781 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5782 | "divmod(x, y)"), |
| 5783 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5784 | "divmod(y, x)"), |
| 5785 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5786 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5787 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5788 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5789 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5790 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5791 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5792 | "abs(x)"), |
| 5793 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5794 | "x != 0"), |
| 5795 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5796 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5797 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5798 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5799 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5800 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5801 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5802 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5803 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5804 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5805 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5806 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5807 | "int(x)"), |
| 5808 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5809 | "float(x)"), |
| 5810 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5811 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
| 5812 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5813 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5815 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5816 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5817 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5818 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5819 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5821 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5823 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5825 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5827 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5828 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5829 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5830 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5831 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5832 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5833 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5834 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5835 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5836 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5837 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5838 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5839 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5842 | "x.__str__() <==> str(x)"), |
| 5843 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5844 | "x.__repr__() <==> repr(x)"), |
| 5845 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5846 | "x.__hash__() <==> hash(x)"), |
| 5847 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5848 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5849 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5850 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5851 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5852 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5853 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5854 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5855 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5856 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5857 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5858 | "x.__delattr__('name') <==> del x.name"), |
| 5859 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5860 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5861 | "x.__lt__(y) <==> x<y"), |
| 5862 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5863 | "x.__le__(y) <==> x<=y"), |
| 5864 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5865 | "x.__eq__(y) <==> x==y"), |
| 5866 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5867 | "x.__ne__(y) <==> x!=y"), |
| 5868 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5869 | "x.__gt__(y) <==> x>y"), |
| 5870 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5871 | "x.__ge__(y) <==> x>=y"), |
| 5872 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5873 | "x.__iter__() <==> iter(x)"), |
| 5874 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5875 | "x.__next__() <==> next(x)"), |
| 5876 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5877 | "descr.__get__(obj[, type]) -> value"), |
| 5878 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5879 | "descr.__set__(obj, value)"), |
| 5880 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5881 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5882 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5883 | "x.__init__(...) initializes x; " |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 5884 | "see help(type(x)) for signature", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | PyWrapperFlag_KEYWORDS), |
| 5886 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5887 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
| 5888 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5889 | }; |
| 5890 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5891 | /* 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] | 5892 | 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] | 5893 | the offset to the type pointer, since it takes care to indirect through the |
| 5894 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5895 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5896 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5897 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5899 | char *ptr; |
| 5900 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5902 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5903 | assert(offset >= 0); |
| 5904 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5905 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5906 | ptr = (char *)type->tp_as_sequence; |
| 5907 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5908 | } |
| 5909 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5910 | ptr = (char *)type->tp_as_mapping; |
| 5911 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5912 | } |
| 5913 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5914 | ptr = (char *)type->tp_as_number; |
| 5915 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5916 | } |
| 5917 | else { |
| 5918 | ptr = (char *)type; |
| 5919 | } |
| 5920 | if (ptr != NULL) |
| 5921 | ptr += offset; |
| 5922 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5923 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5924 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5925 | /* Length of array of slotdef pointers used to store slots with the |
| 5926 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5927 | the same __name__, for any __name__. Since that's a static property, it is |
| 5928 | appropriate to declare fixed-size arrays for this. */ |
| 5929 | #define MAX_EQUIV 10 |
| 5930 | |
| 5931 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5932 | exactly one slot function. The name must be an interned string. */ |
| 5933 | static void ** |
| 5934 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | /* 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] | 5937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | /* pname and ptrs act as a little cache */ |
| 5939 | static PyObject *pname; |
| 5940 | static slotdef *ptrs[MAX_EQUIV]; |
| 5941 | slotdef *p, **pp; |
| 5942 | void **res, **ptr; |
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 | if (pname != name) { |
| 5945 | /* Collect all slotdefs that match name into ptrs. */ |
| 5946 | pname = name; |
| 5947 | pp = ptrs; |
| 5948 | for (p = slotdefs; p->name_strobj; p++) { |
| 5949 | if (p->name_strobj == name) |
| 5950 | *pp++ = p; |
| 5951 | } |
| 5952 | *pp = NULL; |
| 5953 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5955 | /* Look in all matching slots of the type; if exactly one of these has |
| 5956 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5957 | res = NULL; |
| 5958 | for (pp = ptrs; *pp; pp++) { |
| 5959 | ptr = slotptr(type, (*pp)->offset); |
| 5960 | if (ptr == NULL || *ptr == NULL) |
| 5961 | continue; |
| 5962 | if (res != NULL) |
| 5963 | return NULL; |
| 5964 | res = ptr; |
| 5965 | } |
| 5966 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5969 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5970 | does some incredibly complex thinking and then sticks something into the |
| 5971 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5972 | interests, and then stores a generic wrapper or a specific function into |
| 5973 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5974 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5975 | static slotdef * |
| 5976 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5977 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5978 | PyObject *descr; |
| 5979 | PyWrapperDescrObject *d; |
| 5980 | void *generic = NULL, *specific = NULL; |
| 5981 | int use_generic = 0; |
| 5982 | int offset = p->offset; |
| 5983 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5985 | if (ptr == NULL) { |
| 5986 | do { |
| 5987 | ++p; |
| 5988 | } while (p->offset == offset); |
| 5989 | return p; |
| 5990 | } |
| 5991 | do { |
| 5992 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5993 | if (descr == NULL) { |
| 5994 | if (ptr == (void**)&type->tp_iternext) { |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 5995 | specific = (void *)_PyObject_NextNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | } |
| 5997 | continue; |
| 5998 | } |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 5999 | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
| 6000 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6001 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6002 | if (tptr == NULL || tptr == ptr) |
| 6003 | generic = p->function; |
| 6004 | d = (PyWrapperDescrObject *)descr; |
| 6005 | if (d->d_base->wrapper == p->wrapper && |
| 6006 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 6007 | { |
| 6008 | if (specific == NULL || |
| 6009 | specific == d->d_wrapped) |
| 6010 | specific = d->d_wrapped; |
| 6011 | else |
| 6012 | use_generic = 1; |
| 6013 | } |
| 6014 | } |
| 6015 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 6016 | PyCFunction_GET_FUNCTION(descr) == |
| 6017 | (PyCFunction)tp_new_wrapper && |
| 6018 | ptr == (void**)&type->tp_new) |
| 6019 | { |
| 6020 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6021 | so must be special-cased differently. |
| 6022 | If we don't do this, creating an instance will |
| 6023 | always use slot_tp_new which will look up |
| 6024 | __new__ in the MRO which will call tp_new_wrapper |
| 6025 | which will look through the base classes looking |
| 6026 | for a static base and call its tp_new (usually |
| 6027 | PyType_GenericNew), after performing various |
| 6028 | sanity checks and constructing a new argument |
| 6029 | list. Cut all that nonsense short -- this speeds |
| 6030 | up instance creation tremendously. */ |
| 6031 | specific = (void *)type->tp_new; |
| 6032 | /* XXX I'm not 100% sure that there isn't a hole |
| 6033 | in this reasoning that requires additional |
| 6034 | sanity checks. I'll buy the first person to |
| 6035 | point out a bug in this reasoning a beer. */ |
| 6036 | } |
| 6037 | else if (descr == Py_None && |
| 6038 | ptr == (void**)&type->tp_hash) { |
| 6039 | /* We specifically allow __hash__ to be set to None |
| 6040 | to prevent inheritance of the default |
| 6041 | implementation from object.__hash__ */ |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6042 | specific = (void *)PyObject_HashNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6043 | } |
| 6044 | else { |
| 6045 | use_generic = 1; |
| 6046 | generic = p->function; |
| 6047 | } |
| 6048 | } while ((++p)->offset == offset); |
| 6049 | if (specific && !use_generic) |
| 6050 | *ptr = specific; |
| 6051 | else |
| 6052 | *ptr = generic; |
| 6053 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6056 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6057 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6058 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6059 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6061 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | for (; *pp; pp++) |
| 6064 | update_one_slot(type, *pp); |
| 6065 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6068 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 6069 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6070 | static int |
| 6071 | slotdef_cmp(const void *aa, const void *bb) |
| 6072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6073 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 6074 | int c = a->offset - b->offset; |
| 6075 | if (c != 0) |
| 6076 | return c; |
| 6077 | else |
| 6078 | /* Cannot use a-b, as this gives off_t, |
| 6079 | which may lose precision when converted to int. */ |
| 6080 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6083 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6084 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6085 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6086 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6088 | slotdef *p; |
| 6089 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6091 | if (initialized) |
| 6092 | return; |
| 6093 | for (p = slotdefs; p->name; p++) { |
| 6094 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 6095 | if (!p->name_strobj) |
| 6096 | Py_FatalError("Out of memory interning slotdef names"); |
| 6097 | } |
| 6098 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 6099 | slotdef_cmp); |
| 6100 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6103 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6104 | static int |
| 6105 | update_slot(PyTypeObject *type, PyObject *name) |
| 6106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6107 | slotdef *ptrs[MAX_EQUIV]; |
| 6108 | slotdef *p; |
| 6109 | slotdef **pp; |
| 6110 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6112 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6113 | subclasses. This could possibly be unified with the |
| 6114 | update_subclasses() recursion below, but carefully: |
| 6115 | they each have their own conditions on which to stop |
| 6116 | recursing into subclasses. */ |
| 6117 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 6118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6119 | init_slotdefs(); |
| 6120 | pp = ptrs; |
| 6121 | for (p = slotdefs; p->name; p++) { |
| 6122 | /* XXX assume name is interned! */ |
| 6123 | if (p->name_strobj == name) |
| 6124 | *pp++ = p; |
| 6125 | } |
| 6126 | *pp = NULL; |
| 6127 | for (pp = ptrs; *pp; pp++) { |
| 6128 | p = *pp; |
| 6129 | offset = p->offset; |
| 6130 | while (p > slotdefs && (p-1)->offset == offset) |
| 6131 | --p; |
| 6132 | *pp = p; |
| 6133 | } |
| 6134 | if (ptrs[0] == NULL) |
| 6135 | return 0; /* Not an attribute that affects any slots */ |
| 6136 | return update_subclasses(type, name, |
| 6137 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6140 | /* Store the proper functions in the slot dispatches at class (type) |
| 6141 | definition time, based upon which operations the class overrides in its |
| 6142 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6143 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6144 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6146 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6148 | init_slotdefs(); |
| 6149 | for (p = slotdefs; p->name; ) |
| 6150 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6151 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6152 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6153 | static void |
| 6154 | update_all_slots(PyTypeObject* type) |
| 6155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6158 | init_slotdefs(); |
| 6159 | for (p = slotdefs; p->name; p++) { |
| 6160 | /* update_slot returns int but can't actually fail */ |
| 6161 | update_slot(type, p->name_strobj); |
| 6162 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6165 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6166 | recursive functions to call a callback for all subclasses, |
| 6167 | but refraining from recursing into subclasses that define 'name'. */ |
| 6168 | |
| 6169 | static int |
| 6170 | update_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 | if (callback(type, data) < 0) |
| 6174 | return -1; |
| 6175 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | static int |
| 6179 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6180 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6182 | PyTypeObject *subclass; |
| 6183 | PyObject *ref, *subclasses, *dict; |
| 6184 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6186 | subclasses = type->tp_subclasses; |
| 6187 | if (subclasses == NULL) |
| 6188 | return 0; |
| 6189 | assert(PyList_Check(subclasses)); |
| 6190 | n = PyList_GET_SIZE(subclasses); |
| 6191 | for (i = 0; i < n; i++) { |
| 6192 | ref = PyList_GET_ITEM(subclasses, i); |
| 6193 | assert(PyWeakref_CheckRef(ref)); |
| 6194 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6195 | assert(subclass != NULL); |
| 6196 | if ((PyObject *)subclass == Py_None) |
| 6197 | continue; |
| 6198 | assert(PyType_Check(subclass)); |
| 6199 | /* Avoid recursing down into unaffected classes */ |
| 6200 | dict = subclass->tp_dict; |
| 6201 | if (dict != NULL && PyDict_Check(dict) && |
| 6202 | PyDict_GetItem(dict, name) != NULL) |
| 6203 | continue; |
| 6204 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6205 | return -1; |
| 6206 | } |
| 6207 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6210 | /* This function is called by PyType_Ready() to populate the type's |
| 6211 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6212 | function slot (like tp_repr) that's defined in the type, one or more |
| 6213 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6214 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6215 | cause more than one descriptor to be added (for example, the nb_add |
| 6216 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6217 | slots compete for the same descriptor (for example both sq_item and |
| 6218 | mp_subscript generate a __getitem__ descriptor). |
| 6219 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6220 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6221 | 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] | 6222 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6223 | between competing slots: the members of PyHeapTypeObject are listed |
| 6224 | from most general to least general, so the most general slot is |
| 6225 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6226 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6227 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6228 | |
| 6229 | This only adds new descriptors and doesn't overwrite entries in |
| 6230 | tp_dict that were previously defined. The descriptors contain a |
| 6231 | reference to the C function they must call, so that it's safe if they |
| 6232 | are copied into a subtype's __dict__ and the subtype has a different |
| 6233 | C function in its slot -- calling the method defined by the |
| 6234 | descriptor will call the C function that was used to create it, |
| 6235 | rather than the C function present in the slot when it is called. |
| 6236 | (This is important because a subtype may have a C function in the |
| 6237 | slot that calls the method from the dictionary, and we want to avoid |
| 6238 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6239 | |
| 6240 | static int |
| 6241 | add_operators(PyTypeObject *type) |
| 6242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | PyObject *dict = type->tp_dict; |
| 6244 | slotdef *p; |
| 6245 | PyObject *descr; |
| 6246 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6248 | init_slotdefs(); |
| 6249 | for (p = slotdefs; p->name; p++) { |
| 6250 | if (p->wrapper == NULL) |
| 6251 | continue; |
| 6252 | ptr = slotptr(type, p->offset); |
| 6253 | if (!ptr || !*ptr) |
| 6254 | continue; |
| 6255 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6256 | continue; |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6257 | if (*ptr == (void *)PyObject_HashNotImplemented) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | /* Classes may prevent the inheritance of the tp_hash |
| 6259 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6260 | visible as a None value for the __hash__ attribute. */ |
| 6261 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6262 | return -1; |
| 6263 | } |
| 6264 | else { |
| 6265 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6266 | if (descr == NULL) |
| 6267 | return -1; |
| 6268 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6269 | return -1; |
| 6270 | Py_DECREF(descr); |
| 6271 | } |
| 6272 | } |
| 6273 | if (type->tp_new != NULL) { |
| 6274 | if (add_tp_new_wrapper(type) < 0) |
| 6275 | return -1; |
| 6276 | } |
| 6277 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6280 | |
| 6281 | /* Cooperative 'super' */ |
| 6282 | |
| 6283 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6284 | PyObject_HEAD |
| 6285 | PyTypeObject *type; |
| 6286 | PyObject *obj; |
| 6287 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6288 | } superobject; |
| 6289 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6290 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6291 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6292 | "the class invoking super()"}, |
| 6293 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6294 | "the instance invoking super(); may be None"}, |
| 6295 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6296 | "the type of the instance invoking super(); may be None"}, |
| 6297 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6298 | }; |
| 6299 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6300 | static void |
| 6301 | super_dealloc(PyObject *self) |
| 6302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6303 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6305 | _PyObject_GC_UNTRACK(self); |
| 6306 | Py_XDECREF(su->obj); |
| 6307 | Py_XDECREF(su->type); |
| 6308 | Py_XDECREF(su->obj_type); |
| 6309 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6313 | super_repr(PyObject *self) |
| 6314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6315 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6317 | if (su->obj_type) |
| 6318 | return PyUnicode_FromFormat( |
| 6319 | "<super: <class '%s'>, <%s object>>", |
| 6320 | su->type ? su->type->tp_name : "NULL", |
| 6321 | su->obj_type->tp_name); |
| 6322 | else |
| 6323 | return PyUnicode_FromFormat( |
| 6324 | "<super: <class '%s'>, NULL>", |
| 6325 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6329 | super_getattro(PyObject *self, PyObject *name) |
| 6330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6331 | superobject *su = (superobject *)self; |
| 6332 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6334 | if (!skip) { |
| 6335 | /* We want __class__ to return the class of the super object |
| 6336 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6337 | skip = (PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6338 | PyUnicode_GET_LENGTH(name) == 9 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6339 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6340 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | if (!skip) { |
| 6343 | PyObject *mro, *res, *tmp, *dict; |
| 6344 | PyTypeObject *starttype; |
| 6345 | descrgetfunc f; |
| 6346 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6348 | starttype = su->obj_type; |
| 6349 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6351 | if (mro == NULL) |
| 6352 | n = 0; |
| 6353 | else { |
| 6354 | assert(PyTuple_Check(mro)); |
| 6355 | n = PyTuple_GET_SIZE(mro); |
| 6356 | } |
| 6357 | for (i = 0; i < n; i++) { |
| 6358 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6359 | break; |
| 6360 | } |
| 6361 | i++; |
| 6362 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6363 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 6364 | replaced during PyDict_GetItem(dict, name) */ |
| 6365 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6366 | for (; i < n; i++) { |
| 6367 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6368 | if (PyType_Check(tmp)) |
| 6369 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6370 | else |
| 6371 | continue; |
| 6372 | res = PyDict_GetItem(dict, name); |
| 6373 | if (res != NULL) { |
| 6374 | Py_INCREF(res); |
| 6375 | f = Py_TYPE(res)->tp_descr_get; |
| 6376 | if (f != NULL) { |
| 6377 | tmp = f(res, |
| 6378 | /* Only pass 'obj' param if |
| 6379 | this is instance-mode super |
| 6380 | (See SF ID #743627) |
| 6381 | */ |
| 6382 | (su->obj == (PyObject *) |
| 6383 | su->obj_type |
| 6384 | ? (PyObject *)NULL |
| 6385 | : su->obj), |
| 6386 | (PyObject *)starttype); |
| 6387 | Py_DECREF(res); |
| 6388 | res = tmp; |
| 6389 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6390 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6391 | return res; |
| 6392 | } |
| 6393 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6394 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | } |
| 6396 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6399 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6400 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6403 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 6404 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6407 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | - If it is an instance, it must be an instance of 'type'. This is |
| 6410 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6412 | But... when obj is an instance, we want to allow for the case where |
| 6413 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6414 | This will allow using super() with a proxy for obj. |
| 6415 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6417 | /* Check for first bullet above (special case) */ |
| 6418 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6419 | Py_INCREF(obj); |
| 6420 | return (PyTypeObject *)obj; |
| 6421 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6423 | /* Normal case */ |
| 6424 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6425 | Py_INCREF(Py_TYPE(obj)); |
| 6426 | return Py_TYPE(obj); |
| 6427 | } |
| 6428 | else { |
| 6429 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6430 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6431 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6432 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6433 | if (class_attr != NULL && |
| 6434 | PyType_Check(class_attr) && |
| 6435 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6436 | { |
| 6437 | int ok = PyType_IsSubtype( |
| 6438 | (PyTypeObject *)class_attr, type); |
| 6439 | if (ok) |
| 6440 | return (PyTypeObject *)class_attr; |
| 6441 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6443 | if (class_attr == NULL) |
| 6444 | PyErr_Clear(); |
| 6445 | else |
| 6446 | Py_DECREF(class_attr); |
| 6447 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | PyErr_SetString(PyExc_TypeError, |
| 6450 | "super(type, obj): " |
| 6451 | "obj must be an instance or subtype of type"); |
| 6452 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6455 | static PyObject * |
| 6456 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6458 | superobject *su = (superobject *)self; |
| 6459 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6461 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6462 | /* Not binding to an object, or already bound */ |
| 6463 | Py_INCREF(self); |
| 6464 | return self; |
| 6465 | } |
| 6466 | if (Py_TYPE(su) != &PySuper_Type) |
| 6467 | /* If su is an instance of a (strict) subclass of super, |
| 6468 | call its type */ |
| 6469 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6470 | su->type, obj, NULL); |
| 6471 | else { |
| 6472 | /* Inline the common case */ |
| 6473 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6474 | if (obj_type == NULL) |
| 6475 | return NULL; |
| 6476 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6477 | NULL, NULL); |
| 6478 | if (newobj == NULL) |
| 6479 | return NULL; |
| 6480 | Py_INCREF(su->type); |
| 6481 | Py_INCREF(obj); |
| 6482 | newobj->type = su->type; |
| 6483 | newobj->obj = obj; |
| 6484 | newobj->obj_type = obj_type; |
| 6485 | return (PyObject *)newobj; |
| 6486 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | static int |
| 6490 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6492 | superobject *su = (superobject *)self; |
| 6493 | PyTypeObject *type = NULL; |
| 6494 | PyObject *obj = NULL; |
| 6495 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6498 | return -1; |
| 6499 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6500 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | if (type == NULL) { |
| 6503 | /* Call super(), without args -- fill in from __class__ |
| 6504 | and first local variable on the stack. */ |
| 6505 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6506 | PyCodeObject *co = f->f_code; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6507 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6508 | if (co == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6509 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6510 | "super(): no code object"); |
| 6511 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6512 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6513 | if (co->co_argcount == 0) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6514 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6515 | "super(): no arguments"); |
| 6516 | return -1; |
| 6517 | } |
| 6518 | obj = f->f_localsplus[0]; |
| 6519 | if (obj == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6520 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6521 | "super(): arg[0] deleted"); |
| 6522 | return -1; |
| 6523 | } |
| 6524 | if (co->co_freevars == NULL) |
| 6525 | n = 0; |
| 6526 | else { |
| 6527 | assert(PyTuple_Check(co->co_freevars)); |
| 6528 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6529 | } |
| 6530 | for (i = 0; i < n; i++) { |
| 6531 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6532 | assert(PyUnicode_Check(name)); |
| 6533 | if (!PyUnicode_CompareWithASCIIString(name, |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 6534 | "__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6535 | Py_ssize_t index = co->co_nlocals + |
| 6536 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6537 | PyObject *cell = f->f_localsplus[index]; |
| 6538 | if (cell == NULL || !PyCell_Check(cell)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6539 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6540 | "super(): bad __class__ cell"); |
| 6541 | return -1; |
| 6542 | } |
| 6543 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6544 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6545 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | "super(): empty __class__ cell"); |
| 6547 | return -1; |
| 6548 | } |
| 6549 | if (!PyType_Check(type)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6550 | PyErr_Format(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | "super(): __class__ is not a type (%s)", |
| 6552 | Py_TYPE(type)->tp_name); |
| 6553 | return -1; |
| 6554 | } |
| 6555 | break; |
| 6556 | } |
| 6557 | } |
| 6558 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6559 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | "super(): __class__ cell not found"); |
| 6561 | return -1; |
| 6562 | } |
| 6563 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | if (obj == Py_None) |
| 6566 | obj = NULL; |
| 6567 | if (obj != NULL) { |
| 6568 | obj_type = supercheck(type, obj); |
| 6569 | if (obj_type == NULL) |
| 6570 | return -1; |
| 6571 | Py_INCREF(obj); |
| 6572 | } |
| 6573 | Py_INCREF(type); |
| 6574 | su->type = type; |
| 6575 | su->obj = obj; |
| 6576 | su->obj_type = obj_type; |
| 6577 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6580 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6581 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6582 | "super(type) -> unbound super object\n" |
| 6583 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6584 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6585 | "Typical use to call a cooperative superclass method:\n" |
| 6586 | "class C(B):\n" |
| 6587 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6589 | "This works for class methods too:\n" |
| 6590 | "class C(B):\n" |
| 6591 | " @classmethod\n" |
| 6592 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6594 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6595 | static int |
| 6596 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6600 | Py_VISIT(su->obj); |
| 6601 | Py_VISIT(su->type); |
| 6602 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6604 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6607 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6608 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6609 | "super", /* tp_name */ |
| 6610 | sizeof(superobject), /* tp_basicsize */ |
| 6611 | 0, /* tp_itemsize */ |
| 6612 | /* methods */ |
| 6613 | super_dealloc, /* tp_dealloc */ |
| 6614 | 0, /* tp_print */ |
| 6615 | 0, /* tp_getattr */ |
| 6616 | 0, /* tp_setattr */ |
| 6617 | 0, /* tp_reserved */ |
| 6618 | super_repr, /* tp_repr */ |
| 6619 | 0, /* tp_as_number */ |
| 6620 | 0, /* tp_as_sequence */ |
| 6621 | 0, /* tp_as_mapping */ |
| 6622 | 0, /* tp_hash */ |
| 6623 | 0, /* tp_call */ |
| 6624 | 0, /* tp_str */ |
| 6625 | super_getattro, /* tp_getattro */ |
| 6626 | 0, /* tp_setattro */ |
| 6627 | 0, /* tp_as_buffer */ |
| 6628 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6629 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6630 | super_doc, /* tp_doc */ |
| 6631 | super_traverse, /* tp_traverse */ |
| 6632 | 0, /* tp_clear */ |
| 6633 | 0, /* tp_richcompare */ |
| 6634 | 0, /* tp_weaklistoffset */ |
| 6635 | 0, /* tp_iter */ |
| 6636 | 0, /* tp_iternext */ |
| 6637 | 0, /* tp_methods */ |
| 6638 | super_members, /* tp_members */ |
| 6639 | 0, /* tp_getset */ |
| 6640 | 0, /* tp_base */ |
| 6641 | 0, /* tp_dict */ |
| 6642 | super_descr_get, /* tp_descr_get */ |
| 6643 | 0, /* tp_descr_set */ |
| 6644 | 0, /* tp_dictoffset */ |
| 6645 | super_init, /* tp_init */ |
| 6646 | PyType_GenericAlloc, /* tp_alloc */ |
| 6647 | PyType_GenericNew, /* tp_new */ |
| 6648 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6649 | }; |