Merged revisions 78818 via svnmerge from
[python/dscho.git] / Objects / typeobject.c
blob60483e718cdb4ced9f47d19d5b4a4c077ae0f3a5
1 /* Type object implementation */
3 #include "Python.h"
4 #include "frameobject.h"
5 #include "structmember.h"
7 #include <ctype.h>
10 /* Support type attribute cache */
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. */
16 #define MCACHE_MAX_ATTR_SIZE 100
17 #define MCACHE_SIZE_EXP 10
18 #define MCACHE_HASH(version, name_hash) \
19 (((unsigned int)(version) * (unsigned int)(name_hash)) \
20 >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
21 #define MCACHE_HASH_METHOD(type, name) \
22 MCACHE_HASH((type)->tp_version_tag, \
23 ((PyUnicodeObject *)(name))->hash)
24 #define MCACHE_CACHEABLE_NAME(name) \
25 PyUnicode_CheckExact(name) && \
26 PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
28 struct method_cache_entry {
29 unsigned int version;
30 PyObject *name; /* reference to exactly a str or None */
31 PyObject *value; /* borrowed */
34 static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
35 static unsigned int next_version_tag = 0;
37 unsigned int
38 PyType_ClearCache(void)
40 Py_ssize_t i;
41 unsigned int cur_version_tag = next_version_tag - 1;
43 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
44 method_cache[i].version = 0;
45 Py_CLEAR(method_cache[i].name);
46 method_cache[i].value = NULL;
48 next_version_tag = 0;
49 /* mark all version tags as invalid */
50 PyType_Modified(&PyBaseObject_Type);
51 return cur_version_tag;
54 void
55 PyType_Modified(PyTypeObject *type)
57 /* Invalidate any cached data for the specified type and all
58 subclasses. This function is called after the base
59 classes, mro, or attributes of the type are altered.
61 Invariants:
63 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
64 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
65 objects coming from non-recompiled extension modules)
67 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
68 it must first be set on all super types.
70 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
71 type (so it must first clear it on all subclasses). The
72 tp_version_tag value is meaningless unless this flag is set.
73 We don't assign new version tags eagerly, but only as
74 needed.
76 PyObject *raw, *ref;
77 Py_ssize_t i, n;
79 if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
80 return;
82 raw = type->tp_subclasses;
83 if (raw != NULL) {
84 n = PyList_GET_SIZE(raw);
85 for (i = 0; i < n; i++) {
86 ref = PyList_GET_ITEM(raw, i);
87 ref = PyWeakref_GET_OBJECT(ref);
88 if (ref != Py_None) {
89 PyType_Modified((PyTypeObject *)ref);
93 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
96 static void
97 type_mro_modified(PyTypeObject *type, PyObject *bases) {
99 Check that all base classes or elements of the mro of type are
100 able to be cached. This function is called after the base
101 classes or mro of the type are altered.
103 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
104 inherits from an old-style class, either directly or if it
105 appears in the MRO of a new-style class. No support either for
106 custom MROs that include types that are not officially super
107 types.
109 Called from mro_internal, which will subsequently be called on
110 each subclass when their mro is recursively updated.
112 Py_ssize_t i, n;
113 int clear = 0;
115 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
116 return;
118 n = PyTuple_GET_SIZE(bases);
119 for (i = 0; i < n; i++) {
120 PyObject *b = PyTuple_GET_ITEM(bases, i);
121 PyTypeObject *cls;
123 if (!PyType_Check(b) ) {
124 clear = 1;
125 break;
128 cls = (PyTypeObject *)b;
130 if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
131 !PyType_IsSubtype(type, cls)) {
132 clear = 1;
133 break;
137 if (clear)
138 type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
139 Py_TPFLAGS_VALID_VERSION_TAG);
142 static int
143 assign_version_tag(PyTypeObject *type)
145 /* Ensure that the tp_version_tag is valid and set
146 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
147 must first be done on all super classes. Return 0 if this
148 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
150 Py_ssize_t i, n;
151 PyObject *bases;
153 if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
154 return 1;
155 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
156 return 0;
157 if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
158 return 0;
160 type->tp_version_tag = next_version_tag++;
161 /* for stress-testing: next_version_tag &= 0xFF; */
163 if (type->tp_version_tag == 0) {
164 /* wrap-around or just starting Python - clear the whole
165 cache by filling names with references to Py_None.
166 Values are also set to NULL for added protection, as they
167 are borrowed reference */
168 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
169 method_cache[i].value = NULL;
170 Py_XDECREF(method_cache[i].name);
171 method_cache[i].name = Py_None;
172 Py_INCREF(Py_None);
174 /* mark all version tags as invalid */
175 PyType_Modified(&PyBaseObject_Type);
176 return 1;
178 bases = type->tp_bases;
179 n = PyTuple_GET_SIZE(bases);
180 for (i = 0; i < n; i++) {
181 PyObject *b = PyTuple_GET_ITEM(bases, i);
182 assert(PyType_Check(b));
183 if (!assign_version_tag((PyTypeObject *)b))
184 return 0;
186 type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
187 return 1;
191 static PyMemberDef type_members[] = {
192 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
193 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
194 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
195 {"__weakrefoffset__", T_LONG,
196 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
197 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
198 {"__dictoffset__", T_LONG,
199 offsetof(PyTypeObject, tp_dictoffset), READONLY},
200 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
204 static PyObject *
205 type_name(PyTypeObject *type, void *context)
207 const char *s;
209 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
210 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
212 Py_INCREF(et->ht_name);
213 return et->ht_name;
215 else {
216 s = strrchr(type->tp_name, '.');
217 if (s == NULL)
218 s = type->tp_name;
219 else
220 s++;
221 return PyUnicode_FromString(s);
225 static int
226 type_set_name(PyTypeObject *type, PyObject *value, void *context)
228 PyHeapTypeObject* et;
229 char *tp_name;
230 PyObject *tmp;
232 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
233 PyErr_Format(PyExc_TypeError,
234 "can't set %s.__name__", type->tp_name);
235 return -1;
237 if (!value) {
238 PyErr_Format(PyExc_TypeError,
239 "can't delete %s.__name__", type->tp_name);
240 return -1;
242 if (!PyUnicode_Check(value)) {
243 PyErr_Format(PyExc_TypeError,
244 "can only assign string to %s.__name__, not '%s'",
245 type->tp_name, Py_TYPE(value)->tp_name);
246 return -1;
249 /* Check absence of null characters */
250 tmp = PyUnicode_FromStringAndSize("\0", 1);
251 if (tmp == NULL)
252 return -1;
253 if (PyUnicode_Contains(value, tmp) != 0) {
254 Py_DECREF(tmp);
255 PyErr_Format(PyExc_ValueError,
256 "__name__ must not contain null bytes");
257 return -1;
259 Py_DECREF(tmp);
261 tp_name = _PyUnicode_AsString(value);
262 if (tp_name == NULL)
263 return -1;
265 et = (PyHeapTypeObject*)type;
267 Py_INCREF(value);
269 Py_DECREF(et->ht_name);
270 et->ht_name = value;
272 type->tp_name = tp_name;
274 return 0;
277 static PyObject *
278 type_module(PyTypeObject *type, void *context)
280 PyObject *mod;
281 char *s;
283 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
284 mod = PyDict_GetItemString(type->tp_dict, "__module__");
285 if (!mod) {
286 PyErr_Format(PyExc_AttributeError, "__module__");
287 return 0;
289 Py_XINCREF(mod);
290 return mod;
292 else {
293 s = strrchr(type->tp_name, '.');
294 if (s != NULL)
295 return PyUnicode_FromStringAndSize(
296 type->tp_name, (Py_ssize_t)(s - type->tp_name));
297 return PyUnicode_FromString("builtins");
301 static int
302 type_set_module(PyTypeObject *type, PyObject *value, void *context)
304 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
305 PyErr_Format(PyExc_TypeError,
306 "can't set %s.__module__", type->tp_name);
307 return -1;
309 if (!value) {
310 PyErr_Format(PyExc_TypeError,
311 "can't delete %s.__module__", type->tp_name);
312 return -1;
315 PyType_Modified(type);
317 return PyDict_SetItemString(type->tp_dict, "__module__", value);
320 static PyObject *
321 type_abstractmethods(PyTypeObject *type, void *context)
323 PyObject *mod = PyDict_GetItemString(type->tp_dict,
324 "__abstractmethods__");
325 if (!mod) {
326 PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
327 return NULL;
329 Py_XINCREF(mod);
330 return mod;
333 static int
334 type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
336 /* __abstractmethods__ should only be set once on a type, in
337 abc.ABCMeta.__new__, so this function doesn't do anything
338 special to update subclasses.
340 int res = PyDict_SetItemString(type->tp_dict,
341 "__abstractmethods__", value);
342 if (res == 0) {
343 PyType_Modified(type);
344 if (value && PyObject_IsTrue(value)) {
345 type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
347 else {
348 type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
351 return res;
354 static PyObject *
355 type_get_bases(PyTypeObject *type, void *context)
357 Py_INCREF(type->tp_bases);
358 return type->tp_bases;
361 static PyTypeObject *best_base(PyObject *);
362 static int mro_internal(PyTypeObject *);
363 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
364 static int add_subclass(PyTypeObject*, PyTypeObject*);
365 static void remove_subclass(PyTypeObject *, PyTypeObject *);
366 static void update_all_slots(PyTypeObject *);
368 typedef int (*update_callback)(PyTypeObject *, void *);
369 static int update_subclasses(PyTypeObject *type, PyObject *name,
370 update_callback callback, void *data);
371 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
372 update_callback callback, void *data);
374 static int
375 mro_subclasses(PyTypeObject *type, PyObject* temp)
377 PyTypeObject *subclass;
378 PyObject *ref, *subclasses, *old_mro;
379 Py_ssize_t i, n;
381 subclasses = type->tp_subclasses;
382 if (subclasses == NULL)
383 return 0;
384 assert(PyList_Check(subclasses));
385 n = PyList_GET_SIZE(subclasses);
386 for (i = 0; i < n; i++) {
387 ref = PyList_GET_ITEM(subclasses, i);
388 assert(PyWeakref_CheckRef(ref));
389 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
390 assert(subclass != NULL);
391 if ((PyObject *)subclass == Py_None)
392 continue;
393 assert(PyType_Check(subclass));
394 old_mro = subclass->tp_mro;
395 if (mro_internal(subclass) < 0) {
396 subclass->tp_mro = old_mro;
397 return -1;
399 else {
400 PyObject* tuple;
401 tuple = PyTuple_Pack(2, subclass, old_mro);
402 Py_DECREF(old_mro);
403 if (!tuple)
404 return -1;
405 if (PyList_Append(temp, tuple) < 0)
406 return -1;
407 Py_DECREF(tuple);
409 if (mro_subclasses(subclass, temp) < 0)
410 return -1;
412 return 0;
415 static int
416 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
418 Py_ssize_t i;
419 int r = 0;
420 PyObject *ob, *temp;
421 PyTypeObject *new_base, *old_base;
422 PyObject *old_bases, *old_mro;
424 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
425 PyErr_Format(PyExc_TypeError,
426 "can't set %s.__bases__", type->tp_name);
427 return -1;
429 if (!value) {
430 PyErr_Format(PyExc_TypeError,
431 "can't delete %s.__bases__", type->tp_name);
432 return -1;
434 if (!PyTuple_Check(value)) {
435 PyErr_Format(PyExc_TypeError,
436 "can only assign tuple to %s.__bases__, not %s",
437 type->tp_name, Py_TYPE(value)->tp_name);
438 return -1;
440 if (PyTuple_GET_SIZE(value) == 0) {
441 PyErr_Format(PyExc_TypeError,
442 "can only assign non-empty tuple to %s.__bases__, not ()",
443 type->tp_name);
444 return -1;
446 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
447 ob = PyTuple_GET_ITEM(value, i);
448 if (!PyType_Check(ob)) {
449 PyErr_Format(
450 PyExc_TypeError,
451 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
452 type->tp_name, Py_TYPE(ob)->tp_name);
453 return -1;
455 if (PyType_Check(ob)) {
456 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
457 PyErr_SetString(PyExc_TypeError,
458 "a __bases__ item causes an inheritance cycle");
459 return -1;
464 new_base = best_base(value);
466 if (!new_base) {
467 return -1;
470 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
471 return -1;
473 Py_INCREF(new_base);
474 Py_INCREF(value);
476 old_bases = type->tp_bases;
477 old_base = type->tp_base;
478 old_mro = type->tp_mro;
480 type->tp_bases = value;
481 type->tp_base = new_base;
483 if (mro_internal(type) < 0) {
484 goto bail;
487 temp = PyList_New(0);
488 if (!temp)
489 goto bail;
491 r = mro_subclasses(type, temp);
493 if (r < 0) {
494 for (i = 0; i < PyList_Size(temp); i++) {
495 PyTypeObject* cls;
496 PyObject* mro;
497 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
498 "", 2, 2, &cls, &mro);
499 Py_INCREF(mro);
500 ob = cls->tp_mro;
501 cls->tp_mro = mro;
502 Py_DECREF(ob);
504 Py_DECREF(temp);
505 goto bail;
508 Py_DECREF(temp);
510 /* any base that was in __bases__ but now isn't, we
511 need to remove |type| from its tp_subclasses.
512 conversely, any class now in __bases__ that wasn't
513 needs to have |type| added to its subclasses. */
515 /* for now, sod that: just remove from all old_bases,
516 add to all new_bases */
518 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
519 ob = PyTuple_GET_ITEM(old_bases, i);
520 if (PyType_Check(ob)) {
521 remove_subclass(
522 (PyTypeObject*)ob, type);
526 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
527 ob = PyTuple_GET_ITEM(value, i);
528 if (PyType_Check(ob)) {
529 if (add_subclass((PyTypeObject*)ob, type) < 0)
530 r = -1;
534 update_all_slots(type);
536 Py_DECREF(old_bases);
537 Py_DECREF(old_base);
538 Py_DECREF(old_mro);
540 return r;
542 bail:
543 Py_DECREF(type->tp_bases);
544 Py_DECREF(type->tp_base);
545 if (type->tp_mro != old_mro) {
546 Py_DECREF(type->tp_mro);
549 type->tp_bases = old_bases;
550 type->tp_base = old_base;
551 type->tp_mro = old_mro;
553 return -1;
556 static PyObject *
557 type_dict(PyTypeObject *type, void *context)
559 if (type->tp_dict == NULL) {
560 Py_INCREF(Py_None);
561 return Py_None;
563 return PyDictProxy_New(type->tp_dict);
566 static PyObject *
567 type_get_doc(PyTypeObject *type, void *context)
569 PyObject *result;
570 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
571 return PyUnicode_FromString(type->tp_doc);
572 result = PyDict_GetItemString(type->tp_dict, "__doc__");
573 if (result == NULL) {
574 result = Py_None;
575 Py_INCREF(result);
577 else if (Py_TYPE(result)->tp_descr_get) {
578 result = Py_TYPE(result)->tp_descr_get(result, NULL,
579 (PyObject *)type);
581 else {
582 Py_INCREF(result);
584 return result;
587 static PyObject *
588 type___instancecheck__(PyObject *type, PyObject *inst)
590 switch (_PyObject_RealIsInstance(inst, type)) {
591 case -1:
592 return NULL;
593 case 0:
594 Py_RETURN_FALSE;
595 default:
596 Py_RETURN_TRUE;
601 static PyObject *
602 type___subclasscheck__(PyObject *type, PyObject *inst)
604 switch (_PyObject_RealIsSubclass(inst, type)) {
605 case -1:
606 return NULL;
607 case 0:
608 Py_RETURN_FALSE;
609 default:
610 Py_RETURN_TRUE;
615 static PyGetSetDef type_getsets[] = {
616 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
617 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
618 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
619 {"__abstractmethods__", (getter)type_abstractmethods,
620 (setter)type_set_abstractmethods, NULL},
621 {"__dict__", (getter)type_dict, NULL, NULL},
622 {"__doc__", (getter)type_get_doc, NULL, NULL},
626 static PyObject *
627 type_repr(PyTypeObject *type)
629 PyObject *mod, *name, *rtn;
631 mod = type_module(type, NULL);
632 if (mod == NULL)
633 PyErr_Clear();
634 else if (!PyUnicode_Check(mod)) {
635 Py_DECREF(mod);
636 mod = NULL;
638 name = type_name(type, NULL);
639 if (name == NULL)
640 return NULL;
642 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
643 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
644 else
645 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
647 Py_XDECREF(mod);
648 Py_DECREF(name);
649 return rtn;
652 static PyObject *
653 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
655 PyObject *obj;
657 if (type->tp_new == NULL) {
658 PyErr_Format(PyExc_TypeError,
659 "cannot create '%.100s' instances",
660 type->tp_name);
661 return NULL;
664 obj = type->tp_new(type, args, kwds);
665 if (obj != NULL) {
666 /* Ugly exception: when the call was type(something),
667 don't call tp_init on the result. */
668 if (type == &PyType_Type &&
669 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
670 (kwds == NULL ||
671 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
672 return obj;
673 /* If the returned object is not an instance of type,
674 it won't be initialized. */
675 if (!PyType_IsSubtype(Py_TYPE(obj), type))
676 return obj;
677 type = Py_TYPE(obj);
678 if (type->tp_init != NULL &&
679 type->tp_init(obj, args, kwds) < 0) {
680 Py_DECREF(obj);
681 obj = NULL;
684 return obj;
687 PyObject *
688 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
690 PyObject *obj;
691 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
692 /* note that we need to add one, for the sentinel */
694 if (PyType_IS_GC(type))
695 obj = _PyObject_GC_Malloc(size);
696 else
697 obj = (PyObject *)PyObject_MALLOC(size);
699 if (obj == NULL)
700 return PyErr_NoMemory();
702 memset(obj, '\0', size);
704 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
705 Py_INCREF(type);
707 if (type->tp_itemsize == 0)
708 PyObject_INIT(obj, type);
709 else
710 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
712 if (PyType_IS_GC(type))
713 _PyObject_GC_TRACK(obj);
714 return obj;
717 PyObject *
718 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
720 return type->tp_alloc(type, 0);
723 /* Helpers for subtyping */
725 static int
726 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
728 Py_ssize_t i, n;
729 PyMemberDef *mp;
731 n = Py_SIZE(type);
732 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
733 for (i = 0; i < n; i++, mp++) {
734 if (mp->type == T_OBJECT_EX) {
735 char *addr = (char *)self + mp->offset;
736 PyObject *obj = *(PyObject **)addr;
737 if (obj != NULL) {
738 int err = visit(obj, arg);
739 if (err)
740 return err;
744 return 0;
747 static int
748 subtype_traverse(PyObject *self, visitproc visit, void *arg)
750 PyTypeObject *type, *base;
751 traverseproc basetraverse;
753 /* Find the nearest base with a different tp_traverse,
754 and traverse slots while we're at it */
755 type = Py_TYPE(self);
756 base = type;
757 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
758 if (Py_SIZE(base)) {
759 int err = traverse_slots(base, self, visit, arg);
760 if (err)
761 return err;
763 base = base->tp_base;
764 assert(base);
767 if (type->tp_dictoffset != base->tp_dictoffset) {
768 PyObject **dictptr = _PyObject_GetDictPtr(self);
769 if (dictptr && *dictptr)
770 Py_VISIT(*dictptr);
773 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
774 /* For a heaptype, the instances count as references
775 to the type. Traverse the type so the collector
776 can find cycles involving this link. */
777 Py_VISIT(type);
779 if (basetraverse)
780 return basetraverse(self, visit, arg);
781 return 0;
784 static void
785 clear_slots(PyTypeObject *type, PyObject *self)
787 Py_ssize_t i, n;
788 PyMemberDef *mp;
790 n = Py_SIZE(type);
791 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
792 for (i = 0; i < n; i++, mp++) {
793 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
794 char *addr = (char *)self + mp->offset;
795 PyObject *obj = *(PyObject **)addr;
796 if (obj != NULL) {
797 *(PyObject **)addr = NULL;
798 Py_DECREF(obj);
804 static int
805 subtype_clear(PyObject *self)
807 PyTypeObject *type, *base;
808 inquiry baseclear;
810 /* Find the nearest base with a different tp_clear
811 and clear slots while we're at it */
812 type = Py_TYPE(self);
813 base = type;
814 while ((baseclear = base->tp_clear) == subtype_clear) {
815 if (Py_SIZE(base))
816 clear_slots(base, self);
817 base = base->tp_base;
818 assert(base);
821 /* There's no need to clear the instance dict (if any);
822 the collector will call its tp_clear handler. */
824 if (baseclear)
825 return baseclear(self);
826 return 0;
829 static void
830 subtype_dealloc(PyObject *self)
832 PyTypeObject *type, *base;
833 destructor basedealloc;
835 /* Extract the type; we expect it to be a heap type */
836 type = Py_TYPE(self);
837 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
839 /* Test whether the type has GC exactly once */
841 if (!PyType_IS_GC(type)) {
842 /* It's really rare to find a dynamic type that doesn't have
843 GC; it can only happen when deriving from 'object' and not
844 adding any slots or instance variables. This allows
845 certain simplifications: there's no need to call
846 clear_slots(), or DECREF the dict, or clear weakrefs. */
848 /* Maybe call finalizer; exit early if resurrected */
849 if (type->tp_del) {
850 type->tp_del(self);
851 if (self->ob_refcnt > 0)
852 return;
855 /* Find the nearest base with a different tp_dealloc */
856 base = type;
857 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
858 assert(Py_SIZE(base) == 0);
859 base = base->tp_base;
860 assert(base);
863 /* Extract the type again; tp_del may have changed it */
864 type = Py_TYPE(self);
866 /* Call the base tp_dealloc() */
867 assert(basedealloc);
868 basedealloc(self);
870 /* Can't reference self beyond this point */
871 Py_DECREF(type);
873 /* Done */
874 return;
877 /* We get here only if the type has GC */
879 /* UnTrack and re-Track around the trashcan macro, alas */
880 /* See explanation at end of function for full disclosure */
881 PyObject_GC_UnTrack(self);
882 ++_PyTrash_delete_nesting;
883 Py_TRASHCAN_SAFE_BEGIN(self);
884 --_PyTrash_delete_nesting;
885 /* DO NOT restore GC tracking at this point. weakref callbacks
886 * (if any, and whether directly here or indirectly in something we
887 * call) may trigger GC, and if self is tracked at that point, it
888 * will look like trash to GC and GC will try to delete self again.
891 /* Find the nearest base with a different tp_dealloc */
892 base = type;
893 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
894 base = base->tp_base;
895 assert(base);
898 /* If we added a weaklist, we clear it. Do this *before* calling
899 the finalizer (__del__), clearing slots, or clearing the instance
900 dict. */
902 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
903 PyObject_ClearWeakRefs(self);
905 /* Maybe call finalizer; exit early if resurrected */
906 if (type->tp_del) {
907 _PyObject_GC_TRACK(self);
908 type->tp_del(self);
909 if (self->ob_refcnt > 0)
910 goto endlabel; /* resurrected */
911 else
912 _PyObject_GC_UNTRACK(self);
913 /* New weakrefs could be created during the finalizer call.
914 If this occurs, clear them out without calling their
915 finalizers since they might rely on part of the object
916 being finalized that has already been destroyed. */
917 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
918 /* Modeled after GET_WEAKREFS_LISTPTR() */
919 PyWeakReference **list = (PyWeakReference **) \
920 PyObject_GET_WEAKREFS_LISTPTR(self);
921 while (*list)
922 _PyWeakref_ClearRef(*list);
926 /* Clear slots up to the nearest base with a different tp_dealloc */
927 base = type;
928 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
929 if (Py_SIZE(base))
930 clear_slots(base, self);
931 base = base->tp_base;
932 assert(base);
935 /* If we added a dict, DECREF it */
936 if (type->tp_dictoffset && !base->tp_dictoffset) {
937 PyObject **dictptr = _PyObject_GetDictPtr(self);
938 if (dictptr != NULL) {
939 PyObject *dict = *dictptr;
940 if (dict != NULL) {
941 Py_DECREF(dict);
942 *dictptr = NULL;
947 /* Extract the type again; tp_del may have changed it */
948 type = Py_TYPE(self);
950 /* Call the base tp_dealloc(); first retrack self if
951 * basedealloc knows about gc.
953 if (PyType_IS_GC(base))
954 _PyObject_GC_TRACK(self);
955 assert(basedealloc);
956 basedealloc(self);
958 /* Can't reference self beyond this point */
959 Py_DECREF(type);
961 endlabel:
962 ++_PyTrash_delete_nesting;
963 Py_TRASHCAN_SAFE_END(self);
964 --_PyTrash_delete_nesting;
966 /* Explanation of the weirdness around the trashcan macros:
968 Q. What do the trashcan macros do?
970 A. Read the comment titled "Trashcan mechanism" in object.h.
971 For one, this explains why there must be a call to GC-untrack
972 before the trashcan begin macro. Without understanding the
973 trashcan code, the answers to the following questions don't make
974 sense.
976 Q. Why do we GC-untrack before the trashcan and then immediately
977 GC-track again afterward?
979 A. In the case that the base class is GC-aware, the base class
980 probably GC-untracks the object. If it does that using the
981 UNTRACK macro, this will crash when the object is already
982 untracked. Because we don't know what the base class does, the
983 only safe thing is to make sure the object is tracked when we
984 call the base class dealloc. But... The trashcan begin macro
985 requires that the object is *untracked* before it is called. So
986 the dance becomes:
988 GC untrack
989 trashcan begin
990 GC track
992 Q. Why did the last question say "immediately GC-track again"?
993 It's nowhere near immediately.
995 A. Because the code *used* to re-track immediately. Bad Idea.
996 self has a refcount of 0, and if gc ever gets its hands on it
997 (which can happen if any weakref callback gets invoked), it
998 looks like trash to gc too, and gc also tries to delete self
999 then. But we're already deleting self. Double dealloction is
1000 a subtle disaster.
1002 Q. Why the bizarre (net-zero) manipulation of
1003 _PyTrash_delete_nesting around the trashcan macros?
1005 A. Some base classes (e.g. list) also use the trashcan mechanism.
1006 The following scenario used to be possible:
1008 - suppose the trashcan level is one below the trashcan limit
1010 - subtype_dealloc() is called
1012 - the trashcan limit is not yet reached, so the trashcan level
1013 is incremented and the code between trashcan begin and end is
1014 executed
1016 - this destroys much of the object's contents, including its
1017 slots and __dict__
1019 - basedealloc() is called; this is really list_dealloc(), or
1020 some other type which also uses the trashcan macros
1022 - the trashcan limit is now reached, so the object is put on the
1023 trashcan's to-be-deleted-later list
1025 - basedealloc() returns
1027 - subtype_dealloc() decrefs the object's type
1029 - subtype_dealloc() returns
1031 - later, the trashcan code starts deleting the objects from its
1032 to-be-deleted-later list
1034 - subtype_dealloc() is called *AGAIN* for the same object
1036 - at the very least (if the destroyed slots and __dict__ don't
1037 cause problems) the object's type gets decref'ed a second
1038 time, which is *BAD*!!!
1040 The remedy is to make sure that if the code between trashcan
1041 begin and end in subtype_dealloc() is called, the code between
1042 trashcan begin and end in basedealloc() will also be called.
1043 This is done by decrementing the level after passing into the
1044 trashcan block, and incrementing it just before leaving the
1045 block.
1047 But now it's possible that a chain of objects consisting solely
1048 of objects whose deallocator is subtype_dealloc() will defeat
1049 the trashcan mechanism completely: the decremented level means
1050 that the effective level never reaches the limit. Therefore, we
1051 *increment* the level *before* entering the trashcan block, and
1052 matchingly decrement it after leaving. This means the trashcan
1053 code will trigger a little early, but that's no big deal.
1055 Q. Are there any live examples of code in need of all this
1056 complexity?
1058 A. Yes. See SF bug 668433 for code that crashed (when Python was
1059 compiled in debug mode) before the trashcan level manipulations
1060 were added. For more discussion, see SF patches 581742, 575073
1061 and bug 574207.
1065 static PyTypeObject *solid_base(PyTypeObject *type);
1067 /* type test with subclassing support */
1070 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1072 PyObject *mro;
1074 mro = a->tp_mro;
1075 if (mro != NULL) {
1076 /* Deal with multiple inheritance without recursion
1077 by walking the MRO tuple */
1078 Py_ssize_t i, n;
1079 assert(PyTuple_Check(mro));
1080 n = PyTuple_GET_SIZE(mro);
1081 for (i = 0; i < n; i++) {
1082 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1083 return 1;
1085 return 0;
1087 else {
1088 /* a is not completely initilized yet; follow tp_base */
1089 do {
1090 if (a == b)
1091 return 1;
1092 a = a->tp_base;
1093 } while (a != NULL);
1094 return b == &PyBaseObject_Type;
1098 /* Internal routines to do a method lookup in the type
1099 without looking in the instance dictionary
1100 (so we can't use PyObject_GetAttr) but still binding
1101 it to the instance. The arguments are the object,
1102 the method name as a C string, and the address of a
1103 static variable used to cache the interned Python string.
1105 Two variants:
1107 - lookup_maybe() returns NULL without raising an exception
1108 when the _PyType_Lookup() call fails;
1110 - lookup_method() always raises an exception upon errors.
1112 - _PyObject_LookupSpecial() exported for the benefit of other places.
1115 static PyObject *
1116 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
1118 PyObject *res;
1120 if (*attrobj == NULL) {
1121 *attrobj = PyUnicode_InternFromString(attrstr);
1122 if (*attrobj == NULL)
1123 return NULL;
1125 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
1126 if (res != NULL) {
1127 descrgetfunc f;
1128 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
1129 Py_INCREF(res);
1130 else
1131 res = f(res, self, (PyObject *)(Py_TYPE(self)));
1133 return res;
1136 static PyObject *
1137 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1139 PyObject *res = lookup_maybe(self, attrstr, attrobj);
1140 if (res == NULL && !PyErr_Occurred())
1141 PyErr_SetObject(PyExc_AttributeError, *attrobj);
1142 return res;
1145 PyObject *
1146 _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1148 return lookup_maybe(self, attrstr, attrobj);
1151 /* A variation of PyObject_CallMethod that uses lookup_method()
1152 instead of PyObject_GetAttrString(). This uses the same convention
1153 as lookup_method to cache the interned name string object. */
1155 static PyObject *
1156 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1158 va_list va;
1159 PyObject *args, *func = 0, *retval;
1160 va_start(va, format);
1162 func = lookup_maybe(o, name, nameobj);
1163 if (func == NULL) {
1164 va_end(va);
1165 if (!PyErr_Occurred())
1166 PyErr_SetObject(PyExc_AttributeError, *nameobj);
1167 return NULL;
1170 if (format && *format)
1171 args = Py_VaBuildValue(format, va);
1172 else
1173 args = PyTuple_New(0);
1175 va_end(va);
1177 if (args == NULL)
1178 return NULL;
1180 assert(PyTuple_Check(args));
1181 retval = PyObject_Call(func, args, NULL);
1183 Py_DECREF(args);
1184 Py_DECREF(func);
1186 return retval;
1189 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
1191 static PyObject *
1192 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1194 va_list va;
1195 PyObject *args, *func = 0, *retval;
1196 va_start(va, format);
1198 func = lookup_maybe(o, name, nameobj);
1199 if (func == NULL) {
1200 va_end(va);
1201 if (!PyErr_Occurred()) {
1202 Py_INCREF(Py_NotImplemented);
1203 return Py_NotImplemented;
1205 return NULL;
1208 if (format && *format)
1209 args = Py_VaBuildValue(format, va);
1210 else
1211 args = PyTuple_New(0);
1213 va_end(va);
1215 if (args == NULL)
1216 return NULL;
1218 assert(PyTuple_Check(args));
1219 retval = PyObject_Call(func, args, NULL);
1221 Py_DECREF(args);
1222 Py_DECREF(func);
1224 return retval;
1228 Method resolution order algorithm C3 described in
1229 "A Monotonic Superclass Linearization for Dylan",
1230 by Kim Barrett, Bob Cassel, Paul Haahr,
1231 David A. Moon, Keith Playford, and P. Tucker Withington.
1232 (OOPSLA 1996)
1234 Some notes about the rules implied by C3:
1236 No duplicate bases.
1237 It isn't legal to repeat a class in a list of base classes.
1239 The next three properties are the 3 constraints in "C3".
1241 Local precendece order.
1242 If A precedes B in C's MRO, then A will precede B in the MRO of all
1243 subclasses of C.
1245 Monotonicity.
1246 The MRO of a class must be an extension without reordering of the
1247 MRO of each of its superclasses.
1249 Extended Precedence Graph (EPG).
1250 Linearization is consistent if there is a path in the EPG from
1251 each class to all its successors in the linearization. See
1252 the paper for definition of EPG.
1255 static int
1256 tail_contains(PyObject *list, int whence, PyObject *o) {
1257 Py_ssize_t j, size;
1258 size = PyList_GET_SIZE(list);
1260 for (j = whence+1; j < size; j++) {
1261 if (PyList_GET_ITEM(list, j) == o)
1262 return 1;
1264 return 0;
1267 static PyObject *
1268 class_name(PyObject *cls)
1270 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1271 if (name == NULL) {
1272 PyErr_Clear();
1273 Py_XDECREF(name);
1274 name = PyObject_Repr(cls);
1276 if (name == NULL)
1277 return NULL;
1278 if (!PyUnicode_Check(name)) {
1279 Py_DECREF(name);
1280 return NULL;
1282 return name;
1285 static int
1286 check_duplicates(PyObject *list)
1288 Py_ssize_t i, j, n;
1289 /* Let's use a quadratic time algorithm,
1290 assuming that the bases lists is short.
1292 n = PyList_GET_SIZE(list);
1293 for (i = 0; i < n; i++) {
1294 PyObject *o = PyList_GET_ITEM(list, i);
1295 for (j = i + 1; j < n; j++) {
1296 if (PyList_GET_ITEM(list, j) == o) {
1297 o = class_name(o);
1298 PyErr_Format(PyExc_TypeError,
1299 "duplicate base class %.400s",
1300 o ? _PyUnicode_AsString(o) : "?");
1301 Py_XDECREF(o);
1302 return -1;
1306 return 0;
1309 /* Raise a TypeError for an MRO order disagreement.
1311 It's hard to produce a good error message. In the absence of better
1312 insight into error reporting, report the classes that were candidates
1313 to be put next into the MRO. There is some conflict between the
1314 order in which they should be put in the MRO, but it's hard to
1315 diagnose what constraint can't be satisfied.
1318 static void
1319 set_mro_error(PyObject *to_merge, int *remain)
1321 Py_ssize_t i, n, off, to_merge_size;
1322 char buf[1000];
1323 PyObject *k, *v;
1324 PyObject *set = PyDict_New();
1325 if (!set) return;
1327 to_merge_size = PyList_GET_SIZE(to_merge);
1328 for (i = 0; i < to_merge_size; i++) {
1329 PyObject *L = PyList_GET_ITEM(to_merge, i);
1330 if (remain[i] < PyList_GET_SIZE(L)) {
1331 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1332 if (PyDict_SetItem(set, c, Py_None) < 0) {
1333 Py_DECREF(set);
1334 return;
1338 n = PyDict_Size(set);
1340 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1341 consistent method resolution\norder (MRO) for bases");
1342 i = 0;
1343 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1344 PyObject *name = class_name(k);
1345 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1346 name ? _PyUnicode_AsString(name) : "?");
1347 Py_XDECREF(name);
1348 if (--n && (size_t)(off+1) < sizeof(buf)) {
1349 buf[off++] = ',';
1350 buf[off] = '\0';
1353 PyErr_SetString(PyExc_TypeError, buf);
1354 Py_DECREF(set);
1357 static int
1358 pmerge(PyObject *acc, PyObject* to_merge) {
1359 Py_ssize_t i, j, to_merge_size, empty_cnt;
1360 int *remain;
1361 int ok;
1363 to_merge_size = PyList_GET_SIZE(to_merge);
1365 /* remain stores an index into each sublist of to_merge.
1366 remain[i] is the index of the next base in to_merge[i]
1367 that is not included in acc.
1369 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1370 if (remain == NULL)
1371 return -1;
1372 for (i = 0; i < to_merge_size; i++)
1373 remain[i] = 0;
1375 again:
1376 empty_cnt = 0;
1377 for (i = 0; i < to_merge_size; i++) {
1378 PyObject *candidate;
1380 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1382 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1383 empty_cnt++;
1384 continue;
1387 /* Choose next candidate for MRO.
1389 The input sequences alone can determine the choice.
1390 If not, choose the class which appears in the MRO
1391 of the earliest direct superclass of the new class.
1394 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1395 for (j = 0; j < to_merge_size; j++) {
1396 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1397 if (tail_contains(j_lst, remain[j], candidate)) {
1398 goto skip; /* continue outer loop */
1401 ok = PyList_Append(acc, candidate);
1402 if (ok < 0) {
1403 PyMem_Free(remain);
1404 return -1;
1406 for (j = 0; j < to_merge_size; j++) {
1407 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1408 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1409 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1410 remain[j]++;
1413 goto again;
1414 skip: ;
1417 if (empty_cnt == to_merge_size) {
1418 PyMem_FREE(remain);
1419 return 0;
1421 set_mro_error(to_merge, remain);
1422 PyMem_FREE(remain);
1423 return -1;
1426 static PyObject *
1427 mro_implementation(PyTypeObject *type)
1429 Py_ssize_t i, n;
1430 int ok;
1431 PyObject *bases, *result;
1432 PyObject *to_merge, *bases_aslist;
1434 if (type->tp_dict == NULL) {
1435 if (PyType_Ready(type) < 0)
1436 return NULL;
1439 /* Find a superclass linearization that honors the constraints
1440 of the explicit lists of bases and the constraints implied by
1441 each base class.
1443 to_merge is a list of lists, where each list is a superclass
1444 linearization implied by a base class. The last element of
1445 to_merge is the declared list of bases.
1448 bases = type->tp_bases;
1449 n = PyTuple_GET_SIZE(bases);
1451 to_merge = PyList_New(n+1);
1452 if (to_merge == NULL)
1453 return NULL;
1455 for (i = 0; i < n; i++) {
1456 PyObject *base = PyTuple_GET_ITEM(bases, i);
1457 PyObject *parentMRO;
1458 parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
1459 if (parentMRO == NULL) {
1460 Py_DECREF(to_merge);
1461 return NULL;
1464 PyList_SET_ITEM(to_merge, i, parentMRO);
1467 bases_aslist = PySequence_List(bases);
1468 if (bases_aslist == NULL) {
1469 Py_DECREF(to_merge);
1470 return NULL;
1472 /* This is just a basic sanity check. */
1473 if (check_duplicates(bases_aslist) < 0) {
1474 Py_DECREF(to_merge);
1475 Py_DECREF(bases_aslist);
1476 return NULL;
1478 PyList_SET_ITEM(to_merge, n, bases_aslist);
1480 result = Py_BuildValue("[O]", (PyObject *)type);
1481 if (result == NULL) {
1482 Py_DECREF(to_merge);
1483 return NULL;
1486 ok = pmerge(result, to_merge);
1487 Py_DECREF(to_merge);
1488 if (ok < 0) {
1489 Py_DECREF(result);
1490 return NULL;
1493 return result;
1496 static PyObject *
1497 mro_external(PyObject *self)
1499 PyTypeObject *type = (PyTypeObject *)self;
1501 return mro_implementation(type);
1504 static int
1505 mro_internal(PyTypeObject *type)
1507 PyObject *mro, *result, *tuple;
1508 int checkit = 0;
1510 if (Py_TYPE(type) == &PyType_Type) {
1511 result = mro_implementation(type);
1513 else {
1514 static PyObject *mro_str;
1515 checkit = 1;
1516 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1517 if (mro == NULL)
1518 return -1;
1519 result = PyObject_CallObject(mro, NULL);
1520 Py_DECREF(mro);
1522 if (result == NULL)
1523 return -1;
1524 tuple = PySequence_Tuple(result);
1525 Py_DECREF(result);
1526 if (tuple == NULL)
1527 return -1;
1528 if (checkit) {
1529 Py_ssize_t i, len;
1530 PyObject *cls;
1531 PyTypeObject *solid;
1533 solid = solid_base(type);
1535 len = PyTuple_GET_SIZE(tuple);
1537 for (i = 0; i < len; i++) {
1538 PyTypeObject *t;
1539 cls = PyTuple_GET_ITEM(tuple, i);
1540 if (!PyType_Check(cls)) {
1541 PyErr_Format(PyExc_TypeError,
1542 "mro() returned a non-class ('%.500s')",
1543 Py_TYPE(cls)->tp_name);
1544 Py_DECREF(tuple);
1545 return -1;
1547 t = (PyTypeObject*)cls;
1548 if (!PyType_IsSubtype(solid, solid_base(t))) {
1549 PyErr_Format(PyExc_TypeError,
1550 "mro() returned base with unsuitable layout ('%.500s')",
1551 t->tp_name);
1552 Py_DECREF(tuple);
1553 return -1;
1557 type->tp_mro = tuple;
1559 type_mro_modified(type, type->tp_mro);
1560 /* corner case: the old-style super class might have been hidden
1561 from the custom MRO */
1562 type_mro_modified(type, type->tp_bases);
1564 PyType_Modified(type);
1566 return 0;
1570 /* Calculate the best base amongst multiple base classes.
1571 This is the first one that's on the path to the "solid base". */
1573 static PyTypeObject *
1574 best_base(PyObject *bases)
1576 Py_ssize_t i, n;
1577 PyTypeObject *base, *winner, *candidate, *base_i;
1578 PyObject *base_proto;
1580 assert(PyTuple_Check(bases));
1581 n = PyTuple_GET_SIZE(bases);
1582 assert(n > 0);
1583 base = NULL;
1584 winner = NULL;
1585 for (i = 0; i < n; i++) {
1586 base_proto = PyTuple_GET_ITEM(bases, i);
1587 if (!PyType_Check(base_proto)) {
1588 PyErr_SetString(
1589 PyExc_TypeError,
1590 "bases must be types");
1591 return NULL;
1593 base_i = (PyTypeObject *)base_proto;
1594 if (base_i->tp_dict == NULL) {
1595 if (PyType_Ready(base_i) < 0)
1596 return NULL;
1598 candidate = solid_base(base_i);
1599 if (winner == NULL) {
1600 winner = candidate;
1601 base = base_i;
1603 else if (PyType_IsSubtype(winner, candidate))
1605 else if (PyType_IsSubtype(candidate, winner)) {
1606 winner = candidate;
1607 base = base_i;
1609 else {
1610 PyErr_SetString(
1611 PyExc_TypeError,
1612 "multiple bases have "
1613 "instance lay-out conflict");
1614 return NULL;
1617 if (base == NULL)
1618 PyErr_SetString(PyExc_TypeError,
1619 "a new-style class can't have only classic bases");
1620 return base;
1623 static int
1624 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1626 size_t t_size = type->tp_basicsize;
1627 size_t b_size = base->tp_basicsize;
1629 assert(t_size >= b_size); /* Else type smaller than base! */
1630 if (type->tp_itemsize || base->tp_itemsize) {
1631 /* If itemsize is involved, stricter rules */
1632 return t_size != b_size ||
1633 type->tp_itemsize != base->tp_itemsize;
1635 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1636 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1637 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1638 t_size -= sizeof(PyObject *);
1639 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1640 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1641 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1642 t_size -= sizeof(PyObject *);
1644 return t_size != b_size;
1647 static PyTypeObject *
1648 solid_base(PyTypeObject *type)
1650 PyTypeObject *base;
1652 if (type->tp_base)
1653 base = solid_base(type->tp_base);
1654 else
1655 base = &PyBaseObject_Type;
1656 if (extra_ivars(type, base))
1657 return type;
1658 else
1659 return base;
1662 static void object_dealloc(PyObject *);
1663 static int object_init(PyObject *, PyObject *, PyObject *);
1664 static int update_slot(PyTypeObject *, PyObject *);
1665 static void fixup_slot_dispatchers(PyTypeObject *);
1668 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1669 * inherited from various builtin types. The builtin base usually provides
1670 * its own __dict__ descriptor, so we use that when we can.
1672 static PyTypeObject *
1673 get_builtin_base_with_dict(PyTypeObject *type)
1675 while (type->tp_base != NULL) {
1676 if (type->tp_dictoffset != 0 &&
1677 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1678 return type;
1679 type = type->tp_base;
1681 return NULL;
1684 static PyObject *
1685 get_dict_descriptor(PyTypeObject *type)
1687 static PyObject *dict_str;
1688 PyObject *descr;
1690 if (dict_str == NULL) {
1691 dict_str = PyUnicode_InternFromString("__dict__");
1692 if (dict_str == NULL)
1693 return NULL;
1695 descr = _PyType_Lookup(type, dict_str);
1696 if (descr == NULL || !PyDescr_IsData(descr))
1697 return NULL;
1699 return descr;
1702 static void
1703 raise_dict_descr_error(PyObject *obj)
1705 PyErr_Format(PyExc_TypeError,
1706 "this __dict__ descriptor does not support "
1707 "'%.200s' objects", Py_TYPE(obj)->tp_name);
1710 static PyObject *
1711 subtype_dict(PyObject *obj, void *context)
1713 PyObject **dictptr;
1714 PyObject *dict;
1715 PyTypeObject *base;
1717 base = get_builtin_base_with_dict(Py_TYPE(obj));
1718 if (base != NULL) {
1719 descrgetfunc func;
1720 PyObject *descr = get_dict_descriptor(base);
1721 if (descr == NULL) {
1722 raise_dict_descr_error(obj);
1723 return NULL;
1725 func = Py_TYPE(descr)->tp_descr_get;
1726 if (func == NULL) {
1727 raise_dict_descr_error(obj);
1728 return NULL;
1730 return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
1733 dictptr = _PyObject_GetDictPtr(obj);
1734 if (dictptr == NULL) {
1735 PyErr_SetString(PyExc_AttributeError,
1736 "This object has no __dict__");
1737 return NULL;
1739 dict = *dictptr;
1740 if (dict == NULL)
1741 *dictptr = dict = PyDict_New();
1742 Py_XINCREF(dict);
1743 return dict;
1746 static int
1747 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1749 PyObject **dictptr;
1750 PyObject *dict;
1751 PyTypeObject *base;
1753 base = get_builtin_base_with_dict(Py_TYPE(obj));
1754 if (base != NULL) {
1755 descrsetfunc func;
1756 PyObject *descr = get_dict_descriptor(base);
1757 if (descr == NULL) {
1758 raise_dict_descr_error(obj);
1759 return -1;
1761 func = Py_TYPE(descr)->tp_descr_set;
1762 if (func == NULL) {
1763 raise_dict_descr_error(obj);
1764 return -1;
1766 return func(descr, obj, value);
1769 dictptr = _PyObject_GetDictPtr(obj);
1770 if (dictptr == NULL) {
1771 PyErr_SetString(PyExc_AttributeError,
1772 "This object has no __dict__");
1773 return -1;
1775 if (value != NULL && !PyDict_Check(value)) {
1776 PyErr_Format(PyExc_TypeError,
1777 "__dict__ must be set to a dictionary, "
1778 "not a '%.200s'", Py_TYPE(value)->tp_name);
1779 return -1;
1781 dict = *dictptr;
1782 Py_XINCREF(value);
1783 *dictptr = value;
1784 Py_XDECREF(dict);
1785 return 0;
1788 static PyObject *
1789 subtype_getweakref(PyObject *obj, void *context)
1791 PyObject **weaklistptr;
1792 PyObject *result;
1794 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
1795 PyErr_SetString(PyExc_AttributeError,
1796 "This object has no __weakref__");
1797 return NULL;
1799 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1800 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1801 (size_t)(Py_TYPE(obj)->tp_basicsize));
1802 weaklistptr = (PyObject **)
1803 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
1804 if (*weaklistptr == NULL)
1805 result = Py_None;
1806 else
1807 result = *weaklistptr;
1808 Py_INCREF(result);
1809 return result;
1812 /* Three variants on the subtype_getsets list. */
1814 static PyGetSetDef subtype_getsets_full[] = {
1815 {"__dict__", subtype_dict, subtype_setdict,
1816 PyDoc_STR("dictionary for instance variables (if defined)")},
1817 {"__weakref__", subtype_getweakref, NULL,
1818 PyDoc_STR("list of weak references to the object (if defined)")},
1822 static PyGetSetDef subtype_getsets_dict_only[] = {
1823 {"__dict__", subtype_dict, subtype_setdict,
1824 PyDoc_STR("dictionary for instance variables (if defined)")},
1828 static PyGetSetDef subtype_getsets_weakref_only[] = {
1829 {"__weakref__", subtype_getweakref, NULL,
1830 PyDoc_STR("list of weak references to the object (if defined)")},
1834 static int
1835 valid_identifier(PyObject *s)
1837 if (!PyUnicode_Check(s)) {
1838 PyErr_Format(PyExc_TypeError,
1839 "__slots__ items must be strings, not '%.200s'",
1840 Py_TYPE(s)->tp_name);
1841 return 0;
1843 if (!PyUnicode_IsIdentifier(s)) {
1844 PyErr_SetString(PyExc_TypeError,
1845 "__slots__ must be identifiers");
1846 return 0;
1848 return 1;
1851 /* Forward */
1852 static int
1853 object_init(PyObject *self, PyObject *args, PyObject *kwds);
1855 static int
1856 type_init(PyObject *cls, PyObject *args, PyObject *kwds)
1858 int res;
1860 assert(args != NULL && PyTuple_Check(args));
1861 assert(kwds == NULL || PyDict_Check(kwds));
1863 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
1864 PyErr_SetString(PyExc_TypeError,
1865 "type.__init__() takes no keyword arguments");
1866 return -1;
1869 if (args != NULL && PyTuple_Check(args) &&
1870 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
1871 PyErr_SetString(PyExc_TypeError,
1872 "type.__init__() takes 1 or 3 arguments");
1873 return -1;
1876 /* Call object.__init__(self) now. */
1877 /* XXX Could call super(type, cls).__init__() but what's the point? */
1878 args = PyTuple_GetSlice(args, 0, 0);
1879 res = object_init(cls, args, NULL);
1880 Py_DECREF(args);
1881 return res;
1884 static PyObject *
1885 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1887 PyObject *name, *bases, *dict;
1888 static char *kwlist[] = {"name", "bases", "dict", 0};
1889 PyObject *slots, *tmp, *newslots;
1890 PyTypeObject *type, *base, *tmptype, *winner;
1891 PyHeapTypeObject *et;
1892 PyMemberDef *mp;
1893 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
1894 int j, may_add_dict, may_add_weak;
1896 assert(args != NULL && PyTuple_Check(args));
1897 assert(kwds == NULL || PyDict_Check(kwds));
1899 /* Special case: type(x) should return x->ob_type */
1901 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1902 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1904 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1905 PyObject *x = PyTuple_GET_ITEM(args, 0);
1906 Py_INCREF(Py_TYPE(x));
1907 return (PyObject *) Py_TYPE(x);
1910 /* SF bug 475327 -- if that didn't trigger, we need 3
1911 arguments. but PyArg_ParseTupleAndKeywords below may give
1912 a msg saying type() needs exactly 3. */
1913 if (nargs + nkwds != 3) {
1914 PyErr_SetString(PyExc_TypeError,
1915 "type() takes 1 or 3 arguments");
1916 return NULL;
1920 /* Check arguments: (name, bases, dict) */
1921 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
1922 &name,
1923 &PyTuple_Type, &bases,
1924 &PyDict_Type, &dict))
1925 return NULL;
1927 /* Determine the proper metatype to deal with this,
1928 and check for metatype conflicts while we're at it.
1929 Note that if some other metatype wins to contract,
1930 it's possible that its instances are not types. */
1931 nbases = PyTuple_GET_SIZE(bases);
1932 winner = metatype;
1933 for (i = 0; i < nbases; i++) {
1934 tmp = PyTuple_GET_ITEM(bases, i);
1935 tmptype = Py_TYPE(tmp);
1936 if (PyType_IsSubtype(winner, tmptype))
1937 continue;
1938 if (PyType_IsSubtype(tmptype, winner)) {
1939 winner = tmptype;
1940 continue;
1942 PyErr_SetString(PyExc_TypeError,
1943 "metaclass conflict: "
1944 "the metaclass of a derived class "
1945 "must be a (non-strict) subclass "
1946 "of the metaclasses of all its bases");
1947 return NULL;
1949 if (winner != metatype) {
1950 if (winner->tp_new != type_new) /* Pass it to the winner */
1951 return winner->tp_new(winner, args, kwds);
1952 metatype = winner;
1955 /* Adjust for empty tuple bases */
1956 if (nbases == 0) {
1957 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1958 if (bases == NULL)
1959 return NULL;
1960 nbases = 1;
1962 else
1963 Py_INCREF(bases);
1965 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1967 /* Calculate best base, and check that all bases are type objects */
1968 base = best_base(bases);
1969 if (base == NULL) {
1970 Py_DECREF(bases);
1971 return NULL;
1973 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1974 PyErr_Format(PyExc_TypeError,
1975 "type '%.100s' is not an acceptable base type",
1976 base->tp_name);
1977 Py_DECREF(bases);
1978 return NULL;
1981 /* Check for a __slots__ sequence variable in dict, and count it */
1982 slots = PyDict_GetItemString(dict, "__slots__");
1983 nslots = 0;
1984 add_dict = 0;
1985 add_weak = 0;
1986 may_add_dict = base->tp_dictoffset == 0;
1987 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1988 if (slots == NULL) {
1989 if (may_add_dict) {
1990 add_dict++;
1992 if (may_add_weak) {
1993 add_weak++;
1996 else {
1997 /* Have slots */
1999 /* Make it into a tuple */
2000 if (PyUnicode_Check(slots))
2001 slots = PyTuple_Pack(1, slots);
2002 else
2003 slots = PySequence_Tuple(slots);
2004 if (slots == NULL) {
2005 Py_DECREF(bases);
2006 return NULL;
2008 assert(PyTuple_Check(slots));
2010 /* Are slots allowed? */
2011 nslots = PyTuple_GET_SIZE(slots);
2012 if (nslots > 0 && base->tp_itemsize != 0) {
2013 PyErr_Format(PyExc_TypeError,
2014 "nonempty __slots__ "
2015 "not supported for subtype of '%s'",
2016 base->tp_name);
2017 bad_slots:
2018 Py_DECREF(bases);
2019 Py_DECREF(slots);
2020 return NULL;
2023 /* Check for valid slot names and two special cases */
2024 for (i = 0; i < nslots; i++) {
2025 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
2026 if (!valid_identifier(tmp))
2027 goto bad_slots;
2028 assert(PyUnicode_Check(tmp));
2029 if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
2030 if (!may_add_dict || add_dict) {
2031 PyErr_SetString(PyExc_TypeError,
2032 "__dict__ slot disallowed: "
2033 "we already got one");
2034 goto bad_slots;
2036 add_dict++;
2038 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
2039 if (!may_add_weak || add_weak) {
2040 PyErr_SetString(PyExc_TypeError,
2041 "__weakref__ slot disallowed: "
2042 "either we already got one, "
2043 "or __itemsize__ != 0");
2044 goto bad_slots;
2046 add_weak++;
2050 /* Copy slots into a list, mangle names and sort them.
2051 Sorted names are needed for __class__ assignment.
2052 Convert them back to tuple at the end.
2054 newslots = PyList_New(nslots - add_dict - add_weak);
2055 if (newslots == NULL)
2056 goto bad_slots;
2057 for (i = j = 0; i < nslots; i++) {
2058 tmp = PyTuple_GET_ITEM(slots, i);
2059 if ((add_dict &&
2060 PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
2061 (add_weak &&
2062 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
2063 continue;
2064 tmp =_Py_Mangle(name, tmp);
2065 if (!tmp)
2066 goto bad_slots;
2067 PyList_SET_ITEM(newslots, j, tmp);
2068 j++;
2070 assert(j == nslots - add_dict - add_weak);
2071 nslots = j;
2072 Py_DECREF(slots);
2073 if (PyList_Sort(newslots) == -1) {
2074 Py_DECREF(bases);
2075 Py_DECREF(newslots);
2076 return NULL;
2078 slots = PyList_AsTuple(newslots);
2079 Py_DECREF(newslots);
2080 if (slots == NULL) {
2081 Py_DECREF(bases);
2082 return NULL;
2085 /* Secondary bases may provide weakrefs or dict */
2086 if (nbases > 1 &&
2087 ((may_add_dict && !add_dict) ||
2088 (may_add_weak && !add_weak))) {
2089 for (i = 0; i < nbases; i++) {
2090 tmp = PyTuple_GET_ITEM(bases, i);
2091 if (tmp == (PyObject *)base)
2092 continue; /* Skip primary base */
2093 assert(PyType_Check(tmp));
2094 tmptype = (PyTypeObject *)tmp;
2095 if (may_add_dict && !add_dict &&
2096 tmptype->tp_dictoffset != 0)
2097 add_dict++;
2098 if (may_add_weak && !add_weak &&
2099 tmptype->tp_weaklistoffset != 0)
2100 add_weak++;
2101 if (may_add_dict && !add_dict)
2102 continue;
2103 if (may_add_weak && !add_weak)
2104 continue;
2105 /* Nothing more to check */
2106 break;
2111 /* XXX From here until type is safely allocated,
2112 "return NULL" may leak slots! */
2114 /* Allocate the type object */
2115 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
2116 if (type == NULL) {
2117 Py_XDECREF(slots);
2118 Py_DECREF(bases);
2119 return NULL;
2122 /* Keep name and slots alive in the extended type object */
2123 et = (PyHeapTypeObject *)type;
2124 Py_INCREF(name);
2125 et->ht_name = name;
2126 et->ht_slots = slots;
2128 /* Initialize tp_flags */
2129 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2130 Py_TPFLAGS_BASETYPE;
2131 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2132 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2134 /* Initialize essential fields */
2135 type->tp_as_number = &et->as_number;
2136 type->tp_as_sequence = &et->as_sequence;
2137 type->tp_as_mapping = &et->as_mapping;
2138 type->tp_as_buffer = &et->as_buffer;
2139 type->tp_name = _PyUnicode_AsString(name);
2140 if (!type->tp_name) {
2141 Py_DECREF(type);
2142 return NULL;
2145 /* Set tp_base and tp_bases */
2146 type->tp_bases = bases;
2147 Py_INCREF(base);
2148 type->tp_base = base;
2150 /* Initialize tp_dict from passed-in dict */
2151 type->tp_dict = dict = PyDict_Copy(dict);
2152 if (dict == NULL) {
2153 Py_DECREF(type);
2154 return NULL;
2157 /* Set __module__ in the dict */
2158 if (PyDict_GetItemString(dict, "__module__") == NULL) {
2159 tmp = PyEval_GetGlobals();
2160 if (tmp != NULL) {
2161 tmp = PyDict_GetItemString(tmp, "__name__");
2162 if (tmp != NULL) {
2163 if (PyDict_SetItemString(dict, "__module__",
2164 tmp) < 0)
2165 return NULL;
2170 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2171 and is a string. The __doc__ accessor will first look for tp_doc;
2172 if that fails, it will still look into __dict__.
2175 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2176 if (doc != NULL && PyUnicode_Check(doc)) {
2177 Py_ssize_t len;
2178 char *doc_str;
2179 char *tp_doc;
2181 doc_str = _PyUnicode_AsString(doc);
2182 if (doc_str == NULL) {
2183 Py_DECREF(type);
2184 return NULL;
2186 /* Silently truncate the docstring if it contains null bytes. */
2187 len = strlen(doc_str);
2188 tp_doc = (char *)PyObject_MALLOC(len + 1);
2189 if (tp_doc == NULL) {
2190 Py_DECREF(type);
2191 return NULL;
2193 memcpy(tp_doc, doc_str, len + 1);
2194 type->tp_doc = tp_doc;
2198 /* Special-case __new__: if it's a plain function,
2199 make it a static function */
2200 tmp = PyDict_GetItemString(dict, "__new__");
2201 if (tmp != NULL && PyFunction_Check(tmp)) {
2202 tmp = PyStaticMethod_New(tmp);
2203 if (tmp == NULL) {
2204 Py_DECREF(type);
2205 return NULL;
2207 PyDict_SetItemString(dict, "__new__", tmp);
2208 Py_DECREF(tmp);
2211 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2212 mp = PyHeapType_GET_MEMBERS(et);
2213 slotoffset = base->tp_basicsize;
2214 if (slots != NULL) {
2215 for (i = 0; i < nslots; i++, mp++) {
2216 mp->name = _PyUnicode_AsString(
2217 PyTuple_GET_ITEM(slots, i));
2218 mp->type = T_OBJECT_EX;
2219 mp->offset = slotoffset;
2221 /* __dict__ and __weakref__ are already filtered out */
2222 assert(strcmp(mp->name, "__dict__") != 0);
2223 assert(strcmp(mp->name, "__weakref__") != 0);
2225 slotoffset += sizeof(PyObject *);
2228 if (add_dict) {
2229 if (base->tp_itemsize)
2230 type->tp_dictoffset = -(long)sizeof(PyObject *);
2231 else
2232 type->tp_dictoffset = slotoffset;
2233 slotoffset += sizeof(PyObject *);
2235 if (add_weak) {
2236 assert(!base->tp_itemsize);
2237 type->tp_weaklistoffset = slotoffset;
2238 slotoffset += sizeof(PyObject *);
2240 type->tp_basicsize = slotoffset;
2241 type->tp_itemsize = base->tp_itemsize;
2242 type->tp_members = PyHeapType_GET_MEMBERS(et);
2244 if (type->tp_weaklistoffset && type->tp_dictoffset)
2245 type->tp_getset = subtype_getsets_full;
2246 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2247 type->tp_getset = subtype_getsets_weakref_only;
2248 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2249 type->tp_getset = subtype_getsets_dict_only;
2250 else
2251 type->tp_getset = NULL;
2253 /* Special case some slots */
2254 if (type->tp_dictoffset != 0 || nslots > 0) {
2255 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2256 type->tp_getattro = PyObject_GenericGetAttr;
2257 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2258 type->tp_setattro = PyObject_GenericSetAttr;
2260 type->tp_dealloc = subtype_dealloc;
2262 /* Enable GC unless there are really no instance variables possible */
2263 if (!(type->tp_basicsize == sizeof(PyObject) &&
2264 type->tp_itemsize == 0))
2265 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2267 /* Always override allocation strategy to use regular heap */
2268 type->tp_alloc = PyType_GenericAlloc;
2269 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2270 type->tp_free = PyObject_GC_Del;
2271 type->tp_traverse = subtype_traverse;
2272 type->tp_clear = subtype_clear;
2274 else
2275 type->tp_free = PyObject_Del;
2277 /* Initialize the rest */
2278 if (PyType_Ready(type) < 0) {
2279 Py_DECREF(type);
2280 return NULL;
2283 /* Put the proper slots in place */
2284 fixup_slot_dispatchers(type);
2286 return (PyObject *)type;
2289 /* Internal API to look for a name through the MRO.
2290 This returns a borrowed reference, and doesn't set an exception! */
2291 PyObject *
2292 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2294 Py_ssize_t i, n;
2295 PyObject *mro, *res, *base, *dict;
2296 unsigned int h;
2298 if (MCACHE_CACHEABLE_NAME(name) &&
2299 PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
2300 /* fast path */
2301 h = MCACHE_HASH_METHOD(type, name);
2302 if (method_cache[h].version == type->tp_version_tag &&
2303 method_cache[h].name == name)
2304 return method_cache[h].value;
2307 /* Look in tp_dict of types in MRO */
2308 mro = type->tp_mro;
2310 /* If mro is NULL, the type is either not yet initialized
2311 by PyType_Ready(), or already cleared by type_clear().
2312 Either way the safest thing to do is to return NULL. */
2313 if (mro == NULL)
2314 return NULL;
2316 res = NULL;
2317 assert(PyTuple_Check(mro));
2318 n = PyTuple_GET_SIZE(mro);
2319 for (i = 0; i < n; i++) {
2320 base = PyTuple_GET_ITEM(mro, i);
2321 assert(PyType_Check(base));
2322 dict = ((PyTypeObject *)base)->tp_dict;
2323 assert(dict && PyDict_Check(dict));
2324 res = PyDict_GetItem(dict, name);
2325 if (res != NULL)
2326 break;
2329 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2330 h = MCACHE_HASH_METHOD(type, name);
2331 method_cache[h].version = type->tp_version_tag;
2332 method_cache[h].value = res; /* borrowed */
2333 Py_INCREF(name);
2334 Py_DECREF(method_cache[h].name);
2335 method_cache[h].name = name;
2337 return res;
2340 /* This is similar to PyObject_GenericGetAttr(),
2341 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2342 static PyObject *
2343 type_getattro(PyTypeObject *type, PyObject *name)
2345 PyTypeObject *metatype = Py_TYPE(type);
2346 PyObject *meta_attribute, *attribute;
2347 descrgetfunc meta_get;
2349 /* Initialize this type (we'll assume the metatype is initialized) */
2350 if (type->tp_dict == NULL) {
2351 if (PyType_Ready(type) < 0)
2352 return NULL;
2355 /* No readable descriptor found yet */
2356 meta_get = NULL;
2358 /* Look for the attribute in the metatype */
2359 meta_attribute = _PyType_Lookup(metatype, name);
2361 if (meta_attribute != NULL) {
2362 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
2364 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2365 /* Data descriptors implement tp_descr_set to intercept
2366 * writes. Assume the attribute is not overridden in
2367 * type's tp_dict (and bases): call the descriptor now.
2369 return meta_get(meta_attribute, (PyObject *)type,
2370 (PyObject *)metatype);
2372 Py_INCREF(meta_attribute);
2375 /* No data descriptor found on metatype. Look in tp_dict of this
2376 * type and its bases */
2377 attribute = _PyType_Lookup(type, name);
2378 if (attribute != NULL) {
2379 /* Implement descriptor functionality, if any */
2380 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
2382 Py_XDECREF(meta_attribute);
2384 if (local_get != NULL) {
2385 /* NULL 2nd argument indicates the descriptor was
2386 * found on the target object itself (or a base) */
2387 return local_get(attribute, (PyObject *)NULL,
2388 (PyObject *)type);
2391 Py_INCREF(attribute);
2392 return attribute;
2395 /* No attribute found in local __dict__ (or bases): use the
2396 * descriptor from the metatype, if any */
2397 if (meta_get != NULL) {
2398 PyObject *res;
2399 res = meta_get(meta_attribute, (PyObject *)type,
2400 (PyObject *)metatype);
2401 Py_DECREF(meta_attribute);
2402 return res;
2405 /* If an ordinary attribute was found on the metatype, return it now */
2406 if (meta_attribute != NULL) {
2407 return meta_attribute;
2410 /* Give up */
2411 PyErr_Format(PyExc_AttributeError,
2412 "type object '%.50s' has no attribute '%U'",
2413 type->tp_name, name);
2414 return NULL;
2417 static int
2418 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2420 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2421 PyErr_Format(
2422 PyExc_TypeError,
2423 "can't set attributes of built-in/extension type '%s'",
2424 type->tp_name);
2425 return -1;
2427 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2428 return -1;
2429 return update_slot(type, name);
2432 static void
2433 type_dealloc(PyTypeObject *type)
2435 PyHeapTypeObject *et;
2437 /* Assert this is a heap-allocated type object */
2438 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2439 _PyObject_GC_UNTRACK(type);
2440 PyObject_ClearWeakRefs((PyObject *)type);
2441 et = (PyHeapTypeObject *)type;
2442 Py_XDECREF(type->tp_base);
2443 Py_XDECREF(type->tp_dict);
2444 Py_XDECREF(type->tp_bases);
2445 Py_XDECREF(type->tp_mro);
2446 Py_XDECREF(type->tp_cache);
2447 Py_XDECREF(type->tp_subclasses);
2448 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2449 * of most other objects. It's okay to cast it to char *.
2451 PyObject_Free((char *)type->tp_doc);
2452 Py_XDECREF(et->ht_name);
2453 Py_XDECREF(et->ht_slots);
2454 Py_TYPE(type)->tp_free((PyObject *)type);
2457 static PyObject *
2458 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2460 PyObject *list, *raw, *ref;
2461 Py_ssize_t i, n;
2463 list = PyList_New(0);
2464 if (list == NULL)
2465 return NULL;
2466 raw = type->tp_subclasses;
2467 if (raw == NULL)
2468 return list;
2469 assert(PyList_Check(raw));
2470 n = PyList_GET_SIZE(raw);
2471 for (i = 0; i < n; i++) {
2472 ref = PyList_GET_ITEM(raw, i);
2473 assert(PyWeakref_CheckRef(ref));
2474 ref = PyWeakref_GET_OBJECT(ref);
2475 if (ref != Py_None) {
2476 if (PyList_Append(list, ref) < 0) {
2477 Py_DECREF(list);
2478 return NULL;
2482 return list;
2485 static PyObject *
2486 type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
2488 return PyDict_New();
2491 static PyMethodDef type_methods[] = {
2492 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2493 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2494 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2495 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2496 {"__prepare__", (PyCFunction)type_prepare,
2497 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2498 PyDoc_STR("__prepare__() -> dict\n"
2499 "used to create the namespace for the class statement")},
2500 {"__instancecheck__", type___instancecheck__, METH_O,
2501 PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
2502 {"__subclasscheck__", type___subclasscheck__, METH_O,
2503 PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
2507 PyDoc_STRVAR(type_doc,
2508 "type(object) -> the object's type\n"
2509 "type(name, bases, dict) -> a new type");
2511 static int
2512 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2514 /* Because of type_is_gc(), the collector only calls this
2515 for heaptypes. */
2516 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2518 Py_VISIT(type->tp_dict);
2519 Py_VISIT(type->tp_cache);
2520 Py_VISIT(type->tp_mro);
2521 Py_VISIT(type->tp_bases);
2522 Py_VISIT(type->tp_base);
2524 /* There's no need to visit type->tp_subclasses or
2525 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2526 in cycles; tp_subclasses is a list of weak references,
2527 and slots is a tuple of strings. */
2529 return 0;
2532 static int
2533 type_clear(PyTypeObject *type)
2535 /* Because of type_is_gc(), the collector only calls this
2536 for heaptypes. */
2537 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2539 /* The only field we need to clear is tp_mro, which is part of a
2540 hard cycle (its first element is the class itself) that won't
2541 be broken otherwise (it's a tuple and tuples don't have a
2542 tp_clear handler). None of the other fields need to be
2543 cleared, and here's why:
2545 tp_dict:
2546 It is a dict, so the collector will call its tp_clear.
2548 tp_cache:
2549 Not used; if it were, it would be a dict.
2551 tp_bases, tp_base:
2552 If these are involved in a cycle, there must be at least
2553 one other, mutable object in the cycle, e.g. a base
2554 class's dict; the cycle will be broken that way.
2556 tp_subclasses:
2557 A list of weak references can't be part of a cycle; and
2558 lists have their own tp_clear.
2560 slots (in PyHeapTypeObject):
2561 A tuple of strings can't be part of a cycle.
2564 Py_CLEAR(type->tp_mro);
2566 return 0;
2569 static int
2570 type_is_gc(PyTypeObject *type)
2572 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2575 PyTypeObject PyType_Type = {
2576 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2577 "type", /* tp_name */
2578 sizeof(PyHeapTypeObject), /* tp_basicsize */
2579 sizeof(PyMemberDef), /* tp_itemsize */
2580 (destructor)type_dealloc, /* tp_dealloc */
2581 0, /* tp_print */
2582 0, /* tp_getattr */
2583 0, /* tp_setattr */
2584 0, /* tp_reserved */
2585 (reprfunc)type_repr, /* tp_repr */
2586 0, /* tp_as_number */
2587 0, /* tp_as_sequence */
2588 0, /* tp_as_mapping */
2589 0, /* tp_hash */
2590 (ternaryfunc)type_call, /* tp_call */
2591 0, /* tp_str */
2592 (getattrofunc)type_getattro, /* tp_getattro */
2593 (setattrofunc)type_setattro, /* tp_setattro */
2594 0, /* tp_as_buffer */
2595 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2596 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
2597 type_doc, /* tp_doc */
2598 (traverseproc)type_traverse, /* tp_traverse */
2599 (inquiry)type_clear, /* tp_clear */
2600 0, /* tp_richcompare */
2601 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2602 0, /* tp_iter */
2603 0, /* tp_iternext */
2604 type_methods, /* tp_methods */
2605 type_members, /* tp_members */
2606 type_getsets, /* tp_getset */
2607 0, /* tp_base */
2608 0, /* tp_dict */
2609 0, /* tp_descr_get */
2610 0, /* tp_descr_set */
2611 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2612 type_init, /* tp_init */
2613 0, /* tp_alloc */
2614 type_new, /* tp_new */
2615 PyObject_GC_Del, /* tp_free */
2616 (inquiry)type_is_gc, /* tp_is_gc */
2620 /* The base type of all types (eventually)... except itself. */
2622 /* You may wonder why object.__new__() only complains about arguments
2623 when object.__init__() is not overridden, and vice versa.
2625 Consider the use cases:
2627 1. When neither is overridden, we want to hear complaints about
2628 excess (i.e., any) arguments, since their presence could
2629 indicate there's a bug.
2631 2. When defining an Immutable type, we are likely to override only
2632 __new__(), since __init__() is called too late to initialize an
2633 Immutable object. Since __new__() defines the signature for the
2634 type, it would be a pain to have to override __init__() just to
2635 stop it from complaining about excess arguments.
2637 3. When defining a Mutable type, we are likely to override only
2638 __init__(). So here the converse reasoning applies: we don't
2639 want to have to override __new__() just to stop it from
2640 complaining.
2642 4. When __init__() is overridden, and the subclass __init__() calls
2643 object.__init__(), the latter should complain about excess
2644 arguments; ditto for __new__().
2646 Use cases 2 and 3 make it unattractive to unconditionally check for
2647 excess arguments. The best solution that addresses all four use
2648 cases is as follows: __init__() complains about excess arguments
2649 unless __new__() is overridden and __init__() is not overridden
2650 (IOW, if __init__() is overridden or __new__() is not overridden);
2651 symmetrically, __new__() complains about excess arguments unless
2652 __init__() is overridden and __new__() is not overridden
2653 (IOW, if __new__() is overridden or __init__() is not overridden).
2655 However, for backwards compatibility, this breaks too much code.
2656 Therefore, in 2.6, we'll *warn* about excess arguments when both
2657 methods are overridden; for all other cases we'll use the above
2658 rules.
2662 /* Forward */
2663 static PyObject *
2664 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2666 static int
2667 excess_args(PyObject *args, PyObject *kwds)
2669 return PyTuple_GET_SIZE(args) ||
2670 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2673 static int
2674 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2676 int err = 0;
2677 if (excess_args(args, kwds)) {
2678 PyTypeObject *type = Py_TYPE(self);
2679 if (type->tp_init != object_init &&
2680 type->tp_new != object_new)
2682 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2683 "object.__init__() takes no parameters",
2686 else if (type->tp_init != object_init ||
2687 type->tp_new == object_new)
2689 PyErr_SetString(PyExc_TypeError,
2690 "object.__init__() takes no parameters");
2691 err = -1;
2694 return err;
2697 static PyObject *
2698 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2700 int err = 0;
2701 if (excess_args(args, kwds)) {
2702 if (type->tp_new != object_new &&
2703 type->tp_init != object_init)
2705 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2706 "object.__new__() takes no parameters",
2709 else if (type->tp_new != object_new ||
2710 type->tp_init == object_init)
2712 PyErr_SetString(PyExc_TypeError,
2713 "object.__new__() takes no parameters");
2714 err = -1;
2717 if (err < 0)
2718 return NULL;
2720 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2721 static PyObject *comma = NULL;
2722 PyObject *abstract_methods = NULL;
2723 PyObject *builtins;
2724 PyObject *sorted;
2725 PyObject *sorted_methods = NULL;
2726 PyObject *joined = NULL;
2728 /* Compute ", ".join(sorted(type.__abstractmethods__))
2729 into joined. */
2730 abstract_methods = type_abstractmethods(type, NULL);
2731 if (abstract_methods == NULL)
2732 goto error;
2733 builtins = PyEval_GetBuiltins();
2734 if (builtins == NULL)
2735 goto error;
2736 sorted = PyDict_GetItemString(builtins, "sorted");
2737 if (sorted == NULL)
2738 goto error;
2739 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
2740 abstract_methods,
2741 NULL);
2742 if (sorted_methods == NULL)
2743 goto error;
2744 if (comma == NULL) {
2745 comma = PyUnicode_InternFromString(", ");
2746 if (comma == NULL)
2747 goto error;
2749 joined = PyObject_CallMethod(comma, "join",
2750 "O", sorted_methods);
2751 if (joined == NULL)
2752 goto error;
2754 PyErr_Format(PyExc_TypeError,
2755 "Can't instantiate abstract class %s "
2756 "with abstract methods %U",
2757 type->tp_name,
2758 joined);
2759 error:
2760 Py_XDECREF(joined);
2761 Py_XDECREF(sorted_methods);
2762 Py_XDECREF(abstract_methods);
2763 return NULL;
2765 return type->tp_alloc(type, 0);
2768 static void
2769 object_dealloc(PyObject *self)
2771 Py_TYPE(self)->tp_free(self);
2774 static PyObject *
2775 object_repr(PyObject *self)
2777 PyTypeObject *type;
2778 PyObject *mod, *name, *rtn;
2780 type = Py_TYPE(self);
2781 mod = type_module(type, NULL);
2782 if (mod == NULL)
2783 PyErr_Clear();
2784 else if (!PyUnicode_Check(mod)) {
2785 Py_DECREF(mod);
2786 mod = NULL;
2788 name = type_name(type, NULL);
2789 if (name == NULL)
2790 return NULL;
2791 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
2792 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
2793 else
2794 rtn = PyUnicode_FromFormat("<%s object at %p>",
2795 type->tp_name, self);
2796 Py_XDECREF(mod);
2797 Py_DECREF(name);
2798 return rtn;
2801 static PyObject *
2802 object_str(PyObject *self)
2804 unaryfunc f;
2806 f = Py_TYPE(self)->tp_repr;
2807 if (f == NULL)
2808 f = object_repr;
2809 return f(self);
2812 static PyObject *
2813 object_richcompare(PyObject *self, PyObject *other, int op)
2815 PyObject *res;
2817 switch (op) {
2819 case Py_EQ:
2820 /* Return NotImplemented instead of False, so if two
2821 objects are compared, both get a chance at the
2822 comparison. See issue #1393. */
2823 res = (self == other) ? Py_True : Py_NotImplemented;
2824 Py_INCREF(res);
2825 break;
2827 case Py_NE:
2828 /* By default, != returns the opposite of ==,
2829 unless the latter returns NotImplemented. */
2830 res = PyObject_RichCompare(self, other, Py_EQ);
2831 if (res != NULL && res != Py_NotImplemented) {
2832 int ok = PyObject_IsTrue(res);
2833 Py_DECREF(res);
2834 if (ok < 0)
2835 res = NULL;
2836 else {
2837 if (ok)
2838 res = Py_False;
2839 else
2840 res = Py_True;
2841 Py_INCREF(res);
2844 break;
2846 default:
2847 res = Py_NotImplemented;
2848 Py_INCREF(res);
2849 break;
2852 return res;
2855 static PyObject *
2856 object_get_class(PyObject *self, void *closure)
2858 Py_INCREF(Py_TYPE(self));
2859 return (PyObject *)(Py_TYPE(self));
2862 static int
2863 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2865 return a == b ||
2866 (a != NULL &&
2867 b != NULL &&
2868 a->tp_basicsize == b->tp_basicsize &&
2869 a->tp_itemsize == b->tp_itemsize &&
2870 a->tp_dictoffset == b->tp_dictoffset &&
2871 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2872 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2873 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2876 static int
2877 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2879 PyTypeObject *base = a->tp_base;
2880 Py_ssize_t size;
2881 PyObject *slots_a, *slots_b;
2883 if (base != b->tp_base)
2884 return 0;
2885 if (equiv_structs(a, base) && equiv_structs(b, base))
2886 return 1;
2887 size = base->tp_basicsize;
2888 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2889 size += sizeof(PyObject *);
2890 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2891 size += sizeof(PyObject *);
2893 /* Check slots compliance */
2894 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
2895 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
2896 if (slots_a && slots_b) {
2897 if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
2898 return 0;
2899 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
2901 return size == a->tp_basicsize && size == b->tp_basicsize;
2904 static int
2905 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
2907 PyTypeObject *newbase, *oldbase;
2909 if (newto->tp_dealloc != oldto->tp_dealloc ||
2910 newto->tp_free != oldto->tp_free)
2912 PyErr_Format(PyExc_TypeError,
2913 "%s assignment: "
2914 "'%s' deallocator differs from '%s'",
2915 attr,
2916 newto->tp_name,
2917 oldto->tp_name);
2918 return 0;
2920 newbase = newto;
2921 oldbase = oldto;
2922 while (equiv_structs(newbase, newbase->tp_base))
2923 newbase = newbase->tp_base;
2924 while (equiv_structs(oldbase, oldbase->tp_base))
2925 oldbase = oldbase->tp_base;
2926 if (newbase != oldbase &&
2927 (newbase->tp_base != oldbase->tp_base ||
2928 !same_slots_added(newbase, oldbase))) {
2929 PyErr_Format(PyExc_TypeError,
2930 "%s assignment: "
2931 "'%s' object layout differs from '%s'",
2932 attr,
2933 newto->tp_name,
2934 oldto->tp_name);
2935 return 0;
2938 return 1;
2941 static int
2942 object_set_class(PyObject *self, PyObject *value, void *closure)
2944 PyTypeObject *oldto = Py_TYPE(self);
2945 PyTypeObject *newto;
2947 if (value == NULL) {
2948 PyErr_SetString(PyExc_TypeError,
2949 "can't delete __class__ attribute");
2950 return -1;
2952 if (!PyType_Check(value)) {
2953 PyErr_Format(PyExc_TypeError,
2954 "__class__ must be set to new-style class, not '%s' object",
2955 Py_TYPE(value)->tp_name);
2956 return -1;
2958 newto = (PyTypeObject *)value;
2959 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2960 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
2962 PyErr_Format(PyExc_TypeError,
2963 "__class__ assignment: only for heap types");
2964 return -1;
2966 if (compatible_for_assignment(newto, oldto, "__class__")) {
2967 Py_INCREF(newto);
2968 Py_TYPE(self) = newto;
2969 Py_DECREF(oldto);
2970 return 0;
2972 else {
2973 return -1;
2977 static PyGetSetDef object_getsets[] = {
2978 {"__class__", object_get_class, object_set_class,
2979 PyDoc_STR("the object's class")},
2984 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2985 We fall back to helpers in copyreg for:
2986 - pickle protocols < 2
2987 - calculating the list of slot names (done only once per class)
2988 - the __newobj__ function (which is used as a token but never called)
2991 static PyObject *
2992 import_copyreg(void)
2994 static PyObject *copyreg_str;
2996 if (!copyreg_str) {
2997 copyreg_str = PyUnicode_InternFromString("copyreg");
2998 if (copyreg_str == NULL)
2999 return NULL;
3002 return PyImport_Import(copyreg_str);
3005 static PyObject *
3006 slotnames(PyObject *cls)
3008 PyObject *clsdict;
3009 PyObject *copyreg;
3010 PyObject *slotnames;
3012 if (!PyType_Check(cls)) {
3013 Py_INCREF(Py_None);
3014 return Py_None;
3017 clsdict = ((PyTypeObject *)cls)->tp_dict;
3018 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
3019 if (slotnames != NULL && PyList_Check(slotnames)) {
3020 Py_INCREF(slotnames);
3021 return slotnames;
3024 copyreg = import_copyreg();
3025 if (copyreg == NULL)
3026 return NULL;
3028 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3029 Py_DECREF(copyreg);
3030 if (slotnames != NULL &&
3031 slotnames != Py_None &&
3032 !PyList_Check(slotnames))
3034 PyErr_SetString(PyExc_TypeError,
3035 "copyreg._slotnames didn't return a list or None");
3036 Py_DECREF(slotnames);
3037 slotnames = NULL;
3040 return slotnames;
3043 static PyObject *
3044 reduce_2(PyObject *obj)
3046 PyObject *cls, *getnewargs;
3047 PyObject *args = NULL, *args2 = NULL;
3048 PyObject *getstate = NULL, *state = NULL, *names = NULL;
3049 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
3050 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
3051 Py_ssize_t i, n;
3053 cls = PyObject_GetAttrString(obj, "__class__");
3054 if (cls == NULL)
3055 return NULL;
3057 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3058 if (getnewargs != NULL) {
3059 args = PyObject_CallObject(getnewargs, NULL);
3060 Py_DECREF(getnewargs);
3061 if (args != NULL && !PyTuple_Check(args)) {
3062 PyErr_Format(PyExc_TypeError,
3063 "__getnewargs__ should return a tuple, "
3064 "not '%.200s'", Py_TYPE(args)->tp_name);
3065 goto end;
3068 else {
3069 PyErr_Clear();
3070 args = PyTuple_New(0);
3072 if (args == NULL)
3073 goto end;
3075 getstate = PyObject_GetAttrString(obj, "__getstate__");
3076 if (getstate != NULL) {
3077 state = PyObject_CallObject(getstate, NULL);
3078 Py_DECREF(getstate);
3079 if (state == NULL)
3080 goto end;
3082 else {
3083 PyErr_Clear();
3084 state = PyObject_GetAttrString(obj, "__dict__");
3085 if (state == NULL) {
3086 PyErr_Clear();
3087 state = Py_None;
3088 Py_INCREF(state);
3090 names = slotnames(cls);
3091 if (names == NULL)
3092 goto end;
3093 if (names != Py_None) {
3094 assert(PyList_Check(names));
3095 slots = PyDict_New();
3096 if (slots == NULL)
3097 goto end;
3098 n = 0;
3099 /* Can't pre-compute the list size; the list
3100 is stored on the class so accessible to other
3101 threads, which may be run by DECREF */
3102 for (i = 0; i < PyList_GET_SIZE(names); i++) {
3103 PyObject *name, *value;
3104 name = PyList_GET_ITEM(names, i);
3105 value = PyObject_GetAttr(obj, name);
3106 if (value == NULL)
3107 PyErr_Clear();
3108 else {
3109 int err = PyDict_SetItem(slots, name,
3110 value);
3111 Py_DECREF(value);
3112 if (err)
3113 goto end;
3114 n++;
3117 if (n) {
3118 state = Py_BuildValue("(NO)", state, slots);
3119 if (state == NULL)
3120 goto end;
3125 if (!PyList_Check(obj)) {
3126 listitems = Py_None;
3127 Py_INCREF(listitems);
3129 else {
3130 listitems = PyObject_GetIter(obj);
3131 if (listitems == NULL)
3132 goto end;
3135 if (!PyDict_Check(obj)) {
3136 dictitems = Py_None;
3137 Py_INCREF(dictitems);
3139 else {
3140 PyObject *items = PyObject_CallMethod(obj, "items", "");
3141 if (items == NULL)
3142 goto end;
3143 dictitems = PyObject_GetIter(items);
3144 Py_DECREF(items);
3145 if (dictitems == NULL)
3146 goto end;
3149 copyreg = import_copyreg();
3150 if (copyreg == NULL)
3151 goto end;
3152 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
3153 if (newobj == NULL)
3154 goto end;
3156 n = PyTuple_GET_SIZE(args);
3157 args2 = PyTuple_New(n+1);
3158 if (args2 == NULL)
3159 goto end;
3160 PyTuple_SET_ITEM(args2, 0, cls);
3161 cls = NULL;
3162 for (i = 0; i < n; i++) {
3163 PyObject *v = PyTuple_GET_ITEM(args, i);
3164 Py_INCREF(v);
3165 PyTuple_SET_ITEM(args2, i+1, v);
3168 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
3170 end:
3171 Py_XDECREF(cls);
3172 Py_XDECREF(args);
3173 Py_XDECREF(args2);
3174 Py_XDECREF(slots);
3175 Py_XDECREF(state);
3176 Py_XDECREF(names);
3177 Py_XDECREF(listitems);
3178 Py_XDECREF(dictitems);
3179 Py_XDECREF(copyreg);
3180 Py_XDECREF(newobj);
3181 return res;
3185 * There were two problems when object.__reduce__ and object.__reduce_ex__
3186 * were implemented in the same function:
3187 * - trying to pickle an object with a custom __reduce__ method that
3188 * fell back to object.__reduce__ in certain circumstances led to
3189 * infinite recursion at Python level and eventual RuntimeError.
3190 * - Pickling objects that lied about their type by overwriting the
3191 * __class__ descriptor could lead to infinite recursion at C level
3192 * and eventual segfault.
3194 * Because of backwards compatibility, the two methods still have to
3195 * behave in the same way, even if this is not required by the pickle
3196 * protocol. This common functionality was moved to the _common_reduce
3197 * function.
3199 static PyObject *
3200 _common_reduce(PyObject *self, int proto)
3202 PyObject *copyreg, *res;
3204 if (proto >= 2)
3205 return reduce_2(self);
3207 copyreg = import_copyreg();
3208 if (!copyreg)
3209 return NULL;
3211 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3212 Py_DECREF(copyreg);
3214 return res;
3217 static PyObject *
3218 object_reduce(PyObject *self, PyObject *args)
3220 int proto = 0;
3222 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3223 return NULL;
3225 return _common_reduce(self, proto);
3228 static PyObject *
3229 object_reduce_ex(PyObject *self, PyObject *args)
3231 PyObject *reduce, *res;
3232 int proto = 0;
3234 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3235 return NULL;
3237 reduce = PyObject_GetAttrString(self, "__reduce__");
3238 if (reduce == NULL)
3239 PyErr_Clear();
3240 else {
3241 PyObject *cls, *clsreduce, *objreduce;
3242 int override;
3243 cls = PyObject_GetAttrString(self, "__class__");
3244 if (cls == NULL) {
3245 Py_DECREF(reduce);
3246 return NULL;
3248 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3249 Py_DECREF(cls);
3250 if (clsreduce == NULL) {
3251 Py_DECREF(reduce);
3252 return NULL;
3254 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3255 "__reduce__");
3256 override = (clsreduce != objreduce);
3257 Py_DECREF(clsreduce);
3258 if (override) {
3259 res = PyObject_CallObject(reduce, NULL);
3260 Py_DECREF(reduce);
3261 return res;
3263 else
3264 Py_DECREF(reduce);
3267 return _common_reduce(self, proto);
3270 static PyObject *
3271 object_subclasshook(PyObject *cls, PyObject *args)
3273 Py_INCREF(Py_NotImplemented);
3274 return Py_NotImplemented;
3277 PyDoc_STRVAR(object_subclasshook_doc,
3278 "Abstract classes can override this to customize issubclass().\n"
3279 "\n"
3280 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3281 "It should return True, False or NotImplemented. If it returns\n"
3282 "NotImplemented, the normal algorithm is used. Otherwise, it\n"
3283 "overrides the normal algorithm (and the outcome is cached).\n");
3286 from PEP 3101, this code implements:
3288 class object:
3289 def __format__(self, format_spec):
3290 return format(str(self), format_spec)
3292 static PyObject *
3293 object_format(PyObject *self, PyObject *args)
3295 PyObject *format_spec;
3296 PyObject *self_as_str = NULL;
3297 PyObject *result = NULL;
3298 PyObject *format_meth = NULL;
3300 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
3301 return NULL;
3303 self_as_str = PyObject_Str(self);
3304 if (self_as_str != NULL) {
3305 /* find the format function */
3306 format_meth = PyObject_GetAttrString(self_as_str, "__format__");
3307 if (format_meth != NULL) {
3308 /* and call it */
3309 result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
3313 Py_XDECREF(self_as_str);
3314 Py_XDECREF(format_meth);
3316 return result;
3319 static PyObject *
3320 object_sizeof(PyObject *self, PyObject *args)
3322 Py_ssize_t res, isize;
3324 res = 0;
3325 isize = self->ob_type->tp_itemsize;
3326 if (isize > 0)
3327 res = Py_SIZE(self->ob_type) * isize;
3328 res += self->ob_type->tp_basicsize;
3330 return PyLong_FromSsize_t(res);
3333 static PyMethodDef object_methods[] = {
3334 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3335 PyDoc_STR("helper for pickle")},
3336 {"__reduce__", object_reduce, METH_VARARGS,
3337 PyDoc_STR("helper for pickle")},
3338 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3339 object_subclasshook_doc},
3340 {"__format__", object_format, METH_VARARGS,
3341 PyDoc_STR("default object formatter")},
3342 {"__sizeof__", object_sizeof, METH_NOARGS,
3343 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
3348 PyTypeObject PyBaseObject_Type = {
3349 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3350 "object", /* tp_name */
3351 sizeof(PyObject), /* tp_basicsize */
3352 0, /* tp_itemsize */
3353 object_dealloc, /* tp_dealloc */
3354 0, /* tp_print */
3355 0, /* tp_getattr */
3356 0, /* tp_setattr */
3357 0, /* tp_reserved */
3358 object_repr, /* tp_repr */
3359 0, /* tp_as_number */
3360 0, /* tp_as_sequence */
3361 0, /* tp_as_mapping */
3362 (hashfunc)_Py_HashPointer, /* tp_hash */
3363 0, /* tp_call */
3364 object_str, /* tp_str */
3365 PyObject_GenericGetAttr, /* tp_getattro */
3366 PyObject_GenericSetAttr, /* tp_setattro */
3367 0, /* tp_as_buffer */
3368 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3369 PyDoc_STR("The most base type"), /* tp_doc */
3370 0, /* tp_traverse */
3371 0, /* tp_clear */
3372 object_richcompare, /* tp_richcompare */
3373 0, /* tp_weaklistoffset */
3374 0, /* tp_iter */
3375 0, /* tp_iternext */
3376 object_methods, /* tp_methods */
3377 0, /* tp_members */
3378 object_getsets, /* tp_getset */
3379 0, /* tp_base */
3380 0, /* tp_dict */
3381 0, /* tp_descr_get */
3382 0, /* tp_descr_set */
3383 0, /* tp_dictoffset */
3384 object_init, /* tp_init */
3385 PyType_GenericAlloc, /* tp_alloc */
3386 object_new, /* tp_new */
3387 PyObject_Del, /* tp_free */
3391 /* Add the methods from tp_methods to the __dict__ in a type object */
3393 static int
3394 add_methods(PyTypeObject *type, PyMethodDef *meth)
3396 PyObject *dict = type->tp_dict;
3398 for (; meth->ml_name != NULL; meth++) {
3399 PyObject *descr;
3400 if (PyDict_GetItemString(dict, meth->ml_name) &&
3401 !(meth->ml_flags & METH_COEXIST))
3402 continue;
3403 if (meth->ml_flags & METH_CLASS) {
3404 if (meth->ml_flags & METH_STATIC) {
3405 PyErr_SetString(PyExc_ValueError,
3406 "method cannot be both class and static");
3407 return -1;
3409 descr = PyDescr_NewClassMethod(type, meth);
3411 else if (meth->ml_flags & METH_STATIC) {
3412 PyObject *cfunc = PyCFunction_New(meth, NULL);
3413 if (cfunc == NULL)
3414 return -1;
3415 descr = PyStaticMethod_New(cfunc);
3416 Py_DECREF(cfunc);
3418 else {
3419 descr = PyDescr_NewMethod(type, meth);
3421 if (descr == NULL)
3422 return -1;
3423 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
3424 return -1;
3425 Py_DECREF(descr);
3427 return 0;
3430 static int
3431 add_members(PyTypeObject *type, PyMemberDef *memb)
3433 PyObject *dict = type->tp_dict;
3435 for (; memb->name != NULL; memb++) {
3436 PyObject *descr;
3437 if (PyDict_GetItemString(dict, memb->name))
3438 continue;
3439 descr = PyDescr_NewMember(type, memb);
3440 if (descr == NULL)
3441 return -1;
3442 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3443 return -1;
3444 Py_DECREF(descr);
3446 return 0;
3449 static int
3450 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
3452 PyObject *dict = type->tp_dict;
3454 for (; gsp->name != NULL; gsp++) {
3455 PyObject *descr;
3456 if (PyDict_GetItemString(dict, gsp->name))
3457 continue;
3458 descr = PyDescr_NewGetSet(type, gsp);
3460 if (descr == NULL)
3461 return -1;
3462 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3463 return -1;
3464 Py_DECREF(descr);
3466 return 0;
3469 static void
3470 inherit_special(PyTypeObject *type, PyTypeObject *base)
3472 Py_ssize_t oldsize, newsize;
3474 /* Copying basicsize is connected to the GC flags */
3475 oldsize = base->tp_basicsize;
3476 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3477 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3478 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3479 (!type->tp_traverse && !type->tp_clear)) {
3480 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
3481 if (type->tp_traverse == NULL)
3482 type->tp_traverse = base->tp_traverse;
3483 if (type->tp_clear == NULL)
3484 type->tp_clear = base->tp_clear;
3487 /* The condition below could use some explanation.
3488 It appears that tp_new is not inherited for static types
3489 whose base class is 'object'; this seems to be a precaution
3490 so that old extension types don't suddenly become
3491 callable (object.__new__ wouldn't insure the invariants
3492 that the extension type's own factory function ensures).
3493 Heap types, of course, are under our control, so they do
3494 inherit tp_new; static extension types that specify some
3495 other built-in type as the default are considered
3496 new-style-aware so they also inherit object.__new__. */
3497 if (base != &PyBaseObject_Type ||
3498 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3499 if (type->tp_new == NULL)
3500 type->tp_new = base->tp_new;
3503 type->tp_basicsize = newsize;
3505 /* Copy other non-function slots */
3507 #undef COPYVAL
3508 #define COPYVAL(SLOT) \
3509 if (type->SLOT == 0) type->SLOT = base->SLOT
3511 COPYVAL(tp_itemsize);
3512 COPYVAL(tp_weaklistoffset);
3513 COPYVAL(tp_dictoffset);
3515 /* Setup fast subclass flags */
3516 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3517 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3518 else if (PyType_IsSubtype(base, &PyType_Type))
3519 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3520 else if (PyType_IsSubtype(base, &PyLong_Type))
3521 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
3522 else if (PyType_IsSubtype(base, &PyBytes_Type))
3523 type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
3524 else if (PyType_IsSubtype(base, &PyUnicode_Type))
3525 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3526 else if (PyType_IsSubtype(base, &PyTuple_Type))
3527 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3528 else if (PyType_IsSubtype(base, &PyList_Type))
3529 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3530 else if (PyType_IsSubtype(base, &PyDict_Type))
3531 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
3534 static char *hash_name_op[] = {
3535 "__eq__",
3536 "__hash__",
3537 NULL
3540 static int
3541 overrides_hash(PyTypeObject *type)
3543 char **p;
3544 PyObject *dict = type->tp_dict;
3546 assert(dict != NULL);
3547 for (p = hash_name_op; *p; p++) {
3548 if (PyDict_GetItemString(dict, *p) != NULL)
3549 return 1;
3551 return 0;
3554 static void
3555 inherit_slots(PyTypeObject *type, PyTypeObject *base)
3557 PyTypeObject *basebase;
3559 #undef SLOTDEFINED
3560 #undef COPYSLOT
3561 #undef COPYNUM
3562 #undef COPYSEQ
3563 #undef COPYMAP
3564 #undef COPYBUF
3566 #define SLOTDEFINED(SLOT) \
3567 (base->SLOT != 0 && \
3568 (basebase == NULL || base->SLOT != basebase->SLOT))
3570 #define COPYSLOT(SLOT) \
3571 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3573 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3574 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3575 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3576 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3578 /* This won't inherit indirect slots (from tp_as_number etc.)
3579 if type doesn't provide the space. */
3581 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3582 basebase = base->tp_base;
3583 if (basebase->tp_as_number == NULL)
3584 basebase = NULL;
3585 COPYNUM(nb_add);
3586 COPYNUM(nb_subtract);
3587 COPYNUM(nb_multiply);
3588 COPYNUM(nb_remainder);
3589 COPYNUM(nb_divmod);
3590 COPYNUM(nb_power);
3591 COPYNUM(nb_negative);
3592 COPYNUM(nb_positive);
3593 COPYNUM(nb_absolute);
3594 COPYNUM(nb_bool);
3595 COPYNUM(nb_invert);
3596 COPYNUM(nb_lshift);
3597 COPYNUM(nb_rshift);
3598 COPYNUM(nb_and);
3599 COPYNUM(nb_xor);
3600 COPYNUM(nb_or);
3601 COPYNUM(nb_int);
3602 COPYNUM(nb_float);
3603 COPYNUM(nb_inplace_add);
3604 COPYNUM(nb_inplace_subtract);
3605 COPYNUM(nb_inplace_multiply);
3606 COPYNUM(nb_inplace_remainder);
3607 COPYNUM(nb_inplace_power);
3608 COPYNUM(nb_inplace_lshift);
3609 COPYNUM(nb_inplace_rshift);
3610 COPYNUM(nb_inplace_and);
3611 COPYNUM(nb_inplace_xor);
3612 COPYNUM(nb_inplace_or);
3613 COPYNUM(nb_true_divide);
3614 COPYNUM(nb_floor_divide);
3615 COPYNUM(nb_inplace_true_divide);
3616 COPYNUM(nb_inplace_floor_divide);
3617 COPYNUM(nb_index);
3620 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3621 basebase = base->tp_base;
3622 if (basebase->tp_as_sequence == NULL)
3623 basebase = NULL;
3624 COPYSEQ(sq_length);
3625 COPYSEQ(sq_concat);
3626 COPYSEQ(sq_repeat);
3627 COPYSEQ(sq_item);
3628 COPYSEQ(sq_ass_item);
3629 COPYSEQ(sq_contains);
3630 COPYSEQ(sq_inplace_concat);
3631 COPYSEQ(sq_inplace_repeat);
3634 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3635 basebase = base->tp_base;
3636 if (basebase->tp_as_mapping == NULL)
3637 basebase = NULL;
3638 COPYMAP(mp_length);
3639 COPYMAP(mp_subscript);
3640 COPYMAP(mp_ass_subscript);
3643 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3644 basebase = base->tp_base;
3645 if (basebase->tp_as_buffer == NULL)
3646 basebase = NULL;
3647 COPYBUF(bf_getbuffer);
3648 COPYBUF(bf_releasebuffer);
3651 basebase = base->tp_base;
3653 COPYSLOT(tp_dealloc);
3654 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3655 type->tp_getattr = base->tp_getattr;
3656 type->tp_getattro = base->tp_getattro;
3658 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3659 type->tp_setattr = base->tp_setattr;
3660 type->tp_setattro = base->tp_setattro;
3662 /* tp_reserved is ignored */
3663 COPYSLOT(tp_repr);
3664 /* tp_hash see tp_richcompare */
3665 COPYSLOT(tp_call);
3666 COPYSLOT(tp_str);
3668 /* Copy comparison-related slots only when
3669 not overriding them anywhere */
3670 if (type->tp_richcompare == NULL &&
3671 type->tp_hash == NULL &&
3672 !overrides_hash(type))
3674 type->tp_richcompare = base->tp_richcompare;
3675 type->tp_hash = base->tp_hash;
3679 COPYSLOT(tp_iter);
3680 COPYSLOT(tp_iternext);
3683 COPYSLOT(tp_descr_get);
3684 COPYSLOT(tp_descr_set);
3685 COPYSLOT(tp_dictoffset);
3686 COPYSLOT(tp_init);
3687 COPYSLOT(tp_alloc);
3688 COPYSLOT(tp_is_gc);
3689 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3690 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3691 /* They agree about gc. */
3692 COPYSLOT(tp_free);
3694 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3695 type->tp_free == NULL &&
3696 base->tp_free == PyObject_Free) {
3697 /* A bit of magic to plug in the correct default
3698 * tp_free function when a derived class adds gc,
3699 * didn't define tp_free, and the base uses the
3700 * default non-gc tp_free.
3702 type->tp_free = PyObject_GC_Del;
3704 /* else they didn't agree about gc, and there isn't something
3705 * obvious to be done -- the type is on its own.
3710 static int add_operators(PyTypeObject *);
3713 PyType_Ready(PyTypeObject *type)
3715 PyObject *dict, *bases;
3716 PyTypeObject *base;
3717 Py_ssize_t i, n;
3719 if (type->tp_flags & Py_TPFLAGS_READY) {
3720 assert(type->tp_dict != NULL);
3721 return 0;
3723 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3725 type->tp_flags |= Py_TPFLAGS_READYING;
3727 #ifdef Py_TRACE_REFS
3728 /* PyType_Ready is the closest thing we have to a choke point
3729 * for type objects, so is the best place I can think of to try
3730 * to get type objects into the doubly-linked list of all objects.
3731 * Still, not all type objects go thru PyType_Ready.
3733 _Py_AddToAllObjects((PyObject *)type, 0);
3734 #endif
3736 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3737 base = type->tp_base;
3738 if (base == NULL && type != &PyBaseObject_Type) {
3739 base = type->tp_base = &PyBaseObject_Type;
3740 Py_INCREF(base);
3743 /* Now the only way base can still be NULL is if type is
3744 * &PyBaseObject_Type.
3747 /* Initialize the base class */
3748 if (base != NULL && base->tp_dict == NULL) {
3749 if (PyType_Ready(base) < 0)
3750 goto error;
3753 /* Initialize ob_type if NULL. This means extensions that want to be
3754 compilable separately on Windows can call PyType_Ready() instead of
3755 initializing the ob_type field of their type objects. */
3756 /* The test for base != NULL is really unnecessary, since base is only
3757 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3758 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3759 know that. */
3760 if (Py_TYPE(type) == NULL && base != NULL)
3761 Py_TYPE(type) = Py_TYPE(base);
3763 /* Initialize tp_bases */
3764 bases = type->tp_bases;
3765 if (bases == NULL) {
3766 if (base == NULL)
3767 bases = PyTuple_New(0);
3768 else
3769 bases = PyTuple_Pack(1, base);
3770 if (bases == NULL)
3771 goto error;
3772 type->tp_bases = bases;
3775 /* Initialize tp_dict */
3776 dict = type->tp_dict;
3777 if (dict == NULL) {
3778 dict = PyDict_New();
3779 if (dict == NULL)
3780 goto error;
3781 type->tp_dict = dict;
3784 /* Add type-specific descriptors to tp_dict */
3785 if (add_operators(type) < 0)
3786 goto error;
3787 if (type->tp_methods != NULL) {
3788 if (add_methods(type, type->tp_methods) < 0)
3789 goto error;
3791 if (type->tp_members != NULL) {
3792 if (add_members(type, type->tp_members) < 0)
3793 goto error;
3795 if (type->tp_getset != NULL) {
3796 if (add_getset(type, type->tp_getset) < 0)
3797 goto error;
3800 /* Calculate method resolution order */
3801 if (mro_internal(type) < 0) {
3802 goto error;
3805 /* Inherit special flags from dominant base */
3806 if (type->tp_base != NULL)
3807 inherit_special(type, type->tp_base);
3809 /* Initialize tp_dict properly */
3810 bases = type->tp_mro;
3811 assert(bases != NULL);
3812 assert(PyTuple_Check(bases));
3813 n = PyTuple_GET_SIZE(bases);
3814 for (i = 1; i < n; i++) {
3815 PyObject *b = PyTuple_GET_ITEM(bases, i);
3816 if (PyType_Check(b))
3817 inherit_slots(type, (PyTypeObject *)b);
3820 /* Sanity check for tp_free. */
3821 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3822 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3823 /* This base class needs to call tp_free, but doesn't have
3824 * one, or its tp_free is for non-gc'ed objects.
3826 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3827 "gc and is a base type but has inappropriate "
3828 "tp_free slot",
3829 type->tp_name);
3830 goto error;
3833 /* if the type dictionary doesn't contain a __doc__, set it from
3834 the tp_doc slot.
3836 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3837 if (type->tp_doc != NULL) {
3838 PyObject *doc = PyUnicode_FromString(type->tp_doc);
3839 if (doc == NULL)
3840 goto error;
3841 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3842 Py_DECREF(doc);
3843 } else {
3844 PyDict_SetItemString(type->tp_dict,
3845 "__doc__", Py_None);
3849 /* Hack for tp_hash and __hash__.
3850 If after all that, tp_hash is still NULL, and __hash__ is not in
3851 tp_dict, set tp_hash to PyObject_HashNotImplemented and
3852 tp_dict['__hash__'] equal to None.
3853 This signals that __hash__ is not inherited.
3855 if (type->tp_hash == NULL) {
3856 if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
3857 if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
3858 goto error;
3859 type->tp_hash = PyObject_HashNotImplemented;
3863 /* Some more special stuff */
3864 base = type->tp_base;
3865 if (base != NULL) {
3866 if (type->tp_as_number == NULL)
3867 type->tp_as_number = base->tp_as_number;
3868 if (type->tp_as_sequence == NULL)
3869 type->tp_as_sequence = base->tp_as_sequence;
3870 if (type->tp_as_mapping == NULL)
3871 type->tp_as_mapping = base->tp_as_mapping;
3872 if (type->tp_as_buffer == NULL)
3873 type->tp_as_buffer = base->tp_as_buffer;
3876 /* Link into each base class's list of subclasses */
3877 bases = type->tp_bases;
3878 n = PyTuple_GET_SIZE(bases);
3879 for (i = 0; i < n; i++) {
3880 PyObject *b = PyTuple_GET_ITEM(bases, i);
3881 if (PyType_Check(b) &&
3882 add_subclass((PyTypeObject *)b, type) < 0)
3883 goto error;
3886 /* Warn for a type that implements tp_compare (now known as
3887 tp_reserved) but not tp_richcompare. */
3888 if (type->tp_reserved && !type->tp_richcompare) {
3889 int error;
3890 char msg[240];
3891 PyOS_snprintf(msg, sizeof(msg),
3892 "Type %.100s defines tp_reserved (formerly "
3893 "tp_compare) but not tp_richcompare. "
3894 "Comparisons may not behave as intended.",
3895 type->tp_name);
3896 error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1);
3897 if (error == -1)
3898 goto error;
3901 /* All done -- set the ready flag */
3902 assert(type->tp_dict != NULL);
3903 type->tp_flags =
3904 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3905 return 0;
3907 error:
3908 type->tp_flags &= ~Py_TPFLAGS_READYING;
3909 return -1;
3912 static int
3913 add_subclass(PyTypeObject *base, PyTypeObject *type)
3915 Py_ssize_t i;
3916 int result;
3917 PyObject *list, *ref, *newobj;
3919 list = base->tp_subclasses;
3920 if (list == NULL) {
3921 base->tp_subclasses = list = PyList_New(0);
3922 if (list == NULL)
3923 return -1;
3925 assert(PyList_Check(list));
3926 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
3927 i = PyList_GET_SIZE(list);
3928 while (--i >= 0) {
3929 ref = PyList_GET_ITEM(list, i);
3930 assert(PyWeakref_CheckRef(ref));
3931 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3932 return PyList_SetItem(list, i, newobj);
3934 result = PyList_Append(list, newobj);
3935 Py_DECREF(newobj);
3936 return result;
3939 static void
3940 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3942 Py_ssize_t i;
3943 PyObject *list, *ref;
3945 list = base->tp_subclasses;
3946 if (list == NULL) {
3947 return;
3949 assert(PyList_Check(list));
3950 i = PyList_GET_SIZE(list);
3951 while (--i >= 0) {
3952 ref = PyList_GET_ITEM(list, i);
3953 assert(PyWeakref_CheckRef(ref));
3954 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3955 /* this can't fail, right? */
3956 PySequence_DelItem(list, i);
3957 return;
3962 static int
3963 check_num_args(PyObject *ob, int n)
3965 if (!PyTuple_CheckExact(ob)) {
3966 PyErr_SetString(PyExc_SystemError,
3967 "PyArg_UnpackTuple() argument list is not a tuple");
3968 return 0;
3970 if (n == PyTuple_GET_SIZE(ob))
3971 return 1;
3972 PyErr_Format(
3973 PyExc_TypeError,
3974 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
3975 return 0;
3978 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3980 /* There's a wrapper *function* for each distinct function typedef used
3981 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3982 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3983 Most tables have only one entry; the tables for binary operators have two
3984 entries, one regular and one with reversed arguments. */
3986 static PyObject *
3987 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
3989 lenfunc func = (lenfunc)wrapped;
3990 Py_ssize_t res;
3992 if (!check_num_args(args, 0))
3993 return NULL;
3994 res = (*func)(self);
3995 if (res == -1 && PyErr_Occurred())
3996 return NULL;
3997 return PyLong_FromLong((long)res);
4000 static PyObject *
4001 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
4003 inquiry func = (inquiry)wrapped;
4004 int res;
4006 if (!check_num_args(args, 0))
4007 return NULL;
4008 res = (*func)(self);
4009 if (res == -1 && PyErr_Occurred())
4010 return NULL;
4011 return PyBool_FromLong((long)res);
4014 static PyObject *
4015 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4017 binaryfunc func = (binaryfunc)wrapped;
4018 PyObject *other;
4020 if (!check_num_args(args, 1))
4021 return NULL;
4022 other = PyTuple_GET_ITEM(args, 0);
4023 return (*func)(self, other);
4026 static PyObject *
4027 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4029 binaryfunc func = (binaryfunc)wrapped;
4030 PyObject *other;
4032 if (!check_num_args(args, 1))
4033 return NULL;
4034 other = PyTuple_GET_ITEM(args, 0);
4035 return (*func)(self, other);
4038 static PyObject *
4039 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4041 binaryfunc func = (binaryfunc)wrapped;
4042 PyObject *other;
4044 if (!check_num_args(args, 1))
4045 return NULL;
4046 other = PyTuple_GET_ITEM(args, 0);
4047 if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
4048 Py_INCREF(Py_NotImplemented);
4049 return Py_NotImplemented;
4051 return (*func)(other, self);
4054 static PyObject *
4055 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4057 ternaryfunc func = (ternaryfunc)wrapped;
4058 PyObject *other;
4059 PyObject *third = Py_None;
4061 /* Note: This wrapper only works for __pow__() */
4063 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4064 return NULL;
4065 return (*func)(self, other, third);
4068 static PyObject *
4069 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4071 ternaryfunc func = (ternaryfunc)wrapped;
4072 PyObject *other;
4073 PyObject *third = Py_None;
4075 /* Note: This wrapper only works for __pow__() */
4077 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4078 return NULL;
4079 return (*func)(other, self, third);
4082 static PyObject *
4083 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4085 unaryfunc func = (unaryfunc)wrapped;
4087 if (!check_num_args(args, 0))
4088 return NULL;
4089 return (*func)(self);
4092 static PyObject *
4093 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
4095 ssizeargfunc func = (ssizeargfunc)wrapped;
4096 PyObject* o;
4097 Py_ssize_t i;
4099 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4100 return NULL;
4101 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
4102 if (i == -1 && PyErr_Occurred())
4103 return NULL;
4104 return (*func)(self, i);
4107 static Py_ssize_t
4108 getindex(PyObject *self, PyObject *arg)
4110 Py_ssize_t i;
4112 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
4113 if (i == -1 && PyErr_Occurred())
4114 return -1;
4115 if (i < 0) {
4116 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
4117 if (sq && sq->sq_length) {
4118 Py_ssize_t n = (*sq->sq_length)(self);
4119 if (n < 0)
4120 return -1;
4121 i += n;
4124 return i;
4127 static PyObject *
4128 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4130 ssizeargfunc func = (ssizeargfunc)wrapped;
4131 PyObject *arg;
4132 Py_ssize_t i;
4134 if (PyTuple_GET_SIZE(args) == 1) {
4135 arg = PyTuple_GET_ITEM(args, 0);
4136 i = getindex(self, arg);
4137 if (i == -1 && PyErr_Occurred())
4138 return NULL;
4139 return (*func)(self, i);
4141 check_num_args(args, 1);
4142 assert(PyErr_Occurred());
4143 return NULL;
4146 static PyObject *
4147 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
4149 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4150 Py_ssize_t i;
4151 int res;
4152 PyObject *arg, *value;
4154 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
4155 return NULL;
4156 i = getindex(self, arg);
4157 if (i == -1 && PyErr_Occurred())
4158 return NULL;
4159 res = (*func)(self, i, value);
4160 if (res == -1 && PyErr_Occurred())
4161 return NULL;
4162 Py_INCREF(Py_None);
4163 return Py_None;
4166 static PyObject *
4167 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
4169 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4170 Py_ssize_t i;
4171 int res;
4172 PyObject *arg;
4174 if (!check_num_args(args, 1))
4175 return NULL;
4176 arg = PyTuple_GET_ITEM(args, 0);
4177 i = getindex(self, arg);
4178 if (i == -1 && PyErr_Occurred())
4179 return NULL;
4180 res = (*func)(self, i, NULL);
4181 if (res == -1 && PyErr_Occurred())
4182 return NULL;
4183 Py_INCREF(Py_None);
4184 return Py_None;
4187 /* XXX objobjproc is a misnomer; should be objargpred */
4188 static PyObject *
4189 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4191 objobjproc func = (objobjproc)wrapped;
4192 int res;
4193 PyObject *value;
4195 if (!check_num_args(args, 1))
4196 return NULL;
4197 value = PyTuple_GET_ITEM(args, 0);
4198 res = (*func)(self, value);
4199 if (res == -1 && PyErr_Occurred())
4200 return NULL;
4201 else
4202 return PyBool_FromLong(res);
4205 static PyObject *
4206 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4208 objobjargproc func = (objobjargproc)wrapped;
4209 int res;
4210 PyObject *key, *value;
4212 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
4213 return NULL;
4214 res = (*func)(self, key, value);
4215 if (res == -1 && PyErr_Occurred())
4216 return NULL;
4217 Py_INCREF(Py_None);
4218 return Py_None;
4221 static PyObject *
4222 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4224 objobjargproc func = (objobjargproc)wrapped;
4225 int res;
4226 PyObject *key;
4228 if (!check_num_args(args, 1))
4229 return NULL;
4230 key = PyTuple_GET_ITEM(args, 0);
4231 res = (*func)(self, key, NULL);
4232 if (res == -1 && PyErr_Occurred())
4233 return NULL;
4234 Py_INCREF(Py_None);
4235 return Py_None;
4238 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4239 This is called the Carlo Verre hack after its discoverer. */
4240 static int
4241 hackcheck(PyObject *self, setattrofunc func, char *what)
4243 PyTypeObject *type = Py_TYPE(self);
4244 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4245 type = type->tp_base;
4246 /* If type is NULL now, this is a really weird type.
4247 In the spirit of backwards compatibility (?), just shut up. */
4248 if (type && type->tp_setattro != func) {
4249 PyErr_Format(PyExc_TypeError,
4250 "can't apply this %s to %s object",
4251 what,
4252 type->tp_name);
4253 return 0;
4255 return 1;
4258 static PyObject *
4259 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4261 setattrofunc func = (setattrofunc)wrapped;
4262 int res;
4263 PyObject *name, *value;
4265 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
4266 return NULL;
4267 if (!hackcheck(self, func, "__setattr__"))
4268 return NULL;
4269 res = (*func)(self, name, value);
4270 if (res < 0)
4271 return NULL;
4272 Py_INCREF(Py_None);
4273 return Py_None;
4276 static PyObject *
4277 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4279 setattrofunc func = (setattrofunc)wrapped;
4280 int res;
4281 PyObject *name;
4283 if (!check_num_args(args, 1))
4284 return NULL;
4285 name = PyTuple_GET_ITEM(args, 0);
4286 if (!hackcheck(self, func, "__delattr__"))
4287 return NULL;
4288 res = (*func)(self, name, NULL);
4289 if (res < 0)
4290 return NULL;
4291 Py_INCREF(Py_None);
4292 return Py_None;
4295 static PyObject *
4296 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4298 hashfunc func = (hashfunc)wrapped;
4299 long res;
4301 if (!check_num_args(args, 0))
4302 return NULL;
4303 res = (*func)(self);
4304 if (res == -1 && PyErr_Occurred())
4305 return NULL;
4306 return PyLong_FromLong(res);
4309 static PyObject *
4310 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4312 ternaryfunc func = (ternaryfunc)wrapped;
4314 return (*func)(self, args, kwds);
4317 static PyObject *
4318 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4320 richcmpfunc func = (richcmpfunc)wrapped;
4321 PyObject *other;
4323 if (!check_num_args(args, 1))
4324 return NULL;
4325 other = PyTuple_GET_ITEM(args, 0);
4326 return (*func)(self, other, op);
4329 #undef RICHCMP_WRAPPER
4330 #define RICHCMP_WRAPPER(NAME, OP) \
4331 static PyObject * \
4332 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4334 return wrap_richcmpfunc(self, args, wrapped, OP); \
4337 RICHCMP_WRAPPER(lt, Py_LT)
4338 RICHCMP_WRAPPER(le, Py_LE)
4339 RICHCMP_WRAPPER(eq, Py_EQ)
4340 RICHCMP_WRAPPER(ne, Py_NE)
4341 RICHCMP_WRAPPER(gt, Py_GT)
4342 RICHCMP_WRAPPER(ge, Py_GE)
4344 static PyObject *
4345 wrap_next(PyObject *self, PyObject *args, void *wrapped)
4347 unaryfunc func = (unaryfunc)wrapped;
4348 PyObject *res;
4350 if (!check_num_args(args, 0))
4351 return NULL;
4352 res = (*func)(self);
4353 if (res == NULL && !PyErr_Occurred())
4354 PyErr_SetNone(PyExc_StopIteration);
4355 return res;
4358 static PyObject *
4359 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4361 descrgetfunc func = (descrgetfunc)wrapped;
4362 PyObject *obj;
4363 PyObject *type = NULL;
4365 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
4366 return NULL;
4367 if (obj == Py_None)
4368 obj = NULL;
4369 if (type == Py_None)
4370 type = NULL;
4371 if (type == NULL &&obj == NULL) {
4372 PyErr_SetString(PyExc_TypeError,
4373 "__get__(None, None) is invalid");
4374 return NULL;
4376 return (*func)(self, obj, type);
4379 static PyObject *
4380 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
4382 descrsetfunc func = (descrsetfunc)wrapped;
4383 PyObject *obj, *value;
4384 int ret;
4386 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
4387 return NULL;
4388 ret = (*func)(self, obj, value);
4389 if (ret < 0)
4390 return NULL;
4391 Py_INCREF(Py_None);
4392 return Py_None;
4395 static PyObject *
4396 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4398 descrsetfunc func = (descrsetfunc)wrapped;
4399 PyObject *obj;
4400 int ret;
4402 if (!check_num_args(args, 1))
4403 return NULL;
4404 obj = PyTuple_GET_ITEM(args, 0);
4405 ret = (*func)(self, obj, NULL);
4406 if (ret < 0)
4407 return NULL;
4408 Py_INCREF(Py_None);
4409 return Py_None;
4412 static PyObject *
4413 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4415 initproc func = (initproc)wrapped;
4417 if (func(self, args, kwds) < 0)
4418 return NULL;
4419 Py_INCREF(Py_None);
4420 return Py_None;
4423 static PyObject *
4424 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
4426 PyTypeObject *type, *subtype, *staticbase;
4427 PyObject *arg0, *res;
4429 if (self == NULL || !PyType_Check(self))
4430 Py_FatalError("__new__() called with non-type 'self'");
4431 type = (PyTypeObject *)self;
4432 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
4433 PyErr_Format(PyExc_TypeError,
4434 "%s.__new__(): not enough arguments",
4435 type->tp_name);
4436 return NULL;
4438 arg0 = PyTuple_GET_ITEM(args, 0);
4439 if (!PyType_Check(arg0)) {
4440 PyErr_Format(PyExc_TypeError,
4441 "%s.__new__(X): X is not a type object (%s)",
4442 type->tp_name,
4443 Py_TYPE(arg0)->tp_name);
4444 return NULL;
4446 subtype = (PyTypeObject *)arg0;
4447 if (!PyType_IsSubtype(subtype, type)) {
4448 PyErr_Format(PyExc_TypeError,
4449 "%s.__new__(%s): %s is not a subtype of %s",
4450 type->tp_name,
4451 subtype->tp_name,
4452 subtype->tp_name,
4453 type->tp_name);
4454 return NULL;
4457 /* Check that the use doesn't do something silly and unsafe like
4458 object.__new__(dict). To do this, we check that the
4459 most derived base that's not a heap type is this type. */
4460 staticbase = subtype;
4461 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4462 staticbase = staticbase->tp_base;
4463 /* If staticbase is NULL now, it is a really weird type.
4464 In the spirit of backwards compatibility (?), just shut up. */
4465 if (staticbase && staticbase->tp_new != type->tp_new) {
4466 PyErr_Format(PyExc_TypeError,
4467 "%s.__new__(%s) is not safe, use %s.__new__()",
4468 type->tp_name,
4469 subtype->tp_name,
4470 staticbase == NULL ? "?" : staticbase->tp_name);
4471 return NULL;
4474 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4475 if (args == NULL)
4476 return NULL;
4477 res = type->tp_new(subtype, args, kwds);
4478 Py_DECREF(args);
4479 return res;
4482 static struct PyMethodDef tp_new_methoddef[] = {
4483 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
4484 PyDoc_STR("T.__new__(S, ...) -> "
4485 "a new object with type S, a subtype of T")},
4489 static int
4490 add_tp_new_wrapper(PyTypeObject *type)
4492 PyObject *func;
4494 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4495 return 0;
4496 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4497 if (func == NULL)
4498 return -1;
4499 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4500 Py_DECREF(func);
4501 return -1;
4503 Py_DECREF(func);
4504 return 0;
4507 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4508 below at override_slots() for more explanation. */
4510 #define SLOT0(FUNCNAME, OPSTR) \
4511 static PyObject * \
4512 FUNCNAME(PyObject *self) \
4514 static PyObject *cache_str; \
4515 return call_method(self, OPSTR, &cache_str, "()"); \
4518 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4519 static PyObject * \
4520 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4522 static PyObject *cache_str; \
4523 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4526 /* Boolean helper for SLOT1BINFULL().
4527 right.__class__ is a nontrivial subclass of left.__class__. */
4528 static int
4529 method_is_overloaded(PyObject *left, PyObject *right, char *name)
4531 PyObject *a, *b;
4532 int ok;
4534 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
4535 if (b == NULL) {
4536 PyErr_Clear();
4537 /* If right doesn't have it, it's not overloaded */
4538 return 0;
4541 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
4542 if (a == NULL) {
4543 PyErr_Clear();
4544 Py_DECREF(b);
4545 /* If right has it but left doesn't, it's overloaded */
4546 return 1;
4549 ok = PyObject_RichCompareBool(a, b, Py_NE);
4550 Py_DECREF(a);
4551 Py_DECREF(b);
4552 if (ok < 0) {
4553 PyErr_Clear();
4554 return 0;
4557 return ok;
4561 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4562 static PyObject * \
4563 FUNCNAME(PyObject *self, PyObject *other) \
4565 static PyObject *cache_str, *rcache_str; \
4566 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
4567 Py_TYPE(other)->tp_as_number != NULL && \
4568 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4569 if (Py_TYPE(self)->tp_as_number != NULL && \
4570 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
4571 PyObject *r; \
4572 if (do_other && \
4573 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
4574 method_is_overloaded(self, other, ROPSTR)) { \
4575 r = call_maybe( \
4576 other, ROPSTR, &rcache_str, "(O)", self); \
4577 if (r != Py_NotImplemented) \
4578 return r; \
4579 Py_DECREF(r); \
4580 do_other = 0; \
4582 r = call_maybe( \
4583 self, OPSTR, &cache_str, "(O)", other); \
4584 if (r != Py_NotImplemented || \
4585 Py_TYPE(other) == Py_TYPE(self)) \
4586 return r; \
4587 Py_DECREF(r); \
4589 if (do_other) { \
4590 return call_maybe( \
4591 other, ROPSTR, &rcache_str, "(O)", self); \
4593 Py_INCREF(Py_NotImplemented); \
4594 return Py_NotImplemented; \
4597 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4598 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4600 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4601 static PyObject * \
4602 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4604 static PyObject *cache_str; \
4605 return call_method(self, OPSTR, &cache_str, \
4606 "(" ARGCODES ")", arg1, arg2); \
4609 static Py_ssize_t
4610 slot_sq_length(PyObject *self)
4612 static PyObject *len_str;
4613 PyObject *res = call_method(self, "__len__", &len_str, "()");
4614 Py_ssize_t len;
4616 if (res == NULL)
4617 return -1;
4618 len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
4619 Py_DECREF(res);
4620 if (len < 0) {
4621 if (!PyErr_Occurred())
4622 PyErr_SetString(PyExc_ValueError,
4623 "__len__() should return >= 0");
4624 return -1;
4626 return len;
4629 /* Super-optimized version of slot_sq_item.
4630 Other slots could do the same... */
4631 static PyObject *
4632 slot_sq_item(PyObject *self, Py_ssize_t i)
4634 static PyObject *getitem_str;
4635 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4636 descrgetfunc f;
4638 if (getitem_str == NULL) {
4639 getitem_str = PyUnicode_InternFromString("__getitem__");
4640 if (getitem_str == NULL)
4641 return NULL;
4643 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
4644 if (func != NULL) {
4645 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
4646 Py_INCREF(func);
4647 else {
4648 func = f(func, self, (PyObject *)(Py_TYPE(self)));
4649 if (func == NULL) {
4650 return NULL;
4653 ival = PyLong_FromSsize_t(i);
4654 if (ival != NULL) {
4655 args = PyTuple_New(1);
4656 if (args != NULL) {
4657 PyTuple_SET_ITEM(args, 0, ival);
4658 retval = PyObject_Call(func, args, NULL);
4659 Py_XDECREF(args);
4660 Py_XDECREF(func);
4661 return retval;
4665 else {
4666 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4668 Py_XDECREF(args);
4669 Py_XDECREF(ival);
4670 Py_XDECREF(func);
4671 return NULL;
4674 static int
4675 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
4677 PyObject *res;
4678 static PyObject *delitem_str, *setitem_str;
4680 if (value == NULL)
4681 res = call_method(self, "__delitem__", &delitem_str,
4682 "(n)", index);
4683 else
4684 res = call_method(self, "__setitem__", &setitem_str,
4685 "(nO)", index, value);
4686 if (res == NULL)
4687 return -1;
4688 Py_DECREF(res);
4689 return 0;
4692 static int
4693 slot_sq_contains(PyObject *self, PyObject *value)
4695 PyObject *func, *res, *args;
4696 int result = -1;
4698 static PyObject *contains_str;
4700 func = lookup_maybe(self, "__contains__", &contains_str);
4701 if (func != NULL) {
4702 args = PyTuple_Pack(1, value);
4703 if (args == NULL)
4704 res = NULL;
4705 else {
4706 res = PyObject_Call(func, args, NULL);
4707 Py_DECREF(args);
4709 Py_DECREF(func);
4710 if (res != NULL) {
4711 result = PyObject_IsTrue(res);
4712 Py_DECREF(res);
4715 else if (! PyErr_Occurred()) {
4716 /* Possible results: -1 and 1 */
4717 result = (int)_PySequence_IterSearch(self, value,
4718 PY_ITERSEARCH_CONTAINS);
4720 return result;
4723 #define slot_mp_length slot_sq_length
4725 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4727 static int
4728 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4730 PyObject *res;
4731 static PyObject *delitem_str, *setitem_str;
4733 if (value == NULL)
4734 res = call_method(self, "__delitem__", &delitem_str,
4735 "(O)", key);
4736 else
4737 res = call_method(self, "__setitem__", &setitem_str,
4738 "(OO)", key, value);
4739 if (res == NULL)
4740 return -1;
4741 Py_DECREF(res);
4742 return 0;
4745 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4746 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4747 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4748 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4749 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4751 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4753 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4754 nb_power, "__pow__", "__rpow__")
4756 static PyObject *
4757 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4759 static PyObject *pow_str;
4761 if (modulus == Py_None)
4762 return slot_nb_power_binary(self, other);
4763 /* Three-arg power doesn't use __rpow__. But ternary_op
4764 can call this when the second argument's type uses
4765 slot_nb_power, so check before calling self.__pow__. */
4766 if (Py_TYPE(self)->tp_as_number != NULL &&
4767 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
4768 return call_method(self, "__pow__", &pow_str,
4769 "(OO)", other, modulus);
4771 Py_INCREF(Py_NotImplemented);
4772 return Py_NotImplemented;
4775 SLOT0(slot_nb_negative, "__neg__")
4776 SLOT0(slot_nb_positive, "__pos__")
4777 SLOT0(slot_nb_absolute, "__abs__")
4779 static int
4780 slot_nb_bool(PyObject *self)
4782 PyObject *func, *args;
4783 static PyObject *bool_str, *len_str;
4784 int result = -1;
4785 int using_len = 0;
4787 func = lookup_maybe(self, "__bool__", &bool_str);
4788 if (func == NULL) {
4789 if (PyErr_Occurred())
4790 return -1;
4791 func = lookup_maybe(self, "__len__", &len_str);
4792 if (func == NULL)
4793 return PyErr_Occurred() ? -1 : 1;
4794 using_len = 1;
4796 args = PyTuple_New(0);
4797 if (args != NULL) {
4798 PyObject *temp = PyObject_Call(func, args, NULL);
4799 Py_DECREF(args);
4800 if (temp != NULL) {
4801 if (using_len) {
4802 /* enforced by slot_nb_len */
4803 result = PyObject_IsTrue(temp);
4805 else if (PyBool_Check(temp)) {
4806 result = PyObject_IsTrue(temp);
4808 else {
4809 PyErr_Format(PyExc_TypeError,
4810 "__bool__ should return "
4811 "bool, returned %s",
4812 Py_TYPE(temp)->tp_name);
4813 result = -1;
4815 Py_DECREF(temp);
4818 Py_DECREF(func);
4819 return result;
4823 static PyObject *
4824 slot_nb_index(PyObject *self)
4826 static PyObject *index_str;
4827 return call_method(self, "__index__", &index_str, "()");
4831 SLOT0(slot_nb_invert, "__invert__")
4832 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4833 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4834 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4835 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4836 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4838 SLOT0(slot_nb_int, "__int__")
4839 SLOT0(slot_nb_float, "__float__")
4840 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4841 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4842 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4843 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4844 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
4845 static PyObject *
4846 slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4848 static PyObject *cache_str;
4849 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4851 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4852 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4853 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4854 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4855 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4856 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4857 "__floordiv__", "__rfloordiv__")
4858 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4859 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4860 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4862 static PyObject *
4863 slot_tp_repr(PyObject *self)
4865 PyObject *func, *res;
4866 static PyObject *repr_str;
4868 func = lookup_method(self, "__repr__", &repr_str);
4869 if (func != NULL) {
4870 res = PyEval_CallObject(func, NULL);
4871 Py_DECREF(func);
4872 return res;
4874 PyErr_Clear();
4875 return PyUnicode_FromFormat("<%s object at %p>",
4876 Py_TYPE(self)->tp_name, self);
4879 static PyObject *
4880 slot_tp_str(PyObject *self)
4882 PyObject *func, *res;
4883 static PyObject *str_str;
4885 func = lookup_method(self, "__str__", &str_str);
4886 if (func != NULL) {
4887 res = PyEval_CallObject(func, NULL);
4888 Py_DECREF(func);
4889 return res;
4891 else {
4892 PyObject *ress;
4893 PyErr_Clear();
4894 res = slot_tp_repr(self);
4895 if (!res)
4896 return NULL;
4897 ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
4898 Py_DECREF(res);
4899 return ress;
4903 static long
4904 slot_tp_hash(PyObject *self)
4906 PyObject *func, *res;
4907 static PyObject *hash_str;
4908 long h;
4910 func = lookup_method(self, "__hash__", &hash_str);
4912 if (func == Py_None) {
4913 Py_DECREF(func);
4914 func = NULL;
4917 if (func == NULL) {
4918 return PyObject_HashNotImplemented(self);
4921 res = PyEval_CallObject(func, NULL);
4922 Py_DECREF(func);
4923 if (res == NULL)
4924 return -1;
4925 if (PyLong_Check(res))
4926 h = PyLong_Type.tp_hash(res);
4927 else
4928 h = PyLong_AsLong(res);
4929 Py_DECREF(res);
4930 if (h == -1 && !PyErr_Occurred())
4931 h = -2;
4932 return h;
4935 static PyObject *
4936 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4938 static PyObject *call_str;
4939 PyObject *meth = lookup_method(self, "__call__", &call_str);
4940 PyObject *res;
4942 if (meth == NULL)
4943 return NULL;
4945 res = PyObject_Call(meth, args, kwds);
4947 Py_DECREF(meth);
4948 return res;
4951 /* There are two slot dispatch functions for tp_getattro.
4953 - slot_tp_getattro() is used when __getattribute__ is overridden
4954 but no __getattr__ hook is present;
4956 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4958 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4959 detects the absence of __getattr__ and then installs the simpler slot if
4960 necessary. */
4962 static PyObject *
4963 slot_tp_getattro(PyObject *self, PyObject *name)
4965 static PyObject *getattribute_str = NULL;
4966 return call_method(self, "__getattribute__", &getattribute_str,
4967 "(O)", name);
4970 static PyObject *
4971 call_attribute(PyObject *self, PyObject *attr, PyObject *name)
4973 PyObject *res, *descr = NULL;
4974 descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
4976 if (f != NULL) {
4977 descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
4978 if (descr == NULL)
4979 return NULL;
4980 else
4981 attr = descr;
4983 res = PyObject_CallFunctionObjArgs(attr, name, NULL);
4984 Py_XDECREF(descr);
4985 return res;
4988 static PyObject *
4989 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4991 PyTypeObject *tp = Py_TYPE(self);
4992 PyObject *getattr, *getattribute, *res;
4993 static PyObject *getattribute_str = NULL;
4994 static PyObject *getattr_str = NULL;
4996 if (getattr_str == NULL) {
4997 getattr_str = PyUnicode_InternFromString("__getattr__");
4998 if (getattr_str == NULL)
4999 return NULL;
5001 if (getattribute_str == NULL) {
5002 getattribute_str =
5003 PyUnicode_InternFromString("__getattribute__");
5004 if (getattribute_str == NULL)
5005 return NULL;
5007 /* speed hack: we could use lookup_maybe, but that would resolve the
5008 method fully for each attribute lookup for classes with
5009 __getattr__, even when the attribute is present. So we use
5010 _PyType_Lookup and create the method only when needed, with
5011 call_attribute. */
5012 getattr = _PyType_Lookup(tp, getattr_str);
5013 if (getattr == NULL) {
5014 /* No __getattr__ hook: use a simpler dispatcher */
5015 tp->tp_getattro = slot_tp_getattro;
5016 return slot_tp_getattro(self, name);
5018 Py_INCREF(getattr);
5019 /* speed hack: we could use lookup_maybe, but that would resolve the
5020 method fully for each attribute lookup for classes with
5021 __getattr__, even when self has the default __getattribute__
5022 method. So we use _PyType_Lookup and create the method only when
5023 needed, with call_attribute. */
5024 getattribute = _PyType_Lookup(tp, getattribute_str);
5025 if (getattribute == NULL ||
5026 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
5027 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5028 (void *)PyObject_GenericGetAttr))
5029 res = PyObject_GenericGetAttr(self, name);
5030 else {
5031 Py_INCREF(getattribute);
5032 res = call_attribute(self, getattribute, name);
5033 Py_DECREF(getattribute);
5035 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
5036 PyErr_Clear();
5037 res = call_attribute(self, getattr, name);
5039 Py_DECREF(getattr);
5040 return res;
5043 static int
5044 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5046 PyObject *res;
5047 static PyObject *delattr_str, *setattr_str;
5049 if (value == NULL)
5050 res = call_method(self, "__delattr__", &delattr_str,
5051 "(O)", name);
5052 else
5053 res = call_method(self, "__setattr__", &setattr_str,
5054 "(OO)", name, value);
5055 if (res == NULL)
5056 return -1;
5057 Py_DECREF(res);
5058 return 0;
5061 static char *name_op[] = {
5062 "__lt__",
5063 "__le__",
5064 "__eq__",
5065 "__ne__",
5066 "__gt__",
5067 "__ge__",
5070 static PyObject *
5071 half_richcompare(PyObject *self, PyObject *other, int op)
5073 PyObject *func, *args, *res;
5074 static PyObject *op_str[6];
5076 func = lookup_method(self, name_op[op], &op_str[op]);
5077 if (func == NULL) {
5078 PyErr_Clear();
5079 Py_INCREF(Py_NotImplemented);
5080 return Py_NotImplemented;
5082 args = PyTuple_Pack(1, other);
5083 if (args == NULL)
5084 res = NULL;
5085 else {
5086 res = PyObject_Call(func, args, NULL);
5087 Py_DECREF(args);
5089 Py_DECREF(func);
5090 return res;
5093 static PyObject *
5094 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5096 PyObject *res;
5098 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
5099 res = half_richcompare(self, other, op);
5100 if (res != Py_NotImplemented)
5101 return res;
5102 Py_DECREF(res);
5104 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
5105 res = half_richcompare(other, self, _Py_SwappedOp[op]);
5106 if (res != Py_NotImplemented) {
5107 return res;
5109 Py_DECREF(res);
5111 Py_INCREF(Py_NotImplemented);
5112 return Py_NotImplemented;
5115 static PyObject *
5116 slot_tp_iter(PyObject *self)
5118 PyObject *func, *res;
5119 static PyObject *iter_str, *getitem_str;
5121 func = lookup_method(self, "__iter__", &iter_str);
5122 if (func != NULL) {
5123 PyObject *args;
5124 args = res = PyTuple_New(0);
5125 if (args != NULL) {
5126 res = PyObject_Call(func, args, NULL);
5127 Py_DECREF(args);
5129 Py_DECREF(func);
5130 return res;
5132 PyErr_Clear();
5133 func = lookup_method(self, "__getitem__", &getitem_str);
5134 if (func == NULL) {
5135 PyErr_Format(PyExc_TypeError,
5136 "'%.200s' object is not iterable",
5137 Py_TYPE(self)->tp_name);
5138 return NULL;
5140 Py_DECREF(func);
5141 return PySeqIter_New(self);
5144 static PyObject *
5145 slot_tp_iternext(PyObject *self)
5147 static PyObject *next_str;
5148 return call_method(self, "__next__", &next_str, "()");
5151 static PyObject *
5152 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5154 PyTypeObject *tp = Py_TYPE(self);
5155 PyObject *get;
5156 static PyObject *get_str = NULL;
5158 if (get_str == NULL) {
5159 get_str = PyUnicode_InternFromString("__get__");
5160 if (get_str == NULL)
5161 return NULL;
5163 get = _PyType_Lookup(tp, get_str);
5164 if (get == NULL) {
5165 /* Avoid further slowdowns */
5166 if (tp->tp_descr_get == slot_tp_descr_get)
5167 tp->tp_descr_get = NULL;
5168 Py_INCREF(self);
5169 return self;
5171 if (obj == NULL)
5172 obj = Py_None;
5173 if (type == NULL)
5174 type = Py_None;
5175 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
5178 static int
5179 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5181 PyObject *res;
5182 static PyObject *del_str, *set_str;
5184 if (value == NULL)
5185 res = call_method(self, "__delete__", &del_str,
5186 "(O)", target);
5187 else
5188 res = call_method(self, "__set__", &set_str,
5189 "(OO)", target, value);
5190 if (res == NULL)
5191 return -1;
5192 Py_DECREF(res);
5193 return 0;
5196 static int
5197 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5199 static PyObject *init_str;
5200 PyObject *meth = lookup_method(self, "__init__", &init_str);
5201 PyObject *res;
5203 if (meth == NULL)
5204 return -1;
5205 res = PyObject_Call(meth, args, kwds);
5206 Py_DECREF(meth);
5207 if (res == NULL)
5208 return -1;
5209 if (res != Py_None) {
5210 PyErr_Format(PyExc_TypeError,
5211 "__init__() should return None, not '%.200s'",
5212 Py_TYPE(res)->tp_name);
5213 Py_DECREF(res);
5214 return -1;
5216 Py_DECREF(res);
5217 return 0;
5220 static PyObject *
5221 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5223 static PyObject *new_str;
5224 PyObject *func;
5225 PyObject *newargs, *x;
5226 Py_ssize_t i, n;
5228 if (new_str == NULL) {
5229 new_str = PyUnicode_InternFromString("__new__");
5230 if (new_str == NULL)
5231 return NULL;
5233 func = PyObject_GetAttr((PyObject *)type, new_str);
5234 if (func == NULL)
5235 return NULL;
5236 assert(PyTuple_Check(args));
5237 n = PyTuple_GET_SIZE(args);
5238 newargs = PyTuple_New(n+1);
5239 if (newargs == NULL)
5240 return NULL;
5241 Py_INCREF(type);
5242 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5243 for (i = 0; i < n; i++) {
5244 x = PyTuple_GET_ITEM(args, i);
5245 Py_INCREF(x);
5246 PyTuple_SET_ITEM(newargs, i+1, x);
5248 x = PyObject_Call(func, newargs, kwds);
5249 Py_DECREF(newargs);
5250 Py_DECREF(func);
5251 return x;
5254 static void
5255 slot_tp_del(PyObject *self)
5257 static PyObject *del_str = NULL;
5258 PyObject *del, *res;
5259 PyObject *error_type, *error_value, *error_traceback;
5261 /* Temporarily resurrect the object. */
5262 assert(self->ob_refcnt == 0);
5263 self->ob_refcnt = 1;
5265 /* Save the current exception, if any. */
5266 PyErr_Fetch(&error_type, &error_value, &error_traceback);
5268 /* Execute __del__ method, if any. */
5269 del = lookup_maybe(self, "__del__", &del_str);
5270 if (del != NULL) {
5271 res = PyEval_CallObject(del, NULL);
5272 if (res == NULL)
5273 PyErr_WriteUnraisable(del);
5274 else
5275 Py_DECREF(res);
5276 Py_DECREF(del);
5279 /* Restore the saved exception. */
5280 PyErr_Restore(error_type, error_value, error_traceback);
5282 /* Undo the temporary resurrection; can't use DECREF here, it would
5283 * cause a recursive call.
5285 assert(self->ob_refcnt > 0);
5286 if (--self->ob_refcnt == 0)
5287 return; /* this is the normal path out */
5289 /* __del__ resurrected it! Make it look like the original Py_DECREF
5290 * never happened.
5293 Py_ssize_t refcnt = self->ob_refcnt;
5294 _Py_NewReference(self);
5295 self->ob_refcnt = refcnt;
5297 assert(!PyType_IS_GC(Py_TYPE(self)) ||
5298 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
5299 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5300 * we need to undo that. */
5301 _Py_DEC_REFTOTAL;
5302 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5303 * chain, so no more to do there.
5304 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5305 * _Py_NewReference bumped tp_allocs: both of those need to be
5306 * undone.
5308 #ifdef COUNT_ALLOCS
5309 --Py_TYPE(self)->tp_frees;
5310 --Py_TYPE(self)->tp_allocs;
5311 #endif
5315 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
5316 functions. The offsets here are relative to the 'PyHeapTypeObject'
5317 structure, which incorporates the additional structures used for numbers,
5318 sequences and mappings.
5319 Note that multiple names may map to the same slot (e.g. __eq__,
5320 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
5321 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5322 terminated with an all-zero entry. (This table is further initialized and
5323 sorted in init_slotdefs() below.) */
5325 typedef struct wrapperbase slotdef;
5327 #undef TPSLOT
5328 #undef FLSLOT
5329 #undef ETSLOT
5330 #undef SQSLOT
5331 #undef MPSLOT
5332 #undef NBSLOT
5333 #undef UNSLOT
5334 #undef IBSLOT
5335 #undef BINSLOT
5336 #undef RBINSLOT
5338 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5339 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5340 PyDoc_STR(DOC)}
5341 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5342 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5343 PyDoc_STR(DOC), FLAGS}
5344 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5345 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5346 PyDoc_STR(DOC)}
5347 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5348 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5349 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5350 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5351 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5352 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5353 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5354 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5355 "x." NAME "() <==> " DOC)
5356 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5357 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5358 "x." NAME "(y) <==> x" DOC "y")
5359 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5360 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5361 "x." NAME "(y) <==> x" DOC "y")
5362 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5363 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5364 "x." NAME "(y) <==> y" DOC "x")
5365 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5366 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5367 "x." NAME "(y) <==> " DOC)
5368 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5369 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5370 "x." NAME "(y) <==> " DOC)
5372 static slotdef slotdefs[] = {
5373 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
5374 "x.__len__() <==> len(x)"),
5375 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5376 The logic in abstract.c always falls back to nb_add/nb_multiply in
5377 this case. Defining both the nb_* and the sq_* slots to call the
5378 user-defined methods has unexpected side-effects, as shown by
5379 test_descr.notimplemented() */
5380 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
5381 "x.__add__(y) <==> x+y"),
5382 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
5383 "x.__mul__(n) <==> x*n"),
5384 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
5385 "x.__rmul__(n) <==> n*x"),
5386 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5387 "x.__getitem__(y) <==> x[y]"),
5388 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
5389 "x.__setitem__(i, y) <==> x[i]=y"),
5390 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
5391 "x.__delitem__(y) <==> del x[y]"),
5392 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5393 "x.__contains__(y) <==> y in x"),
5394 SQSLOT("__iadd__", sq_inplace_concat, NULL,
5395 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
5396 SQSLOT("__imul__", sq_inplace_repeat, NULL,
5397 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
5399 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
5400 "x.__len__() <==> len(x)"),
5401 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
5402 wrap_binaryfunc,
5403 "x.__getitem__(y) <==> x[y]"),
5404 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
5405 wrap_objobjargproc,
5406 "x.__setitem__(i, y) <==> x[i]=y"),
5407 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
5408 wrap_delitem,
5409 "x.__delitem__(y) <==> del x[y]"),
5411 BINSLOT("__add__", nb_add, slot_nb_add,
5412 "+"),
5413 RBINSLOT("__radd__", nb_add, slot_nb_add,
5414 "+"),
5415 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5416 "-"),
5417 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5418 "-"),
5419 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5420 "*"),
5421 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5422 "*"),
5423 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5424 "%"),
5425 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5426 "%"),
5427 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
5428 "divmod(x, y)"),
5429 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
5430 "divmod(y, x)"),
5431 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5432 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5433 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5434 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5435 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5436 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5437 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5438 "abs(x)"),
5439 UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
5440 "x != 0"),
5441 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5442 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5443 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5444 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5445 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5446 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5447 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5448 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5449 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5450 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5451 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5452 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5453 "int(x)"),
5454 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5455 "float(x)"),
5456 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
5457 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5458 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5459 wrap_binaryfunc, "+"),
5460 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5461 wrap_binaryfunc, "-"),
5462 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5463 wrap_binaryfunc, "*"),
5464 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5465 wrap_binaryfunc, "%"),
5466 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5467 wrap_binaryfunc, "**"),
5468 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5469 wrap_binaryfunc, "<<"),
5470 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5471 wrap_binaryfunc, ">>"),
5472 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5473 wrap_binaryfunc, "&"),
5474 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5475 wrap_binaryfunc, "^"),
5476 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5477 wrap_binaryfunc, "|"),
5478 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5479 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5480 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5481 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5482 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5483 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5484 IBSLOT("__itruediv__", nb_inplace_true_divide,
5485 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5487 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5488 "x.__str__() <==> str(x)"),
5489 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5490 "x.__repr__() <==> repr(x)"),
5491 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5492 "x.__hash__() <==> hash(x)"),
5493 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5494 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5495 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5496 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5497 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5498 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5499 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5500 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5501 "x.__setattr__('name', value) <==> x.name = value"),
5502 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5503 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5504 "x.__delattr__('name') <==> del x.name"),
5505 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5506 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5507 "x.__lt__(y) <==> x<y"),
5508 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5509 "x.__le__(y) <==> x<=y"),
5510 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5511 "x.__eq__(y) <==> x==y"),
5512 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5513 "x.__ne__(y) <==> x!=y"),
5514 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5515 "x.__gt__(y) <==> x>y"),
5516 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5517 "x.__ge__(y) <==> x>=y"),
5518 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5519 "x.__iter__() <==> iter(x)"),
5520 TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
5521 "x.__next__() <==> next(x)"),
5522 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5523 "descr.__get__(obj[, type]) -> value"),
5524 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5525 "descr.__set__(obj, value)"),
5526 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5527 wrap_descr_delete, "descr.__delete__(obj)"),
5528 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5529 "x.__init__(...) initializes x; "
5530 "see x.__class__.__doc__ for signature",
5531 PyWrapperFlag_KEYWORDS),
5532 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5533 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5534 {NULL}
5537 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5538 pointer to the actual slot. This is not quite the same as simply adding
5539 the offset to the type pointer, since it takes care to indirect through the
5540 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5541 indirection pointer is NULL. */
5542 static void **
5543 slotptr(PyTypeObject *type, int ioffset)
5545 char *ptr;
5546 long offset = ioffset;
5548 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5549 assert(offset >= 0);
5550 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5551 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5552 ptr = (char *)type->tp_as_sequence;
5553 offset -= offsetof(PyHeapTypeObject, as_sequence);
5555 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5556 ptr = (char *)type->tp_as_mapping;
5557 offset -= offsetof(PyHeapTypeObject, as_mapping);
5559 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5560 ptr = (char *)type->tp_as_number;
5561 offset -= offsetof(PyHeapTypeObject, as_number);
5563 else {
5564 ptr = (char *)type;
5566 if (ptr != NULL)
5567 ptr += offset;
5568 return (void **)ptr;
5571 /* Length of array of slotdef pointers used to store slots with the
5572 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5573 the same __name__, for any __name__. Since that's a static property, it is
5574 appropriate to declare fixed-size arrays for this. */
5575 #define MAX_EQUIV 10
5577 /* Return a slot pointer for a given name, but ONLY if the attribute has
5578 exactly one slot function. The name must be an interned string. */
5579 static void **
5580 resolve_slotdups(PyTypeObject *type, PyObject *name)
5582 /* XXX Maybe this could be optimized more -- but is it worth it? */
5584 /* pname and ptrs act as a little cache */
5585 static PyObject *pname;
5586 static slotdef *ptrs[MAX_EQUIV];
5587 slotdef *p, **pp;
5588 void **res, **ptr;
5590 if (pname != name) {
5591 /* Collect all slotdefs that match name into ptrs. */
5592 pname = name;
5593 pp = ptrs;
5594 for (p = slotdefs; p->name_strobj; p++) {
5595 if (p->name_strobj == name)
5596 *pp++ = p;
5598 *pp = NULL;
5601 /* Look in all matching slots of the type; if exactly one of these has
5602 a filled-in slot, return its value. Otherwise return NULL. */
5603 res = NULL;
5604 for (pp = ptrs; *pp; pp++) {
5605 ptr = slotptr(type, (*pp)->offset);
5606 if (ptr == NULL || *ptr == NULL)
5607 continue;
5608 if (res != NULL)
5609 return NULL;
5610 res = ptr;
5612 return res;
5615 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5616 does some incredibly complex thinking and then sticks something into the
5617 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5618 interests, and then stores a generic wrapper or a specific function into
5619 the slot.) Return a pointer to the next slotdef with a different offset,
5620 because that's convenient for fixup_slot_dispatchers(). */
5621 static slotdef *
5622 update_one_slot(PyTypeObject *type, slotdef *p)
5624 PyObject *descr;
5625 PyWrapperDescrObject *d;
5626 void *generic = NULL, *specific = NULL;
5627 int use_generic = 0;
5628 int offset = p->offset;
5629 void **ptr = slotptr(type, offset);
5631 if (ptr == NULL) {
5632 do {
5633 ++p;
5634 } while (p->offset == offset);
5635 return p;
5637 do {
5638 descr = _PyType_Lookup(type, p->name_strobj);
5639 if (descr == NULL) {
5640 if (ptr == (void**)&type->tp_iternext) {
5641 specific = _PyObject_NextNotImplemented;
5643 continue;
5645 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
5646 void **tptr = resolve_slotdups(type, p->name_strobj);
5647 if (tptr == NULL || tptr == ptr)
5648 generic = p->function;
5649 d = (PyWrapperDescrObject *)descr;
5650 if (d->d_base->wrapper == p->wrapper &&
5651 PyType_IsSubtype(type, d->d_type))
5653 if (specific == NULL ||
5654 specific == d->d_wrapped)
5655 specific = d->d_wrapped;
5656 else
5657 use_generic = 1;
5660 else if (Py_TYPE(descr) == &PyCFunction_Type &&
5661 PyCFunction_GET_FUNCTION(descr) ==
5662 (PyCFunction)tp_new_wrapper &&
5663 ptr == (void**)&type->tp_new)
5665 /* The __new__ wrapper is not a wrapper descriptor,
5666 so must be special-cased differently.
5667 If we don't do this, creating an instance will
5668 always use slot_tp_new which will look up
5669 __new__ in the MRO which will call tp_new_wrapper
5670 which will look through the base classes looking
5671 for a static base and call its tp_new (usually
5672 PyType_GenericNew), after performing various
5673 sanity checks and constructing a new argument
5674 list. Cut all that nonsense short -- this speeds
5675 up instance creation tremendously. */
5676 specific = (void *)type->tp_new;
5677 /* XXX I'm not 100% sure that there isn't a hole
5678 in this reasoning that requires additional
5679 sanity checks. I'll buy the first person to
5680 point out a bug in this reasoning a beer. */
5682 else if (descr == Py_None &&
5683 ptr == (void**)&type->tp_hash) {
5684 /* We specifically allow __hash__ to be set to None
5685 to prevent inheritance of the default
5686 implementation from object.__hash__ */
5687 specific = PyObject_HashNotImplemented;
5689 else {
5690 use_generic = 1;
5691 generic = p->function;
5693 } while ((++p)->offset == offset);
5694 if (specific && !use_generic)
5695 *ptr = specific;
5696 else
5697 *ptr = generic;
5698 return p;
5701 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5702 This is a callback for update_subclasses(). */
5703 static int
5704 update_slots_callback(PyTypeObject *type, void *data)
5706 slotdef **pp = (slotdef **)data;
5708 for (; *pp; pp++)
5709 update_one_slot(type, *pp);
5710 return 0;
5713 /* Comparison function for qsort() to compare slotdefs by their offset, and
5714 for equal offset by their address (to force a stable sort). */
5715 static int
5716 slotdef_cmp(const void *aa, const void *bb)
5718 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5719 int c = a->offset - b->offset;
5720 if (c != 0)
5721 return c;
5722 else
5723 /* Cannot use a-b, as this gives off_t,
5724 which may lose precision when converted to int. */
5725 return (a > b) ? 1 : (a < b) ? -1 : 0;
5728 /* Initialize the slotdefs table by adding interned string objects for the
5729 names and sorting the entries. */
5730 static void
5731 init_slotdefs(void)
5733 slotdef *p;
5734 static int initialized = 0;
5736 if (initialized)
5737 return;
5738 for (p = slotdefs; p->name; p++) {
5739 p->name_strobj = PyUnicode_InternFromString(p->name);
5740 if (!p->name_strobj)
5741 Py_FatalError("Out of memory interning slotdef names");
5743 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5744 slotdef_cmp);
5745 initialized = 1;
5748 /* Update the slots after assignment to a class (type) attribute. */
5749 static int
5750 update_slot(PyTypeObject *type, PyObject *name)
5752 slotdef *ptrs[MAX_EQUIV];
5753 slotdef *p;
5754 slotdef **pp;
5755 int offset;
5757 /* Clear the VALID_VERSION flag of 'type' and all its
5758 subclasses. This could possibly be unified with the
5759 update_subclasses() recursion below, but carefully:
5760 they each have their own conditions on which to stop
5761 recursing into subclasses. */
5762 PyType_Modified(type);
5764 init_slotdefs();
5765 pp = ptrs;
5766 for (p = slotdefs; p->name; p++) {
5767 /* XXX assume name is interned! */
5768 if (p->name_strobj == name)
5769 *pp++ = p;
5771 *pp = NULL;
5772 for (pp = ptrs; *pp; pp++) {
5773 p = *pp;
5774 offset = p->offset;
5775 while (p > slotdefs && (p-1)->offset == offset)
5776 --p;
5777 *pp = p;
5779 if (ptrs[0] == NULL)
5780 return 0; /* Not an attribute that affects any slots */
5781 return update_subclasses(type, name,
5782 update_slots_callback, (void *)ptrs);
5785 /* Store the proper functions in the slot dispatches at class (type)
5786 definition time, based upon which operations the class overrides in its
5787 dict. */
5788 static void
5789 fixup_slot_dispatchers(PyTypeObject *type)
5791 slotdef *p;
5793 init_slotdefs();
5794 for (p = slotdefs; p->name; )
5795 p = update_one_slot(type, p);
5798 static void
5799 update_all_slots(PyTypeObject* type)
5801 slotdef *p;
5803 init_slotdefs();
5804 for (p = slotdefs; p->name; p++) {
5805 /* update_slot returns int but can't actually fail */
5806 update_slot(type, p->name_strobj);
5810 /* recurse_down_subclasses() and update_subclasses() are mutually
5811 recursive functions to call a callback for all subclasses,
5812 but refraining from recursing into subclasses that define 'name'. */
5814 static int
5815 update_subclasses(PyTypeObject *type, PyObject *name,
5816 update_callback callback, void *data)
5818 if (callback(type, data) < 0)
5819 return -1;
5820 return recurse_down_subclasses(type, name, callback, data);
5823 static int
5824 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5825 update_callback callback, void *data)
5827 PyTypeObject *subclass;
5828 PyObject *ref, *subclasses, *dict;
5829 Py_ssize_t i, n;
5831 subclasses = type->tp_subclasses;
5832 if (subclasses == NULL)
5833 return 0;
5834 assert(PyList_Check(subclasses));
5835 n = PyList_GET_SIZE(subclasses);
5836 for (i = 0; i < n; i++) {
5837 ref = PyList_GET_ITEM(subclasses, i);
5838 assert(PyWeakref_CheckRef(ref));
5839 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5840 assert(subclass != NULL);
5841 if ((PyObject *)subclass == Py_None)
5842 continue;
5843 assert(PyType_Check(subclass));
5844 /* Avoid recursing down into unaffected classes */
5845 dict = subclass->tp_dict;
5846 if (dict != NULL && PyDict_Check(dict) &&
5847 PyDict_GetItem(dict, name) != NULL)
5848 continue;
5849 if (update_subclasses(subclass, name, callback, data) < 0)
5850 return -1;
5852 return 0;
5855 /* This function is called by PyType_Ready() to populate the type's
5856 dictionary with method descriptors for function slots. For each
5857 function slot (like tp_repr) that's defined in the type, one or more
5858 corresponding descriptors are added in the type's tp_dict dictionary
5859 under the appropriate name (like __repr__). Some function slots
5860 cause more than one descriptor to be added (for example, the nb_add
5861 slot adds both __add__ and __radd__ descriptors) and some function
5862 slots compete for the same descriptor (for example both sq_item and
5863 mp_subscript generate a __getitem__ descriptor).
5865 In the latter case, the first slotdef entry encoutered wins. Since
5866 slotdef entries are sorted by the offset of the slot in the
5867 PyHeapTypeObject, this gives us some control over disambiguating
5868 between competing slots: the members of PyHeapTypeObject are listed
5869 from most general to least general, so the most general slot is
5870 preferred. In particular, because as_mapping comes before as_sequence,
5871 for a type that defines both mp_subscript and sq_item, mp_subscript
5872 wins.
5874 This only adds new descriptors and doesn't overwrite entries in
5875 tp_dict that were previously defined. The descriptors contain a
5876 reference to the C function they must call, so that it's safe if they
5877 are copied into a subtype's __dict__ and the subtype has a different
5878 C function in its slot -- calling the method defined by the
5879 descriptor will call the C function that was used to create it,
5880 rather than the C function present in the slot when it is called.
5881 (This is important because a subtype may have a C function in the
5882 slot that calls the method from the dictionary, and we want to avoid
5883 infinite recursion here.) */
5885 static int
5886 add_operators(PyTypeObject *type)
5888 PyObject *dict = type->tp_dict;
5889 slotdef *p;
5890 PyObject *descr;
5891 void **ptr;
5893 init_slotdefs();
5894 for (p = slotdefs; p->name; p++) {
5895 if (p->wrapper == NULL)
5896 continue;
5897 ptr = slotptr(type, p->offset);
5898 if (!ptr || !*ptr)
5899 continue;
5900 if (PyDict_GetItem(dict, p->name_strobj))
5901 continue;
5902 if (*ptr == PyObject_HashNotImplemented) {
5903 /* Classes may prevent the inheritance of the tp_hash
5904 slot by storing PyObject_HashNotImplemented in it. Make it
5905 visible as a None value for the __hash__ attribute. */
5906 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
5907 return -1;
5909 else {
5910 descr = PyDescr_NewWrapper(type, p, *ptr);
5911 if (descr == NULL)
5912 return -1;
5913 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5914 return -1;
5915 Py_DECREF(descr);
5918 if (type->tp_new != NULL) {
5919 if (add_tp_new_wrapper(type) < 0)
5920 return -1;
5922 return 0;
5926 /* Cooperative 'super' */
5928 typedef struct {
5929 PyObject_HEAD
5930 PyTypeObject *type;
5931 PyObject *obj;
5932 PyTypeObject *obj_type;
5933 } superobject;
5935 static PyMemberDef super_members[] = {
5936 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5937 "the class invoking super()"},
5938 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5939 "the instance invoking super(); may be None"},
5940 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5941 "the type of the instance invoking super(); may be None"},
5945 static void
5946 super_dealloc(PyObject *self)
5948 superobject *su = (superobject *)self;
5950 _PyObject_GC_UNTRACK(self);
5951 Py_XDECREF(su->obj);
5952 Py_XDECREF(su->type);
5953 Py_XDECREF(su->obj_type);
5954 Py_TYPE(self)->tp_free(self);
5957 static PyObject *
5958 super_repr(PyObject *self)
5960 superobject *su = (superobject *)self;
5962 if (su->obj_type)
5963 return PyUnicode_FromFormat(
5964 "<super: <class '%s'>, <%s object>>",
5965 su->type ? su->type->tp_name : "NULL",
5966 su->obj_type->tp_name);
5967 else
5968 return PyUnicode_FromFormat(
5969 "<super: <class '%s'>, NULL>",
5970 su->type ? su->type->tp_name : "NULL");
5973 static PyObject *
5974 super_getattro(PyObject *self, PyObject *name)
5976 superobject *su = (superobject *)self;
5977 int skip = su->obj_type == NULL;
5979 if (!skip) {
5980 /* We want __class__ to return the class of the super object
5981 (i.e. super, or a subclass), not the class of su->obj. */
5982 skip = (PyUnicode_Check(name) &&
5983 PyUnicode_GET_SIZE(name) == 9 &&
5984 PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
5987 if (!skip) {
5988 PyObject *mro, *res, *tmp, *dict;
5989 PyTypeObject *starttype;
5990 descrgetfunc f;
5991 Py_ssize_t i, n;
5993 starttype = su->obj_type;
5994 mro = starttype->tp_mro;
5996 if (mro == NULL)
5997 n = 0;
5998 else {
5999 assert(PyTuple_Check(mro));
6000 n = PyTuple_GET_SIZE(mro);
6002 for (i = 0; i < n; i++) {
6003 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
6004 break;
6006 i++;
6007 res = NULL;
6008 for (; i < n; i++) {
6009 tmp = PyTuple_GET_ITEM(mro, i);
6010 if (PyType_Check(tmp))
6011 dict = ((PyTypeObject *)tmp)->tp_dict;
6012 else
6013 continue;
6014 res = PyDict_GetItem(dict, name);
6015 if (res != NULL) {
6016 Py_INCREF(res);
6017 f = Py_TYPE(res)->tp_descr_get;
6018 if (f != NULL) {
6019 tmp = f(res,
6020 /* Only pass 'obj' param if
6021 this is instance-mode super
6022 (See SF ID #743627)
6024 (su->obj == (PyObject *)
6025 su->obj_type
6026 ? (PyObject *)NULL
6027 : su->obj),
6028 (PyObject *)starttype);
6029 Py_DECREF(res);
6030 res = tmp;
6032 return res;
6036 return PyObject_GenericGetAttr(self, name);
6039 static PyTypeObject *
6040 supercheck(PyTypeObject *type, PyObject *obj)
6042 /* Check that a super() call makes sense. Return a type object.
6044 obj can be a new-style class, or an instance of one:
6046 - If it is a class, it must be a subclass of 'type'. This case is
6047 used for class methods; the return value is obj.
6049 - If it is an instance, it must be an instance of 'type'. This is
6050 the normal case; the return value is obj.__class__.
6052 But... when obj is an instance, we want to allow for the case where
6053 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
6054 This will allow using super() with a proxy for obj.
6057 /* Check for first bullet above (special case) */
6058 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6059 Py_INCREF(obj);
6060 return (PyTypeObject *)obj;
6063 /* Normal case */
6064 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6065 Py_INCREF(Py_TYPE(obj));
6066 return Py_TYPE(obj);
6068 else {
6069 /* Try the slow way */
6070 static PyObject *class_str = NULL;
6071 PyObject *class_attr;
6073 if (class_str == NULL) {
6074 class_str = PyUnicode_FromString("__class__");
6075 if (class_str == NULL)
6076 return NULL;
6079 class_attr = PyObject_GetAttr(obj, class_str);
6081 if (class_attr != NULL &&
6082 PyType_Check(class_attr) &&
6083 (PyTypeObject *)class_attr != Py_TYPE(obj))
6085 int ok = PyType_IsSubtype(
6086 (PyTypeObject *)class_attr, type);
6087 if (ok)
6088 return (PyTypeObject *)class_attr;
6091 if (class_attr == NULL)
6092 PyErr_Clear();
6093 else
6094 Py_DECREF(class_attr);
6097 PyErr_SetString(PyExc_TypeError,
6098 "super(type, obj): "
6099 "obj must be an instance or subtype of type");
6100 return NULL;
6103 static PyObject *
6104 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6106 superobject *su = (superobject *)self;
6107 superobject *newobj;
6109 if (obj == NULL || obj == Py_None || su->obj != NULL) {
6110 /* Not binding to an object, or already bound */
6111 Py_INCREF(self);
6112 return self;
6114 if (Py_TYPE(su) != &PySuper_Type)
6115 /* If su is an instance of a (strict) subclass of super,
6116 call its type */
6117 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
6118 su->type, obj, NULL);
6119 else {
6120 /* Inline the common case */
6121 PyTypeObject *obj_type = supercheck(su->type, obj);
6122 if (obj_type == NULL)
6123 return NULL;
6124 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
6125 NULL, NULL);
6126 if (newobj == NULL)
6127 return NULL;
6128 Py_INCREF(su->type);
6129 Py_INCREF(obj);
6130 newobj->type = su->type;
6131 newobj->obj = obj;
6132 newobj->obj_type = obj_type;
6133 return (PyObject *)newobj;
6137 static int
6138 super_init(PyObject *self, PyObject *args, PyObject *kwds)
6140 superobject *su = (superobject *)self;
6141 PyTypeObject *type = NULL;
6142 PyObject *obj = NULL;
6143 PyTypeObject *obj_type = NULL;
6145 if (!_PyArg_NoKeywords("super", kwds))
6146 return -1;
6147 if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
6148 return -1;
6150 if (type == NULL) {
6151 /* Call super(), without args -- fill in from __class__
6152 and first local variable on the stack. */
6153 PyFrameObject *f = PyThreadState_GET()->frame;
6154 PyCodeObject *co = f->f_code;
6155 int i, n;
6156 if (co == NULL) {
6157 PyErr_SetString(PyExc_SystemError,
6158 "super(): no code object");
6159 return -1;
6161 if (co->co_argcount == 0) {
6162 PyErr_SetString(PyExc_SystemError,
6163 "super(): no arguments");
6164 return -1;
6166 obj = f->f_localsplus[0];
6167 if (obj == NULL) {
6168 PyErr_SetString(PyExc_SystemError,
6169 "super(): arg[0] deleted");
6170 return -1;
6172 if (co->co_freevars == NULL)
6173 n = 0;
6174 else {
6175 assert(PyTuple_Check(co->co_freevars));
6176 n = PyTuple_GET_SIZE(co->co_freevars);
6178 for (i = 0; i < n; i++) {
6179 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
6180 assert(PyUnicode_Check(name));
6181 if (!PyUnicode_CompareWithASCIIString(name,
6182 "__class__")) {
6183 Py_ssize_t index = co->co_nlocals +
6184 PyTuple_GET_SIZE(co->co_cellvars) + i;
6185 PyObject *cell = f->f_localsplus[index];
6186 if (cell == NULL || !PyCell_Check(cell)) {
6187 PyErr_SetString(PyExc_SystemError,
6188 "super(): bad __class__ cell");
6189 return -1;
6191 type = (PyTypeObject *) PyCell_GET(cell);
6192 if (type == NULL) {
6193 PyErr_SetString(PyExc_SystemError,
6194 "super(): empty __class__ cell");
6195 return -1;
6197 if (!PyType_Check(type)) {
6198 PyErr_Format(PyExc_SystemError,
6199 "super(): __class__ is not a type (%s)",
6200 Py_TYPE(type)->tp_name);
6201 return -1;
6203 break;
6206 if (type == NULL) {
6207 PyErr_SetString(PyExc_SystemError,
6208 "super(): __class__ cell not found");
6209 return -1;
6213 if (obj == Py_None)
6214 obj = NULL;
6215 if (obj != NULL) {
6216 obj_type = supercheck(type, obj);
6217 if (obj_type == NULL)
6218 return -1;
6219 Py_INCREF(obj);
6221 Py_INCREF(type);
6222 su->type = type;
6223 su->obj = obj;
6224 su->obj_type = obj_type;
6225 return 0;
6228 PyDoc_STRVAR(super_doc,
6229 "super() -> same as super(__class__, <first argument>)\n"
6230 "super(type) -> unbound super object\n"
6231 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
6232 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
6233 "Typical use to call a cooperative superclass method:\n"
6234 "class C(B):\n"
6235 " def meth(self, arg):\n"
6236 " super().meth(arg)\n"
6237 "This works for class methods too:\n"
6238 "class C(B):\n"
6239 " @classmethod\n"
6240 " def cmeth(cls, arg):\n"
6241 " super().cmeth(arg)\n");
6243 static int
6244 super_traverse(PyObject *self, visitproc visit, void *arg)
6246 superobject *su = (superobject *)self;
6248 Py_VISIT(su->obj);
6249 Py_VISIT(su->type);
6250 Py_VISIT(su->obj_type);
6252 return 0;
6255 PyTypeObject PySuper_Type = {
6256 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6257 "super", /* tp_name */
6258 sizeof(superobject), /* tp_basicsize */
6259 0, /* tp_itemsize */
6260 /* methods */
6261 super_dealloc, /* tp_dealloc */
6262 0, /* tp_print */
6263 0, /* tp_getattr */
6264 0, /* tp_setattr */
6265 0, /* tp_reserved */
6266 super_repr, /* tp_repr */
6267 0, /* tp_as_number */
6268 0, /* tp_as_sequence */
6269 0, /* tp_as_mapping */
6270 0, /* tp_hash */
6271 0, /* tp_call */
6272 0, /* tp_str */
6273 super_getattro, /* tp_getattro */
6274 0, /* tp_setattro */
6275 0, /* tp_as_buffer */
6276 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6277 Py_TPFLAGS_BASETYPE, /* tp_flags */
6278 super_doc, /* tp_doc */
6279 super_traverse, /* tp_traverse */
6280 0, /* tp_clear */
6281 0, /* tp_richcompare */
6282 0, /* tp_weaklistoffset */
6283 0, /* tp_iter */
6284 0, /* tp_iternext */
6285 0, /* tp_methods */
6286 super_members, /* tp_members */
6287 0, /* tp_getset */
6288 0, /* tp_base */
6289 0, /* tp_dict */
6290 super_descr_get, /* tp_descr_get */
6291 0, /* tp_descr_set */
6292 0, /* tp_dictoffset */
6293 super_init, /* tp_init */
6294 PyType_GenericAlloc, /* tp_alloc */
6295 PyType_GenericNew, /* tp_new */
6296 PyObject_GC_Del, /* tp_free */