Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Objects / typeobject.c
blob03f1adb1d17345c669c677e7de88fd05616f5ff5
1 /* Type object implementation */
3 #include "Python.h"
4 #include "structmember.h"
6 #include <ctype.h>
8 static PyMemberDef type_members[] = {
9 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
12 {"__weakrefoffset__", T_LONG,
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
21 static PyObject *
22 type_name(PyTypeObject *type, void *context)
24 const char *s;
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
29 Py_INCREF(et->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;
1291 int checkit = 0;
1293 if (type->ob_type == &PyType_Type) {
1294 result = mro_implementation(type);
1296 else {
1297 static PyObject *mro_str;
1298 checkit = 1;
1299 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1300 if (mro == NULL)
1301 return -1;
1302 result = PyObject_CallObject(mro, NULL);
1303 Py_DECREF(mro);
1305 if (result == NULL)
1306 return -1;
1307 tuple = PySequence_Tuple(result);
1308 Py_DECREF(result);
1309 if (tuple == NULL)
1310 return -1;
1311 if (checkit) {
1312 int i, len;
1313 PyObject *cls;
1314 PyTypeObject *solid;
1316 solid = solid_base(type);
1318 len = PyTuple_GET_SIZE(tuple);
1320 for (i = 0; i < len; i++) {
1321 PyTypeObject *t;
1322 cls = PyTuple_GET_ITEM(tuple, i);
1323 if (PyClass_Check(cls))
1324 continue;
1325 else if (!PyType_Check(cls)) {
1326 PyErr_Format(PyExc_TypeError,
1327 "mro() returned a non-class ('%.500s')",
1328 cls->ob_type->tp_name);
1329 Py_DECREF(tuple);
1330 return -1;
1332 t = (PyTypeObject*)cls;
1333 if (!PyType_IsSubtype(solid, solid_base(t))) {
1334 PyErr_Format(PyExc_TypeError,
1335 "mro() returned base with unsuitable layout ('%.500s')",
1336 t->tp_name);
1337 Py_DECREF(tuple);
1338 return -1;
1342 type->tp_mro = tuple;
1343 return 0;
1347 /* Calculate the best base amongst multiple base classes.
1348 This is the first one that's on the path to the "solid base". */
1350 static PyTypeObject *
1351 best_base(PyObject *bases)
1353 int i, n;
1354 PyTypeObject *base, *winner, *candidate, *base_i;
1355 PyObject *base_proto;
1357 assert(PyTuple_Check(bases));
1358 n = PyTuple_GET_SIZE(bases);
1359 assert(n > 0);
1360 base = NULL;
1361 winner = NULL;
1362 for (i = 0; i < n; i++) {
1363 base_proto = PyTuple_GET_ITEM(bases, i);
1364 if (PyClass_Check(base_proto))
1365 continue;
1366 if (!PyType_Check(base_proto)) {
1367 PyErr_SetString(
1368 PyExc_TypeError,
1369 "bases must be types");
1370 return NULL;
1372 base_i = (PyTypeObject *)base_proto;
1373 if (base_i->tp_dict == NULL) {
1374 if (PyType_Ready(base_i) < 0)
1375 return NULL;
1377 candidate = solid_base(base_i);
1378 if (winner == NULL) {
1379 winner = candidate;
1380 base = base_i;
1382 else if (PyType_IsSubtype(winner, candidate))
1384 else if (PyType_IsSubtype(candidate, winner)) {
1385 winner = candidate;
1386 base = base_i;
1388 else {
1389 PyErr_SetString(
1390 PyExc_TypeError,
1391 "multiple bases have "
1392 "instance lay-out conflict");
1393 return NULL;
1396 if (base == NULL)
1397 PyErr_SetString(PyExc_TypeError,
1398 "a new-style class can't have only classic bases");
1399 return base;
1402 static int
1403 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1405 size_t t_size = type->tp_basicsize;
1406 size_t b_size = base->tp_basicsize;
1408 assert(t_size >= b_size); /* Else type smaller than base! */
1409 if (type->tp_itemsize || base->tp_itemsize) {
1410 /* If itemsize is involved, stricter rules */
1411 return t_size != b_size ||
1412 type->tp_itemsize != base->tp_itemsize;
1414 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1415 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1416 t_size -= sizeof(PyObject *);
1417 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1418 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1419 t_size -= sizeof(PyObject *);
1421 return t_size != b_size;
1424 static PyTypeObject *
1425 solid_base(PyTypeObject *type)
1427 PyTypeObject *base;
1429 if (type->tp_base)
1430 base = solid_base(type->tp_base);
1431 else
1432 base = &PyBaseObject_Type;
1433 if (extra_ivars(type, base))
1434 return type;
1435 else
1436 return base;
1439 static void object_dealloc(PyObject *);
1440 static int object_init(PyObject *, PyObject *, PyObject *);
1441 static int update_slot(PyTypeObject *, PyObject *);
1442 static void fixup_slot_dispatchers(PyTypeObject *);
1444 static PyObject *
1445 subtype_dict(PyObject *obj, void *context)
1447 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1448 PyObject *dict;
1450 if (dictptr == NULL) {
1451 PyErr_SetString(PyExc_AttributeError,
1452 "This object has no __dict__");
1453 return NULL;
1455 dict = *dictptr;
1456 if (dict == NULL)
1457 *dictptr = dict = PyDict_New();
1458 Py_XINCREF(dict);
1459 return dict;
1462 static int
1463 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1465 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1466 PyObject *dict;
1468 if (dictptr == NULL) {
1469 PyErr_SetString(PyExc_AttributeError,
1470 "This object has no __dict__");
1471 return -1;
1473 if (value != NULL && !PyDict_Check(value)) {
1474 PyErr_SetString(PyExc_TypeError,
1475 "__dict__ must be set to a dictionary");
1476 return -1;
1478 dict = *dictptr;
1479 Py_XINCREF(value);
1480 *dictptr = value;
1481 Py_XDECREF(dict);
1482 return 0;
1485 static PyObject *
1486 subtype_getweakref(PyObject *obj, void *context)
1488 PyObject **weaklistptr;
1489 PyObject *result;
1491 if (obj->ob_type->tp_weaklistoffset == 0) {
1492 PyErr_SetString(PyExc_AttributeError,
1493 "This object has no __weaklist__");
1494 return NULL;
1496 assert(obj->ob_type->tp_weaklistoffset > 0);
1497 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1498 (size_t)(obj->ob_type->tp_basicsize));
1499 weaklistptr = (PyObject **)
1500 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1501 if (*weaklistptr == NULL)
1502 result = Py_None;
1503 else
1504 result = *weaklistptr;
1505 Py_INCREF(result);
1506 return result;
1509 /* Three variants on the subtype_getsets list. */
1511 static PyGetSetDef subtype_getsets_full[] = {
1512 {"__dict__", subtype_dict, subtype_setdict,
1513 PyDoc_STR("dictionary for instance variables (if defined)")},
1514 {"__weakref__", subtype_getweakref, NULL,
1515 PyDoc_STR("list of weak references to the object (if defined)")},
1519 static PyGetSetDef subtype_getsets_dict_only[] = {
1520 {"__dict__", subtype_dict, subtype_setdict,
1521 PyDoc_STR("dictionary for instance variables (if defined)")},
1525 static PyGetSetDef subtype_getsets_weakref_only[] = {
1526 {"__weakref__", subtype_getweakref, NULL,
1527 PyDoc_STR("list of weak references to the object (if defined)")},
1531 static int
1532 valid_identifier(PyObject *s)
1534 unsigned char *p;
1535 int i, n;
1537 if (!PyString_Check(s)) {
1538 PyErr_SetString(PyExc_TypeError,
1539 "__slots__ must be strings");
1540 return 0;
1542 p = (unsigned char *) PyString_AS_STRING(s);
1543 n = PyString_GET_SIZE(s);
1544 /* We must reject an empty name. As a hack, we bump the
1545 length to 1 so that the loop will balk on the trailing \0. */
1546 if (n == 0)
1547 n = 1;
1548 for (i = 0; i < n; i++, p++) {
1549 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1550 PyErr_SetString(PyExc_TypeError,
1551 "__slots__ must be identifiers");
1552 return 0;
1555 return 1;
1558 #ifdef Py_USING_UNICODE
1559 /* Replace Unicode objects in slots. */
1561 static PyObject *
1562 _unicode_to_string(PyObject *slots, int nslots)
1564 PyObject *tmp = slots;
1565 PyObject *o, *o1;
1566 int i;
1567 intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1568 for (i = 0; i < nslots; i++) {
1569 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1570 if (tmp == slots) {
1571 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1572 if (tmp == NULL)
1573 return NULL;
1575 o1 = _PyUnicode_AsDefaultEncodedString
1576 (o, NULL);
1577 if (o1 == NULL) {
1578 Py_DECREF(tmp);
1579 return 0;
1581 Py_INCREF(o1);
1582 Py_DECREF(o);
1583 PyTuple_SET_ITEM(tmp, i, o1);
1586 return tmp;
1588 #endif
1590 static PyObject *
1591 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1593 PyObject *name, *bases, *dict;
1594 static const char *kwlist[] = {"name", "bases", "dict", 0};
1595 PyObject *slots, *tmp, *newslots;
1596 PyTypeObject *type, *base, *tmptype, *winner;
1597 PyHeapTypeObject *et;
1598 PyMemberDef *mp;
1599 int i, nbases, nslots, slotoffset, add_dict, add_weak;
1600 int j, may_add_dict, may_add_weak;
1602 assert(args != NULL && PyTuple_Check(args));
1603 assert(kwds == NULL || PyDict_Check(kwds));
1605 /* Special case: type(x) should return x->ob_type */
1607 const int nargs = PyTuple_GET_SIZE(args);
1608 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1610 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1611 PyObject *x = PyTuple_GET_ITEM(args, 0);
1612 Py_INCREF(x->ob_type);
1613 return (PyObject *) x->ob_type;
1616 /* SF bug 475327 -- if that didn't trigger, we need 3
1617 arguments. but PyArg_ParseTupleAndKeywords below may give
1618 a msg saying type() needs exactly 3. */
1619 if (nargs + nkwds != 3) {
1620 PyErr_SetString(PyExc_TypeError,
1621 "type() takes 1 or 3 arguments");
1622 return NULL;
1626 /* Check arguments: (name, bases, dict) */
1627 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1628 &name,
1629 &PyTuple_Type, &bases,
1630 &PyDict_Type, &dict))
1631 return NULL;
1633 /* Determine the proper metatype to deal with this,
1634 and check for metatype conflicts while we're at it.
1635 Note that if some other metatype wins to contract,
1636 it's possible that its instances are not types. */
1637 nbases = PyTuple_GET_SIZE(bases);
1638 winner = metatype;
1639 for (i = 0; i < nbases; i++) {
1640 tmp = PyTuple_GET_ITEM(bases, i);
1641 tmptype = tmp->ob_type;
1642 if (tmptype == &PyClass_Type)
1643 continue; /* Special case classic classes */
1644 if (PyType_IsSubtype(winner, tmptype))
1645 continue;
1646 if (PyType_IsSubtype(tmptype, winner)) {
1647 winner = tmptype;
1648 continue;
1650 PyErr_SetString(PyExc_TypeError,
1651 "metaclass conflict: "
1652 "the metaclass of a derived class "
1653 "must be a (non-strict) subclass "
1654 "of the metaclasses of all its bases");
1655 return NULL;
1657 if (winner != metatype) {
1658 if (winner->tp_new != type_new) /* Pass it to the winner */
1659 return winner->tp_new(winner, args, kwds);
1660 metatype = winner;
1663 /* Adjust for empty tuple bases */
1664 if (nbases == 0) {
1665 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1666 if (bases == NULL)
1667 return NULL;
1668 nbases = 1;
1670 else
1671 Py_INCREF(bases);
1673 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1675 /* Calculate best base, and check that all bases are type objects */
1676 base = best_base(bases);
1677 if (base == NULL) {
1678 Py_DECREF(bases);
1679 return NULL;
1681 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1682 PyErr_Format(PyExc_TypeError,
1683 "type '%.100s' is not an acceptable base type",
1684 base->tp_name);
1685 Py_DECREF(bases);
1686 return NULL;
1689 /* Check for a __slots__ sequence variable in dict, and count it */
1690 slots = PyDict_GetItemString(dict, "__slots__");
1691 nslots = 0;
1692 add_dict = 0;
1693 add_weak = 0;
1694 may_add_dict = base->tp_dictoffset == 0;
1695 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1696 if (slots == NULL) {
1697 if (may_add_dict) {
1698 add_dict++;
1700 if (may_add_weak) {
1701 add_weak++;
1704 else {
1705 /* Have slots */
1707 /* Make it into a tuple */
1708 if (PyString_Check(slots))
1709 slots = PyTuple_Pack(1, slots);
1710 else
1711 slots = PySequence_Tuple(slots);
1712 if (slots == NULL) {
1713 Py_DECREF(bases);
1714 return NULL;
1716 assert(PyTuple_Check(slots));
1718 /* Are slots allowed? */
1719 nslots = PyTuple_GET_SIZE(slots);
1720 if (nslots > 0 && base->tp_itemsize != 0) {
1721 PyErr_Format(PyExc_TypeError,
1722 "nonempty __slots__ "
1723 "not supported for subtype of '%s'",
1724 base->tp_name);
1725 bad_slots:
1726 Py_DECREF(bases);
1727 Py_DECREF(slots);
1728 return NULL;
1731 #ifdef Py_USING_UNICODE
1732 tmp = _unicode_to_string(slots, nslots);
1733 if (tmp != slots) {
1734 Py_DECREF(slots);
1735 slots = tmp;
1737 if (!tmp)
1738 return NULL;
1739 #endif
1740 /* Check for valid slot names and two special cases */
1741 for (i = 0; i < nslots; i++) {
1742 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1743 char *s;
1744 if (!valid_identifier(tmp))
1745 goto bad_slots;
1746 assert(PyString_Check(tmp));
1747 s = PyString_AS_STRING(tmp);
1748 if (strcmp(s, "__dict__") == 0) {
1749 if (!may_add_dict || add_dict) {
1750 PyErr_SetString(PyExc_TypeError,
1751 "__dict__ slot disallowed: "
1752 "we already got one");
1753 goto bad_slots;
1755 add_dict++;
1757 if (strcmp(s, "__weakref__") == 0) {
1758 if (!may_add_weak || add_weak) {
1759 PyErr_SetString(PyExc_TypeError,
1760 "__weakref__ slot disallowed: "
1761 "either we already got one, "
1762 "or __itemsize__ != 0");
1763 goto bad_slots;
1765 add_weak++;
1769 /* Copy slots into yet another tuple, demangling names */
1770 newslots = PyTuple_New(nslots - add_dict - add_weak);
1771 if (newslots == NULL)
1772 goto bad_slots;
1773 for (i = j = 0; i < nslots; i++) {
1774 char *s;
1775 tmp = PyTuple_GET_ITEM(slots, i);
1776 s = PyString_AS_STRING(tmp);
1777 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1778 (add_weak && strcmp(s, "__weakref__") == 0))
1779 continue;
1780 tmp =_Py_Mangle(name, tmp);
1781 if (!tmp)
1782 goto bad_slots;
1783 PyTuple_SET_ITEM(newslots, j, tmp);
1784 j++;
1786 assert(j == nslots - add_dict - add_weak);
1787 nslots = j;
1788 Py_DECREF(slots);
1789 slots = newslots;
1791 /* Secondary bases may provide weakrefs or dict */
1792 if (nbases > 1 &&
1793 ((may_add_dict && !add_dict) ||
1794 (may_add_weak && !add_weak))) {
1795 for (i = 0; i < nbases; i++) {
1796 tmp = PyTuple_GET_ITEM(bases, i);
1797 if (tmp == (PyObject *)base)
1798 continue; /* Skip primary base */
1799 if (PyClass_Check(tmp)) {
1800 /* Classic base class provides both */
1801 if (may_add_dict && !add_dict)
1802 add_dict++;
1803 if (may_add_weak && !add_weak)
1804 add_weak++;
1805 break;
1807 assert(PyType_Check(tmp));
1808 tmptype = (PyTypeObject *)tmp;
1809 if (may_add_dict && !add_dict &&
1810 tmptype->tp_dictoffset != 0)
1811 add_dict++;
1812 if (may_add_weak && !add_weak &&
1813 tmptype->tp_weaklistoffset != 0)
1814 add_weak++;
1815 if (may_add_dict && !add_dict)
1816 continue;
1817 if (may_add_weak && !add_weak)
1818 continue;
1819 /* Nothing more to check */
1820 break;
1825 /* XXX From here until type is safely allocated,
1826 "return NULL" may leak slots! */
1828 /* Allocate the type object */
1829 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1830 if (type == NULL) {
1831 Py_XDECREF(slots);
1832 Py_DECREF(bases);
1833 return NULL;
1836 /* Keep name and slots alive in the extended type object */
1837 et = (PyHeapTypeObject *)type;
1838 Py_INCREF(name);
1839 et->name = name;
1840 et->slots = slots;
1842 /* Initialize tp_flags */
1843 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1844 Py_TPFLAGS_BASETYPE;
1845 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1846 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1848 /* It's a new-style number unless it specifically inherits any
1849 old-style numeric behavior */
1850 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1851 (base->tp_as_number == NULL))
1852 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1854 /* Initialize essential fields */
1855 type->tp_as_number = &et->as_number;
1856 type->tp_as_sequence = &et->as_sequence;
1857 type->tp_as_mapping = &et->as_mapping;
1858 type->tp_as_buffer = &et->as_buffer;
1859 type->tp_name = PyString_AS_STRING(name);
1861 /* Set tp_base and tp_bases */
1862 type->tp_bases = bases;
1863 Py_INCREF(base);
1864 type->tp_base = base;
1866 /* Initialize tp_dict from passed-in dict */
1867 type->tp_dict = dict = PyDict_Copy(dict);
1868 if (dict == NULL) {
1869 Py_DECREF(type);
1870 return NULL;
1873 /* Set __module__ in the dict */
1874 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1875 tmp = PyEval_GetGlobals();
1876 if (tmp != NULL) {
1877 tmp = PyDict_GetItemString(tmp, "__name__");
1878 if (tmp != NULL) {
1879 if (PyDict_SetItemString(dict, "__module__",
1880 tmp) < 0)
1881 return NULL;
1886 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1887 and is a string. The __doc__ accessor will first look for tp_doc;
1888 if that fails, it will still look into __dict__.
1891 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1892 if (doc != NULL && PyString_Check(doc)) {
1893 const size_t n = (size_t)PyString_GET_SIZE(doc);
1894 char *tp_doc = PyObject_MALLOC(n+1);
1895 if (tp_doc == NULL) {
1896 Py_DECREF(type);
1897 return NULL;
1899 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1900 type->tp_doc = tp_doc;
1904 /* Special-case __new__: if it's a plain function,
1905 make it a static function */
1906 tmp = PyDict_GetItemString(dict, "__new__");
1907 if (tmp != NULL && PyFunction_Check(tmp)) {
1908 tmp = PyStaticMethod_New(tmp);
1909 if (tmp == NULL) {
1910 Py_DECREF(type);
1911 return NULL;
1913 PyDict_SetItemString(dict, "__new__", tmp);
1914 Py_DECREF(tmp);
1917 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1918 mp = PyHeapType_GET_MEMBERS(et);
1919 slotoffset = base->tp_basicsize;
1920 if (slots != NULL) {
1921 for (i = 0; i < nslots; i++, mp++) {
1922 mp->name = PyString_AS_STRING(
1923 PyTuple_GET_ITEM(slots, i));
1924 mp->type = T_OBJECT_EX;
1925 mp->offset = slotoffset;
1926 if (base->tp_weaklistoffset == 0 &&
1927 strcmp(mp->name, "__weakref__") == 0) {
1928 add_weak++;
1929 mp->type = T_OBJECT;
1930 mp->flags = READONLY;
1931 type->tp_weaklistoffset = slotoffset;
1933 slotoffset += sizeof(PyObject *);
1936 if (add_dict) {
1937 if (base->tp_itemsize)
1938 type->tp_dictoffset = -(long)sizeof(PyObject *);
1939 else
1940 type->tp_dictoffset = slotoffset;
1941 slotoffset += sizeof(PyObject *);
1943 if (add_weak) {
1944 assert(!base->tp_itemsize);
1945 type->tp_weaklistoffset = slotoffset;
1946 slotoffset += sizeof(PyObject *);
1948 type->tp_basicsize = slotoffset;
1949 type->tp_itemsize = base->tp_itemsize;
1950 type->tp_members = PyHeapType_GET_MEMBERS(et);
1952 if (type->tp_weaklistoffset && type->tp_dictoffset)
1953 type->tp_getset = subtype_getsets_full;
1954 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1955 type->tp_getset = subtype_getsets_weakref_only;
1956 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1957 type->tp_getset = subtype_getsets_dict_only;
1958 else
1959 type->tp_getset = NULL;
1961 /* Special case some slots */
1962 if (type->tp_dictoffset != 0 || nslots > 0) {
1963 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1964 type->tp_getattro = PyObject_GenericGetAttr;
1965 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1966 type->tp_setattro = PyObject_GenericSetAttr;
1968 type->tp_dealloc = subtype_dealloc;
1970 /* Enable GC unless there are really no instance variables possible */
1971 if (!(type->tp_basicsize == sizeof(PyObject) &&
1972 type->tp_itemsize == 0))
1973 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1975 /* Always override allocation strategy to use regular heap */
1976 type->tp_alloc = PyType_GenericAlloc;
1977 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1978 type->tp_free = PyObject_GC_Del;
1979 type->tp_traverse = subtype_traverse;
1980 type->tp_clear = subtype_clear;
1982 else
1983 type->tp_free = PyObject_Del;
1985 /* Initialize the rest */
1986 if (PyType_Ready(type) < 0) {
1987 Py_DECREF(type);
1988 return NULL;
1991 /* Put the proper slots in place */
1992 fixup_slot_dispatchers(type);
1994 return (PyObject *)type;
1997 /* Internal API to look for a name through the MRO.
1998 This returns a borrowed reference, and doesn't set an exception! */
1999 PyObject *
2000 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2002 int i, n;
2003 PyObject *mro, *res, *base, *dict;
2005 /* Look in tp_dict of types in MRO */
2006 mro = type->tp_mro;
2008 /* If mro is NULL, the type is either not yet initialized
2009 by PyType_Ready(), or already cleared by type_clear().
2010 Either way the safest thing to do is to return NULL. */
2011 if (mro == NULL)
2012 return NULL;
2014 assert(PyTuple_Check(mro));
2015 n = PyTuple_GET_SIZE(mro);
2016 for (i = 0; i < n; i++) {
2017 base = PyTuple_GET_ITEM(mro, i);
2018 if (PyClass_Check(base))
2019 dict = ((PyClassObject *)base)->cl_dict;
2020 else {
2021 assert(PyType_Check(base));
2022 dict = ((PyTypeObject *)base)->tp_dict;
2024 assert(dict && PyDict_Check(dict));
2025 res = PyDict_GetItem(dict, name);
2026 if (res != NULL)
2027 return res;
2029 return NULL;
2032 /* This is similar to PyObject_GenericGetAttr(),
2033 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2034 static PyObject *
2035 type_getattro(PyTypeObject *type, PyObject *name)
2037 PyTypeObject *metatype = type->ob_type;
2038 PyObject *meta_attribute, *attribute;
2039 descrgetfunc meta_get;
2041 /* Initialize this type (we'll assume the metatype is initialized) */
2042 if (type->tp_dict == NULL) {
2043 if (PyType_Ready(type) < 0)
2044 return NULL;
2047 /* No readable descriptor found yet */
2048 meta_get = NULL;
2050 /* Look for the attribute in the metatype */
2051 meta_attribute = _PyType_Lookup(metatype, name);
2053 if (meta_attribute != NULL) {
2054 meta_get = meta_attribute->ob_type->tp_descr_get;
2056 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2057 /* Data descriptors implement tp_descr_set to intercept
2058 * writes. Assume the attribute is not overridden in
2059 * type's tp_dict (and bases): call the descriptor now.
2061 return meta_get(meta_attribute, (PyObject *)type,
2062 (PyObject *)metatype);
2064 Py_INCREF(meta_attribute);
2067 /* No data descriptor found on metatype. Look in tp_dict of this
2068 * type and its bases */
2069 attribute = _PyType_Lookup(type, name);
2070 if (attribute != NULL) {
2071 /* Implement descriptor functionality, if any */
2072 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2074 Py_XDECREF(meta_attribute);
2076 if (local_get != NULL) {
2077 /* NULL 2nd argument indicates the descriptor was
2078 * found on the target object itself (or a base) */
2079 return local_get(attribute, (PyObject *)NULL,
2080 (PyObject *)type);
2083 Py_INCREF(attribute);
2084 return attribute;
2087 /* No attribute found in local __dict__ (or bases): use the
2088 * descriptor from the metatype, if any */
2089 if (meta_get != NULL) {
2090 PyObject *res;
2091 res = meta_get(meta_attribute, (PyObject *)type,
2092 (PyObject *)metatype);
2093 Py_DECREF(meta_attribute);
2094 return res;
2097 /* If an ordinary attribute was found on the metatype, return it now */
2098 if (meta_attribute != NULL) {
2099 return meta_attribute;
2102 /* Give up */
2103 PyErr_Format(PyExc_AttributeError,
2104 "type object '%.50s' has no attribute '%.400s'",
2105 type->tp_name, PyString_AS_STRING(name));
2106 return NULL;
2109 static int
2110 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2112 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2113 PyErr_Format(
2114 PyExc_TypeError,
2115 "can't set attributes of built-in/extension type '%s'",
2116 type->tp_name);
2117 return -1;
2119 /* XXX Example of how I expect this to be used...
2120 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2121 return -1;
2123 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2124 return -1;
2125 return update_slot(type, name);
2128 static void
2129 type_dealloc(PyTypeObject *type)
2131 PyHeapTypeObject *et;
2133 /* Assert this is a heap-allocated type object */
2134 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2135 _PyObject_GC_UNTRACK(type);
2136 PyObject_ClearWeakRefs((PyObject *)type);
2137 et = (PyHeapTypeObject *)type;
2138 Py_XDECREF(type->tp_base);
2139 Py_XDECREF(type->tp_dict);
2140 Py_XDECREF(type->tp_bases);
2141 Py_XDECREF(type->tp_mro);
2142 Py_XDECREF(type->tp_cache);
2143 Py_XDECREF(type->tp_subclasses);
2144 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2145 * of most other objects. It's okay to cast it to char *.
2147 PyObject_Free((char *)type->tp_doc);
2148 Py_XDECREF(et->name);
2149 Py_XDECREF(et->slots);
2150 type->ob_type->tp_free((PyObject *)type);
2153 static PyObject *
2154 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2156 PyObject *list, *raw, *ref;
2157 int i, n;
2159 list = PyList_New(0);
2160 if (list == NULL)
2161 return NULL;
2162 raw = type->tp_subclasses;
2163 if (raw == NULL)
2164 return list;
2165 assert(PyList_Check(raw));
2166 n = PyList_GET_SIZE(raw);
2167 for (i = 0; i < n; i++) {
2168 ref = PyList_GET_ITEM(raw, i);
2169 assert(PyWeakref_CheckRef(ref));
2170 ref = PyWeakref_GET_OBJECT(ref);
2171 if (ref != Py_None) {
2172 if (PyList_Append(list, ref) < 0) {
2173 Py_DECREF(list);
2174 return NULL;
2178 return list;
2181 static PyMethodDef type_methods[] = {
2182 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2183 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2184 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2185 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2189 PyDoc_STRVAR(type_doc,
2190 "type(object) -> the object's type\n"
2191 "type(name, bases, dict) -> a new type");
2193 static int
2194 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2196 int err;
2198 /* Because of type_is_gc(), the collector only calls this
2199 for heaptypes. */
2200 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2202 #define VISIT(SLOT) \
2203 if (SLOT) { \
2204 err = visit((PyObject *)(SLOT), arg); \
2205 if (err) \
2206 return err; \
2209 VISIT(type->tp_dict);
2210 VISIT(type->tp_cache);
2211 VISIT(type->tp_mro);
2212 VISIT(type->tp_bases);
2213 VISIT(type->tp_base);
2215 /* There's no need to visit type->tp_subclasses or
2216 ((PyHeapTypeObject *)type)->slots, because they can't be involved
2217 in cycles; tp_subclasses is a list of weak references,
2218 and slots is a tuple of strings. */
2220 #undef VISIT
2222 return 0;
2225 static int
2226 type_clear(PyTypeObject *type)
2228 PyObject *tmp;
2230 /* Because of type_is_gc(), the collector only calls this
2231 for heaptypes. */
2232 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2234 #define CLEAR(SLOT) \
2235 if (SLOT) { \
2236 tmp = (PyObject *)(SLOT); \
2237 SLOT = NULL; \
2238 Py_DECREF(tmp); \
2241 /* The only field we need to clear is tp_mro, which is part of a
2242 hard cycle (its first element is the class itself) that won't
2243 be broken otherwise (it's a tuple and tuples don't have a
2244 tp_clear handler). None of the other fields need to be
2245 cleared, and here's why:
2247 tp_dict:
2248 It is a dict, so the collector will call its tp_clear.
2250 tp_cache:
2251 Not used; if it were, it would be a dict.
2253 tp_bases, tp_base:
2254 If these are involved in a cycle, there must be at least
2255 one other, mutable object in the cycle, e.g. a base
2256 class's dict; the cycle will be broken that way.
2258 tp_subclasses:
2259 A list of weak references can't be part of a cycle; and
2260 lists have their own tp_clear.
2262 slots (in PyHeapTypeObject):
2263 A tuple of strings can't be part of a cycle.
2266 CLEAR(type->tp_mro);
2268 #undef CLEAR
2270 return 0;
2273 static int
2274 type_is_gc(PyTypeObject *type)
2276 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2279 PyTypeObject PyType_Type = {
2280 PyObject_HEAD_INIT(&PyType_Type)
2281 0, /* ob_size */
2282 "type", /* tp_name */
2283 sizeof(PyHeapTypeObject), /* tp_basicsize */
2284 sizeof(PyMemberDef), /* tp_itemsize */
2285 (destructor)type_dealloc, /* tp_dealloc */
2286 0, /* tp_print */
2287 0, /* tp_getattr */
2288 0, /* tp_setattr */
2289 type_compare, /* tp_compare */
2290 (reprfunc)type_repr, /* tp_repr */
2291 0, /* tp_as_number */
2292 0, /* tp_as_sequence */
2293 0, /* tp_as_mapping */
2294 (hashfunc)_Py_HashPointer, /* tp_hash */
2295 (ternaryfunc)type_call, /* tp_call */
2296 0, /* tp_str */
2297 (getattrofunc)type_getattro, /* tp_getattro */
2298 (setattrofunc)type_setattro, /* tp_setattro */
2299 0, /* tp_as_buffer */
2300 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2301 Py_TPFLAGS_BASETYPE, /* tp_flags */
2302 type_doc, /* tp_doc */
2303 (traverseproc)type_traverse, /* tp_traverse */
2304 (inquiry)type_clear, /* tp_clear */
2305 0, /* tp_richcompare */
2306 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2307 0, /* tp_iter */
2308 0, /* tp_iternext */
2309 type_methods, /* tp_methods */
2310 type_members, /* tp_members */
2311 type_getsets, /* tp_getset */
2312 0, /* tp_base */
2313 0, /* tp_dict */
2314 0, /* tp_descr_get */
2315 0, /* tp_descr_set */
2316 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2317 0, /* tp_init */
2318 0, /* tp_alloc */
2319 type_new, /* tp_new */
2320 PyObject_GC_Del, /* tp_free */
2321 (inquiry)type_is_gc, /* tp_is_gc */
2325 /* The base type of all types (eventually)... except itself. */
2327 static int
2328 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2330 return 0;
2333 /* If we don't have a tp_new for a new-style class, new will use this one.
2334 Therefore this should take no arguments/keywords. However, this new may
2335 also be inherited by objects that define a tp_init but no tp_new. These
2336 objects WILL pass argumets to tp_new, because it gets the same args as
2337 tp_init. So only allow arguments if we aren't using the default init, in
2338 which case we expect init to handle argument parsing. */
2339 static PyObject *
2340 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2342 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2343 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2344 PyErr_SetString(PyExc_TypeError,
2345 "default __new__ takes no parameters");
2346 return NULL;
2348 return type->tp_alloc(type, 0);
2351 static void
2352 object_dealloc(PyObject *self)
2354 self->ob_type->tp_free(self);
2357 static PyObject *
2358 object_repr(PyObject *self)
2360 PyTypeObject *type;
2361 PyObject *mod, *name, *rtn;
2363 type = self->ob_type;
2364 mod = type_module(type, NULL);
2365 if (mod == NULL)
2366 PyErr_Clear();
2367 else if (!PyString_Check(mod)) {
2368 Py_DECREF(mod);
2369 mod = NULL;
2371 name = type_name(type, NULL);
2372 if (name == NULL)
2373 return NULL;
2374 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2375 rtn = PyString_FromFormat("<%s.%s object at %p>",
2376 PyString_AS_STRING(mod),
2377 PyString_AS_STRING(name),
2378 self);
2379 else
2380 rtn = PyString_FromFormat("<%s object at %p>",
2381 type->tp_name, self);
2382 Py_XDECREF(mod);
2383 Py_DECREF(name);
2384 return rtn;
2387 static PyObject *
2388 object_str(PyObject *self)
2390 unaryfunc f;
2392 f = self->ob_type->tp_repr;
2393 if (f == NULL)
2394 f = object_repr;
2395 return f(self);
2398 static long
2399 object_hash(PyObject *self)
2401 return _Py_HashPointer(self);
2404 static PyObject *
2405 object_get_class(PyObject *self, void *closure)
2407 Py_INCREF(self->ob_type);
2408 return (PyObject *)(self->ob_type);
2411 static int
2412 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2414 return a == b ||
2415 (a != NULL &&
2416 b != NULL &&
2417 a->tp_basicsize == b->tp_basicsize &&
2418 a->tp_itemsize == b->tp_itemsize &&
2419 a->tp_dictoffset == b->tp_dictoffset &&
2420 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2421 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2422 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2425 static int
2426 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2428 PyTypeObject *base = a->tp_base;
2429 int size;
2431 if (base != b->tp_base)
2432 return 0;
2433 if (equiv_structs(a, base) && equiv_structs(b, base))
2434 return 1;
2435 size = base->tp_basicsize;
2436 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2437 size += sizeof(PyObject *);
2438 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2439 size += sizeof(PyObject *);
2440 return size == a->tp_basicsize && size == b->tp_basicsize;
2443 static int
2444 compatible_for_assignment(PyTypeObject* old, PyTypeObject* new, char* attr)
2446 PyTypeObject *newbase, *oldbase;
2448 if (new->tp_dealloc != old->tp_dealloc ||
2449 new->tp_free != old->tp_free)
2451 PyErr_Format(PyExc_TypeError,
2452 "%s assignment: "
2453 "'%s' deallocator differs from '%s'",
2454 attr,
2455 new->tp_name,
2456 old->tp_name);
2457 return 0;
2459 newbase = new;
2460 oldbase = old;
2461 while (equiv_structs(newbase, newbase->tp_base))
2462 newbase = newbase->tp_base;
2463 while (equiv_structs(oldbase, oldbase->tp_base))
2464 oldbase = oldbase->tp_base;
2465 if (newbase != oldbase &&
2466 (newbase->tp_base != oldbase->tp_base ||
2467 !same_slots_added(newbase, oldbase))) {
2468 PyErr_Format(PyExc_TypeError,
2469 "%s assignment: "
2470 "'%s' object layout differs from '%s'",
2471 attr,
2472 new->tp_name,
2473 old->tp_name);
2474 return 0;
2477 return 1;
2480 static int
2481 object_set_class(PyObject *self, PyObject *value, void *closure)
2483 PyTypeObject *old = self->ob_type;
2484 PyTypeObject *new;
2486 if (value == NULL) {
2487 PyErr_SetString(PyExc_TypeError,
2488 "can't delete __class__ attribute");
2489 return -1;
2491 if (!PyType_Check(value)) {
2492 PyErr_Format(PyExc_TypeError,
2493 "__class__ must be set to new-style class, not '%s' object",
2494 value->ob_type->tp_name);
2495 return -1;
2497 new = (PyTypeObject *)value;
2498 if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2499 !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
2501 PyErr_Format(PyExc_TypeError,
2502 "__class__ assignment: only for heap types");
2503 return -1;
2505 if (compatible_for_assignment(new, old, "__class__")) {
2506 Py_INCREF(new);
2507 self->ob_type = new;
2508 Py_DECREF(old);
2509 return 0;
2511 else {
2512 return -1;
2516 static PyGetSetDef object_getsets[] = {
2517 {"__class__", object_get_class, object_set_class,
2518 PyDoc_STR("the object's class")},
2523 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2524 We fall back to helpers in copy_reg for:
2525 - pickle protocols < 2
2526 - calculating the list of slot names (done only once per class)
2527 - the __newobj__ function (which is used as a token but never called)
2530 static PyObject *
2531 import_copy_reg(void)
2533 static PyObject *copy_reg_str;
2535 if (!copy_reg_str) {
2536 copy_reg_str = PyString_InternFromString("copy_reg");
2537 if (copy_reg_str == NULL)
2538 return NULL;
2541 return PyImport_Import(copy_reg_str);
2544 static PyObject *
2545 slotnames(PyObject *cls)
2547 PyObject *clsdict;
2548 PyObject *copy_reg;
2549 PyObject *slotnames;
2551 if (!PyType_Check(cls)) {
2552 Py_INCREF(Py_None);
2553 return Py_None;
2556 clsdict = ((PyTypeObject *)cls)->tp_dict;
2557 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2558 if (slotnames != NULL && PyList_Check(slotnames)) {
2559 Py_INCREF(slotnames);
2560 return slotnames;
2563 copy_reg = import_copy_reg();
2564 if (copy_reg == NULL)
2565 return NULL;
2567 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2568 Py_DECREF(copy_reg);
2569 if (slotnames != NULL &&
2570 slotnames != Py_None &&
2571 !PyList_Check(slotnames))
2573 PyErr_SetString(PyExc_TypeError,
2574 "copy_reg._slotnames didn't return a list or None");
2575 Py_DECREF(slotnames);
2576 slotnames = NULL;
2579 return slotnames;
2582 static PyObject *
2583 reduce_2(PyObject *obj)
2585 PyObject *cls, *getnewargs;
2586 PyObject *args = NULL, *args2 = NULL;
2587 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2588 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2589 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2590 int i, n;
2592 cls = PyObject_GetAttrString(obj, "__class__");
2593 if (cls == NULL)
2594 return NULL;
2596 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2597 if (getnewargs != NULL) {
2598 args = PyObject_CallObject(getnewargs, NULL);
2599 Py_DECREF(getnewargs);
2600 if (args != NULL && !PyTuple_Check(args)) {
2601 PyErr_SetString(PyExc_TypeError,
2602 "__getnewargs__ should return a tuple");
2603 goto end;
2606 else {
2607 PyErr_Clear();
2608 args = PyTuple_New(0);
2610 if (args == NULL)
2611 goto end;
2613 getstate = PyObject_GetAttrString(obj, "__getstate__");
2614 if (getstate != NULL) {
2615 state = PyObject_CallObject(getstate, NULL);
2616 Py_DECREF(getstate);
2617 if (state == NULL)
2618 goto end;
2620 else {
2621 PyErr_Clear();
2622 state = PyObject_GetAttrString(obj, "__dict__");
2623 if (state == NULL) {
2624 PyErr_Clear();
2625 state = Py_None;
2626 Py_INCREF(state);
2628 names = slotnames(cls);
2629 if (names == NULL)
2630 goto end;
2631 if (names != Py_None) {
2632 assert(PyList_Check(names));
2633 slots = PyDict_New();
2634 if (slots == NULL)
2635 goto end;
2636 n = 0;
2637 /* Can't pre-compute the list size; the list
2638 is stored on the class so accessible to other
2639 threads, which may be run by DECREF */
2640 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2641 PyObject *name, *value;
2642 name = PyList_GET_ITEM(names, i);
2643 value = PyObject_GetAttr(obj, name);
2644 if (value == NULL)
2645 PyErr_Clear();
2646 else {
2647 int err = PyDict_SetItem(slots, name,
2648 value);
2649 Py_DECREF(value);
2650 if (err)
2651 goto end;
2652 n++;
2655 if (n) {
2656 state = Py_BuildValue("(NO)", state, slots);
2657 if (state == NULL)
2658 goto end;
2663 if (!PyList_Check(obj)) {
2664 listitems = Py_None;
2665 Py_INCREF(listitems);
2667 else {
2668 listitems = PyObject_GetIter(obj);
2669 if (listitems == NULL)
2670 goto end;
2673 if (!PyDict_Check(obj)) {
2674 dictitems = Py_None;
2675 Py_INCREF(dictitems);
2677 else {
2678 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2679 if (dictitems == NULL)
2680 goto end;
2683 copy_reg = import_copy_reg();
2684 if (copy_reg == NULL)
2685 goto end;
2686 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2687 if (newobj == NULL)
2688 goto end;
2690 n = PyTuple_GET_SIZE(args);
2691 args2 = PyTuple_New(n+1);
2692 if (args2 == NULL)
2693 goto end;
2694 PyTuple_SET_ITEM(args2, 0, cls);
2695 cls = NULL;
2696 for (i = 0; i < n; i++) {
2697 PyObject *v = PyTuple_GET_ITEM(args, i);
2698 Py_INCREF(v);
2699 PyTuple_SET_ITEM(args2, i+1, v);
2702 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2704 end:
2705 Py_XDECREF(cls);
2706 Py_XDECREF(args);
2707 Py_XDECREF(args2);
2708 Py_XDECREF(slots);
2709 Py_XDECREF(state);
2710 Py_XDECREF(names);
2711 Py_XDECREF(listitems);
2712 Py_XDECREF(dictitems);
2713 Py_XDECREF(copy_reg);
2714 Py_XDECREF(newobj);
2715 return res;
2718 static PyObject *
2719 object_reduce_ex(PyObject *self, PyObject *args)
2721 /* Call copy_reg._reduce_ex(self, proto) */
2722 PyObject *reduce, *copy_reg, *res;
2723 int proto = 0;
2725 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2726 return NULL;
2728 reduce = PyObject_GetAttrString(self, "__reduce__");
2729 if (reduce == NULL)
2730 PyErr_Clear();
2731 else {
2732 PyObject *cls, *clsreduce, *objreduce;
2733 int override;
2734 cls = PyObject_GetAttrString(self, "__class__");
2735 if (cls == NULL) {
2736 Py_DECREF(reduce);
2737 return NULL;
2739 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2740 Py_DECREF(cls);
2741 if (clsreduce == NULL) {
2742 Py_DECREF(reduce);
2743 return NULL;
2745 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2746 "__reduce__");
2747 override = (clsreduce != objreduce);
2748 Py_DECREF(clsreduce);
2749 if (override) {
2750 res = PyObject_CallObject(reduce, NULL);
2751 Py_DECREF(reduce);
2752 return res;
2754 else
2755 Py_DECREF(reduce);
2758 if (proto >= 2)
2759 return reduce_2(self);
2761 copy_reg = import_copy_reg();
2762 if (!copy_reg)
2763 return NULL;
2765 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2766 Py_DECREF(copy_reg);
2768 return res;
2771 static PyMethodDef object_methods[] = {
2772 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2773 PyDoc_STR("helper for pickle")},
2774 {"__reduce__", object_reduce_ex, METH_VARARGS,
2775 PyDoc_STR("helper for pickle")},
2780 PyTypeObject PyBaseObject_Type = {
2781 PyObject_HEAD_INIT(&PyType_Type)
2782 0, /* ob_size */
2783 "object", /* tp_name */
2784 sizeof(PyObject), /* tp_basicsize */
2785 0, /* tp_itemsize */
2786 (destructor)object_dealloc, /* tp_dealloc */
2787 0, /* tp_print */
2788 0, /* tp_getattr */
2789 0, /* tp_setattr */
2790 0, /* tp_compare */
2791 object_repr, /* tp_repr */
2792 0, /* tp_as_number */
2793 0, /* tp_as_sequence */
2794 0, /* tp_as_mapping */
2795 object_hash, /* tp_hash */
2796 0, /* tp_call */
2797 object_str, /* tp_str */
2798 PyObject_GenericGetAttr, /* tp_getattro */
2799 PyObject_GenericSetAttr, /* tp_setattro */
2800 0, /* tp_as_buffer */
2801 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2802 PyDoc_STR("The most base type"), /* tp_doc */
2803 0, /* tp_traverse */
2804 0, /* tp_clear */
2805 0, /* tp_richcompare */
2806 0, /* tp_weaklistoffset */
2807 0, /* tp_iter */
2808 0, /* tp_iternext */
2809 object_methods, /* tp_methods */
2810 0, /* tp_members */
2811 object_getsets, /* tp_getset */
2812 0, /* tp_base */
2813 0, /* tp_dict */
2814 0, /* tp_descr_get */
2815 0, /* tp_descr_set */
2816 0, /* tp_dictoffset */
2817 object_init, /* tp_init */
2818 PyType_GenericAlloc, /* tp_alloc */
2819 object_new, /* tp_new */
2820 PyObject_Del, /* tp_free */
2824 /* Initialize the __dict__ in a type object */
2826 static int
2827 add_methods(PyTypeObject *type, PyMethodDef *meth)
2829 PyObject *dict = type->tp_dict;
2831 for (; meth->ml_name != NULL; meth++) {
2832 PyObject *descr;
2833 if (PyDict_GetItemString(dict, meth->ml_name) &&
2834 !(meth->ml_flags & METH_COEXIST))
2835 continue;
2836 if (meth->ml_flags & METH_CLASS) {
2837 if (meth->ml_flags & METH_STATIC) {
2838 PyErr_SetString(PyExc_ValueError,
2839 "method cannot be both class and static");
2840 return -1;
2842 descr = PyDescr_NewClassMethod(type, meth);
2844 else if (meth->ml_flags & METH_STATIC) {
2845 PyObject *cfunc = PyCFunction_New(meth, NULL);
2846 if (cfunc == NULL)
2847 return -1;
2848 descr = PyStaticMethod_New(cfunc);
2849 Py_DECREF(cfunc);
2851 else {
2852 descr = PyDescr_NewMethod(type, meth);
2854 if (descr == NULL)
2855 return -1;
2856 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2857 return -1;
2858 Py_DECREF(descr);
2860 return 0;
2863 static int
2864 add_members(PyTypeObject *type, PyMemberDef *memb)
2866 PyObject *dict = type->tp_dict;
2868 for (; memb->name != NULL; memb++) {
2869 PyObject *descr;
2870 if (PyDict_GetItemString(dict, memb->name))
2871 continue;
2872 descr = PyDescr_NewMember(type, memb);
2873 if (descr == NULL)
2874 return -1;
2875 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2876 return -1;
2877 Py_DECREF(descr);
2879 return 0;
2882 static int
2883 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2885 PyObject *dict = type->tp_dict;
2887 for (; gsp->name != NULL; gsp++) {
2888 PyObject *descr;
2889 if (PyDict_GetItemString(dict, gsp->name))
2890 continue;
2891 descr = PyDescr_NewGetSet(type, gsp);
2893 if (descr == NULL)
2894 return -1;
2895 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2896 return -1;
2897 Py_DECREF(descr);
2899 return 0;
2902 static void
2903 inherit_special(PyTypeObject *type, PyTypeObject *base)
2905 int oldsize, newsize;
2907 /* Special flag magic */
2908 if (!type->tp_as_buffer && base->tp_as_buffer) {
2909 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2910 type->tp_flags |=
2911 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2913 if (!type->tp_as_sequence && base->tp_as_sequence) {
2914 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2915 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2917 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2918 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2919 if ((!type->tp_as_number && base->tp_as_number) ||
2920 (!type->tp_as_sequence && base->tp_as_sequence)) {
2921 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2922 if (!type->tp_as_number && !type->tp_as_sequence) {
2923 type->tp_flags |= base->tp_flags &
2924 Py_TPFLAGS_HAVE_INPLACEOPS;
2927 /* Wow */
2929 if (!type->tp_as_number && base->tp_as_number) {
2930 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2931 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2934 /* Copying basicsize is connected to the GC flags */
2935 oldsize = base->tp_basicsize;
2936 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2937 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2938 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2939 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2940 (!type->tp_traverse && !type->tp_clear)) {
2941 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2942 if (type->tp_traverse == NULL)
2943 type->tp_traverse = base->tp_traverse;
2944 if (type->tp_clear == NULL)
2945 type->tp_clear = base->tp_clear;
2947 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2948 /* The condition below could use some explanation.
2949 It appears that tp_new is not inherited for static types
2950 whose base class is 'object'; this seems to be a precaution
2951 so that old extension types don't suddenly become
2952 callable (object.__new__ wouldn't insure the invariants
2953 that the extension type's own factory function ensures).
2954 Heap types, of course, are under our control, so they do
2955 inherit tp_new; static extension types that specify some
2956 other built-in type as the default are considered
2957 new-style-aware so they also inherit object.__new__. */
2958 if (base != &PyBaseObject_Type ||
2959 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2960 if (type->tp_new == NULL)
2961 type->tp_new = base->tp_new;
2964 type->tp_basicsize = newsize;
2966 /* Copy other non-function slots */
2968 #undef COPYVAL
2969 #define COPYVAL(SLOT) \
2970 if (type->SLOT == 0) type->SLOT = base->SLOT
2972 COPYVAL(tp_itemsize);
2973 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2974 COPYVAL(tp_weaklistoffset);
2976 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2977 COPYVAL(tp_dictoffset);
2981 static void
2982 inherit_slots(PyTypeObject *type, PyTypeObject *base)
2984 PyTypeObject *basebase;
2986 #undef SLOTDEFINED
2987 #undef COPYSLOT
2988 #undef COPYNUM
2989 #undef COPYSEQ
2990 #undef COPYMAP
2991 #undef COPYBUF
2993 #define SLOTDEFINED(SLOT) \
2994 (base->SLOT != 0 && \
2995 (basebase == NULL || base->SLOT != basebase->SLOT))
2997 #define COPYSLOT(SLOT) \
2998 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3000 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3001 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3002 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3003 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3005 /* This won't inherit indirect slots (from tp_as_number etc.)
3006 if type doesn't provide the space. */
3008 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3009 basebase = base->tp_base;
3010 if (basebase->tp_as_number == NULL)
3011 basebase = NULL;
3012 COPYNUM(nb_add);
3013 COPYNUM(nb_subtract);
3014 COPYNUM(nb_multiply);
3015 COPYNUM(nb_divide);
3016 COPYNUM(nb_remainder);
3017 COPYNUM(nb_divmod);
3018 COPYNUM(nb_power);
3019 COPYNUM(nb_negative);
3020 COPYNUM(nb_positive);
3021 COPYNUM(nb_absolute);
3022 COPYNUM(nb_nonzero);
3023 COPYNUM(nb_invert);
3024 COPYNUM(nb_lshift);
3025 COPYNUM(nb_rshift);
3026 COPYNUM(nb_and);
3027 COPYNUM(nb_xor);
3028 COPYNUM(nb_or);
3029 COPYNUM(nb_coerce);
3030 COPYNUM(nb_int);
3031 COPYNUM(nb_long);
3032 COPYNUM(nb_float);
3033 COPYNUM(nb_oct);
3034 COPYNUM(nb_hex);
3035 COPYNUM(nb_inplace_add);
3036 COPYNUM(nb_inplace_subtract);
3037 COPYNUM(nb_inplace_multiply);
3038 COPYNUM(nb_inplace_divide);
3039 COPYNUM(nb_inplace_remainder);
3040 COPYNUM(nb_inplace_power);
3041 COPYNUM(nb_inplace_lshift);
3042 COPYNUM(nb_inplace_rshift);
3043 COPYNUM(nb_inplace_and);
3044 COPYNUM(nb_inplace_xor);
3045 COPYNUM(nb_inplace_or);
3046 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3047 COPYNUM(nb_true_divide);
3048 COPYNUM(nb_floor_divide);
3049 COPYNUM(nb_inplace_true_divide);
3050 COPYNUM(nb_inplace_floor_divide);
3054 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3055 basebase = base->tp_base;
3056 if (basebase->tp_as_sequence == NULL)
3057 basebase = NULL;
3058 COPYSEQ(sq_length);
3059 COPYSEQ(sq_concat);
3060 COPYSEQ(sq_repeat);
3061 COPYSEQ(sq_item);
3062 COPYSEQ(sq_slice);
3063 COPYSEQ(sq_ass_item);
3064 COPYSEQ(sq_ass_slice);
3065 COPYSEQ(sq_contains);
3066 COPYSEQ(sq_inplace_concat);
3067 COPYSEQ(sq_inplace_repeat);
3070 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3071 basebase = base->tp_base;
3072 if (basebase->tp_as_mapping == NULL)
3073 basebase = NULL;
3074 COPYMAP(mp_length);
3075 COPYMAP(mp_subscript);
3076 COPYMAP(mp_ass_subscript);
3079 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3080 basebase = base->tp_base;
3081 if (basebase->tp_as_buffer == NULL)
3082 basebase = NULL;
3083 COPYBUF(bf_getreadbuffer);
3084 COPYBUF(bf_getwritebuffer);
3085 COPYBUF(bf_getsegcount);
3086 COPYBUF(bf_getcharbuffer);
3089 basebase = base->tp_base;
3091 COPYSLOT(tp_dealloc);
3092 COPYSLOT(tp_print);
3093 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3094 type->tp_getattr = base->tp_getattr;
3095 type->tp_getattro = base->tp_getattro;
3097 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3098 type->tp_setattr = base->tp_setattr;
3099 type->tp_setattro = base->tp_setattro;
3101 /* tp_compare see tp_richcompare */
3102 COPYSLOT(tp_repr);
3103 /* tp_hash see tp_richcompare */
3104 COPYSLOT(tp_call);
3105 COPYSLOT(tp_str);
3106 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3107 if (type->tp_compare == NULL &&
3108 type->tp_richcompare == NULL &&
3109 type->tp_hash == NULL)
3111 type->tp_compare = base->tp_compare;
3112 type->tp_richcompare = base->tp_richcompare;
3113 type->tp_hash = base->tp_hash;
3116 else {
3117 COPYSLOT(tp_compare);
3119 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3120 COPYSLOT(tp_iter);
3121 COPYSLOT(tp_iternext);
3123 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3124 COPYSLOT(tp_descr_get);
3125 COPYSLOT(tp_descr_set);
3126 COPYSLOT(tp_dictoffset);
3127 COPYSLOT(tp_init);
3128 COPYSLOT(tp_alloc);
3129 COPYSLOT(tp_is_gc);
3130 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3131 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3132 /* They agree about gc. */
3133 COPYSLOT(tp_free);
3135 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3136 type->tp_free == NULL &&
3137 base->tp_free == _PyObject_Del) {
3138 /* A bit of magic to plug in the correct default
3139 * tp_free function when a derived class adds gc,
3140 * didn't define tp_free, and the base uses the
3141 * default non-gc tp_free.
3143 type->tp_free = PyObject_GC_Del;
3145 /* else they didn't agree about gc, and there isn't something
3146 * obvious to be done -- the type is on its own.
3151 static int add_operators(PyTypeObject *);
3154 PyType_Ready(PyTypeObject *type)
3156 PyObject *dict, *bases;
3157 PyTypeObject *base;
3158 int i, n;
3160 if (type->tp_flags & Py_TPFLAGS_READY) {
3161 assert(type->tp_dict != NULL);
3162 return 0;
3164 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3166 type->tp_flags |= Py_TPFLAGS_READYING;
3168 #ifdef Py_TRACE_REFS
3169 /* PyType_Ready is the closest thing we have to a choke point
3170 * for type objects, so is the best place I can think of to try
3171 * to get type objects into the doubly-linked list of all objects.
3172 * Still, not all type objects go thru PyType_Ready.
3174 _Py_AddToAllObjects((PyObject *)type, 0);
3175 #endif
3177 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3178 base = type->tp_base;
3179 if (base == NULL && type != &PyBaseObject_Type) {
3180 base = type->tp_base = &PyBaseObject_Type;
3181 Py_INCREF(base);
3184 /* Initialize the base class */
3185 if (base && base->tp_dict == NULL) {
3186 if (PyType_Ready(base) < 0)
3187 goto error;
3190 /* Initialize ob_type if NULL. This means extensions that want to be
3191 compilable separately on Windows can call PyType_Ready() instead of
3192 initializing the ob_type field of their type objects. */
3193 if (type->ob_type == NULL)
3194 type->ob_type = base->ob_type;
3196 /* Initialize tp_bases */
3197 bases = type->tp_bases;
3198 if (bases == NULL) {
3199 if (base == NULL)
3200 bases = PyTuple_New(0);
3201 else
3202 bases = PyTuple_Pack(1, base);
3203 if (bases == NULL)
3204 goto error;
3205 type->tp_bases = bases;
3208 /* Initialize tp_dict */
3209 dict = type->tp_dict;
3210 if (dict == NULL) {
3211 dict = PyDict_New();
3212 if (dict == NULL)
3213 goto error;
3214 type->tp_dict = dict;
3217 /* Add type-specific descriptors to tp_dict */
3218 if (add_operators(type) < 0)
3219 goto error;
3220 if (type->tp_methods != NULL) {
3221 if (add_methods(type, type->tp_methods) < 0)
3222 goto error;
3224 if (type->tp_members != NULL) {
3225 if (add_members(type, type->tp_members) < 0)
3226 goto error;
3228 if (type->tp_getset != NULL) {
3229 if (add_getset(type, type->tp_getset) < 0)
3230 goto error;
3233 /* Calculate method resolution order */
3234 if (mro_internal(type) < 0) {
3235 goto error;
3238 /* Inherit special flags from dominant base */
3239 if (type->tp_base != NULL)
3240 inherit_special(type, type->tp_base);
3242 /* Initialize tp_dict properly */
3243 bases = type->tp_mro;
3244 assert(bases != NULL);
3245 assert(PyTuple_Check(bases));
3246 n = PyTuple_GET_SIZE(bases);
3247 for (i = 1; i < n; i++) {
3248 PyObject *b = PyTuple_GET_ITEM(bases, i);
3249 if (PyType_Check(b))
3250 inherit_slots(type, (PyTypeObject *)b);
3253 /* Sanity check for tp_free. */
3254 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3255 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3256 /* This base class needs to call tp_free, but doesn't have
3257 * one, or its tp_free is for non-gc'ed objects.
3259 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3260 "gc and is a base type but has inappropriate "
3261 "tp_free slot",
3262 type->tp_name);
3263 goto error;
3266 /* if the type dictionary doesn't contain a __doc__, set it from
3267 the tp_doc slot.
3269 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3270 if (type->tp_doc != NULL) {
3271 PyObject *doc = PyString_FromString(type->tp_doc);
3272 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3273 Py_DECREF(doc);
3274 } else {
3275 PyDict_SetItemString(type->tp_dict,
3276 "__doc__", Py_None);
3280 /* Some more special stuff */
3281 base = type->tp_base;
3282 if (base != NULL) {
3283 if (type->tp_as_number == NULL)
3284 type->tp_as_number = base->tp_as_number;
3285 if (type->tp_as_sequence == NULL)
3286 type->tp_as_sequence = base->tp_as_sequence;
3287 if (type->tp_as_mapping == NULL)
3288 type->tp_as_mapping = base->tp_as_mapping;
3289 if (type->tp_as_buffer == NULL)
3290 type->tp_as_buffer = base->tp_as_buffer;
3293 /* Link into each base class's list of subclasses */
3294 bases = type->tp_bases;
3295 n = PyTuple_GET_SIZE(bases);
3296 for (i = 0; i < n; i++) {
3297 PyObject *b = PyTuple_GET_ITEM(bases, i);
3298 if (PyType_Check(b) &&
3299 add_subclass((PyTypeObject *)b, type) < 0)
3300 goto error;
3303 /* All done -- set the ready flag */
3304 assert(type->tp_dict != NULL);
3305 type->tp_flags =
3306 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3307 return 0;
3309 error:
3310 type->tp_flags &= ~Py_TPFLAGS_READYING;
3311 return -1;
3314 static int
3315 add_subclass(PyTypeObject *base, PyTypeObject *type)
3317 int i;
3318 PyObject *list, *ref, *new;
3320 list = base->tp_subclasses;
3321 if (list == NULL) {
3322 base->tp_subclasses = list = PyList_New(0);
3323 if (list == NULL)
3324 return -1;
3326 assert(PyList_Check(list));
3327 new = PyWeakref_NewRef((PyObject *)type, NULL);
3328 i = PyList_GET_SIZE(list);
3329 while (--i >= 0) {
3330 ref = PyList_GET_ITEM(list, i);
3331 assert(PyWeakref_CheckRef(ref));
3332 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3333 return PyList_SetItem(list, i, new);
3335 i = PyList_Append(list, new);
3336 Py_DECREF(new);
3337 return i;
3340 static void
3341 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3343 int i;
3344 PyObject *list, *ref;
3346 list = base->tp_subclasses;
3347 if (list == NULL) {
3348 return;
3350 assert(PyList_Check(list));
3351 i = PyList_GET_SIZE(list);
3352 while (--i >= 0) {
3353 ref = PyList_GET_ITEM(list, i);
3354 assert(PyWeakref_CheckRef(ref));
3355 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3356 /* this can't fail, right? */
3357 PySequence_DelItem(list, i);
3358 return;
3363 static int
3364 check_num_args(PyObject *ob, int n)
3366 if (!PyTuple_CheckExact(ob)) {
3367 PyErr_SetString(PyExc_SystemError,
3368 "PyArg_UnpackTuple() argument list is not a tuple");
3369 return 0;
3371 if (n == PyTuple_GET_SIZE(ob))
3372 return 1;
3373 PyErr_Format(
3374 PyExc_TypeError,
3375 "expected %d arguments, got %d", n, PyTuple_GET_SIZE(ob));
3376 return 0;
3379 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3381 /* There's a wrapper *function* for each distinct function typedef used
3382 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3383 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3384 Most tables have only one entry; the tables for binary operators have two
3385 entries, one regular and one with reversed arguments. */
3387 static PyObject *
3388 wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
3390 inquiry func = (inquiry)wrapped;
3391 int res;
3393 if (!check_num_args(args, 0))
3394 return NULL;
3395 res = (*func)(self);
3396 if (res == -1 && PyErr_Occurred())
3397 return NULL;
3398 return PyInt_FromLong((long)res);
3401 static PyObject *
3402 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3404 inquiry func = (inquiry)wrapped;
3405 int res;
3407 if (!check_num_args(args, 0))
3408 return NULL;
3409 res = (*func)(self);
3410 if (res == -1 && PyErr_Occurred())
3411 return NULL;
3412 return PyBool_FromLong((long)res);
3415 static PyObject *
3416 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3418 binaryfunc func = (binaryfunc)wrapped;
3419 PyObject *other;
3421 if (!check_num_args(args, 1))
3422 return NULL;
3423 other = PyTuple_GET_ITEM(args, 0);
3424 return (*func)(self, other);
3427 static PyObject *
3428 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3430 binaryfunc func = (binaryfunc)wrapped;
3431 PyObject *other;
3433 if (!check_num_args(args, 1))
3434 return NULL;
3435 other = PyTuple_GET_ITEM(args, 0);
3436 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3437 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3438 Py_INCREF(Py_NotImplemented);
3439 return Py_NotImplemented;
3441 return (*func)(self, other);
3444 static PyObject *
3445 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3447 binaryfunc func = (binaryfunc)wrapped;
3448 PyObject *other;
3450 if (!check_num_args(args, 1))
3451 return NULL;
3452 other = PyTuple_GET_ITEM(args, 0);
3453 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3454 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3455 Py_INCREF(Py_NotImplemented);
3456 return Py_NotImplemented;
3458 return (*func)(other, self);
3461 static PyObject *
3462 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3464 coercion func = (coercion)wrapped;
3465 PyObject *other, *res;
3466 int ok;
3468 if (!check_num_args(args, 1))
3469 return NULL;
3470 other = PyTuple_GET_ITEM(args, 0);
3471 ok = func(&self, &other);
3472 if (ok < 0)
3473 return NULL;
3474 if (ok > 0) {
3475 Py_INCREF(Py_NotImplemented);
3476 return Py_NotImplemented;
3478 res = PyTuple_New(2);
3479 if (res == NULL) {
3480 Py_DECREF(self);
3481 Py_DECREF(other);
3482 return NULL;
3484 PyTuple_SET_ITEM(res, 0, self);
3485 PyTuple_SET_ITEM(res, 1, other);
3486 return res;
3489 static PyObject *
3490 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3492 ternaryfunc func = (ternaryfunc)wrapped;
3493 PyObject *other;
3494 PyObject *third = Py_None;
3496 /* Note: This wrapper only works for __pow__() */
3498 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3499 return NULL;
3500 return (*func)(self, other, third);
3503 static PyObject *
3504 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3506 ternaryfunc func = (ternaryfunc)wrapped;
3507 PyObject *other;
3508 PyObject *third = Py_None;
3510 /* Note: This wrapper only works for __pow__() */
3512 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3513 return NULL;
3514 return (*func)(other, self, third);
3517 static PyObject *
3518 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3520 unaryfunc func = (unaryfunc)wrapped;
3522 if (!check_num_args(args, 0))
3523 return NULL;
3524 return (*func)(self);
3527 static PyObject *
3528 wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
3530 intargfunc func = (intargfunc)wrapped;
3531 int i;
3533 if (!PyArg_ParseTuple(args, "i", &i))
3534 return NULL;
3535 return (*func)(self, i);
3538 static int
3539 getindex(PyObject *self, PyObject *arg)
3541 int i;
3543 i = PyInt_AsLong(arg);
3544 if (i == -1 && PyErr_Occurred())
3545 return -1;
3546 if (i < 0) {
3547 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3548 if (sq && sq->sq_length) {
3549 int n = (*sq->sq_length)(self);
3550 if (n < 0)
3551 return -1;
3552 i += n;
3555 return i;
3558 static PyObject *
3559 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3561 intargfunc func = (intargfunc)wrapped;
3562 PyObject *arg;
3563 int i;
3565 if (PyTuple_GET_SIZE(args) == 1) {
3566 arg = PyTuple_GET_ITEM(args, 0);
3567 i = getindex(self, arg);
3568 if (i == -1 && PyErr_Occurred())
3569 return NULL;
3570 return (*func)(self, i);
3572 check_num_args(args, 1);
3573 assert(PyErr_Occurred());
3574 return NULL;
3577 static PyObject *
3578 wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
3580 intintargfunc func = (intintargfunc)wrapped;
3581 int i, j;
3583 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3584 return NULL;
3585 return (*func)(self, i, j);
3588 static PyObject *
3589 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3591 intobjargproc func = (intobjargproc)wrapped;
3592 int i, res;
3593 PyObject *arg, *value;
3595 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3596 return NULL;
3597 i = getindex(self, arg);
3598 if (i == -1 && PyErr_Occurred())
3599 return NULL;
3600 res = (*func)(self, i, value);
3601 if (res == -1 && PyErr_Occurred())
3602 return NULL;
3603 Py_INCREF(Py_None);
3604 return Py_None;
3607 static PyObject *
3608 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3610 intobjargproc func = (intobjargproc)wrapped;
3611 int i, res;
3612 PyObject *arg;
3614 if (!check_num_args(args, 1))
3615 return NULL;
3616 arg = PyTuple_GET_ITEM(args, 0);
3617 i = getindex(self, arg);
3618 if (i == -1 && PyErr_Occurred())
3619 return NULL;
3620 res = (*func)(self, i, NULL);
3621 if (res == -1 && PyErr_Occurred())
3622 return NULL;
3623 Py_INCREF(Py_None);
3624 return Py_None;
3627 static PyObject *
3628 wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
3630 intintobjargproc func = (intintobjargproc)wrapped;
3631 int i, j, res;
3632 PyObject *value;
3634 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
3635 return NULL;
3636 res = (*func)(self, i, j, value);
3637 if (res == -1 && PyErr_Occurred())
3638 return NULL;
3639 Py_INCREF(Py_None);
3640 return Py_None;
3643 static PyObject *
3644 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3646 intintobjargproc func = (intintobjargproc)wrapped;
3647 int i, j, res;
3649 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3650 return NULL;
3651 res = (*func)(self, i, j, NULL);
3652 if (res == -1 && PyErr_Occurred())
3653 return NULL;
3654 Py_INCREF(Py_None);
3655 return Py_None;
3658 /* XXX objobjproc is a misnomer; should be objargpred */
3659 static PyObject *
3660 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3662 objobjproc func = (objobjproc)wrapped;
3663 int res;
3664 PyObject *value;
3666 if (!check_num_args(args, 1))
3667 return NULL;
3668 value = PyTuple_GET_ITEM(args, 0);
3669 res = (*func)(self, value);
3670 if (res == -1 && PyErr_Occurred())
3671 return NULL;
3672 else
3673 return PyBool_FromLong(res);
3676 static PyObject *
3677 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3679 objobjargproc func = (objobjargproc)wrapped;
3680 int res;
3681 PyObject *key, *value;
3683 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3684 return NULL;
3685 res = (*func)(self, key, value);
3686 if (res == -1 && PyErr_Occurred())
3687 return NULL;
3688 Py_INCREF(Py_None);
3689 return Py_None;
3692 static PyObject *
3693 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3695 objobjargproc func = (objobjargproc)wrapped;
3696 int res;
3697 PyObject *key;
3699 if (!check_num_args(args, 1))
3700 return NULL;
3701 key = PyTuple_GET_ITEM(args, 0);
3702 res = (*func)(self, key, NULL);
3703 if (res == -1 && PyErr_Occurred())
3704 return NULL;
3705 Py_INCREF(Py_None);
3706 return Py_None;
3709 static PyObject *
3710 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3712 cmpfunc func = (cmpfunc)wrapped;
3713 int res;
3714 PyObject *other;
3716 if (!check_num_args(args, 1))
3717 return NULL;
3718 other = PyTuple_GET_ITEM(args, 0);
3719 if (other->ob_type->tp_compare != func &&
3720 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3721 PyErr_Format(
3722 PyExc_TypeError,
3723 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3724 self->ob_type->tp_name,
3725 self->ob_type->tp_name,
3726 other->ob_type->tp_name);
3727 return NULL;
3729 res = (*func)(self, other);
3730 if (PyErr_Occurred())
3731 return NULL;
3732 return PyInt_FromLong((long)res);
3735 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3736 This is called the Carlo Verre hack after its discoverer. */
3737 static int
3738 hackcheck(PyObject *self, setattrofunc func, char *what)
3740 PyTypeObject *type = self->ob_type;
3741 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3742 type = type->tp_base;
3743 if (type->tp_setattro != func) {
3744 PyErr_Format(PyExc_TypeError,
3745 "can't apply this %s to %s object",
3746 what,
3747 type->tp_name);
3748 return 0;
3750 return 1;
3753 static PyObject *
3754 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3756 setattrofunc func = (setattrofunc)wrapped;
3757 int res;
3758 PyObject *name, *value;
3760 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3761 return NULL;
3762 if (!hackcheck(self, func, "__setattr__"))
3763 return NULL;
3764 res = (*func)(self, name, value);
3765 if (res < 0)
3766 return NULL;
3767 Py_INCREF(Py_None);
3768 return Py_None;
3771 static PyObject *
3772 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3774 setattrofunc func = (setattrofunc)wrapped;
3775 int res;
3776 PyObject *name;
3778 if (!check_num_args(args, 1))
3779 return NULL;
3780 name = PyTuple_GET_ITEM(args, 0);
3781 if (!hackcheck(self, func, "__delattr__"))
3782 return NULL;
3783 res = (*func)(self, name, NULL);
3784 if (res < 0)
3785 return NULL;
3786 Py_INCREF(Py_None);
3787 return Py_None;
3790 static PyObject *
3791 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3793 hashfunc func = (hashfunc)wrapped;
3794 long res;
3796 if (!check_num_args(args, 0))
3797 return NULL;
3798 res = (*func)(self);
3799 if (res == -1 && PyErr_Occurred())
3800 return NULL;
3801 return PyInt_FromLong(res);
3804 static PyObject *
3805 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3807 ternaryfunc func = (ternaryfunc)wrapped;
3809 return (*func)(self, args, kwds);
3812 static PyObject *
3813 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3815 richcmpfunc func = (richcmpfunc)wrapped;
3816 PyObject *other;
3818 if (!check_num_args(args, 1))
3819 return NULL;
3820 other = PyTuple_GET_ITEM(args, 0);
3821 return (*func)(self, other, op);
3824 #undef RICHCMP_WRAPPER
3825 #define RICHCMP_WRAPPER(NAME, OP) \
3826 static PyObject * \
3827 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3829 return wrap_richcmpfunc(self, args, wrapped, OP); \
3832 RICHCMP_WRAPPER(lt, Py_LT)
3833 RICHCMP_WRAPPER(le, Py_LE)
3834 RICHCMP_WRAPPER(eq, Py_EQ)
3835 RICHCMP_WRAPPER(ne, Py_NE)
3836 RICHCMP_WRAPPER(gt, Py_GT)
3837 RICHCMP_WRAPPER(ge, Py_GE)
3839 static PyObject *
3840 wrap_next(PyObject *self, PyObject *args, void *wrapped)
3842 unaryfunc func = (unaryfunc)wrapped;
3843 PyObject *res;
3845 if (!check_num_args(args, 0))
3846 return NULL;
3847 res = (*func)(self);
3848 if (res == NULL && !PyErr_Occurred())
3849 PyErr_SetNone(PyExc_StopIteration);
3850 return res;
3853 static PyObject *
3854 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3856 descrgetfunc func = (descrgetfunc)wrapped;
3857 PyObject *obj;
3858 PyObject *type = NULL;
3860 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3861 return NULL;
3862 if (obj == Py_None)
3863 obj = NULL;
3864 if (type == Py_None)
3865 type = NULL;
3866 if (type == NULL &&obj == NULL) {
3867 PyErr_SetString(PyExc_TypeError,
3868 "__get__(None, None) is invalid");
3869 return NULL;
3871 return (*func)(self, obj, type);
3874 static PyObject *
3875 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3877 descrsetfunc func = (descrsetfunc)wrapped;
3878 PyObject *obj, *value;
3879 int ret;
3881 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3882 return NULL;
3883 ret = (*func)(self, obj, value);
3884 if (ret < 0)
3885 return NULL;
3886 Py_INCREF(Py_None);
3887 return Py_None;
3890 static PyObject *
3891 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3893 descrsetfunc func = (descrsetfunc)wrapped;
3894 PyObject *obj;
3895 int ret;
3897 if (!check_num_args(args, 1))
3898 return NULL;
3899 obj = PyTuple_GET_ITEM(args, 0);
3900 ret = (*func)(self, obj, NULL);
3901 if (ret < 0)
3902 return NULL;
3903 Py_INCREF(Py_None);
3904 return Py_None;
3907 static PyObject *
3908 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3910 initproc func = (initproc)wrapped;
3912 if (func(self, args, kwds) < 0)
3913 return NULL;
3914 Py_INCREF(Py_None);
3915 return Py_None;
3918 static PyObject *
3919 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3921 PyTypeObject *type, *subtype, *staticbase;
3922 PyObject *arg0, *res;
3924 if (self == NULL || !PyType_Check(self))
3925 Py_FatalError("__new__() called with non-type 'self'");
3926 type = (PyTypeObject *)self;
3927 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3928 PyErr_Format(PyExc_TypeError,
3929 "%s.__new__(): not enough arguments",
3930 type->tp_name);
3931 return NULL;
3933 arg0 = PyTuple_GET_ITEM(args, 0);
3934 if (!PyType_Check(arg0)) {
3935 PyErr_Format(PyExc_TypeError,
3936 "%s.__new__(X): X is not a type object (%s)",
3937 type->tp_name,
3938 arg0->ob_type->tp_name);
3939 return NULL;
3941 subtype = (PyTypeObject *)arg0;
3942 if (!PyType_IsSubtype(subtype, type)) {
3943 PyErr_Format(PyExc_TypeError,
3944 "%s.__new__(%s): %s is not a subtype of %s",
3945 type->tp_name,
3946 subtype->tp_name,
3947 subtype->tp_name,
3948 type->tp_name);
3949 return NULL;
3952 /* Check that the use doesn't do something silly and unsafe like
3953 object.__new__(dict). To do this, we check that the
3954 most derived base that's not a heap type is this type. */
3955 staticbase = subtype;
3956 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3957 staticbase = staticbase->tp_base;
3958 if (staticbase->tp_new != type->tp_new) {
3959 PyErr_Format(PyExc_TypeError,
3960 "%s.__new__(%s) is not safe, use %s.__new__()",
3961 type->tp_name,
3962 subtype->tp_name,
3963 staticbase == NULL ? "?" : staticbase->tp_name);
3964 return NULL;
3967 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3968 if (args == NULL)
3969 return NULL;
3970 res = type->tp_new(subtype, args, kwds);
3971 Py_DECREF(args);
3972 return res;
3975 static struct PyMethodDef tp_new_methoddef[] = {
3976 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
3977 PyDoc_STR("T.__new__(S, ...) -> "
3978 "a new object with type S, a subtype of T")},
3982 static int
3983 add_tp_new_wrapper(PyTypeObject *type)
3985 PyObject *func;
3987 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
3988 return 0;
3989 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
3990 if (func == NULL)
3991 return -1;
3992 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
3993 Py_DECREF(func);
3994 return -1;
3996 Py_DECREF(func);
3997 return 0;
4000 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4001 below at override_slots() for more explanation. */
4003 #define SLOT0(FUNCNAME, OPSTR) \
4004 static PyObject * \
4005 FUNCNAME(PyObject *self) \
4007 static PyObject *cache_str; \
4008 return call_method(self, OPSTR, &cache_str, "()"); \
4011 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4012 static PyObject * \
4013 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4015 static PyObject *cache_str; \
4016 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4019 /* Boolean helper for SLOT1BINFULL().
4020 right.__class__ is a nontrivial subclass of left.__class__. */
4021 static int
4022 method_is_overloaded(PyObject *left, PyObject *right, char *name)
4024 PyObject *a, *b;
4025 int ok;
4027 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4028 if (b == NULL) {
4029 PyErr_Clear();
4030 /* If right doesn't have it, it's not overloaded */
4031 return 0;
4034 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4035 if (a == NULL) {
4036 PyErr_Clear();
4037 Py_DECREF(b);
4038 /* If right has it but left doesn't, it's overloaded */
4039 return 1;
4042 ok = PyObject_RichCompareBool(a, b, Py_NE);
4043 Py_DECREF(a);
4044 Py_DECREF(b);
4045 if (ok < 0) {
4046 PyErr_Clear();
4047 return 0;
4050 return ok;
4054 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4055 static PyObject * \
4056 FUNCNAME(PyObject *self, PyObject *other) \
4058 static PyObject *cache_str, *rcache_str; \
4059 int do_other = self->ob_type != other->ob_type && \
4060 other->ob_type->tp_as_number != NULL && \
4061 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4062 if (self->ob_type->tp_as_number != NULL && \
4063 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4064 PyObject *r; \
4065 if (do_other && \
4066 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4067 method_is_overloaded(self, other, ROPSTR)) { \
4068 r = call_maybe( \
4069 other, ROPSTR, &rcache_str, "(O)", self); \
4070 if (r != Py_NotImplemented) \
4071 return r; \
4072 Py_DECREF(r); \
4073 do_other = 0; \
4075 r = call_maybe( \
4076 self, OPSTR, &cache_str, "(O)", other); \
4077 if (r != Py_NotImplemented || \
4078 other->ob_type == self->ob_type) \
4079 return r; \
4080 Py_DECREF(r); \
4082 if (do_other) { \
4083 return call_maybe( \
4084 other, ROPSTR, &rcache_str, "(O)", self); \
4086 Py_INCREF(Py_NotImplemented); \
4087 return Py_NotImplemented; \
4090 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4091 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4093 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4094 static PyObject * \
4095 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4097 static PyObject *cache_str; \
4098 return call_method(self, OPSTR, &cache_str, \
4099 "(" ARGCODES ")", arg1, arg2); \
4102 static int
4103 slot_sq_length(PyObject *self)
4105 static PyObject *len_str;
4106 PyObject *res = call_method(self, "__len__", &len_str, "()");
4107 long temp;
4108 int len;
4110 if (res == NULL)
4111 return -1;
4112 temp = PyInt_AsLong(res);
4113 len = (int)temp;
4114 Py_DECREF(res);
4115 if (len == -1 && PyErr_Occurred())
4116 return -1;
4117 #if SIZEOF_INT < SIZEOF_LONG
4118 /* Overflow check -- range of PyInt is more than C int */
4119 if (len != temp) {
4120 PyErr_SetString(PyExc_OverflowError,
4121 "__len__() should return 0 <= outcome < 2**31");
4122 return -1;
4124 #endif
4125 if (len < 0) {
4126 PyErr_SetString(PyExc_ValueError,
4127 "__len__() should return >= 0");
4128 return -1;
4130 return len;
4133 /* Super-optimized version of slot_sq_item.
4134 Other slots could do the same... */
4135 static PyObject *
4136 slot_sq_item(PyObject *self, int i)
4138 static PyObject *getitem_str;
4139 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4140 descrgetfunc f;
4142 if (getitem_str == NULL) {
4143 getitem_str = PyString_InternFromString("__getitem__");
4144 if (getitem_str == NULL)
4145 return NULL;
4147 func = _PyType_Lookup(self->ob_type, getitem_str);
4148 if (func != NULL) {
4149 if ((f = func->ob_type->tp_descr_get) == NULL)
4150 Py_INCREF(func);
4151 else {
4152 func = f(func, self, (PyObject *)(self->ob_type));
4153 if (func == NULL) {
4154 return NULL;
4157 ival = PyInt_FromLong(i);
4158 if (ival != NULL) {
4159 args = PyTuple_New(1);
4160 if (args != NULL) {
4161 PyTuple_SET_ITEM(args, 0, ival);
4162 retval = PyObject_Call(func, args, NULL);
4163 Py_XDECREF(args);
4164 Py_XDECREF(func);
4165 return retval;
4169 else {
4170 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4172 Py_XDECREF(args);
4173 Py_XDECREF(ival);
4174 Py_XDECREF(func);
4175 return NULL;
4178 SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
4180 static int
4181 slot_sq_ass_item(PyObject *self, int index, PyObject *value)
4183 PyObject *res;
4184 static PyObject *delitem_str, *setitem_str;
4186 if (value == NULL)
4187 res = call_method(self, "__delitem__", &delitem_str,
4188 "(i)", index);
4189 else
4190 res = call_method(self, "__setitem__", &setitem_str,
4191 "(iO)", index, value);
4192 if (res == NULL)
4193 return -1;
4194 Py_DECREF(res);
4195 return 0;
4198 static int
4199 slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
4201 PyObject *res;
4202 static PyObject *delslice_str, *setslice_str;
4204 if (value == NULL)
4205 res = call_method(self, "__delslice__", &delslice_str,
4206 "(ii)", i, j);
4207 else
4208 res = call_method(self, "__setslice__", &setslice_str,
4209 "(iiO)", i, j, value);
4210 if (res == NULL)
4211 return -1;
4212 Py_DECREF(res);
4213 return 0;
4216 static int
4217 slot_sq_contains(PyObject *self, PyObject *value)
4219 PyObject *func, *res, *args;
4220 int result = -1;
4222 static PyObject *contains_str;
4224 func = lookup_maybe(self, "__contains__", &contains_str);
4225 if (func != NULL) {
4226 args = PyTuple_Pack(1, value);
4227 if (args == NULL)
4228 res = NULL;
4229 else {
4230 res = PyObject_Call(func, args, NULL);
4231 Py_DECREF(args);
4233 Py_DECREF(func);
4234 if (res != NULL) {
4235 result = PyObject_IsTrue(res);
4236 Py_DECREF(res);
4239 else if (! PyErr_Occurred()) {
4240 result = _PySequence_IterSearch(self, value,
4241 PY_ITERSEARCH_CONTAINS);
4243 return result;
4246 #define slot_mp_length slot_sq_length
4248 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4250 static int
4251 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4253 PyObject *res;
4254 static PyObject *delitem_str, *setitem_str;
4256 if (value == NULL)
4257 res = call_method(self, "__delitem__", &delitem_str,
4258 "(O)", key);
4259 else
4260 res = call_method(self, "__setitem__", &setitem_str,
4261 "(OO)", key, value);
4262 if (res == NULL)
4263 return -1;
4264 Py_DECREF(res);
4265 return 0;
4268 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4269 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4270 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4271 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4272 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4273 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4275 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4277 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4278 nb_power, "__pow__", "__rpow__")
4280 static PyObject *
4281 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4283 static PyObject *pow_str;
4285 if (modulus == Py_None)
4286 return slot_nb_power_binary(self, other);
4287 /* Three-arg power doesn't use __rpow__. But ternary_op
4288 can call this when the second argument's type uses
4289 slot_nb_power, so check before calling self.__pow__. */
4290 if (self->ob_type->tp_as_number != NULL &&
4291 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4292 return call_method(self, "__pow__", &pow_str,
4293 "(OO)", other, modulus);
4295 Py_INCREF(Py_NotImplemented);
4296 return Py_NotImplemented;
4299 SLOT0(slot_nb_negative, "__neg__")
4300 SLOT0(slot_nb_positive, "__pos__")
4301 SLOT0(slot_nb_absolute, "__abs__")
4303 static int
4304 slot_nb_nonzero(PyObject *self)
4306 PyObject *func, *args;
4307 static PyObject *nonzero_str, *len_str;
4308 int result = -1;
4310 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4311 if (func == NULL) {
4312 if (PyErr_Occurred())
4313 return -1;
4314 func = lookup_maybe(self, "__len__", &len_str);
4315 if (func == NULL)
4316 return PyErr_Occurred() ? -1 : 1;
4318 args = PyTuple_New(0);
4319 if (args != NULL) {
4320 PyObject *temp = PyObject_Call(func, args, NULL);
4321 Py_DECREF(args);
4322 if (temp != NULL) {
4323 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4324 result = PyObject_IsTrue(temp);
4325 else {
4326 PyErr_Format(PyExc_TypeError,
4327 "__nonzero__ should return "
4328 "bool or int, returned %s",
4329 temp->ob_type->tp_name);
4330 result = -1;
4332 Py_DECREF(temp);
4335 Py_DECREF(func);
4336 return result;
4339 SLOT0(slot_nb_invert, "__invert__")
4340 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4341 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4342 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4343 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4344 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4346 static int
4347 slot_nb_coerce(PyObject **a, PyObject **b)
4349 static PyObject *coerce_str;
4350 PyObject *self = *a, *other = *b;
4352 if (self->ob_type->tp_as_number != NULL &&
4353 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4354 PyObject *r;
4355 r = call_maybe(
4356 self, "__coerce__", &coerce_str, "(O)", other);
4357 if (r == NULL)
4358 return -1;
4359 if (r == Py_NotImplemented) {
4360 Py_DECREF(r);
4362 else {
4363 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4364 PyErr_SetString(PyExc_TypeError,
4365 "__coerce__ didn't return a 2-tuple");
4366 Py_DECREF(r);
4367 return -1;
4369 *a = PyTuple_GET_ITEM(r, 0);
4370 Py_INCREF(*a);
4371 *b = PyTuple_GET_ITEM(r, 1);
4372 Py_INCREF(*b);
4373 Py_DECREF(r);
4374 return 0;
4377 if (other->ob_type->tp_as_number != NULL &&
4378 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4379 PyObject *r;
4380 r = call_maybe(
4381 other, "__coerce__", &coerce_str, "(O)", self);
4382 if (r == NULL)
4383 return -1;
4384 if (r == Py_NotImplemented) {
4385 Py_DECREF(r);
4386 return 1;
4388 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4389 PyErr_SetString(PyExc_TypeError,
4390 "__coerce__ didn't return a 2-tuple");
4391 Py_DECREF(r);
4392 return -1;
4394 *a = PyTuple_GET_ITEM(r, 1);
4395 Py_INCREF(*a);
4396 *b = PyTuple_GET_ITEM(r, 0);
4397 Py_INCREF(*b);
4398 Py_DECREF(r);
4399 return 0;
4401 return 1;
4404 SLOT0(slot_nb_int, "__int__")
4405 SLOT0(slot_nb_long, "__long__")
4406 SLOT0(slot_nb_float, "__float__")
4407 SLOT0(slot_nb_oct, "__oct__")
4408 SLOT0(slot_nb_hex, "__hex__")
4409 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4410 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4411 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4412 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4413 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4414 SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
4415 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4416 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4417 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4418 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4419 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4420 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4421 "__floordiv__", "__rfloordiv__")
4422 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4423 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4424 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4426 static int
4427 half_compare(PyObject *self, PyObject *other)
4429 PyObject *func, *args, *res;
4430 static PyObject *cmp_str;
4431 int c;
4433 func = lookup_method(self, "__cmp__", &cmp_str);
4434 if (func == NULL) {
4435 PyErr_Clear();
4437 else {
4438 args = PyTuple_Pack(1, other);
4439 if (args == NULL)
4440 res = NULL;
4441 else {
4442 res = PyObject_Call(func, args, NULL);
4443 Py_DECREF(args);
4445 Py_DECREF(func);
4446 if (res != Py_NotImplemented) {
4447 if (res == NULL)
4448 return -2;
4449 c = PyInt_AsLong(res);
4450 Py_DECREF(res);
4451 if (c == -1 && PyErr_Occurred())
4452 return -2;
4453 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4455 Py_DECREF(res);
4457 return 2;
4460 /* This slot is published for the benefit of try_3way_compare in object.c */
4462 _PyObject_SlotCompare(PyObject *self, PyObject *other)
4464 int c;
4466 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4467 c = half_compare(self, other);
4468 if (c <= 1)
4469 return c;
4471 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4472 c = half_compare(other, self);
4473 if (c < -1)
4474 return -2;
4475 if (c <= 1)
4476 return -c;
4478 return (void *)self < (void *)other ? -1 :
4479 (void *)self > (void *)other ? 1 : 0;
4482 static PyObject *
4483 slot_tp_repr(PyObject *self)
4485 PyObject *func, *res;
4486 static PyObject *repr_str;
4488 func = lookup_method(self, "__repr__", &repr_str);
4489 if (func != NULL) {
4490 res = PyEval_CallObject(func, NULL);
4491 Py_DECREF(func);
4492 return res;
4494 PyErr_Clear();
4495 return PyString_FromFormat("<%s object at %p>",
4496 self->ob_type->tp_name, self);
4499 static PyObject *
4500 slot_tp_str(PyObject *self)
4502 PyObject *func, *res;
4503 static PyObject *str_str;
4505 func = lookup_method(self, "__str__", &str_str);
4506 if (func != NULL) {
4507 res = PyEval_CallObject(func, NULL);
4508 Py_DECREF(func);
4509 return res;
4511 else {
4512 PyErr_Clear();
4513 return slot_tp_repr(self);
4517 static long
4518 slot_tp_hash(PyObject *self)
4520 PyObject *func;
4521 static PyObject *hash_str, *eq_str, *cmp_str;
4522 long h;
4524 func = lookup_method(self, "__hash__", &hash_str);
4526 if (func != NULL) {
4527 PyObject *res = PyEval_CallObject(func, NULL);
4528 Py_DECREF(func);
4529 if (res == NULL)
4530 return -1;
4531 h = PyInt_AsLong(res);
4532 Py_DECREF(res);
4534 else {
4535 PyErr_Clear();
4536 func = lookup_method(self, "__eq__", &eq_str);
4537 if (func == NULL) {
4538 PyErr_Clear();
4539 func = lookup_method(self, "__cmp__", &cmp_str);
4541 if (func != NULL) {
4542 Py_DECREF(func);
4543 PyErr_SetString(PyExc_TypeError, "unhashable type");
4544 return -1;
4546 PyErr_Clear();
4547 h = _Py_HashPointer((void *)self);
4549 if (h == -1 && !PyErr_Occurred())
4550 h = -2;
4551 return h;
4554 static PyObject *
4555 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4557 static PyObject *call_str;
4558 PyObject *meth = lookup_method(self, "__call__", &call_str);
4559 PyObject *res;
4561 if (meth == NULL)
4562 return NULL;
4563 res = PyObject_Call(meth, args, kwds);
4564 Py_DECREF(meth);
4565 return res;
4568 /* There are two slot dispatch functions for tp_getattro.
4570 - slot_tp_getattro() is used when __getattribute__ is overridden
4571 but no __getattr__ hook is present;
4573 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4575 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4576 detects the absence of __getattr__ and then installs the simpler slot if
4577 necessary. */
4579 static PyObject *
4580 slot_tp_getattro(PyObject *self, PyObject *name)
4582 static PyObject *getattribute_str = NULL;
4583 return call_method(self, "__getattribute__", &getattribute_str,
4584 "(O)", name);
4587 static PyObject *
4588 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4590 PyTypeObject *tp = self->ob_type;
4591 PyObject *getattr, *getattribute, *res;
4592 static PyObject *getattribute_str = NULL;
4593 static PyObject *getattr_str = NULL;
4595 if (getattr_str == NULL) {
4596 getattr_str = PyString_InternFromString("__getattr__");
4597 if (getattr_str == NULL)
4598 return NULL;
4600 if (getattribute_str == NULL) {
4601 getattribute_str =
4602 PyString_InternFromString("__getattribute__");
4603 if (getattribute_str == NULL)
4604 return NULL;
4606 getattr = _PyType_Lookup(tp, getattr_str);
4607 if (getattr == NULL) {
4608 /* No __getattr__ hook: use a simpler dispatcher */
4609 tp->tp_getattro = slot_tp_getattro;
4610 return slot_tp_getattro(self, name);
4612 getattribute = _PyType_Lookup(tp, getattribute_str);
4613 if (getattribute == NULL ||
4614 (getattribute->ob_type == &PyWrapperDescr_Type &&
4615 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4616 (void *)PyObject_GenericGetAttr))
4617 res = PyObject_GenericGetAttr(self, name);
4618 else
4619 res = PyObject_CallFunction(getattribute, "OO", self, name);
4620 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4621 PyErr_Clear();
4622 res = PyObject_CallFunction(getattr, "OO", self, name);
4624 return res;
4627 static int
4628 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4630 PyObject *res;
4631 static PyObject *delattr_str, *setattr_str;
4633 if (value == NULL)
4634 res = call_method(self, "__delattr__", &delattr_str,
4635 "(O)", name);
4636 else
4637 res = call_method(self, "__setattr__", &setattr_str,
4638 "(OO)", name, value);
4639 if (res == NULL)
4640 return -1;
4641 Py_DECREF(res);
4642 return 0;
4645 /* Map rich comparison operators to their __xx__ namesakes */
4646 static char *name_op[] = {
4647 "__lt__",
4648 "__le__",
4649 "__eq__",
4650 "__ne__",
4651 "__gt__",
4652 "__ge__",
4655 static PyObject *
4656 half_richcompare(PyObject *self, PyObject *other, int op)
4658 PyObject *func, *args, *res;
4659 static PyObject *op_str[6];
4661 func = lookup_method(self, name_op[op], &op_str[op]);
4662 if (func == NULL) {
4663 PyErr_Clear();
4664 Py_INCREF(Py_NotImplemented);
4665 return Py_NotImplemented;
4667 args = PyTuple_Pack(1, other);
4668 if (args == NULL)
4669 res = NULL;
4670 else {
4671 res = PyObject_Call(func, args, NULL);
4672 Py_DECREF(args);
4674 Py_DECREF(func);
4675 return res;
4678 static PyObject *
4679 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4681 PyObject *res;
4683 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4684 res = half_richcompare(self, other, op);
4685 if (res != Py_NotImplemented)
4686 return res;
4687 Py_DECREF(res);
4689 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4690 res = half_richcompare(other, self, _Py_SwappedOp[op]);
4691 if (res != Py_NotImplemented) {
4692 return res;
4694 Py_DECREF(res);
4696 Py_INCREF(Py_NotImplemented);
4697 return Py_NotImplemented;
4700 static PyObject *
4701 slot_tp_iter(PyObject *self)
4703 PyObject *func, *res;
4704 static PyObject *iter_str, *getitem_str;
4706 func = lookup_method(self, "__iter__", &iter_str);
4707 if (func != NULL) {
4708 PyObject *args;
4709 args = res = PyTuple_New(0);
4710 if (args != NULL) {
4711 res = PyObject_Call(func, args, NULL);
4712 Py_DECREF(args);
4714 Py_DECREF(func);
4715 return res;
4717 PyErr_Clear();
4718 func = lookup_method(self, "__getitem__", &getitem_str);
4719 if (func == NULL) {
4720 PyErr_SetString(PyExc_TypeError,
4721 "iteration over non-sequence");
4722 return NULL;
4724 Py_DECREF(func);
4725 return PySeqIter_New(self);
4728 static PyObject *
4729 slot_tp_iternext(PyObject *self)
4731 static PyObject *next_str;
4732 return call_method(self, "next", &next_str, "()");
4735 static PyObject *
4736 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4738 PyTypeObject *tp = self->ob_type;
4739 PyObject *get;
4740 static PyObject *get_str = NULL;
4742 if (get_str == NULL) {
4743 get_str = PyString_InternFromString("__get__");
4744 if (get_str == NULL)
4745 return NULL;
4747 get = _PyType_Lookup(tp, get_str);
4748 if (get == NULL) {
4749 /* Avoid further slowdowns */
4750 if (tp->tp_descr_get == slot_tp_descr_get)
4751 tp->tp_descr_get = NULL;
4752 Py_INCREF(self);
4753 return self;
4755 if (obj == NULL)
4756 obj = Py_None;
4757 if (type == NULL)
4758 type = Py_None;
4759 return PyObject_CallFunction(get, "OOO", self, obj, type);
4762 static int
4763 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4765 PyObject *res;
4766 static PyObject *del_str, *set_str;
4768 if (value == NULL)
4769 res = call_method(self, "__delete__", &del_str,
4770 "(O)", target);
4771 else
4772 res = call_method(self, "__set__", &set_str,
4773 "(OO)", target, value);
4774 if (res == NULL)
4775 return -1;
4776 Py_DECREF(res);
4777 return 0;
4780 static int
4781 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4783 static PyObject *init_str;
4784 PyObject *meth = lookup_method(self, "__init__", &init_str);
4785 PyObject *res;
4787 if (meth == NULL)
4788 return -1;
4789 res = PyObject_Call(meth, args, kwds);
4790 Py_DECREF(meth);
4791 if (res == NULL)
4792 return -1;
4793 if (res != Py_None) {
4794 PyErr_SetString(PyExc_TypeError,
4795 "__init__() should return None");
4796 Py_DECREF(res);
4797 return -1;
4799 Py_DECREF(res);
4800 return 0;
4803 static PyObject *
4804 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4806 static PyObject *new_str;
4807 PyObject *func;
4808 PyObject *newargs, *x;
4809 int i, n;
4811 if (new_str == NULL) {
4812 new_str = PyString_InternFromString("__new__");
4813 if (new_str == NULL)
4814 return NULL;
4816 func = PyObject_GetAttr((PyObject *)type, new_str);
4817 if (func == NULL)
4818 return NULL;
4819 assert(PyTuple_Check(args));
4820 n = PyTuple_GET_SIZE(args);
4821 newargs = PyTuple_New(n+1);
4822 if (newargs == NULL)
4823 return NULL;
4824 Py_INCREF(type);
4825 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4826 for (i = 0; i < n; i++) {
4827 x = PyTuple_GET_ITEM(args, i);
4828 Py_INCREF(x);
4829 PyTuple_SET_ITEM(newargs, i+1, x);
4831 x = PyObject_Call(func, newargs, kwds);
4832 Py_DECREF(newargs);
4833 Py_DECREF(func);
4834 return x;
4837 static void
4838 slot_tp_del(PyObject *self)
4840 static PyObject *del_str = NULL;
4841 PyObject *del, *res;
4842 PyObject *error_type, *error_value, *error_traceback;
4844 /* Temporarily resurrect the object. */
4845 assert(self->ob_refcnt == 0);
4846 self->ob_refcnt = 1;
4848 /* Save the current exception, if any. */
4849 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4851 /* Execute __del__ method, if any. */
4852 del = lookup_maybe(self, "__del__", &del_str);
4853 if (del != NULL) {
4854 res = PyEval_CallObject(del, NULL);
4855 if (res == NULL)
4856 PyErr_WriteUnraisable(del);
4857 else
4858 Py_DECREF(res);
4859 Py_DECREF(del);
4862 /* Restore the saved exception. */
4863 PyErr_Restore(error_type, error_value, error_traceback);
4865 /* Undo the temporary resurrection; can't use DECREF here, it would
4866 * cause a recursive call.
4868 assert(self->ob_refcnt > 0);
4869 if (--self->ob_refcnt == 0)
4870 return; /* this is the normal path out */
4872 /* __del__ resurrected it! Make it look like the original Py_DECREF
4873 * never happened.
4876 int refcnt = self->ob_refcnt;
4877 _Py_NewReference(self);
4878 self->ob_refcnt = refcnt;
4880 assert(!PyType_IS_GC(self->ob_type) ||
4881 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4882 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4883 * we need to undo that. */
4884 _Py_DEC_REFTOTAL;
4885 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4886 * chain, so no more to do there.
4887 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4888 * _Py_NewReference bumped tp_allocs: both of those need to be
4889 * undone.
4891 #ifdef COUNT_ALLOCS
4892 --self->ob_type->tp_frees;
4893 --self->ob_type->tp_allocs;
4894 #endif
4898 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4899 functions. The offsets here are relative to the 'PyHeapTypeObject'
4900 structure, which incorporates the additional structures used for numbers,
4901 sequences and mappings.
4902 Note that multiple names may map to the same slot (e.g. __eq__,
4903 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4904 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4905 terminated with an all-zero entry. (This table is further initialized and
4906 sorted in init_slotdefs() below.) */
4908 typedef struct wrapperbase slotdef;
4910 #undef TPSLOT
4911 #undef FLSLOT
4912 #undef ETSLOT
4913 #undef SQSLOT
4914 #undef MPSLOT
4915 #undef NBSLOT
4916 #undef UNSLOT
4917 #undef IBSLOT
4918 #undef BINSLOT
4919 #undef RBINSLOT
4921 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4922 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4923 PyDoc_STR(DOC)}
4924 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4925 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4926 PyDoc_STR(DOC), FLAGS}
4927 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4928 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4929 PyDoc_STR(DOC)}
4930 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4931 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4932 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4933 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4934 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4935 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4936 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4937 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4938 "x." NAME "() <==> " DOC)
4939 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4940 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4941 "x." NAME "(y) <==> x" DOC "y")
4942 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4943 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4944 "x." NAME "(y) <==> x" DOC "y")
4945 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4946 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4947 "x." NAME "(y) <==> y" DOC "x")
4948 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4949 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4950 "x." NAME "(y) <==> " DOC)
4951 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4952 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4953 "x." NAME "(y) <==> " DOC)
4955 static slotdef slotdefs[] = {
4956 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
4957 "x.__len__() <==> len(x)"),
4958 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
4959 The logic in abstract.c always falls back to nb_add/nb_multiply in
4960 this case. Defining both the nb_* and the sq_* slots to call the
4961 user-defined methods has unexpected side-effects, as shown by
4962 test_descr.notimplemented() */
4963 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
4964 "x.__add__(y) <==> x+y"),
4965 SQSLOT("__mul__", sq_repeat, NULL, wrap_intargfunc,
4966 "x.__mul__(n) <==> x*n"),
4967 SQSLOT("__rmul__", sq_repeat, NULL, wrap_intargfunc,
4968 "x.__rmul__(n) <==> n*x"),
4969 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
4970 "x.__getitem__(y) <==> x[y]"),
4971 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
4972 "x.__getslice__(i, j) <==> x[i:j]\n\
4974 Use of negative indices is not supported."),
4975 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
4976 "x.__setitem__(i, y) <==> x[i]=y"),
4977 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
4978 "x.__delitem__(y) <==> del x[y]"),
4979 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
4980 wrap_intintobjargproc,
4981 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4983 Use of negative indices is not supported."),
4984 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
4985 "x.__delslice__(i, j) <==> del x[i:j]\n\
4987 Use of negative indices is not supported."),
4988 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
4989 "x.__contains__(y) <==> y in x"),
4990 SQSLOT("__iadd__", sq_inplace_concat, NULL,
4991 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
4992 SQSLOT("__imul__", sq_inplace_repeat, NULL,
4993 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
4995 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
4996 "x.__len__() <==> len(x)"),
4997 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
4998 wrap_binaryfunc,
4999 "x.__getitem__(y) <==> x[y]"),
5000 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
5001 wrap_objobjargproc,
5002 "x.__setitem__(i, y) <==> x[i]=y"),
5003 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
5004 wrap_delitem,
5005 "x.__delitem__(y) <==> del x[y]"),
5007 BINSLOT("__add__", nb_add, slot_nb_add,
5008 "+"),
5009 RBINSLOT("__radd__", nb_add, slot_nb_add,
5010 "+"),
5011 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5012 "-"),
5013 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5014 "-"),
5015 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5016 "*"),
5017 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5018 "*"),
5019 BINSLOT("__div__", nb_divide, slot_nb_divide,
5020 "/"),
5021 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
5022 "/"),
5023 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5024 "%"),
5025 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5026 "%"),
5027 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
5028 "divmod(x, y)"),
5029 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
5030 "divmod(y, x)"),
5031 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5032 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5033 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5034 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5035 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5036 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5037 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5038 "abs(x)"),
5039 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
5040 "x != 0"),
5041 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5042 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5043 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5044 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5045 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5046 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5047 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5048 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5049 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5050 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5051 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5052 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5053 "x.__coerce__(y) <==> coerce(x, y)"),
5054 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5055 "int(x)"),
5056 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5057 "long(x)"),
5058 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5059 "float(x)"),
5060 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5061 "oct(x)"),
5062 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5063 "hex(x)"),
5064 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5065 wrap_binaryfunc, "+"),
5066 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5067 wrap_binaryfunc, "-"),
5068 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5069 wrap_binaryfunc, "*"),
5070 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5071 wrap_binaryfunc, "/"),
5072 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5073 wrap_binaryfunc, "%"),
5074 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5075 wrap_binaryfunc, "**"),
5076 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5077 wrap_binaryfunc, "<<"),
5078 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5079 wrap_binaryfunc, ">>"),
5080 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5081 wrap_binaryfunc, "&"),
5082 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5083 wrap_binaryfunc, "^"),
5084 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5085 wrap_binaryfunc, "|"),
5086 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5087 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5088 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5089 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5090 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5091 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5092 IBSLOT("__itruediv__", nb_inplace_true_divide,
5093 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5095 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5096 "x.__str__() <==> str(x)"),
5097 TPSLOT("__str__", tp_print, NULL, NULL, ""),
5098 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5099 "x.__repr__() <==> repr(x)"),
5100 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5101 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5102 "x.__cmp__(y) <==> cmp(x,y)"),
5103 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5104 "x.__hash__() <==> hash(x)"),
5105 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5106 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5107 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5108 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5109 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5110 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5111 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5112 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5113 "x.__setattr__('name', value) <==> x.name = value"),
5114 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5115 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5116 "x.__delattr__('name') <==> del x.name"),
5117 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5118 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5119 "x.__lt__(y) <==> x<y"),
5120 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5121 "x.__le__(y) <==> x<=y"),
5122 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5123 "x.__eq__(y) <==> x==y"),
5124 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5125 "x.__ne__(y) <==> x!=y"),
5126 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5127 "x.__gt__(y) <==> x>y"),
5128 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5129 "x.__ge__(y) <==> x>=y"),
5130 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5131 "x.__iter__() <==> iter(x)"),
5132 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5133 "x.next() -> the next value, or raise StopIteration"),
5134 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5135 "descr.__get__(obj[, type]) -> value"),
5136 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5137 "descr.__set__(obj, value)"),
5138 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5139 wrap_descr_delete, "descr.__delete__(obj)"),
5140 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5141 "x.__init__(...) initializes x; "
5142 "see x.__class__.__doc__ for signature",
5143 PyWrapperFlag_KEYWORDS),
5144 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5145 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5146 {NULL}
5149 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5150 pointer to the actual slot. This is not quite the same as simply adding
5151 the offset to the type pointer, since it takes care to indirect through the
5152 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5153 indirection pointer is NULL. */
5154 static void **
5155 slotptr(PyTypeObject *type, int offset)
5157 char *ptr;
5159 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5160 assert(offset >= 0);
5161 assert(offset < offsetof(PyHeapTypeObject, as_buffer));
5162 if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5163 ptr = (void *)type->tp_as_sequence;
5164 offset -= offsetof(PyHeapTypeObject, as_sequence);
5166 else if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5167 ptr = (void *)type->tp_as_mapping;
5168 offset -= offsetof(PyHeapTypeObject, as_mapping);
5170 else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
5171 ptr = (void *)type->tp_as_number;
5172 offset -= offsetof(PyHeapTypeObject, as_number);
5174 else {
5175 ptr = (void *)type;
5177 if (ptr != NULL)
5178 ptr += offset;
5179 return (void **)ptr;
5182 /* Length of array of slotdef pointers used to store slots with the
5183 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5184 the same __name__, for any __name__. Since that's a static property, it is
5185 appropriate to declare fixed-size arrays for this. */
5186 #define MAX_EQUIV 10
5188 /* Return a slot pointer for a given name, but ONLY if the attribute has
5189 exactly one slot function. The name must be an interned string. */
5190 static void **
5191 resolve_slotdups(PyTypeObject *type, PyObject *name)
5193 /* XXX Maybe this could be optimized more -- but is it worth it? */
5195 /* pname and ptrs act as a little cache */
5196 static PyObject *pname;
5197 static slotdef *ptrs[MAX_EQUIV];
5198 slotdef *p, **pp;
5199 void **res, **ptr;
5201 if (pname != name) {
5202 /* Collect all slotdefs that match name into ptrs. */
5203 pname = name;
5204 pp = ptrs;
5205 for (p = slotdefs; p->name_strobj; p++) {
5206 if (p->name_strobj == name)
5207 *pp++ = p;
5209 *pp = NULL;
5212 /* Look in all matching slots of the type; if exactly one of these has
5213 a filled-in slot, return its value. Otherwise return NULL. */
5214 res = NULL;
5215 for (pp = ptrs; *pp; pp++) {
5216 ptr = slotptr(type, (*pp)->offset);
5217 if (ptr == NULL || *ptr == NULL)
5218 continue;
5219 if (res != NULL)
5220 return NULL;
5221 res = ptr;
5223 return res;
5226 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5227 does some incredibly complex thinking and then sticks something into the
5228 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5229 interests, and then stores a generic wrapper or a specific function into
5230 the slot.) Return a pointer to the next slotdef with a different offset,
5231 because that's convenient for fixup_slot_dispatchers(). */
5232 static slotdef *
5233 update_one_slot(PyTypeObject *type, slotdef *p)
5235 PyObject *descr;
5236 PyWrapperDescrObject *d;
5237 void *generic = NULL, *specific = NULL;
5238 int use_generic = 0;
5239 int offset = p->offset;
5240 void **ptr = slotptr(type, offset);
5242 if (ptr == NULL) {
5243 do {
5244 ++p;
5245 } while (p->offset == offset);
5246 return p;
5248 do {
5249 descr = _PyType_Lookup(type, p->name_strobj);
5250 if (descr == NULL)
5251 continue;
5252 if (descr->ob_type == &PyWrapperDescr_Type) {
5253 void **tptr = resolve_slotdups(type, p->name_strobj);
5254 if (tptr == NULL || tptr == ptr)
5255 generic = p->function;
5256 d = (PyWrapperDescrObject *)descr;
5257 if (d->d_base->wrapper == p->wrapper &&
5258 PyType_IsSubtype(type, d->d_type))
5260 if (specific == NULL ||
5261 specific == d->d_wrapped)
5262 specific = d->d_wrapped;
5263 else
5264 use_generic = 1;
5267 else if (descr->ob_type == &PyCFunction_Type &&
5268 PyCFunction_GET_FUNCTION(descr) ==
5269 (PyCFunction)tp_new_wrapper &&
5270 strcmp(p->name, "__new__") == 0)
5272 /* The __new__ wrapper is not a wrapper descriptor,
5273 so must be special-cased differently.
5274 If we don't do this, creating an instance will
5275 always use slot_tp_new which will look up
5276 __new__ in the MRO which will call tp_new_wrapper
5277 which will look through the base classes looking
5278 for a static base and call its tp_new (usually
5279 PyType_GenericNew), after performing various
5280 sanity checks and constructing a new argument
5281 list. Cut all that nonsense short -- this speeds
5282 up instance creation tremendously. */
5283 specific = (void *)type->tp_new;
5284 /* XXX I'm not 100% sure that there isn't a hole
5285 in this reasoning that requires additional
5286 sanity checks. I'll buy the first person to
5287 point out a bug in this reasoning a beer. */
5289 else {
5290 use_generic = 1;
5291 generic = p->function;
5293 } while ((++p)->offset == offset);
5294 if (specific && !use_generic)
5295 *ptr = specific;
5296 else
5297 *ptr = generic;
5298 return p;
5301 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5302 This is a callback for update_subclasses(). */
5303 static int
5304 update_slots_callback(PyTypeObject *type, void *data)
5306 slotdef **pp = (slotdef **)data;
5308 for (; *pp; pp++)
5309 update_one_slot(type, *pp);
5310 return 0;
5313 /* Comparison function for qsort() to compare slotdefs by their offset, and
5314 for equal offset by their address (to force a stable sort). */
5315 static int
5316 slotdef_cmp(const void *aa, const void *bb)
5318 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5319 int c = a->offset - b->offset;
5320 if (c != 0)
5321 return c;
5322 else
5323 return a - b;
5326 /* Initialize the slotdefs table by adding interned string objects for the
5327 names and sorting the entries. */
5328 static void
5329 init_slotdefs(void)
5331 slotdef *p;
5332 static int initialized = 0;
5334 if (initialized)
5335 return;
5336 for (p = slotdefs; p->name; p++) {
5337 p->name_strobj = PyString_InternFromString(p->name);
5338 if (!p->name_strobj)
5339 Py_FatalError("Out of memory interning slotdef names");
5341 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5342 slotdef_cmp);
5343 initialized = 1;
5346 /* Update the slots after assignment to a class (type) attribute. */
5347 static int
5348 update_slot(PyTypeObject *type, PyObject *name)
5350 slotdef *ptrs[MAX_EQUIV];
5351 slotdef *p;
5352 slotdef **pp;
5353 int offset;
5355 init_slotdefs();
5356 pp = ptrs;
5357 for (p = slotdefs; p->name; p++) {
5358 /* XXX assume name is interned! */
5359 if (p->name_strobj == name)
5360 *pp++ = p;
5362 *pp = NULL;
5363 for (pp = ptrs; *pp; pp++) {
5364 p = *pp;
5365 offset = p->offset;
5366 while (p > slotdefs && (p-1)->offset == offset)
5367 --p;
5368 *pp = p;
5370 if (ptrs[0] == NULL)
5371 return 0; /* Not an attribute that affects any slots */
5372 return update_subclasses(type, name,
5373 update_slots_callback, (void *)ptrs);
5376 /* Store the proper functions in the slot dispatches at class (type)
5377 definition time, based upon which operations the class overrides in its
5378 dict. */
5379 static void
5380 fixup_slot_dispatchers(PyTypeObject *type)
5382 slotdef *p;
5384 init_slotdefs();
5385 for (p = slotdefs; p->name; )
5386 p = update_one_slot(type, p);
5389 static void
5390 update_all_slots(PyTypeObject* type)
5392 slotdef *p;
5394 init_slotdefs();
5395 for (p = slotdefs; p->name; p++) {
5396 /* update_slot returns int but can't actually fail */
5397 update_slot(type, p->name_strobj);
5401 /* recurse_down_subclasses() and update_subclasses() are mutually
5402 recursive functions to call a callback for all subclasses,
5403 but refraining from recursing into subclasses that define 'name'. */
5405 static int
5406 update_subclasses(PyTypeObject *type, PyObject *name,
5407 update_callback callback, void *data)
5409 if (callback(type, data) < 0)
5410 return -1;
5411 return recurse_down_subclasses(type, name, callback, data);
5414 static int
5415 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5416 update_callback callback, void *data)
5418 PyTypeObject *subclass;
5419 PyObject *ref, *subclasses, *dict;
5420 int i, n;
5422 subclasses = type->tp_subclasses;
5423 if (subclasses == NULL)
5424 return 0;
5425 assert(PyList_Check(subclasses));
5426 n = PyList_GET_SIZE(subclasses);
5427 for (i = 0; i < n; i++) {
5428 ref = PyList_GET_ITEM(subclasses, i);
5429 assert(PyWeakref_CheckRef(ref));
5430 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5431 assert(subclass != NULL);
5432 if ((PyObject *)subclass == Py_None)
5433 continue;
5434 assert(PyType_Check(subclass));
5435 /* Avoid recursing down into unaffected classes */
5436 dict = subclass->tp_dict;
5437 if (dict != NULL && PyDict_Check(dict) &&
5438 PyDict_GetItem(dict, name) != NULL)
5439 continue;
5440 if (update_subclasses(subclass, name, callback, data) < 0)
5441 return -1;
5443 return 0;
5446 /* This function is called by PyType_Ready() to populate the type's
5447 dictionary with method descriptors for function slots. For each
5448 function slot (like tp_repr) that's defined in the type, one or more
5449 corresponding descriptors are added in the type's tp_dict dictionary
5450 under the appropriate name (like __repr__). Some function slots
5451 cause more than one descriptor to be added (for example, the nb_add
5452 slot adds both __add__ and __radd__ descriptors) and some function
5453 slots compete for the same descriptor (for example both sq_item and
5454 mp_subscript generate a __getitem__ descriptor).
5456 In the latter case, the first slotdef entry encoutered wins. Since
5457 slotdef entries are sorted by the offset of the slot in the
5458 PyHeapTypeObject, this gives us some control over disambiguating
5459 between competing slots: the members of PyHeapTypeObject are listed
5460 from most general to least general, so the most general slot is
5461 preferred. In particular, because as_mapping comes before as_sequence,
5462 for a type that defines both mp_subscript and sq_item, mp_subscript
5463 wins.
5465 This only adds new descriptors and doesn't overwrite entries in
5466 tp_dict that were previously defined. The descriptors contain a
5467 reference to the C function they must call, so that it's safe if they
5468 are copied into a subtype's __dict__ and the subtype has a different
5469 C function in its slot -- calling the method defined by the
5470 descriptor will call the C function that was used to create it,
5471 rather than the C function present in the slot when it is called.
5472 (This is important because a subtype may have a C function in the
5473 slot that calls the method from the dictionary, and we want to avoid
5474 infinite recursion here.) */
5476 static int
5477 add_operators(PyTypeObject *type)
5479 PyObject *dict = type->tp_dict;
5480 slotdef *p;
5481 PyObject *descr;
5482 void **ptr;
5484 init_slotdefs();
5485 for (p = slotdefs; p->name; p++) {
5486 if (p->wrapper == NULL)
5487 continue;
5488 ptr = slotptr(type, p->offset);
5489 if (!ptr || !*ptr)
5490 continue;
5491 if (PyDict_GetItem(dict, p->name_strobj))
5492 continue;
5493 descr = PyDescr_NewWrapper(type, p, *ptr);
5494 if (descr == NULL)
5495 return -1;
5496 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5497 return -1;
5498 Py_DECREF(descr);
5500 if (type->tp_new != NULL) {
5501 if (add_tp_new_wrapper(type) < 0)
5502 return -1;
5504 return 0;
5508 /* Cooperative 'super' */
5510 typedef struct {
5511 PyObject_HEAD
5512 PyTypeObject *type;
5513 PyObject *obj;
5514 PyTypeObject *obj_type;
5515 } superobject;
5517 static PyMemberDef super_members[] = {
5518 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5519 "the class invoking super()"},
5520 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5521 "the instance invoking super(); may be None"},
5522 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5523 "the type of the instance invoking super(); may be None"},
5527 static void
5528 super_dealloc(PyObject *self)
5530 superobject *su = (superobject *)self;
5532 _PyObject_GC_UNTRACK(self);
5533 Py_XDECREF(su->obj);
5534 Py_XDECREF(su->type);
5535 Py_XDECREF(su->obj_type);
5536 self->ob_type->tp_free(self);
5539 static PyObject *
5540 super_repr(PyObject *self)
5542 superobject *su = (superobject *)self;
5544 if (su->obj_type)
5545 return PyString_FromFormat(
5546 "<super: <class '%s'>, <%s object>>",
5547 su->type ? su->type->tp_name : "NULL",
5548 su->obj_type->tp_name);
5549 else
5550 return PyString_FromFormat(
5551 "<super: <class '%s'>, NULL>",
5552 su->type ? su->type->tp_name : "NULL");
5555 static PyObject *
5556 super_getattro(PyObject *self, PyObject *name)
5558 superobject *su = (superobject *)self;
5559 int skip = su->obj_type == NULL;
5561 if (!skip) {
5562 /* We want __class__ to return the class of the super object
5563 (i.e. super, or a subclass), not the class of su->obj. */
5564 skip = (PyString_Check(name) &&
5565 PyString_GET_SIZE(name) == 9 &&
5566 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5569 if (!skip) {
5570 PyObject *mro, *res, *tmp, *dict;
5571 PyTypeObject *starttype;
5572 descrgetfunc f;
5573 int i, n;
5575 starttype = su->obj_type;
5576 mro = starttype->tp_mro;
5578 if (mro == NULL)
5579 n = 0;
5580 else {
5581 assert(PyTuple_Check(mro));
5582 n = PyTuple_GET_SIZE(mro);
5584 for (i = 0; i < n; i++) {
5585 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
5586 break;
5588 i++;
5589 res = NULL;
5590 for (; i < n; i++) {
5591 tmp = PyTuple_GET_ITEM(mro, i);
5592 if (PyType_Check(tmp))
5593 dict = ((PyTypeObject *)tmp)->tp_dict;
5594 else if (PyClass_Check(tmp))
5595 dict = ((PyClassObject *)tmp)->cl_dict;
5596 else
5597 continue;
5598 res = PyDict_GetItem(dict, name);
5599 if (res != NULL) {
5600 Py_INCREF(res);
5601 f = res->ob_type->tp_descr_get;
5602 if (f != NULL) {
5603 tmp = f(res,
5604 /* Only pass 'obj' param if
5605 this is instance-mode super
5606 (See SF ID #743627)
5608 (su->obj == (PyObject *)
5609 su->obj_type
5610 ? (PyObject *)NULL
5611 : su->obj),
5612 (PyObject *)starttype);
5613 Py_DECREF(res);
5614 res = tmp;
5616 return res;
5620 return PyObject_GenericGetAttr(self, name);
5623 static PyTypeObject *
5624 supercheck(PyTypeObject *type, PyObject *obj)
5626 /* Check that a super() call makes sense. Return a type object.
5628 obj can be a new-style class, or an instance of one:
5630 - If it is a class, it must be a subclass of 'type'. This case is
5631 used for class methods; the return value is obj.
5633 - If it is an instance, it must be an instance of 'type'. This is
5634 the normal case; the return value is obj.__class__.
5636 But... when obj is an instance, we want to allow for the case where
5637 obj->ob_type is not a subclass of type, but obj.__class__ is!
5638 This will allow using super() with a proxy for obj.
5641 /* Check for first bullet above (special case) */
5642 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5643 Py_INCREF(obj);
5644 return (PyTypeObject *)obj;
5647 /* Normal case */
5648 if (PyType_IsSubtype(obj->ob_type, type)) {
5649 Py_INCREF(obj->ob_type);
5650 return obj->ob_type;
5652 else {
5653 /* Try the slow way */
5654 static PyObject *class_str = NULL;
5655 PyObject *class_attr;
5657 if (class_str == NULL) {
5658 class_str = PyString_FromString("__class__");
5659 if (class_str == NULL)
5660 return NULL;
5663 class_attr = PyObject_GetAttr(obj, class_str);
5665 if (class_attr != NULL &&
5666 PyType_Check(class_attr) &&
5667 (PyTypeObject *)class_attr != obj->ob_type)
5669 int ok = PyType_IsSubtype(
5670 (PyTypeObject *)class_attr, type);
5671 if (ok)
5672 return (PyTypeObject *)class_attr;
5675 if (class_attr == NULL)
5676 PyErr_Clear();
5677 else
5678 Py_DECREF(class_attr);
5681 PyErr_SetString(PyExc_TypeError,
5682 "super(type, obj): "
5683 "obj must be an instance or subtype of type");
5684 return NULL;
5687 static PyObject *
5688 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5690 superobject *su = (superobject *)self;
5691 superobject *new;
5693 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5694 /* Not binding to an object, or already bound */
5695 Py_INCREF(self);
5696 return self;
5698 if (su->ob_type != &PySuper_Type)
5699 /* If su is an instance of a (strict) subclass of super,
5700 call its type */
5701 return PyObject_CallFunction((PyObject *)su->ob_type,
5702 "OO", su->type, obj);
5703 else {
5704 /* Inline the common case */
5705 PyTypeObject *obj_type = supercheck(su->type, obj);
5706 if (obj_type == NULL)
5707 return NULL;
5708 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5709 NULL, NULL);
5710 if (new == NULL)
5711 return NULL;
5712 Py_INCREF(su->type);
5713 Py_INCREF(obj);
5714 new->type = su->type;
5715 new->obj = obj;
5716 new->obj_type = obj_type;
5717 return (PyObject *)new;
5721 static int
5722 super_init(PyObject *self, PyObject *args, PyObject *kwds)
5724 superobject *su = (superobject *)self;
5725 PyTypeObject *type;
5726 PyObject *obj = NULL;
5727 PyTypeObject *obj_type = NULL;
5729 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5730 return -1;
5731 if (obj == Py_None)
5732 obj = NULL;
5733 if (obj != NULL) {
5734 obj_type = supercheck(type, obj);
5735 if (obj_type == NULL)
5736 return -1;
5737 Py_INCREF(obj);
5739 Py_INCREF(type);
5740 su->type = type;
5741 su->obj = obj;
5742 su->obj_type = obj_type;
5743 return 0;
5746 PyDoc_STRVAR(super_doc,
5747 "super(type) -> unbound super object\n"
5748 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5749 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5750 "Typical use to call a cooperative superclass method:\n"
5751 "class C(B):\n"
5752 " def meth(self, arg):\n"
5753 " super(C, self).meth(arg)");
5755 static int
5756 super_traverse(PyObject *self, visitproc visit, void *arg)
5758 superobject *su = (superobject *)self;
5759 int err;
5761 #define VISIT(SLOT) \
5762 if (SLOT) { \
5763 err = visit((PyObject *)(SLOT), arg); \
5764 if (err) \
5765 return err; \
5768 VISIT(su->obj);
5769 VISIT(su->type);
5770 VISIT(su->obj_type);
5772 #undef VISIT
5774 return 0;
5777 PyTypeObject PySuper_Type = {
5778 PyObject_HEAD_INIT(&PyType_Type)
5779 0, /* ob_size */
5780 "super", /* tp_name */
5781 sizeof(superobject), /* tp_basicsize */
5782 0, /* tp_itemsize */
5783 /* methods */
5784 super_dealloc, /* tp_dealloc */
5785 0, /* tp_print */
5786 0, /* tp_getattr */
5787 0, /* tp_setattr */
5788 0, /* tp_compare */
5789 super_repr, /* tp_repr */
5790 0, /* tp_as_number */
5791 0, /* tp_as_sequence */
5792 0, /* tp_as_mapping */
5793 0, /* tp_hash */
5794 0, /* tp_call */
5795 0, /* tp_str */
5796 super_getattro, /* tp_getattro */
5797 0, /* tp_setattro */
5798 0, /* tp_as_buffer */
5799 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5800 Py_TPFLAGS_BASETYPE, /* tp_flags */
5801 super_doc, /* tp_doc */
5802 super_traverse, /* tp_traverse */
5803 0, /* tp_clear */
5804 0, /* tp_richcompare */
5805 0, /* tp_weaklistoffset */
5806 0, /* tp_iter */
5807 0, /* tp_iternext */
5808 0, /* tp_methods */
5809 super_members, /* tp_members */
5810 0, /* tp_getset */
5811 0, /* tp_base */
5812 0, /* tp_dict */
5813 super_descr_get, /* tp_descr_get */
5814 0, /* tp_descr_set */
5815 0, /* tp_dictoffset */
5816 super_init, /* tp_init */
5817 PyType_GenericAlloc, /* tp_alloc */
5818 PyType_GenericNew, /* tp_new */
5819 PyObject_GC_Del, /* tp_free */