Whitespace normalization.
[python/dscho.git] / Objects / typeobject.c
blob1a052c78c145dfd55c4772ab1e6dd260f5ef0d8c
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 char *s;
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
29 Py_INCREF(et->name);
30 return et->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->name);
75 et->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 int 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 int i, r = 0;
188 PyObject *ob, *temp;
189 PyTypeObject *new_base, *old_base;
190 PyObject *old_bases, *old_mro;
192 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
193 PyErr_Format(PyExc_TypeError,
194 "can't set %s.__bases__", type->tp_name);
195 return -1;
197 if (!value) {
198 PyErr_Format(PyExc_TypeError,
199 "can't delete %s.__bases__", type->tp_name);
200 return -1;
202 if (!PyTuple_Check(value)) {
203 PyErr_Format(PyExc_TypeError,
204 "can only assign tuple to %s.__bases__, not %s",
205 type->tp_name, value->ob_type->tp_name);
206 return -1;
208 if (PyTuple_GET_SIZE(value) == 0) {
209 PyErr_Format(PyExc_TypeError,
210 "can only assign non-empty tuple to %s.__bases__, not ()",
211 type->tp_name);
212 return -1;
214 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
215 ob = PyTuple_GET_ITEM(value, i);
216 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
217 PyErr_Format(
218 PyExc_TypeError,
219 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
220 type->tp_name, ob->ob_type->tp_name);
221 return -1;
223 if (PyType_Check(ob)) {
224 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
225 PyErr_SetString(PyExc_TypeError,
226 "a __bases__ item causes an inheritance cycle");
227 return -1;
232 new_base = best_base(value);
234 if (!new_base) {
235 return -1;
238 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
239 return -1;
241 Py_INCREF(new_base);
242 Py_INCREF(value);
244 old_bases = type->tp_bases;
245 old_base = type->tp_base;
246 old_mro = type->tp_mro;
248 type->tp_bases = value;
249 type->tp_base = new_base;
251 if (mro_internal(type) < 0) {
252 goto bail;
255 temp = PyList_New(0);
256 if (!temp)
257 goto bail;
259 r = mro_subclasses(type, temp);
261 if (r < 0) {
262 for (i = 0; i < PyList_Size(temp); i++) {
263 PyTypeObject* cls;
264 PyObject* mro;
265 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
266 "", 2, 2, &cls, &mro);
267 Py_DECREF(cls->tp_mro);
268 cls->tp_mro = mro;
269 Py_INCREF(cls->tp_mro);
271 Py_DECREF(temp);
272 goto bail;
275 Py_DECREF(temp);
277 /* any base that was in __bases__ but now isn't, we
278 need to remove |type| from its tp_subclasses.
279 conversely, any class now in __bases__ that wasn't
280 needs to have |type| added to its subclasses. */
282 /* for now, sod that: just remove from all old_bases,
283 add to all new_bases */
285 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
286 ob = PyTuple_GET_ITEM(old_bases, i);
287 if (PyType_Check(ob)) {
288 remove_subclass(
289 (PyTypeObject*)ob, type);
293 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
294 ob = PyTuple_GET_ITEM(value, i);
295 if (PyType_Check(ob)) {
296 if (add_subclass((PyTypeObject*)ob, type) < 0)
297 r = -1;
301 update_all_slots(type);
303 Py_DECREF(old_bases);
304 Py_DECREF(old_base);
305 Py_DECREF(old_mro);
307 return r;
309 bail:
310 Py_DECREF(type->tp_bases);
311 Py_DECREF(type->tp_base);
312 if (type->tp_mro != old_mro) {
313 Py_DECREF(type->tp_mro);
316 type->tp_bases = old_bases;
317 type->tp_base = old_base;
318 type->tp_mro = old_mro;
320 return -1;
323 static PyObject *
324 type_dict(PyTypeObject *type, void *context)
326 if (type->tp_dict == NULL) {
327 Py_INCREF(Py_None);
328 return Py_None;
330 return PyDictProxy_New(type->tp_dict);
333 static PyObject *
334 type_get_doc(PyTypeObject *type, void *context)
336 PyObject *result;
337 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
338 return PyString_FromString(type->tp_doc);
339 result = PyDict_GetItemString(type->tp_dict, "__doc__");
340 if (result == NULL) {
341 result = Py_None;
342 Py_INCREF(result);
344 else if (result->ob_type->tp_descr_get) {
345 result = result->ob_type->tp_descr_get(result, NULL,
346 (PyObject *)type);
348 else {
349 Py_INCREF(result);
351 return result;
354 static PyGetSetDef type_getsets[] = {
355 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
356 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
357 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
358 {"__dict__", (getter)type_dict, NULL, NULL},
359 {"__doc__", (getter)type_get_doc, NULL, NULL},
363 static int
364 type_compare(PyObject *v, PyObject *w)
366 /* This is called with type objects only. So we
367 can just compare the addresses. */
368 Py_uintptr_t vv = (Py_uintptr_t)v;
369 Py_uintptr_t ww = (Py_uintptr_t)w;
370 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
373 static PyObject *
374 type_repr(PyTypeObject *type)
376 PyObject *mod, *name, *rtn;
377 char *kind;
379 mod = type_module(type, NULL);
380 if (mod == NULL)
381 PyErr_Clear();
382 else if (!PyString_Check(mod)) {
383 Py_DECREF(mod);
384 mod = NULL;
386 name = type_name(type, NULL);
387 if (name == NULL)
388 return NULL;
390 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
391 kind = "class";
392 else
393 kind = "type";
395 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
396 rtn = PyString_FromFormat("<%s '%s.%s'>",
397 kind,
398 PyString_AS_STRING(mod),
399 PyString_AS_STRING(name));
401 else
402 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
404 Py_XDECREF(mod);
405 Py_DECREF(name);
406 return rtn;
409 static PyObject *
410 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
412 PyObject *obj;
414 if (type->tp_new == NULL) {
415 PyErr_Format(PyExc_TypeError,
416 "cannot create '%.100s' instances",
417 type->tp_name);
418 return NULL;
421 obj = type->tp_new(type, args, kwds);
422 if (obj != NULL) {
423 /* Ugly exception: when the call was type(something),
424 don't call tp_init on the result. */
425 if (type == &PyType_Type &&
426 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
427 (kwds == NULL ||
428 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
429 return obj;
430 /* If the returned object is not an instance of type,
431 it won't be initialized. */
432 if (!PyType_IsSubtype(obj->ob_type, type))
433 return obj;
434 type = obj->ob_type;
435 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
436 type->tp_init != NULL &&
437 type->tp_init(obj, args, kwds) < 0) {
438 Py_DECREF(obj);
439 obj = NULL;
442 return obj;
445 PyObject *
446 PyType_GenericAlloc(PyTypeObject *type, int nitems)
448 PyObject *obj;
449 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
450 /* note that we need to add one, for the sentinel */
452 if (PyType_IS_GC(type))
453 obj = _PyObject_GC_Malloc(size);
454 else
455 obj = PyObject_MALLOC(size);
457 if (obj == NULL)
458 return PyErr_NoMemory();
460 memset(obj, '\0', size);
462 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
463 Py_INCREF(type);
465 if (type->tp_itemsize == 0)
466 PyObject_INIT(obj, type);
467 else
468 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
470 if (PyType_IS_GC(type))
471 _PyObject_GC_TRACK(obj);
472 return obj;
475 PyObject *
476 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
478 return type->tp_alloc(type, 0);
481 /* Helpers for subtyping */
483 static int
484 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
486 int i, n;
487 PyMemberDef *mp;
489 n = type->ob_size;
490 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
491 for (i = 0; i < n; i++, mp++) {
492 if (mp->type == T_OBJECT_EX) {
493 char *addr = (char *)self + mp->offset;
494 PyObject *obj = *(PyObject **)addr;
495 if (obj != NULL) {
496 int err = visit(obj, arg);
497 if (err)
498 return err;
502 return 0;
505 static int
506 subtype_traverse(PyObject *self, visitproc visit, void *arg)
508 PyTypeObject *type, *base;
509 traverseproc basetraverse;
511 /* Find the nearest base with a different tp_traverse,
512 and traverse slots while we're at it */
513 type = self->ob_type;
514 base = type;
515 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
516 if (base->ob_size) {
517 int err = traverse_slots(base, self, visit, arg);
518 if (err)
519 return err;
521 base = base->tp_base;
522 assert(base);
525 if (type->tp_dictoffset != base->tp_dictoffset) {
526 PyObject **dictptr = _PyObject_GetDictPtr(self);
527 if (dictptr && *dictptr) {
528 int err = visit(*dictptr, arg);
529 if (err)
530 return err;
534 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
535 /* For a heaptype, the instances count as references
536 to the type. Traverse the type so the collector
537 can find cycles involving this link. */
538 int err = visit((PyObject *)type, arg);
539 if (err)
540 return err;
543 if (basetraverse)
544 return basetraverse(self, visit, arg);
545 return 0;
548 static void
549 clear_slots(PyTypeObject *type, PyObject *self)
551 int i, n;
552 PyMemberDef *mp;
554 n = type->ob_size;
555 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
556 for (i = 0; i < n; i++, mp++) {
557 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
558 char *addr = (char *)self + mp->offset;
559 PyObject *obj = *(PyObject **)addr;
560 if (obj != NULL) {
561 Py_DECREF(obj);
562 *(PyObject **)addr = NULL;
568 static int
569 subtype_clear(PyObject *self)
571 PyTypeObject *type, *base;
572 inquiry baseclear;
574 /* Find the nearest base with a different tp_clear
575 and clear slots while we're at it */
576 type = self->ob_type;
577 base = type;
578 while ((baseclear = base->tp_clear) == subtype_clear) {
579 if (base->ob_size)
580 clear_slots(base, self);
581 base = base->tp_base;
582 assert(base);
585 /* There's no need to clear the instance dict (if any);
586 the collector will call its tp_clear handler. */
588 if (baseclear)
589 return baseclear(self);
590 return 0;
593 static void
594 subtype_dealloc(PyObject *self)
596 PyTypeObject *type, *base;
597 destructor basedealloc;
599 /* Extract the type; we expect it to be a heap type */
600 type = self->ob_type;
601 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
603 /* Test whether the type has GC exactly once */
605 if (!PyType_IS_GC(type)) {
606 /* It's really rare to find a dynamic type that doesn't have
607 GC; it can only happen when deriving from 'object' and not
608 adding any slots or instance variables. This allows
609 certain simplifications: there's no need to call
610 clear_slots(), or DECREF the dict, or clear weakrefs. */
612 /* Maybe call finalizer; exit early if resurrected */
613 if (type->tp_del) {
614 type->tp_del(self);
615 if (self->ob_refcnt > 0)
616 return;
619 /* Find the nearest base with a different tp_dealloc */
620 base = type;
621 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
622 assert(base->ob_size == 0);
623 base = base->tp_base;
624 assert(base);
627 /* Call the base tp_dealloc() */
628 assert(basedealloc);
629 basedealloc(self);
631 /* Can't reference self beyond this point */
632 Py_DECREF(type);
634 /* Done */
635 return;
638 /* We get here only if the type has GC */
640 /* UnTrack and re-Track around the trashcan macro, alas */
641 /* See explanation at end of function for full disclosure */
642 PyObject_GC_UnTrack(self);
643 ++_PyTrash_delete_nesting;
644 Py_TRASHCAN_SAFE_BEGIN(self);
645 --_PyTrash_delete_nesting;
646 /* DO NOT restore GC tracking at this point. weakref callbacks
647 * (if any, and whether directly here or indirectly in something we
648 * call) may trigger GC, and if self is tracked at that point, it
649 * will look like trash to GC and GC will try to delete self again.
652 /* Find the nearest base with a different tp_dealloc */
653 base = type;
654 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
655 base = base->tp_base;
656 assert(base);
659 /* If we added a weaklist, we clear it. Do this *before* calling
660 the finalizer (__del__), clearing slots, or clearing the instance
661 dict. */
663 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
664 PyObject_ClearWeakRefs(self);
666 /* Maybe call finalizer; exit early if resurrected */
667 if (type->tp_del) {
668 _PyObject_GC_TRACK(self);
669 type->tp_del(self);
670 if (self->ob_refcnt > 0)
671 goto endlabel; /* resurrected */
672 else
673 _PyObject_GC_UNTRACK(self);
676 /* Clear slots up to the nearest base with a different tp_dealloc */
677 base = type;
678 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
679 if (base->ob_size)
680 clear_slots(base, self);
681 base = base->tp_base;
682 assert(base);
685 /* If we added a dict, DECREF it */
686 if (type->tp_dictoffset && !base->tp_dictoffset) {
687 PyObject **dictptr = _PyObject_GetDictPtr(self);
688 if (dictptr != NULL) {
689 PyObject *dict = *dictptr;
690 if (dict != NULL) {
691 Py_DECREF(dict);
692 *dictptr = NULL;
697 /* Call the base tp_dealloc(); first retrack self if
698 * basedealloc knows about gc.
700 if (PyType_IS_GC(base))
701 _PyObject_GC_TRACK(self);
702 assert(basedealloc);
703 basedealloc(self);
705 /* Can't reference self beyond this point */
706 Py_DECREF(type);
708 endlabel:
709 ++_PyTrash_delete_nesting;
710 Py_TRASHCAN_SAFE_END(self);
711 --_PyTrash_delete_nesting;
713 /* Explanation of the weirdness around the trashcan macros:
715 Q. What do the trashcan macros do?
717 A. Read the comment titled "Trashcan mechanism" in object.h.
718 For one, this explains why there must be a call to GC-untrack
719 before the trashcan begin macro. Without understanding the
720 trashcan code, the answers to the following questions don't make
721 sense.
723 Q. Why do we GC-untrack before the trashcan and then immediately
724 GC-track again afterward?
726 A. In the case that the base class is GC-aware, the base class
727 probably GC-untracks the object. If it does that using the
728 UNTRACK macro, this will crash when the object is already
729 untracked. Because we don't know what the base class does, the
730 only safe thing is to make sure the object is tracked when we
731 call the base class dealloc. But... The trashcan begin macro
732 requires that the object is *untracked* before it is called. So
733 the dance becomes:
735 GC untrack
736 trashcan begin
737 GC track
739 Q. Why did the last question say "immediately GC-track again"?
740 It's nowhere near immediately.
742 A. Because the code *used* to re-track immediately. Bad Idea.
743 self has a refcount of 0, and if gc ever gets its hands on it
744 (which can happen if any weakref callback gets invoked), it
745 looks like trash to gc too, and gc also tries to delete self
746 then. But we're already deleting self. Double dealloction is
747 a subtle disaster.
749 Q. Why the bizarre (net-zero) manipulation of
750 _PyTrash_delete_nesting around the trashcan macros?
752 A. Some base classes (e.g. list) also use the trashcan mechanism.
753 The following scenario used to be possible:
755 - suppose the trashcan level is one below the trashcan limit
757 - subtype_dealloc() is called
759 - the trashcan limit is not yet reached, so the trashcan level
760 is incremented and the code between trashcan begin and end is
761 executed
763 - this destroys much of the object's contents, including its
764 slots and __dict__
766 - basedealloc() is called; this is really list_dealloc(), or
767 some other type which also uses the trashcan macros
769 - the trashcan limit is now reached, so the object is put on the
770 trashcan's to-be-deleted-later list
772 - basedealloc() returns
774 - subtype_dealloc() decrefs the object's type
776 - subtype_dealloc() returns
778 - later, the trashcan code starts deleting the objects from its
779 to-be-deleted-later list
781 - subtype_dealloc() is called *AGAIN* for the same object
783 - at the very least (if the destroyed slots and __dict__ don't
784 cause problems) the object's type gets decref'ed a second
785 time, which is *BAD*!!!
787 The remedy is to make sure that if the code between trashcan
788 begin and end in subtype_dealloc() is called, the code between
789 trashcan begin and end in basedealloc() will also be called.
790 This is done by decrementing the level after passing into the
791 trashcan block, and incrementing it just before leaving the
792 block.
794 But now it's possible that a chain of objects consisting solely
795 of objects whose deallocator is subtype_dealloc() will defeat
796 the trashcan mechanism completely: the decremented level means
797 that the effective level never reaches the limit. Therefore, we
798 *increment* the level *before* entering the trashcan block, and
799 matchingly decrement it after leaving. This means the trashcan
800 code will trigger a little early, but that's no big deal.
802 Q. Are there any live examples of code in need of all this
803 complexity?
805 A. Yes. See SF bug 668433 for code that crashed (when Python was
806 compiled in debug mode) before the trashcan level manipulations
807 were added. For more discussion, see SF patches 581742, 575073
808 and bug 574207.
812 static PyTypeObject *solid_base(PyTypeObject *type);
814 /* type test with subclassing support */
817 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
819 PyObject *mro;
821 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
822 return b == a || b == &PyBaseObject_Type;
824 mro = a->tp_mro;
825 if (mro != NULL) {
826 /* Deal with multiple inheritance without recursion
827 by walking the MRO tuple */
828 int i, n;
829 assert(PyTuple_Check(mro));
830 n = PyTuple_GET_SIZE(mro);
831 for (i = 0; i < n; i++) {
832 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
833 return 1;
835 return 0;
837 else {
838 /* a is not completely initilized yet; follow tp_base */
839 do {
840 if (a == b)
841 return 1;
842 a = a->tp_base;
843 } while (a != NULL);
844 return b == &PyBaseObject_Type;
848 /* Internal routines to do a method lookup in the type
849 without looking in the instance dictionary
850 (so we can't use PyObject_GetAttr) but still binding
851 it to the instance. The arguments are the object,
852 the method name as a C string, and the address of a
853 static variable used to cache the interned Python string.
855 Two variants:
857 - lookup_maybe() returns NULL without raising an exception
858 when the _PyType_Lookup() call fails;
860 - lookup_method() always raises an exception upon errors.
863 static PyObject *
864 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
866 PyObject *res;
868 if (*attrobj == NULL) {
869 *attrobj = PyString_InternFromString(attrstr);
870 if (*attrobj == NULL)
871 return NULL;
873 res = _PyType_Lookup(self->ob_type, *attrobj);
874 if (res != NULL) {
875 descrgetfunc f;
876 if ((f = res->ob_type->tp_descr_get) == NULL)
877 Py_INCREF(res);
878 else
879 res = f(res, self, (PyObject *)(self->ob_type));
881 return res;
884 static PyObject *
885 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
887 PyObject *res = lookup_maybe(self, attrstr, attrobj);
888 if (res == NULL && !PyErr_Occurred())
889 PyErr_SetObject(PyExc_AttributeError, *attrobj);
890 return res;
893 /* A variation of PyObject_CallMethod that uses lookup_method()
894 instead of PyObject_GetAttrString(). This uses the same convention
895 as lookup_method to cache the interned name string object. */
897 static PyObject *
898 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
900 va_list va;
901 PyObject *args, *func = 0, *retval;
902 va_start(va, format);
904 func = lookup_maybe(o, name, nameobj);
905 if (func == NULL) {
906 va_end(va);
907 if (!PyErr_Occurred())
908 PyErr_SetObject(PyExc_AttributeError, *nameobj);
909 return NULL;
912 if (format && *format)
913 args = Py_VaBuildValue(format, va);
914 else
915 args = PyTuple_New(0);
917 va_end(va);
919 if (args == NULL)
920 return NULL;
922 assert(PyTuple_Check(args));
923 retval = PyObject_Call(func, args, NULL);
925 Py_DECREF(args);
926 Py_DECREF(func);
928 return retval;
931 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
933 static PyObject *
934 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
936 va_list va;
937 PyObject *args, *func = 0, *retval;
938 va_start(va, format);
940 func = lookup_maybe(o, name, nameobj);
941 if (func == NULL) {
942 va_end(va);
943 if (!PyErr_Occurred()) {
944 Py_INCREF(Py_NotImplemented);
945 return Py_NotImplemented;
947 return NULL;
950 if (format && *format)
951 args = Py_VaBuildValue(format, va);
952 else
953 args = PyTuple_New(0);
955 va_end(va);
957 if (args == NULL)
958 return NULL;
960 assert(PyTuple_Check(args));
961 retval = PyObject_Call(func, args, NULL);
963 Py_DECREF(args);
964 Py_DECREF(func);
966 return retval;
969 static int
970 fill_classic_mro(PyObject *mro, PyObject *cls)
972 PyObject *bases, *base;
973 int i, n;
975 assert(PyList_Check(mro));
976 assert(PyClass_Check(cls));
977 i = PySequence_Contains(mro, cls);
978 if (i < 0)
979 return -1;
980 if (!i) {
981 if (PyList_Append(mro, cls) < 0)
982 return -1;
984 bases = ((PyClassObject *)cls)->cl_bases;
985 assert(bases && PyTuple_Check(bases));
986 n = PyTuple_GET_SIZE(bases);
987 for (i = 0; i < n; i++) {
988 base = PyTuple_GET_ITEM(bases, i);
989 if (fill_classic_mro(mro, base) < 0)
990 return -1;
992 return 0;
995 static PyObject *
996 classic_mro(PyObject *cls)
998 PyObject *mro;
1000 assert(PyClass_Check(cls));
1001 mro = PyList_New(0);
1002 if (mro != NULL) {
1003 if (fill_classic_mro(mro, cls) == 0)
1004 return mro;
1005 Py_DECREF(mro);
1007 return NULL;
1011 Method resolution order algorithm C3 described in
1012 "A Monotonic Superclass Linearization for Dylan",
1013 by Kim Barrett, Bob Cassel, Paul Haahr,
1014 David A. Moon, Keith Playford, and P. Tucker Withington.
1015 (OOPSLA 1996)
1017 Some notes about the rules implied by C3:
1019 No duplicate bases.
1020 It isn't legal to repeat a class in a list of base classes.
1022 The next three properties are the 3 constraints in "C3".
1024 Local precendece order.
1025 If A precedes B in C's MRO, then A will precede B in the MRO of all
1026 subclasses of C.
1028 Monotonicity.
1029 The MRO of a class must be an extension without reordering of the
1030 MRO of each of its superclasses.
1032 Extended Precedence Graph (EPG).
1033 Linearization is consistent if there is a path in the EPG from
1034 each class to all its successors in the linearization. See
1035 the paper for definition of EPG.
1038 static int
1039 tail_contains(PyObject *list, int whence, PyObject *o) {
1040 int j, size;
1041 size = PyList_GET_SIZE(list);
1043 for (j = whence+1; j < size; j++) {
1044 if (PyList_GET_ITEM(list, j) == o)
1045 return 1;
1047 return 0;
1050 static PyObject *
1051 class_name(PyObject *cls)
1053 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1054 if (name == NULL) {
1055 PyErr_Clear();
1056 Py_XDECREF(name);
1057 name = PyObject_Repr(cls);
1059 if (name == NULL)
1060 return NULL;
1061 if (!PyString_Check(name)) {
1062 Py_DECREF(name);
1063 return NULL;
1065 return name;
1068 static int
1069 check_duplicates(PyObject *list)
1071 int i, j, n;
1072 /* Let's use a quadratic time algorithm,
1073 assuming that the bases lists is short.
1075 n = PyList_GET_SIZE(list);
1076 for (i = 0; i < n; i++) {
1077 PyObject *o = PyList_GET_ITEM(list, i);
1078 for (j = i + 1; j < n; j++) {
1079 if (PyList_GET_ITEM(list, j) == o) {
1080 o = class_name(o);
1081 PyErr_Format(PyExc_TypeError,
1082 "duplicate base class %s",
1083 o ? PyString_AS_STRING(o) : "?");
1084 Py_XDECREF(o);
1085 return -1;
1089 return 0;
1092 /* Raise a TypeError for an MRO order disagreement.
1094 It's hard to produce a good error message. In the absence of better
1095 insight into error reporting, report the classes that were candidates
1096 to be put next into the MRO. There is some conflict between the
1097 order in which they should be put in the MRO, but it's hard to
1098 diagnose what constraint can't be satisfied.
1101 static void
1102 set_mro_error(PyObject *to_merge, int *remain)
1104 int i, n, off, to_merge_size;
1105 char buf[1000];
1106 PyObject *k, *v;
1107 PyObject *set = PyDict_New();
1109 to_merge_size = PyList_GET_SIZE(to_merge);
1110 for (i = 0; i < to_merge_size; i++) {
1111 PyObject *L = PyList_GET_ITEM(to_merge, i);
1112 if (remain[i] < PyList_GET_SIZE(L)) {
1113 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1114 if (PyDict_SetItem(set, c, Py_None) < 0)
1115 return;
1118 n = PyDict_Size(set);
1120 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1121 consistent method resolution\norder (MRO) for bases");
1122 i = 0;
1123 while (PyDict_Next(set, &i, &k, &v) && off < sizeof(buf)) {
1124 PyObject *name = class_name(k);
1125 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1126 name ? PyString_AS_STRING(name) : "?");
1127 Py_XDECREF(name);
1128 if (--n && off+1 < sizeof(buf)) {
1129 buf[off++] = ',';
1130 buf[off] = '\0';
1133 PyErr_SetString(PyExc_TypeError, buf);
1134 Py_DECREF(set);
1137 static int
1138 pmerge(PyObject *acc, PyObject* to_merge) {
1139 int i, j, to_merge_size;
1140 int *remain;
1141 int ok, empty_cnt;
1143 to_merge_size = PyList_GET_SIZE(to_merge);
1145 /* remain stores an index into each sublist of to_merge.
1146 remain[i] is the index of the next base in to_merge[i]
1147 that is not included in acc.
1149 remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1150 if (remain == NULL)
1151 return -1;
1152 for (i = 0; i < to_merge_size; i++)
1153 remain[i] = 0;
1155 again:
1156 empty_cnt = 0;
1157 for (i = 0; i < to_merge_size; i++) {
1158 PyObject *candidate;
1160 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1162 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1163 empty_cnt++;
1164 continue;
1167 /* Choose next candidate for MRO.
1169 The input sequences alone can determine the choice.
1170 If not, choose the class which appears in the MRO
1171 of the earliest direct superclass of the new class.
1174 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1175 for (j = 0; j < to_merge_size; j++) {
1176 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1177 if (tail_contains(j_lst, remain[j], candidate)) {
1178 goto skip; /* continue outer loop */
1181 ok = PyList_Append(acc, candidate);
1182 if (ok < 0) {
1183 PyMem_Free(remain);
1184 return -1;
1186 for (j = 0; j < to_merge_size; j++) {
1187 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1188 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1189 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1190 remain[j]++;
1193 goto again;
1194 skip: ;
1197 if (empty_cnt == to_merge_size) {
1198 PyMem_FREE(remain);
1199 return 0;
1201 set_mro_error(to_merge, remain);
1202 PyMem_FREE(remain);
1203 return -1;
1206 static PyObject *
1207 mro_implementation(PyTypeObject *type)
1209 int i, n, ok;
1210 PyObject *bases, *result;
1211 PyObject *to_merge, *bases_aslist;
1213 if(type->tp_dict == NULL) {
1214 if(PyType_Ready(type) < 0)
1215 return NULL;
1218 /* Find a superclass linearization that honors the constraints
1219 of the explicit lists of bases and the constraints implied by
1220 each base class.
1222 to_merge is a list of lists, where each list is a superclass
1223 linearization implied by a base class. The last element of
1224 to_merge is the declared list of bases.
1227 bases = type->tp_bases;
1228 n = PyTuple_GET_SIZE(bases);
1230 to_merge = PyList_New(n+1);
1231 if (to_merge == NULL)
1232 return NULL;
1234 for (i = 0; i < n; i++) {
1235 PyObject *base = PyTuple_GET_ITEM(bases, i);
1236 PyObject *parentMRO;
1237 if (PyType_Check(base))
1238 parentMRO = PySequence_List(
1239 ((PyTypeObject*)base)->tp_mro);
1240 else
1241 parentMRO = classic_mro(base);
1242 if (parentMRO == NULL) {
1243 Py_DECREF(to_merge);
1244 return NULL;
1247 PyList_SET_ITEM(to_merge, i, parentMRO);
1250 bases_aslist = PySequence_List(bases);
1251 if (bases_aslist == NULL) {
1252 Py_DECREF(to_merge);
1253 return NULL;
1255 /* This is just a basic sanity check. */
1256 if (check_duplicates(bases_aslist) < 0) {
1257 Py_DECREF(to_merge);
1258 Py_DECREF(bases_aslist);
1259 return NULL;
1261 PyList_SET_ITEM(to_merge, n, bases_aslist);
1263 result = Py_BuildValue("[O]", (PyObject *)type);
1264 if (result == NULL) {
1265 Py_DECREF(to_merge);
1266 return NULL;
1269 ok = pmerge(result, to_merge);
1270 Py_DECREF(to_merge);
1271 if (ok < 0) {
1272 Py_DECREF(result);
1273 return NULL;
1276 return result;
1279 static PyObject *
1280 mro_external(PyObject *self)
1282 PyTypeObject *type = (PyTypeObject *)self;
1284 return mro_implementation(type);
1287 static int
1288 mro_internal(PyTypeObject *type)
1290 PyObject *mro, *result, *tuple;
1292 if (type->ob_type == &PyType_Type) {
1293 result = mro_implementation(type);
1295 else {
1296 static PyObject *mro_str;
1297 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1298 if (mro == NULL)
1299 return -1;
1300 result = PyObject_CallObject(mro, NULL);
1301 Py_DECREF(mro);
1303 if (result == NULL)
1304 return -1;
1305 tuple = PySequence_Tuple(result);
1306 Py_DECREF(result);
1307 type->tp_mro = tuple;
1308 return 0;
1312 /* Calculate the best base amongst multiple base classes.
1313 This is the first one that's on the path to the "solid base". */
1315 static PyTypeObject *
1316 best_base(PyObject *bases)
1318 int i, n;
1319 PyTypeObject *base, *winner, *candidate, *base_i;
1320 PyObject *base_proto;
1322 assert(PyTuple_Check(bases));
1323 n = PyTuple_GET_SIZE(bases);
1324 assert(n > 0);
1325 base = NULL;
1326 winner = NULL;
1327 for (i = 0; i < n; i++) {
1328 base_proto = PyTuple_GET_ITEM(bases, i);
1329 if (PyClass_Check(base_proto))
1330 continue;
1331 if (!PyType_Check(base_proto)) {
1332 PyErr_SetString(
1333 PyExc_TypeError,
1334 "bases must be types");
1335 return NULL;
1337 base_i = (PyTypeObject *)base_proto;
1338 if (base_i->tp_dict == NULL) {
1339 if (PyType_Ready(base_i) < 0)
1340 return NULL;
1342 candidate = solid_base(base_i);
1343 if (winner == NULL) {
1344 winner = candidate;
1345 base = base_i;
1347 else if (PyType_IsSubtype(winner, candidate))
1349 else if (PyType_IsSubtype(candidate, winner)) {
1350 winner = candidate;
1351 base = base_i;
1353 else {
1354 PyErr_SetString(
1355 PyExc_TypeError,
1356 "multiple bases have "
1357 "instance lay-out conflict");
1358 return NULL;
1361 if (base == NULL)
1362 PyErr_SetString(PyExc_TypeError,
1363 "a new-style class can't have only classic bases");
1364 return base;
1367 static int
1368 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1370 size_t t_size = type->tp_basicsize;
1371 size_t b_size = base->tp_basicsize;
1373 assert(t_size >= b_size); /* Else type smaller than base! */
1374 if (type->tp_itemsize || base->tp_itemsize) {
1375 /* If itemsize is involved, stricter rules */
1376 return t_size != b_size ||
1377 type->tp_itemsize != base->tp_itemsize;
1379 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1380 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1381 t_size -= sizeof(PyObject *);
1382 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1383 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1384 t_size -= sizeof(PyObject *);
1386 return t_size != b_size;
1389 static PyTypeObject *
1390 solid_base(PyTypeObject *type)
1392 PyTypeObject *base;
1394 if (type->tp_base)
1395 base = solid_base(type->tp_base);
1396 else
1397 base = &PyBaseObject_Type;
1398 if (extra_ivars(type, base))
1399 return type;
1400 else
1401 return base;
1404 static void object_dealloc(PyObject *);
1405 static int object_init(PyObject *, PyObject *, PyObject *);
1406 static int update_slot(PyTypeObject *, PyObject *);
1407 static void fixup_slot_dispatchers(PyTypeObject *);
1409 static PyObject *
1410 subtype_dict(PyObject *obj, void *context)
1412 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1413 PyObject *dict;
1415 if (dictptr == NULL) {
1416 PyErr_SetString(PyExc_AttributeError,
1417 "This object has no __dict__");
1418 return NULL;
1420 dict = *dictptr;
1421 if (dict == NULL)
1422 *dictptr = dict = PyDict_New();
1423 Py_XINCREF(dict);
1424 return dict;
1427 static int
1428 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1430 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1431 PyObject *dict;
1433 if (dictptr == NULL) {
1434 PyErr_SetString(PyExc_AttributeError,
1435 "This object has no __dict__");
1436 return -1;
1438 if (value != NULL && !PyDict_Check(value)) {
1439 PyErr_SetString(PyExc_TypeError,
1440 "__dict__ must be set to a dictionary");
1441 return -1;
1443 dict = *dictptr;
1444 Py_XINCREF(value);
1445 *dictptr = value;
1446 Py_XDECREF(dict);
1447 return 0;
1450 static PyObject *
1451 subtype_getweakref(PyObject *obj, void *context)
1453 PyObject **weaklistptr;
1454 PyObject *result;
1456 if (obj->ob_type->tp_weaklistoffset == 0) {
1457 PyErr_SetString(PyExc_AttributeError,
1458 "This object has no __weaklist__");
1459 return NULL;
1461 assert(obj->ob_type->tp_weaklistoffset > 0);
1462 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1463 (size_t)(obj->ob_type->tp_basicsize));
1464 weaklistptr = (PyObject **)
1465 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1466 if (*weaklistptr == NULL)
1467 result = Py_None;
1468 else
1469 result = *weaklistptr;
1470 Py_INCREF(result);
1471 return result;
1474 /* Three variants on the subtype_getsets list. */
1476 static PyGetSetDef subtype_getsets_full[] = {
1477 {"__dict__", subtype_dict, subtype_setdict,
1478 PyDoc_STR("dictionary for instance variables (if defined)")},
1479 {"__weakref__", subtype_getweakref, NULL,
1480 PyDoc_STR("list of weak references to the object (if defined)")},
1484 static PyGetSetDef subtype_getsets_dict_only[] = {
1485 {"__dict__", subtype_dict, subtype_setdict,
1486 PyDoc_STR("dictionary for instance variables (if defined)")},
1490 static PyGetSetDef subtype_getsets_weakref_only[] = {
1491 {"__weakref__", subtype_getweakref, NULL,
1492 PyDoc_STR("list of weak references to the object (if defined)")},
1496 static int
1497 valid_identifier(PyObject *s)
1499 unsigned char *p;
1500 int i, n;
1502 if (!PyString_Check(s)) {
1503 PyErr_SetString(PyExc_TypeError,
1504 "__slots__ must be strings");
1505 return 0;
1507 p = (unsigned char *) PyString_AS_STRING(s);
1508 n = PyString_GET_SIZE(s);
1509 /* We must reject an empty name. As a hack, we bump the
1510 length to 1 so that the loop will balk on the trailing \0. */
1511 if (n == 0)
1512 n = 1;
1513 for (i = 0; i < n; i++, p++) {
1514 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1515 PyErr_SetString(PyExc_TypeError,
1516 "__slots__ must be identifiers");
1517 return 0;
1520 return 1;
1523 #ifdef Py_USING_UNICODE
1524 /* Replace Unicode objects in slots. */
1526 static PyObject *
1527 _unicode_to_string(PyObject *slots, int nslots)
1529 PyObject *tmp = slots;
1530 PyObject *o, *o1;
1531 int i;
1532 intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1533 for (i = 0; i < nslots; i++) {
1534 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1535 if (tmp == slots) {
1536 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1537 if (tmp == NULL)
1538 return NULL;
1540 o1 = _PyUnicode_AsDefaultEncodedString
1541 (o, NULL);
1542 if (o1 == NULL) {
1543 Py_DECREF(tmp);
1544 return 0;
1546 Py_INCREF(o1);
1547 Py_DECREF(o);
1548 PyTuple_SET_ITEM(tmp, i, o1);
1551 return tmp;
1553 #endif
1555 static PyObject *
1556 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1558 PyObject *name, *bases, *dict;
1559 static char *kwlist[] = {"name", "bases", "dict", 0};
1560 PyObject *slots, *tmp, *newslots;
1561 PyTypeObject *type, *base, *tmptype, *winner;
1562 PyHeapTypeObject *et;
1563 PyMemberDef *mp;
1564 int i, nbases, nslots, slotoffset, add_dict, add_weak;
1565 int j, may_add_dict, may_add_weak;
1567 assert(args != NULL && PyTuple_Check(args));
1568 assert(kwds == NULL || PyDict_Check(kwds));
1570 /* Special case: type(x) should return x->ob_type */
1572 const int nargs = PyTuple_GET_SIZE(args);
1573 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1575 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1576 PyObject *x = PyTuple_GET_ITEM(args, 0);
1577 Py_INCREF(x->ob_type);
1578 return (PyObject *) x->ob_type;
1581 /* SF bug 475327 -- if that didn't trigger, we need 3
1582 arguments. but PyArg_ParseTupleAndKeywords below may give
1583 a msg saying type() needs exactly 3. */
1584 if (nargs + nkwds != 3) {
1585 PyErr_SetString(PyExc_TypeError,
1586 "type() takes 1 or 3 arguments");
1587 return NULL;
1591 /* Check arguments: (name, bases, dict) */
1592 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1593 &name,
1594 &PyTuple_Type, &bases,
1595 &PyDict_Type, &dict))
1596 return NULL;
1598 /* Determine the proper metatype to deal with this,
1599 and check for metatype conflicts while we're at it.
1600 Note that if some other metatype wins to contract,
1601 it's possible that its instances are not types. */
1602 nbases = PyTuple_GET_SIZE(bases);
1603 winner = metatype;
1604 for (i = 0; i < nbases; i++) {
1605 tmp = PyTuple_GET_ITEM(bases, i);
1606 tmptype = tmp->ob_type;
1607 if (tmptype == &PyClass_Type)
1608 continue; /* Special case classic classes */
1609 if (PyType_IsSubtype(winner, tmptype))
1610 continue;
1611 if (PyType_IsSubtype(tmptype, winner)) {
1612 winner = tmptype;
1613 continue;
1615 PyErr_SetString(PyExc_TypeError,
1616 "metaclass conflict: "
1617 "the metaclass of a derived class "
1618 "must be a (non-strict) subclass "
1619 "of the metaclasses of all its bases");
1620 return NULL;
1622 if (winner != metatype) {
1623 if (winner->tp_new != type_new) /* Pass it to the winner */
1624 return winner->tp_new(winner, args, kwds);
1625 metatype = winner;
1628 /* Adjust for empty tuple bases */
1629 if (nbases == 0) {
1630 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1631 if (bases == NULL)
1632 return NULL;
1633 nbases = 1;
1635 else
1636 Py_INCREF(bases);
1638 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1640 /* Calculate best base, and check that all bases are type objects */
1641 base = best_base(bases);
1642 if (base == NULL) {
1643 Py_DECREF(bases);
1644 return NULL;
1646 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1647 PyErr_Format(PyExc_TypeError,
1648 "type '%.100s' is not an acceptable base type",
1649 base->tp_name);
1650 Py_DECREF(bases);
1651 return NULL;
1654 /* Check for a __slots__ sequence variable in dict, and count it */
1655 slots = PyDict_GetItemString(dict, "__slots__");
1656 nslots = 0;
1657 add_dict = 0;
1658 add_weak = 0;
1659 may_add_dict = base->tp_dictoffset == 0;
1660 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1661 if (slots == NULL) {
1662 if (may_add_dict) {
1663 add_dict++;
1665 if (may_add_weak) {
1666 add_weak++;
1669 else {
1670 /* Have slots */
1672 /* Make it into a tuple */
1673 if (PyString_Check(slots))
1674 slots = PyTuple_Pack(1, slots);
1675 else
1676 slots = PySequence_Tuple(slots);
1677 if (slots == NULL) {
1678 Py_DECREF(bases);
1679 return NULL;
1681 assert(PyTuple_Check(slots));
1683 /* Are slots allowed? */
1684 nslots = PyTuple_GET_SIZE(slots);
1685 if (nslots > 0 && base->tp_itemsize != 0) {
1686 PyErr_Format(PyExc_TypeError,
1687 "nonempty __slots__ "
1688 "not supported for subtype of '%s'",
1689 base->tp_name);
1690 bad_slots:
1691 Py_DECREF(bases);
1692 Py_DECREF(slots);
1693 return NULL;
1696 #ifdef Py_USING_UNICODE
1697 tmp = _unicode_to_string(slots, nslots);
1698 if (tmp != slots) {
1699 Py_DECREF(slots);
1700 slots = tmp;
1702 if (!tmp)
1703 return NULL;
1704 #endif
1705 /* Check for valid slot names and two special cases */
1706 for (i = 0; i < nslots; i++) {
1707 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1708 char *s;
1709 if (!valid_identifier(tmp))
1710 goto bad_slots;
1711 assert(PyString_Check(tmp));
1712 s = PyString_AS_STRING(tmp);
1713 if (strcmp(s, "__dict__") == 0) {
1714 if (!may_add_dict || add_dict) {
1715 PyErr_SetString(PyExc_TypeError,
1716 "__dict__ slot disallowed: "
1717 "we already got one");
1718 goto bad_slots;
1720 add_dict++;
1722 if (strcmp(s, "__weakref__") == 0) {
1723 if (!may_add_weak || add_weak) {
1724 PyErr_SetString(PyExc_TypeError,
1725 "__weakref__ slot disallowed: "
1726 "either we already got one, "
1727 "or __itemsize__ != 0");
1728 goto bad_slots;
1730 add_weak++;
1734 /* Copy slots into yet another tuple, demangling names */
1735 newslots = PyTuple_New(nslots - add_dict - add_weak);
1736 if (newslots == NULL)
1737 goto bad_slots;
1738 for (i = j = 0; i < nslots; i++) {
1739 char *s;
1740 char buffer[256];
1741 tmp = PyTuple_GET_ITEM(slots, i);
1742 s = PyString_AS_STRING(tmp);
1743 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1744 (add_weak && strcmp(s, "__weakref__") == 0))
1745 continue;
1746 if (_Py_Mangle(PyString_AS_STRING(name),
1747 PyString_AS_STRING(tmp),
1748 buffer, sizeof(buffer)))
1750 tmp = PyString_FromString(buffer);
1751 } else {
1752 Py_INCREF(tmp);
1754 PyTuple_SET_ITEM(newslots, j, tmp);
1755 j++;
1757 assert(j == nslots - add_dict - add_weak);
1758 nslots = j;
1759 Py_DECREF(slots);
1760 slots = newslots;
1762 /* Secondary bases may provide weakrefs or dict */
1763 if (nbases > 1 &&
1764 ((may_add_dict && !add_dict) ||
1765 (may_add_weak && !add_weak))) {
1766 for (i = 0; i < nbases; i++) {
1767 tmp = PyTuple_GET_ITEM(bases, i);
1768 if (tmp == (PyObject *)base)
1769 continue; /* Skip primary base */
1770 if (PyClass_Check(tmp)) {
1771 /* Classic base class provides both */
1772 if (may_add_dict && !add_dict)
1773 add_dict++;
1774 if (may_add_weak && !add_weak)
1775 add_weak++;
1776 break;
1778 assert(PyType_Check(tmp));
1779 tmptype = (PyTypeObject *)tmp;
1780 if (may_add_dict && !add_dict &&
1781 tmptype->tp_dictoffset != 0)
1782 add_dict++;
1783 if (may_add_weak && !add_weak &&
1784 tmptype->tp_weaklistoffset != 0)
1785 add_weak++;
1786 if (may_add_dict && !add_dict)
1787 continue;
1788 if (may_add_weak && !add_weak)
1789 continue;
1790 /* Nothing more to check */
1791 break;
1796 /* XXX From here until type is safely allocated,
1797 "return NULL" may leak slots! */
1799 /* Allocate the type object */
1800 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1801 if (type == NULL) {
1802 Py_XDECREF(slots);
1803 Py_DECREF(bases);
1804 return NULL;
1807 /* Keep name and slots alive in the extended type object */
1808 et = (PyHeapTypeObject *)type;
1809 Py_INCREF(name);
1810 et->name = name;
1811 et->slots = slots;
1813 /* Initialize tp_flags */
1814 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1815 Py_TPFLAGS_BASETYPE;
1816 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1817 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1819 /* It's a new-style number unless it specifically inherits any
1820 old-style numeric behavior */
1821 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1822 (base->tp_as_number == NULL))
1823 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1825 /* Initialize essential fields */
1826 type->tp_as_number = &et->as_number;
1827 type->tp_as_sequence = &et->as_sequence;
1828 type->tp_as_mapping = &et->as_mapping;
1829 type->tp_as_buffer = &et->as_buffer;
1830 type->tp_name = PyString_AS_STRING(name);
1832 /* Set tp_base and tp_bases */
1833 type->tp_bases = bases;
1834 Py_INCREF(base);
1835 type->tp_base = base;
1837 /* Initialize tp_dict from passed-in dict */
1838 type->tp_dict = dict = PyDict_Copy(dict);
1839 if (dict == NULL) {
1840 Py_DECREF(type);
1841 return NULL;
1844 /* Set __module__ in the dict */
1845 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1846 tmp = PyEval_GetGlobals();
1847 if (tmp != NULL) {
1848 tmp = PyDict_GetItemString(tmp, "__name__");
1849 if (tmp != NULL) {
1850 if (PyDict_SetItemString(dict, "__module__",
1851 tmp) < 0)
1852 return NULL;
1857 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1858 and is a string. The __doc__ accessor will first look for tp_doc;
1859 if that fails, it will still look into __dict__.
1862 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1863 if (doc != NULL && PyString_Check(doc)) {
1864 const size_t n = (size_t)PyString_GET_SIZE(doc);
1865 type->tp_doc = (char *)PyObject_MALLOC(n+1);
1866 if (type->tp_doc == NULL) {
1867 Py_DECREF(type);
1868 return NULL;
1870 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1874 /* Special-case __new__: if it's a plain function,
1875 make it a static function */
1876 tmp = PyDict_GetItemString(dict, "__new__");
1877 if (tmp != NULL && PyFunction_Check(tmp)) {
1878 tmp = PyStaticMethod_New(tmp);
1879 if (tmp == NULL) {
1880 Py_DECREF(type);
1881 return NULL;
1883 PyDict_SetItemString(dict, "__new__", tmp);
1884 Py_DECREF(tmp);
1887 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1888 mp = PyHeapType_GET_MEMBERS(et);
1889 slotoffset = base->tp_basicsize;
1890 if (slots != NULL) {
1891 for (i = 0; i < nslots; i++, mp++) {
1892 mp->name = PyString_AS_STRING(
1893 PyTuple_GET_ITEM(slots, i));
1894 mp->type = T_OBJECT_EX;
1895 mp->offset = slotoffset;
1896 if (base->tp_weaklistoffset == 0 &&
1897 strcmp(mp->name, "__weakref__") == 0) {
1898 add_weak++;
1899 mp->type = T_OBJECT;
1900 mp->flags = READONLY;
1901 type->tp_weaklistoffset = slotoffset;
1903 slotoffset += sizeof(PyObject *);
1906 if (add_dict) {
1907 if (base->tp_itemsize)
1908 type->tp_dictoffset = -(long)sizeof(PyObject *);
1909 else
1910 type->tp_dictoffset = slotoffset;
1911 slotoffset += sizeof(PyObject *);
1913 if (add_weak) {
1914 assert(!base->tp_itemsize);
1915 type->tp_weaklistoffset = slotoffset;
1916 slotoffset += sizeof(PyObject *);
1918 type->tp_basicsize = slotoffset;
1919 type->tp_itemsize = base->tp_itemsize;
1920 type->tp_members = PyHeapType_GET_MEMBERS(et);
1922 if (type->tp_weaklistoffset && type->tp_dictoffset)
1923 type->tp_getset = subtype_getsets_full;
1924 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1925 type->tp_getset = subtype_getsets_weakref_only;
1926 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1927 type->tp_getset = subtype_getsets_dict_only;
1928 else
1929 type->tp_getset = NULL;
1931 /* Special case some slots */
1932 if (type->tp_dictoffset != 0 || nslots > 0) {
1933 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1934 type->tp_getattro = PyObject_GenericGetAttr;
1935 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1936 type->tp_setattro = PyObject_GenericSetAttr;
1938 type->tp_dealloc = subtype_dealloc;
1940 /* Enable GC unless there are really no instance variables possible */
1941 if (!(type->tp_basicsize == sizeof(PyObject) &&
1942 type->tp_itemsize == 0))
1943 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1945 /* Always override allocation strategy to use regular heap */
1946 type->tp_alloc = PyType_GenericAlloc;
1947 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1948 type->tp_free = PyObject_GC_Del;
1949 type->tp_traverse = subtype_traverse;
1950 type->tp_clear = subtype_clear;
1952 else
1953 type->tp_free = PyObject_Del;
1955 /* Initialize the rest */
1956 if (PyType_Ready(type) < 0) {
1957 Py_DECREF(type);
1958 return NULL;
1961 /* Put the proper slots in place */
1962 fixup_slot_dispatchers(type);
1964 return (PyObject *)type;
1967 /* Internal API to look for a name through the MRO.
1968 This returns a borrowed reference, and doesn't set an exception! */
1969 PyObject *
1970 _PyType_Lookup(PyTypeObject *type, PyObject *name)
1972 int i, n;
1973 PyObject *mro, *res, *base, *dict;
1975 /* Look in tp_dict of types in MRO */
1976 mro = type->tp_mro;
1978 /* If mro is NULL, the type is either not yet initialized
1979 by PyType_Ready(), or already cleared by type_clear().
1980 Either way the safest thing to do is to return NULL. */
1981 if (mro == NULL)
1982 return NULL;
1984 assert(PyTuple_Check(mro));
1985 n = PyTuple_GET_SIZE(mro);
1986 for (i = 0; i < n; i++) {
1987 base = PyTuple_GET_ITEM(mro, i);
1988 if (PyClass_Check(base))
1989 dict = ((PyClassObject *)base)->cl_dict;
1990 else {
1991 assert(PyType_Check(base));
1992 dict = ((PyTypeObject *)base)->tp_dict;
1994 assert(dict && PyDict_Check(dict));
1995 res = PyDict_GetItem(dict, name);
1996 if (res != NULL)
1997 return res;
1999 return NULL;
2002 /* This is similar to PyObject_GenericGetAttr(),
2003 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2004 static PyObject *
2005 type_getattro(PyTypeObject *type, PyObject *name)
2007 PyTypeObject *metatype = type->ob_type;
2008 PyObject *meta_attribute, *attribute;
2009 descrgetfunc meta_get;
2011 /* Initialize this type (we'll assume the metatype is initialized) */
2012 if (type->tp_dict == NULL) {
2013 if (PyType_Ready(type) < 0)
2014 return NULL;
2017 /* No readable descriptor found yet */
2018 meta_get = NULL;
2020 /* Look for the attribute in the metatype */
2021 meta_attribute = _PyType_Lookup(metatype, name);
2023 if (meta_attribute != NULL) {
2024 meta_get = meta_attribute->ob_type->tp_descr_get;
2026 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2027 /* Data descriptors implement tp_descr_set to intercept
2028 * writes. Assume the attribute is not overridden in
2029 * type's tp_dict (and bases): call the descriptor now.
2031 return meta_get(meta_attribute, (PyObject *)type,
2032 (PyObject *)metatype);
2034 Py_INCREF(meta_attribute);
2037 /* No data descriptor found on metatype. Look in tp_dict of this
2038 * type and its bases */
2039 attribute = _PyType_Lookup(type, name);
2040 if (attribute != NULL) {
2041 /* Implement descriptor functionality, if any */
2042 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2044 Py_XDECREF(meta_attribute);
2046 if (local_get != NULL) {
2047 /* NULL 2nd argument indicates the descriptor was
2048 * found on the target object itself (or a base) */
2049 return local_get(attribute, (PyObject *)NULL,
2050 (PyObject *)type);
2053 Py_INCREF(attribute);
2054 return attribute;
2057 /* No attribute found in local __dict__ (or bases): use the
2058 * descriptor from the metatype, if any */
2059 if (meta_get != NULL) {
2060 PyObject *res;
2061 res = meta_get(meta_attribute, (PyObject *)type,
2062 (PyObject *)metatype);
2063 Py_DECREF(meta_attribute);
2064 return res;
2067 /* If an ordinary attribute was found on the metatype, return it now */
2068 if (meta_attribute != NULL) {
2069 return meta_attribute;
2072 /* Give up */
2073 PyErr_Format(PyExc_AttributeError,
2074 "type object '%.50s' has no attribute '%.400s'",
2075 type->tp_name, PyString_AS_STRING(name));
2076 return NULL;
2079 static int
2080 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2082 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2083 PyErr_Format(
2084 PyExc_TypeError,
2085 "can't set attributes of built-in/extension type '%s'",
2086 type->tp_name);
2087 return -1;
2089 /* XXX Example of how I expect this to be used...
2090 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2091 return -1;
2093 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2094 return -1;
2095 return update_slot(type, name);
2098 static void
2099 type_dealloc(PyTypeObject *type)
2101 PyHeapTypeObject *et;
2103 /* Assert this is a heap-allocated type object */
2104 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2105 _PyObject_GC_UNTRACK(type);
2106 PyObject_ClearWeakRefs((PyObject *)type);
2107 et = (PyHeapTypeObject *)type;
2108 Py_XDECREF(type->tp_base);
2109 Py_XDECREF(type->tp_dict);
2110 Py_XDECREF(type->tp_bases);
2111 Py_XDECREF(type->tp_mro);
2112 Py_XDECREF(type->tp_cache);
2113 Py_XDECREF(type->tp_subclasses);
2114 PyObject_Free(type->tp_doc);
2115 Py_XDECREF(et->name);
2116 Py_XDECREF(et->slots);
2117 type->ob_type->tp_free((PyObject *)type);
2120 static PyObject *
2121 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2123 PyObject *list, *raw, *ref;
2124 int i, n;
2126 list = PyList_New(0);
2127 if (list == NULL)
2128 return NULL;
2129 raw = type->tp_subclasses;
2130 if (raw == NULL)
2131 return list;
2132 assert(PyList_Check(raw));
2133 n = PyList_GET_SIZE(raw);
2134 for (i = 0; i < n; i++) {
2135 ref = PyList_GET_ITEM(raw, i);
2136 assert(PyWeakref_CheckRef(ref));
2137 ref = PyWeakref_GET_OBJECT(ref);
2138 if (ref != Py_None) {
2139 if (PyList_Append(list, ref) < 0) {
2140 Py_DECREF(list);
2141 return NULL;
2145 return list;
2148 static PyMethodDef type_methods[] = {
2149 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2150 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2151 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2152 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2156 PyDoc_STRVAR(type_doc,
2157 "type(object) -> the object's type\n"
2158 "type(name, bases, dict) -> a new type");
2160 static int
2161 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2163 int err;
2165 /* Because of type_is_gc(), the collector only calls this
2166 for heaptypes. */
2167 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2169 #define VISIT(SLOT) \
2170 if (SLOT) { \
2171 err = visit((PyObject *)(SLOT), arg); \
2172 if (err) \
2173 return err; \
2176 VISIT(type->tp_dict);
2177 VISIT(type->tp_cache);
2178 VISIT(type->tp_mro);
2179 VISIT(type->tp_bases);
2180 VISIT(type->tp_base);
2182 /* There's no need to visit type->tp_subclasses or
2183 ((PyHeapTypeObject *)type)->slots, because they can't be involved
2184 in cycles; tp_subclasses is a list of weak references,
2185 and slots is a tuple of strings. */
2187 #undef VISIT
2189 return 0;
2192 static int
2193 type_clear(PyTypeObject *type)
2195 PyObject *tmp;
2197 /* Because of type_is_gc(), the collector only calls this
2198 for heaptypes. */
2199 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2201 #define CLEAR(SLOT) \
2202 if (SLOT) { \
2203 tmp = (PyObject *)(SLOT); \
2204 SLOT = NULL; \
2205 Py_DECREF(tmp); \
2208 /* The only field we need to clear is tp_mro, which is part of a
2209 hard cycle (its first element is the class itself) that won't
2210 be broken otherwise (it's a tuple and tuples don't have a
2211 tp_clear handler). None of the other fields need to be
2212 cleared, and here's why:
2214 tp_dict:
2215 It is a dict, so the collector will call its tp_clear.
2217 tp_cache:
2218 Not used; if it were, it would be a dict.
2220 tp_bases, tp_base:
2221 If these are involved in a cycle, there must be at least
2222 one other, mutable object in the cycle, e.g. a base
2223 class's dict; the cycle will be broken that way.
2225 tp_subclasses:
2226 A list of weak references can't be part of a cycle; and
2227 lists have their own tp_clear.
2229 slots (in PyHeapTypeObject):
2230 A tuple of strings can't be part of a cycle.
2233 CLEAR(type->tp_mro);
2235 #undef CLEAR
2237 return 0;
2240 static int
2241 type_is_gc(PyTypeObject *type)
2243 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2246 PyTypeObject PyType_Type = {
2247 PyObject_HEAD_INIT(&PyType_Type)
2248 0, /* ob_size */
2249 "type", /* tp_name */
2250 sizeof(PyHeapTypeObject), /* tp_basicsize */
2251 sizeof(PyMemberDef), /* tp_itemsize */
2252 (destructor)type_dealloc, /* tp_dealloc */
2253 0, /* tp_print */
2254 0, /* tp_getattr */
2255 0, /* tp_setattr */
2256 type_compare, /* tp_compare */
2257 (reprfunc)type_repr, /* tp_repr */
2258 0, /* tp_as_number */
2259 0, /* tp_as_sequence */
2260 0, /* tp_as_mapping */
2261 (hashfunc)_Py_HashPointer, /* tp_hash */
2262 (ternaryfunc)type_call, /* tp_call */
2263 0, /* tp_str */
2264 (getattrofunc)type_getattro, /* tp_getattro */
2265 (setattrofunc)type_setattro, /* tp_setattro */
2266 0, /* tp_as_buffer */
2267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2268 Py_TPFLAGS_BASETYPE, /* tp_flags */
2269 type_doc, /* tp_doc */
2270 (traverseproc)type_traverse, /* tp_traverse */
2271 (inquiry)type_clear, /* tp_clear */
2272 0, /* tp_richcompare */
2273 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2274 0, /* tp_iter */
2275 0, /* tp_iternext */
2276 type_methods, /* tp_methods */
2277 type_members, /* tp_members */
2278 type_getsets, /* tp_getset */
2279 0, /* tp_base */
2280 0, /* tp_dict */
2281 0, /* tp_descr_get */
2282 0, /* tp_descr_set */
2283 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2284 0, /* tp_init */
2285 0, /* tp_alloc */
2286 type_new, /* tp_new */
2287 PyObject_GC_Del, /* tp_free */
2288 (inquiry)type_is_gc, /* tp_is_gc */
2292 /* The base type of all types (eventually)... except itself. */
2294 static int
2295 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2297 return 0;
2300 /* If we don't have a tp_new for a new-style class, new will use this one.
2301 Therefore this should take no arguments/keywords. However, this new may
2302 also be inherited by objects that define a tp_init but no tp_new. These
2303 objects WILL pass argumets to tp_new, because it gets the same args as
2304 tp_init. So only allow arguments if we aren't using the default init, in
2305 which case we expect init to handle argument parsing. */
2306 static PyObject *
2307 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2309 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2310 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2311 PyErr_SetString(PyExc_TypeError,
2312 "default __new__ takes no parameters");
2313 return NULL;
2315 return type->tp_alloc(type, 0);
2318 static void
2319 object_dealloc(PyObject *self)
2321 self->ob_type->tp_free(self);
2324 static PyObject *
2325 object_repr(PyObject *self)
2327 PyTypeObject *type;
2328 PyObject *mod, *name, *rtn;
2330 type = self->ob_type;
2331 mod = type_module(type, NULL);
2332 if (mod == NULL)
2333 PyErr_Clear();
2334 else if (!PyString_Check(mod)) {
2335 Py_DECREF(mod);
2336 mod = NULL;
2338 name = type_name(type, NULL);
2339 if (name == NULL)
2340 return NULL;
2341 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2342 rtn = PyString_FromFormat("<%s.%s object at %p>",
2343 PyString_AS_STRING(mod),
2344 PyString_AS_STRING(name),
2345 self);
2346 else
2347 rtn = PyString_FromFormat("<%s object at %p>",
2348 type->tp_name, self);
2349 Py_XDECREF(mod);
2350 Py_DECREF(name);
2351 return rtn;
2354 static PyObject *
2355 object_str(PyObject *self)
2357 unaryfunc f;
2359 f = self->ob_type->tp_repr;
2360 if (f == NULL)
2361 f = object_repr;
2362 return f(self);
2365 static long
2366 object_hash(PyObject *self)
2368 return _Py_HashPointer(self);
2371 static PyObject *
2372 object_get_class(PyObject *self, void *closure)
2374 Py_INCREF(self->ob_type);
2375 return (PyObject *)(self->ob_type);
2378 static int
2379 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2381 return a == b ||
2382 (a != NULL &&
2383 b != NULL &&
2384 a->tp_basicsize == b->tp_basicsize &&
2385 a->tp_itemsize == b->tp_itemsize &&
2386 a->tp_dictoffset == b->tp_dictoffset &&
2387 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2388 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2389 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2392 static int
2393 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2395 PyTypeObject *base = a->tp_base;
2396 int size;
2398 if (base != b->tp_base)
2399 return 0;
2400 if (equiv_structs(a, base) && equiv_structs(b, base))
2401 return 1;
2402 size = base->tp_basicsize;
2403 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2404 size += sizeof(PyObject *);
2405 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2406 size += sizeof(PyObject *);
2407 return size == a->tp_basicsize && size == b->tp_basicsize;
2410 static int
2411 compatible_for_assignment(PyTypeObject* old, PyTypeObject* new, char* attr)
2413 PyTypeObject *newbase, *oldbase;
2415 if (new->tp_dealloc != old->tp_dealloc ||
2416 new->tp_free != old->tp_free)
2418 PyErr_Format(PyExc_TypeError,
2419 "%s assignment: "
2420 "'%s' deallocator differs from '%s'",
2421 attr,
2422 new->tp_name,
2423 old->tp_name);
2424 return 0;
2426 newbase = new;
2427 oldbase = old;
2428 while (equiv_structs(newbase, newbase->tp_base))
2429 newbase = newbase->tp_base;
2430 while (equiv_structs(oldbase, oldbase->tp_base))
2431 oldbase = oldbase->tp_base;
2432 if (newbase != oldbase &&
2433 (newbase->tp_base != oldbase->tp_base ||
2434 !same_slots_added(newbase, oldbase))) {
2435 PyErr_Format(PyExc_TypeError,
2436 "%s assignment: "
2437 "'%s' object layout differs from '%s'",
2438 attr,
2439 new->tp_name,
2440 old->tp_name);
2441 return 0;
2444 return 1;
2447 static int
2448 object_set_class(PyObject *self, PyObject *value, void *closure)
2450 PyTypeObject *old = self->ob_type;
2451 PyTypeObject *new;
2453 if (value == NULL) {
2454 PyErr_SetString(PyExc_TypeError,
2455 "can't delete __class__ attribute");
2456 return -1;
2458 if (!PyType_Check(value)) {
2459 PyErr_Format(PyExc_TypeError,
2460 "__class__ must be set to new-style class, not '%s' object",
2461 value->ob_type->tp_name);
2462 return -1;
2464 new = (PyTypeObject *)value;
2465 if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2466 !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
2468 PyErr_Format(PyExc_TypeError,
2469 "__class__ assignment: only for heap types");
2470 return -1;
2472 if (compatible_for_assignment(new, old, "__class__")) {
2473 Py_INCREF(new);
2474 self->ob_type = new;
2475 Py_DECREF(old);
2476 return 0;
2478 else {
2479 return -1;
2483 static PyGetSetDef object_getsets[] = {
2484 {"__class__", object_get_class, object_set_class,
2485 PyDoc_STR("the object's class")},
2490 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2491 We fall back to helpers in copy_reg for:
2492 - pickle protocols < 2
2493 - calculating the list of slot names (done only once per class)
2494 - the __newobj__ function (which is used as a token but never called)
2497 static PyObject *
2498 import_copy_reg(void)
2500 static PyObject *copy_reg_str;
2502 if (!copy_reg_str) {
2503 copy_reg_str = PyString_InternFromString("copy_reg");
2504 if (copy_reg_str == NULL)
2505 return NULL;
2508 return PyImport_Import(copy_reg_str);
2511 static PyObject *
2512 slotnames(PyObject *cls)
2514 PyObject *clsdict;
2515 PyObject *copy_reg;
2516 PyObject *slotnames;
2518 if (!PyType_Check(cls)) {
2519 Py_INCREF(Py_None);
2520 return Py_None;
2523 clsdict = ((PyTypeObject *)cls)->tp_dict;
2524 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2525 if (slotnames != NULL) {
2526 Py_INCREF(slotnames);
2527 return slotnames;
2530 copy_reg = import_copy_reg();
2531 if (copy_reg == NULL)
2532 return NULL;
2534 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2535 Py_DECREF(copy_reg);
2536 if (slotnames != NULL &&
2537 slotnames != Py_None &&
2538 !PyList_Check(slotnames))
2540 PyErr_SetString(PyExc_TypeError,
2541 "copy_reg._slotnames didn't return a list or None");
2542 Py_DECREF(slotnames);
2543 slotnames = NULL;
2546 return slotnames;
2549 static PyObject *
2550 reduce_2(PyObject *obj)
2552 PyObject *cls, *getnewargs;
2553 PyObject *args = NULL, *args2 = NULL;
2554 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2555 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2556 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2557 int i, n;
2559 cls = PyObject_GetAttrString(obj, "__class__");
2560 if (cls == NULL)
2561 return NULL;
2563 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2564 if (getnewargs != NULL) {
2565 args = PyObject_CallObject(getnewargs, NULL);
2566 Py_DECREF(getnewargs);
2567 if (args != NULL && !PyTuple_Check(args)) {
2568 PyErr_SetString(PyExc_TypeError,
2569 "__getnewargs__ should return a tuple");
2570 goto end;
2573 else {
2574 PyErr_Clear();
2575 args = PyTuple_New(0);
2577 if (args == NULL)
2578 goto end;
2580 getstate = PyObject_GetAttrString(obj, "__getstate__");
2581 if (getstate != NULL) {
2582 state = PyObject_CallObject(getstate, NULL);
2583 Py_DECREF(getstate);
2584 if (state == NULL)
2585 goto end;
2587 else {
2588 PyErr_Clear();
2589 state = PyObject_GetAttrString(obj, "__dict__");
2590 if (state == NULL) {
2591 PyErr_Clear();
2592 state = Py_None;
2593 Py_INCREF(state);
2595 names = slotnames(cls);
2596 if (names == NULL)
2597 goto end;
2598 if (names != Py_None) {
2599 assert(PyList_Check(names));
2600 slots = PyDict_New();
2601 if (slots == NULL)
2602 goto end;
2603 n = 0;
2604 /* Can't pre-compute the list size; the list
2605 is stored on the class so accessible to other
2606 threads, which may be run by DECREF */
2607 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2608 PyObject *name, *value;
2609 name = PyList_GET_ITEM(names, i);
2610 value = PyObject_GetAttr(obj, name);
2611 if (value == NULL)
2612 PyErr_Clear();
2613 else {
2614 int err = PyDict_SetItem(slots, name,
2615 value);
2616 Py_DECREF(value);
2617 if (err)
2618 goto end;
2619 n++;
2622 if (n) {
2623 state = Py_BuildValue("(NO)", state, slots);
2624 if (state == NULL)
2625 goto end;
2630 if (!PyList_Check(obj)) {
2631 listitems = Py_None;
2632 Py_INCREF(listitems);
2634 else {
2635 listitems = PyObject_GetIter(obj);
2636 if (listitems == NULL)
2637 goto end;
2640 if (!PyDict_Check(obj)) {
2641 dictitems = Py_None;
2642 Py_INCREF(dictitems);
2644 else {
2645 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2646 if (dictitems == NULL)
2647 goto end;
2650 copy_reg = import_copy_reg();
2651 if (copy_reg == NULL)
2652 goto end;
2653 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2654 if (newobj == NULL)
2655 goto end;
2657 n = PyTuple_GET_SIZE(args);
2658 args2 = PyTuple_New(n+1);
2659 if (args2 == NULL)
2660 goto end;
2661 PyTuple_SET_ITEM(args2, 0, cls);
2662 cls = NULL;
2663 for (i = 0; i < n; i++) {
2664 PyObject *v = PyTuple_GET_ITEM(args, i);
2665 Py_INCREF(v);
2666 PyTuple_SET_ITEM(args2, i+1, v);
2669 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2671 end:
2672 Py_XDECREF(cls);
2673 Py_XDECREF(args);
2674 Py_XDECREF(args2);
2675 Py_XDECREF(slots);
2676 Py_XDECREF(state);
2677 Py_XDECREF(names);
2678 Py_XDECREF(listitems);
2679 Py_XDECREF(dictitems);
2680 Py_XDECREF(copy_reg);
2681 Py_XDECREF(newobj);
2682 return res;
2685 static PyObject *
2686 object_reduce_ex(PyObject *self, PyObject *args)
2688 /* Call copy_reg._reduce_ex(self, proto) */
2689 PyObject *reduce, *copy_reg, *res;
2690 int proto = 0;
2692 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2693 return NULL;
2695 reduce = PyObject_GetAttrString(self, "__reduce__");
2696 if (reduce == NULL)
2697 PyErr_Clear();
2698 else {
2699 PyObject *cls, *clsreduce, *objreduce;
2700 int override;
2701 cls = PyObject_GetAttrString(self, "__class__");
2702 if (cls == NULL) {
2703 Py_DECREF(reduce);
2704 return NULL;
2706 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2707 Py_DECREF(cls);
2708 if (clsreduce == NULL) {
2709 Py_DECREF(reduce);
2710 return NULL;
2712 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2713 "__reduce__");
2714 override = (clsreduce != objreduce);
2715 Py_DECREF(clsreduce);
2716 if (override) {
2717 res = PyObject_CallObject(reduce, NULL);
2718 Py_DECREF(reduce);
2719 return res;
2721 else
2722 Py_DECREF(reduce);
2725 if (proto >= 2)
2726 return reduce_2(self);
2728 copy_reg = import_copy_reg();
2729 if (!copy_reg)
2730 return NULL;
2732 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2733 Py_DECREF(copy_reg);
2735 return res;
2738 static PyMethodDef object_methods[] = {
2739 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2740 PyDoc_STR("helper for pickle")},
2741 {"__reduce__", object_reduce_ex, METH_VARARGS,
2742 PyDoc_STR("helper for pickle")},
2747 PyTypeObject PyBaseObject_Type = {
2748 PyObject_HEAD_INIT(&PyType_Type)
2749 0, /* ob_size */
2750 "object", /* tp_name */
2751 sizeof(PyObject), /* tp_basicsize */
2752 0, /* tp_itemsize */
2753 (destructor)object_dealloc, /* tp_dealloc */
2754 0, /* tp_print */
2755 0, /* tp_getattr */
2756 0, /* tp_setattr */
2757 0, /* tp_compare */
2758 object_repr, /* tp_repr */
2759 0, /* tp_as_number */
2760 0, /* tp_as_sequence */
2761 0, /* tp_as_mapping */
2762 object_hash, /* tp_hash */
2763 0, /* tp_call */
2764 object_str, /* tp_str */
2765 PyObject_GenericGetAttr, /* tp_getattro */
2766 PyObject_GenericSetAttr, /* tp_setattro */
2767 0, /* tp_as_buffer */
2768 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2769 PyDoc_STR("The most base type"), /* tp_doc */
2770 0, /* tp_traverse */
2771 0, /* tp_clear */
2772 0, /* tp_richcompare */
2773 0, /* tp_weaklistoffset */
2774 0, /* tp_iter */
2775 0, /* tp_iternext */
2776 object_methods, /* tp_methods */
2777 0, /* tp_members */
2778 object_getsets, /* tp_getset */
2779 0, /* tp_base */
2780 0, /* tp_dict */
2781 0, /* tp_descr_get */
2782 0, /* tp_descr_set */
2783 0, /* tp_dictoffset */
2784 object_init, /* tp_init */
2785 PyType_GenericAlloc, /* tp_alloc */
2786 object_new, /* tp_new */
2787 PyObject_Del, /* tp_free */
2791 /* Initialize the __dict__ in a type object */
2793 static int
2794 add_methods(PyTypeObject *type, PyMethodDef *meth)
2796 PyObject *dict = type->tp_dict;
2798 for (; meth->ml_name != NULL; meth++) {
2799 PyObject *descr;
2800 if (PyDict_GetItemString(dict, meth->ml_name) &&
2801 !(meth->ml_flags & METH_COEXIST))
2802 continue;
2803 if (meth->ml_flags & METH_CLASS) {
2804 if (meth->ml_flags & METH_STATIC) {
2805 PyErr_SetString(PyExc_ValueError,
2806 "method cannot be both class and static");
2807 return -1;
2809 descr = PyDescr_NewClassMethod(type, meth);
2811 else if (meth->ml_flags & METH_STATIC) {
2812 PyObject *cfunc = PyCFunction_New(meth, NULL);
2813 if (cfunc == NULL)
2814 return -1;
2815 descr = PyStaticMethod_New(cfunc);
2816 Py_DECREF(cfunc);
2818 else {
2819 descr = PyDescr_NewMethod(type, meth);
2821 if (descr == NULL)
2822 return -1;
2823 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2824 return -1;
2825 Py_DECREF(descr);
2827 return 0;
2830 static int
2831 add_members(PyTypeObject *type, PyMemberDef *memb)
2833 PyObject *dict = type->tp_dict;
2835 for (; memb->name != NULL; memb++) {
2836 PyObject *descr;
2837 if (PyDict_GetItemString(dict, memb->name))
2838 continue;
2839 descr = PyDescr_NewMember(type, memb);
2840 if (descr == NULL)
2841 return -1;
2842 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2843 return -1;
2844 Py_DECREF(descr);
2846 return 0;
2849 static int
2850 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2852 PyObject *dict = type->tp_dict;
2854 for (; gsp->name != NULL; gsp++) {
2855 PyObject *descr;
2856 if (PyDict_GetItemString(dict, gsp->name))
2857 continue;
2858 descr = PyDescr_NewGetSet(type, gsp);
2860 if (descr == NULL)
2861 return -1;
2862 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2863 return -1;
2864 Py_DECREF(descr);
2866 return 0;
2869 static void
2870 inherit_special(PyTypeObject *type, PyTypeObject *base)
2872 int oldsize, newsize;
2874 /* Special flag magic */
2875 if (!type->tp_as_buffer && base->tp_as_buffer) {
2876 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2877 type->tp_flags |=
2878 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2880 if (!type->tp_as_sequence && base->tp_as_sequence) {
2881 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2882 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2884 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2885 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2886 if ((!type->tp_as_number && base->tp_as_number) ||
2887 (!type->tp_as_sequence && base->tp_as_sequence)) {
2888 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2889 if (!type->tp_as_number && !type->tp_as_sequence) {
2890 type->tp_flags |= base->tp_flags &
2891 Py_TPFLAGS_HAVE_INPLACEOPS;
2894 /* Wow */
2896 if (!type->tp_as_number && base->tp_as_number) {
2897 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2898 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2901 /* Copying basicsize is connected to the GC flags */
2902 oldsize = base->tp_basicsize;
2903 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2904 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2905 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2906 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2907 (!type->tp_traverse && !type->tp_clear)) {
2908 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2909 if (type->tp_traverse == NULL)
2910 type->tp_traverse = base->tp_traverse;
2911 if (type->tp_clear == NULL)
2912 type->tp_clear = base->tp_clear;
2914 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2915 /* The condition below could use some explanation.
2916 It appears that tp_new is not inherited for static types
2917 whose base class is 'object'; this seems to be a precaution
2918 so that old extension types don't suddenly become
2919 callable (object.__new__ wouldn't insure the invariants
2920 that the extension type's own factory function ensures).
2921 Heap types, of course, are under our control, so they do
2922 inherit tp_new; static extension types that specify some
2923 other built-in type as the default are considered
2924 new-style-aware so they also inherit object.__new__. */
2925 if (base != &PyBaseObject_Type ||
2926 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2927 if (type->tp_new == NULL)
2928 type->tp_new = base->tp_new;
2931 type->tp_basicsize = newsize;
2933 /* Copy other non-function slots */
2935 #undef COPYVAL
2936 #define COPYVAL(SLOT) \
2937 if (type->SLOT == 0) type->SLOT = base->SLOT
2939 COPYVAL(tp_itemsize);
2940 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2941 COPYVAL(tp_weaklistoffset);
2943 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2944 COPYVAL(tp_dictoffset);
2948 static void
2949 inherit_slots(PyTypeObject *type, PyTypeObject *base)
2951 PyTypeObject *basebase;
2953 #undef SLOTDEFINED
2954 #undef COPYSLOT
2955 #undef COPYNUM
2956 #undef COPYSEQ
2957 #undef COPYMAP
2958 #undef COPYBUF
2960 #define SLOTDEFINED(SLOT) \
2961 (base->SLOT != 0 && \
2962 (basebase == NULL || base->SLOT != basebase->SLOT))
2964 #define COPYSLOT(SLOT) \
2965 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
2967 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2968 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2969 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
2970 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
2972 /* This won't inherit indirect slots (from tp_as_number etc.)
2973 if type doesn't provide the space. */
2975 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2976 basebase = base->tp_base;
2977 if (basebase->tp_as_number == NULL)
2978 basebase = NULL;
2979 COPYNUM(nb_add);
2980 COPYNUM(nb_subtract);
2981 COPYNUM(nb_multiply);
2982 COPYNUM(nb_divide);
2983 COPYNUM(nb_remainder);
2984 COPYNUM(nb_divmod);
2985 COPYNUM(nb_power);
2986 COPYNUM(nb_negative);
2987 COPYNUM(nb_positive);
2988 COPYNUM(nb_absolute);
2989 COPYNUM(nb_nonzero);
2990 COPYNUM(nb_invert);
2991 COPYNUM(nb_lshift);
2992 COPYNUM(nb_rshift);
2993 COPYNUM(nb_and);
2994 COPYNUM(nb_xor);
2995 COPYNUM(nb_or);
2996 COPYNUM(nb_coerce);
2997 COPYNUM(nb_int);
2998 COPYNUM(nb_long);
2999 COPYNUM(nb_float);
3000 COPYNUM(nb_oct);
3001 COPYNUM(nb_hex);
3002 COPYNUM(nb_inplace_add);
3003 COPYNUM(nb_inplace_subtract);
3004 COPYNUM(nb_inplace_multiply);
3005 COPYNUM(nb_inplace_divide);
3006 COPYNUM(nb_inplace_remainder);
3007 COPYNUM(nb_inplace_power);
3008 COPYNUM(nb_inplace_lshift);
3009 COPYNUM(nb_inplace_rshift);
3010 COPYNUM(nb_inplace_and);
3011 COPYNUM(nb_inplace_xor);
3012 COPYNUM(nb_inplace_or);
3013 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3014 COPYNUM(nb_true_divide);
3015 COPYNUM(nb_floor_divide);
3016 COPYNUM(nb_inplace_true_divide);
3017 COPYNUM(nb_inplace_floor_divide);
3021 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3022 basebase = base->tp_base;
3023 if (basebase->tp_as_sequence == NULL)
3024 basebase = NULL;
3025 COPYSEQ(sq_length);
3026 COPYSEQ(sq_concat);
3027 COPYSEQ(sq_repeat);
3028 COPYSEQ(sq_item);
3029 COPYSEQ(sq_slice);
3030 COPYSEQ(sq_ass_item);
3031 COPYSEQ(sq_ass_slice);
3032 COPYSEQ(sq_contains);
3033 COPYSEQ(sq_inplace_concat);
3034 COPYSEQ(sq_inplace_repeat);
3037 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3038 basebase = base->tp_base;
3039 if (basebase->tp_as_mapping == NULL)
3040 basebase = NULL;
3041 COPYMAP(mp_length);
3042 COPYMAP(mp_subscript);
3043 COPYMAP(mp_ass_subscript);
3046 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3047 basebase = base->tp_base;
3048 if (basebase->tp_as_buffer == NULL)
3049 basebase = NULL;
3050 COPYBUF(bf_getreadbuffer);
3051 COPYBUF(bf_getwritebuffer);
3052 COPYBUF(bf_getsegcount);
3053 COPYBUF(bf_getcharbuffer);
3056 basebase = base->tp_base;
3058 COPYSLOT(tp_dealloc);
3059 COPYSLOT(tp_print);
3060 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3061 type->tp_getattr = base->tp_getattr;
3062 type->tp_getattro = base->tp_getattro;
3064 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3065 type->tp_setattr = base->tp_setattr;
3066 type->tp_setattro = base->tp_setattro;
3068 /* tp_compare see tp_richcompare */
3069 COPYSLOT(tp_repr);
3070 /* tp_hash see tp_richcompare */
3071 COPYSLOT(tp_call);
3072 COPYSLOT(tp_str);
3073 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3074 if (type->tp_compare == NULL &&
3075 type->tp_richcompare == NULL &&
3076 type->tp_hash == NULL)
3078 type->tp_compare = base->tp_compare;
3079 type->tp_richcompare = base->tp_richcompare;
3080 type->tp_hash = base->tp_hash;
3083 else {
3084 COPYSLOT(tp_compare);
3086 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3087 COPYSLOT(tp_iter);
3088 COPYSLOT(tp_iternext);
3090 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3091 COPYSLOT(tp_descr_get);
3092 COPYSLOT(tp_descr_set);
3093 COPYSLOT(tp_dictoffset);
3094 COPYSLOT(tp_init);
3095 COPYSLOT(tp_alloc);
3096 COPYSLOT(tp_is_gc);
3097 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3098 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3099 /* They agree about gc. */
3100 COPYSLOT(tp_free);
3102 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3103 type->tp_free == NULL &&
3104 base->tp_free == _PyObject_Del) {
3105 /* A bit of magic to plug in the correct default
3106 * tp_free function when a derived class adds gc,
3107 * didn't define tp_free, and the base uses the
3108 * default non-gc tp_free.
3110 type->tp_free = PyObject_GC_Del;
3112 /* else they didn't agree about gc, and there isn't something
3113 * obvious to be done -- the type is on its own.
3118 static int add_operators(PyTypeObject *);
3121 PyType_Ready(PyTypeObject *type)
3123 PyObject *dict, *bases;
3124 PyTypeObject *base;
3125 int i, n;
3127 if (type->tp_flags & Py_TPFLAGS_READY) {
3128 assert(type->tp_dict != NULL);
3129 return 0;
3131 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3133 type->tp_flags |= Py_TPFLAGS_READYING;
3135 #ifdef Py_TRACE_REFS
3136 /* PyType_Ready is the closest thing we have to a choke point
3137 * for type objects, so is the best place I can think of to try
3138 * to get type objects into the doubly-linked list of all objects.
3139 * Still, not all type objects go thru PyType_Ready.
3141 _Py_AddToAllObjects((PyObject *)type, 0);
3142 #endif
3144 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3145 base = type->tp_base;
3146 if (base == NULL && type != &PyBaseObject_Type)
3147 base = type->tp_base = &PyBaseObject_Type;
3149 /* Initialize the base class */
3150 if (base && base->tp_dict == NULL) {
3151 if (PyType_Ready(base) < 0)
3152 goto error;
3155 /* Initialize ob_type if NULL. This means extensions that want to be
3156 compilable separately on Windows can call PyType_Ready() instead of
3157 initializing the ob_type field of their type objects. */
3158 if (type->ob_type == NULL)
3159 type->ob_type = base->ob_type;
3161 /* Initialize tp_bases */
3162 bases = type->tp_bases;
3163 if (bases == NULL) {
3164 if (base == NULL)
3165 bases = PyTuple_New(0);
3166 else
3167 bases = PyTuple_Pack(1, base);
3168 if (bases == NULL)
3169 goto error;
3170 type->tp_bases = bases;
3173 /* Initialize tp_dict */
3174 dict = type->tp_dict;
3175 if (dict == NULL) {
3176 dict = PyDict_New();
3177 if (dict == NULL)
3178 goto error;
3179 type->tp_dict = dict;
3182 /* Add type-specific descriptors to tp_dict */
3183 if (add_operators(type) < 0)
3184 goto error;
3185 if (type->tp_methods != NULL) {
3186 if (add_methods(type, type->tp_methods) < 0)
3187 goto error;
3189 if (type->tp_members != NULL) {
3190 if (add_members(type, type->tp_members) < 0)
3191 goto error;
3193 if (type->tp_getset != NULL) {
3194 if (add_getset(type, type->tp_getset) < 0)
3195 goto error;
3198 /* Calculate method resolution order */
3199 if (mro_internal(type) < 0) {
3200 goto error;
3203 /* Inherit special flags from dominant base */
3204 if (type->tp_base != NULL)
3205 inherit_special(type, type->tp_base);
3207 /* Initialize tp_dict properly */
3208 bases = type->tp_mro;
3209 assert(bases != NULL);
3210 assert(PyTuple_Check(bases));
3211 n = PyTuple_GET_SIZE(bases);
3212 for (i = 1; i < n; i++) {
3213 PyObject *b = PyTuple_GET_ITEM(bases, i);
3214 if (PyType_Check(b))
3215 inherit_slots(type, (PyTypeObject *)b);
3218 /* Sanity check for tp_free. */
3219 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3220 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3221 /* This base class needs to call tp_free, but doesn't have
3222 * one, or its tp_free is for non-gc'ed objects.
3224 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3225 "gc and is a base type but has inappropriate "
3226 "tp_free slot",
3227 type->tp_name);
3228 goto error;
3231 /* if the type dictionary doesn't contain a __doc__, set it from
3232 the tp_doc slot.
3234 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3235 if (type->tp_doc != NULL) {
3236 PyObject *doc = PyString_FromString(type->tp_doc);
3237 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3238 Py_DECREF(doc);
3239 } else {
3240 PyDict_SetItemString(type->tp_dict,
3241 "__doc__", Py_None);
3245 /* Some more special stuff */
3246 base = type->tp_base;
3247 if (base != NULL) {
3248 if (type->tp_as_number == NULL)
3249 type->tp_as_number = base->tp_as_number;
3250 if (type->tp_as_sequence == NULL)
3251 type->tp_as_sequence = base->tp_as_sequence;
3252 if (type->tp_as_mapping == NULL)
3253 type->tp_as_mapping = base->tp_as_mapping;
3254 if (type->tp_as_buffer == NULL)
3255 type->tp_as_buffer = base->tp_as_buffer;
3258 /* Link into each base class's list of subclasses */
3259 bases = type->tp_bases;
3260 n = PyTuple_GET_SIZE(bases);
3261 for (i = 0; i < n; i++) {
3262 PyObject *b = PyTuple_GET_ITEM(bases, i);
3263 if (PyType_Check(b) &&
3264 add_subclass((PyTypeObject *)b, type) < 0)
3265 goto error;
3268 /* All done -- set the ready flag */
3269 assert(type->tp_dict != NULL);
3270 type->tp_flags =
3271 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3272 return 0;
3274 error:
3275 type->tp_flags &= ~Py_TPFLAGS_READYING;
3276 return -1;
3279 static int
3280 add_subclass(PyTypeObject *base, PyTypeObject *type)
3282 int i;
3283 PyObject *list, *ref, *new;
3285 list = base->tp_subclasses;
3286 if (list == NULL) {
3287 base->tp_subclasses = list = PyList_New(0);
3288 if (list == NULL)
3289 return -1;
3291 assert(PyList_Check(list));
3292 new = PyWeakref_NewRef((PyObject *)type, NULL);
3293 i = PyList_GET_SIZE(list);
3294 while (--i >= 0) {
3295 ref = PyList_GET_ITEM(list, i);
3296 assert(PyWeakref_CheckRef(ref));
3297 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3298 return PyList_SetItem(list, i, new);
3300 i = PyList_Append(list, new);
3301 Py_DECREF(new);
3302 return i;
3305 static void
3306 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3308 int i;
3309 PyObject *list, *ref;
3311 list = base->tp_subclasses;
3312 if (list == NULL) {
3313 return;
3315 assert(PyList_Check(list));
3316 i = PyList_GET_SIZE(list);
3317 while (--i >= 0) {
3318 ref = PyList_GET_ITEM(list, i);
3319 assert(PyWeakref_CheckRef(ref));
3320 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3321 /* this can't fail, right? */
3322 PySequence_DelItem(list, i);
3323 return;
3328 static int
3329 check_num_args(PyObject *ob, int n)
3331 if (!PyTuple_CheckExact(ob)) {
3332 PyErr_SetString(PyExc_SystemError,
3333 "PyArg_UnpackTuple() argument list is not a tuple");
3334 return 0;
3336 if (n == PyTuple_GET_SIZE(ob))
3337 return 1;
3338 PyErr_Format(
3339 PyExc_TypeError,
3340 "expected %d arguments, got %d", n, PyTuple_GET_SIZE(ob));
3341 return 0;
3344 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3346 /* There's a wrapper *function* for each distinct function typedef used
3347 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3348 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3349 Most tables have only one entry; the tables for binary operators have two
3350 entries, one regular and one with reversed arguments. */
3352 static PyObject *
3353 wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
3355 inquiry func = (inquiry)wrapped;
3356 int res;
3358 if (!check_num_args(args, 0))
3359 return NULL;
3360 res = (*func)(self);
3361 if (res == -1 && PyErr_Occurred())
3362 return NULL;
3363 return PyInt_FromLong((long)res);
3366 static PyObject *
3367 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3369 inquiry func = (inquiry)wrapped;
3370 int res;
3372 if (!check_num_args(args, 0))
3373 return NULL;
3374 res = (*func)(self);
3375 if (res == -1 && PyErr_Occurred())
3376 return NULL;
3377 return PyBool_FromLong((long)res);
3380 static PyObject *
3381 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3383 binaryfunc func = (binaryfunc)wrapped;
3384 PyObject *other;
3386 if (!check_num_args(args, 1))
3387 return NULL;
3388 other = PyTuple_GET_ITEM(args, 0);
3389 return (*func)(self, other);
3392 static PyObject *
3393 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3395 binaryfunc func = (binaryfunc)wrapped;
3396 PyObject *other;
3398 if (!check_num_args(args, 1))
3399 return NULL;
3400 other = PyTuple_GET_ITEM(args, 0);
3401 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3402 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3403 Py_INCREF(Py_NotImplemented);
3404 return Py_NotImplemented;
3406 return (*func)(self, other);
3409 static PyObject *
3410 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3412 binaryfunc func = (binaryfunc)wrapped;
3413 PyObject *other;
3415 if (!check_num_args(args, 1))
3416 return NULL;
3417 other = PyTuple_GET_ITEM(args, 0);
3418 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3419 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3420 Py_INCREF(Py_NotImplemented);
3421 return Py_NotImplemented;
3423 return (*func)(other, self);
3426 static PyObject *
3427 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3429 coercion func = (coercion)wrapped;
3430 PyObject *other, *res;
3431 int ok;
3433 if (!check_num_args(args, 1))
3434 return NULL;
3435 other = PyTuple_GET_ITEM(args, 0);
3436 ok = func(&self, &other);
3437 if (ok < 0)
3438 return NULL;
3439 if (ok > 0) {
3440 Py_INCREF(Py_NotImplemented);
3441 return Py_NotImplemented;
3443 res = PyTuple_New(2);
3444 if (res == NULL) {
3445 Py_DECREF(self);
3446 Py_DECREF(other);
3447 return NULL;
3449 PyTuple_SET_ITEM(res, 0, self);
3450 PyTuple_SET_ITEM(res, 1, other);
3451 return res;
3454 static PyObject *
3455 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3457 ternaryfunc func = (ternaryfunc)wrapped;
3458 PyObject *other;
3459 PyObject *third = Py_None;
3461 /* Note: This wrapper only works for __pow__() */
3463 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3464 return NULL;
3465 return (*func)(self, other, third);
3468 static PyObject *
3469 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3471 ternaryfunc func = (ternaryfunc)wrapped;
3472 PyObject *other;
3473 PyObject *third = Py_None;
3475 /* Note: This wrapper only works for __pow__() */
3477 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3478 return NULL;
3479 return (*func)(other, self, third);
3482 static PyObject *
3483 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3485 unaryfunc func = (unaryfunc)wrapped;
3487 if (!check_num_args(args, 0))
3488 return NULL;
3489 return (*func)(self);
3492 static PyObject *
3493 wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
3495 intargfunc func = (intargfunc)wrapped;
3496 int i;
3498 if (!PyArg_ParseTuple(args, "i", &i))
3499 return NULL;
3500 return (*func)(self, i);
3503 static int
3504 getindex(PyObject *self, PyObject *arg)
3506 int i;
3508 i = PyInt_AsLong(arg);
3509 if (i == -1 && PyErr_Occurred())
3510 return -1;
3511 if (i < 0) {
3512 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3513 if (sq && sq->sq_length) {
3514 int n = (*sq->sq_length)(self);
3515 if (n < 0)
3516 return -1;
3517 i += n;
3520 return i;
3523 static PyObject *
3524 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3526 intargfunc func = (intargfunc)wrapped;
3527 PyObject *arg;
3528 int i;
3530 if (PyTuple_GET_SIZE(args) == 1) {
3531 arg = PyTuple_GET_ITEM(args, 0);
3532 i = getindex(self, arg);
3533 if (i == -1 && PyErr_Occurred())
3534 return NULL;
3535 return (*func)(self, i);
3537 check_num_args(args, 1);
3538 assert(PyErr_Occurred());
3539 return NULL;
3542 static PyObject *
3543 wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
3545 intintargfunc func = (intintargfunc)wrapped;
3546 int i, j;
3548 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3549 return NULL;
3550 return (*func)(self, i, j);
3553 static PyObject *
3554 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3556 intobjargproc func = (intobjargproc)wrapped;
3557 int i, res;
3558 PyObject *arg, *value;
3560 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3561 return NULL;
3562 i = getindex(self, arg);
3563 if (i == -1 && PyErr_Occurred())
3564 return NULL;
3565 res = (*func)(self, i, value);
3566 if (res == -1 && PyErr_Occurred())
3567 return NULL;
3568 Py_INCREF(Py_None);
3569 return Py_None;
3572 static PyObject *
3573 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3575 intobjargproc func = (intobjargproc)wrapped;
3576 int i, res;
3577 PyObject *arg;
3579 if (!check_num_args(args, 1))
3580 return NULL;
3581 arg = PyTuple_GET_ITEM(args, 0);
3582 i = getindex(self, arg);
3583 if (i == -1 && PyErr_Occurred())
3584 return NULL;
3585 res = (*func)(self, i, NULL);
3586 if (res == -1 && PyErr_Occurred())
3587 return NULL;
3588 Py_INCREF(Py_None);
3589 return Py_None;
3592 static PyObject *
3593 wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
3595 intintobjargproc func = (intintobjargproc)wrapped;
3596 int i, j, res;
3597 PyObject *value;
3599 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
3600 return NULL;
3601 res = (*func)(self, i, j, value);
3602 if (res == -1 && PyErr_Occurred())
3603 return NULL;
3604 Py_INCREF(Py_None);
3605 return Py_None;
3608 static PyObject *
3609 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3611 intintobjargproc func = (intintobjargproc)wrapped;
3612 int i, j, res;
3614 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3615 return NULL;
3616 res = (*func)(self, i, j, NULL);
3617 if (res == -1 && PyErr_Occurred())
3618 return NULL;
3619 Py_INCREF(Py_None);
3620 return Py_None;
3623 /* XXX objobjproc is a misnomer; should be objargpred */
3624 static PyObject *
3625 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3627 objobjproc func = (objobjproc)wrapped;
3628 int res;
3629 PyObject *value;
3631 if (!check_num_args(args, 1))
3632 return NULL;
3633 value = PyTuple_GET_ITEM(args, 0);
3634 res = (*func)(self, value);
3635 if (res == -1 && PyErr_Occurred())
3636 return NULL;
3637 else
3638 return PyBool_FromLong(res);
3641 static PyObject *
3642 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3644 objobjargproc func = (objobjargproc)wrapped;
3645 int res;
3646 PyObject *key, *value;
3648 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3649 return NULL;
3650 res = (*func)(self, key, value);
3651 if (res == -1 && PyErr_Occurred())
3652 return NULL;
3653 Py_INCREF(Py_None);
3654 return Py_None;
3657 static PyObject *
3658 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3660 objobjargproc func = (objobjargproc)wrapped;
3661 int res;
3662 PyObject *key;
3664 if (!check_num_args(args, 1))
3665 return NULL;
3666 key = PyTuple_GET_ITEM(args, 0);
3667 res = (*func)(self, key, NULL);
3668 if (res == -1 && PyErr_Occurred())
3669 return NULL;
3670 Py_INCREF(Py_None);
3671 return Py_None;
3674 static PyObject *
3675 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3677 cmpfunc func = (cmpfunc)wrapped;
3678 int res;
3679 PyObject *other;
3681 if (!check_num_args(args, 1))
3682 return NULL;
3683 other = PyTuple_GET_ITEM(args, 0);
3684 if (other->ob_type->tp_compare != func &&
3685 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3686 PyErr_Format(
3687 PyExc_TypeError,
3688 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3689 self->ob_type->tp_name,
3690 self->ob_type->tp_name,
3691 other->ob_type->tp_name);
3692 return NULL;
3694 res = (*func)(self, other);
3695 if (PyErr_Occurred())
3696 return NULL;
3697 return PyInt_FromLong((long)res);
3700 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3701 This is called the Carlo Verre hack after its discoverer. */
3702 static int
3703 hackcheck(PyObject *self, setattrofunc func, char *what)
3705 PyTypeObject *type = self->ob_type;
3706 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3707 type = type->tp_base;
3708 if (type->tp_setattro != func) {
3709 PyErr_Format(PyExc_TypeError,
3710 "can't apply this %s to %s object",
3711 what,
3712 type->tp_name);
3713 return 0;
3715 return 1;
3718 static PyObject *
3719 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3721 setattrofunc func = (setattrofunc)wrapped;
3722 int res;
3723 PyObject *name, *value;
3725 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3726 return NULL;
3727 if (!hackcheck(self, func, "__setattr__"))
3728 return NULL;
3729 res = (*func)(self, name, value);
3730 if (res < 0)
3731 return NULL;
3732 Py_INCREF(Py_None);
3733 return Py_None;
3736 static PyObject *
3737 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3739 setattrofunc func = (setattrofunc)wrapped;
3740 int res;
3741 PyObject *name;
3743 if (!check_num_args(args, 1))
3744 return NULL;
3745 name = PyTuple_GET_ITEM(args, 0);
3746 if (!hackcheck(self, func, "__delattr__"))
3747 return NULL;
3748 res = (*func)(self, name, NULL);
3749 if (res < 0)
3750 return NULL;
3751 Py_INCREF(Py_None);
3752 return Py_None;
3755 static PyObject *
3756 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3758 hashfunc func = (hashfunc)wrapped;
3759 long res;
3761 if (!check_num_args(args, 0))
3762 return NULL;
3763 res = (*func)(self);
3764 if (res == -1 && PyErr_Occurred())
3765 return NULL;
3766 return PyInt_FromLong(res);
3769 static PyObject *
3770 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3772 ternaryfunc func = (ternaryfunc)wrapped;
3774 return (*func)(self, args, kwds);
3777 static PyObject *
3778 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3780 richcmpfunc func = (richcmpfunc)wrapped;
3781 PyObject *other;
3783 if (!check_num_args(args, 1))
3784 return NULL;
3785 other = PyTuple_GET_ITEM(args, 0);
3786 return (*func)(self, other, op);
3789 #undef RICHCMP_WRAPPER
3790 #define RICHCMP_WRAPPER(NAME, OP) \
3791 static PyObject * \
3792 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3794 return wrap_richcmpfunc(self, args, wrapped, OP); \
3797 RICHCMP_WRAPPER(lt, Py_LT)
3798 RICHCMP_WRAPPER(le, Py_LE)
3799 RICHCMP_WRAPPER(eq, Py_EQ)
3800 RICHCMP_WRAPPER(ne, Py_NE)
3801 RICHCMP_WRAPPER(gt, Py_GT)
3802 RICHCMP_WRAPPER(ge, Py_GE)
3804 static PyObject *
3805 wrap_next(PyObject *self, PyObject *args, void *wrapped)
3807 unaryfunc func = (unaryfunc)wrapped;
3808 PyObject *res;
3810 if (!check_num_args(args, 0))
3811 return NULL;
3812 res = (*func)(self);
3813 if (res == NULL && !PyErr_Occurred())
3814 PyErr_SetNone(PyExc_StopIteration);
3815 return res;
3818 static PyObject *
3819 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3821 descrgetfunc func = (descrgetfunc)wrapped;
3822 PyObject *obj;
3823 PyObject *type = NULL;
3825 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3826 return NULL;
3827 if (obj == Py_None)
3828 obj = NULL;
3829 if (type == Py_None)
3830 type = NULL;
3831 if (type == NULL &&obj == NULL) {
3832 PyErr_SetString(PyExc_TypeError,
3833 "__get__(None, None) is invalid");
3834 return NULL;
3836 return (*func)(self, obj, type);
3839 static PyObject *
3840 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3842 descrsetfunc func = (descrsetfunc)wrapped;
3843 PyObject *obj, *value;
3844 int ret;
3846 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3847 return NULL;
3848 ret = (*func)(self, obj, value);
3849 if (ret < 0)
3850 return NULL;
3851 Py_INCREF(Py_None);
3852 return Py_None;
3855 static PyObject *
3856 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3858 descrsetfunc func = (descrsetfunc)wrapped;
3859 PyObject *obj;
3860 int ret;
3862 if (!check_num_args(args, 1))
3863 return NULL;
3864 obj = PyTuple_GET_ITEM(args, 0);
3865 ret = (*func)(self, obj, NULL);
3866 if (ret < 0)
3867 return NULL;
3868 Py_INCREF(Py_None);
3869 return Py_None;
3872 static PyObject *
3873 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3875 initproc func = (initproc)wrapped;
3877 if (func(self, args, kwds) < 0)
3878 return NULL;
3879 Py_INCREF(Py_None);
3880 return Py_None;
3883 static PyObject *
3884 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3886 PyTypeObject *type, *subtype, *staticbase;
3887 PyObject *arg0, *res;
3889 if (self == NULL || !PyType_Check(self))
3890 Py_FatalError("__new__() called with non-type 'self'");
3891 type = (PyTypeObject *)self;
3892 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3893 PyErr_Format(PyExc_TypeError,
3894 "%s.__new__(): not enough arguments",
3895 type->tp_name);
3896 return NULL;
3898 arg0 = PyTuple_GET_ITEM(args, 0);
3899 if (!PyType_Check(arg0)) {
3900 PyErr_Format(PyExc_TypeError,
3901 "%s.__new__(X): X is not a type object (%s)",
3902 type->tp_name,
3903 arg0->ob_type->tp_name);
3904 return NULL;
3906 subtype = (PyTypeObject *)arg0;
3907 if (!PyType_IsSubtype(subtype, type)) {
3908 PyErr_Format(PyExc_TypeError,
3909 "%s.__new__(%s): %s is not a subtype of %s",
3910 type->tp_name,
3911 subtype->tp_name,
3912 subtype->tp_name,
3913 type->tp_name);
3914 return NULL;
3917 /* Check that the use doesn't do something silly and unsafe like
3918 object.__new__(dict). To do this, we check that the
3919 most derived base that's not a heap type is this type. */
3920 staticbase = subtype;
3921 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3922 staticbase = staticbase->tp_base;
3923 if (staticbase->tp_new != type->tp_new) {
3924 PyErr_Format(PyExc_TypeError,
3925 "%s.__new__(%s) is not safe, use %s.__new__()",
3926 type->tp_name,
3927 subtype->tp_name,
3928 staticbase == NULL ? "?" : staticbase->tp_name);
3929 return NULL;
3932 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3933 if (args == NULL)
3934 return NULL;
3935 res = type->tp_new(subtype, args, kwds);
3936 Py_DECREF(args);
3937 return res;
3940 static struct PyMethodDef tp_new_methoddef[] = {
3941 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
3942 PyDoc_STR("T.__new__(S, ...) -> "
3943 "a new object with type S, a subtype of T")},
3947 static int
3948 add_tp_new_wrapper(PyTypeObject *type)
3950 PyObject *func;
3952 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
3953 return 0;
3954 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
3955 if (func == NULL)
3956 return -1;
3957 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
3958 Py_DECREF(func);
3959 return -1;
3961 Py_DECREF(func);
3962 return 0;
3965 /* Slot wrappers that call the corresponding __foo__ slot. See comments
3966 below at override_slots() for more explanation. */
3968 #define SLOT0(FUNCNAME, OPSTR) \
3969 static PyObject * \
3970 FUNCNAME(PyObject *self) \
3972 static PyObject *cache_str; \
3973 return call_method(self, OPSTR, &cache_str, "()"); \
3976 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
3977 static PyObject * \
3978 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
3980 static PyObject *cache_str; \
3981 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
3984 /* Boolean helper for SLOT1BINFULL().
3985 right.__class__ is a nontrivial subclass of left.__class__. */
3986 static int
3987 method_is_overloaded(PyObject *left, PyObject *right, char *name)
3989 PyObject *a, *b;
3990 int ok;
3992 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
3993 if (b == NULL) {
3994 PyErr_Clear();
3995 /* If right doesn't have it, it's not overloaded */
3996 return 0;
3999 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4000 if (a == NULL) {
4001 PyErr_Clear();
4002 Py_DECREF(b);
4003 /* If right has it but left doesn't, it's overloaded */
4004 return 1;
4007 ok = PyObject_RichCompareBool(a, b, Py_NE);
4008 Py_DECREF(a);
4009 Py_DECREF(b);
4010 if (ok < 0) {
4011 PyErr_Clear();
4012 return 0;
4015 return ok;
4019 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4020 static PyObject * \
4021 FUNCNAME(PyObject *self, PyObject *other) \
4023 static PyObject *cache_str, *rcache_str; \
4024 int do_other = self->ob_type != other->ob_type && \
4025 other->ob_type->tp_as_number != NULL && \
4026 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4027 if (self->ob_type->tp_as_number != NULL && \
4028 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4029 PyObject *r; \
4030 if (do_other && \
4031 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4032 method_is_overloaded(self, other, ROPSTR)) { \
4033 r = call_maybe( \
4034 other, ROPSTR, &rcache_str, "(O)", self); \
4035 if (r != Py_NotImplemented) \
4036 return r; \
4037 Py_DECREF(r); \
4038 do_other = 0; \
4040 r = call_maybe( \
4041 self, OPSTR, &cache_str, "(O)", other); \
4042 if (r != Py_NotImplemented || \
4043 other->ob_type == self->ob_type) \
4044 return r; \
4045 Py_DECREF(r); \
4047 if (do_other) { \
4048 return call_maybe( \
4049 other, ROPSTR, &rcache_str, "(O)", self); \
4051 Py_INCREF(Py_NotImplemented); \
4052 return Py_NotImplemented; \
4055 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4056 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4058 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4059 static PyObject * \
4060 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4062 static PyObject *cache_str; \
4063 return call_method(self, OPSTR, &cache_str, \
4064 "(" ARGCODES ")", arg1, arg2); \
4067 static int
4068 slot_sq_length(PyObject *self)
4070 static PyObject *len_str;
4071 PyObject *res = call_method(self, "__len__", &len_str, "()");
4072 int len;
4074 if (res == NULL)
4075 return -1;
4076 len = (int)PyInt_AsLong(res);
4077 Py_DECREF(res);
4078 if (len == -1 && PyErr_Occurred())
4079 return -1;
4080 if (len < 0) {
4081 PyErr_SetString(PyExc_ValueError,
4082 "__len__() should return >= 0");
4083 return -1;
4085 return len;
4088 SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
4089 SLOT1(slot_sq_repeat, "__mul__", int, "i")
4091 /* Super-optimized version of slot_sq_item.
4092 Other slots could do the same... */
4093 static PyObject *
4094 slot_sq_item(PyObject *self, int i)
4096 static PyObject *getitem_str;
4097 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4098 descrgetfunc f;
4100 if (getitem_str == NULL) {
4101 getitem_str = PyString_InternFromString("__getitem__");
4102 if (getitem_str == NULL)
4103 return NULL;
4105 func = _PyType_Lookup(self->ob_type, getitem_str);
4106 if (func != NULL) {
4107 if ((f = func->ob_type->tp_descr_get) == NULL)
4108 Py_INCREF(func);
4109 else {
4110 func = f(func, self, (PyObject *)(self->ob_type));
4111 if (func == NULL) {
4112 return NULL;
4115 ival = PyInt_FromLong(i);
4116 if (ival != NULL) {
4117 args = PyTuple_New(1);
4118 if (args != NULL) {
4119 PyTuple_SET_ITEM(args, 0, ival);
4120 retval = PyObject_Call(func, args, NULL);
4121 Py_XDECREF(args);
4122 Py_XDECREF(func);
4123 return retval;
4127 else {
4128 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4130 Py_XDECREF(args);
4131 Py_XDECREF(ival);
4132 Py_XDECREF(func);
4133 return NULL;
4136 SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
4138 static int
4139 slot_sq_ass_item(PyObject *self, int index, PyObject *value)
4141 PyObject *res;
4142 static PyObject *delitem_str, *setitem_str;
4144 if (value == NULL)
4145 res = call_method(self, "__delitem__", &delitem_str,
4146 "(i)", index);
4147 else
4148 res = call_method(self, "__setitem__", &setitem_str,
4149 "(iO)", index, value);
4150 if (res == NULL)
4151 return -1;
4152 Py_DECREF(res);
4153 return 0;
4156 static int
4157 slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
4159 PyObject *res;
4160 static PyObject *delslice_str, *setslice_str;
4162 if (value == NULL)
4163 res = call_method(self, "__delslice__", &delslice_str,
4164 "(ii)", i, j);
4165 else
4166 res = call_method(self, "__setslice__", &setslice_str,
4167 "(iiO)", i, j, value);
4168 if (res == NULL)
4169 return -1;
4170 Py_DECREF(res);
4171 return 0;
4174 static int
4175 slot_sq_contains(PyObject *self, PyObject *value)
4177 PyObject *func, *res, *args;
4178 int result = -1;
4180 static PyObject *contains_str;
4182 func = lookup_maybe(self, "__contains__", &contains_str);
4183 if (func != NULL) {
4184 args = PyTuple_Pack(1, value);
4185 if (args == NULL)
4186 res = NULL;
4187 else {
4188 res = PyObject_Call(func, args, NULL);
4189 Py_DECREF(args);
4191 Py_DECREF(func);
4192 if (res != NULL) {
4193 result = PyObject_IsTrue(res);
4194 Py_DECREF(res);
4197 else if (! PyErr_Occurred()) {
4198 result = _PySequence_IterSearch(self, value,
4199 PY_ITERSEARCH_CONTAINS);
4201 return result;
4204 SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
4205 SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
4207 #define slot_mp_length slot_sq_length
4209 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4211 static int
4212 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4214 PyObject *res;
4215 static PyObject *delitem_str, *setitem_str;
4217 if (value == NULL)
4218 res = call_method(self, "__delitem__", &delitem_str,
4219 "(O)", key);
4220 else
4221 res = call_method(self, "__setitem__", &setitem_str,
4222 "(OO)", key, value);
4223 if (res == NULL)
4224 return -1;
4225 Py_DECREF(res);
4226 return 0;
4229 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4230 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4231 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4232 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4233 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4234 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4236 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4238 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4239 nb_power, "__pow__", "__rpow__")
4241 static PyObject *
4242 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4244 static PyObject *pow_str;
4246 if (modulus == Py_None)
4247 return slot_nb_power_binary(self, other);
4248 /* Three-arg power doesn't use __rpow__. But ternary_op
4249 can call this when the second argument's type uses
4250 slot_nb_power, so check before calling self.__pow__. */
4251 if (self->ob_type->tp_as_number != NULL &&
4252 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4253 return call_method(self, "__pow__", &pow_str,
4254 "(OO)", other, modulus);
4256 Py_INCREF(Py_NotImplemented);
4257 return Py_NotImplemented;
4260 SLOT0(slot_nb_negative, "__neg__")
4261 SLOT0(slot_nb_positive, "__pos__")
4262 SLOT0(slot_nb_absolute, "__abs__")
4264 static int
4265 slot_nb_nonzero(PyObject *self)
4267 PyObject *func, *args;
4268 static PyObject *nonzero_str, *len_str;
4269 int result = -1;
4271 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4272 if (func == NULL) {
4273 if (PyErr_Occurred())
4274 return -1;
4275 func = lookup_maybe(self, "__len__", &len_str);
4276 if (func == NULL)
4277 return PyErr_Occurred() ? -1 : 1;
4279 args = PyTuple_New(0);
4280 if (args != NULL) {
4281 PyObject *temp = PyObject_Call(func, args, NULL);
4282 Py_DECREF(args);
4283 if (temp != NULL) {
4284 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4285 result = PyObject_IsTrue(temp);
4286 else {
4287 PyErr_Format(PyExc_TypeError,
4288 "__nonzero__ should return "
4289 "bool or int, returned %s",
4290 temp->ob_type->tp_name);
4291 result = -1;
4293 Py_DECREF(temp);
4296 Py_DECREF(func);
4297 return result;
4300 SLOT0(slot_nb_invert, "__invert__")
4301 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4302 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4303 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4304 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4305 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4307 static int
4308 slot_nb_coerce(PyObject **a, PyObject **b)
4310 static PyObject *coerce_str;
4311 PyObject *self = *a, *other = *b;
4313 if (self->ob_type->tp_as_number != NULL &&
4314 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4315 PyObject *r;
4316 r = call_maybe(
4317 self, "__coerce__", &coerce_str, "(O)", other);
4318 if (r == NULL)
4319 return -1;
4320 if (r == Py_NotImplemented) {
4321 Py_DECREF(r);
4323 else {
4324 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4325 PyErr_SetString(PyExc_TypeError,
4326 "__coerce__ didn't return a 2-tuple");
4327 Py_DECREF(r);
4328 return -1;
4330 *a = PyTuple_GET_ITEM(r, 0);
4331 Py_INCREF(*a);
4332 *b = PyTuple_GET_ITEM(r, 1);
4333 Py_INCREF(*b);
4334 Py_DECREF(r);
4335 return 0;
4338 if (other->ob_type->tp_as_number != NULL &&
4339 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4340 PyObject *r;
4341 r = call_maybe(
4342 other, "__coerce__", &coerce_str, "(O)", self);
4343 if (r == NULL)
4344 return -1;
4345 if (r == Py_NotImplemented) {
4346 Py_DECREF(r);
4347 return 1;
4349 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4350 PyErr_SetString(PyExc_TypeError,
4351 "__coerce__ didn't return a 2-tuple");
4352 Py_DECREF(r);
4353 return -1;
4355 *a = PyTuple_GET_ITEM(r, 1);
4356 Py_INCREF(*a);
4357 *b = PyTuple_GET_ITEM(r, 0);
4358 Py_INCREF(*b);
4359 Py_DECREF(r);
4360 return 0;
4362 return 1;
4365 SLOT0(slot_nb_int, "__int__")
4366 SLOT0(slot_nb_long, "__long__")
4367 SLOT0(slot_nb_float, "__float__")
4368 SLOT0(slot_nb_oct, "__oct__")
4369 SLOT0(slot_nb_hex, "__hex__")
4370 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4371 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4372 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4373 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4374 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4375 SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
4376 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4377 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4378 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4379 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4380 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4381 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4382 "__floordiv__", "__rfloordiv__")
4383 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4384 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4385 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4387 static int
4388 half_compare(PyObject *self, PyObject *other)
4390 PyObject *func, *args, *res;
4391 static PyObject *cmp_str;
4392 int c;
4394 func = lookup_method(self, "__cmp__", &cmp_str);
4395 if (func == NULL) {
4396 PyErr_Clear();
4398 else {
4399 args = PyTuple_Pack(1, other);
4400 if (args == NULL)
4401 res = NULL;
4402 else {
4403 res = PyObject_Call(func, args, NULL);
4404 Py_DECREF(args);
4406 Py_DECREF(func);
4407 if (res != Py_NotImplemented) {
4408 if (res == NULL)
4409 return -2;
4410 c = PyInt_AsLong(res);
4411 Py_DECREF(res);
4412 if (c == -1 && PyErr_Occurred())
4413 return -2;
4414 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4416 Py_DECREF(res);
4418 return 2;
4421 /* This slot is published for the benefit of try_3way_compare in object.c */
4423 _PyObject_SlotCompare(PyObject *self, PyObject *other)
4425 int c;
4427 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4428 c = half_compare(self, other);
4429 if (c <= 1)
4430 return c;
4432 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4433 c = half_compare(other, self);
4434 if (c < -1)
4435 return -2;
4436 if (c <= 1)
4437 return -c;
4439 return (void *)self < (void *)other ? -1 :
4440 (void *)self > (void *)other ? 1 : 0;
4443 static PyObject *
4444 slot_tp_repr(PyObject *self)
4446 PyObject *func, *res;
4447 static PyObject *repr_str;
4449 func = lookup_method(self, "__repr__", &repr_str);
4450 if (func != NULL) {
4451 res = PyEval_CallObject(func, NULL);
4452 Py_DECREF(func);
4453 return res;
4455 PyErr_Clear();
4456 return PyString_FromFormat("<%s object at %p>",
4457 self->ob_type->tp_name, self);
4460 static PyObject *
4461 slot_tp_str(PyObject *self)
4463 PyObject *func, *res;
4464 static PyObject *str_str;
4466 func = lookup_method(self, "__str__", &str_str);
4467 if (func != NULL) {
4468 res = PyEval_CallObject(func, NULL);
4469 Py_DECREF(func);
4470 return res;
4472 else {
4473 PyErr_Clear();
4474 return slot_tp_repr(self);
4478 static long
4479 slot_tp_hash(PyObject *self)
4481 PyObject *func;
4482 static PyObject *hash_str, *eq_str, *cmp_str;
4483 long h;
4485 func = lookup_method(self, "__hash__", &hash_str);
4487 if (func != NULL) {
4488 PyObject *res = PyEval_CallObject(func, NULL);
4489 Py_DECREF(func);
4490 if (res == NULL)
4491 return -1;
4492 h = PyInt_AsLong(res);
4493 Py_DECREF(res);
4495 else {
4496 PyErr_Clear();
4497 func = lookup_method(self, "__eq__", &eq_str);
4498 if (func == NULL) {
4499 PyErr_Clear();
4500 func = lookup_method(self, "__cmp__", &cmp_str);
4502 if (func != NULL) {
4503 Py_DECREF(func);
4504 PyErr_SetString(PyExc_TypeError, "unhashable type");
4505 return -1;
4507 PyErr_Clear();
4508 h = _Py_HashPointer((void *)self);
4510 if (h == -1 && !PyErr_Occurred())
4511 h = -2;
4512 return h;
4515 static PyObject *
4516 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4518 static PyObject *call_str;
4519 PyObject *meth = lookup_method(self, "__call__", &call_str);
4520 PyObject *res;
4522 if (meth == NULL)
4523 return NULL;
4524 res = PyObject_Call(meth, args, kwds);
4525 Py_DECREF(meth);
4526 return res;
4529 /* There are two slot dispatch functions for tp_getattro.
4531 - slot_tp_getattro() is used when __getattribute__ is overridden
4532 but no __getattr__ hook is present;
4534 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4536 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4537 detects the absence of __getattr__ and then installs the simpler slot if
4538 necessary. */
4540 static PyObject *
4541 slot_tp_getattro(PyObject *self, PyObject *name)
4543 static PyObject *getattribute_str = NULL;
4544 return call_method(self, "__getattribute__", &getattribute_str,
4545 "(O)", name);
4548 static PyObject *
4549 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4551 PyTypeObject *tp = self->ob_type;
4552 PyObject *getattr, *getattribute, *res;
4553 static PyObject *getattribute_str = NULL;
4554 static PyObject *getattr_str = NULL;
4556 if (getattr_str == NULL) {
4557 getattr_str = PyString_InternFromString("__getattr__");
4558 if (getattr_str == NULL)
4559 return NULL;
4561 if (getattribute_str == NULL) {
4562 getattribute_str =
4563 PyString_InternFromString("__getattribute__");
4564 if (getattribute_str == NULL)
4565 return NULL;
4567 getattr = _PyType_Lookup(tp, getattr_str);
4568 if (getattr == NULL) {
4569 /* No __getattr__ hook: use a simpler dispatcher */
4570 tp->tp_getattro = slot_tp_getattro;
4571 return slot_tp_getattro(self, name);
4573 getattribute = _PyType_Lookup(tp, getattribute_str);
4574 if (getattribute == NULL ||
4575 (getattribute->ob_type == &PyWrapperDescr_Type &&
4576 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4577 (void *)PyObject_GenericGetAttr))
4578 res = PyObject_GenericGetAttr(self, name);
4579 else
4580 res = PyObject_CallFunction(getattribute, "OO", self, name);
4581 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4582 PyErr_Clear();
4583 res = PyObject_CallFunction(getattr, "OO", self, name);
4585 return res;
4588 static int
4589 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4591 PyObject *res;
4592 static PyObject *delattr_str, *setattr_str;
4594 if (value == NULL)
4595 res = call_method(self, "__delattr__", &delattr_str,
4596 "(O)", name);
4597 else
4598 res = call_method(self, "__setattr__", &setattr_str,
4599 "(OO)", name, value);
4600 if (res == NULL)
4601 return -1;
4602 Py_DECREF(res);
4603 return 0;
4606 /* Map rich comparison operators to their __xx__ namesakes */
4607 static char *name_op[] = {
4608 "__lt__",
4609 "__le__",
4610 "__eq__",
4611 "__ne__",
4612 "__gt__",
4613 "__ge__",
4616 static PyObject *
4617 half_richcompare(PyObject *self, PyObject *other, int op)
4619 PyObject *func, *args, *res;
4620 static PyObject *op_str[6];
4622 func = lookup_method(self, name_op[op], &op_str[op]);
4623 if (func == NULL) {
4624 PyErr_Clear();
4625 Py_INCREF(Py_NotImplemented);
4626 return Py_NotImplemented;
4628 args = PyTuple_Pack(1, other);
4629 if (args == NULL)
4630 res = NULL;
4631 else {
4632 res = PyObject_Call(func, args, NULL);
4633 Py_DECREF(args);
4635 Py_DECREF(func);
4636 return res;
4639 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
4640 static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
4642 static PyObject *
4643 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4645 PyObject *res;
4647 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4648 res = half_richcompare(self, other, op);
4649 if (res != Py_NotImplemented)
4650 return res;
4651 Py_DECREF(res);
4653 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4654 res = half_richcompare(other, self, swapped_op[op]);
4655 if (res != Py_NotImplemented) {
4656 return res;
4658 Py_DECREF(res);
4660 Py_INCREF(Py_NotImplemented);
4661 return Py_NotImplemented;
4664 static PyObject *
4665 slot_tp_iter(PyObject *self)
4667 PyObject *func, *res;
4668 static PyObject *iter_str, *getitem_str;
4670 func = lookup_method(self, "__iter__", &iter_str);
4671 if (func != NULL) {
4672 PyObject *args;
4673 args = res = PyTuple_New(0);
4674 if (args != NULL) {
4675 res = PyObject_Call(func, args, NULL);
4676 Py_DECREF(args);
4678 Py_DECREF(func);
4679 return res;
4681 PyErr_Clear();
4682 func = lookup_method(self, "__getitem__", &getitem_str);
4683 if (func == NULL) {
4684 PyErr_SetString(PyExc_TypeError,
4685 "iteration over non-sequence");
4686 return NULL;
4688 Py_DECREF(func);
4689 return PySeqIter_New(self);
4692 static PyObject *
4693 slot_tp_iternext(PyObject *self)
4695 static PyObject *next_str;
4696 return call_method(self, "next", &next_str, "()");
4699 static PyObject *
4700 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4702 PyTypeObject *tp = self->ob_type;
4703 PyObject *get;
4704 static PyObject *get_str = NULL;
4706 if (get_str == NULL) {
4707 get_str = PyString_InternFromString("__get__");
4708 if (get_str == NULL)
4709 return NULL;
4711 get = _PyType_Lookup(tp, get_str);
4712 if (get == NULL) {
4713 /* Avoid further slowdowns */
4714 if (tp->tp_descr_get == slot_tp_descr_get)
4715 tp->tp_descr_get = NULL;
4716 Py_INCREF(self);
4717 return self;
4719 if (obj == NULL)
4720 obj = Py_None;
4721 if (type == NULL)
4722 type = Py_None;
4723 return PyObject_CallFunction(get, "OOO", self, obj, type);
4726 static int
4727 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4729 PyObject *res;
4730 static PyObject *del_str, *set_str;
4732 if (value == NULL)
4733 res = call_method(self, "__delete__", &del_str,
4734 "(O)", target);
4735 else
4736 res = call_method(self, "__set__", &set_str,
4737 "(OO)", target, value);
4738 if (res == NULL)
4739 return -1;
4740 Py_DECREF(res);
4741 return 0;
4744 static int
4745 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4747 static PyObject *init_str;
4748 PyObject *meth = lookup_method(self, "__init__", &init_str);
4749 PyObject *res;
4751 if (meth == NULL)
4752 return -1;
4753 res = PyObject_Call(meth, args, kwds);
4754 Py_DECREF(meth);
4755 if (res == NULL)
4756 return -1;
4757 Py_DECREF(res);
4758 return 0;
4761 static PyObject *
4762 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4764 static PyObject *new_str;
4765 PyObject *func;
4766 PyObject *newargs, *x;
4767 int i, n;
4769 if (new_str == NULL) {
4770 new_str = PyString_InternFromString("__new__");
4771 if (new_str == NULL)
4772 return NULL;
4774 func = PyObject_GetAttr((PyObject *)type, new_str);
4775 if (func == NULL)
4776 return NULL;
4777 assert(PyTuple_Check(args));
4778 n = PyTuple_GET_SIZE(args);
4779 newargs = PyTuple_New(n+1);
4780 if (newargs == NULL)
4781 return NULL;
4782 Py_INCREF(type);
4783 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4784 for (i = 0; i < n; i++) {
4785 x = PyTuple_GET_ITEM(args, i);
4786 Py_INCREF(x);
4787 PyTuple_SET_ITEM(newargs, i+1, x);
4789 x = PyObject_Call(func, newargs, kwds);
4790 Py_DECREF(newargs);
4791 Py_DECREF(func);
4792 return x;
4795 static void
4796 slot_tp_del(PyObject *self)
4798 static PyObject *del_str = NULL;
4799 PyObject *del, *res;
4800 PyObject *error_type, *error_value, *error_traceback;
4802 /* Temporarily resurrect the object. */
4803 assert(self->ob_refcnt == 0);
4804 self->ob_refcnt = 1;
4806 /* Save the current exception, if any. */
4807 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4809 /* Execute __del__ method, if any. */
4810 del = lookup_maybe(self, "__del__", &del_str);
4811 if (del != NULL) {
4812 res = PyEval_CallObject(del, NULL);
4813 if (res == NULL)
4814 PyErr_WriteUnraisable(del);
4815 else
4816 Py_DECREF(res);
4817 Py_DECREF(del);
4820 /* Restore the saved exception. */
4821 PyErr_Restore(error_type, error_value, error_traceback);
4823 /* Undo the temporary resurrection; can't use DECREF here, it would
4824 * cause a recursive call.
4826 assert(self->ob_refcnt > 0);
4827 if (--self->ob_refcnt == 0)
4828 return; /* this is the normal path out */
4830 /* __del__ resurrected it! Make it look like the original Py_DECREF
4831 * never happened.
4834 int refcnt = self->ob_refcnt;
4835 _Py_NewReference(self);
4836 self->ob_refcnt = refcnt;
4838 assert(!PyType_IS_GC(self->ob_type) ||
4839 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4840 /* If Py_REF_DEBUG, the original decref dropped _Py_RefTotal, but
4841 * _Py_NewReference bumped it again, so that's a wash.
4842 * If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4843 * chain, so no more to do there either.
4844 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4845 * _Py_NewReference bumped tp_allocs: both of those need to be
4846 * undone.
4848 #ifdef COUNT_ALLOCS
4849 --self->ob_type->tp_frees;
4850 --self->ob_type->tp_allocs;
4851 #endif
4855 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4856 functions. The offsets here are relative to the 'PyHeapTypeObject'
4857 structure, which incorporates the additional structures used for numbers,
4858 sequences and mappings.
4859 Note that multiple names may map to the same slot (e.g. __eq__,
4860 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4861 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4862 terminated with an all-zero entry. (This table is further initialized and
4863 sorted in init_slotdefs() below.) */
4865 typedef struct wrapperbase slotdef;
4867 #undef TPSLOT
4868 #undef FLSLOT
4869 #undef ETSLOT
4870 #undef SQSLOT
4871 #undef MPSLOT
4872 #undef NBSLOT
4873 #undef UNSLOT
4874 #undef IBSLOT
4875 #undef BINSLOT
4876 #undef RBINSLOT
4878 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4879 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4880 PyDoc_STR(DOC)}
4881 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4882 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4883 PyDoc_STR(DOC), FLAGS}
4884 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4885 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4886 PyDoc_STR(DOC)}
4887 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4888 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4889 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4890 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4891 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4892 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4893 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4894 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4895 "x." NAME "() <==> " DOC)
4896 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4897 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4898 "x." NAME "(y) <==> x" DOC "y")
4899 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4900 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4901 "x." NAME "(y) <==> x" DOC "y")
4902 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4903 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4904 "x." NAME "(y) <==> y" DOC "x")
4906 static slotdef slotdefs[] = {
4907 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
4908 "x.__len__() <==> len(x)"),
4909 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
4910 "x.__add__(y) <==> x+y"),
4911 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
4912 "x.__mul__(n) <==> x*n"),
4913 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
4914 "x.__rmul__(n) <==> n*x"),
4915 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
4916 "x.__getitem__(y) <==> x[y]"),
4917 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
4918 "x.__getslice__(i, j) <==> x[i:j]\n\
4920 Use of negative indices is not supported."),
4921 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
4922 "x.__setitem__(i, y) <==> x[i]=y"),
4923 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
4924 "x.__delitem__(y) <==> del x[y]"),
4925 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
4926 wrap_intintobjargproc,
4927 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4929 Use of negative indices is not supported."),
4930 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
4931 "x.__delslice__(i, j) <==> del x[i:j]\n\
4933 Use of negative indices is not supported."),
4934 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
4935 "x.__contains__(y) <==> y in x"),
4936 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
4937 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
4938 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
4939 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
4941 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
4942 "x.__len__() <==> len(x)"),
4943 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
4944 wrap_binaryfunc,
4945 "x.__getitem__(y) <==> x[y]"),
4946 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
4947 wrap_objobjargproc,
4948 "x.__setitem__(i, y) <==> x[i]=y"),
4949 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
4950 wrap_delitem,
4951 "x.__delitem__(y) <==> del x[y]"),
4953 BINSLOT("__add__", nb_add, slot_nb_add,
4954 "+"),
4955 RBINSLOT("__radd__", nb_add, slot_nb_add,
4956 "+"),
4957 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
4958 "-"),
4959 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
4960 "-"),
4961 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
4962 "*"),
4963 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
4964 "*"),
4965 BINSLOT("__div__", nb_divide, slot_nb_divide,
4966 "/"),
4967 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
4968 "/"),
4969 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
4970 "%"),
4971 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
4972 "%"),
4973 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
4974 "divmod(x, y)"),
4975 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
4976 "divmod(y, x)"),
4977 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
4978 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
4979 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
4980 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
4981 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
4982 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
4983 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
4984 "abs(x)"),
4985 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
4986 "x != 0"),
4987 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
4988 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
4989 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
4990 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
4991 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
4992 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
4993 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
4994 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
4995 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
4996 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
4997 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
4998 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
4999 "x.__coerce__(y) <==> coerce(x, y)"),
5000 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5001 "int(x)"),
5002 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5003 "long(x)"),
5004 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5005 "float(x)"),
5006 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5007 "oct(x)"),
5008 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5009 "hex(x)"),
5010 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5011 wrap_binaryfunc, "+"),
5012 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5013 wrap_binaryfunc, "-"),
5014 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5015 wrap_binaryfunc, "*"),
5016 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5017 wrap_binaryfunc, "/"),
5018 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5019 wrap_binaryfunc, "%"),
5020 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5021 wrap_binaryfunc, "**"),
5022 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5023 wrap_binaryfunc, "<<"),
5024 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5025 wrap_binaryfunc, ">>"),
5026 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5027 wrap_binaryfunc, "&"),
5028 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5029 wrap_binaryfunc, "^"),
5030 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5031 wrap_binaryfunc, "|"),
5032 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5033 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5034 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5035 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5036 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5037 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5038 IBSLOT("__itruediv__", nb_inplace_true_divide,
5039 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5041 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5042 "x.__str__() <==> str(x)"),
5043 TPSLOT("__str__", tp_print, NULL, NULL, ""),
5044 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5045 "x.__repr__() <==> repr(x)"),
5046 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5047 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5048 "x.__cmp__(y) <==> cmp(x,y)"),
5049 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5050 "x.__hash__() <==> hash(x)"),
5051 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5052 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5053 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5054 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5055 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5056 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5057 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5058 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5059 "x.__setattr__('name', value) <==> x.name = value"),
5060 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5061 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5062 "x.__delattr__('name') <==> del x.name"),
5063 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5064 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5065 "x.__lt__(y) <==> x<y"),
5066 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5067 "x.__le__(y) <==> x<=y"),
5068 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5069 "x.__eq__(y) <==> x==y"),
5070 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5071 "x.__ne__(y) <==> x!=y"),
5072 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5073 "x.__gt__(y) <==> x>y"),
5074 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5075 "x.__ge__(y) <==> x>=y"),
5076 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5077 "x.__iter__() <==> iter(x)"),
5078 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5079 "x.next() -> the next value, or raise StopIteration"),
5080 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5081 "descr.__get__(obj[, type]) -> value"),
5082 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5083 "descr.__set__(obj, value)"),
5084 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5085 wrap_descr_delete, "descr.__delete__(obj)"),
5086 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5087 "x.__init__(...) initializes x; "
5088 "see x.__class__.__doc__ for signature",
5089 PyWrapperFlag_KEYWORDS),
5090 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5091 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5092 {NULL}
5095 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5096 pointer to the actual slot. This is not quite the same as simply adding
5097 the offset to the type pointer, since it takes care to indirect through the
5098 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5099 indirection pointer is NULL. */
5100 static void **
5101 slotptr(PyTypeObject *type, int offset)
5103 char *ptr;
5105 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5106 assert(offset >= 0);
5107 assert(offset < offsetof(PyHeapTypeObject, as_buffer));
5108 if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5109 ptr = (void *)type->tp_as_sequence;
5110 offset -= offsetof(PyHeapTypeObject, as_sequence);
5112 else if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5113 ptr = (void *)type->tp_as_mapping;
5114 offset -= offsetof(PyHeapTypeObject, as_mapping);
5116 else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
5117 ptr = (void *)type->tp_as_number;
5118 offset -= offsetof(PyHeapTypeObject, as_number);
5120 else {
5121 ptr = (void *)type;
5123 if (ptr != NULL)
5124 ptr += offset;
5125 return (void **)ptr;
5128 /* Length of array of slotdef pointers used to store slots with the
5129 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5130 the same __name__, for any __name__. Since that's a static property, it is
5131 appropriate to declare fixed-size arrays for this. */
5132 #define MAX_EQUIV 10
5134 /* Return a slot pointer for a given name, but ONLY if the attribute has
5135 exactly one slot function. The name must be an interned string. */
5136 static void **
5137 resolve_slotdups(PyTypeObject *type, PyObject *name)
5139 /* XXX Maybe this could be optimized more -- but is it worth it? */
5141 /* pname and ptrs act as a little cache */
5142 static PyObject *pname;
5143 static slotdef *ptrs[MAX_EQUIV];
5144 slotdef *p, **pp;
5145 void **res, **ptr;
5147 if (pname != name) {
5148 /* Collect all slotdefs that match name into ptrs. */
5149 pname = name;
5150 pp = ptrs;
5151 for (p = slotdefs; p->name_strobj; p++) {
5152 if (p->name_strobj == name)
5153 *pp++ = p;
5155 *pp = NULL;
5158 /* Look in all matching slots of the type; if exactly one of these has
5159 a filled-in slot, return its value. Otherwise return NULL. */
5160 res = NULL;
5161 for (pp = ptrs; *pp; pp++) {
5162 ptr = slotptr(type, (*pp)->offset);
5163 if (ptr == NULL || *ptr == NULL)
5164 continue;
5165 if (res != NULL)
5166 return NULL;
5167 res = ptr;
5169 return res;
5172 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5173 does some incredibly complex thinking and then sticks something into the
5174 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5175 interests, and then stores a generic wrapper or a specific function into
5176 the slot.) Return a pointer to the next slotdef with a different offset,
5177 because that's convenient for fixup_slot_dispatchers(). */
5178 static slotdef *
5179 update_one_slot(PyTypeObject *type, slotdef *p)
5181 PyObject *descr;
5182 PyWrapperDescrObject *d;
5183 void *generic = NULL, *specific = NULL;
5184 int use_generic = 0;
5185 int offset = p->offset;
5186 void **ptr = slotptr(type, offset);
5188 if (ptr == NULL) {
5189 do {
5190 ++p;
5191 } while (p->offset == offset);
5192 return p;
5194 do {
5195 descr = _PyType_Lookup(type, p->name_strobj);
5196 if (descr == NULL)
5197 continue;
5198 if (descr->ob_type == &PyWrapperDescr_Type) {
5199 void **tptr = resolve_slotdups(type, p->name_strobj);
5200 if (tptr == NULL || tptr == ptr)
5201 generic = p->function;
5202 d = (PyWrapperDescrObject *)descr;
5203 if (d->d_base->wrapper == p->wrapper &&
5204 PyType_IsSubtype(type, d->d_type))
5206 if (specific == NULL ||
5207 specific == d->d_wrapped)
5208 specific = d->d_wrapped;
5209 else
5210 use_generic = 1;
5213 else if (descr->ob_type == &PyCFunction_Type &&
5214 PyCFunction_GET_FUNCTION(descr) ==
5215 (PyCFunction)tp_new_wrapper &&
5216 strcmp(p->name, "__new__") == 0)
5218 /* The __new__ wrapper is not a wrapper descriptor,
5219 so must be special-cased differently.
5220 If we don't do this, creating an instance will
5221 always use slot_tp_new which will look up
5222 __new__ in the MRO which will call tp_new_wrapper
5223 which will look through the base classes looking
5224 for a static base and call its tp_new (usually
5225 PyType_GenericNew), after performing various
5226 sanity checks and constructing a new argument
5227 list. Cut all that nonsense short -- this speeds
5228 up instance creation tremendously. */
5229 specific = (void *)type->tp_new;
5230 /* XXX I'm not 100% sure that there isn't a hole
5231 in this reasoning that requires additional
5232 sanity checks. I'll buy the first person to
5233 point out a bug in this reasoning a beer. */
5235 else {
5236 use_generic = 1;
5237 generic = p->function;
5239 } while ((++p)->offset == offset);
5240 if (specific && !use_generic)
5241 *ptr = specific;
5242 else
5243 *ptr = generic;
5244 return p;
5247 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5248 This is a callback for update_subclasses(). */
5249 static int
5250 update_slots_callback(PyTypeObject *type, void *data)
5252 slotdef **pp = (slotdef **)data;
5254 for (; *pp; pp++)
5255 update_one_slot(type, *pp);
5256 return 0;
5259 /* Comparison function for qsort() to compare slotdefs by their offset, and
5260 for equal offset by their address (to force a stable sort). */
5261 static int
5262 slotdef_cmp(const void *aa, const void *bb)
5264 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5265 int c = a->offset - b->offset;
5266 if (c != 0)
5267 return c;
5268 else
5269 return a - b;
5272 /* Initialize the slotdefs table by adding interned string objects for the
5273 names and sorting the entries. */
5274 static void
5275 init_slotdefs(void)
5277 slotdef *p;
5278 static int initialized = 0;
5280 if (initialized)
5281 return;
5282 for (p = slotdefs; p->name; p++) {
5283 p->name_strobj = PyString_InternFromString(p->name);
5284 if (!p->name_strobj)
5285 Py_FatalError("Out of memory interning slotdef names");
5287 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5288 slotdef_cmp);
5289 initialized = 1;
5292 /* Update the slots after assignment to a class (type) attribute. */
5293 static int
5294 update_slot(PyTypeObject *type, PyObject *name)
5296 slotdef *ptrs[MAX_EQUIV];
5297 slotdef *p;
5298 slotdef **pp;
5299 int offset;
5301 init_slotdefs();
5302 pp = ptrs;
5303 for (p = slotdefs; p->name; p++) {
5304 /* XXX assume name is interned! */
5305 if (p->name_strobj == name)
5306 *pp++ = p;
5308 *pp = NULL;
5309 for (pp = ptrs; *pp; pp++) {
5310 p = *pp;
5311 offset = p->offset;
5312 while (p > slotdefs && (p-1)->offset == offset)
5313 --p;
5314 *pp = p;
5316 if (ptrs[0] == NULL)
5317 return 0; /* Not an attribute that affects any slots */
5318 return update_subclasses(type, name,
5319 update_slots_callback, (void *)ptrs);
5322 /* Store the proper functions in the slot dispatches at class (type)
5323 definition time, based upon which operations the class overrides in its
5324 dict. */
5325 static void
5326 fixup_slot_dispatchers(PyTypeObject *type)
5328 slotdef *p;
5330 init_slotdefs();
5331 for (p = slotdefs; p->name; )
5332 p = update_one_slot(type, p);
5335 static void
5336 update_all_slots(PyTypeObject* type)
5338 slotdef *p;
5340 init_slotdefs();
5341 for (p = slotdefs; p->name; p++) {
5342 /* update_slot returns int but can't actually fail */
5343 update_slot(type, p->name_strobj);
5347 /* recurse_down_subclasses() and update_subclasses() are mutually
5348 recursive functions to call a callback for all subclasses,
5349 but refraining from recursing into subclasses that define 'name'. */
5351 static int
5352 update_subclasses(PyTypeObject *type, PyObject *name,
5353 update_callback callback, void *data)
5355 if (callback(type, data) < 0)
5356 return -1;
5357 return recurse_down_subclasses(type, name, callback, data);
5360 static int
5361 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5362 update_callback callback, void *data)
5364 PyTypeObject *subclass;
5365 PyObject *ref, *subclasses, *dict;
5366 int i, n;
5368 subclasses = type->tp_subclasses;
5369 if (subclasses == NULL)
5370 return 0;
5371 assert(PyList_Check(subclasses));
5372 n = PyList_GET_SIZE(subclasses);
5373 for (i = 0; i < n; i++) {
5374 ref = PyList_GET_ITEM(subclasses, i);
5375 assert(PyWeakref_CheckRef(ref));
5376 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5377 assert(subclass != NULL);
5378 if ((PyObject *)subclass == Py_None)
5379 continue;
5380 assert(PyType_Check(subclass));
5381 /* Avoid recursing down into unaffected classes */
5382 dict = subclass->tp_dict;
5383 if (dict != NULL && PyDict_Check(dict) &&
5384 PyDict_GetItem(dict, name) != NULL)
5385 continue;
5386 if (update_subclasses(subclass, name, callback, data) < 0)
5387 return -1;
5389 return 0;
5392 /* This function is called by PyType_Ready() to populate the type's
5393 dictionary with method descriptors for function slots. For each
5394 function slot (like tp_repr) that's defined in the type, one or more
5395 corresponding descriptors are added in the type's tp_dict dictionary
5396 under the appropriate name (like __repr__). Some function slots
5397 cause more than one descriptor to be added (for example, the nb_add
5398 slot adds both __add__ and __radd__ descriptors) and some function
5399 slots compete for the same descriptor (for example both sq_item and
5400 mp_subscript generate a __getitem__ descriptor).
5402 In the latter case, the first slotdef entry encoutered wins. Since
5403 slotdef entries are sorted by the offset of the slot in the
5404 PyHeapTypeObject, this gives us some control over disambiguating
5405 between competing slots: the members of PyHeapTypeObject are listed
5406 from most general to least general, so the most general slot is
5407 preferred. In particular, because as_mapping comes before as_sequence,
5408 for a type that defines both mp_subscript and sq_item, mp_subscript
5409 wins.
5411 This only adds new descriptors and doesn't overwrite entries in
5412 tp_dict that were previously defined. The descriptors contain a
5413 reference to the C function they must call, so that it's safe if they
5414 are copied into a subtype's __dict__ and the subtype has a different
5415 C function in its slot -- calling the method defined by the
5416 descriptor will call the C function that was used to create it,
5417 rather than the C function present in the slot when it is called.
5418 (This is important because a subtype may have a C function in the
5419 slot that calls the method from the dictionary, and we want to avoid
5420 infinite recursion here.) */
5422 static int
5423 add_operators(PyTypeObject *type)
5425 PyObject *dict = type->tp_dict;
5426 slotdef *p;
5427 PyObject *descr;
5428 void **ptr;
5430 init_slotdefs();
5431 for (p = slotdefs; p->name; p++) {
5432 if (p->wrapper == NULL)
5433 continue;
5434 ptr = slotptr(type, p->offset);
5435 if (!ptr || !*ptr)
5436 continue;
5437 if (PyDict_GetItem(dict, p->name_strobj))
5438 continue;
5439 descr = PyDescr_NewWrapper(type, p, *ptr);
5440 if (descr == NULL)
5441 return -1;
5442 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5443 return -1;
5444 Py_DECREF(descr);
5446 if (type->tp_new != NULL) {
5447 if (add_tp_new_wrapper(type) < 0)
5448 return -1;
5450 return 0;
5454 /* Cooperative 'super' */
5456 typedef struct {
5457 PyObject_HEAD
5458 PyTypeObject *type;
5459 PyObject *obj;
5460 PyTypeObject *obj_type;
5461 } superobject;
5463 static PyMemberDef super_members[] = {
5464 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5465 "the class invoking super()"},
5466 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5467 "the instance invoking super(); may be None"},
5468 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5469 "the type of the instance invoking super(); may be None"},
5473 static void
5474 super_dealloc(PyObject *self)
5476 superobject *su = (superobject *)self;
5478 _PyObject_GC_UNTRACK(self);
5479 Py_XDECREF(su->obj);
5480 Py_XDECREF(su->type);
5481 Py_XDECREF(su->obj_type);
5482 self->ob_type->tp_free(self);
5485 static PyObject *
5486 super_repr(PyObject *self)
5488 superobject *su = (superobject *)self;
5490 if (su->obj_type)
5491 return PyString_FromFormat(
5492 "<super: <class '%s'>, <%s object>>",
5493 su->type ? su->type->tp_name : "NULL",
5494 su->obj_type->tp_name);
5495 else
5496 return PyString_FromFormat(
5497 "<super: <class '%s'>, NULL>",
5498 su->type ? su->type->tp_name : "NULL");
5501 static PyObject *
5502 super_getattro(PyObject *self, PyObject *name)
5504 superobject *su = (superobject *)self;
5505 int skip = su->obj_type == NULL;
5507 if (!skip) {
5508 /* We want __class__ to return the class of the super object
5509 (i.e. super, or a subclass), not the class of su->obj. */
5510 skip = (PyString_Check(name) &&
5511 PyString_GET_SIZE(name) == 9 &&
5512 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5515 if (!skip) {
5516 PyObject *mro, *res, *tmp, *dict;
5517 PyTypeObject *starttype;
5518 descrgetfunc f;
5519 int i, n;
5521 starttype = su->obj_type;
5522 mro = starttype->tp_mro;
5524 if (mro == NULL)
5525 n = 0;
5526 else {
5527 assert(PyTuple_Check(mro));
5528 n = PyTuple_GET_SIZE(mro);
5530 for (i = 0; i < n; i++) {
5531 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
5532 break;
5534 i++;
5535 res = NULL;
5536 for (; i < n; i++) {
5537 tmp = PyTuple_GET_ITEM(mro, i);
5538 if (PyType_Check(tmp))
5539 dict = ((PyTypeObject *)tmp)->tp_dict;
5540 else if (PyClass_Check(tmp))
5541 dict = ((PyClassObject *)tmp)->cl_dict;
5542 else
5543 continue;
5544 res = PyDict_GetItem(dict, name);
5545 if (res != NULL) {
5546 Py_INCREF(res);
5547 f = res->ob_type->tp_descr_get;
5548 if (f != NULL) {
5549 tmp = f(res,
5550 /* Only pass 'obj' param if
5551 this is instance-mode super
5552 (See SF ID #743627)
5554 (su->obj == (PyObject *)
5555 su->obj_type
5556 ? (PyObject *)NULL
5557 : su->obj),
5558 (PyObject *)starttype);
5559 Py_DECREF(res);
5560 res = tmp;
5562 return res;
5566 return PyObject_GenericGetAttr(self, name);
5569 static PyTypeObject *
5570 supercheck(PyTypeObject *type, PyObject *obj)
5572 /* Check that a super() call makes sense. Return a type object.
5574 obj can be a new-style class, or an instance of one:
5576 - If it is a class, it must be a subclass of 'type'. This case is
5577 used for class methods; the return value is obj.
5579 - If it is an instance, it must be an instance of 'type'. This is
5580 the normal case; the return value is obj.__class__.
5582 But... when obj is an instance, we want to allow for the case where
5583 obj->ob_type is not a subclass of type, but obj.__class__ is!
5584 This will allow using super() with a proxy for obj.
5587 /* Check for first bullet above (special case) */
5588 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5589 Py_INCREF(obj);
5590 return (PyTypeObject *)obj;
5593 /* Normal case */
5594 if (PyType_IsSubtype(obj->ob_type, type)) {
5595 Py_INCREF(obj->ob_type);
5596 return obj->ob_type;
5598 else {
5599 /* Try the slow way */
5600 static PyObject *class_str = NULL;
5601 PyObject *class_attr;
5603 if (class_str == NULL) {
5604 class_str = PyString_FromString("__class__");
5605 if (class_str == NULL)
5606 return NULL;
5609 class_attr = PyObject_GetAttr(obj, class_str);
5611 if (class_attr != NULL &&
5612 PyType_Check(class_attr) &&
5613 (PyTypeObject *)class_attr != obj->ob_type)
5615 int ok = PyType_IsSubtype(
5616 (PyTypeObject *)class_attr, type);
5617 if (ok)
5618 return (PyTypeObject *)class_attr;
5621 if (class_attr == NULL)
5622 PyErr_Clear();
5623 else
5624 Py_DECREF(class_attr);
5627 PyErr_SetString(PyExc_TypeError,
5628 "super(type, obj): "
5629 "obj must be an instance or subtype of type");
5630 return NULL;
5633 static PyObject *
5634 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5636 superobject *su = (superobject *)self;
5637 superobject *new;
5639 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5640 /* Not binding to an object, or already bound */
5641 Py_INCREF(self);
5642 return self;
5644 if (su->ob_type != &PySuper_Type)
5645 /* If su is not an instance of a subclass of super,
5646 call its type */
5647 return PyObject_CallFunction((PyObject *)su->ob_type,
5648 "OO", su->type, obj);
5649 else {
5650 /* Inline the common case */
5651 PyTypeObject *obj_type = supercheck(su->type, obj);
5652 if (obj_type == NULL)
5653 return NULL;
5654 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5655 NULL, NULL);
5656 if (new == NULL)
5657 return NULL;
5658 Py_INCREF(su->type);
5659 Py_INCREF(obj);
5660 new->type = su->type;
5661 new->obj = obj;
5662 new->obj_type = obj_type;
5663 return (PyObject *)new;
5667 static int
5668 super_init(PyObject *self, PyObject *args, PyObject *kwds)
5670 superobject *su = (superobject *)self;
5671 PyTypeObject *type;
5672 PyObject *obj = NULL;
5673 PyTypeObject *obj_type = NULL;
5675 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5676 return -1;
5677 if (obj == Py_None)
5678 obj = NULL;
5679 if (obj != NULL) {
5680 obj_type = supercheck(type, obj);
5681 if (obj_type == NULL)
5682 return -1;
5683 Py_INCREF(obj);
5685 Py_INCREF(type);
5686 su->type = type;
5687 su->obj = obj;
5688 su->obj_type = obj_type;
5689 return 0;
5692 PyDoc_STRVAR(super_doc,
5693 "super(type) -> unbound super object\n"
5694 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5695 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5696 "Typical use to call a cooperative superclass method:\n"
5697 "class C(B):\n"
5698 " def meth(self, arg):\n"
5699 " super(C, self).meth(arg)");
5701 static int
5702 super_traverse(PyObject *self, visitproc visit, void *arg)
5704 superobject *su = (superobject *)self;
5705 int err;
5707 #define VISIT(SLOT) \
5708 if (SLOT) { \
5709 err = visit((PyObject *)(SLOT), arg); \
5710 if (err) \
5711 return err; \
5714 VISIT(su->obj);
5715 VISIT(su->type);
5716 VISIT(su->obj_type);
5718 #undef VISIT
5720 return 0;
5723 PyTypeObject PySuper_Type = {
5724 PyObject_HEAD_INIT(&PyType_Type)
5725 0, /* ob_size */
5726 "super", /* tp_name */
5727 sizeof(superobject), /* tp_basicsize */
5728 0, /* tp_itemsize */
5729 /* methods */
5730 super_dealloc, /* tp_dealloc */
5731 0, /* tp_print */
5732 0, /* tp_getattr */
5733 0, /* tp_setattr */
5734 0, /* tp_compare */
5735 super_repr, /* tp_repr */
5736 0, /* tp_as_number */
5737 0, /* tp_as_sequence */
5738 0, /* tp_as_mapping */
5739 0, /* tp_hash */
5740 0, /* tp_call */
5741 0, /* tp_str */
5742 super_getattro, /* tp_getattro */
5743 0, /* tp_setattro */
5744 0, /* tp_as_buffer */
5745 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5746 Py_TPFLAGS_BASETYPE, /* tp_flags */
5747 super_doc, /* tp_doc */
5748 super_traverse, /* tp_traverse */
5749 0, /* tp_clear */
5750 0, /* tp_richcompare */
5751 0, /* tp_weaklistoffset */
5752 0, /* tp_iter */
5753 0, /* tp_iternext */
5754 0, /* tp_methods */
5755 super_members, /* tp_members */
5756 0, /* tp_getset */
5757 0, /* tp_base */
5758 0, /* tp_dict */
5759 super_descr_get, /* tp_descr_get */
5760 0, /* tp_descr_set */
5761 0, /* tp_dictoffset */
5762 super_init, /* tp_init */
5763 PyType_GenericAlloc, /* tp_alloc */
5764 PyType_GenericNew, /* tp_new */
5765 PyObject_GC_Del, /* tp_free */