Added duplicate call to fileConfig() to ensure that it cleans up after itself correctly.
[python.git] / Objects / typeobject.c
blob2046972646efed5e02ae2e4a2417e106ae1483a0
1 /* Type object implementation */
3 #include "Python.h"
4 #include "structmember.h"
6 #include <ctype.h>
8 static PyMemberDef type_members[] = {
9 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
12 {"__weakrefoffset__", T_LONG,
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
21 static PyObject *
22 type_name(PyTypeObject *type, void *context)
24 const char *s;
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
29 Py_INCREF(et->ht_name);
30 return et->ht_name;
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
42 static int
43 type_set_name(PyTypeObject *type, PyObject *value, void *context)
45 PyHeapTypeObject* et;
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
63 if (strlen(PyString_AS_STRING(value))
64 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
70 et = (PyHeapTypeObject*)type;
72 Py_INCREF(value);
74 Py_DECREF(et->ht_name);
75 et->ht_name = value;
77 type->tp_name = PyString_AS_STRING(value);
79 return 0;
82 static PyObject *
83 type_module(PyTypeObject *type, void *context)
85 PyObject *mod;
86 char *s;
88 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
90 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
94 Py_XINCREF(mod);
95 return mod;
97 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
101 type->tp_name, (int)(s - type->tp_name));
102 return PyString_FromString("__builtin__");
106 static int
107 type_set_module(PyTypeObject *type, PyObject *value, void *context)
109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
123 static PyObject *
124 type_get_bases(PyTypeObject *type, void *context)
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
130 static PyTypeObject *best_base(PyObject *);
131 static int mro_internal(PyTypeObject *);
132 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133 static int add_subclass(PyTypeObject*, PyTypeObject*);
134 static void remove_subclass(PyTypeObject *, PyTypeObject *);
135 static void update_all_slots(PyTypeObject *);
137 typedef int (*update_callback)(PyTypeObject *, void *);
138 static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
143 static int
144 mro_subclasses(PyTypeObject *type, PyObject* temp)
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
148 Py_ssize_t i, n;
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
166 return -1;
168 else {
169 PyObject* tuple;
170 tuple = PyTuple_Pack(2, subclass, old_mro);
171 Py_DECREF(old_mro);
172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
176 Py_DECREF(tuple);
178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
181 return 0;
184 static int
185 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
187 Py_ssize_t i;
188 int r = 0;
189 PyObject *ob, *temp;
190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
217 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
233 new_base = best_base(value);
235 if (!new_base) {
236 return -1;
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
242 Py_INCREF(new_base);
243 Py_INCREF(value);
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
249 type->tp_bases = value;
250 type->tp_base = new_base;
252 if (mro_internal(type) < 0) {
253 goto bail;
256 temp = PyList_New(0);
257 if (!temp)
258 goto bail;
260 r = mro_subclasses(type, temp);
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
272 Py_DECREF(temp);
273 goto bail;
276 Py_DECREF(temp);
278 /* any base that was in __bases__ but now isn't, we
279 need to remove |type| from its tp_subclasses.
280 conversely, any class now in __bases__ that wasn't
281 needs to have |type| added to its subclasses. */
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
302 update_all_slots(type);
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
308 return r;
310 bail:
311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
321 return -1;
324 static PyObject *
325 type_dict(PyTypeObject *type, void *context)
327 if (type->tp_dict == NULL) {
328 Py_INCREF(Py_None);
329 return Py_None;
331 return PyDictProxy_New(type->tp_dict);
334 static PyObject *
335 type_get_doc(PyTypeObject *type, void *context)
337 PyObject *result;
338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
339 return PyString_FromString(type->tp_doc);
340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
345 else if (result->ob_type->tp_descr_get) {
346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
349 else {
350 Py_INCREF(result);
352 return result;
355 static PyGetSetDef type_getsets[] = {
356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
359 {"__dict__", (getter)type_dict, NULL, NULL},
360 {"__doc__", (getter)type_get_doc, NULL, NULL},
364 static int
365 type_compare(PyObject *v, PyObject *w)
367 /* This is called with type objects only. So we
368 can just compare the addresses. */
369 Py_uintptr_t vv = (Py_uintptr_t)v;
370 Py_uintptr_t ww = (Py_uintptr_t)w;
371 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
374 static PyObject *
375 type_repr(PyTypeObject *type)
377 PyObject *mod, *name, *rtn;
378 char *kind;
380 mod = type_module(type, NULL);
381 if (mod == NULL)
382 PyErr_Clear();
383 else if (!PyString_Check(mod)) {
384 Py_DECREF(mod);
385 mod = NULL;
387 name = type_name(type, NULL);
388 if (name == NULL)
389 return NULL;
391 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
392 kind = "class";
393 else
394 kind = "type";
396 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
397 rtn = PyString_FromFormat("<%s '%s.%s'>",
398 kind,
399 PyString_AS_STRING(mod),
400 PyString_AS_STRING(name));
402 else
403 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
405 Py_XDECREF(mod);
406 Py_DECREF(name);
407 return rtn;
410 static PyObject *
411 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
413 PyObject *obj;
415 if (type->tp_new == NULL) {
416 PyErr_Format(PyExc_TypeError,
417 "cannot create '%.100s' instances",
418 type->tp_name);
419 return NULL;
422 obj = type->tp_new(type, args, kwds);
423 if (obj != NULL) {
424 /* Ugly exception: when the call was type(something),
425 don't call tp_init on the result. */
426 if (type == &PyType_Type &&
427 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
428 (kwds == NULL ||
429 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
430 return obj;
431 /* If the returned object is not an instance of type,
432 it won't be initialized. */
433 if (!PyType_IsSubtype(obj->ob_type, type))
434 return obj;
435 type = obj->ob_type;
436 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
437 type->tp_init != NULL &&
438 type->tp_init(obj, args, kwds) < 0) {
439 Py_DECREF(obj);
440 obj = NULL;
443 return obj;
446 PyObject *
447 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
449 PyObject *obj;
450 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
451 /* note that we need to add one, for the sentinel */
453 if (PyType_IS_GC(type))
454 obj = _PyObject_GC_Malloc(size);
455 else
456 obj = (PyObject *)PyObject_MALLOC(size);
458 if (obj == NULL)
459 return PyErr_NoMemory();
461 memset(obj, '\0', size);
463 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
464 Py_INCREF(type);
466 if (type->tp_itemsize == 0)
467 PyObject_INIT(obj, type);
468 else
469 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
471 if (PyType_IS_GC(type))
472 _PyObject_GC_TRACK(obj);
473 return obj;
476 PyObject *
477 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
479 return type->tp_alloc(type, 0);
482 /* Helpers for subtyping */
484 static int
485 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
487 Py_ssize_t i, n;
488 PyMemberDef *mp;
490 n = type->ob_size;
491 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
492 for (i = 0; i < n; i++, mp++) {
493 if (mp->type == T_OBJECT_EX) {
494 char *addr = (char *)self + mp->offset;
495 PyObject *obj = *(PyObject **)addr;
496 if (obj != NULL) {
497 int err = visit(obj, arg);
498 if (err)
499 return err;
503 return 0;
506 static int
507 subtype_traverse(PyObject *self, visitproc visit, void *arg)
509 PyTypeObject *type, *base;
510 traverseproc basetraverse;
512 /* Find the nearest base with a different tp_traverse,
513 and traverse slots while we're at it */
514 type = self->ob_type;
515 base = type;
516 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
517 if (base->ob_size) {
518 int err = traverse_slots(base, self, visit, arg);
519 if (err)
520 return err;
522 base = base->tp_base;
523 assert(base);
526 if (type->tp_dictoffset != base->tp_dictoffset) {
527 PyObject **dictptr = _PyObject_GetDictPtr(self);
528 if (dictptr && *dictptr)
529 Py_VISIT(*dictptr);
532 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
533 /* For a heaptype, the instances count as references
534 to the type. Traverse the type so the collector
535 can find cycles involving this link. */
536 Py_VISIT(type);
538 if (basetraverse)
539 return basetraverse(self, visit, arg);
540 return 0;
543 static void
544 clear_slots(PyTypeObject *type, PyObject *self)
546 Py_ssize_t i, n;
547 PyMemberDef *mp;
549 n = type->ob_size;
550 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
551 for (i = 0; i < n; i++, mp++) {
552 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
553 char *addr = (char *)self + mp->offset;
554 PyObject *obj = *(PyObject **)addr;
555 if (obj != NULL) {
556 *(PyObject **)addr = NULL;
557 Py_DECREF(obj);
563 static int
564 subtype_clear(PyObject *self)
566 PyTypeObject *type, *base;
567 inquiry baseclear;
569 /* Find the nearest base with a different tp_clear
570 and clear slots while we're at it */
571 type = self->ob_type;
572 base = type;
573 while ((baseclear = base->tp_clear) == subtype_clear) {
574 if (base->ob_size)
575 clear_slots(base, self);
576 base = base->tp_base;
577 assert(base);
580 /* There's no need to clear the instance dict (if any);
581 the collector will call its tp_clear handler. */
583 if (baseclear)
584 return baseclear(self);
585 return 0;
588 static void
589 subtype_dealloc(PyObject *self)
591 PyTypeObject *type, *base;
592 destructor basedealloc;
594 /* Extract the type; we expect it to be a heap type */
595 type = self->ob_type;
596 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
598 /* Test whether the type has GC exactly once */
600 if (!PyType_IS_GC(type)) {
601 /* It's really rare to find a dynamic type that doesn't have
602 GC; it can only happen when deriving from 'object' and not
603 adding any slots or instance variables. This allows
604 certain simplifications: there's no need to call
605 clear_slots(), or DECREF the dict, or clear weakrefs. */
607 /* Maybe call finalizer; exit early if resurrected */
608 if (type->tp_del) {
609 type->tp_del(self);
610 if (self->ob_refcnt > 0)
611 return;
614 /* Find the nearest base with a different tp_dealloc */
615 base = type;
616 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
617 assert(base->ob_size == 0);
618 base = base->tp_base;
619 assert(base);
622 /* Call the base tp_dealloc() */
623 assert(basedealloc);
624 basedealloc(self);
626 /* Can't reference self beyond this point */
627 Py_DECREF(type);
629 /* Done */
630 return;
633 /* We get here only if the type has GC */
635 /* UnTrack and re-Track around the trashcan macro, alas */
636 /* See explanation at end of function for full disclosure */
637 PyObject_GC_UnTrack(self);
638 ++_PyTrash_delete_nesting;
639 Py_TRASHCAN_SAFE_BEGIN(self);
640 --_PyTrash_delete_nesting;
641 /* DO NOT restore GC tracking at this point. weakref callbacks
642 * (if any, and whether directly here or indirectly in something we
643 * call) may trigger GC, and if self is tracked at that point, it
644 * will look like trash to GC and GC will try to delete self again.
647 /* Find the nearest base with a different tp_dealloc */
648 base = type;
649 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
650 base = base->tp_base;
651 assert(base);
654 /* If we added a weaklist, we clear it. Do this *before* calling
655 the finalizer (__del__), clearing slots, or clearing the instance
656 dict. */
658 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
659 PyObject_ClearWeakRefs(self);
661 /* Maybe call finalizer; exit early if resurrected */
662 if (type->tp_del) {
663 _PyObject_GC_TRACK(self);
664 type->tp_del(self);
665 if (self->ob_refcnt > 0)
666 goto endlabel; /* resurrected */
667 else
668 _PyObject_GC_UNTRACK(self);
671 /* Clear slots up to the nearest base with a different tp_dealloc */
672 base = type;
673 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
674 if (base->ob_size)
675 clear_slots(base, self);
676 base = base->tp_base;
677 assert(base);
680 /* If we added a dict, DECREF it */
681 if (type->tp_dictoffset && !base->tp_dictoffset) {
682 PyObject **dictptr = _PyObject_GetDictPtr(self);
683 if (dictptr != NULL) {
684 PyObject *dict = *dictptr;
685 if (dict != NULL) {
686 Py_DECREF(dict);
687 *dictptr = NULL;
692 /* Call the base tp_dealloc(); first retrack self if
693 * basedealloc knows about gc.
695 if (PyType_IS_GC(base))
696 _PyObject_GC_TRACK(self);
697 assert(basedealloc);
698 basedealloc(self);
700 /* Can't reference self beyond this point */
701 Py_DECREF(type);
703 endlabel:
704 ++_PyTrash_delete_nesting;
705 Py_TRASHCAN_SAFE_END(self);
706 --_PyTrash_delete_nesting;
708 /* Explanation of the weirdness around the trashcan macros:
710 Q. What do the trashcan macros do?
712 A. Read the comment titled "Trashcan mechanism" in object.h.
713 For one, this explains why there must be a call to GC-untrack
714 before the trashcan begin macro. Without understanding the
715 trashcan code, the answers to the following questions don't make
716 sense.
718 Q. Why do we GC-untrack before the trashcan and then immediately
719 GC-track again afterward?
721 A. In the case that the base class is GC-aware, the base class
722 probably GC-untracks the object. If it does that using the
723 UNTRACK macro, this will crash when the object is already
724 untracked. Because we don't know what the base class does, the
725 only safe thing is to make sure the object is tracked when we
726 call the base class dealloc. But... The trashcan begin macro
727 requires that the object is *untracked* before it is called. So
728 the dance becomes:
730 GC untrack
731 trashcan begin
732 GC track
734 Q. Why did the last question say "immediately GC-track again"?
735 It's nowhere near immediately.
737 A. Because the code *used* to re-track immediately. Bad Idea.
738 self has a refcount of 0, and if gc ever gets its hands on it
739 (which can happen if any weakref callback gets invoked), it
740 looks like trash to gc too, and gc also tries to delete self
741 then. But we're already deleting self. Double dealloction is
742 a subtle disaster.
744 Q. Why the bizarre (net-zero) manipulation of
745 _PyTrash_delete_nesting around the trashcan macros?
747 A. Some base classes (e.g. list) also use the trashcan mechanism.
748 The following scenario used to be possible:
750 - suppose the trashcan level is one below the trashcan limit
752 - subtype_dealloc() is called
754 - the trashcan limit is not yet reached, so the trashcan level
755 is incremented and the code between trashcan begin and end is
756 executed
758 - this destroys much of the object's contents, including its
759 slots and __dict__
761 - basedealloc() is called; this is really list_dealloc(), or
762 some other type which also uses the trashcan macros
764 - the trashcan limit is now reached, so the object is put on the
765 trashcan's to-be-deleted-later list
767 - basedealloc() returns
769 - subtype_dealloc() decrefs the object's type
771 - subtype_dealloc() returns
773 - later, the trashcan code starts deleting the objects from its
774 to-be-deleted-later list
776 - subtype_dealloc() is called *AGAIN* for the same object
778 - at the very least (if the destroyed slots and __dict__ don't
779 cause problems) the object's type gets decref'ed a second
780 time, which is *BAD*!!!
782 The remedy is to make sure that if the code between trashcan
783 begin and end in subtype_dealloc() is called, the code between
784 trashcan begin and end in basedealloc() will also be called.
785 This is done by decrementing the level after passing into the
786 trashcan block, and incrementing it just before leaving the
787 block.
789 But now it's possible that a chain of objects consisting solely
790 of objects whose deallocator is subtype_dealloc() will defeat
791 the trashcan mechanism completely: the decremented level means
792 that the effective level never reaches the limit. Therefore, we
793 *increment* the level *before* entering the trashcan block, and
794 matchingly decrement it after leaving. This means the trashcan
795 code will trigger a little early, but that's no big deal.
797 Q. Are there any live examples of code in need of all this
798 complexity?
800 A. Yes. See SF bug 668433 for code that crashed (when Python was
801 compiled in debug mode) before the trashcan level manipulations
802 were added. For more discussion, see SF patches 581742, 575073
803 and bug 574207.
807 static PyTypeObject *solid_base(PyTypeObject *type);
809 /* type test with subclassing support */
812 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
814 PyObject *mro;
816 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
817 return b == a || b == &PyBaseObject_Type;
819 mro = a->tp_mro;
820 if (mro != NULL) {
821 /* Deal with multiple inheritance without recursion
822 by walking the MRO tuple */
823 Py_ssize_t i, n;
824 assert(PyTuple_Check(mro));
825 n = PyTuple_GET_SIZE(mro);
826 for (i = 0; i < n; i++) {
827 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
828 return 1;
830 return 0;
832 else {
833 /* a is not completely initilized yet; follow tp_base */
834 do {
835 if (a == b)
836 return 1;
837 a = a->tp_base;
838 } while (a != NULL);
839 return b == &PyBaseObject_Type;
843 /* Internal routines to do a method lookup in the type
844 without looking in the instance dictionary
845 (so we can't use PyObject_GetAttr) but still binding
846 it to the instance. The arguments are the object,
847 the method name as a C string, and the address of a
848 static variable used to cache the interned Python string.
850 Two variants:
852 - lookup_maybe() returns NULL without raising an exception
853 when the _PyType_Lookup() call fails;
855 - lookup_method() always raises an exception upon errors.
858 static PyObject *
859 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
861 PyObject *res;
863 if (*attrobj == NULL) {
864 *attrobj = PyString_InternFromString(attrstr);
865 if (*attrobj == NULL)
866 return NULL;
868 res = _PyType_Lookup(self->ob_type, *attrobj);
869 if (res != NULL) {
870 descrgetfunc f;
871 if ((f = res->ob_type->tp_descr_get) == NULL)
872 Py_INCREF(res);
873 else
874 res = f(res, self, (PyObject *)(self->ob_type));
876 return res;
879 static PyObject *
880 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
882 PyObject *res = lookup_maybe(self, attrstr, attrobj);
883 if (res == NULL && !PyErr_Occurred())
884 PyErr_SetObject(PyExc_AttributeError, *attrobj);
885 return res;
888 /* A variation of PyObject_CallMethod that uses lookup_method()
889 instead of PyObject_GetAttrString(). This uses the same convention
890 as lookup_method to cache the interned name string object. */
892 static PyObject *
893 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
895 va_list va;
896 PyObject *args, *func = 0, *retval;
897 va_start(va, format);
899 func = lookup_maybe(o, name, nameobj);
900 if (func == NULL) {
901 va_end(va);
902 if (!PyErr_Occurred())
903 PyErr_SetObject(PyExc_AttributeError, *nameobj);
904 return NULL;
907 if (format && *format)
908 args = Py_VaBuildValue(format, va);
909 else
910 args = PyTuple_New(0);
912 va_end(va);
914 if (args == NULL)
915 return NULL;
917 assert(PyTuple_Check(args));
918 retval = PyObject_Call(func, args, NULL);
920 Py_DECREF(args);
921 Py_DECREF(func);
923 return retval;
926 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
928 static PyObject *
929 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
931 va_list va;
932 PyObject *args, *func = 0, *retval;
933 va_start(va, format);
935 func = lookup_maybe(o, name, nameobj);
936 if (func == NULL) {
937 va_end(va);
938 if (!PyErr_Occurred()) {
939 Py_INCREF(Py_NotImplemented);
940 return Py_NotImplemented;
942 return NULL;
945 if (format && *format)
946 args = Py_VaBuildValue(format, va);
947 else
948 args = PyTuple_New(0);
950 va_end(va);
952 if (args == NULL)
953 return NULL;
955 assert(PyTuple_Check(args));
956 retval = PyObject_Call(func, args, NULL);
958 Py_DECREF(args);
959 Py_DECREF(func);
961 return retval;
964 static int
965 fill_classic_mro(PyObject *mro, PyObject *cls)
967 PyObject *bases, *base;
968 Py_ssize_t i, n;
970 assert(PyList_Check(mro));
971 assert(PyClass_Check(cls));
972 i = PySequence_Contains(mro, cls);
973 if (i < 0)
974 return -1;
975 if (!i) {
976 if (PyList_Append(mro, cls) < 0)
977 return -1;
979 bases = ((PyClassObject *)cls)->cl_bases;
980 assert(bases && PyTuple_Check(bases));
981 n = PyTuple_GET_SIZE(bases);
982 for (i = 0; i < n; i++) {
983 base = PyTuple_GET_ITEM(bases, i);
984 if (fill_classic_mro(mro, base) < 0)
985 return -1;
987 return 0;
990 static PyObject *
991 classic_mro(PyObject *cls)
993 PyObject *mro;
995 assert(PyClass_Check(cls));
996 mro = PyList_New(0);
997 if (mro != NULL) {
998 if (fill_classic_mro(mro, cls) == 0)
999 return mro;
1000 Py_DECREF(mro);
1002 return NULL;
1006 Method resolution order algorithm C3 described in
1007 "A Monotonic Superclass Linearization for Dylan",
1008 by Kim Barrett, Bob Cassel, Paul Haahr,
1009 David A. Moon, Keith Playford, and P. Tucker Withington.
1010 (OOPSLA 1996)
1012 Some notes about the rules implied by C3:
1014 No duplicate bases.
1015 It isn't legal to repeat a class in a list of base classes.
1017 The next three properties are the 3 constraints in "C3".
1019 Local precendece order.
1020 If A precedes B in C's MRO, then A will precede B in the MRO of all
1021 subclasses of C.
1023 Monotonicity.
1024 The MRO of a class must be an extension without reordering of the
1025 MRO of each of its superclasses.
1027 Extended Precedence Graph (EPG).
1028 Linearization is consistent if there is a path in the EPG from
1029 each class to all its successors in the linearization. See
1030 the paper for definition of EPG.
1033 static int
1034 tail_contains(PyObject *list, int whence, PyObject *o) {
1035 Py_ssize_t j, size;
1036 size = PyList_GET_SIZE(list);
1038 for (j = whence+1; j < size; j++) {
1039 if (PyList_GET_ITEM(list, j) == o)
1040 return 1;
1042 return 0;
1045 static PyObject *
1046 class_name(PyObject *cls)
1048 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1049 if (name == NULL) {
1050 PyErr_Clear();
1051 Py_XDECREF(name);
1052 name = PyObject_Repr(cls);
1054 if (name == NULL)
1055 return NULL;
1056 if (!PyString_Check(name)) {
1057 Py_DECREF(name);
1058 return NULL;
1060 return name;
1063 static int
1064 check_duplicates(PyObject *list)
1066 Py_ssize_t i, j, n;
1067 /* Let's use a quadratic time algorithm,
1068 assuming that the bases lists is short.
1070 n = PyList_GET_SIZE(list);
1071 for (i = 0; i < n; i++) {
1072 PyObject *o = PyList_GET_ITEM(list, i);
1073 for (j = i + 1; j < n; j++) {
1074 if (PyList_GET_ITEM(list, j) == o) {
1075 o = class_name(o);
1076 PyErr_Format(PyExc_TypeError,
1077 "duplicate base class %s",
1078 o ? PyString_AS_STRING(o) : "?");
1079 Py_XDECREF(o);
1080 return -1;
1084 return 0;
1087 /* Raise a TypeError for an MRO order disagreement.
1089 It's hard to produce a good error message. In the absence of better
1090 insight into error reporting, report the classes that were candidates
1091 to be put next into the MRO. There is some conflict between the
1092 order in which they should be put in the MRO, but it's hard to
1093 diagnose what constraint can't be satisfied.
1096 static void
1097 set_mro_error(PyObject *to_merge, int *remain)
1099 Py_ssize_t i, n, off, to_merge_size;
1100 char buf[1000];
1101 PyObject *k, *v;
1102 PyObject *set = PyDict_New();
1103 if (!set) return;
1105 to_merge_size = PyList_GET_SIZE(to_merge);
1106 for (i = 0; i < to_merge_size; i++) {
1107 PyObject *L = PyList_GET_ITEM(to_merge, i);
1108 if (remain[i] < PyList_GET_SIZE(L)) {
1109 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1110 if (PyDict_SetItem(set, c, Py_None) < 0) {
1111 Py_DECREF(set);
1112 return;
1116 n = PyDict_Size(set);
1118 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1119 consistent method resolution\norder (MRO) for bases");
1120 i = 0;
1121 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1122 PyObject *name = class_name(k);
1123 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1124 name ? PyString_AS_STRING(name) : "?");
1125 Py_XDECREF(name);
1126 if (--n && (size_t)(off+1) < sizeof(buf)) {
1127 buf[off++] = ',';
1128 buf[off] = '\0';
1131 PyErr_SetString(PyExc_TypeError, buf);
1132 Py_DECREF(set);
1135 static int
1136 pmerge(PyObject *acc, PyObject* to_merge) {
1137 Py_ssize_t i, j, to_merge_size, empty_cnt;
1138 int *remain;
1139 int ok;
1141 to_merge_size = PyList_GET_SIZE(to_merge);
1143 /* remain stores an index into each sublist of to_merge.
1144 remain[i] is the index of the next base in to_merge[i]
1145 that is not included in acc.
1147 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1148 if (remain == NULL)
1149 return -1;
1150 for (i = 0; i < to_merge_size; i++)
1151 remain[i] = 0;
1153 again:
1154 empty_cnt = 0;
1155 for (i = 0; i < to_merge_size; i++) {
1156 PyObject *candidate;
1158 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1160 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1161 empty_cnt++;
1162 continue;
1165 /* Choose next candidate for MRO.
1167 The input sequences alone can determine the choice.
1168 If not, choose the class which appears in the MRO
1169 of the earliest direct superclass of the new class.
1172 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1173 for (j = 0; j < to_merge_size; j++) {
1174 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1175 if (tail_contains(j_lst, remain[j], candidate)) {
1176 goto skip; /* continue outer loop */
1179 ok = PyList_Append(acc, candidate);
1180 if (ok < 0) {
1181 PyMem_Free(remain);
1182 return -1;
1184 for (j = 0; j < to_merge_size; j++) {
1185 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1186 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1187 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1188 remain[j]++;
1191 goto again;
1192 skip: ;
1195 if (empty_cnt == to_merge_size) {
1196 PyMem_FREE(remain);
1197 return 0;
1199 set_mro_error(to_merge, remain);
1200 PyMem_FREE(remain);
1201 return -1;
1204 static PyObject *
1205 mro_implementation(PyTypeObject *type)
1207 Py_ssize_t i, n;
1208 int ok;
1209 PyObject *bases, *result;
1210 PyObject *to_merge, *bases_aslist;
1212 if(type->tp_dict == NULL) {
1213 if(PyType_Ready(type) < 0)
1214 return NULL;
1217 /* Find a superclass linearization that honors the constraints
1218 of the explicit lists of bases and the constraints implied by
1219 each base class.
1221 to_merge is a list of lists, where each list is a superclass
1222 linearization implied by a base class. The last element of
1223 to_merge is the declared list of bases.
1226 bases = type->tp_bases;
1227 n = PyTuple_GET_SIZE(bases);
1229 to_merge = PyList_New(n+1);
1230 if (to_merge == NULL)
1231 return NULL;
1233 for (i = 0; i < n; i++) {
1234 PyObject *base = PyTuple_GET_ITEM(bases, i);
1235 PyObject *parentMRO;
1236 if (PyType_Check(base))
1237 parentMRO = PySequence_List(
1238 ((PyTypeObject*)base)->tp_mro);
1239 else
1240 parentMRO = classic_mro(base);
1241 if (parentMRO == NULL) {
1242 Py_DECREF(to_merge);
1243 return NULL;
1246 PyList_SET_ITEM(to_merge, i, parentMRO);
1249 bases_aslist = PySequence_List(bases);
1250 if (bases_aslist == NULL) {
1251 Py_DECREF(to_merge);
1252 return NULL;
1254 /* This is just a basic sanity check. */
1255 if (check_duplicates(bases_aslist) < 0) {
1256 Py_DECREF(to_merge);
1257 Py_DECREF(bases_aslist);
1258 return NULL;
1260 PyList_SET_ITEM(to_merge, n, bases_aslist);
1262 result = Py_BuildValue("[O]", (PyObject *)type);
1263 if (result == NULL) {
1264 Py_DECREF(to_merge);
1265 return NULL;
1268 ok = pmerge(result, to_merge);
1269 Py_DECREF(to_merge);
1270 if (ok < 0) {
1271 Py_DECREF(result);
1272 return NULL;
1275 return result;
1278 static PyObject *
1279 mro_external(PyObject *self)
1281 PyTypeObject *type = (PyTypeObject *)self;
1283 return mro_implementation(type);
1286 static int
1287 mro_internal(PyTypeObject *type)
1289 PyObject *mro, *result, *tuple;
1290 int checkit = 0;
1292 if (type->ob_type == &PyType_Type) {
1293 result = mro_implementation(type);
1295 else {
1296 static PyObject *mro_str;
1297 checkit = 1;
1298 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1299 if (mro == NULL)
1300 return -1;
1301 result = PyObject_CallObject(mro, NULL);
1302 Py_DECREF(mro);
1304 if (result == NULL)
1305 return -1;
1306 tuple = PySequence_Tuple(result);
1307 Py_DECREF(result);
1308 if (tuple == NULL)
1309 return -1;
1310 if (checkit) {
1311 Py_ssize_t i, len;
1312 PyObject *cls;
1313 PyTypeObject *solid;
1315 solid = solid_base(type);
1317 len = PyTuple_GET_SIZE(tuple);
1319 for (i = 0; i < len; i++) {
1320 PyTypeObject *t;
1321 cls = PyTuple_GET_ITEM(tuple, i);
1322 if (PyClass_Check(cls))
1323 continue;
1324 else if (!PyType_Check(cls)) {
1325 PyErr_Format(PyExc_TypeError,
1326 "mro() returned a non-class ('%.500s')",
1327 cls->ob_type->tp_name);
1328 Py_DECREF(tuple);
1329 return -1;
1331 t = (PyTypeObject*)cls;
1332 if (!PyType_IsSubtype(solid, solid_base(t))) {
1333 PyErr_Format(PyExc_TypeError,
1334 "mro() returned base with unsuitable layout ('%.500s')",
1335 t->tp_name);
1336 Py_DECREF(tuple);
1337 return -1;
1341 type->tp_mro = tuple;
1342 return 0;
1346 /* Calculate the best base amongst multiple base classes.
1347 This is the first one that's on the path to the "solid base". */
1349 static PyTypeObject *
1350 best_base(PyObject *bases)
1352 Py_ssize_t i, n;
1353 PyTypeObject *base, *winner, *candidate, *base_i;
1354 PyObject *base_proto;
1356 assert(PyTuple_Check(bases));
1357 n = PyTuple_GET_SIZE(bases);
1358 assert(n > 0);
1359 base = NULL;
1360 winner = NULL;
1361 for (i = 0; i < n; i++) {
1362 base_proto = PyTuple_GET_ITEM(bases, i);
1363 if (PyClass_Check(base_proto))
1364 continue;
1365 if (!PyType_Check(base_proto)) {
1366 PyErr_SetString(
1367 PyExc_TypeError,
1368 "bases must be types");
1369 return NULL;
1371 base_i = (PyTypeObject *)base_proto;
1372 if (base_i->tp_dict == NULL) {
1373 if (PyType_Ready(base_i) < 0)
1374 return NULL;
1376 candidate = solid_base(base_i);
1377 if (winner == NULL) {
1378 winner = candidate;
1379 base = base_i;
1381 else if (PyType_IsSubtype(winner, candidate))
1383 else if (PyType_IsSubtype(candidate, winner)) {
1384 winner = candidate;
1385 base = base_i;
1387 else {
1388 PyErr_SetString(
1389 PyExc_TypeError,
1390 "multiple bases have "
1391 "instance lay-out conflict");
1392 return NULL;
1395 if (base == NULL)
1396 PyErr_SetString(PyExc_TypeError,
1397 "a new-style class can't have only classic bases");
1398 return base;
1401 static int
1402 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1404 size_t t_size = type->tp_basicsize;
1405 size_t b_size = base->tp_basicsize;
1407 assert(t_size >= b_size); /* Else type smaller than base! */
1408 if (type->tp_itemsize || base->tp_itemsize) {
1409 /* If itemsize is involved, stricter rules */
1410 return t_size != b_size ||
1411 type->tp_itemsize != base->tp_itemsize;
1413 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1414 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1415 t_size -= sizeof(PyObject *);
1416 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1417 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1418 t_size -= sizeof(PyObject *);
1420 return t_size != b_size;
1423 static PyTypeObject *
1424 solid_base(PyTypeObject *type)
1426 PyTypeObject *base;
1428 if (type->tp_base)
1429 base = solid_base(type->tp_base);
1430 else
1431 base = &PyBaseObject_Type;
1432 if (extra_ivars(type, base))
1433 return type;
1434 else
1435 return base;
1438 static void object_dealloc(PyObject *);
1439 static int object_init(PyObject *, PyObject *, PyObject *);
1440 static int update_slot(PyTypeObject *, PyObject *);
1441 static void fixup_slot_dispatchers(PyTypeObject *);
1443 static PyObject *
1444 subtype_dict(PyObject *obj, void *context)
1446 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1447 PyObject *dict;
1449 if (dictptr == NULL) {
1450 PyErr_SetString(PyExc_AttributeError,
1451 "This object has no __dict__");
1452 return NULL;
1454 dict = *dictptr;
1455 if (dict == NULL)
1456 *dictptr = dict = PyDict_New();
1457 Py_XINCREF(dict);
1458 return dict;
1461 static int
1462 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1464 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1465 PyObject *dict;
1467 if (dictptr == NULL) {
1468 PyErr_SetString(PyExc_AttributeError,
1469 "This object has no __dict__");
1470 return -1;
1472 if (value != NULL && !PyDict_Check(value)) {
1473 PyErr_Format(PyExc_TypeError,
1474 "__dict__ must be set to a dictionary, "
1475 "not a '%.200s'", value->ob_type->tp_name);
1476 return -1;
1478 dict = *dictptr;
1479 Py_XINCREF(value);
1480 *dictptr = value;
1481 Py_XDECREF(dict);
1482 return 0;
1485 static PyObject *
1486 subtype_getweakref(PyObject *obj, void *context)
1488 PyObject **weaklistptr;
1489 PyObject *result;
1491 if (obj->ob_type->tp_weaklistoffset == 0) {
1492 PyErr_SetString(PyExc_AttributeError,
1493 "This object has no __weaklist__");
1494 return NULL;
1496 assert(obj->ob_type->tp_weaklistoffset > 0);
1497 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1498 (size_t)(obj->ob_type->tp_basicsize));
1499 weaklistptr = (PyObject **)
1500 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1501 if (*weaklistptr == NULL)
1502 result = Py_None;
1503 else
1504 result = *weaklistptr;
1505 Py_INCREF(result);
1506 return result;
1509 /* Three variants on the subtype_getsets list. */
1511 static PyGetSetDef subtype_getsets_full[] = {
1512 {"__dict__", subtype_dict, subtype_setdict,
1513 PyDoc_STR("dictionary for instance variables (if defined)")},
1514 {"__weakref__", subtype_getweakref, NULL,
1515 PyDoc_STR("list of weak references to the object (if defined)")},
1519 static PyGetSetDef subtype_getsets_dict_only[] = {
1520 {"__dict__", subtype_dict, subtype_setdict,
1521 PyDoc_STR("dictionary for instance variables (if defined)")},
1525 static PyGetSetDef subtype_getsets_weakref_only[] = {
1526 {"__weakref__", subtype_getweakref, NULL,
1527 PyDoc_STR("list of weak references to the object (if defined)")},
1531 static int
1532 valid_identifier(PyObject *s)
1534 unsigned char *p;
1535 Py_ssize_t i, n;
1537 if (!PyString_Check(s)) {
1538 PyErr_Format(PyExc_TypeError,
1539 "__slots__ items must be strings, not '%.200s'",
1540 s->ob_type->tp_name);
1541 return 0;
1543 p = (unsigned char *) PyString_AS_STRING(s);
1544 n = PyString_GET_SIZE(s);
1545 /* We must reject an empty name. As a hack, we bump the
1546 length to 1 so that the loop will balk on the trailing \0. */
1547 if (n == 0)
1548 n = 1;
1549 for (i = 0; i < n; i++, p++) {
1550 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1551 PyErr_SetString(PyExc_TypeError,
1552 "__slots__ must be identifiers");
1553 return 0;
1556 return 1;
1559 #ifdef Py_USING_UNICODE
1560 /* Replace Unicode objects in slots. */
1562 static PyObject *
1563 _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
1565 PyObject *tmp = slots;
1566 PyObject *o, *o1;
1567 Py_ssize_t i;
1568 ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1569 for (i = 0; i < nslots; i++) {
1570 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1571 if (tmp == slots) {
1572 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1573 if (tmp == NULL)
1574 return NULL;
1576 o1 = _PyUnicode_AsDefaultEncodedString
1577 (o, NULL);
1578 if (o1 == NULL) {
1579 Py_DECREF(tmp);
1580 return 0;
1582 Py_INCREF(o1);
1583 Py_DECREF(o);
1584 PyTuple_SET_ITEM(tmp, i, o1);
1587 return tmp;
1589 #endif
1591 static PyObject *
1592 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1594 PyObject *name, *bases, *dict;
1595 static char *kwlist[] = {"name", "bases", "dict", 0};
1596 PyObject *slots, *tmp, *newslots;
1597 PyTypeObject *type, *base, *tmptype, *winner;
1598 PyHeapTypeObject *et;
1599 PyMemberDef *mp;
1600 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
1601 int j, may_add_dict, may_add_weak;
1603 assert(args != NULL && PyTuple_Check(args));
1604 assert(kwds == NULL || PyDict_Check(kwds));
1606 /* Special case: type(x) should return x->ob_type */
1608 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1609 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1611 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1612 PyObject *x = PyTuple_GET_ITEM(args, 0);
1613 Py_INCREF(x->ob_type);
1614 return (PyObject *) x->ob_type;
1617 /* SF bug 475327 -- if that didn't trigger, we need 3
1618 arguments. but PyArg_ParseTupleAndKeywords below may give
1619 a msg saying type() needs exactly 3. */
1620 if (nargs + nkwds != 3) {
1621 PyErr_SetString(PyExc_TypeError,
1622 "type() takes 1 or 3 arguments");
1623 return NULL;
1627 /* Check arguments: (name, bases, dict) */
1628 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1629 &name,
1630 &PyTuple_Type, &bases,
1631 &PyDict_Type, &dict))
1632 return NULL;
1634 /* Determine the proper metatype to deal with this,
1635 and check for metatype conflicts while we're at it.
1636 Note that if some other metatype wins to contract,
1637 it's possible that its instances are not types. */
1638 nbases = PyTuple_GET_SIZE(bases);
1639 winner = metatype;
1640 for (i = 0; i < nbases; i++) {
1641 tmp = PyTuple_GET_ITEM(bases, i);
1642 tmptype = tmp->ob_type;
1643 if (tmptype == &PyClass_Type)
1644 continue; /* Special case classic classes */
1645 if (PyType_IsSubtype(winner, tmptype))
1646 continue;
1647 if (PyType_IsSubtype(tmptype, winner)) {
1648 winner = tmptype;
1649 continue;
1651 PyErr_SetString(PyExc_TypeError,
1652 "metaclass conflict: "
1653 "the metaclass of a derived class "
1654 "must be a (non-strict) subclass "
1655 "of the metaclasses of all its bases");
1656 return NULL;
1658 if (winner != metatype) {
1659 if (winner->tp_new != type_new) /* Pass it to the winner */
1660 return winner->tp_new(winner, args, kwds);
1661 metatype = winner;
1664 /* Adjust for empty tuple bases */
1665 if (nbases == 0) {
1666 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1667 if (bases == NULL)
1668 return NULL;
1669 nbases = 1;
1671 else
1672 Py_INCREF(bases);
1674 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1676 /* Calculate best base, and check that all bases are type objects */
1677 base = best_base(bases);
1678 if (base == NULL) {
1679 Py_DECREF(bases);
1680 return NULL;
1682 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1683 PyErr_Format(PyExc_TypeError,
1684 "type '%.100s' is not an acceptable base type",
1685 base->tp_name);
1686 Py_DECREF(bases);
1687 return NULL;
1690 /* Check for a __slots__ sequence variable in dict, and count it */
1691 slots = PyDict_GetItemString(dict, "__slots__");
1692 nslots = 0;
1693 add_dict = 0;
1694 add_weak = 0;
1695 may_add_dict = base->tp_dictoffset == 0;
1696 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1697 if (slots == NULL) {
1698 if (may_add_dict) {
1699 add_dict++;
1701 if (may_add_weak) {
1702 add_weak++;
1705 else {
1706 /* Have slots */
1708 /* Make it into a tuple */
1709 if (PyString_Check(slots))
1710 slots = PyTuple_Pack(1, slots);
1711 else
1712 slots = PySequence_Tuple(slots);
1713 if (slots == NULL) {
1714 Py_DECREF(bases);
1715 return NULL;
1717 assert(PyTuple_Check(slots));
1719 /* Are slots allowed? */
1720 nslots = PyTuple_GET_SIZE(slots);
1721 if (nslots > 0 && base->tp_itemsize != 0) {
1722 PyErr_Format(PyExc_TypeError,
1723 "nonempty __slots__ "
1724 "not supported for subtype of '%s'",
1725 base->tp_name);
1726 bad_slots:
1727 Py_DECREF(bases);
1728 Py_DECREF(slots);
1729 return NULL;
1732 #ifdef Py_USING_UNICODE
1733 tmp = _unicode_to_string(slots, nslots);
1734 if (tmp != slots) {
1735 Py_DECREF(slots);
1736 slots = tmp;
1738 if (!tmp)
1739 return NULL;
1740 #endif
1741 /* Check for valid slot names and two special cases */
1742 for (i = 0; i < nslots; i++) {
1743 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1744 char *s;
1745 if (!valid_identifier(tmp))
1746 goto bad_slots;
1747 assert(PyString_Check(tmp));
1748 s = PyString_AS_STRING(tmp);
1749 if (strcmp(s, "__dict__") == 0) {
1750 if (!may_add_dict || add_dict) {
1751 PyErr_SetString(PyExc_TypeError,
1752 "__dict__ slot disallowed: "
1753 "we already got one");
1754 goto bad_slots;
1756 add_dict++;
1758 if (strcmp(s, "__weakref__") == 0) {
1759 if (!may_add_weak || add_weak) {
1760 PyErr_SetString(PyExc_TypeError,
1761 "__weakref__ slot disallowed: "
1762 "either we already got one, "
1763 "or __itemsize__ != 0");
1764 goto bad_slots;
1766 add_weak++;
1770 /* Copy slots into yet another tuple, demangling names */
1771 newslots = PyTuple_New(nslots - add_dict - add_weak);
1772 if (newslots == NULL)
1773 goto bad_slots;
1774 for (i = j = 0; i < nslots; i++) {
1775 char *s;
1776 tmp = PyTuple_GET_ITEM(slots, i);
1777 s = PyString_AS_STRING(tmp);
1778 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1779 (add_weak && strcmp(s, "__weakref__") == 0))
1780 continue;
1781 tmp =_Py_Mangle(name, tmp);
1782 if (!tmp)
1783 goto bad_slots;
1784 PyTuple_SET_ITEM(newslots, j, tmp);
1785 j++;
1787 assert(j == nslots - add_dict - add_weak);
1788 nslots = j;
1789 Py_DECREF(slots);
1790 slots = newslots;
1792 /* Secondary bases may provide weakrefs or dict */
1793 if (nbases > 1 &&
1794 ((may_add_dict && !add_dict) ||
1795 (may_add_weak && !add_weak))) {
1796 for (i = 0; i < nbases; i++) {
1797 tmp = PyTuple_GET_ITEM(bases, i);
1798 if (tmp == (PyObject *)base)
1799 continue; /* Skip primary base */
1800 if (PyClass_Check(tmp)) {
1801 /* Classic base class provides both */
1802 if (may_add_dict && !add_dict)
1803 add_dict++;
1804 if (may_add_weak && !add_weak)
1805 add_weak++;
1806 break;
1808 assert(PyType_Check(tmp));
1809 tmptype = (PyTypeObject *)tmp;
1810 if (may_add_dict && !add_dict &&
1811 tmptype->tp_dictoffset != 0)
1812 add_dict++;
1813 if (may_add_weak && !add_weak &&
1814 tmptype->tp_weaklistoffset != 0)
1815 add_weak++;
1816 if (may_add_dict && !add_dict)
1817 continue;
1818 if (may_add_weak && !add_weak)
1819 continue;
1820 /* Nothing more to check */
1821 break;
1826 /* XXX From here until type is safely allocated,
1827 "return NULL" may leak slots! */
1829 /* Allocate the type object */
1830 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1831 if (type == NULL) {
1832 Py_XDECREF(slots);
1833 Py_DECREF(bases);
1834 return NULL;
1837 /* Keep name and slots alive in the extended type object */
1838 et = (PyHeapTypeObject *)type;
1839 Py_INCREF(name);
1840 et->ht_name = name;
1841 et->ht_slots = slots;
1843 /* Initialize tp_flags */
1844 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1845 Py_TPFLAGS_BASETYPE;
1846 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1847 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1849 /* It's a new-style number unless it specifically inherits any
1850 old-style numeric behavior */
1851 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1852 (base->tp_as_number == NULL))
1853 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1855 /* Initialize essential fields */
1856 type->tp_as_number = &et->as_number;
1857 type->tp_as_sequence = &et->as_sequence;
1858 type->tp_as_mapping = &et->as_mapping;
1859 type->tp_as_buffer = &et->as_buffer;
1860 type->tp_name = PyString_AS_STRING(name);
1862 /* Set tp_base and tp_bases */
1863 type->tp_bases = bases;
1864 Py_INCREF(base);
1865 type->tp_base = base;
1867 /* Initialize tp_dict from passed-in dict */
1868 type->tp_dict = dict = PyDict_Copy(dict);
1869 if (dict == NULL) {
1870 Py_DECREF(type);
1871 return NULL;
1874 /* Set __module__ in the dict */
1875 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1876 tmp = PyEval_GetGlobals();
1877 if (tmp != NULL) {
1878 tmp = PyDict_GetItemString(tmp, "__name__");
1879 if (tmp != NULL) {
1880 if (PyDict_SetItemString(dict, "__module__",
1881 tmp) < 0)
1882 return NULL;
1887 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1888 and is a string. The __doc__ accessor will first look for tp_doc;
1889 if that fails, it will still look into __dict__.
1892 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1893 if (doc != NULL && PyString_Check(doc)) {
1894 const size_t n = (size_t)PyString_GET_SIZE(doc);
1895 char *tp_doc = (char *)PyObject_MALLOC(n+1);
1896 if (tp_doc == NULL) {
1897 Py_DECREF(type);
1898 return NULL;
1900 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1901 type->tp_doc = tp_doc;
1905 /* Special-case __new__: if it's a plain function,
1906 make it a static function */
1907 tmp = PyDict_GetItemString(dict, "__new__");
1908 if (tmp != NULL && PyFunction_Check(tmp)) {
1909 tmp = PyStaticMethod_New(tmp);
1910 if (tmp == NULL) {
1911 Py_DECREF(type);
1912 return NULL;
1914 PyDict_SetItemString(dict, "__new__", tmp);
1915 Py_DECREF(tmp);
1918 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1919 mp = PyHeapType_GET_MEMBERS(et);
1920 slotoffset = base->tp_basicsize;
1921 if (slots != NULL) {
1922 for (i = 0; i < nslots; i++, mp++) {
1923 mp->name = PyString_AS_STRING(
1924 PyTuple_GET_ITEM(slots, i));
1925 mp->type = T_OBJECT_EX;
1926 mp->offset = slotoffset;
1927 if (base->tp_weaklistoffset == 0 &&
1928 strcmp(mp->name, "__weakref__") == 0) {
1929 add_weak++;
1930 mp->type = T_OBJECT;
1931 mp->flags = READONLY;
1932 type->tp_weaklistoffset = slotoffset;
1934 slotoffset += sizeof(PyObject *);
1937 if (add_dict) {
1938 if (base->tp_itemsize)
1939 type->tp_dictoffset = -(long)sizeof(PyObject *);
1940 else
1941 type->tp_dictoffset = slotoffset;
1942 slotoffset += sizeof(PyObject *);
1944 if (add_weak) {
1945 assert(!base->tp_itemsize);
1946 type->tp_weaklistoffset = slotoffset;
1947 slotoffset += sizeof(PyObject *);
1949 type->tp_basicsize = slotoffset;
1950 type->tp_itemsize = base->tp_itemsize;
1951 type->tp_members = PyHeapType_GET_MEMBERS(et);
1953 if (type->tp_weaklistoffset && type->tp_dictoffset)
1954 type->tp_getset = subtype_getsets_full;
1955 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1956 type->tp_getset = subtype_getsets_weakref_only;
1957 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1958 type->tp_getset = subtype_getsets_dict_only;
1959 else
1960 type->tp_getset = NULL;
1962 /* Special case some slots */
1963 if (type->tp_dictoffset != 0 || nslots > 0) {
1964 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1965 type->tp_getattro = PyObject_GenericGetAttr;
1966 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1967 type->tp_setattro = PyObject_GenericSetAttr;
1969 type->tp_dealloc = subtype_dealloc;
1971 /* Enable GC unless there are really no instance variables possible */
1972 if (!(type->tp_basicsize == sizeof(PyObject) &&
1973 type->tp_itemsize == 0))
1974 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1976 /* Always override allocation strategy to use regular heap */
1977 type->tp_alloc = PyType_GenericAlloc;
1978 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1979 type->tp_free = PyObject_GC_Del;
1980 type->tp_traverse = subtype_traverse;
1981 type->tp_clear = subtype_clear;
1983 else
1984 type->tp_free = PyObject_Del;
1986 /* Initialize the rest */
1987 if (PyType_Ready(type) < 0) {
1988 Py_DECREF(type);
1989 return NULL;
1992 /* Put the proper slots in place */
1993 fixup_slot_dispatchers(type);
1995 return (PyObject *)type;
1998 /* Internal API to look for a name through the MRO.
1999 This returns a borrowed reference, and doesn't set an exception! */
2000 PyObject *
2001 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2003 Py_ssize_t i, n;
2004 PyObject *mro, *res, *base, *dict;
2006 /* Look in tp_dict of types in MRO */
2007 mro = type->tp_mro;
2009 /* If mro is NULL, the type is either not yet initialized
2010 by PyType_Ready(), or already cleared by type_clear().
2011 Either way the safest thing to do is to return NULL. */
2012 if (mro == NULL)
2013 return NULL;
2015 assert(PyTuple_Check(mro));
2016 n = PyTuple_GET_SIZE(mro);
2017 for (i = 0; i < n; i++) {
2018 base = PyTuple_GET_ITEM(mro, i);
2019 if (PyClass_Check(base))
2020 dict = ((PyClassObject *)base)->cl_dict;
2021 else {
2022 assert(PyType_Check(base));
2023 dict = ((PyTypeObject *)base)->tp_dict;
2025 assert(dict && PyDict_Check(dict));
2026 res = PyDict_GetItem(dict, name);
2027 if (res != NULL)
2028 return res;
2030 return NULL;
2033 /* This is similar to PyObject_GenericGetAttr(),
2034 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2035 static PyObject *
2036 type_getattro(PyTypeObject *type, PyObject *name)
2038 PyTypeObject *metatype = type->ob_type;
2039 PyObject *meta_attribute, *attribute;
2040 descrgetfunc meta_get;
2042 /* Initialize this type (we'll assume the metatype is initialized) */
2043 if (type->tp_dict == NULL) {
2044 if (PyType_Ready(type) < 0)
2045 return NULL;
2048 /* No readable descriptor found yet */
2049 meta_get = NULL;
2051 /* Look for the attribute in the metatype */
2052 meta_attribute = _PyType_Lookup(metatype, name);
2054 if (meta_attribute != NULL) {
2055 meta_get = meta_attribute->ob_type->tp_descr_get;
2057 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2058 /* Data descriptors implement tp_descr_set to intercept
2059 * writes. Assume the attribute is not overridden in
2060 * type's tp_dict (and bases): call the descriptor now.
2062 return meta_get(meta_attribute, (PyObject *)type,
2063 (PyObject *)metatype);
2065 Py_INCREF(meta_attribute);
2068 /* No data descriptor found on metatype. Look in tp_dict of this
2069 * type and its bases */
2070 attribute = _PyType_Lookup(type, name);
2071 if (attribute != NULL) {
2072 /* Implement descriptor functionality, if any */
2073 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2075 Py_XDECREF(meta_attribute);
2077 if (local_get != NULL) {
2078 /* NULL 2nd argument indicates the descriptor was
2079 * found on the target object itself (or a base) */
2080 return local_get(attribute, (PyObject *)NULL,
2081 (PyObject *)type);
2084 Py_INCREF(attribute);
2085 return attribute;
2088 /* No attribute found in local __dict__ (or bases): use the
2089 * descriptor from the metatype, if any */
2090 if (meta_get != NULL) {
2091 PyObject *res;
2092 res = meta_get(meta_attribute, (PyObject *)type,
2093 (PyObject *)metatype);
2094 Py_DECREF(meta_attribute);
2095 return res;
2098 /* If an ordinary attribute was found on the metatype, return it now */
2099 if (meta_attribute != NULL) {
2100 return meta_attribute;
2103 /* Give up */
2104 PyErr_Format(PyExc_AttributeError,
2105 "type object '%.50s' has no attribute '%.400s'",
2106 type->tp_name, PyString_AS_STRING(name));
2107 return NULL;
2110 static int
2111 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2113 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2114 PyErr_Format(
2115 PyExc_TypeError,
2116 "can't set attributes of built-in/extension type '%s'",
2117 type->tp_name);
2118 return -1;
2120 /* XXX Example of how I expect this to be used...
2121 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2122 return -1;
2124 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2125 return -1;
2126 return update_slot(type, name);
2129 static void
2130 type_dealloc(PyTypeObject *type)
2132 PyHeapTypeObject *et;
2134 /* Assert this is a heap-allocated type object */
2135 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2136 _PyObject_GC_UNTRACK(type);
2137 PyObject_ClearWeakRefs((PyObject *)type);
2138 et = (PyHeapTypeObject *)type;
2139 Py_XDECREF(type->tp_base);
2140 Py_XDECREF(type->tp_dict);
2141 Py_XDECREF(type->tp_bases);
2142 Py_XDECREF(type->tp_mro);
2143 Py_XDECREF(type->tp_cache);
2144 Py_XDECREF(type->tp_subclasses);
2145 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2146 * of most other objects. It's okay to cast it to char *.
2148 PyObject_Free((char *)type->tp_doc);
2149 Py_XDECREF(et->ht_name);
2150 Py_XDECREF(et->ht_slots);
2151 type->ob_type->tp_free((PyObject *)type);
2154 static PyObject *
2155 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2157 PyObject *list, *raw, *ref;
2158 Py_ssize_t i, n;
2160 list = PyList_New(0);
2161 if (list == NULL)
2162 return NULL;
2163 raw = type->tp_subclasses;
2164 if (raw == NULL)
2165 return list;
2166 assert(PyList_Check(raw));
2167 n = PyList_GET_SIZE(raw);
2168 for (i = 0; i < n; i++) {
2169 ref = PyList_GET_ITEM(raw, i);
2170 assert(PyWeakref_CheckRef(ref));
2171 ref = PyWeakref_GET_OBJECT(ref);
2172 if (ref != Py_None) {
2173 if (PyList_Append(list, ref) < 0) {
2174 Py_DECREF(list);
2175 return NULL;
2179 return list;
2182 static PyMethodDef type_methods[] = {
2183 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2184 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2185 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2186 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2190 PyDoc_STRVAR(type_doc,
2191 "type(object) -> the object's type\n"
2192 "type(name, bases, dict) -> a new type");
2194 static int
2195 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2197 /* Because of type_is_gc(), the collector only calls this
2198 for heaptypes. */
2199 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2201 Py_VISIT(type->tp_dict);
2202 Py_VISIT(type->tp_cache);
2203 Py_VISIT(type->tp_mro);
2204 Py_VISIT(type->tp_bases);
2205 Py_VISIT(type->tp_base);
2207 /* There's no need to visit type->tp_subclasses or
2208 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2209 in cycles; tp_subclasses is a list of weak references,
2210 and slots is a tuple of strings. */
2212 return 0;
2215 static int
2216 type_clear(PyTypeObject *type)
2218 /* Because of type_is_gc(), the collector only calls this
2219 for heaptypes. */
2220 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2222 /* The only field we need to clear is tp_mro, which is part of a
2223 hard cycle (its first element is the class itself) that won't
2224 be broken otherwise (it's a tuple and tuples don't have a
2225 tp_clear handler). None of the other fields need to be
2226 cleared, and here's why:
2228 tp_dict:
2229 It is a dict, so the collector will call its tp_clear.
2231 tp_cache:
2232 Not used; if it were, it would be a dict.
2234 tp_bases, tp_base:
2235 If these are involved in a cycle, there must be at least
2236 one other, mutable object in the cycle, e.g. a base
2237 class's dict; the cycle will be broken that way.
2239 tp_subclasses:
2240 A list of weak references can't be part of a cycle; and
2241 lists have their own tp_clear.
2243 slots (in PyHeapTypeObject):
2244 A tuple of strings can't be part of a cycle.
2247 Py_CLEAR(type->tp_mro);
2249 return 0;
2252 static int
2253 type_is_gc(PyTypeObject *type)
2255 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2258 PyTypeObject PyType_Type = {
2259 PyObject_HEAD_INIT(&PyType_Type)
2260 0, /* ob_size */
2261 "type", /* tp_name */
2262 sizeof(PyHeapTypeObject), /* tp_basicsize */
2263 sizeof(PyMemberDef), /* tp_itemsize */
2264 (destructor)type_dealloc, /* tp_dealloc */
2265 0, /* tp_print */
2266 0, /* tp_getattr */
2267 0, /* tp_setattr */
2268 type_compare, /* tp_compare */
2269 (reprfunc)type_repr, /* tp_repr */
2270 0, /* tp_as_number */
2271 0, /* tp_as_sequence */
2272 0, /* tp_as_mapping */
2273 (hashfunc)_Py_HashPointer, /* tp_hash */
2274 (ternaryfunc)type_call, /* tp_call */
2275 0, /* tp_str */
2276 (getattrofunc)type_getattro, /* tp_getattro */
2277 (setattrofunc)type_setattro, /* tp_setattro */
2278 0, /* tp_as_buffer */
2279 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2280 Py_TPFLAGS_BASETYPE, /* tp_flags */
2281 type_doc, /* tp_doc */
2282 (traverseproc)type_traverse, /* tp_traverse */
2283 (inquiry)type_clear, /* tp_clear */
2284 0, /* tp_richcompare */
2285 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2286 0, /* tp_iter */
2287 0, /* tp_iternext */
2288 type_methods, /* tp_methods */
2289 type_members, /* tp_members */
2290 type_getsets, /* tp_getset */
2291 0, /* tp_base */
2292 0, /* tp_dict */
2293 0, /* tp_descr_get */
2294 0, /* tp_descr_set */
2295 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2296 0, /* tp_init */
2297 0, /* tp_alloc */
2298 type_new, /* tp_new */
2299 PyObject_GC_Del, /* tp_free */
2300 (inquiry)type_is_gc, /* tp_is_gc */
2304 /* The base type of all types (eventually)... except itself. */
2306 static int
2307 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2309 return 0;
2312 /* If we don't have a tp_new for a new-style class, new will use this one.
2313 Therefore this should take no arguments/keywords. However, this new may
2314 also be inherited by objects that define a tp_init but no tp_new. These
2315 objects WILL pass argumets to tp_new, because it gets the same args as
2316 tp_init. So only allow arguments if we aren't using the default init, in
2317 which case we expect init to handle argument parsing. */
2318 static PyObject *
2319 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2321 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2322 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2323 PyErr_SetString(PyExc_TypeError,
2324 "default __new__ takes no parameters");
2325 return NULL;
2327 return type->tp_alloc(type, 0);
2330 static void
2331 object_dealloc(PyObject *self)
2333 self->ob_type->tp_free(self);
2336 static PyObject *
2337 object_repr(PyObject *self)
2339 PyTypeObject *type;
2340 PyObject *mod, *name, *rtn;
2342 type = self->ob_type;
2343 mod = type_module(type, NULL);
2344 if (mod == NULL)
2345 PyErr_Clear();
2346 else if (!PyString_Check(mod)) {
2347 Py_DECREF(mod);
2348 mod = NULL;
2350 name = type_name(type, NULL);
2351 if (name == NULL)
2352 return NULL;
2353 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2354 rtn = PyString_FromFormat("<%s.%s object at %p>",
2355 PyString_AS_STRING(mod),
2356 PyString_AS_STRING(name),
2357 self);
2358 else
2359 rtn = PyString_FromFormat("<%s object at %p>",
2360 type->tp_name, self);
2361 Py_XDECREF(mod);
2362 Py_DECREF(name);
2363 return rtn;
2366 static PyObject *
2367 object_str(PyObject *self)
2369 unaryfunc f;
2371 f = self->ob_type->tp_repr;
2372 if (f == NULL)
2373 f = object_repr;
2374 return f(self);
2377 static long
2378 object_hash(PyObject *self)
2380 return _Py_HashPointer(self);
2383 static PyObject *
2384 object_get_class(PyObject *self, void *closure)
2386 Py_INCREF(self->ob_type);
2387 return (PyObject *)(self->ob_type);
2390 static int
2391 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2393 return a == b ||
2394 (a != NULL &&
2395 b != NULL &&
2396 a->tp_basicsize == b->tp_basicsize &&
2397 a->tp_itemsize == b->tp_itemsize &&
2398 a->tp_dictoffset == b->tp_dictoffset &&
2399 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2400 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2401 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2404 static int
2405 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2407 PyTypeObject *base = a->tp_base;
2408 Py_ssize_t size;
2410 if (base != b->tp_base)
2411 return 0;
2412 if (equiv_structs(a, base) && equiv_structs(b, base))
2413 return 1;
2414 size = base->tp_basicsize;
2415 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2416 size += sizeof(PyObject *);
2417 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2418 size += sizeof(PyObject *);
2419 return size == a->tp_basicsize && size == b->tp_basicsize;
2422 static int
2423 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
2425 PyTypeObject *newbase, *oldbase;
2427 if (newto->tp_dealloc != oldto->tp_dealloc ||
2428 newto->tp_free != oldto->tp_free)
2430 PyErr_Format(PyExc_TypeError,
2431 "%s assignment: "
2432 "'%s' deallocator differs from '%s'",
2433 attr,
2434 newto->tp_name,
2435 oldto->tp_name);
2436 return 0;
2438 newbase = newto;
2439 oldbase = oldto;
2440 while (equiv_structs(newbase, newbase->tp_base))
2441 newbase = newbase->tp_base;
2442 while (equiv_structs(oldbase, oldbase->tp_base))
2443 oldbase = oldbase->tp_base;
2444 if (newbase != oldbase &&
2445 (newbase->tp_base != oldbase->tp_base ||
2446 !same_slots_added(newbase, oldbase))) {
2447 PyErr_Format(PyExc_TypeError,
2448 "%s assignment: "
2449 "'%s' object layout differs from '%s'",
2450 attr,
2451 newto->tp_name,
2452 oldto->tp_name);
2453 return 0;
2456 return 1;
2459 static int
2460 object_set_class(PyObject *self, PyObject *value, void *closure)
2462 PyTypeObject *oldto = self->ob_type;
2463 PyTypeObject *newto;
2465 if (value == NULL) {
2466 PyErr_SetString(PyExc_TypeError,
2467 "can't delete __class__ attribute");
2468 return -1;
2470 if (!PyType_Check(value)) {
2471 PyErr_Format(PyExc_TypeError,
2472 "__class__ must be set to new-style class, not '%s' object",
2473 value->ob_type->tp_name);
2474 return -1;
2476 newto = (PyTypeObject *)value;
2477 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2478 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
2480 PyErr_Format(PyExc_TypeError,
2481 "__class__ assignment: only for heap types");
2482 return -1;
2484 if (compatible_for_assignment(newto, oldto, "__class__")) {
2485 Py_INCREF(newto);
2486 self->ob_type = newto;
2487 Py_DECREF(oldto);
2488 return 0;
2490 else {
2491 return -1;
2495 static PyGetSetDef object_getsets[] = {
2496 {"__class__", object_get_class, object_set_class,
2497 PyDoc_STR("the object's class")},
2502 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2503 We fall back to helpers in copy_reg for:
2504 - pickle protocols < 2
2505 - calculating the list of slot names (done only once per class)
2506 - the __newobj__ function (which is used as a token but never called)
2509 static PyObject *
2510 import_copy_reg(void)
2512 static PyObject *copy_reg_str;
2514 if (!copy_reg_str) {
2515 copy_reg_str = PyString_InternFromString("copy_reg");
2516 if (copy_reg_str == NULL)
2517 return NULL;
2520 return PyImport_Import(copy_reg_str);
2523 static PyObject *
2524 slotnames(PyObject *cls)
2526 PyObject *clsdict;
2527 PyObject *copy_reg;
2528 PyObject *slotnames;
2530 if (!PyType_Check(cls)) {
2531 Py_INCREF(Py_None);
2532 return Py_None;
2535 clsdict = ((PyTypeObject *)cls)->tp_dict;
2536 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2537 if (slotnames != NULL && PyList_Check(slotnames)) {
2538 Py_INCREF(slotnames);
2539 return slotnames;
2542 copy_reg = import_copy_reg();
2543 if (copy_reg == NULL)
2544 return NULL;
2546 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2547 Py_DECREF(copy_reg);
2548 if (slotnames != NULL &&
2549 slotnames != Py_None &&
2550 !PyList_Check(slotnames))
2552 PyErr_SetString(PyExc_TypeError,
2553 "copy_reg._slotnames didn't return a list or None");
2554 Py_DECREF(slotnames);
2555 slotnames = NULL;
2558 return slotnames;
2561 static PyObject *
2562 reduce_2(PyObject *obj)
2564 PyObject *cls, *getnewargs;
2565 PyObject *args = NULL, *args2 = NULL;
2566 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2567 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2568 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2569 Py_ssize_t i, n;
2571 cls = PyObject_GetAttrString(obj, "__class__");
2572 if (cls == NULL)
2573 return NULL;
2575 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2576 if (getnewargs != NULL) {
2577 args = PyObject_CallObject(getnewargs, NULL);
2578 Py_DECREF(getnewargs);
2579 if (args != NULL && !PyTuple_Check(args)) {
2580 PyErr_Format(PyExc_TypeError,
2581 "__getnewargs__ should return a tuple, "
2582 "not '%.200s'", args->ob_type->tp_name);
2583 goto end;
2586 else {
2587 PyErr_Clear();
2588 args = PyTuple_New(0);
2590 if (args == NULL)
2591 goto end;
2593 getstate = PyObject_GetAttrString(obj, "__getstate__");
2594 if (getstate != NULL) {
2595 state = PyObject_CallObject(getstate, NULL);
2596 Py_DECREF(getstate);
2597 if (state == NULL)
2598 goto end;
2600 else {
2601 PyErr_Clear();
2602 state = PyObject_GetAttrString(obj, "__dict__");
2603 if (state == NULL) {
2604 PyErr_Clear();
2605 state = Py_None;
2606 Py_INCREF(state);
2608 names = slotnames(cls);
2609 if (names == NULL)
2610 goto end;
2611 if (names != Py_None) {
2612 assert(PyList_Check(names));
2613 slots = PyDict_New();
2614 if (slots == NULL)
2615 goto end;
2616 n = 0;
2617 /* Can't pre-compute the list size; the list
2618 is stored on the class so accessible to other
2619 threads, which may be run by DECREF */
2620 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2621 PyObject *name, *value;
2622 name = PyList_GET_ITEM(names, i);
2623 value = PyObject_GetAttr(obj, name);
2624 if (value == NULL)
2625 PyErr_Clear();
2626 else {
2627 int err = PyDict_SetItem(slots, name,
2628 value);
2629 Py_DECREF(value);
2630 if (err)
2631 goto end;
2632 n++;
2635 if (n) {
2636 state = Py_BuildValue("(NO)", state, slots);
2637 if (state == NULL)
2638 goto end;
2643 if (!PyList_Check(obj)) {
2644 listitems = Py_None;
2645 Py_INCREF(listitems);
2647 else {
2648 listitems = PyObject_GetIter(obj);
2649 if (listitems == NULL)
2650 goto end;
2653 if (!PyDict_Check(obj)) {
2654 dictitems = Py_None;
2655 Py_INCREF(dictitems);
2657 else {
2658 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2659 if (dictitems == NULL)
2660 goto end;
2663 copy_reg = import_copy_reg();
2664 if (copy_reg == NULL)
2665 goto end;
2666 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2667 if (newobj == NULL)
2668 goto end;
2670 n = PyTuple_GET_SIZE(args);
2671 args2 = PyTuple_New(n+1);
2672 if (args2 == NULL)
2673 goto end;
2674 PyTuple_SET_ITEM(args2, 0, cls);
2675 cls = NULL;
2676 for (i = 0; i < n; i++) {
2677 PyObject *v = PyTuple_GET_ITEM(args, i);
2678 Py_INCREF(v);
2679 PyTuple_SET_ITEM(args2, i+1, v);
2682 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2684 end:
2685 Py_XDECREF(cls);
2686 Py_XDECREF(args);
2687 Py_XDECREF(args2);
2688 Py_XDECREF(slots);
2689 Py_XDECREF(state);
2690 Py_XDECREF(names);
2691 Py_XDECREF(listitems);
2692 Py_XDECREF(dictitems);
2693 Py_XDECREF(copy_reg);
2694 Py_XDECREF(newobj);
2695 return res;
2698 static PyObject *
2699 object_reduce_ex(PyObject *self, PyObject *args)
2701 /* Call copy_reg._reduce_ex(self, proto) */
2702 PyObject *reduce, *copy_reg, *res;
2703 int proto = 0;
2705 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2706 return NULL;
2708 reduce = PyObject_GetAttrString(self, "__reduce__");
2709 if (reduce == NULL)
2710 PyErr_Clear();
2711 else {
2712 PyObject *cls, *clsreduce, *objreduce;
2713 int override;
2714 cls = PyObject_GetAttrString(self, "__class__");
2715 if (cls == NULL) {
2716 Py_DECREF(reduce);
2717 return NULL;
2719 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2720 Py_DECREF(cls);
2721 if (clsreduce == NULL) {
2722 Py_DECREF(reduce);
2723 return NULL;
2725 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2726 "__reduce__");
2727 override = (clsreduce != objreduce);
2728 Py_DECREF(clsreduce);
2729 if (override) {
2730 res = PyObject_CallObject(reduce, NULL);
2731 Py_DECREF(reduce);
2732 return res;
2734 else
2735 Py_DECREF(reduce);
2738 if (proto >= 2)
2739 return reduce_2(self);
2741 copy_reg = import_copy_reg();
2742 if (!copy_reg)
2743 return NULL;
2745 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2746 Py_DECREF(copy_reg);
2748 return res;
2751 static PyMethodDef object_methods[] = {
2752 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2753 PyDoc_STR("helper for pickle")},
2754 {"__reduce__", object_reduce_ex, METH_VARARGS,
2755 PyDoc_STR("helper for pickle")},
2760 PyTypeObject PyBaseObject_Type = {
2761 PyObject_HEAD_INIT(&PyType_Type)
2762 0, /* ob_size */
2763 "object", /* tp_name */
2764 sizeof(PyObject), /* tp_basicsize */
2765 0, /* tp_itemsize */
2766 object_dealloc, /* tp_dealloc */
2767 0, /* tp_print */
2768 0, /* tp_getattr */
2769 0, /* tp_setattr */
2770 0, /* tp_compare */
2771 object_repr, /* tp_repr */
2772 0, /* tp_as_number */
2773 0, /* tp_as_sequence */
2774 0, /* tp_as_mapping */
2775 object_hash, /* tp_hash */
2776 0, /* tp_call */
2777 object_str, /* tp_str */
2778 PyObject_GenericGetAttr, /* tp_getattro */
2779 PyObject_GenericSetAttr, /* tp_setattro */
2780 0, /* tp_as_buffer */
2781 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2782 PyDoc_STR("The most base type"), /* tp_doc */
2783 0, /* tp_traverse */
2784 0, /* tp_clear */
2785 0, /* tp_richcompare */
2786 0, /* tp_weaklistoffset */
2787 0, /* tp_iter */
2788 0, /* tp_iternext */
2789 object_methods, /* tp_methods */
2790 0, /* tp_members */
2791 object_getsets, /* tp_getset */
2792 0, /* tp_base */
2793 0, /* tp_dict */
2794 0, /* tp_descr_get */
2795 0, /* tp_descr_set */
2796 0, /* tp_dictoffset */
2797 object_init, /* tp_init */
2798 PyType_GenericAlloc, /* tp_alloc */
2799 object_new, /* tp_new */
2800 PyObject_Del, /* tp_free */
2804 /* Initialize the __dict__ in a type object */
2806 static int
2807 add_methods(PyTypeObject *type, PyMethodDef *meth)
2809 PyObject *dict = type->tp_dict;
2811 for (; meth->ml_name != NULL; meth++) {
2812 PyObject *descr;
2813 if (PyDict_GetItemString(dict, meth->ml_name) &&
2814 !(meth->ml_flags & METH_COEXIST))
2815 continue;
2816 if (meth->ml_flags & METH_CLASS) {
2817 if (meth->ml_flags & METH_STATIC) {
2818 PyErr_SetString(PyExc_ValueError,
2819 "method cannot be both class and static");
2820 return -1;
2822 descr = PyDescr_NewClassMethod(type, meth);
2824 else if (meth->ml_flags & METH_STATIC) {
2825 PyObject *cfunc = PyCFunction_New(meth, NULL);
2826 if (cfunc == NULL)
2827 return -1;
2828 descr = PyStaticMethod_New(cfunc);
2829 Py_DECREF(cfunc);
2831 else {
2832 descr = PyDescr_NewMethod(type, meth);
2834 if (descr == NULL)
2835 return -1;
2836 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2837 return -1;
2838 Py_DECREF(descr);
2840 return 0;
2843 static int
2844 add_members(PyTypeObject *type, PyMemberDef *memb)
2846 PyObject *dict = type->tp_dict;
2848 for (; memb->name != NULL; memb++) {
2849 PyObject *descr;
2850 if (PyDict_GetItemString(dict, memb->name))
2851 continue;
2852 descr = PyDescr_NewMember(type, memb);
2853 if (descr == NULL)
2854 return -1;
2855 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2856 return -1;
2857 Py_DECREF(descr);
2859 return 0;
2862 static int
2863 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2865 PyObject *dict = type->tp_dict;
2867 for (; gsp->name != NULL; gsp++) {
2868 PyObject *descr;
2869 if (PyDict_GetItemString(dict, gsp->name))
2870 continue;
2871 descr = PyDescr_NewGetSet(type, gsp);
2873 if (descr == NULL)
2874 return -1;
2875 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2876 return -1;
2877 Py_DECREF(descr);
2879 return 0;
2882 static void
2883 inherit_special(PyTypeObject *type, PyTypeObject *base)
2885 Py_ssize_t oldsize, newsize;
2887 /* Special flag magic */
2888 if (!type->tp_as_buffer && base->tp_as_buffer) {
2889 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2890 type->tp_flags |=
2891 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2893 if (!type->tp_as_sequence && base->tp_as_sequence) {
2894 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2895 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2897 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2898 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2899 if ((!type->tp_as_number && base->tp_as_number) ||
2900 (!type->tp_as_sequence && base->tp_as_sequence)) {
2901 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2902 if (!type->tp_as_number && !type->tp_as_sequence) {
2903 type->tp_flags |= base->tp_flags &
2904 Py_TPFLAGS_HAVE_INPLACEOPS;
2907 /* Wow */
2909 if (!type->tp_as_number && base->tp_as_number) {
2910 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2911 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2914 /* Copying basicsize is connected to the GC flags */
2915 oldsize = base->tp_basicsize;
2916 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2917 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2918 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2919 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2920 (!type->tp_traverse && !type->tp_clear)) {
2921 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2922 if (type->tp_traverse == NULL)
2923 type->tp_traverse = base->tp_traverse;
2924 if (type->tp_clear == NULL)
2925 type->tp_clear = base->tp_clear;
2927 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2928 /* The condition below could use some explanation.
2929 It appears that tp_new is not inherited for static types
2930 whose base class is 'object'; this seems to be a precaution
2931 so that old extension types don't suddenly become
2932 callable (object.__new__ wouldn't insure the invariants
2933 that the extension type's own factory function ensures).
2934 Heap types, of course, are under our control, so they do
2935 inherit tp_new; static extension types that specify some
2936 other built-in type as the default are considered
2937 new-style-aware so they also inherit object.__new__. */
2938 if (base != &PyBaseObject_Type ||
2939 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2940 if (type->tp_new == NULL)
2941 type->tp_new = base->tp_new;
2944 type->tp_basicsize = newsize;
2946 /* Copy other non-function slots */
2948 #undef COPYVAL
2949 #define COPYVAL(SLOT) \
2950 if (type->SLOT == 0) type->SLOT = base->SLOT
2952 COPYVAL(tp_itemsize);
2953 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2954 COPYVAL(tp_weaklistoffset);
2956 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2957 COPYVAL(tp_dictoffset);
2961 static void
2962 inherit_slots(PyTypeObject *type, PyTypeObject *base)
2964 PyTypeObject *basebase;
2966 #undef SLOTDEFINED
2967 #undef COPYSLOT
2968 #undef COPYNUM
2969 #undef COPYSEQ
2970 #undef COPYMAP
2971 #undef COPYBUF
2973 #define SLOTDEFINED(SLOT) \
2974 (base->SLOT != 0 && \
2975 (basebase == NULL || base->SLOT != basebase->SLOT))
2977 #define COPYSLOT(SLOT) \
2978 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
2980 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2981 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2982 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
2983 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
2985 /* This won't inherit indirect slots (from tp_as_number etc.)
2986 if type doesn't provide the space. */
2988 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2989 basebase = base->tp_base;
2990 if (basebase->tp_as_number == NULL)
2991 basebase = NULL;
2992 COPYNUM(nb_add);
2993 COPYNUM(nb_subtract);
2994 COPYNUM(nb_multiply);
2995 COPYNUM(nb_divide);
2996 COPYNUM(nb_remainder);
2997 COPYNUM(nb_divmod);
2998 COPYNUM(nb_power);
2999 COPYNUM(nb_negative);
3000 COPYNUM(nb_positive);
3001 COPYNUM(nb_absolute);
3002 COPYNUM(nb_nonzero);
3003 COPYNUM(nb_invert);
3004 COPYNUM(nb_lshift);
3005 COPYNUM(nb_rshift);
3006 COPYNUM(nb_and);
3007 COPYNUM(nb_xor);
3008 COPYNUM(nb_or);
3009 COPYNUM(nb_coerce);
3010 COPYNUM(nb_int);
3011 COPYNUM(nb_long);
3012 COPYNUM(nb_float);
3013 COPYNUM(nb_oct);
3014 COPYNUM(nb_hex);
3015 COPYNUM(nb_inplace_add);
3016 COPYNUM(nb_inplace_subtract);
3017 COPYNUM(nb_inplace_multiply);
3018 COPYNUM(nb_inplace_divide);
3019 COPYNUM(nb_inplace_remainder);
3020 COPYNUM(nb_inplace_power);
3021 COPYNUM(nb_inplace_lshift);
3022 COPYNUM(nb_inplace_rshift);
3023 COPYNUM(nb_inplace_and);
3024 COPYNUM(nb_inplace_xor);
3025 COPYNUM(nb_inplace_or);
3026 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3027 COPYNUM(nb_true_divide);
3028 COPYNUM(nb_floor_divide);
3029 COPYNUM(nb_inplace_true_divide);
3030 COPYNUM(nb_inplace_floor_divide);
3032 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3033 COPYNUM(nb_index);
3037 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3038 basebase = base->tp_base;
3039 if (basebase->tp_as_sequence == NULL)
3040 basebase = NULL;
3041 COPYSEQ(sq_length);
3042 COPYSEQ(sq_concat);
3043 COPYSEQ(sq_repeat);
3044 COPYSEQ(sq_item);
3045 COPYSEQ(sq_slice);
3046 COPYSEQ(sq_ass_item);
3047 COPYSEQ(sq_ass_slice);
3048 COPYSEQ(sq_contains);
3049 COPYSEQ(sq_inplace_concat);
3050 COPYSEQ(sq_inplace_repeat);
3053 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3054 basebase = base->tp_base;
3055 if (basebase->tp_as_mapping == NULL)
3056 basebase = NULL;
3057 COPYMAP(mp_length);
3058 COPYMAP(mp_subscript);
3059 COPYMAP(mp_ass_subscript);
3062 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3063 basebase = base->tp_base;
3064 if (basebase->tp_as_buffer == NULL)
3065 basebase = NULL;
3066 COPYBUF(bf_getreadbuffer);
3067 COPYBUF(bf_getwritebuffer);
3068 COPYBUF(bf_getsegcount);
3069 COPYBUF(bf_getcharbuffer);
3072 basebase = base->tp_base;
3074 COPYSLOT(tp_dealloc);
3075 COPYSLOT(tp_print);
3076 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3077 type->tp_getattr = base->tp_getattr;
3078 type->tp_getattro = base->tp_getattro;
3080 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3081 type->tp_setattr = base->tp_setattr;
3082 type->tp_setattro = base->tp_setattro;
3084 /* tp_compare see tp_richcompare */
3085 COPYSLOT(tp_repr);
3086 /* tp_hash see tp_richcompare */
3087 COPYSLOT(tp_call);
3088 COPYSLOT(tp_str);
3089 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3090 if (type->tp_compare == NULL &&
3091 type->tp_richcompare == NULL &&
3092 type->tp_hash == NULL)
3094 type->tp_compare = base->tp_compare;
3095 type->tp_richcompare = base->tp_richcompare;
3096 type->tp_hash = base->tp_hash;
3099 else {
3100 COPYSLOT(tp_compare);
3102 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3103 COPYSLOT(tp_iter);
3104 COPYSLOT(tp_iternext);
3106 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3107 COPYSLOT(tp_descr_get);
3108 COPYSLOT(tp_descr_set);
3109 COPYSLOT(tp_dictoffset);
3110 COPYSLOT(tp_init);
3111 COPYSLOT(tp_alloc);
3112 COPYSLOT(tp_is_gc);
3113 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3114 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3115 /* They agree about gc. */
3116 COPYSLOT(tp_free);
3118 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3119 type->tp_free == NULL &&
3120 base->tp_free == _PyObject_Del) {
3121 /* A bit of magic to plug in the correct default
3122 * tp_free function when a derived class adds gc,
3123 * didn't define tp_free, and the base uses the
3124 * default non-gc tp_free.
3126 type->tp_free = PyObject_GC_Del;
3128 /* else they didn't agree about gc, and there isn't something
3129 * obvious to be done -- the type is on its own.
3134 static int add_operators(PyTypeObject *);
3137 PyType_Ready(PyTypeObject *type)
3139 PyObject *dict, *bases;
3140 PyTypeObject *base;
3141 Py_ssize_t i, n;
3143 if (type->tp_flags & Py_TPFLAGS_READY) {
3144 assert(type->tp_dict != NULL);
3145 return 0;
3147 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3149 type->tp_flags |= Py_TPFLAGS_READYING;
3151 #ifdef Py_TRACE_REFS
3152 /* PyType_Ready is the closest thing we have to a choke point
3153 * for type objects, so is the best place I can think of to try
3154 * to get type objects into the doubly-linked list of all objects.
3155 * Still, not all type objects go thru PyType_Ready.
3157 _Py_AddToAllObjects((PyObject *)type, 0);
3158 #endif
3160 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3161 base = type->tp_base;
3162 if (base == NULL && type != &PyBaseObject_Type) {
3163 base = type->tp_base = &PyBaseObject_Type;
3164 Py_INCREF(base);
3167 /* Now the only way base can still be NULL is if type is
3168 * &PyBaseObject_Type.
3171 /* Initialize the base class */
3172 if (base && base->tp_dict == NULL) {
3173 if (PyType_Ready(base) < 0)
3174 goto error;
3177 /* Initialize ob_type if NULL. This means extensions that want to be
3178 compilable separately on Windows can call PyType_Ready() instead of
3179 initializing the ob_type field of their type objects. */
3180 /* The test for base != NULL is really unnecessary, since base is only
3181 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3182 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3183 know that. */
3184 if (type->ob_type == NULL && base != NULL)
3185 type->ob_type = base->ob_type;
3187 /* Initialize tp_bases */
3188 bases = type->tp_bases;
3189 if (bases == NULL) {
3190 if (base == NULL)
3191 bases = PyTuple_New(0);
3192 else
3193 bases = PyTuple_Pack(1, base);
3194 if (bases == NULL)
3195 goto error;
3196 type->tp_bases = bases;
3199 /* Initialize tp_dict */
3200 dict = type->tp_dict;
3201 if (dict == NULL) {
3202 dict = PyDict_New();
3203 if (dict == NULL)
3204 goto error;
3205 type->tp_dict = dict;
3208 /* Add type-specific descriptors to tp_dict */
3209 if (add_operators(type) < 0)
3210 goto error;
3211 if (type->tp_methods != NULL) {
3212 if (add_methods(type, type->tp_methods) < 0)
3213 goto error;
3215 if (type->tp_members != NULL) {
3216 if (add_members(type, type->tp_members) < 0)
3217 goto error;
3219 if (type->tp_getset != NULL) {
3220 if (add_getset(type, type->tp_getset) < 0)
3221 goto error;
3224 /* Calculate method resolution order */
3225 if (mro_internal(type) < 0) {
3226 goto error;
3229 /* Inherit special flags from dominant base */
3230 if (type->tp_base != NULL)
3231 inherit_special(type, type->tp_base);
3233 /* Initialize tp_dict properly */
3234 bases = type->tp_mro;
3235 assert(bases != NULL);
3236 assert(PyTuple_Check(bases));
3237 n = PyTuple_GET_SIZE(bases);
3238 for (i = 1; i < n; i++) {
3239 PyObject *b = PyTuple_GET_ITEM(bases, i);
3240 if (PyType_Check(b))
3241 inherit_slots(type, (PyTypeObject *)b);
3244 /* Sanity check for tp_free. */
3245 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3246 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3247 /* This base class needs to call tp_free, but doesn't have
3248 * one, or its tp_free is for non-gc'ed objects.
3250 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3251 "gc and is a base type but has inappropriate "
3252 "tp_free slot",
3253 type->tp_name);
3254 goto error;
3257 /* if the type dictionary doesn't contain a __doc__, set it from
3258 the tp_doc slot.
3260 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3261 if (type->tp_doc != NULL) {
3262 PyObject *doc = PyString_FromString(type->tp_doc);
3263 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3264 Py_DECREF(doc);
3265 } else {
3266 PyDict_SetItemString(type->tp_dict,
3267 "__doc__", Py_None);
3271 /* Some more special stuff */
3272 base = type->tp_base;
3273 if (base != NULL) {
3274 if (type->tp_as_number == NULL)
3275 type->tp_as_number = base->tp_as_number;
3276 if (type->tp_as_sequence == NULL)
3277 type->tp_as_sequence = base->tp_as_sequence;
3278 if (type->tp_as_mapping == NULL)
3279 type->tp_as_mapping = base->tp_as_mapping;
3280 if (type->tp_as_buffer == NULL)
3281 type->tp_as_buffer = base->tp_as_buffer;
3284 /* Link into each base class's list of subclasses */
3285 bases = type->tp_bases;
3286 n = PyTuple_GET_SIZE(bases);
3287 for (i = 0; i < n; i++) {
3288 PyObject *b = PyTuple_GET_ITEM(bases, i);
3289 if (PyType_Check(b) &&
3290 add_subclass((PyTypeObject *)b, type) < 0)
3291 goto error;
3294 /* All done -- set the ready flag */
3295 assert(type->tp_dict != NULL);
3296 type->tp_flags =
3297 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3298 return 0;
3300 error:
3301 type->tp_flags &= ~Py_TPFLAGS_READYING;
3302 return -1;
3305 static int
3306 add_subclass(PyTypeObject *base, PyTypeObject *type)
3308 Py_ssize_t i;
3309 int result;
3310 PyObject *list, *ref, *newobj;
3312 list = base->tp_subclasses;
3313 if (list == NULL) {
3314 base->tp_subclasses = list = PyList_New(0);
3315 if (list == NULL)
3316 return -1;
3318 assert(PyList_Check(list));
3319 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
3320 i = PyList_GET_SIZE(list);
3321 while (--i >= 0) {
3322 ref = PyList_GET_ITEM(list, i);
3323 assert(PyWeakref_CheckRef(ref));
3324 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3325 return PyList_SetItem(list, i, newobj);
3327 result = PyList_Append(list, newobj);
3328 Py_DECREF(newobj);
3329 return result;
3332 static void
3333 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3335 Py_ssize_t i;
3336 PyObject *list, *ref;
3338 list = base->tp_subclasses;
3339 if (list == NULL) {
3340 return;
3342 assert(PyList_Check(list));
3343 i = PyList_GET_SIZE(list);
3344 while (--i >= 0) {
3345 ref = PyList_GET_ITEM(list, i);
3346 assert(PyWeakref_CheckRef(ref));
3347 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3348 /* this can't fail, right? */
3349 PySequence_DelItem(list, i);
3350 return;
3355 static int
3356 check_num_args(PyObject *ob, int n)
3358 if (!PyTuple_CheckExact(ob)) {
3359 PyErr_SetString(PyExc_SystemError,
3360 "PyArg_UnpackTuple() argument list is not a tuple");
3361 return 0;
3363 if (n == PyTuple_GET_SIZE(ob))
3364 return 1;
3365 PyErr_Format(
3366 PyExc_TypeError,
3367 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
3368 return 0;
3371 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3373 /* There's a wrapper *function* for each distinct function typedef used
3374 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3375 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3376 Most tables have only one entry; the tables for binary operators have two
3377 entries, one regular and one with reversed arguments. */
3379 static PyObject *
3380 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
3382 lenfunc func = (lenfunc)wrapped;
3383 Py_ssize_t res;
3385 if (!check_num_args(args, 0))
3386 return NULL;
3387 res = (*func)(self);
3388 if (res == -1 && PyErr_Occurred())
3389 return NULL;
3390 return PyInt_FromLong((long)res);
3393 static PyObject *
3394 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3396 inquiry func = (inquiry)wrapped;
3397 int res;
3399 if (!check_num_args(args, 0))
3400 return NULL;
3401 res = (*func)(self);
3402 if (res == -1 && PyErr_Occurred())
3403 return NULL;
3404 return PyBool_FromLong((long)res);
3407 static PyObject *
3408 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3410 binaryfunc func = (binaryfunc)wrapped;
3411 PyObject *other;
3413 if (!check_num_args(args, 1))
3414 return NULL;
3415 other = PyTuple_GET_ITEM(args, 0);
3416 return (*func)(self, other);
3419 static PyObject *
3420 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3422 binaryfunc func = (binaryfunc)wrapped;
3423 PyObject *other;
3425 if (!check_num_args(args, 1))
3426 return NULL;
3427 other = PyTuple_GET_ITEM(args, 0);
3428 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3429 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3430 Py_INCREF(Py_NotImplemented);
3431 return Py_NotImplemented;
3433 return (*func)(self, other);
3436 static PyObject *
3437 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3439 binaryfunc func = (binaryfunc)wrapped;
3440 PyObject *other;
3442 if (!check_num_args(args, 1))
3443 return NULL;
3444 other = PyTuple_GET_ITEM(args, 0);
3445 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3446 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3447 Py_INCREF(Py_NotImplemented);
3448 return Py_NotImplemented;
3450 return (*func)(other, self);
3453 static PyObject *
3454 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3456 coercion func = (coercion)wrapped;
3457 PyObject *other, *res;
3458 int ok;
3460 if (!check_num_args(args, 1))
3461 return NULL;
3462 other = PyTuple_GET_ITEM(args, 0);
3463 ok = func(&self, &other);
3464 if (ok < 0)
3465 return NULL;
3466 if (ok > 0) {
3467 Py_INCREF(Py_NotImplemented);
3468 return Py_NotImplemented;
3470 res = PyTuple_New(2);
3471 if (res == NULL) {
3472 Py_DECREF(self);
3473 Py_DECREF(other);
3474 return NULL;
3476 PyTuple_SET_ITEM(res, 0, self);
3477 PyTuple_SET_ITEM(res, 1, other);
3478 return res;
3481 static PyObject *
3482 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3484 ternaryfunc func = (ternaryfunc)wrapped;
3485 PyObject *other;
3486 PyObject *third = Py_None;
3488 /* Note: This wrapper only works for __pow__() */
3490 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3491 return NULL;
3492 return (*func)(self, other, third);
3495 static PyObject *
3496 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3498 ternaryfunc func = (ternaryfunc)wrapped;
3499 PyObject *other;
3500 PyObject *third = Py_None;
3502 /* Note: This wrapper only works for __pow__() */
3504 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3505 return NULL;
3506 return (*func)(other, self, third);
3509 static PyObject *
3510 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3512 unaryfunc func = (unaryfunc)wrapped;
3514 if (!check_num_args(args, 0))
3515 return NULL;
3516 return (*func)(self);
3519 static PyObject *
3520 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
3522 ssizeargfunc func = (ssizeargfunc)wrapped;
3523 PyObject* o;
3524 Py_ssize_t i;
3526 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3527 return NULL;
3528 i = PyNumber_Index(o);
3529 if (i == -1 && PyErr_Occurred())
3530 return NULL;
3531 return (*func)(self, i);
3534 static Py_ssize_t
3535 getindex(PyObject *self, PyObject *arg)
3537 Py_ssize_t i;
3539 i = PyNumber_Index(arg);
3540 if (i == -1 && PyErr_Occurred())
3541 return -1;
3542 if (i < 0) {
3543 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3544 if (sq && sq->sq_length) {
3545 Py_ssize_t n = (*sq->sq_length)(self);
3546 if (n < 0)
3547 return -1;
3548 i += n;
3551 return i;
3554 static PyObject *
3555 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3557 ssizeargfunc func = (ssizeargfunc)wrapped;
3558 PyObject *arg;
3559 Py_ssize_t i;
3561 if (PyTuple_GET_SIZE(args) == 1) {
3562 arg = PyTuple_GET_ITEM(args, 0);
3563 i = getindex(self, arg);
3564 if (i == -1 && PyErr_Occurred())
3565 return NULL;
3566 return (*func)(self, i);
3568 check_num_args(args, 1);
3569 assert(PyErr_Occurred());
3570 return NULL;
3573 static PyObject *
3574 wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
3576 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3577 Py_ssize_t i, j;
3579 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3580 return NULL;
3581 return (*func)(self, i, j);
3584 static PyObject *
3585 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3587 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3588 Py_ssize_t i;
3589 int res;
3590 PyObject *arg, *value;
3592 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3593 return NULL;
3594 i = getindex(self, arg);
3595 if (i == -1 && PyErr_Occurred())
3596 return NULL;
3597 res = (*func)(self, i, value);
3598 if (res == -1 && PyErr_Occurred())
3599 return NULL;
3600 Py_INCREF(Py_None);
3601 return Py_None;
3604 static PyObject *
3605 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3607 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3608 Py_ssize_t i;
3609 int res;
3610 PyObject *arg;
3612 if (!check_num_args(args, 1))
3613 return NULL;
3614 arg = PyTuple_GET_ITEM(args, 0);
3615 i = getindex(self, arg);
3616 if (i == -1 && PyErr_Occurred())
3617 return NULL;
3618 res = (*func)(self, i, NULL);
3619 if (res == -1 && PyErr_Occurred())
3620 return NULL;
3621 Py_INCREF(Py_None);
3622 return Py_None;
3625 static PyObject *
3626 wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
3628 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3629 Py_ssize_t i, j;
3630 int res;
3631 PyObject *value;
3633 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
3634 return NULL;
3635 res = (*func)(self, i, j, value);
3636 if (res == -1 && PyErr_Occurred())
3637 return NULL;
3638 Py_INCREF(Py_None);
3639 return Py_None;
3642 static PyObject *
3643 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3645 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3646 Py_ssize_t i, j;
3647 int res;
3649 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3650 return NULL;
3651 res = (*func)(self, i, j, NULL);
3652 if (res == -1 && PyErr_Occurred())
3653 return NULL;
3654 Py_INCREF(Py_None);
3655 return Py_None;
3658 /* XXX objobjproc is a misnomer; should be objargpred */
3659 static PyObject *
3660 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3662 objobjproc func = (objobjproc)wrapped;
3663 int res;
3664 PyObject *value;
3666 if (!check_num_args(args, 1))
3667 return NULL;
3668 value = PyTuple_GET_ITEM(args, 0);
3669 res = (*func)(self, value);
3670 if (res == -1 && PyErr_Occurred())
3671 return NULL;
3672 else
3673 return PyBool_FromLong(res);
3676 static PyObject *
3677 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3679 objobjargproc func = (objobjargproc)wrapped;
3680 int res;
3681 PyObject *key, *value;
3683 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3684 return NULL;
3685 res = (*func)(self, key, value);
3686 if (res == -1 && PyErr_Occurred())
3687 return NULL;
3688 Py_INCREF(Py_None);
3689 return Py_None;
3692 static PyObject *
3693 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3695 objobjargproc func = (objobjargproc)wrapped;
3696 int res;
3697 PyObject *key;
3699 if (!check_num_args(args, 1))
3700 return NULL;
3701 key = PyTuple_GET_ITEM(args, 0);
3702 res = (*func)(self, key, NULL);
3703 if (res == -1 && PyErr_Occurred())
3704 return NULL;
3705 Py_INCREF(Py_None);
3706 return Py_None;
3709 static PyObject *
3710 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3712 cmpfunc func = (cmpfunc)wrapped;
3713 int res;
3714 PyObject *other;
3716 if (!check_num_args(args, 1))
3717 return NULL;
3718 other = PyTuple_GET_ITEM(args, 0);
3719 if (other->ob_type->tp_compare != func &&
3720 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3721 PyErr_Format(
3722 PyExc_TypeError,
3723 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3724 self->ob_type->tp_name,
3725 self->ob_type->tp_name,
3726 other->ob_type->tp_name);
3727 return NULL;
3729 res = (*func)(self, other);
3730 if (PyErr_Occurred())
3731 return NULL;
3732 return PyInt_FromLong((long)res);
3735 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3736 This is called the Carlo Verre hack after its discoverer. */
3737 static int
3738 hackcheck(PyObject *self, setattrofunc func, char *what)
3740 PyTypeObject *type = self->ob_type;
3741 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3742 type = type->tp_base;
3743 /* If type is NULL now, this is a really weird type.
3744 In the same of backwards compatibility (?), just shut up. */
3745 if (type && type->tp_setattro != func) {
3746 PyErr_Format(PyExc_TypeError,
3747 "can't apply this %s to %s object",
3748 what,
3749 type->tp_name);
3750 return 0;
3752 return 1;
3755 static PyObject *
3756 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3758 setattrofunc func = (setattrofunc)wrapped;
3759 int res;
3760 PyObject *name, *value;
3762 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3763 return NULL;
3764 if (!hackcheck(self, func, "__setattr__"))
3765 return NULL;
3766 res = (*func)(self, name, value);
3767 if (res < 0)
3768 return NULL;
3769 Py_INCREF(Py_None);
3770 return Py_None;
3773 static PyObject *
3774 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3776 setattrofunc func = (setattrofunc)wrapped;
3777 int res;
3778 PyObject *name;
3780 if (!check_num_args(args, 1))
3781 return NULL;
3782 name = PyTuple_GET_ITEM(args, 0);
3783 if (!hackcheck(self, func, "__delattr__"))
3784 return NULL;
3785 res = (*func)(self, name, NULL);
3786 if (res < 0)
3787 return NULL;
3788 Py_INCREF(Py_None);
3789 return Py_None;
3792 static PyObject *
3793 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3795 hashfunc func = (hashfunc)wrapped;
3796 long res;
3798 if (!check_num_args(args, 0))
3799 return NULL;
3800 res = (*func)(self);
3801 if (res == -1 && PyErr_Occurred())
3802 return NULL;
3803 return PyInt_FromLong(res);
3806 static PyObject *
3807 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3809 ternaryfunc func = (ternaryfunc)wrapped;
3811 return (*func)(self, args, kwds);
3814 static PyObject *
3815 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3817 richcmpfunc func = (richcmpfunc)wrapped;
3818 PyObject *other;
3820 if (!check_num_args(args, 1))
3821 return NULL;
3822 other = PyTuple_GET_ITEM(args, 0);
3823 return (*func)(self, other, op);
3826 #undef RICHCMP_WRAPPER
3827 #define RICHCMP_WRAPPER(NAME, OP) \
3828 static PyObject * \
3829 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3831 return wrap_richcmpfunc(self, args, wrapped, OP); \
3834 RICHCMP_WRAPPER(lt, Py_LT)
3835 RICHCMP_WRAPPER(le, Py_LE)
3836 RICHCMP_WRAPPER(eq, Py_EQ)
3837 RICHCMP_WRAPPER(ne, Py_NE)
3838 RICHCMP_WRAPPER(gt, Py_GT)
3839 RICHCMP_WRAPPER(ge, Py_GE)
3841 static PyObject *
3842 wrap_next(PyObject *self, PyObject *args, void *wrapped)
3844 unaryfunc func = (unaryfunc)wrapped;
3845 PyObject *res;
3847 if (!check_num_args(args, 0))
3848 return NULL;
3849 res = (*func)(self);
3850 if (res == NULL && !PyErr_Occurred())
3851 PyErr_SetNone(PyExc_StopIteration);
3852 return res;
3855 static PyObject *
3856 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3858 descrgetfunc func = (descrgetfunc)wrapped;
3859 PyObject *obj;
3860 PyObject *type = NULL;
3862 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3863 return NULL;
3864 if (obj == Py_None)
3865 obj = NULL;
3866 if (type == Py_None)
3867 type = NULL;
3868 if (type == NULL &&obj == NULL) {
3869 PyErr_SetString(PyExc_TypeError,
3870 "__get__(None, None) is invalid");
3871 return NULL;
3873 return (*func)(self, obj, type);
3876 static PyObject *
3877 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3879 descrsetfunc func = (descrsetfunc)wrapped;
3880 PyObject *obj, *value;
3881 int ret;
3883 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3884 return NULL;
3885 ret = (*func)(self, obj, value);
3886 if (ret < 0)
3887 return NULL;
3888 Py_INCREF(Py_None);
3889 return Py_None;
3892 static PyObject *
3893 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3895 descrsetfunc func = (descrsetfunc)wrapped;
3896 PyObject *obj;
3897 int ret;
3899 if (!check_num_args(args, 1))
3900 return NULL;
3901 obj = PyTuple_GET_ITEM(args, 0);
3902 ret = (*func)(self, obj, NULL);
3903 if (ret < 0)
3904 return NULL;
3905 Py_INCREF(Py_None);
3906 return Py_None;
3909 static PyObject *
3910 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3912 initproc func = (initproc)wrapped;
3914 if (func(self, args, kwds) < 0)
3915 return NULL;
3916 Py_INCREF(Py_None);
3917 return Py_None;
3920 static PyObject *
3921 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3923 PyTypeObject *type, *subtype, *staticbase;
3924 PyObject *arg0, *res;
3926 if (self == NULL || !PyType_Check(self))
3927 Py_FatalError("__new__() called with non-type 'self'");
3928 type = (PyTypeObject *)self;
3929 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3930 PyErr_Format(PyExc_TypeError,
3931 "%s.__new__(): not enough arguments",
3932 type->tp_name);
3933 return NULL;
3935 arg0 = PyTuple_GET_ITEM(args, 0);
3936 if (!PyType_Check(arg0)) {
3937 PyErr_Format(PyExc_TypeError,
3938 "%s.__new__(X): X is not a type object (%s)",
3939 type->tp_name,
3940 arg0->ob_type->tp_name);
3941 return NULL;
3943 subtype = (PyTypeObject *)arg0;
3944 if (!PyType_IsSubtype(subtype, type)) {
3945 PyErr_Format(PyExc_TypeError,
3946 "%s.__new__(%s): %s is not a subtype of %s",
3947 type->tp_name,
3948 subtype->tp_name,
3949 subtype->tp_name,
3950 type->tp_name);
3951 return NULL;
3954 /* Check that the use doesn't do something silly and unsafe like
3955 object.__new__(dict). To do this, we check that the
3956 most derived base that's not a heap type is this type. */
3957 staticbase = subtype;
3958 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3959 staticbase = staticbase->tp_base;
3960 /* If staticbase is NULL now, it is a really weird type.
3961 In the same of backwards compatibility (?), just shut up. */
3962 if (staticbase && staticbase->tp_new != type->tp_new) {
3963 PyErr_Format(PyExc_TypeError,
3964 "%s.__new__(%s) is not safe, use %s.__new__()",
3965 type->tp_name,
3966 subtype->tp_name,
3967 staticbase == NULL ? "?" : staticbase->tp_name);
3968 return NULL;
3971 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3972 if (args == NULL)
3973 return NULL;
3974 res = type->tp_new(subtype, args, kwds);
3975 Py_DECREF(args);
3976 return res;
3979 static struct PyMethodDef tp_new_methoddef[] = {
3980 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
3981 PyDoc_STR("T.__new__(S, ...) -> "
3982 "a new object with type S, a subtype of T")},
3986 static int
3987 add_tp_new_wrapper(PyTypeObject *type)
3989 PyObject *func;
3991 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
3992 return 0;
3993 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
3994 if (func == NULL)
3995 return -1;
3996 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
3997 Py_DECREF(func);
3998 return -1;
4000 Py_DECREF(func);
4001 return 0;
4004 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4005 below at override_slots() for more explanation. */
4007 #define SLOT0(FUNCNAME, OPSTR) \
4008 static PyObject * \
4009 FUNCNAME(PyObject *self) \
4011 static PyObject *cache_str; \
4012 return call_method(self, OPSTR, &cache_str, "()"); \
4015 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4016 static PyObject * \
4017 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4019 static PyObject *cache_str; \
4020 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4023 /* Boolean helper for SLOT1BINFULL().
4024 right.__class__ is a nontrivial subclass of left.__class__. */
4025 static int
4026 method_is_overloaded(PyObject *left, PyObject *right, char *name)
4028 PyObject *a, *b;
4029 int ok;
4031 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4032 if (b == NULL) {
4033 PyErr_Clear();
4034 /* If right doesn't have it, it's not overloaded */
4035 return 0;
4038 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4039 if (a == NULL) {
4040 PyErr_Clear();
4041 Py_DECREF(b);
4042 /* If right has it but left doesn't, it's overloaded */
4043 return 1;
4046 ok = PyObject_RichCompareBool(a, b, Py_NE);
4047 Py_DECREF(a);
4048 Py_DECREF(b);
4049 if (ok < 0) {
4050 PyErr_Clear();
4051 return 0;
4054 return ok;
4058 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4059 static PyObject * \
4060 FUNCNAME(PyObject *self, PyObject *other) \
4062 static PyObject *cache_str, *rcache_str; \
4063 int do_other = self->ob_type != other->ob_type && \
4064 other->ob_type->tp_as_number != NULL && \
4065 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4066 if (self->ob_type->tp_as_number != NULL && \
4067 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4068 PyObject *r; \
4069 if (do_other && \
4070 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4071 method_is_overloaded(self, other, ROPSTR)) { \
4072 r = call_maybe( \
4073 other, ROPSTR, &rcache_str, "(O)", self); \
4074 if (r != Py_NotImplemented) \
4075 return r; \
4076 Py_DECREF(r); \
4077 do_other = 0; \
4079 r = call_maybe( \
4080 self, OPSTR, &cache_str, "(O)", other); \
4081 if (r != Py_NotImplemented || \
4082 other->ob_type == self->ob_type) \
4083 return r; \
4084 Py_DECREF(r); \
4086 if (do_other) { \
4087 return call_maybe( \
4088 other, ROPSTR, &rcache_str, "(O)", self); \
4090 Py_INCREF(Py_NotImplemented); \
4091 return Py_NotImplemented; \
4094 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4095 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4097 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4098 static PyObject * \
4099 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4101 static PyObject *cache_str; \
4102 return call_method(self, OPSTR, &cache_str, \
4103 "(" ARGCODES ")", arg1, arg2); \
4106 static Py_ssize_t
4107 slot_sq_length(PyObject *self)
4109 static PyObject *len_str;
4110 PyObject *res = call_method(self, "__len__", &len_str, "()");
4111 Py_ssize_t temp;
4112 Py_ssize_t len;
4114 if (res == NULL)
4115 return -1;
4116 temp = PyInt_AsSsize_t(res);
4117 len = (int)temp;
4118 Py_DECREF(res);
4119 if (len == -1 && PyErr_Occurred())
4120 return -1;
4121 #if SIZEOF_SIZE_T < SIZEOF_LONG
4122 /* Overflow check -- range of PyInt is more than C ssize_t */
4123 if (len != temp) {
4124 PyErr_SetString(PyExc_OverflowError,
4125 "__len__() should return 0 <= outcome < 2**31");
4126 return -1;
4128 #endif
4129 if (len < 0) {
4130 PyErr_SetString(PyExc_ValueError,
4131 "__len__() should return >= 0");
4132 return -1;
4134 return len;
4137 /* Super-optimized version of slot_sq_item.
4138 Other slots could do the same... */
4139 static PyObject *
4140 slot_sq_item(PyObject *self, Py_ssize_t i)
4142 static PyObject *getitem_str;
4143 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4144 descrgetfunc f;
4146 if (getitem_str == NULL) {
4147 getitem_str = PyString_InternFromString("__getitem__");
4148 if (getitem_str == NULL)
4149 return NULL;
4151 func = _PyType_Lookup(self->ob_type, getitem_str);
4152 if (func != NULL) {
4153 if ((f = func->ob_type->tp_descr_get) == NULL)
4154 Py_INCREF(func);
4155 else {
4156 func = f(func, self, (PyObject *)(self->ob_type));
4157 if (func == NULL) {
4158 return NULL;
4161 ival = PyInt_FromSsize_t(i);
4162 if (ival != NULL) {
4163 args = PyTuple_New(1);
4164 if (args != NULL) {
4165 PyTuple_SET_ITEM(args, 0, ival);
4166 retval = PyObject_Call(func, args, NULL);
4167 Py_XDECREF(args);
4168 Py_XDECREF(func);
4169 return retval;
4173 else {
4174 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4176 Py_XDECREF(args);
4177 Py_XDECREF(ival);
4178 Py_XDECREF(func);
4179 return NULL;
4182 SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
4184 static int
4185 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
4187 PyObject *res;
4188 static PyObject *delitem_str, *setitem_str;
4190 if (value == NULL)
4191 res = call_method(self, "__delitem__", &delitem_str,
4192 "(n)", index);
4193 else
4194 res = call_method(self, "__setitem__", &setitem_str,
4195 "(nO)", index, value);
4196 if (res == NULL)
4197 return -1;
4198 Py_DECREF(res);
4199 return 0;
4202 static int
4203 slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
4205 PyObject *res;
4206 static PyObject *delslice_str, *setslice_str;
4208 if (value == NULL)
4209 res = call_method(self, "__delslice__", &delslice_str,
4210 "(nn)", i, j);
4211 else
4212 res = call_method(self, "__setslice__", &setslice_str,
4213 "(nnO)", i, j, value);
4214 if (res == NULL)
4215 return -1;
4216 Py_DECREF(res);
4217 return 0;
4220 static int
4221 slot_sq_contains(PyObject *self, PyObject *value)
4223 PyObject *func, *res, *args;
4224 int result = -1;
4226 static PyObject *contains_str;
4228 func = lookup_maybe(self, "__contains__", &contains_str);
4229 if (func != NULL) {
4230 args = PyTuple_Pack(1, value);
4231 if (args == NULL)
4232 res = NULL;
4233 else {
4234 res = PyObject_Call(func, args, NULL);
4235 Py_DECREF(args);
4237 Py_DECREF(func);
4238 if (res != NULL) {
4239 result = PyObject_IsTrue(res);
4240 Py_DECREF(res);
4243 else if (! PyErr_Occurred()) {
4244 /* Possible results: -1 and 1 */
4245 result = (int)_PySequence_IterSearch(self, value,
4246 PY_ITERSEARCH_CONTAINS);
4248 return result;
4251 #define slot_mp_length slot_sq_length
4253 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4255 static int
4256 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4258 PyObject *res;
4259 static PyObject *delitem_str, *setitem_str;
4261 if (value == NULL)
4262 res = call_method(self, "__delitem__", &delitem_str,
4263 "(O)", key);
4264 else
4265 res = call_method(self, "__setitem__", &setitem_str,
4266 "(OO)", key, value);
4267 if (res == NULL)
4268 return -1;
4269 Py_DECREF(res);
4270 return 0;
4273 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4274 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4275 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4276 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4277 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4278 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4280 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4282 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4283 nb_power, "__pow__", "__rpow__")
4285 static PyObject *
4286 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4288 static PyObject *pow_str;
4290 if (modulus == Py_None)
4291 return slot_nb_power_binary(self, other);
4292 /* Three-arg power doesn't use __rpow__. But ternary_op
4293 can call this when the second argument's type uses
4294 slot_nb_power, so check before calling self.__pow__. */
4295 if (self->ob_type->tp_as_number != NULL &&
4296 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4297 return call_method(self, "__pow__", &pow_str,
4298 "(OO)", other, modulus);
4300 Py_INCREF(Py_NotImplemented);
4301 return Py_NotImplemented;
4304 SLOT0(slot_nb_negative, "__neg__")
4305 SLOT0(slot_nb_positive, "__pos__")
4306 SLOT0(slot_nb_absolute, "__abs__")
4308 static int
4309 slot_nb_nonzero(PyObject *self)
4311 PyObject *func, *args;
4312 static PyObject *nonzero_str, *len_str;
4313 int result = -1;
4315 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4316 if (func == NULL) {
4317 if (PyErr_Occurred())
4318 return -1;
4319 func = lookup_maybe(self, "__len__", &len_str);
4320 if (func == NULL)
4321 return PyErr_Occurred() ? -1 : 1;
4323 args = PyTuple_New(0);
4324 if (args != NULL) {
4325 PyObject *temp = PyObject_Call(func, args, NULL);
4326 Py_DECREF(args);
4327 if (temp != NULL) {
4328 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4329 result = PyObject_IsTrue(temp);
4330 else {
4331 PyErr_Format(PyExc_TypeError,
4332 "__nonzero__ should return "
4333 "bool or int, returned %s",
4334 temp->ob_type->tp_name);
4335 result = -1;
4337 Py_DECREF(temp);
4340 Py_DECREF(func);
4341 return result;
4345 static Py_ssize_t
4346 slot_nb_index(PyObject *self)
4348 static PyObject *index_str;
4349 PyObject *temp = call_method(self, "__index__", &index_str, "()");
4350 Py_ssize_t result;
4352 if (temp == NULL)
4353 return -1;
4354 if (PyInt_CheckExact(temp) || PyLong_CheckExact(temp)) {
4355 result = temp->ob_type->tp_as_number->nb_index(temp);
4357 else {
4358 PyErr_Format(PyExc_TypeError,
4359 "__index__ must return an int or a long, "
4360 "not '%.200s'", temp->ob_type->tp_name);
4361 result = -1;
4363 Py_DECREF(temp);
4364 return result;
4368 SLOT0(slot_nb_invert, "__invert__")
4369 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4370 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4371 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4372 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4373 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4375 static int
4376 slot_nb_coerce(PyObject **a, PyObject **b)
4378 static PyObject *coerce_str;
4379 PyObject *self = *a, *other = *b;
4381 if (self->ob_type->tp_as_number != NULL &&
4382 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4383 PyObject *r;
4384 r = call_maybe(
4385 self, "__coerce__", &coerce_str, "(O)", other);
4386 if (r == NULL)
4387 return -1;
4388 if (r == Py_NotImplemented) {
4389 Py_DECREF(r);
4391 else {
4392 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4393 PyErr_SetString(PyExc_TypeError,
4394 "__coerce__ didn't return a 2-tuple");
4395 Py_DECREF(r);
4396 return -1;
4398 *a = PyTuple_GET_ITEM(r, 0);
4399 Py_INCREF(*a);
4400 *b = PyTuple_GET_ITEM(r, 1);
4401 Py_INCREF(*b);
4402 Py_DECREF(r);
4403 return 0;
4406 if (other->ob_type->tp_as_number != NULL &&
4407 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4408 PyObject *r;
4409 r = call_maybe(
4410 other, "__coerce__", &coerce_str, "(O)", self);
4411 if (r == NULL)
4412 return -1;
4413 if (r == Py_NotImplemented) {
4414 Py_DECREF(r);
4415 return 1;
4417 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4418 PyErr_SetString(PyExc_TypeError,
4419 "__coerce__ didn't return a 2-tuple");
4420 Py_DECREF(r);
4421 return -1;
4423 *a = PyTuple_GET_ITEM(r, 1);
4424 Py_INCREF(*a);
4425 *b = PyTuple_GET_ITEM(r, 0);
4426 Py_INCREF(*b);
4427 Py_DECREF(r);
4428 return 0;
4430 return 1;
4433 SLOT0(slot_nb_int, "__int__")
4434 SLOT0(slot_nb_long, "__long__")
4435 SLOT0(slot_nb_float, "__float__")
4436 SLOT0(slot_nb_oct, "__oct__")
4437 SLOT0(slot_nb_hex, "__hex__")
4438 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4439 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4440 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4441 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4442 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4443 SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
4444 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4445 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4446 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4447 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4448 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4449 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4450 "__floordiv__", "__rfloordiv__")
4451 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4452 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4453 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4455 static int
4456 half_compare(PyObject *self, PyObject *other)
4458 PyObject *func, *args, *res;
4459 static PyObject *cmp_str;
4460 Py_ssize_t c;
4462 func = lookup_method(self, "__cmp__", &cmp_str);
4463 if (func == NULL) {
4464 PyErr_Clear();
4466 else {
4467 args = PyTuple_Pack(1, other);
4468 if (args == NULL)
4469 res = NULL;
4470 else {
4471 res = PyObject_Call(func, args, NULL);
4472 Py_DECREF(args);
4474 Py_DECREF(func);
4475 if (res != Py_NotImplemented) {
4476 if (res == NULL)
4477 return -2;
4478 c = PyInt_AsLong(res);
4479 Py_DECREF(res);
4480 if (c == -1 && PyErr_Occurred())
4481 return -2;
4482 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4484 Py_DECREF(res);
4486 return 2;
4489 /* This slot is published for the benefit of try_3way_compare in object.c */
4491 _PyObject_SlotCompare(PyObject *self, PyObject *other)
4493 int c;
4495 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4496 c = half_compare(self, other);
4497 if (c <= 1)
4498 return c;
4500 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4501 c = half_compare(other, self);
4502 if (c < -1)
4503 return -2;
4504 if (c <= 1)
4505 return -c;
4507 return (void *)self < (void *)other ? -1 :
4508 (void *)self > (void *)other ? 1 : 0;
4511 static PyObject *
4512 slot_tp_repr(PyObject *self)
4514 PyObject *func, *res;
4515 static PyObject *repr_str;
4517 func = lookup_method(self, "__repr__", &repr_str);
4518 if (func != NULL) {
4519 res = PyEval_CallObject(func, NULL);
4520 Py_DECREF(func);
4521 return res;
4523 PyErr_Clear();
4524 return PyString_FromFormat("<%s object at %p>",
4525 self->ob_type->tp_name, self);
4528 static PyObject *
4529 slot_tp_str(PyObject *self)
4531 PyObject *func, *res;
4532 static PyObject *str_str;
4534 func = lookup_method(self, "__str__", &str_str);
4535 if (func != NULL) {
4536 res = PyEval_CallObject(func, NULL);
4537 Py_DECREF(func);
4538 return res;
4540 else {
4541 PyErr_Clear();
4542 return slot_tp_repr(self);
4546 static long
4547 slot_tp_hash(PyObject *self)
4549 PyObject *func;
4550 static PyObject *hash_str, *eq_str, *cmp_str;
4551 long h;
4553 func = lookup_method(self, "__hash__", &hash_str);
4555 if (func != NULL) {
4556 PyObject *res = PyEval_CallObject(func, NULL);
4557 Py_DECREF(func);
4558 if (res == NULL)
4559 return -1;
4560 h = PyInt_AsLong(res);
4561 Py_DECREF(res);
4563 else {
4564 PyErr_Clear();
4565 func = lookup_method(self, "__eq__", &eq_str);
4566 if (func == NULL) {
4567 PyErr_Clear();
4568 func = lookup_method(self, "__cmp__", &cmp_str);
4570 if (func != NULL) {
4571 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
4572 self->ob_type->tp_name);
4573 Py_DECREF(func);
4574 return -1;
4576 PyErr_Clear();
4577 h = _Py_HashPointer((void *)self);
4579 if (h == -1 && !PyErr_Occurred())
4580 h = -2;
4581 return h;
4584 static PyObject *
4585 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4587 static PyObject *call_str;
4588 PyObject *meth = lookup_method(self, "__call__", &call_str);
4589 PyObject *res;
4591 if (meth == NULL)
4592 return NULL;
4594 /* PyObject_Call() will end up calling slot_tp_call() again if
4595 the object returned for __call__ has __call__ itself defined
4596 upon it. This can be an infinite recursion if you set
4597 __call__ in a class to an instance of it. */
4598 if (Py_EnterRecursiveCall(" in __call__")) {
4599 Py_DECREF(meth);
4600 return NULL;
4602 res = PyObject_Call(meth, args, kwds);
4603 Py_LeaveRecursiveCall();
4605 Py_DECREF(meth);
4606 return res;
4609 /* There are two slot dispatch functions for tp_getattro.
4611 - slot_tp_getattro() is used when __getattribute__ is overridden
4612 but no __getattr__ hook is present;
4614 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4616 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4617 detects the absence of __getattr__ and then installs the simpler slot if
4618 necessary. */
4620 static PyObject *
4621 slot_tp_getattro(PyObject *self, PyObject *name)
4623 static PyObject *getattribute_str = NULL;
4624 return call_method(self, "__getattribute__", &getattribute_str,
4625 "(O)", name);
4628 static PyObject *
4629 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4631 PyTypeObject *tp = self->ob_type;
4632 PyObject *getattr, *getattribute, *res;
4633 static PyObject *getattribute_str = NULL;
4634 static PyObject *getattr_str = NULL;
4636 if (getattr_str == NULL) {
4637 getattr_str = PyString_InternFromString("__getattr__");
4638 if (getattr_str == NULL)
4639 return NULL;
4641 if (getattribute_str == NULL) {
4642 getattribute_str =
4643 PyString_InternFromString("__getattribute__");
4644 if (getattribute_str == NULL)
4645 return NULL;
4647 getattr = _PyType_Lookup(tp, getattr_str);
4648 if (getattr == NULL) {
4649 /* No __getattr__ hook: use a simpler dispatcher */
4650 tp->tp_getattro = slot_tp_getattro;
4651 return slot_tp_getattro(self, name);
4653 getattribute = _PyType_Lookup(tp, getattribute_str);
4654 if (getattribute == NULL ||
4655 (getattribute->ob_type == &PyWrapperDescr_Type &&
4656 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4657 (void *)PyObject_GenericGetAttr))
4658 res = PyObject_GenericGetAttr(self, name);
4659 else
4660 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
4661 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4662 PyErr_Clear();
4663 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
4665 return res;
4668 static int
4669 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4671 PyObject *res;
4672 static PyObject *delattr_str, *setattr_str;
4674 if (value == NULL)
4675 res = call_method(self, "__delattr__", &delattr_str,
4676 "(O)", name);
4677 else
4678 res = call_method(self, "__setattr__", &setattr_str,
4679 "(OO)", name, value);
4680 if (res == NULL)
4681 return -1;
4682 Py_DECREF(res);
4683 return 0;
4686 /* Map rich comparison operators to their __xx__ namesakes */
4687 static char *name_op[] = {
4688 "__lt__",
4689 "__le__",
4690 "__eq__",
4691 "__ne__",
4692 "__gt__",
4693 "__ge__",
4696 static PyObject *
4697 half_richcompare(PyObject *self, PyObject *other, int op)
4699 PyObject *func, *args, *res;
4700 static PyObject *op_str[6];
4702 func = lookup_method(self, name_op[op], &op_str[op]);
4703 if (func == NULL) {
4704 PyErr_Clear();
4705 Py_INCREF(Py_NotImplemented);
4706 return Py_NotImplemented;
4708 args = PyTuple_Pack(1, other);
4709 if (args == NULL)
4710 res = NULL;
4711 else {
4712 res = PyObject_Call(func, args, NULL);
4713 Py_DECREF(args);
4715 Py_DECREF(func);
4716 return res;
4719 static PyObject *
4720 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4722 PyObject *res;
4724 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4725 res = half_richcompare(self, other, op);
4726 if (res != Py_NotImplemented)
4727 return res;
4728 Py_DECREF(res);
4730 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4731 res = half_richcompare(other, self, _Py_SwappedOp[op]);
4732 if (res != Py_NotImplemented) {
4733 return res;
4735 Py_DECREF(res);
4737 Py_INCREF(Py_NotImplemented);
4738 return Py_NotImplemented;
4741 static PyObject *
4742 slot_tp_iter(PyObject *self)
4744 PyObject *func, *res;
4745 static PyObject *iter_str, *getitem_str;
4747 func = lookup_method(self, "__iter__", &iter_str);
4748 if (func != NULL) {
4749 PyObject *args;
4750 args = res = PyTuple_New(0);
4751 if (args != NULL) {
4752 res = PyObject_Call(func, args, NULL);
4753 Py_DECREF(args);
4755 Py_DECREF(func);
4756 return res;
4758 PyErr_Clear();
4759 func = lookup_method(self, "__getitem__", &getitem_str);
4760 if (func == NULL) {
4761 PyErr_Format(PyExc_TypeError,
4762 "'%.200s' object is not iterable",
4763 self->ob_type->tp_name);
4764 return NULL;
4766 Py_DECREF(func);
4767 return PySeqIter_New(self);
4770 static PyObject *
4771 slot_tp_iternext(PyObject *self)
4773 static PyObject *next_str;
4774 return call_method(self, "next", &next_str, "()");
4777 static PyObject *
4778 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4780 PyTypeObject *tp = self->ob_type;
4781 PyObject *get;
4782 static PyObject *get_str = NULL;
4784 if (get_str == NULL) {
4785 get_str = PyString_InternFromString("__get__");
4786 if (get_str == NULL)
4787 return NULL;
4789 get = _PyType_Lookup(tp, get_str);
4790 if (get == NULL) {
4791 /* Avoid further slowdowns */
4792 if (tp->tp_descr_get == slot_tp_descr_get)
4793 tp->tp_descr_get = NULL;
4794 Py_INCREF(self);
4795 return self;
4797 if (obj == NULL)
4798 obj = Py_None;
4799 if (type == NULL)
4800 type = Py_None;
4801 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
4804 static int
4805 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4807 PyObject *res;
4808 static PyObject *del_str, *set_str;
4810 if (value == NULL)
4811 res = call_method(self, "__delete__", &del_str,
4812 "(O)", target);
4813 else
4814 res = call_method(self, "__set__", &set_str,
4815 "(OO)", target, value);
4816 if (res == NULL)
4817 return -1;
4818 Py_DECREF(res);
4819 return 0;
4822 static int
4823 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4825 static PyObject *init_str;
4826 PyObject *meth = lookup_method(self, "__init__", &init_str);
4827 PyObject *res;
4829 if (meth == NULL)
4830 return -1;
4831 res = PyObject_Call(meth, args, kwds);
4832 Py_DECREF(meth);
4833 if (res == NULL)
4834 return -1;
4835 if (res != Py_None) {
4836 PyErr_Format(PyExc_TypeError,
4837 "__init__() should return None, not '%.200s'",
4838 res->ob_type->tp_name);
4839 Py_DECREF(res);
4840 return -1;
4842 Py_DECREF(res);
4843 return 0;
4846 static PyObject *
4847 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4849 static PyObject *new_str;
4850 PyObject *func;
4851 PyObject *newargs, *x;
4852 Py_ssize_t i, n;
4854 if (new_str == NULL) {
4855 new_str = PyString_InternFromString("__new__");
4856 if (new_str == NULL)
4857 return NULL;
4859 func = PyObject_GetAttr((PyObject *)type, new_str);
4860 if (func == NULL)
4861 return NULL;
4862 assert(PyTuple_Check(args));
4863 n = PyTuple_GET_SIZE(args);
4864 newargs = PyTuple_New(n+1);
4865 if (newargs == NULL)
4866 return NULL;
4867 Py_INCREF(type);
4868 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4869 for (i = 0; i < n; i++) {
4870 x = PyTuple_GET_ITEM(args, i);
4871 Py_INCREF(x);
4872 PyTuple_SET_ITEM(newargs, i+1, x);
4874 x = PyObject_Call(func, newargs, kwds);
4875 Py_DECREF(newargs);
4876 Py_DECREF(func);
4877 return x;
4880 static void
4881 slot_tp_del(PyObject *self)
4883 static PyObject *del_str = NULL;
4884 PyObject *del, *res;
4885 PyObject *error_type, *error_value, *error_traceback;
4887 /* Temporarily resurrect the object. */
4888 assert(self->ob_refcnt == 0);
4889 self->ob_refcnt = 1;
4891 /* Save the current exception, if any. */
4892 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4894 /* Execute __del__ method, if any. */
4895 del = lookup_maybe(self, "__del__", &del_str);
4896 if (del != NULL) {
4897 res = PyEval_CallObject(del, NULL);
4898 if (res == NULL)
4899 PyErr_WriteUnraisable(del);
4900 else
4901 Py_DECREF(res);
4902 Py_DECREF(del);
4905 /* Restore the saved exception. */
4906 PyErr_Restore(error_type, error_value, error_traceback);
4908 /* Undo the temporary resurrection; can't use DECREF here, it would
4909 * cause a recursive call.
4911 assert(self->ob_refcnt > 0);
4912 if (--self->ob_refcnt == 0)
4913 return; /* this is the normal path out */
4915 /* __del__ resurrected it! Make it look like the original Py_DECREF
4916 * never happened.
4919 Py_ssize_t refcnt = self->ob_refcnt;
4920 _Py_NewReference(self);
4921 self->ob_refcnt = refcnt;
4923 assert(!PyType_IS_GC(self->ob_type) ||
4924 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4925 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4926 * we need to undo that. */
4927 _Py_DEC_REFTOTAL;
4928 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4929 * chain, so no more to do there.
4930 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4931 * _Py_NewReference bumped tp_allocs: both of those need to be
4932 * undone.
4934 #ifdef COUNT_ALLOCS
4935 --self->ob_type->tp_frees;
4936 --self->ob_type->tp_allocs;
4937 #endif
4941 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4942 functions. The offsets here are relative to the 'PyHeapTypeObject'
4943 structure, which incorporates the additional structures used for numbers,
4944 sequences and mappings.
4945 Note that multiple names may map to the same slot (e.g. __eq__,
4946 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4947 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4948 terminated with an all-zero entry. (This table is further initialized and
4949 sorted in init_slotdefs() below.) */
4951 typedef struct wrapperbase slotdef;
4953 #undef TPSLOT
4954 #undef FLSLOT
4955 #undef ETSLOT
4956 #undef SQSLOT
4957 #undef MPSLOT
4958 #undef NBSLOT
4959 #undef UNSLOT
4960 #undef IBSLOT
4961 #undef BINSLOT
4962 #undef RBINSLOT
4964 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4965 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4966 PyDoc_STR(DOC)}
4967 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4968 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4969 PyDoc_STR(DOC), FLAGS}
4970 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4971 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4972 PyDoc_STR(DOC)}
4973 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4974 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4975 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4976 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4977 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4978 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4979 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4980 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4981 "x." NAME "() <==> " DOC)
4982 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4983 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4984 "x." NAME "(y) <==> x" DOC "y")
4985 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4986 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4987 "x." NAME "(y) <==> x" DOC "y")
4988 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4989 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4990 "x." NAME "(y) <==> y" DOC "x")
4991 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4992 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4993 "x." NAME "(y) <==> " DOC)
4994 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4995 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4996 "x." NAME "(y) <==> " DOC)
4998 static slotdef slotdefs[] = {
4999 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
5000 "x.__len__() <==> len(x)"),
5001 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5002 The logic in abstract.c always falls back to nb_add/nb_multiply in
5003 this case. Defining both the nb_* and the sq_* slots to call the
5004 user-defined methods has unexpected side-effects, as shown by
5005 test_descr.notimplemented() */
5006 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
5007 "x.__add__(y) <==> x+y"),
5008 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
5009 "x.__mul__(n) <==> x*n"),
5010 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
5011 "x.__rmul__(n) <==> n*x"),
5012 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5013 "x.__getitem__(y) <==> x[y]"),
5014 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
5015 "x.__getslice__(i, j) <==> x[i:j]\n\
5017 Use of negative indices is not supported."),
5018 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
5019 "x.__setitem__(i, y) <==> x[i]=y"),
5020 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
5021 "x.__delitem__(y) <==> del x[y]"),
5022 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
5023 wrap_ssizessizeobjargproc,
5024 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
5026 Use of negative indices is not supported."),
5027 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
5028 "x.__delslice__(i, j) <==> del x[i:j]\n\
5030 Use of negative indices is not supported."),
5031 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5032 "x.__contains__(y) <==> y in x"),
5033 SQSLOT("__iadd__", sq_inplace_concat, NULL,
5034 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
5035 SQSLOT("__imul__", sq_inplace_repeat, NULL,
5036 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
5038 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
5039 "x.__len__() <==> len(x)"),
5040 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
5041 wrap_binaryfunc,
5042 "x.__getitem__(y) <==> x[y]"),
5043 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
5044 wrap_objobjargproc,
5045 "x.__setitem__(i, y) <==> x[i]=y"),
5046 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
5047 wrap_delitem,
5048 "x.__delitem__(y) <==> del x[y]"),
5050 BINSLOT("__add__", nb_add, slot_nb_add,
5051 "+"),
5052 RBINSLOT("__radd__", nb_add, slot_nb_add,
5053 "+"),
5054 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5055 "-"),
5056 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5057 "-"),
5058 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5059 "*"),
5060 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5061 "*"),
5062 BINSLOT("__div__", nb_divide, slot_nb_divide,
5063 "/"),
5064 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
5065 "/"),
5066 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5067 "%"),
5068 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5069 "%"),
5070 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
5071 "divmod(x, y)"),
5072 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
5073 "divmod(y, x)"),
5074 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5075 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5076 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5077 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5078 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5079 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5080 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5081 "abs(x)"),
5082 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
5083 "x != 0"),
5084 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5085 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5086 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5087 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5088 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5089 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5090 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5091 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5092 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5093 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5094 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5095 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5096 "x.__coerce__(y) <==> coerce(x, y)"),
5097 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5098 "int(x)"),
5099 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5100 "long(x)"),
5101 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5102 "float(x)"),
5103 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5104 "oct(x)"),
5105 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5106 "hex(x)"),
5107 NBSLOT("__index__", nb_index, slot_nb_index, wrap_lenfunc,
5108 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5109 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5110 wrap_binaryfunc, "+"),
5111 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5112 wrap_binaryfunc, "-"),
5113 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5114 wrap_binaryfunc, "*"),
5115 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5116 wrap_binaryfunc, "/"),
5117 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5118 wrap_binaryfunc, "%"),
5119 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5120 wrap_binaryfunc, "**"),
5121 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5122 wrap_binaryfunc, "<<"),
5123 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5124 wrap_binaryfunc, ">>"),
5125 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5126 wrap_binaryfunc, "&"),
5127 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5128 wrap_binaryfunc, "^"),
5129 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5130 wrap_binaryfunc, "|"),
5131 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5132 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5133 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5134 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5135 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5136 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5137 IBSLOT("__itruediv__", nb_inplace_true_divide,
5138 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5140 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5141 "x.__str__() <==> str(x)"),
5142 TPSLOT("__str__", tp_print, NULL, NULL, ""),
5143 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5144 "x.__repr__() <==> repr(x)"),
5145 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5146 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5147 "x.__cmp__(y) <==> cmp(x,y)"),
5148 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5149 "x.__hash__() <==> hash(x)"),
5150 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5151 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5152 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5153 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5154 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5155 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5156 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5157 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5158 "x.__setattr__('name', value) <==> x.name = value"),
5159 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5160 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5161 "x.__delattr__('name') <==> del x.name"),
5162 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5163 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5164 "x.__lt__(y) <==> x<y"),
5165 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5166 "x.__le__(y) <==> x<=y"),
5167 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5168 "x.__eq__(y) <==> x==y"),
5169 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5170 "x.__ne__(y) <==> x!=y"),
5171 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5172 "x.__gt__(y) <==> x>y"),
5173 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5174 "x.__ge__(y) <==> x>=y"),
5175 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5176 "x.__iter__() <==> iter(x)"),
5177 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5178 "x.next() -> the next value, or raise StopIteration"),
5179 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5180 "descr.__get__(obj[, type]) -> value"),
5181 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5182 "descr.__set__(obj, value)"),
5183 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5184 wrap_descr_delete, "descr.__delete__(obj)"),
5185 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5186 "x.__init__(...) initializes x; "
5187 "see x.__class__.__doc__ for signature",
5188 PyWrapperFlag_KEYWORDS),
5189 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5190 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5191 {NULL}
5194 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5195 pointer to the actual slot. This is not quite the same as simply adding
5196 the offset to the type pointer, since it takes care to indirect through the
5197 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5198 indirection pointer is NULL. */
5199 static void **
5200 slotptr(PyTypeObject *type, int ioffset)
5202 char *ptr;
5203 long offset = ioffset;
5205 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5206 assert(offset >= 0);
5207 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5208 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5209 ptr = (char *)type->tp_as_sequence;
5210 offset -= offsetof(PyHeapTypeObject, as_sequence);
5212 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5213 ptr = (char *)type->tp_as_mapping;
5214 offset -= offsetof(PyHeapTypeObject, as_mapping);
5216 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5217 ptr = (char *)type->tp_as_number;
5218 offset -= offsetof(PyHeapTypeObject, as_number);
5220 else {
5221 ptr = (char *)type;
5223 if (ptr != NULL)
5224 ptr += offset;
5225 return (void **)ptr;
5228 /* Length of array of slotdef pointers used to store slots with the
5229 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5230 the same __name__, for any __name__. Since that's a static property, it is
5231 appropriate to declare fixed-size arrays for this. */
5232 #define MAX_EQUIV 10
5234 /* Return a slot pointer for a given name, but ONLY if the attribute has
5235 exactly one slot function. The name must be an interned string. */
5236 static void **
5237 resolve_slotdups(PyTypeObject *type, PyObject *name)
5239 /* XXX Maybe this could be optimized more -- but is it worth it? */
5241 /* pname and ptrs act as a little cache */
5242 static PyObject *pname;
5243 static slotdef *ptrs[MAX_EQUIV];
5244 slotdef *p, **pp;
5245 void **res, **ptr;
5247 if (pname != name) {
5248 /* Collect all slotdefs that match name into ptrs. */
5249 pname = name;
5250 pp = ptrs;
5251 for (p = slotdefs; p->name_strobj; p++) {
5252 if (p->name_strobj == name)
5253 *pp++ = p;
5255 *pp = NULL;
5258 /* Look in all matching slots of the type; if exactly one of these has
5259 a filled-in slot, return its value. Otherwise return NULL. */
5260 res = NULL;
5261 for (pp = ptrs; *pp; pp++) {
5262 ptr = slotptr(type, (*pp)->offset);
5263 if (ptr == NULL || *ptr == NULL)
5264 continue;
5265 if (res != NULL)
5266 return NULL;
5267 res = ptr;
5269 return res;
5272 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5273 does some incredibly complex thinking and then sticks something into the
5274 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5275 interests, and then stores a generic wrapper or a specific function into
5276 the slot.) Return a pointer to the next slotdef with a different offset,
5277 because that's convenient for fixup_slot_dispatchers(). */
5278 static slotdef *
5279 update_one_slot(PyTypeObject *type, slotdef *p)
5281 PyObject *descr;
5282 PyWrapperDescrObject *d;
5283 void *generic = NULL, *specific = NULL;
5284 int use_generic = 0;
5285 int offset = p->offset;
5286 void **ptr = slotptr(type, offset);
5288 if (ptr == NULL) {
5289 do {
5290 ++p;
5291 } while (p->offset == offset);
5292 return p;
5294 do {
5295 descr = _PyType_Lookup(type, p->name_strobj);
5296 if (descr == NULL)
5297 continue;
5298 if (descr->ob_type == &PyWrapperDescr_Type) {
5299 void **tptr = resolve_slotdups(type, p->name_strobj);
5300 if (tptr == NULL || tptr == ptr)
5301 generic = p->function;
5302 d = (PyWrapperDescrObject *)descr;
5303 if (d->d_base->wrapper == p->wrapper &&
5304 PyType_IsSubtype(type, d->d_type))
5306 if (specific == NULL ||
5307 specific == d->d_wrapped)
5308 specific = d->d_wrapped;
5309 else
5310 use_generic = 1;
5313 else if (descr->ob_type == &PyCFunction_Type &&
5314 PyCFunction_GET_FUNCTION(descr) ==
5315 (PyCFunction)tp_new_wrapper &&
5316 strcmp(p->name, "__new__") == 0)
5318 /* The __new__ wrapper is not a wrapper descriptor,
5319 so must be special-cased differently.
5320 If we don't do this, creating an instance will
5321 always use slot_tp_new which will look up
5322 __new__ in the MRO which will call tp_new_wrapper
5323 which will look through the base classes looking
5324 for a static base and call its tp_new (usually
5325 PyType_GenericNew), after performing various
5326 sanity checks and constructing a new argument
5327 list. Cut all that nonsense short -- this speeds
5328 up instance creation tremendously. */
5329 specific = (void *)type->tp_new;
5330 /* XXX I'm not 100% sure that there isn't a hole
5331 in this reasoning that requires additional
5332 sanity checks. I'll buy the first person to
5333 point out a bug in this reasoning a beer. */
5335 else {
5336 use_generic = 1;
5337 generic = p->function;
5339 } while ((++p)->offset == offset);
5340 if (specific && !use_generic)
5341 *ptr = specific;
5342 else
5343 *ptr = generic;
5344 return p;
5347 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5348 This is a callback for update_subclasses(). */
5349 static int
5350 update_slots_callback(PyTypeObject *type, void *data)
5352 slotdef **pp = (slotdef **)data;
5354 for (; *pp; pp++)
5355 update_one_slot(type, *pp);
5356 return 0;
5359 /* Comparison function for qsort() to compare slotdefs by their offset, and
5360 for equal offset by their address (to force a stable sort). */
5361 static int
5362 slotdef_cmp(const void *aa, const void *bb)
5364 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5365 int c = a->offset - b->offset;
5366 if (c != 0)
5367 return c;
5368 else
5369 /* Cannot use a-b, as this gives off_t,
5370 which may lose precision when converted to int. */
5371 return (a > b) ? 1 : (a < b) ? -1 : 0;
5374 /* Initialize the slotdefs table by adding interned string objects for the
5375 names and sorting the entries. */
5376 static void
5377 init_slotdefs(void)
5379 slotdef *p;
5380 static int initialized = 0;
5382 if (initialized)
5383 return;
5384 for (p = slotdefs; p->name; p++) {
5385 p->name_strobj = PyString_InternFromString(p->name);
5386 if (!p->name_strobj)
5387 Py_FatalError("Out of memory interning slotdef names");
5389 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5390 slotdef_cmp);
5391 initialized = 1;
5394 /* Update the slots after assignment to a class (type) attribute. */
5395 static int
5396 update_slot(PyTypeObject *type, PyObject *name)
5398 slotdef *ptrs[MAX_EQUIV];
5399 slotdef *p;
5400 slotdef **pp;
5401 int offset;
5403 init_slotdefs();
5404 pp = ptrs;
5405 for (p = slotdefs; p->name; p++) {
5406 /* XXX assume name is interned! */
5407 if (p->name_strobj == name)
5408 *pp++ = p;
5410 *pp = NULL;
5411 for (pp = ptrs; *pp; pp++) {
5412 p = *pp;
5413 offset = p->offset;
5414 while (p > slotdefs && (p-1)->offset == offset)
5415 --p;
5416 *pp = p;
5418 if (ptrs[0] == NULL)
5419 return 0; /* Not an attribute that affects any slots */
5420 return update_subclasses(type, name,
5421 update_slots_callback, (void *)ptrs);
5424 /* Store the proper functions in the slot dispatches at class (type)
5425 definition time, based upon which operations the class overrides in its
5426 dict. */
5427 static void
5428 fixup_slot_dispatchers(PyTypeObject *type)
5430 slotdef *p;
5432 init_slotdefs();
5433 for (p = slotdefs; p->name; )
5434 p = update_one_slot(type, p);
5437 static void
5438 update_all_slots(PyTypeObject* type)
5440 slotdef *p;
5442 init_slotdefs();
5443 for (p = slotdefs; p->name; p++) {
5444 /* update_slot returns int but can't actually fail */
5445 update_slot(type, p->name_strobj);
5449 /* recurse_down_subclasses() and update_subclasses() are mutually
5450 recursive functions to call a callback for all subclasses,
5451 but refraining from recursing into subclasses that define 'name'. */
5453 static int
5454 update_subclasses(PyTypeObject *type, PyObject *name,
5455 update_callback callback, void *data)
5457 if (callback(type, data) < 0)
5458 return -1;
5459 return recurse_down_subclasses(type, name, callback, data);
5462 static int
5463 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5464 update_callback callback, void *data)
5466 PyTypeObject *subclass;
5467 PyObject *ref, *subclasses, *dict;
5468 Py_ssize_t i, n;
5470 subclasses = type->tp_subclasses;
5471 if (subclasses == NULL)
5472 return 0;
5473 assert(PyList_Check(subclasses));
5474 n = PyList_GET_SIZE(subclasses);
5475 for (i = 0; i < n; i++) {
5476 ref = PyList_GET_ITEM(subclasses, i);
5477 assert(PyWeakref_CheckRef(ref));
5478 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5479 assert(subclass != NULL);
5480 if ((PyObject *)subclass == Py_None)
5481 continue;
5482 assert(PyType_Check(subclass));
5483 /* Avoid recursing down into unaffected classes */
5484 dict = subclass->tp_dict;
5485 if (dict != NULL && PyDict_Check(dict) &&
5486 PyDict_GetItem(dict, name) != NULL)
5487 continue;
5488 if (update_subclasses(subclass, name, callback, data) < 0)
5489 return -1;
5491 return 0;
5494 /* This function is called by PyType_Ready() to populate the type's
5495 dictionary with method descriptors for function slots. For each
5496 function slot (like tp_repr) that's defined in the type, one or more
5497 corresponding descriptors are added in the type's tp_dict dictionary
5498 under the appropriate name (like __repr__). Some function slots
5499 cause more than one descriptor to be added (for example, the nb_add
5500 slot adds both __add__ and __radd__ descriptors) and some function
5501 slots compete for the same descriptor (for example both sq_item and
5502 mp_subscript generate a __getitem__ descriptor).
5504 In the latter case, the first slotdef entry encoutered wins. Since
5505 slotdef entries are sorted by the offset of the slot in the
5506 PyHeapTypeObject, this gives us some control over disambiguating
5507 between competing slots: the members of PyHeapTypeObject are listed
5508 from most general to least general, so the most general slot is
5509 preferred. In particular, because as_mapping comes before as_sequence,
5510 for a type that defines both mp_subscript and sq_item, mp_subscript
5511 wins.
5513 This only adds new descriptors and doesn't overwrite entries in
5514 tp_dict that were previously defined. The descriptors contain a
5515 reference to the C function they must call, so that it's safe if they
5516 are copied into a subtype's __dict__ and the subtype has a different
5517 C function in its slot -- calling the method defined by the
5518 descriptor will call the C function that was used to create it,
5519 rather than the C function present in the slot when it is called.
5520 (This is important because a subtype may have a C function in the
5521 slot that calls the method from the dictionary, and we want to avoid
5522 infinite recursion here.) */
5524 static int
5525 add_operators(PyTypeObject *type)
5527 PyObject *dict = type->tp_dict;
5528 slotdef *p;
5529 PyObject *descr;
5530 void **ptr;
5532 init_slotdefs();
5533 for (p = slotdefs; p->name; p++) {
5534 if (p->wrapper == NULL)
5535 continue;
5536 ptr = slotptr(type, p->offset);
5537 if (!ptr || !*ptr)
5538 continue;
5539 if (PyDict_GetItem(dict, p->name_strobj))
5540 continue;
5541 descr = PyDescr_NewWrapper(type, p, *ptr);
5542 if (descr == NULL)
5543 return -1;
5544 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5545 return -1;
5546 Py_DECREF(descr);
5548 if (type->tp_new != NULL) {
5549 if (add_tp_new_wrapper(type) < 0)
5550 return -1;
5552 return 0;
5556 /* Cooperative 'super' */
5558 typedef struct {
5559 PyObject_HEAD
5560 PyTypeObject *type;
5561 PyObject *obj;
5562 PyTypeObject *obj_type;
5563 } superobject;
5565 static PyMemberDef super_members[] = {
5566 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5567 "the class invoking super()"},
5568 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5569 "the instance invoking super(); may be None"},
5570 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5571 "the type of the instance invoking super(); may be None"},
5575 static void
5576 super_dealloc(PyObject *self)
5578 superobject *su = (superobject *)self;
5580 _PyObject_GC_UNTRACK(self);
5581 Py_XDECREF(su->obj);
5582 Py_XDECREF(su->type);
5583 Py_XDECREF(su->obj_type);
5584 self->ob_type->tp_free(self);
5587 static PyObject *
5588 super_repr(PyObject *self)
5590 superobject *su = (superobject *)self;
5592 if (su->obj_type)
5593 return PyString_FromFormat(
5594 "<super: <class '%s'>, <%s object>>",
5595 su->type ? su->type->tp_name : "NULL",
5596 su->obj_type->tp_name);
5597 else
5598 return PyString_FromFormat(
5599 "<super: <class '%s'>, NULL>",
5600 su->type ? su->type->tp_name : "NULL");
5603 static PyObject *
5604 super_getattro(PyObject *self, PyObject *name)
5606 superobject *su = (superobject *)self;
5607 int skip = su->obj_type == NULL;
5609 if (!skip) {
5610 /* We want __class__ to return the class of the super object
5611 (i.e. super, or a subclass), not the class of su->obj. */
5612 skip = (PyString_Check(name) &&
5613 PyString_GET_SIZE(name) == 9 &&
5614 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5617 if (!skip) {
5618 PyObject *mro, *res, *tmp, *dict;
5619 PyTypeObject *starttype;
5620 descrgetfunc f;
5621 Py_ssize_t i, n;
5623 starttype = su->obj_type;
5624 mro = starttype->tp_mro;
5626 if (mro == NULL)
5627 n = 0;
5628 else {
5629 assert(PyTuple_Check(mro));
5630 n = PyTuple_GET_SIZE(mro);
5632 for (i = 0; i < n; i++) {
5633 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
5634 break;
5636 i++;
5637 res = NULL;
5638 for (; i < n; i++) {
5639 tmp = PyTuple_GET_ITEM(mro, i);
5640 if (PyType_Check(tmp))
5641 dict = ((PyTypeObject *)tmp)->tp_dict;
5642 else if (PyClass_Check(tmp))
5643 dict = ((PyClassObject *)tmp)->cl_dict;
5644 else
5645 continue;
5646 res = PyDict_GetItem(dict, name);
5647 if (res != NULL) {
5648 Py_INCREF(res);
5649 f = res->ob_type->tp_descr_get;
5650 if (f != NULL) {
5651 tmp = f(res,
5652 /* Only pass 'obj' param if
5653 this is instance-mode super
5654 (See SF ID #743627)
5656 (su->obj == (PyObject *)
5657 su->obj_type
5658 ? (PyObject *)NULL
5659 : su->obj),
5660 (PyObject *)starttype);
5661 Py_DECREF(res);
5662 res = tmp;
5664 return res;
5668 return PyObject_GenericGetAttr(self, name);
5671 static PyTypeObject *
5672 supercheck(PyTypeObject *type, PyObject *obj)
5674 /* Check that a super() call makes sense. Return a type object.
5676 obj can be a new-style class, or an instance of one:
5678 - If it is a class, it must be a subclass of 'type'. This case is
5679 used for class methods; the return value is obj.
5681 - If it is an instance, it must be an instance of 'type'. This is
5682 the normal case; the return value is obj.__class__.
5684 But... when obj is an instance, we want to allow for the case where
5685 obj->ob_type is not a subclass of type, but obj.__class__ is!
5686 This will allow using super() with a proxy for obj.
5689 /* Check for first bullet above (special case) */
5690 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5691 Py_INCREF(obj);
5692 return (PyTypeObject *)obj;
5695 /* Normal case */
5696 if (PyType_IsSubtype(obj->ob_type, type)) {
5697 Py_INCREF(obj->ob_type);
5698 return obj->ob_type;
5700 else {
5701 /* Try the slow way */
5702 static PyObject *class_str = NULL;
5703 PyObject *class_attr;
5705 if (class_str == NULL) {
5706 class_str = PyString_FromString("__class__");
5707 if (class_str == NULL)
5708 return NULL;
5711 class_attr = PyObject_GetAttr(obj, class_str);
5713 if (class_attr != NULL &&
5714 PyType_Check(class_attr) &&
5715 (PyTypeObject *)class_attr != obj->ob_type)
5717 int ok = PyType_IsSubtype(
5718 (PyTypeObject *)class_attr, type);
5719 if (ok)
5720 return (PyTypeObject *)class_attr;
5723 if (class_attr == NULL)
5724 PyErr_Clear();
5725 else
5726 Py_DECREF(class_attr);
5729 PyErr_SetString(PyExc_TypeError,
5730 "super(type, obj): "
5731 "obj must be an instance or subtype of type");
5732 return NULL;
5735 static PyObject *
5736 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5738 superobject *su = (superobject *)self;
5739 superobject *newobj;
5741 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5742 /* Not binding to an object, or already bound */
5743 Py_INCREF(self);
5744 return self;
5746 if (su->ob_type != &PySuper_Type)
5747 /* If su is an instance of a (strict) subclass of super,
5748 call its type */
5749 return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
5750 su->type, obj, NULL);
5751 else {
5752 /* Inline the common case */
5753 PyTypeObject *obj_type = supercheck(su->type, obj);
5754 if (obj_type == NULL)
5755 return NULL;
5756 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5757 NULL, NULL);
5758 if (newobj == NULL)
5759 return NULL;
5760 Py_INCREF(su->type);
5761 Py_INCREF(obj);
5762 newobj->type = su->type;
5763 newobj->obj = obj;
5764 newobj->obj_type = obj_type;
5765 return (PyObject *)newobj;
5769 static int
5770 super_init(PyObject *self, PyObject *args, PyObject *kwds)
5772 superobject *su = (superobject *)self;
5773 PyTypeObject *type;
5774 PyObject *obj = NULL;
5775 PyTypeObject *obj_type = NULL;
5777 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5778 return -1;
5779 if (obj == Py_None)
5780 obj = NULL;
5781 if (obj != NULL) {
5782 obj_type = supercheck(type, obj);
5783 if (obj_type == NULL)
5784 return -1;
5785 Py_INCREF(obj);
5787 Py_INCREF(type);
5788 su->type = type;
5789 su->obj = obj;
5790 su->obj_type = obj_type;
5791 return 0;
5794 PyDoc_STRVAR(super_doc,
5795 "super(type) -> unbound super object\n"
5796 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5797 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5798 "Typical use to call a cooperative superclass method:\n"
5799 "class C(B):\n"
5800 " def meth(self, arg):\n"
5801 " super(C, self).meth(arg)");
5803 static int
5804 super_traverse(PyObject *self, visitproc visit, void *arg)
5806 superobject *su = (superobject *)self;
5808 Py_VISIT(su->obj);
5809 Py_VISIT(su->type);
5810 Py_VISIT(su->obj_type);
5812 return 0;
5815 PyTypeObject PySuper_Type = {
5816 PyObject_HEAD_INIT(&PyType_Type)
5817 0, /* ob_size */
5818 "super", /* tp_name */
5819 sizeof(superobject), /* tp_basicsize */
5820 0, /* tp_itemsize */
5821 /* methods */
5822 super_dealloc, /* tp_dealloc */
5823 0, /* tp_print */
5824 0, /* tp_getattr */
5825 0, /* tp_setattr */
5826 0, /* tp_compare */
5827 super_repr, /* tp_repr */
5828 0, /* tp_as_number */
5829 0, /* tp_as_sequence */
5830 0, /* tp_as_mapping */
5831 0, /* tp_hash */
5832 0, /* tp_call */
5833 0, /* tp_str */
5834 super_getattro, /* tp_getattro */
5835 0, /* tp_setattro */
5836 0, /* tp_as_buffer */
5837 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5838 Py_TPFLAGS_BASETYPE, /* tp_flags */
5839 super_doc, /* tp_doc */
5840 super_traverse, /* tp_traverse */
5841 0, /* tp_clear */
5842 0, /* tp_richcompare */
5843 0, /* tp_weaklistoffset */
5844 0, /* tp_iter */
5845 0, /* tp_iternext */
5846 0, /* tp_methods */
5847 super_members, /* tp_members */
5848 0, /* tp_getset */
5849 0, /* tp_base */
5850 0, /* tp_dict */
5851 super_descr_get, /* tp_descr_get */
5852 0, /* tp_descr_set */
5853 0, /* tp_dictoffset */
5854 super_init, /* tp_init */
5855 PyType_GenericAlloc, /* tp_alloc */
5856 PyType_GenericNew, /* tp_new */
5857 PyObject_GC_Del, /* tp_free */