Typo fix
[pytest.git] / Objects / typeobject.c
blob8a6f78266c1486f0e412488e5e4e2825b0f7e6aa
1 /* Type object implementation */
3 #include "Python.h"
4 #include "structmember.h"
6 #include <ctype.h>
8 static PyMemberDef type_members[] = {
9 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
12 {"__weakrefoffset__", T_LONG,
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
21 static PyObject *
22 type_name(PyTypeObject *type, void *context)
24 const char *s;
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
29 Py_INCREF(et->ht_name);
30 return et->ht_name;
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
42 static int
43 type_set_name(PyTypeObject *type, PyObject *value, void *context)
45 PyHeapTypeObject* et;
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
63 if (strlen(PyString_AS_STRING(value))
64 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
70 et = (PyHeapTypeObject*)type;
72 Py_INCREF(value);
74 Py_DECREF(et->ht_name);
75 et->ht_name = value;
77 type->tp_name = PyString_AS_STRING(value);
79 return 0;
82 static PyObject *
83 type_module(PyTypeObject *type, void *context)
85 PyObject *mod;
86 char *s;
88 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
90 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
94 Py_XINCREF(mod);
95 return mod;
97 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
101 type->tp_name, (Py_ssize_t)(s - type->tp_name));
102 return PyString_FromString("__builtin__");
106 static int
107 type_set_module(PyTypeObject *type, PyObject *value, void *context)
109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
123 static PyObject *
124 type_get_bases(PyTypeObject *type, void *context)
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
130 static PyTypeObject *best_base(PyObject *);
131 static int mro_internal(PyTypeObject *);
132 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133 static int add_subclass(PyTypeObject*, PyTypeObject*);
134 static void remove_subclass(PyTypeObject *, PyTypeObject *);
135 static void update_all_slots(PyTypeObject *);
137 typedef int (*update_callback)(PyTypeObject *, void *);
138 static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
143 static int
144 mro_subclasses(PyTypeObject *type, PyObject* temp)
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
148 Py_ssize_t i, n;
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
166 return -1;
168 else {
169 PyObject* tuple;
170 tuple = PyTuple_Pack(2, subclass, old_mro);
171 Py_DECREF(old_mro);
172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
176 Py_DECREF(tuple);
178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
181 return 0;
184 static int
185 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
187 Py_ssize_t i;
188 int r = 0;
189 PyObject *ob, *temp;
190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
217 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
233 new_base = best_base(value);
235 if (!new_base) {
236 return -1;
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
242 Py_INCREF(new_base);
243 Py_INCREF(value);
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
249 type->tp_bases = value;
250 type->tp_base = new_base;
252 if (mro_internal(type) < 0) {
253 goto bail;
256 temp = PyList_New(0);
257 if (!temp)
258 goto bail;
260 r = mro_subclasses(type, temp);
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
272 Py_DECREF(temp);
273 goto bail;
276 Py_DECREF(temp);
278 /* any base that was in __bases__ but now isn't, we
279 need to remove |type| from its tp_subclasses.
280 conversely, any class now in __bases__ that wasn't
281 needs to have |type| added to its subclasses. */
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
302 update_all_slots(type);
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
308 return r;
310 bail:
311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
321 return -1;
324 static PyObject *
325 type_dict(PyTypeObject *type, void *context)
327 if (type->tp_dict == NULL) {
328 Py_INCREF(Py_None);
329 return Py_None;
331 return PyDictProxy_New(type->tp_dict);
334 static PyObject *
335 type_get_doc(PyTypeObject *type, void *context)
337 PyObject *result;
338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
339 return PyString_FromString(type->tp_doc);
340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
345 else if (result->ob_type->tp_descr_get) {
346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
349 else {
350 Py_INCREF(result);
352 return result;
355 static PyGetSetDef type_getsets[] = {
356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
359 {"__dict__", (getter)type_dict, NULL, NULL},
360 {"__doc__", (getter)type_get_doc, NULL, NULL},
364 static int
365 type_compare(PyObject *v, PyObject *w)
367 /* This is called with type objects only. So we
368 can just compare the addresses. */
369 Py_uintptr_t vv = (Py_uintptr_t)v;
370 Py_uintptr_t ww = (Py_uintptr_t)w;
371 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
374 static PyObject *
375 type_repr(PyTypeObject *type)
377 PyObject *mod, *name, *rtn;
378 char *kind;
380 mod = type_module(type, NULL);
381 if (mod == NULL)
382 PyErr_Clear();
383 else if (!PyString_Check(mod)) {
384 Py_DECREF(mod);
385 mod = NULL;
387 name = type_name(type, NULL);
388 if (name == NULL)
389 return NULL;
391 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
392 kind = "class";
393 else
394 kind = "type";
396 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
397 rtn = PyString_FromFormat("<%s '%s.%s'>",
398 kind,
399 PyString_AS_STRING(mod),
400 PyString_AS_STRING(name));
402 else
403 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
405 Py_XDECREF(mod);
406 Py_DECREF(name);
407 return rtn;
410 static PyObject *
411 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
413 PyObject *obj;
415 if (type->tp_new == NULL) {
416 PyErr_Format(PyExc_TypeError,
417 "cannot create '%.100s' instances",
418 type->tp_name);
419 return NULL;
422 obj = type->tp_new(type, args, kwds);
423 if (obj != NULL) {
424 /* Ugly exception: when the call was type(something),
425 don't call tp_init on the result. */
426 if (type == &PyType_Type &&
427 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
428 (kwds == NULL ||
429 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
430 return obj;
431 /* If the returned object is not an instance of type,
432 it won't be initialized. */
433 if (!PyType_IsSubtype(obj->ob_type, type))
434 return obj;
435 type = obj->ob_type;
436 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
437 type->tp_init != NULL &&
438 type->tp_init(obj, args, kwds) < 0) {
439 Py_DECREF(obj);
440 obj = NULL;
443 return obj;
446 PyObject *
447 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
449 PyObject *obj;
450 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
451 /* note that we need to add one, for the sentinel */
453 if (PyType_IS_GC(type))
454 obj = _PyObject_GC_Malloc(size);
455 else
456 obj = (PyObject *)PyObject_MALLOC(size);
458 if (obj == NULL)
459 return PyErr_NoMemory();
461 memset(obj, '\0', size);
463 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
464 Py_INCREF(type);
466 if (type->tp_itemsize == 0)
467 PyObject_INIT(obj, type);
468 else
469 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
471 if (PyType_IS_GC(type))
472 _PyObject_GC_TRACK(obj);
473 return obj;
476 PyObject *
477 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
479 return type->tp_alloc(type, 0);
482 /* Helpers for subtyping */
484 static int
485 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
487 Py_ssize_t i, n;
488 PyMemberDef *mp;
490 n = type->ob_size;
491 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
492 for (i = 0; i < n; i++, mp++) {
493 if (mp->type == T_OBJECT_EX) {
494 char *addr = (char *)self + mp->offset;
495 PyObject *obj = *(PyObject **)addr;
496 if (obj != NULL) {
497 int err = visit(obj, arg);
498 if (err)
499 return err;
503 return 0;
506 static int
507 subtype_traverse(PyObject *self, visitproc visit, void *arg)
509 PyTypeObject *type, *base;
510 traverseproc basetraverse;
512 /* Find the nearest base with a different tp_traverse,
513 and traverse slots while we're at it */
514 type = self->ob_type;
515 base = type;
516 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
517 if (base->ob_size) {
518 int err = traverse_slots(base, self, visit, arg);
519 if (err)
520 return err;
522 base = base->tp_base;
523 assert(base);
526 if (type->tp_dictoffset != base->tp_dictoffset) {
527 PyObject **dictptr = _PyObject_GetDictPtr(self);
528 if (dictptr && *dictptr)
529 Py_VISIT(*dictptr);
532 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
533 /* For a heaptype, the instances count as references
534 to the type. Traverse the type so the collector
535 can find cycles involving this link. */
536 Py_VISIT(type);
538 if (basetraverse)
539 return basetraverse(self, visit, arg);
540 return 0;
543 static void
544 clear_slots(PyTypeObject *type, PyObject *self)
546 Py_ssize_t i, n;
547 PyMemberDef *mp;
549 n = type->ob_size;
550 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
551 for (i = 0; i < n; i++, mp++) {
552 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
553 char *addr = (char *)self + mp->offset;
554 PyObject *obj = *(PyObject **)addr;
555 if (obj != NULL) {
556 *(PyObject **)addr = NULL;
557 Py_DECREF(obj);
563 static int
564 subtype_clear(PyObject *self)
566 PyTypeObject *type, *base;
567 inquiry baseclear;
569 /* Find the nearest base with a different tp_clear
570 and clear slots while we're at it */
571 type = self->ob_type;
572 base = type;
573 while ((baseclear = base->tp_clear) == subtype_clear) {
574 if (base->ob_size)
575 clear_slots(base, self);
576 base = base->tp_base;
577 assert(base);
580 /* There's no need to clear the instance dict (if any);
581 the collector will call its tp_clear handler. */
583 if (baseclear)
584 return baseclear(self);
585 return 0;
588 static void
589 subtype_dealloc(PyObject *self)
591 PyTypeObject *type, *base;
592 destructor basedealloc;
594 /* Extract the type; we expect it to be a heap type */
595 type = self->ob_type;
596 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
598 /* Test whether the type has GC exactly once */
600 if (!PyType_IS_GC(type)) {
601 /* It's really rare to find a dynamic type that doesn't have
602 GC; it can only happen when deriving from 'object' and not
603 adding any slots or instance variables. This allows
604 certain simplifications: there's no need to call
605 clear_slots(), or DECREF the dict, or clear weakrefs. */
607 /* Maybe call finalizer; exit early if resurrected */
608 if (type->tp_del) {
609 type->tp_del(self);
610 if (self->ob_refcnt > 0)
611 return;
614 /* Find the nearest base with a different tp_dealloc */
615 base = type;
616 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
617 assert(base->ob_size == 0);
618 base = base->tp_base;
619 assert(base);
622 /* Call the base tp_dealloc() */
623 assert(basedealloc);
624 basedealloc(self);
626 /* Can't reference self beyond this point */
627 Py_DECREF(type);
629 /* Done */
630 return;
633 /* We get here only if the type has GC */
635 /* UnTrack and re-Track around the trashcan macro, alas */
636 /* See explanation at end of function for full disclosure */
637 PyObject_GC_UnTrack(self);
638 ++_PyTrash_delete_nesting;
639 Py_TRASHCAN_SAFE_BEGIN(self);
640 --_PyTrash_delete_nesting;
641 /* DO NOT restore GC tracking at this point. weakref callbacks
642 * (if any, and whether directly here or indirectly in something we
643 * call) may trigger GC, and if self is tracked at that point, it
644 * will look like trash to GC and GC will try to delete self again.
647 /* Find the nearest base with a different tp_dealloc */
648 base = type;
649 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
650 base = base->tp_base;
651 assert(base);
654 /* If we added a weaklist, we clear it. Do this *before* calling
655 the finalizer (__del__), clearing slots, or clearing the instance
656 dict. */
658 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
659 PyObject_ClearWeakRefs(self);
661 /* Maybe call finalizer; exit early if resurrected */
662 if (type->tp_del) {
663 _PyObject_GC_TRACK(self);
664 type->tp_del(self);
665 if (self->ob_refcnt > 0)
666 goto endlabel; /* resurrected */
667 else
668 _PyObject_GC_UNTRACK(self);
669 /* New weakrefs could be created during the finalizer call.
670 If this occurs, clear them out without calling their
671 finalizers since they might rely on part of the object
672 being finalized that has already been destroyed. */
673 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
674 /* Modeled after GET_WEAKREFS_LISTPTR() */
675 PyWeakReference **list = (PyWeakReference **) \
676 PyObject_GET_WEAKREFS_LISTPTR(self);
677 while (*list)
678 _PyWeakref_ClearRef(*list);
682 /* Clear slots up to the nearest base with a different tp_dealloc */
683 base = type;
684 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
685 if (base->ob_size)
686 clear_slots(base, self);
687 base = base->tp_base;
688 assert(base);
691 /* If we added a dict, DECREF it */
692 if (type->tp_dictoffset && !base->tp_dictoffset) {
693 PyObject **dictptr = _PyObject_GetDictPtr(self);
694 if (dictptr != NULL) {
695 PyObject *dict = *dictptr;
696 if (dict != NULL) {
697 Py_DECREF(dict);
698 *dictptr = NULL;
703 /* Call the base tp_dealloc(); first retrack self if
704 * basedealloc knows about gc.
706 if (PyType_IS_GC(base))
707 _PyObject_GC_TRACK(self);
708 assert(basedealloc);
709 basedealloc(self);
711 /* Can't reference self beyond this point */
712 Py_DECREF(type);
714 endlabel:
715 ++_PyTrash_delete_nesting;
716 Py_TRASHCAN_SAFE_END(self);
717 --_PyTrash_delete_nesting;
719 /* Explanation of the weirdness around the trashcan macros:
721 Q. What do the trashcan macros do?
723 A. Read the comment titled "Trashcan mechanism" in object.h.
724 For one, this explains why there must be a call to GC-untrack
725 before the trashcan begin macro. Without understanding the
726 trashcan code, the answers to the following questions don't make
727 sense.
729 Q. Why do we GC-untrack before the trashcan and then immediately
730 GC-track again afterward?
732 A. In the case that the base class is GC-aware, the base class
733 probably GC-untracks the object. If it does that using the
734 UNTRACK macro, this will crash when the object is already
735 untracked. Because we don't know what the base class does, the
736 only safe thing is to make sure the object is tracked when we
737 call the base class dealloc. But... The trashcan begin macro
738 requires that the object is *untracked* before it is called. So
739 the dance becomes:
741 GC untrack
742 trashcan begin
743 GC track
745 Q. Why did the last question say "immediately GC-track again"?
746 It's nowhere near immediately.
748 A. Because the code *used* to re-track immediately. Bad Idea.
749 self has a refcount of 0, and if gc ever gets its hands on it
750 (which can happen if any weakref callback gets invoked), it
751 looks like trash to gc too, and gc also tries to delete self
752 then. But we're already deleting self. Double dealloction is
753 a subtle disaster.
755 Q. Why the bizarre (net-zero) manipulation of
756 _PyTrash_delete_nesting around the trashcan macros?
758 A. Some base classes (e.g. list) also use the trashcan mechanism.
759 The following scenario used to be possible:
761 - suppose the trashcan level is one below the trashcan limit
763 - subtype_dealloc() is called
765 - the trashcan limit is not yet reached, so the trashcan level
766 is incremented and the code between trashcan begin and end is
767 executed
769 - this destroys much of the object's contents, including its
770 slots and __dict__
772 - basedealloc() is called; this is really list_dealloc(), or
773 some other type which also uses the trashcan macros
775 - the trashcan limit is now reached, so the object is put on the
776 trashcan's to-be-deleted-later list
778 - basedealloc() returns
780 - subtype_dealloc() decrefs the object's type
782 - subtype_dealloc() returns
784 - later, the trashcan code starts deleting the objects from its
785 to-be-deleted-later list
787 - subtype_dealloc() is called *AGAIN* for the same object
789 - at the very least (if the destroyed slots and __dict__ don't
790 cause problems) the object's type gets decref'ed a second
791 time, which is *BAD*!!!
793 The remedy is to make sure that if the code between trashcan
794 begin and end in subtype_dealloc() is called, the code between
795 trashcan begin and end in basedealloc() will also be called.
796 This is done by decrementing the level after passing into the
797 trashcan block, and incrementing it just before leaving the
798 block.
800 But now it's possible that a chain of objects consisting solely
801 of objects whose deallocator is subtype_dealloc() will defeat
802 the trashcan mechanism completely: the decremented level means
803 that the effective level never reaches the limit. Therefore, we
804 *increment* the level *before* entering the trashcan block, and
805 matchingly decrement it after leaving. This means the trashcan
806 code will trigger a little early, but that's no big deal.
808 Q. Are there any live examples of code in need of all this
809 complexity?
811 A. Yes. See SF bug 668433 for code that crashed (when Python was
812 compiled in debug mode) before the trashcan level manipulations
813 were added. For more discussion, see SF patches 581742, 575073
814 and bug 574207.
818 static PyTypeObject *solid_base(PyTypeObject *type);
820 /* type test with subclassing support */
823 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
825 PyObject *mro;
827 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
828 return b == a || b == &PyBaseObject_Type;
830 mro = a->tp_mro;
831 if (mro != NULL) {
832 /* Deal with multiple inheritance without recursion
833 by walking the MRO tuple */
834 Py_ssize_t i, n;
835 assert(PyTuple_Check(mro));
836 n = PyTuple_GET_SIZE(mro);
837 for (i = 0; i < n; i++) {
838 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
839 return 1;
841 return 0;
843 else {
844 /* a is not completely initilized yet; follow tp_base */
845 do {
846 if (a == b)
847 return 1;
848 a = a->tp_base;
849 } while (a != NULL);
850 return b == &PyBaseObject_Type;
854 /* Internal routines to do a method lookup in the type
855 without looking in the instance dictionary
856 (so we can't use PyObject_GetAttr) but still binding
857 it to the instance. The arguments are the object,
858 the method name as a C string, and the address of a
859 static variable used to cache the interned Python string.
861 Two variants:
863 - lookup_maybe() returns NULL without raising an exception
864 when the _PyType_Lookup() call fails;
866 - lookup_method() always raises an exception upon errors.
869 static PyObject *
870 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
872 PyObject *res;
874 if (*attrobj == NULL) {
875 *attrobj = PyString_InternFromString(attrstr);
876 if (*attrobj == NULL)
877 return NULL;
879 res = _PyType_Lookup(self->ob_type, *attrobj);
880 if (res != NULL) {
881 descrgetfunc f;
882 if ((f = res->ob_type->tp_descr_get) == NULL)
883 Py_INCREF(res);
884 else
885 res = f(res, self, (PyObject *)(self->ob_type));
887 return res;
890 static PyObject *
891 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
893 PyObject *res = lookup_maybe(self, attrstr, attrobj);
894 if (res == NULL && !PyErr_Occurred())
895 PyErr_SetObject(PyExc_AttributeError, *attrobj);
896 return res;
899 /* A variation of PyObject_CallMethod that uses lookup_method()
900 instead of PyObject_GetAttrString(). This uses the same convention
901 as lookup_method to cache the interned name string object. */
903 static PyObject *
904 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
906 va_list va;
907 PyObject *args, *func = 0, *retval;
908 va_start(va, format);
910 func = lookup_maybe(o, name, nameobj);
911 if (func == NULL) {
912 va_end(va);
913 if (!PyErr_Occurred())
914 PyErr_SetObject(PyExc_AttributeError, *nameobj);
915 return NULL;
918 if (format && *format)
919 args = Py_VaBuildValue(format, va);
920 else
921 args = PyTuple_New(0);
923 va_end(va);
925 if (args == NULL)
926 return NULL;
928 assert(PyTuple_Check(args));
929 retval = PyObject_Call(func, args, NULL);
931 Py_DECREF(args);
932 Py_DECREF(func);
934 return retval;
937 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
939 static PyObject *
940 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
942 va_list va;
943 PyObject *args, *func = 0, *retval;
944 va_start(va, format);
946 func = lookup_maybe(o, name, nameobj);
947 if (func == NULL) {
948 va_end(va);
949 if (!PyErr_Occurred()) {
950 Py_INCREF(Py_NotImplemented);
951 return Py_NotImplemented;
953 return NULL;
956 if (format && *format)
957 args = Py_VaBuildValue(format, va);
958 else
959 args = PyTuple_New(0);
961 va_end(va);
963 if (args == NULL)
964 return NULL;
966 assert(PyTuple_Check(args));
967 retval = PyObject_Call(func, args, NULL);
969 Py_DECREF(args);
970 Py_DECREF(func);
972 return retval;
975 static int
976 fill_classic_mro(PyObject *mro, PyObject *cls)
978 PyObject *bases, *base;
979 Py_ssize_t i, n;
981 assert(PyList_Check(mro));
982 assert(PyClass_Check(cls));
983 i = PySequence_Contains(mro, cls);
984 if (i < 0)
985 return -1;
986 if (!i) {
987 if (PyList_Append(mro, cls) < 0)
988 return -1;
990 bases = ((PyClassObject *)cls)->cl_bases;
991 assert(bases && PyTuple_Check(bases));
992 n = PyTuple_GET_SIZE(bases);
993 for (i = 0; i < n; i++) {
994 base = PyTuple_GET_ITEM(bases, i);
995 if (fill_classic_mro(mro, base) < 0)
996 return -1;
998 return 0;
1001 static PyObject *
1002 classic_mro(PyObject *cls)
1004 PyObject *mro;
1006 assert(PyClass_Check(cls));
1007 mro = PyList_New(0);
1008 if (mro != NULL) {
1009 if (fill_classic_mro(mro, cls) == 0)
1010 return mro;
1011 Py_DECREF(mro);
1013 return NULL;
1017 Method resolution order algorithm C3 described in
1018 "A Monotonic Superclass Linearization for Dylan",
1019 by Kim Barrett, Bob Cassel, Paul Haahr,
1020 David A. Moon, Keith Playford, and P. Tucker Withington.
1021 (OOPSLA 1996)
1023 Some notes about the rules implied by C3:
1025 No duplicate bases.
1026 It isn't legal to repeat a class in a list of base classes.
1028 The next three properties are the 3 constraints in "C3".
1030 Local precendece order.
1031 If A precedes B in C's MRO, then A will precede B in the MRO of all
1032 subclasses of C.
1034 Monotonicity.
1035 The MRO of a class must be an extension without reordering of the
1036 MRO of each of its superclasses.
1038 Extended Precedence Graph (EPG).
1039 Linearization is consistent if there is a path in the EPG from
1040 each class to all its successors in the linearization. See
1041 the paper for definition of EPG.
1044 static int
1045 tail_contains(PyObject *list, int whence, PyObject *o) {
1046 Py_ssize_t j, size;
1047 size = PyList_GET_SIZE(list);
1049 for (j = whence+1; j < size; j++) {
1050 if (PyList_GET_ITEM(list, j) == o)
1051 return 1;
1053 return 0;
1056 static PyObject *
1057 class_name(PyObject *cls)
1059 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1060 if (name == NULL) {
1061 PyErr_Clear();
1062 Py_XDECREF(name);
1063 name = PyObject_Repr(cls);
1065 if (name == NULL)
1066 return NULL;
1067 if (!PyString_Check(name)) {
1068 Py_DECREF(name);
1069 return NULL;
1071 return name;
1074 static int
1075 check_duplicates(PyObject *list)
1077 Py_ssize_t i, j, n;
1078 /* Let's use a quadratic time algorithm,
1079 assuming that the bases lists is short.
1081 n = PyList_GET_SIZE(list);
1082 for (i = 0; i < n; i++) {
1083 PyObject *o = PyList_GET_ITEM(list, i);
1084 for (j = i + 1; j < n; j++) {
1085 if (PyList_GET_ITEM(list, j) == o) {
1086 o = class_name(o);
1087 PyErr_Format(PyExc_TypeError,
1088 "duplicate base class %s",
1089 o ? PyString_AS_STRING(o) : "?");
1090 Py_XDECREF(o);
1091 return -1;
1095 return 0;
1098 /* Raise a TypeError for an MRO order disagreement.
1100 It's hard to produce a good error message. In the absence of better
1101 insight into error reporting, report the classes that were candidates
1102 to be put next into the MRO. There is some conflict between the
1103 order in which they should be put in the MRO, but it's hard to
1104 diagnose what constraint can't be satisfied.
1107 static void
1108 set_mro_error(PyObject *to_merge, int *remain)
1110 Py_ssize_t i, n, off, to_merge_size;
1111 char buf[1000];
1112 PyObject *k, *v;
1113 PyObject *set = PyDict_New();
1114 if (!set) return;
1116 to_merge_size = PyList_GET_SIZE(to_merge);
1117 for (i = 0; i < to_merge_size; i++) {
1118 PyObject *L = PyList_GET_ITEM(to_merge, i);
1119 if (remain[i] < PyList_GET_SIZE(L)) {
1120 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1121 if (PyDict_SetItem(set, c, Py_None) < 0) {
1122 Py_DECREF(set);
1123 return;
1127 n = PyDict_Size(set);
1129 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1130 consistent method resolution\norder (MRO) for bases");
1131 i = 0;
1132 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1133 PyObject *name = class_name(k);
1134 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1135 name ? PyString_AS_STRING(name) : "?");
1136 Py_XDECREF(name);
1137 if (--n && (size_t)(off+1) < sizeof(buf)) {
1138 buf[off++] = ',';
1139 buf[off] = '\0';
1142 PyErr_SetString(PyExc_TypeError, buf);
1143 Py_DECREF(set);
1146 static int
1147 pmerge(PyObject *acc, PyObject* to_merge) {
1148 Py_ssize_t i, j, to_merge_size, empty_cnt;
1149 int *remain;
1150 int ok;
1152 to_merge_size = PyList_GET_SIZE(to_merge);
1154 /* remain stores an index into each sublist of to_merge.
1155 remain[i] is the index of the next base in to_merge[i]
1156 that is not included in acc.
1158 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1159 if (remain == NULL)
1160 return -1;
1161 for (i = 0; i < to_merge_size; i++)
1162 remain[i] = 0;
1164 again:
1165 empty_cnt = 0;
1166 for (i = 0; i < to_merge_size; i++) {
1167 PyObject *candidate;
1169 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1171 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1172 empty_cnt++;
1173 continue;
1176 /* Choose next candidate for MRO.
1178 The input sequences alone can determine the choice.
1179 If not, choose the class which appears in the MRO
1180 of the earliest direct superclass of the new class.
1183 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1184 for (j = 0; j < to_merge_size; j++) {
1185 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1186 if (tail_contains(j_lst, remain[j], candidate)) {
1187 goto skip; /* continue outer loop */
1190 ok = PyList_Append(acc, candidate);
1191 if (ok < 0) {
1192 PyMem_Free(remain);
1193 return -1;
1195 for (j = 0; j < to_merge_size; j++) {
1196 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1197 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1198 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1199 remain[j]++;
1202 goto again;
1203 skip: ;
1206 if (empty_cnt == to_merge_size) {
1207 PyMem_FREE(remain);
1208 return 0;
1210 set_mro_error(to_merge, remain);
1211 PyMem_FREE(remain);
1212 return -1;
1215 static PyObject *
1216 mro_implementation(PyTypeObject *type)
1218 Py_ssize_t i, n;
1219 int ok;
1220 PyObject *bases, *result;
1221 PyObject *to_merge, *bases_aslist;
1223 if(type->tp_dict == NULL) {
1224 if(PyType_Ready(type) < 0)
1225 return NULL;
1228 /* Find a superclass linearization that honors the constraints
1229 of the explicit lists of bases and the constraints implied by
1230 each base class.
1232 to_merge is a list of lists, where each list is a superclass
1233 linearization implied by a base class. The last element of
1234 to_merge is the declared list of bases.
1237 bases = type->tp_bases;
1238 n = PyTuple_GET_SIZE(bases);
1240 to_merge = PyList_New(n+1);
1241 if (to_merge == NULL)
1242 return NULL;
1244 for (i = 0; i < n; i++) {
1245 PyObject *base = PyTuple_GET_ITEM(bases, i);
1246 PyObject *parentMRO;
1247 if (PyType_Check(base))
1248 parentMRO = PySequence_List(
1249 ((PyTypeObject*)base)->tp_mro);
1250 else
1251 parentMRO = classic_mro(base);
1252 if (parentMRO == NULL) {
1253 Py_DECREF(to_merge);
1254 return NULL;
1257 PyList_SET_ITEM(to_merge, i, parentMRO);
1260 bases_aslist = PySequence_List(bases);
1261 if (bases_aslist == NULL) {
1262 Py_DECREF(to_merge);
1263 return NULL;
1265 /* This is just a basic sanity check. */
1266 if (check_duplicates(bases_aslist) < 0) {
1267 Py_DECREF(to_merge);
1268 Py_DECREF(bases_aslist);
1269 return NULL;
1271 PyList_SET_ITEM(to_merge, n, bases_aslist);
1273 result = Py_BuildValue("[O]", (PyObject *)type);
1274 if (result == NULL) {
1275 Py_DECREF(to_merge);
1276 return NULL;
1279 ok = pmerge(result, to_merge);
1280 Py_DECREF(to_merge);
1281 if (ok < 0) {
1282 Py_DECREF(result);
1283 return NULL;
1286 return result;
1289 static PyObject *
1290 mro_external(PyObject *self)
1292 PyTypeObject *type = (PyTypeObject *)self;
1294 return mro_implementation(type);
1297 static int
1298 mro_internal(PyTypeObject *type)
1300 PyObject *mro, *result, *tuple;
1301 int checkit = 0;
1303 if (type->ob_type == &PyType_Type) {
1304 result = mro_implementation(type);
1306 else {
1307 static PyObject *mro_str;
1308 checkit = 1;
1309 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1310 if (mro == NULL)
1311 return -1;
1312 result = PyObject_CallObject(mro, NULL);
1313 Py_DECREF(mro);
1315 if (result == NULL)
1316 return -1;
1317 tuple = PySequence_Tuple(result);
1318 Py_DECREF(result);
1319 if (tuple == NULL)
1320 return -1;
1321 if (checkit) {
1322 Py_ssize_t i, len;
1323 PyObject *cls;
1324 PyTypeObject *solid;
1326 solid = solid_base(type);
1328 len = PyTuple_GET_SIZE(tuple);
1330 for (i = 0; i < len; i++) {
1331 PyTypeObject *t;
1332 cls = PyTuple_GET_ITEM(tuple, i);
1333 if (PyClass_Check(cls))
1334 continue;
1335 else if (!PyType_Check(cls)) {
1336 PyErr_Format(PyExc_TypeError,
1337 "mro() returned a non-class ('%.500s')",
1338 cls->ob_type->tp_name);
1339 Py_DECREF(tuple);
1340 return -1;
1342 t = (PyTypeObject*)cls;
1343 if (!PyType_IsSubtype(solid, solid_base(t))) {
1344 PyErr_Format(PyExc_TypeError,
1345 "mro() returned base with unsuitable layout ('%.500s')",
1346 t->tp_name);
1347 Py_DECREF(tuple);
1348 return -1;
1352 type->tp_mro = tuple;
1353 return 0;
1357 /* Calculate the best base amongst multiple base classes.
1358 This is the first one that's on the path to the "solid base". */
1360 static PyTypeObject *
1361 best_base(PyObject *bases)
1363 Py_ssize_t i, n;
1364 PyTypeObject *base, *winner, *candidate, *base_i;
1365 PyObject *base_proto;
1367 assert(PyTuple_Check(bases));
1368 n = PyTuple_GET_SIZE(bases);
1369 assert(n > 0);
1370 base = NULL;
1371 winner = NULL;
1372 for (i = 0; i < n; i++) {
1373 base_proto = PyTuple_GET_ITEM(bases, i);
1374 if (PyClass_Check(base_proto))
1375 continue;
1376 if (!PyType_Check(base_proto)) {
1377 PyErr_SetString(
1378 PyExc_TypeError,
1379 "bases must be types");
1380 return NULL;
1382 base_i = (PyTypeObject *)base_proto;
1383 if (base_i->tp_dict == NULL) {
1384 if (PyType_Ready(base_i) < 0)
1385 return NULL;
1387 candidate = solid_base(base_i);
1388 if (winner == NULL) {
1389 winner = candidate;
1390 base = base_i;
1392 else if (PyType_IsSubtype(winner, candidate))
1394 else if (PyType_IsSubtype(candidate, winner)) {
1395 winner = candidate;
1396 base = base_i;
1398 else {
1399 PyErr_SetString(
1400 PyExc_TypeError,
1401 "multiple bases have "
1402 "instance lay-out conflict");
1403 return NULL;
1406 if (base == NULL)
1407 PyErr_SetString(PyExc_TypeError,
1408 "a new-style class can't have only classic bases");
1409 return base;
1412 static int
1413 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1415 size_t t_size = type->tp_basicsize;
1416 size_t b_size = base->tp_basicsize;
1418 assert(t_size >= b_size); /* Else type smaller than base! */
1419 if (type->tp_itemsize || base->tp_itemsize) {
1420 /* If itemsize is involved, stricter rules */
1421 return t_size != b_size ||
1422 type->tp_itemsize != base->tp_itemsize;
1424 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1425 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1426 t_size -= sizeof(PyObject *);
1427 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1428 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1429 t_size -= sizeof(PyObject *);
1431 return t_size != b_size;
1434 static PyTypeObject *
1435 solid_base(PyTypeObject *type)
1437 PyTypeObject *base;
1439 if (type->tp_base)
1440 base = solid_base(type->tp_base);
1441 else
1442 base = &PyBaseObject_Type;
1443 if (extra_ivars(type, base))
1444 return type;
1445 else
1446 return base;
1449 static void object_dealloc(PyObject *);
1450 static int object_init(PyObject *, PyObject *, PyObject *);
1451 static int update_slot(PyTypeObject *, PyObject *);
1452 static void fixup_slot_dispatchers(PyTypeObject *);
1454 static PyObject *
1455 subtype_dict(PyObject *obj, void *context)
1457 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1458 PyObject *dict;
1460 if (dictptr == NULL) {
1461 PyErr_SetString(PyExc_AttributeError,
1462 "This object has no __dict__");
1463 return NULL;
1465 dict = *dictptr;
1466 if (dict == NULL)
1467 *dictptr = dict = PyDict_New();
1468 Py_XINCREF(dict);
1469 return dict;
1472 static int
1473 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1475 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1476 PyObject *dict;
1478 if (dictptr == NULL) {
1479 PyErr_SetString(PyExc_AttributeError,
1480 "This object has no __dict__");
1481 return -1;
1483 if (value != NULL && !PyDict_Check(value)) {
1484 PyErr_Format(PyExc_TypeError,
1485 "__dict__ must be set to a dictionary, "
1486 "not a '%.200s'", value->ob_type->tp_name);
1487 return -1;
1489 dict = *dictptr;
1490 Py_XINCREF(value);
1491 *dictptr = value;
1492 Py_XDECREF(dict);
1493 return 0;
1496 static PyObject *
1497 subtype_getweakref(PyObject *obj, void *context)
1499 PyObject **weaklistptr;
1500 PyObject *result;
1502 if (obj->ob_type->tp_weaklistoffset == 0) {
1503 PyErr_SetString(PyExc_AttributeError,
1504 "This object has no __weakref__");
1505 return NULL;
1507 assert(obj->ob_type->tp_weaklistoffset > 0);
1508 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1509 (size_t)(obj->ob_type->tp_basicsize));
1510 weaklistptr = (PyObject **)
1511 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1512 if (*weaklistptr == NULL)
1513 result = Py_None;
1514 else
1515 result = *weaklistptr;
1516 Py_INCREF(result);
1517 return result;
1520 /* Three variants on the subtype_getsets list. */
1522 static PyGetSetDef subtype_getsets_full[] = {
1523 {"__dict__", subtype_dict, subtype_setdict,
1524 PyDoc_STR("dictionary for instance variables (if defined)")},
1525 {"__weakref__", subtype_getweakref, NULL,
1526 PyDoc_STR("list of weak references to the object (if defined)")},
1530 static PyGetSetDef subtype_getsets_dict_only[] = {
1531 {"__dict__", subtype_dict, subtype_setdict,
1532 PyDoc_STR("dictionary for instance variables (if defined)")},
1536 static PyGetSetDef subtype_getsets_weakref_only[] = {
1537 {"__weakref__", subtype_getweakref, NULL,
1538 PyDoc_STR("list of weak references to the object (if defined)")},
1542 static int
1543 valid_identifier(PyObject *s)
1545 unsigned char *p;
1546 Py_ssize_t i, n;
1548 if (!PyString_Check(s)) {
1549 PyErr_Format(PyExc_TypeError,
1550 "__slots__ items must be strings, not '%.200s'",
1551 s->ob_type->tp_name);
1552 return 0;
1554 p = (unsigned char *) PyString_AS_STRING(s);
1555 n = PyString_GET_SIZE(s);
1556 /* We must reject an empty name. As a hack, we bump the
1557 length to 1 so that the loop will balk on the trailing \0. */
1558 if (n == 0)
1559 n = 1;
1560 for (i = 0; i < n; i++, p++) {
1561 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1562 PyErr_SetString(PyExc_TypeError,
1563 "__slots__ must be identifiers");
1564 return 0;
1567 return 1;
1570 #ifdef Py_USING_UNICODE
1571 /* Replace Unicode objects in slots. */
1573 static PyObject *
1574 _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
1576 PyObject *tmp = NULL;
1577 PyObject *slot_name, *new_name;
1578 Py_ssize_t i;
1580 for (i = 0; i < nslots; i++) {
1581 if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
1582 if (tmp == NULL) {
1583 tmp = PySequence_List(slots);
1584 if (tmp == NULL)
1585 return NULL;
1587 new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
1588 NULL);
1589 if (new_name == NULL) {
1590 Py_DECREF(tmp);
1591 return NULL;
1593 Py_INCREF(new_name);
1594 PyList_SET_ITEM(tmp, i, new_name);
1595 Py_DECREF(slot_name);
1598 if (tmp != NULL) {
1599 slots = PyList_AsTuple(tmp);
1600 Py_DECREF(tmp);
1602 return slots;
1604 #endif
1606 static PyObject *
1607 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1609 PyObject *name, *bases, *dict;
1610 static char *kwlist[] = {"name", "bases", "dict", 0};
1611 PyObject *slots, *tmp, *newslots;
1612 PyTypeObject *type, *base, *tmptype, *winner;
1613 PyHeapTypeObject *et;
1614 PyMemberDef *mp;
1615 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
1616 int j, may_add_dict, may_add_weak;
1618 assert(args != NULL && PyTuple_Check(args));
1619 assert(kwds == NULL || PyDict_Check(kwds));
1621 /* Special case: type(x) should return x->ob_type */
1623 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1624 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1626 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1627 PyObject *x = PyTuple_GET_ITEM(args, 0);
1628 Py_INCREF(x->ob_type);
1629 return (PyObject *) x->ob_type;
1632 /* SF bug 475327 -- if that didn't trigger, we need 3
1633 arguments. but PyArg_ParseTupleAndKeywords below may give
1634 a msg saying type() needs exactly 3. */
1635 if (nargs + nkwds != 3) {
1636 PyErr_SetString(PyExc_TypeError,
1637 "type() takes 1 or 3 arguments");
1638 return NULL;
1642 /* Check arguments: (name, bases, dict) */
1643 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1644 &name,
1645 &PyTuple_Type, &bases,
1646 &PyDict_Type, &dict))
1647 return NULL;
1649 /* Determine the proper metatype to deal with this,
1650 and check for metatype conflicts while we're at it.
1651 Note that if some other metatype wins to contract,
1652 it's possible that its instances are not types. */
1653 nbases = PyTuple_GET_SIZE(bases);
1654 winner = metatype;
1655 for (i = 0; i < nbases; i++) {
1656 tmp = PyTuple_GET_ITEM(bases, i);
1657 tmptype = tmp->ob_type;
1658 if (tmptype == &PyClass_Type)
1659 continue; /* Special case classic classes */
1660 if (PyType_IsSubtype(winner, tmptype))
1661 continue;
1662 if (PyType_IsSubtype(tmptype, winner)) {
1663 winner = tmptype;
1664 continue;
1666 PyErr_SetString(PyExc_TypeError,
1667 "metaclass conflict: "
1668 "the metaclass of a derived class "
1669 "must be a (non-strict) subclass "
1670 "of the metaclasses of all its bases");
1671 return NULL;
1673 if (winner != metatype) {
1674 if (winner->tp_new != type_new) /* Pass it to the winner */
1675 return winner->tp_new(winner, args, kwds);
1676 metatype = winner;
1679 /* Adjust for empty tuple bases */
1680 if (nbases == 0) {
1681 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1682 if (bases == NULL)
1683 return NULL;
1684 nbases = 1;
1686 else
1687 Py_INCREF(bases);
1689 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1691 /* Calculate best base, and check that all bases are type objects */
1692 base = best_base(bases);
1693 if (base == NULL) {
1694 Py_DECREF(bases);
1695 return NULL;
1697 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1698 PyErr_Format(PyExc_TypeError,
1699 "type '%.100s' is not an acceptable base type",
1700 base->tp_name);
1701 Py_DECREF(bases);
1702 return NULL;
1705 /* Check for a __slots__ sequence variable in dict, and count it */
1706 slots = PyDict_GetItemString(dict, "__slots__");
1707 nslots = 0;
1708 add_dict = 0;
1709 add_weak = 0;
1710 may_add_dict = base->tp_dictoffset == 0;
1711 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1712 if (slots == NULL) {
1713 if (may_add_dict) {
1714 add_dict++;
1716 if (may_add_weak) {
1717 add_weak++;
1720 else {
1721 /* Have slots */
1723 /* Make it into a tuple */
1724 if (PyString_Check(slots))
1725 slots = PyTuple_Pack(1, slots);
1726 else
1727 slots = PySequence_Tuple(slots);
1728 if (slots == NULL) {
1729 Py_DECREF(bases);
1730 return NULL;
1732 assert(PyTuple_Check(slots));
1734 /* Are slots allowed? */
1735 nslots = PyTuple_GET_SIZE(slots);
1736 if (nslots > 0 && base->tp_itemsize != 0) {
1737 PyErr_Format(PyExc_TypeError,
1738 "nonempty __slots__ "
1739 "not supported for subtype of '%s'",
1740 base->tp_name);
1741 bad_slots:
1742 Py_DECREF(bases);
1743 Py_DECREF(slots);
1744 return NULL;
1747 #ifdef Py_USING_UNICODE
1748 tmp = _unicode_to_string(slots, nslots);
1749 if (tmp == NULL)
1750 goto bad_slots;
1751 if (tmp != slots) {
1752 Py_DECREF(slots);
1753 slots = tmp;
1755 #endif
1756 /* Check for valid slot names and two special cases */
1757 for (i = 0; i < nslots; i++) {
1758 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1759 char *s;
1760 if (!valid_identifier(tmp))
1761 goto bad_slots;
1762 assert(PyString_Check(tmp));
1763 s = PyString_AS_STRING(tmp);
1764 if (strcmp(s, "__dict__") == 0) {
1765 if (!may_add_dict || add_dict) {
1766 PyErr_SetString(PyExc_TypeError,
1767 "__dict__ slot disallowed: "
1768 "we already got one");
1769 goto bad_slots;
1771 add_dict++;
1773 if (strcmp(s, "__weakref__") == 0) {
1774 if (!may_add_weak || add_weak) {
1775 PyErr_SetString(PyExc_TypeError,
1776 "__weakref__ slot disallowed: "
1777 "either we already got one, "
1778 "or __itemsize__ != 0");
1779 goto bad_slots;
1781 add_weak++;
1785 /* Copy slots into yet another tuple, demangling names */
1786 newslots = PyTuple_New(nslots - add_dict - add_weak);
1787 if (newslots == NULL)
1788 goto bad_slots;
1789 for (i = j = 0; i < nslots; i++) {
1790 char *s;
1791 tmp = PyTuple_GET_ITEM(slots, i);
1792 s = PyString_AS_STRING(tmp);
1793 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1794 (add_weak && strcmp(s, "__weakref__") == 0))
1795 continue;
1796 tmp =_Py_Mangle(name, tmp);
1797 if (!tmp)
1798 goto bad_slots;
1799 PyTuple_SET_ITEM(newslots, j, tmp);
1800 j++;
1802 assert(j == nslots - add_dict - add_weak);
1803 nslots = j;
1804 Py_DECREF(slots);
1805 slots = newslots;
1807 /* Secondary bases may provide weakrefs or dict */
1808 if (nbases > 1 &&
1809 ((may_add_dict && !add_dict) ||
1810 (may_add_weak && !add_weak))) {
1811 for (i = 0; i < nbases; i++) {
1812 tmp = PyTuple_GET_ITEM(bases, i);
1813 if (tmp == (PyObject *)base)
1814 continue; /* Skip primary base */
1815 if (PyClass_Check(tmp)) {
1816 /* Classic base class provides both */
1817 if (may_add_dict && !add_dict)
1818 add_dict++;
1819 if (may_add_weak && !add_weak)
1820 add_weak++;
1821 break;
1823 assert(PyType_Check(tmp));
1824 tmptype = (PyTypeObject *)tmp;
1825 if (may_add_dict && !add_dict &&
1826 tmptype->tp_dictoffset != 0)
1827 add_dict++;
1828 if (may_add_weak && !add_weak &&
1829 tmptype->tp_weaklistoffset != 0)
1830 add_weak++;
1831 if (may_add_dict && !add_dict)
1832 continue;
1833 if (may_add_weak && !add_weak)
1834 continue;
1835 /* Nothing more to check */
1836 break;
1841 /* XXX From here until type is safely allocated,
1842 "return NULL" may leak slots! */
1844 /* Allocate the type object */
1845 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1846 if (type == NULL) {
1847 Py_XDECREF(slots);
1848 Py_DECREF(bases);
1849 return NULL;
1852 /* Keep name and slots alive in the extended type object */
1853 et = (PyHeapTypeObject *)type;
1854 Py_INCREF(name);
1855 et->ht_name = name;
1856 et->ht_slots = slots;
1858 /* Initialize tp_flags */
1859 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1860 Py_TPFLAGS_BASETYPE;
1861 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1862 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1864 /* It's a new-style number unless it specifically inherits any
1865 old-style numeric behavior */
1866 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1867 (base->tp_as_number == NULL))
1868 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1870 /* Initialize essential fields */
1871 type->tp_as_number = &et->as_number;
1872 type->tp_as_sequence = &et->as_sequence;
1873 type->tp_as_mapping = &et->as_mapping;
1874 type->tp_as_buffer = &et->as_buffer;
1875 type->tp_name = PyString_AS_STRING(name);
1877 /* Set tp_base and tp_bases */
1878 type->tp_bases = bases;
1879 Py_INCREF(base);
1880 type->tp_base = base;
1882 /* Initialize tp_dict from passed-in dict */
1883 type->tp_dict = dict = PyDict_Copy(dict);
1884 if (dict == NULL) {
1885 Py_DECREF(type);
1886 return NULL;
1889 /* Set __module__ in the dict */
1890 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1891 tmp = PyEval_GetGlobals();
1892 if (tmp != NULL) {
1893 tmp = PyDict_GetItemString(tmp, "__name__");
1894 if (tmp != NULL) {
1895 if (PyDict_SetItemString(dict, "__module__",
1896 tmp) < 0)
1897 return NULL;
1902 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1903 and is a string. The __doc__ accessor will first look for tp_doc;
1904 if that fails, it will still look into __dict__.
1907 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1908 if (doc != NULL && PyString_Check(doc)) {
1909 const size_t n = (size_t)PyString_GET_SIZE(doc);
1910 char *tp_doc = (char *)PyObject_MALLOC(n+1);
1911 if (tp_doc == NULL) {
1912 Py_DECREF(type);
1913 return NULL;
1915 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1916 type->tp_doc = tp_doc;
1920 /* Special-case __new__: if it's a plain function,
1921 make it a static function */
1922 tmp = PyDict_GetItemString(dict, "__new__");
1923 if (tmp != NULL && PyFunction_Check(tmp)) {
1924 tmp = PyStaticMethod_New(tmp);
1925 if (tmp == NULL) {
1926 Py_DECREF(type);
1927 return NULL;
1929 PyDict_SetItemString(dict, "__new__", tmp);
1930 Py_DECREF(tmp);
1933 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1934 mp = PyHeapType_GET_MEMBERS(et);
1935 slotoffset = base->tp_basicsize;
1936 if (slots != NULL) {
1937 for (i = 0; i < nslots; i++, mp++) {
1938 mp->name = PyString_AS_STRING(
1939 PyTuple_GET_ITEM(slots, i));
1940 mp->type = T_OBJECT_EX;
1941 mp->offset = slotoffset;
1943 /* __dict__ and __weakref__ are already filtered out */
1944 assert(strcmp(mp->name, "__dict__") != 0);
1945 assert(strcmp(mp->name, "__weakref__") != 0);
1947 slotoffset += sizeof(PyObject *);
1950 if (add_dict) {
1951 if (base->tp_itemsize)
1952 type->tp_dictoffset = -(long)sizeof(PyObject *);
1953 else
1954 type->tp_dictoffset = slotoffset;
1955 slotoffset += sizeof(PyObject *);
1957 if (add_weak) {
1958 assert(!base->tp_itemsize);
1959 type->tp_weaklistoffset = slotoffset;
1960 slotoffset += sizeof(PyObject *);
1962 type->tp_basicsize = slotoffset;
1963 type->tp_itemsize = base->tp_itemsize;
1964 type->tp_members = PyHeapType_GET_MEMBERS(et);
1966 if (type->tp_weaklistoffset && type->tp_dictoffset)
1967 type->tp_getset = subtype_getsets_full;
1968 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1969 type->tp_getset = subtype_getsets_weakref_only;
1970 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1971 type->tp_getset = subtype_getsets_dict_only;
1972 else
1973 type->tp_getset = NULL;
1975 /* Special case some slots */
1976 if (type->tp_dictoffset != 0 || nslots > 0) {
1977 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1978 type->tp_getattro = PyObject_GenericGetAttr;
1979 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1980 type->tp_setattro = PyObject_GenericSetAttr;
1982 type->tp_dealloc = subtype_dealloc;
1984 /* Enable GC unless there are really no instance variables possible */
1985 if (!(type->tp_basicsize == sizeof(PyObject) &&
1986 type->tp_itemsize == 0))
1987 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1989 /* Always override allocation strategy to use regular heap */
1990 type->tp_alloc = PyType_GenericAlloc;
1991 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1992 type->tp_free = PyObject_GC_Del;
1993 type->tp_traverse = subtype_traverse;
1994 type->tp_clear = subtype_clear;
1996 else
1997 type->tp_free = PyObject_Del;
1999 /* Initialize the rest */
2000 if (PyType_Ready(type) < 0) {
2001 Py_DECREF(type);
2002 return NULL;
2005 /* Put the proper slots in place */
2006 fixup_slot_dispatchers(type);
2008 return (PyObject *)type;
2011 /* Internal API to look for a name through the MRO.
2012 This returns a borrowed reference, and doesn't set an exception! */
2013 PyObject *
2014 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2016 Py_ssize_t i, n;
2017 PyObject *mro, *res, *base, *dict;
2019 /* Look in tp_dict of types in MRO */
2020 mro = type->tp_mro;
2022 /* If mro is NULL, the type is either not yet initialized
2023 by PyType_Ready(), or already cleared by type_clear().
2024 Either way the safest thing to do is to return NULL. */
2025 if (mro == NULL)
2026 return NULL;
2028 assert(PyTuple_Check(mro));
2029 n = PyTuple_GET_SIZE(mro);
2030 for (i = 0; i < n; i++) {
2031 base = PyTuple_GET_ITEM(mro, i);
2032 if (PyClass_Check(base))
2033 dict = ((PyClassObject *)base)->cl_dict;
2034 else {
2035 assert(PyType_Check(base));
2036 dict = ((PyTypeObject *)base)->tp_dict;
2038 assert(dict && PyDict_Check(dict));
2039 res = PyDict_GetItem(dict, name);
2040 if (res != NULL)
2041 return res;
2043 return NULL;
2046 /* This is similar to PyObject_GenericGetAttr(),
2047 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2048 static PyObject *
2049 type_getattro(PyTypeObject *type, PyObject *name)
2051 PyTypeObject *metatype = type->ob_type;
2052 PyObject *meta_attribute, *attribute;
2053 descrgetfunc meta_get;
2055 /* Initialize this type (we'll assume the metatype is initialized) */
2056 if (type->tp_dict == NULL) {
2057 if (PyType_Ready(type) < 0)
2058 return NULL;
2061 /* No readable descriptor found yet */
2062 meta_get = NULL;
2064 /* Look for the attribute in the metatype */
2065 meta_attribute = _PyType_Lookup(metatype, name);
2067 if (meta_attribute != NULL) {
2068 meta_get = meta_attribute->ob_type->tp_descr_get;
2070 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2071 /* Data descriptors implement tp_descr_set to intercept
2072 * writes. Assume the attribute is not overridden in
2073 * type's tp_dict (and bases): call the descriptor now.
2075 return meta_get(meta_attribute, (PyObject *)type,
2076 (PyObject *)metatype);
2078 Py_INCREF(meta_attribute);
2081 /* No data descriptor found on metatype. Look in tp_dict of this
2082 * type and its bases */
2083 attribute = _PyType_Lookup(type, name);
2084 if (attribute != NULL) {
2085 /* Implement descriptor functionality, if any */
2086 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2088 Py_XDECREF(meta_attribute);
2090 if (local_get != NULL) {
2091 /* NULL 2nd argument indicates the descriptor was
2092 * found on the target object itself (or a base) */
2093 return local_get(attribute, (PyObject *)NULL,
2094 (PyObject *)type);
2097 Py_INCREF(attribute);
2098 return attribute;
2101 /* No attribute found in local __dict__ (or bases): use the
2102 * descriptor from the metatype, if any */
2103 if (meta_get != NULL) {
2104 PyObject *res;
2105 res = meta_get(meta_attribute, (PyObject *)type,
2106 (PyObject *)metatype);
2107 Py_DECREF(meta_attribute);
2108 return res;
2111 /* If an ordinary attribute was found on the metatype, return it now */
2112 if (meta_attribute != NULL) {
2113 return meta_attribute;
2116 /* Give up */
2117 PyErr_Format(PyExc_AttributeError,
2118 "type object '%.50s' has no attribute '%.400s'",
2119 type->tp_name, PyString_AS_STRING(name));
2120 return NULL;
2123 static int
2124 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2126 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2127 PyErr_Format(
2128 PyExc_TypeError,
2129 "can't set attributes of built-in/extension type '%s'",
2130 type->tp_name);
2131 return -1;
2133 /* XXX Example of how I expect this to be used...
2134 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2135 return -1;
2137 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2138 return -1;
2139 return update_slot(type, name);
2142 static void
2143 type_dealloc(PyTypeObject *type)
2145 PyHeapTypeObject *et;
2147 /* Assert this is a heap-allocated type object */
2148 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2149 _PyObject_GC_UNTRACK(type);
2150 PyObject_ClearWeakRefs((PyObject *)type);
2151 et = (PyHeapTypeObject *)type;
2152 Py_XDECREF(type->tp_base);
2153 Py_XDECREF(type->tp_dict);
2154 Py_XDECREF(type->tp_bases);
2155 Py_XDECREF(type->tp_mro);
2156 Py_XDECREF(type->tp_cache);
2157 Py_XDECREF(type->tp_subclasses);
2158 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2159 * of most other objects. It's okay to cast it to char *.
2161 PyObject_Free((char *)type->tp_doc);
2162 Py_XDECREF(et->ht_name);
2163 Py_XDECREF(et->ht_slots);
2164 type->ob_type->tp_free((PyObject *)type);
2167 static PyObject *
2168 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2170 PyObject *list, *raw, *ref;
2171 Py_ssize_t i, n;
2173 list = PyList_New(0);
2174 if (list == NULL)
2175 return NULL;
2176 raw = type->tp_subclasses;
2177 if (raw == NULL)
2178 return list;
2179 assert(PyList_Check(raw));
2180 n = PyList_GET_SIZE(raw);
2181 for (i = 0; i < n; i++) {
2182 ref = PyList_GET_ITEM(raw, i);
2183 assert(PyWeakref_CheckRef(ref));
2184 ref = PyWeakref_GET_OBJECT(ref);
2185 if (ref != Py_None) {
2186 if (PyList_Append(list, ref) < 0) {
2187 Py_DECREF(list);
2188 return NULL;
2192 return list;
2195 static PyMethodDef type_methods[] = {
2196 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2197 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2198 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2199 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2203 PyDoc_STRVAR(type_doc,
2204 "type(object) -> the object's type\n"
2205 "type(name, bases, dict) -> a new type");
2207 static int
2208 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2210 /* Because of type_is_gc(), the collector only calls this
2211 for heaptypes. */
2212 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2214 Py_VISIT(type->tp_dict);
2215 Py_VISIT(type->tp_cache);
2216 Py_VISIT(type->tp_mro);
2217 Py_VISIT(type->tp_bases);
2218 Py_VISIT(type->tp_base);
2220 /* There's no need to visit type->tp_subclasses or
2221 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2222 in cycles; tp_subclasses is a list of weak references,
2223 and slots is a tuple of strings. */
2225 return 0;
2228 static int
2229 type_clear(PyTypeObject *type)
2231 /* Because of type_is_gc(), the collector only calls this
2232 for heaptypes. */
2233 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2235 /* The only field we need to clear is tp_mro, which is part of a
2236 hard cycle (its first element is the class itself) that won't
2237 be broken otherwise (it's a tuple and tuples don't have a
2238 tp_clear handler). None of the other fields need to be
2239 cleared, and here's why:
2241 tp_dict:
2242 It is a dict, so the collector will call its tp_clear.
2244 tp_cache:
2245 Not used; if it were, it would be a dict.
2247 tp_bases, tp_base:
2248 If these are involved in a cycle, there must be at least
2249 one other, mutable object in the cycle, e.g. a base
2250 class's dict; the cycle will be broken that way.
2252 tp_subclasses:
2253 A list of weak references can't be part of a cycle; and
2254 lists have their own tp_clear.
2256 slots (in PyHeapTypeObject):
2257 A tuple of strings can't be part of a cycle.
2260 Py_CLEAR(type->tp_mro);
2262 return 0;
2265 static int
2266 type_is_gc(PyTypeObject *type)
2268 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2271 PyTypeObject PyType_Type = {
2272 PyObject_HEAD_INIT(&PyType_Type)
2273 0, /* ob_size */
2274 "type", /* tp_name */
2275 sizeof(PyHeapTypeObject), /* tp_basicsize */
2276 sizeof(PyMemberDef), /* tp_itemsize */
2277 (destructor)type_dealloc, /* tp_dealloc */
2278 0, /* tp_print */
2279 0, /* tp_getattr */
2280 0, /* tp_setattr */
2281 type_compare, /* tp_compare */
2282 (reprfunc)type_repr, /* tp_repr */
2283 0, /* tp_as_number */
2284 0, /* tp_as_sequence */
2285 0, /* tp_as_mapping */
2286 (hashfunc)_Py_HashPointer, /* tp_hash */
2287 (ternaryfunc)type_call, /* tp_call */
2288 0, /* tp_str */
2289 (getattrofunc)type_getattro, /* tp_getattro */
2290 (setattrofunc)type_setattro, /* tp_setattro */
2291 0, /* tp_as_buffer */
2292 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2293 Py_TPFLAGS_BASETYPE, /* tp_flags */
2294 type_doc, /* tp_doc */
2295 (traverseproc)type_traverse, /* tp_traverse */
2296 (inquiry)type_clear, /* tp_clear */
2297 0, /* tp_richcompare */
2298 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2299 0, /* tp_iter */
2300 0, /* tp_iternext */
2301 type_methods, /* tp_methods */
2302 type_members, /* tp_members */
2303 type_getsets, /* tp_getset */
2304 0, /* tp_base */
2305 0, /* tp_dict */
2306 0, /* tp_descr_get */
2307 0, /* tp_descr_set */
2308 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2309 0, /* tp_init */
2310 0, /* tp_alloc */
2311 type_new, /* tp_new */
2312 PyObject_GC_Del, /* tp_free */
2313 (inquiry)type_is_gc, /* tp_is_gc */
2317 /* The base type of all types (eventually)... except itself. */
2319 static int
2320 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2322 return 0;
2325 /* If we don't have a tp_new for a new-style class, new will use this one.
2326 Therefore this should take no arguments/keywords. However, this new may
2327 also be inherited by objects that define a tp_init but no tp_new. These
2328 objects WILL pass argumets to tp_new, because it gets the same args as
2329 tp_init. So only allow arguments if we aren't using the default init, in
2330 which case we expect init to handle argument parsing. */
2331 static PyObject *
2332 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2334 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2335 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2336 PyErr_SetString(PyExc_TypeError,
2337 "default __new__ takes no parameters");
2338 return NULL;
2340 return type->tp_alloc(type, 0);
2343 static void
2344 object_dealloc(PyObject *self)
2346 self->ob_type->tp_free(self);
2349 static PyObject *
2350 object_repr(PyObject *self)
2352 PyTypeObject *type;
2353 PyObject *mod, *name, *rtn;
2355 type = self->ob_type;
2356 mod = type_module(type, NULL);
2357 if (mod == NULL)
2358 PyErr_Clear();
2359 else if (!PyString_Check(mod)) {
2360 Py_DECREF(mod);
2361 mod = NULL;
2363 name = type_name(type, NULL);
2364 if (name == NULL)
2365 return NULL;
2366 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2367 rtn = PyString_FromFormat("<%s.%s object at %p>",
2368 PyString_AS_STRING(mod),
2369 PyString_AS_STRING(name),
2370 self);
2371 else
2372 rtn = PyString_FromFormat("<%s object at %p>",
2373 type->tp_name, self);
2374 Py_XDECREF(mod);
2375 Py_DECREF(name);
2376 return rtn;
2379 static PyObject *
2380 object_str(PyObject *self)
2382 unaryfunc f;
2384 f = self->ob_type->tp_repr;
2385 if (f == NULL)
2386 f = object_repr;
2387 return f(self);
2390 static long
2391 object_hash(PyObject *self)
2393 return _Py_HashPointer(self);
2396 static PyObject *
2397 object_get_class(PyObject *self, void *closure)
2399 Py_INCREF(self->ob_type);
2400 return (PyObject *)(self->ob_type);
2403 static int
2404 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2406 return a == b ||
2407 (a != NULL &&
2408 b != NULL &&
2409 a->tp_basicsize == b->tp_basicsize &&
2410 a->tp_itemsize == b->tp_itemsize &&
2411 a->tp_dictoffset == b->tp_dictoffset &&
2412 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2413 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2414 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2417 static int
2418 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2420 PyTypeObject *base = a->tp_base;
2421 Py_ssize_t size;
2423 if (base != b->tp_base)
2424 return 0;
2425 if (equiv_structs(a, base) && equiv_structs(b, base))
2426 return 1;
2427 size = base->tp_basicsize;
2428 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2429 size += sizeof(PyObject *);
2430 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2431 size += sizeof(PyObject *);
2432 return size == a->tp_basicsize && size == b->tp_basicsize;
2435 static int
2436 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
2438 PyTypeObject *newbase, *oldbase;
2440 if (newto->tp_dealloc != oldto->tp_dealloc ||
2441 newto->tp_free != oldto->tp_free)
2443 PyErr_Format(PyExc_TypeError,
2444 "%s assignment: "
2445 "'%s' deallocator differs from '%s'",
2446 attr,
2447 newto->tp_name,
2448 oldto->tp_name);
2449 return 0;
2451 newbase = newto;
2452 oldbase = oldto;
2453 while (equiv_structs(newbase, newbase->tp_base))
2454 newbase = newbase->tp_base;
2455 while (equiv_structs(oldbase, oldbase->tp_base))
2456 oldbase = oldbase->tp_base;
2457 if (newbase != oldbase &&
2458 (newbase->tp_base != oldbase->tp_base ||
2459 !same_slots_added(newbase, oldbase))) {
2460 PyErr_Format(PyExc_TypeError,
2461 "%s assignment: "
2462 "'%s' object layout differs from '%s'",
2463 attr,
2464 newto->tp_name,
2465 oldto->tp_name);
2466 return 0;
2469 return 1;
2472 static int
2473 object_set_class(PyObject *self, PyObject *value, void *closure)
2475 PyTypeObject *oldto = self->ob_type;
2476 PyTypeObject *newto;
2478 if (value == NULL) {
2479 PyErr_SetString(PyExc_TypeError,
2480 "can't delete __class__ attribute");
2481 return -1;
2483 if (!PyType_Check(value)) {
2484 PyErr_Format(PyExc_TypeError,
2485 "__class__ must be set to new-style class, not '%s' object",
2486 value->ob_type->tp_name);
2487 return -1;
2489 newto = (PyTypeObject *)value;
2490 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2491 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
2493 PyErr_Format(PyExc_TypeError,
2494 "__class__ assignment: only for heap types");
2495 return -1;
2497 if (compatible_for_assignment(newto, oldto, "__class__")) {
2498 Py_INCREF(newto);
2499 self->ob_type = newto;
2500 Py_DECREF(oldto);
2501 return 0;
2503 else {
2504 return -1;
2508 static PyGetSetDef object_getsets[] = {
2509 {"__class__", object_get_class, object_set_class,
2510 PyDoc_STR("the object's class")},
2515 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2516 We fall back to helpers in copy_reg for:
2517 - pickle protocols < 2
2518 - calculating the list of slot names (done only once per class)
2519 - the __newobj__ function (which is used as a token but never called)
2522 static PyObject *
2523 import_copy_reg(void)
2525 static PyObject *copy_reg_str;
2527 if (!copy_reg_str) {
2528 copy_reg_str = PyString_InternFromString("copy_reg");
2529 if (copy_reg_str == NULL)
2530 return NULL;
2533 return PyImport_Import(copy_reg_str);
2536 static PyObject *
2537 slotnames(PyObject *cls)
2539 PyObject *clsdict;
2540 PyObject *copy_reg;
2541 PyObject *slotnames;
2543 if (!PyType_Check(cls)) {
2544 Py_INCREF(Py_None);
2545 return Py_None;
2548 clsdict = ((PyTypeObject *)cls)->tp_dict;
2549 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2550 if (slotnames != NULL && PyList_Check(slotnames)) {
2551 Py_INCREF(slotnames);
2552 return slotnames;
2555 copy_reg = import_copy_reg();
2556 if (copy_reg == NULL)
2557 return NULL;
2559 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2560 Py_DECREF(copy_reg);
2561 if (slotnames != NULL &&
2562 slotnames != Py_None &&
2563 !PyList_Check(slotnames))
2565 PyErr_SetString(PyExc_TypeError,
2566 "copy_reg._slotnames didn't return a list or None");
2567 Py_DECREF(slotnames);
2568 slotnames = NULL;
2571 return slotnames;
2574 static PyObject *
2575 reduce_2(PyObject *obj)
2577 PyObject *cls, *getnewargs;
2578 PyObject *args = NULL, *args2 = NULL;
2579 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2580 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2581 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2582 Py_ssize_t i, n;
2584 cls = PyObject_GetAttrString(obj, "__class__");
2585 if (cls == NULL)
2586 return NULL;
2588 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2589 if (getnewargs != NULL) {
2590 args = PyObject_CallObject(getnewargs, NULL);
2591 Py_DECREF(getnewargs);
2592 if (args != NULL && !PyTuple_Check(args)) {
2593 PyErr_Format(PyExc_TypeError,
2594 "__getnewargs__ should return a tuple, "
2595 "not '%.200s'", args->ob_type->tp_name);
2596 goto end;
2599 else {
2600 PyErr_Clear();
2601 args = PyTuple_New(0);
2603 if (args == NULL)
2604 goto end;
2606 getstate = PyObject_GetAttrString(obj, "__getstate__");
2607 if (getstate != NULL) {
2608 state = PyObject_CallObject(getstate, NULL);
2609 Py_DECREF(getstate);
2610 if (state == NULL)
2611 goto end;
2613 else {
2614 PyErr_Clear();
2615 state = PyObject_GetAttrString(obj, "__dict__");
2616 if (state == NULL) {
2617 PyErr_Clear();
2618 state = Py_None;
2619 Py_INCREF(state);
2621 names = slotnames(cls);
2622 if (names == NULL)
2623 goto end;
2624 if (names != Py_None) {
2625 assert(PyList_Check(names));
2626 slots = PyDict_New();
2627 if (slots == NULL)
2628 goto end;
2629 n = 0;
2630 /* Can't pre-compute the list size; the list
2631 is stored on the class so accessible to other
2632 threads, which may be run by DECREF */
2633 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2634 PyObject *name, *value;
2635 name = PyList_GET_ITEM(names, i);
2636 value = PyObject_GetAttr(obj, name);
2637 if (value == NULL)
2638 PyErr_Clear();
2639 else {
2640 int err = PyDict_SetItem(slots, name,
2641 value);
2642 Py_DECREF(value);
2643 if (err)
2644 goto end;
2645 n++;
2648 if (n) {
2649 state = Py_BuildValue("(NO)", state, slots);
2650 if (state == NULL)
2651 goto end;
2656 if (!PyList_Check(obj)) {
2657 listitems = Py_None;
2658 Py_INCREF(listitems);
2660 else {
2661 listitems = PyObject_GetIter(obj);
2662 if (listitems == NULL)
2663 goto end;
2666 if (!PyDict_Check(obj)) {
2667 dictitems = Py_None;
2668 Py_INCREF(dictitems);
2670 else {
2671 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2672 if (dictitems == NULL)
2673 goto end;
2676 copy_reg = import_copy_reg();
2677 if (copy_reg == NULL)
2678 goto end;
2679 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2680 if (newobj == NULL)
2681 goto end;
2683 n = PyTuple_GET_SIZE(args);
2684 args2 = PyTuple_New(n+1);
2685 if (args2 == NULL)
2686 goto end;
2687 PyTuple_SET_ITEM(args2, 0, cls);
2688 cls = NULL;
2689 for (i = 0; i < n; i++) {
2690 PyObject *v = PyTuple_GET_ITEM(args, i);
2691 Py_INCREF(v);
2692 PyTuple_SET_ITEM(args2, i+1, v);
2695 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2697 end:
2698 Py_XDECREF(cls);
2699 Py_XDECREF(args);
2700 Py_XDECREF(args2);
2701 Py_XDECREF(slots);
2702 Py_XDECREF(state);
2703 Py_XDECREF(names);
2704 Py_XDECREF(listitems);
2705 Py_XDECREF(dictitems);
2706 Py_XDECREF(copy_reg);
2707 Py_XDECREF(newobj);
2708 return res;
2712 * There were two problems when object.__reduce__ and object.__reduce_ex__
2713 * were implemented in the same function:
2714 * - trying to pickle an object with a custom __reduce__ method that
2715 * fell back to object.__reduce__ in certain circumstances led to
2716 * infinite recursion at Python level and eventual RuntimeError.
2717 * - Pickling objects that lied about their type by overwriting the
2718 * __class__ descriptor could lead to infinite recursion at C level
2719 * and eventual segfault.
2721 * Because of backwards compatibility, the two methods still have to
2722 * behave in the same way, even if this is not required by the pickle
2723 * protocol. This common functionality was moved to the _common_reduce
2724 * function.
2726 static PyObject *
2727 _common_reduce(PyObject *self, int proto)
2729 PyObject *copy_reg, *res;
2731 if (proto >= 2)
2732 return reduce_2(self);
2734 copy_reg = import_copy_reg();
2735 if (!copy_reg)
2736 return NULL;
2738 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2739 Py_DECREF(copy_reg);
2741 return res;
2744 static PyObject *
2745 object_reduce(PyObject *self, PyObject *args)
2747 int proto = 0;
2749 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
2750 return NULL;
2752 return _common_reduce(self, proto);
2755 static PyObject *
2756 object_reduce_ex(PyObject *self, PyObject *args)
2758 PyObject *reduce, *res;
2759 int proto = 0;
2761 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2762 return NULL;
2764 reduce = PyObject_GetAttrString(self, "__reduce__");
2765 if (reduce == NULL)
2766 PyErr_Clear();
2767 else {
2768 PyObject *cls, *clsreduce, *objreduce;
2769 int override;
2770 cls = PyObject_GetAttrString(self, "__class__");
2771 if (cls == NULL) {
2772 Py_DECREF(reduce);
2773 return NULL;
2775 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2776 Py_DECREF(cls);
2777 if (clsreduce == NULL) {
2778 Py_DECREF(reduce);
2779 return NULL;
2781 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2782 "__reduce__");
2783 override = (clsreduce != objreduce);
2784 Py_DECREF(clsreduce);
2785 if (override) {
2786 res = PyObject_CallObject(reduce, NULL);
2787 Py_DECREF(reduce);
2788 return res;
2790 else
2791 Py_DECREF(reduce);
2794 return _common_reduce(self, proto);
2797 static PyMethodDef object_methods[] = {
2798 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2799 PyDoc_STR("helper for pickle")},
2800 {"__reduce__", object_reduce, METH_VARARGS,
2801 PyDoc_STR("helper for pickle")},
2806 PyTypeObject PyBaseObject_Type = {
2807 PyObject_HEAD_INIT(&PyType_Type)
2808 0, /* ob_size */
2809 "object", /* tp_name */
2810 sizeof(PyObject), /* tp_basicsize */
2811 0, /* tp_itemsize */
2812 object_dealloc, /* tp_dealloc */
2813 0, /* tp_print */
2814 0, /* tp_getattr */
2815 0, /* tp_setattr */
2816 0, /* tp_compare */
2817 object_repr, /* tp_repr */
2818 0, /* tp_as_number */
2819 0, /* tp_as_sequence */
2820 0, /* tp_as_mapping */
2821 object_hash, /* tp_hash */
2822 0, /* tp_call */
2823 object_str, /* tp_str */
2824 PyObject_GenericGetAttr, /* tp_getattro */
2825 PyObject_GenericSetAttr, /* tp_setattro */
2826 0, /* tp_as_buffer */
2827 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2828 PyDoc_STR("The most base type"), /* tp_doc */
2829 0, /* tp_traverse */
2830 0, /* tp_clear */
2831 0, /* tp_richcompare */
2832 0, /* tp_weaklistoffset */
2833 0, /* tp_iter */
2834 0, /* tp_iternext */
2835 object_methods, /* tp_methods */
2836 0, /* tp_members */
2837 object_getsets, /* tp_getset */
2838 0, /* tp_base */
2839 0, /* tp_dict */
2840 0, /* tp_descr_get */
2841 0, /* tp_descr_set */
2842 0, /* tp_dictoffset */
2843 object_init, /* tp_init */
2844 PyType_GenericAlloc, /* tp_alloc */
2845 object_new, /* tp_new */
2846 PyObject_Del, /* tp_free */
2850 /* Initialize the __dict__ in a type object */
2852 static int
2853 add_methods(PyTypeObject *type, PyMethodDef *meth)
2855 PyObject *dict = type->tp_dict;
2857 for (; meth->ml_name != NULL; meth++) {
2858 PyObject *descr;
2859 if (PyDict_GetItemString(dict, meth->ml_name) &&
2860 !(meth->ml_flags & METH_COEXIST))
2861 continue;
2862 if (meth->ml_flags & METH_CLASS) {
2863 if (meth->ml_flags & METH_STATIC) {
2864 PyErr_SetString(PyExc_ValueError,
2865 "method cannot be both class and static");
2866 return -1;
2868 descr = PyDescr_NewClassMethod(type, meth);
2870 else if (meth->ml_flags & METH_STATIC) {
2871 PyObject *cfunc = PyCFunction_New(meth, NULL);
2872 if (cfunc == NULL)
2873 return -1;
2874 descr = PyStaticMethod_New(cfunc);
2875 Py_DECREF(cfunc);
2877 else {
2878 descr = PyDescr_NewMethod(type, meth);
2880 if (descr == NULL)
2881 return -1;
2882 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2883 return -1;
2884 Py_DECREF(descr);
2886 return 0;
2889 static int
2890 add_members(PyTypeObject *type, PyMemberDef *memb)
2892 PyObject *dict = type->tp_dict;
2894 for (; memb->name != NULL; memb++) {
2895 PyObject *descr;
2896 if (PyDict_GetItemString(dict, memb->name))
2897 continue;
2898 descr = PyDescr_NewMember(type, memb);
2899 if (descr == NULL)
2900 return -1;
2901 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2902 return -1;
2903 Py_DECREF(descr);
2905 return 0;
2908 static int
2909 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2911 PyObject *dict = type->tp_dict;
2913 for (; gsp->name != NULL; gsp++) {
2914 PyObject *descr;
2915 if (PyDict_GetItemString(dict, gsp->name))
2916 continue;
2917 descr = PyDescr_NewGetSet(type, gsp);
2919 if (descr == NULL)
2920 return -1;
2921 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2922 return -1;
2923 Py_DECREF(descr);
2925 return 0;
2928 static void
2929 inherit_special(PyTypeObject *type, PyTypeObject *base)
2931 Py_ssize_t oldsize, newsize;
2933 /* Special flag magic */
2934 if (!type->tp_as_buffer && base->tp_as_buffer) {
2935 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2936 type->tp_flags |=
2937 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2939 if (!type->tp_as_sequence && base->tp_as_sequence) {
2940 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2941 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2943 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2944 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2945 if ((!type->tp_as_number && base->tp_as_number) ||
2946 (!type->tp_as_sequence && base->tp_as_sequence)) {
2947 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2948 if (!type->tp_as_number && !type->tp_as_sequence) {
2949 type->tp_flags |= base->tp_flags &
2950 Py_TPFLAGS_HAVE_INPLACEOPS;
2953 /* Wow */
2955 if (!type->tp_as_number && base->tp_as_number) {
2956 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2957 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2960 /* Copying basicsize is connected to the GC flags */
2961 oldsize = base->tp_basicsize;
2962 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2963 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2964 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2965 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2966 (!type->tp_traverse && !type->tp_clear)) {
2967 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2968 if (type->tp_traverse == NULL)
2969 type->tp_traverse = base->tp_traverse;
2970 if (type->tp_clear == NULL)
2971 type->tp_clear = base->tp_clear;
2973 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2974 /* The condition below could use some explanation.
2975 It appears that tp_new is not inherited for static types
2976 whose base class is 'object'; this seems to be a precaution
2977 so that old extension types don't suddenly become
2978 callable (object.__new__ wouldn't insure the invariants
2979 that the extension type's own factory function ensures).
2980 Heap types, of course, are under our control, so they do
2981 inherit tp_new; static extension types that specify some
2982 other built-in type as the default are considered
2983 new-style-aware so they also inherit object.__new__. */
2984 if (base != &PyBaseObject_Type ||
2985 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2986 if (type->tp_new == NULL)
2987 type->tp_new = base->tp_new;
2990 type->tp_basicsize = newsize;
2992 /* Copy other non-function slots */
2994 #undef COPYVAL
2995 #define COPYVAL(SLOT) \
2996 if (type->SLOT == 0) type->SLOT = base->SLOT
2998 COPYVAL(tp_itemsize);
2999 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3000 COPYVAL(tp_weaklistoffset);
3002 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3003 COPYVAL(tp_dictoffset);
3007 static void
3008 inherit_slots(PyTypeObject *type, PyTypeObject *base)
3010 PyTypeObject *basebase;
3012 #undef SLOTDEFINED
3013 #undef COPYSLOT
3014 #undef COPYNUM
3015 #undef COPYSEQ
3016 #undef COPYMAP
3017 #undef COPYBUF
3019 #define SLOTDEFINED(SLOT) \
3020 (base->SLOT != 0 && \
3021 (basebase == NULL || base->SLOT != basebase->SLOT))
3023 #define COPYSLOT(SLOT) \
3024 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3026 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3027 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3028 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3029 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3031 /* This won't inherit indirect slots (from tp_as_number etc.)
3032 if type doesn't provide the space. */
3034 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3035 basebase = base->tp_base;
3036 if (basebase->tp_as_number == NULL)
3037 basebase = NULL;
3038 COPYNUM(nb_add);
3039 COPYNUM(nb_subtract);
3040 COPYNUM(nb_multiply);
3041 COPYNUM(nb_divide);
3042 COPYNUM(nb_remainder);
3043 COPYNUM(nb_divmod);
3044 COPYNUM(nb_power);
3045 COPYNUM(nb_negative);
3046 COPYNUM(nb_positive);
3047 COPYNUM(nb_absolute);
3048 COPYNUM(nb_nonzero);
3049 COPYNUM(nb_invert);
3050 COPYNUM(nb_lshift);
3051 COPYNUM(nb_rshift);
3052 COPYNUM(nb_and);
3053 COPYNUM(nb_xor);
3054 COPYNUM(nb_or);
3055 COPYNUM(nb_coerce);
3056 COPYNUM(nb_int);
3057 COPYNUM(nb_long);
3058 COPYNUM(nb_float);
3059 COPYNUM(nb_oct);
3060 COPYNUM(nb_hex);
3061 COPYNUM(nb_inplace_add);
3062 COPYNUM(nb_inplace_subtract);
3063 COPYNUM(nb_inplace_multiply);
3064 COPYNUM(nb_inplace_divide);
3065 COPYNUM(nb_inplace_remainder);
3066 COPYNUM(nb_inplace_power);
3067 COPYNUM(nb_inplace_lshift);
3068 COPYNUM(nb_inplace_rshift);
3069 COPYNUM(nb_inplace_and);
3070 COPYNUM(nb_inplace_xor);
3071 COPYNUM(nb_inplace_or);
3072 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3073 COPYNUM(nb_true_divide);
3074 COPYNUM(nb_floor_divide);
3075 COPYNUM(nb_inplace_true_divide);
3076 COPYNUM(nb_inplace_floor_divide);
3078 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3079 COPYNUM(nb_index);
3083 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3084 basebase = base->tp_base;
3085 if (basebase->tp_as_sequence == NULL)
3086 basebase = NULL;
3087 COPYSEQ(sq_length);
3088 COPYSEQ(sq_concat);
3089 COPYSEQ(sq_repeat);
3090 COPYSEQ(sq_item);
3091 COPYSEQ(sq_slice);
3092 COPYSEQ(sq_ass_item);
3093 COPYSEQ(sq_ass_slice);
3094 COPYSEQ(sq_contains);
3095 COPYSEQ(sq_inplace_concat);
3096 COPYSEQ(sq_inplace_repeat);
3099 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3100 basebase = base->tp_base;
3101 if (basebase->tp_as_mapping == NULL)
3102 basebase = NULL;
3103 COPYMAP(mp_length);
3104 COPYMAP(mp_subscript);
3105 COPYMAP(mp_ass_subscript);
3108 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3109 basebase = base->tp_base;
3110 if (basebase->tp_as_buffer == NULL)
3111 basebase = NULL;
3112 COPYBUF(bf_getreadbuffer);
3113 COPYBUF(bf_getwritebuffer);
3114 COPYBUF(bf_getsegcount);
3115 COPYBUF(bf_getcharbuffer);
3118 basebase = base->tp_base;
3120 COPYSLOT(tp_dealloc);
3121 COPYSLOT(tp_print);
3122 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3123 type->tp_getattr = base->tp_getattr;
3124 type->tp_getattro = base->tp_getattro;
3126 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3127 type->tp_setattr = base->tp_setattr;
3128 type->tp_setattro = base->tp_setattro;
3130 /* tp_compare see tp_richcompare */
3131 COPYSLOT(tp_repr);
3132 /* tp_hash see tp_richcompare */
3133 COPYSLOT(tp_call);
3134 COPYSLOT(tp_str);
3135 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3136 if (type->tp_compare == NULL &&
3137 type->tp_richcompare == NULL &&
3138 type->tp_hash == NULL)
3140 type->tp_compare = base->tp_compare;
3141 type->tp_richcompare = base->tp_richcompare;
3142 type->tp_hash = base->tp_hash;
3145 else {
3146 COPYSLOT(tp_compare);
3148 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3149 COPYSLOT(tp_iter);
3150 COPYSLOT(tp_iternext);
3152 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3153 COPYSLOT(tp_descr_get);
3154 COPYSLOT(tp_descr_set);
3155 COPYSLOT(tp_dictoffset);
3156 COPYSLOT(tp_init);
3157 COPYSLOT(tp_alloc);
3158 COPYSLOT(tp_is_gc);
3159 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3160 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3161 /* They agree about gc. */
3162 COPYSLOT(tp_free);
3164 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3165 type->tp_free == NULL &&
3166 base->tp_free == _PyObject_Del) {
3167 /* A bit of magic to plug in the correct default
3168 * tp_free function when a derived class adds gc,
3169 * didn't define tp_free, and the base uses the
3170 * default non-gc tp_free.
3172 type->tp_free = PyObject_GC_Del;
3174 /* else they didn't agree about gc, and there isn't something
3175 * obvious to be done -- the type is on its own.
3180 static int add_operators(PyTypeObject *);
3183 PyType_Ready(PyTypeObject *type)
3185 PyObject *dict, *bases;
3186 PyTypeObject *base;
3187 Py_ssize_t i, n;
3189 if (type->tp_flags & Py_TPFLAGS_READY) {
3190 assert(type->tp_dict != NULL);
3191 return 0;
3193 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3195 type->tp_flags |= Py_TPFLAGS_READYING;
3197 #ifdef Py_TRACE_REFS
3198 /* PyType_Ready is the closest thing we have to a choke point
3199 * for type objects, so is the best place I can think of to try
3200 * to get type objects into the doubly-linked list of all objects.
3201 * Still, not all type objects go thru PyType_Ready.
3203 _Py_AddToAllObjects((PyObject *)type, 0);
3204 #endif
3206 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3207 base = type->tp_base;
3208 if (base == NULL && type != &PyBaseObject_Type) {
3209 base = type->tp_base = &PyBaseObject_Type;
3210 Py_INCREF(base);
3213 /* Now the only way base can still be NULL is if type is
3214 * &PyBaseObject_Type.
3217 /* Initialize the base class */
3218 if (base && base->tp_dict == NULL) {
3219 if (PyType_Ready(base) < 0)
3220 goto error;
3223 /* Initialize ob_type if NULL. This means extensions that want to be
3224 compilable separately on Windows can call PyType_Ready() instead of
3225 initializing the ob_type field of their type objects. */
3226 /* The test for base != NULL is really unnecessary, since base is only
3227 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3228 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3229 know that. */
3230 if (type->ob_type == NULL && base != NULL)
3231 type->ob_type = base->ob_type;
3233 /* Initialize tp_bases */
3234 bases = type->tp_bases;
3235 if (bases == NULL) {
3236 if (base == NULL)
3237 bases = PyTuple_New(0);
3238 else
3239 bases = PyTuple_Pack(1, base);
3240 if (bases == NULL)
3241 goto error;
3242 type->tp_bases = bases;
3245 /* Initialize tp_dict */
3246 dict = type->tp_dict;
3247 if (dict == NULL) {
3248 dict = PyDict_New();
3249 if (dict == NULL)
3250 goto error;
3251 type->tp_dict = dict;
3254 /* Add type-specific descriptors to tp_dict */
3255 if (add_operators(type) < 0)
3256 goto error;
3257 if (type->tp_methods != NULL) {
3258 if (add_methods(type, type->tp_methods) < 0)
3259 goto error;
3261 if (type->tp_members != NULL) {
3262 if (add_members(type, type->tp_members) < 0)
3263 goto error;
3265 if (type->tp_getset != NULL) {
3266 if (add_getset(type, type->tp_getset) < 0)
3267 goto error;
3270 /* Calculate method resolution order */
3271 if (mro_internal(type) < 0) {
3272 goto error;
3275 /* Inherit special flags from dominant base */
3276 if (type->tp_base != NULL)
3277 inherit_special(type, type->tp_base);
3279 /* Initialize tp_dict properly */
3280 bases = type->tp_mro;
3281 assert(bases != NULL);
3282 assert(PyTuple_Check(bases));
3283 n = PyTuple_GET_SIZE(bases);
3284 for (i = 1; i < n; i++) {
3285 PyObject *b = PyTuple_GET_ITEM(bases, i);
3286 if (PyType_Check(b))
3287 inherit_slots(type, (PyTypeObject *)b);
3290 /* Sanity check for tp_free. */
3291 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3292 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3293 /* This base class needs to call tp_free, but doesn't have
3294 * one, or its tp_free is for non-gc'ed objects.
3296 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3297 "gc and is a base type but has inappropriate "
3298 "tp_free slot",
3299 type->tp_name);
3300 goto error;
3303 /* if the type dictionary doesn't contain a __doc__, set it from
3304 the tp_doc slot.
3306 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3307 if (type->tp_doc != NULL) {
3308 PyObject *doc = PyString_FromString(type->tp_doc);
3309 if (doc == NULL)
3310 goto error;
3311 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3312 Py_DECREF(doc);
3313 } else {
3314 PyDict_SetItemString(type->tp_dict,
3315 "__doc__", Py_None);
3319 /* Some more special stuff */
3320 base = type->tp_base;
3321 if (base != NULL) {
3322 if (type->tp_as_number == NULL)
3323 type->tp_as_number = base->tp_as_number;
3324 if (type->tp_as_sequence == NULL)
3325 type->tp_as_sequence = base->tp_as_sequence;
3326 if (type->tp_as_mapping == NULL)
3327 type->tp_as_mapping = base->tp_as_mapping;
3328 if (type->tp_as_buffer == NULL)
3329 type->tp_as_buffer = base->tp_as_buffer;
3332 /* Link into each base class's list of subclasses */
3333 bases = type->tp_bases;
3334 n = PyTuple_GET_SIZE(bases);
3335 for (i = 0; i < n; i++) {
3336 PyObject *b = PyTuple_GET_ITEM(bases, i);
3337 if (PyType_Check(b) &&
3338 add_subclass((PyTypeObject *)b, type) < 0)
3339 goto error;
3342 /* All done -- set the ready flag */
3343 assert(type->tp_dict != NULL);
3344 type->tp_flags =
3345 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3346 return 0;
3348 error:
3349 type->tp_flags &= ~Py_TPFLAGS_READYING;
3350 return -1;
3353 static int
3354 add_subclass(PyTypeObject *base, PyTypeObject *type)
3356 Py_ssize_t i;
3357 int result;
3358 PyObject *list, *ref, *newobj;
3360 list = base->tp_subclasses;
3361 if (list == NULL) {
3362 base->tp_subclasses = list = PyList_New(0);
3363 if (list == NULL)
3364 return -1;
3366 assert(PyList_Check(list));
3367 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
3368 i = PyList_GET_SIZE(list);
3369 while (--i >= 0) {
3370 ref = PyList_GET_ITEM(list, i);
3371 assert(PyWeakref_CheckRef(ref));
3372 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3373 return PyList_SetItem(list, i, newobj);
3375 result = PyList_Append(list, newobj);
3376 Py_DECREF(newobj);
3377 return result;
3380 static void
3381 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3383 Py_ssize_t i;
3384 PyObject *list, *ref;
3386 list = base->tp_subclasses;
3387 if (list == NULL) {
3388 return;
3390 assert(PyList_Check(list));
3391 i = PyList_GET_SIZE(list);
3392 while (--i >= 0) {
3393 ref = PyList_GET_ITEM(list, i);
3394 assert(PyWeakref_CheckRef(ref));
3395 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3396 /* this can't fail, right? */
3397 PySequence_DelItem(list, i);
3398 return;
3403 static int
3404 check_num_args(PyObject *ob, int n)
3406 if (!PyTuple_CheckExact(ob)) {
3407 PyErr_SetString(PyExc_SystemError,
3408 "PyArg_UnpackTuple() argument list is not a tuple");
3409 return 0;
3411 if (n == PyTuple_GET_SIZE(ob))
3412 return 1;
3413 PyErr_Format(
3414 PyExc_TypeError,
3415 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
3416 return 0;
3419 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3421 /* There's a wrapper *function* for each distinct function typedef used
3422 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3423 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3424 Most tables have only one entry; the tables for binary operators have two
3425 entries, one regular and one with reversed arguments. */
3427 static PyObject *
3428 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
3430 lenfunc func = (lenfunc)wrapped;
3431 Py_ssize_t res;
3433 if (!check_num_args(args, 0))
3434 return NULL;
3435 res = (*func)(self);
3436 if (res == -1 && PyErr_Occurred())
3437 return NULL;
3438 return PyInt_FromLong((long)res);
3441 static PyObject *
3442 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3444 inquiry func = (inquiry)wrapped;
3445 int res;
3447 if (!check_num_args(args, 0))
3448 return NULL;
3449 res = (*func)(self);
3450 if (res == -1 && PyErr_Occurred())
3451 return NULL;
3452 return PyBool_FromLong((long)res);
3455 static PyObject *
3456 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3458 binaryfunc func = (binaryfunc)wrapped;
3459 PyObject *other;
3461 if (!check_num_args(args, 1))
3462 return NULL;
3463 other = PyTuple_GET_ITEM(args, 0);
3464 return (*func)(self, other);
3467 static PyObject *
3468 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3470 binaryfunc func = (binaryfunc)wrapped;
3471 PyObject *other;
3473 if (!check_num_args(args, 1))
3474 return NULL;
3475 other = PyTuple_GET_ITEM(args, 0);
3476 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3477 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3478 Py_INCREF(Py_NotImplemented);
3479 return Py_NotImplemented;
3481 return (*func)(self, other);
3484 static PyObject *
3485 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3487 binaryfunc func = (binaryfunc)wrapped;
3488 PyObject *other;
3490 if (!check_num_args(args, 1))
3491 return NULL;
3492 other = PyTuple_GET_ITEM(args, 0);
3493 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3494 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3495 Py_INCREF(Py_NotImplemented);
3496 return Py_NotImplemented;
3498 return (*func)(other, self);
3501 static PyObject *
3502 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3504 coercion func = (coercion)wrapped;
3505 PyObject *other, *res;
3506 int ok;
3508 if (!check_num_args(args, 1))
3509 return NULL;
3510 other = PyTuple_GET_ITEM(args, 0);
3511 ok = func(&self, &other);
3512 if (ok < 0)
3513 return NULL;
3514 if (ok > 0) {
3515 Py_INCREF(Py_NotImplemented);
3516 return Py_NotImplemented;
3518 res = PyTuple_New(2);
3519 if (res == NULL) {
3520 Py_DECREF(self);
3521 Py_DECREF(other);
3522 return NULL;
3524 PyTuple_SET_ITEM(res, 0, self);
3525 PyTuple_SET_ITEM(res, 1, other);
3526 return res;
3529 static PyObject *
3530 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3532 ternaryfunc func = (ternaryfunc)wrapped;
3533 PyObject *other;
3534 PyObject *third = Py_None;
3536 /* Note: This wrapper only works for __pow__() */
3538 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3539 return NULL;
3540 return (*func)(self, other, third);
3543 static PyObject *
3544 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3546 ternaryfunc func = (ternaryfunc)wrapped;
3547 PyObject *other;
3548 PyObject *third = Py_None;
3550 /* Note: This wrapper only works for __pow__() */
3552 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3553 return NULL;
3554 return (*func)(other, self, third);
3557 static PyObject *
3558 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3560 unaryfunc func = (unaryfunc)wrapped;
3562 if (!check_num_args(args, 0))
3563 return NULL;
3564 return (*func)(self);
3567 static PyObject *
3568 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
3570 ssizeargfunc func = (ssizeargfunc)wrapped;
3571 PyObject* o;
3572 Py_ssize_t i;
3574 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3575 return NULL;
3576 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
3577 if (i == -1 && PyErr_Occurred())
3578 return NULL;
3579 return (*func)(self, i);
3582 static Py_ssize_t
3583 getindex(PyObject *self, PyObject *arg)
3585 Py_ssize_t i;
3587 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
3588 if (i == -1 && PyErr_Occurred())
3589 return -1;
3590 if (i < 0) {
3591 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3592 if (sq && sq->sq_length) {
3593 Py_ssize_t n = (*sq->sq_length)(self);
3594 if (n < 0)
3595 return -1;
3596 i += n;
3599 return i;
3602 static PyObject *
3603 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3605 ssizeargfunc func = (ssizeargfunc)wrapped;
3606 PyObject *arg;
3607 Py_ssize_t i;
3609 if (PyTuple_GET_SIZE(args) == 1) {
3610 arg = PyTuple_GET_ITEM(args, 0);
3611 i = getindex(self, arg);
3612 if (i == -1 && PyErr_Occurred())
3613 return NULL;
3614 return (*func)(self, i);
3616 check_num_args(args, 1);
3617 assert(PyErr_Occurred());
3618 return NULL;
3621 static PyObject *
3622 wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
3624 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3625 Py_ssize_t i, j;
3627 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3628 return NULL;
3629 return (*func)(self, i, j);
3632 static PyObject *
3633 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3635 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3636 Py_ssize_t i;
3637 int res;
3638 PyObject *arg, *value;
3640 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3641 return NULL;
3642 i = getindex(self, arg);
3643 if (i == -1 && PyErr_Occurred())
3644 return NULL;
3645 res = (*func)(self, i, value);
3646 if (res == -1 && PyErr_Occurred())
3647 return NULL;
3648 Py_INCREF(Py_None);
3649 return Py_None;
3652 static PyObject *
3653 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3655 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3656 Py_ssize_t i;
3657 int res;
3658 PyObject *arg;
3660 if (!check_num_args(args, 1))
3661 return NULL;
3662 arg = PyTuple_GET_ITEM(args, 0);
3663 i = getindex(self, arg);
3664 if (i == -1 && PyErr_Occurred())
3665 return NULL;
3666 res = (*func)(self, i, NULL);
3667 if (res == -1 && PyErr_Occurred())
3668 return NULL;
3669 Py_INCREF(Py_None);
3670 return Py_None;
3673 static PyObject *
3674 wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
3676 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3677 Py_ssize_t i, j;
3678 int res;
3679 PyObject *value;
3681 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
3682 return NULL;
3683 res = (*func)(self, i, j, value);
3684 if (res == -1 && PyErr_Occurred())
3685 return NULL;
3686 Py_INCREF(Py_None);
3687 return Py_None;
3690 static PyObject *
3691 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3693 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3694 Py_ssize_t i, j;
3695 int res;
3697 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3698 return NULL;
3699 res = (*func)(self, i, j, NULL);
3700 if (res == -1 && PyErr_Occurred())
3701 return NULL;
3702 Py_INCREF(Py_None);
3703 return Py_None;
3706 /* XXX objobjproc is a misnomer; should be objargpred */
3707 static PyObject *
3708 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3710 objobjproc func = (objobjproc)wrapped;
3711 int res;
3712 PyObject *value;
3714 if (!check_num_args(args, 1))
3715 return NULL;
3716 value = PyTuple_GET_ITEM(args, 0);
3717 res = (*func)(self, value);
3718 if (res == -1 && PyErr_Occurred())
3719 return NULL;
3720 else
3721 return PyBool_FromLong(res);
3724 static PyObject *
3725 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3727 objobjargproc func = (objobjargproc)wrapped;
3728 int res;
3729 PyObject *key, *value;
3731 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3732 return NULL;
3733 res = (*func)(self, key, value);
3734 if (res == -1 && PyErr_Occurred())
3735 return NULL;
3736 Py_INCREF(Py_None);
3737 return Py_None;
3740 static PyObject *
3741 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3743 objobjargproc func = (objobjargproc)wrapped;
3744 int res;
3745 PyObject *key;
3747 if (!check_num_args(args, 1))
3748 return NULL;
3749 key = PyTuple_GET_ITEM(args, 0);
3750 res = (*func)(self, key, NULL);
3751 if (res == -1 && PyErr_Occurred())
3752 return NULL;
3753 Py_INCREF(Py_None);
3754 return Py_None;
3757 static PyObject *
3758 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3760 cmpfunc func = (cmpfunc)wrapped;
3761 int res;
3762 PyObject *other;
3764 if (!check_num_args(args, 1))
3765 return NULL;
3766 other = PyTuple_GET_ITEM(args, 0);
3767 if (other->ob_type->tp_compare != func &&
3768 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3769 PyErr_Format(
3770 PyExc_TypeError,
3771 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3772 self->ob_type->tp_name,
3773 self->ob_type->tp_name,
3774 other->ob_type->tp_name);
3775 return NULL;
3777 res = (*func)(self, other);
3778 if (PyErr_Occurred())
3779 return NULL;
3780 return PyInt_FromLong((long)res);
3783 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3784 This is called the Carlo Verre hack after its discoverer. */
3785 static int
3786 hackcheck(PyObject *self, setattrofunc func, char *what)
3788 PyTypeObject *type = self->ob_type;
3789 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3790 type = type->tp_base;
3791 /* If type is NULL now, this is a really weird type.
3792 In the same of backwards compatibility (?), just shut up. */
3793 if (type && type->tp_setattro != func) {
3794 PyErr_Format(PyExc_TypeError,
3795 "can't apply this %s to %s object",
3796 what,
3797 type->tp_name);
3798 return 0;
3800 return 1;
3803 static PyObject *
3804 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3806 setattrofunc func = (setattrofunc)wrapped;
3807 int res;
3808 PyObject *name, *value;
3810 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3811 return NULL;
3812 if (!hackcheck(self, func, "__setattr__"))
3813 return NULL;
3814 res = (*func)(self, name, value);
3815 if (res < 0)
3816 return NULL;
3817 Py_INCREF(Py_None);
3818 return Py_None;
3821 static PyObject *
3822 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3824 setattrofunc func = (setattrofunc)wrapped;
3825 int res;
3826 PyObject *name;
3828 if (!check_num_args(args, 1))
3829 return NULL;
3830 name = PyTuple_GET_ITEM(args, 0);
3831 if (!hackcheck(self, func, "__delattr__"))
3832 return NULL;
3833 res = (*func)(self, name, NULL);
3834 if (res < 0)
3835 return NULL;
3836 Py_INCREF(Py_None);
3837 return Py_None;
3840 static PyObject *
3841 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3843 hashfunc func = (hashfunc)wrapped;
3844 long res;
3846 if (!check_num_args(args, 0))
3847 return NULL;
3848 res = (*func)(self);
3849 if (res == -1 && PyErr_Occurred())
3850 return NULL;
3851 return PyInt_FromLong(res);
3854 static PyObject *
3855 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3857 ternaryfunc func = (ternaryfunc)wrapped;
3859 return (*func)(self, args, kwds);
3862 static PyObject *
3863 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3865 richcmpfunc func = (richcmpfunc)wrapped;
3866 PyObject *other;
3868 if (!check_num_args(args, 1))
3869 return NULL;
3870 other = PyTuple_GET_ITEM(args, 0);
3871 return (*func)(self, other, op);
3874 #undef RICHCMP_WRAPPER
3875 #define RICHCMP_WRAPPER(NAME, OP) \
3876 static PyObject * \
3877 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3879 return wrap_richcmpfunc(self, args, wrapped, OP); \
3882 RICHCMP_WRAPPER(lt, Py_LT)
3883 RICHCMP_WRAPPER(le, Py_LE)
3884 RICHCMP_WRAPPER(eq, Py_EQ)
3885 RICHCMP_WRAPPER(ne, Py_NE)
3886 RICHCMP_WRAPPER(gt, Py_GT)
3887 RICHCMP_WRAPPER(ge, Py_GE)
3889 static PyObject *
3890 wrap_next(PyObject *self, PyObject *args, void *wrapped)
3892 unaryfunc func = (unaryfunc)wrapped;
3893 PyObject *res;
3895 if (!check_num_args(args, 0))
3896 return NULL;
3897 res = (*func)(self);
3898 if (res == NULL && !PyErr_Occurred())
3899 PyErr_SetNone(PyExc_StopIteration);
3900 return res;
3903 static PyObject *
3904 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3906 descrgetfunc func = (descrgetfunc)wrapped;
3907 PyObject *obj;
3908 PyObject *type = NULL;
3910 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3911 return NULL;
3912 if (obj == Py_None)
3913 obj = NULL;
3914 if (type == Py_None)
3915 type = NULL;
3916 if (type == NULL &&obj == NULL) {
3917 PyErr_SetString(PyExc_TypeError,
3918 "__get__(None, None) is invalid");
3919 return NULL;
3921 return (*func)(self, obj, type);
3924 static PyObject *
3925 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3927 descrsetfunc func = (descrsetfunc)wrapped;
3928 PyObject *obj, *value;
3929 int ret;
3931 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3932 return NULL;
3933 ret = (*func)(self, obj, value);
3934 if (ret < 0)
3935 return NULL;
3936 Py_INCREF(Py_None);
3937 return Py_None;
3940 static PyObject *
3941 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3943 descrsetfunc func = (descrsetfunc)wrapped;
3944 PyObject *obj;
3945 int ret;
3947 if (!check_num_args(args, 1))
3948 return NULL;
3949 obj = PyTuple_GET_ITEM(args, 0);
3950 ret = (*func)(self, obj, NULL);
3951 if (ret < 0)
3952 return NULL;
3953 Py_INCREF(Py_None);
3954 return Py_None;
3957 static PyObject *
3958 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3960 initproc func = (initproc)wrapped;
3962 if (func(self, args, kwds) < 0)
3963 return NULL;
3964 Py_INCREF(Py_None);
3965 return Py_None;
3968 static PyObject *
3969 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3971 PyTypeObject *type, *subtype, *staticbase;
3972 PyObject *arg0, *res;
3974 if (self == NULL || !PyType_Check(self))
3975 Py_FatalError("__new__() called with non-type 'self'");
3976 type = (PyTypeObject *)self;
3977 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3978 PyErr_Format(PyExc_TypeError,
3979 "%s.__new__(): not enough arguments",
3980 type->tp_name);
3981 return NULL;
3983 arg0 = PyTuple_GET_ITEM(args, 0);
3984 if (!PyType_Check(arg0)) {
3985 PyErr_Format(PyExc_TypeError,
3986 "%s.__new__(X): X is not a type object (%s)",
3987 type->tp_name,
3988 arg0->ob_type->tp_name);
3989 return NULL;
3991 subtype = (PyTypeObject *)arg0;
3992 if (!PyType_IsSubtype(subtype, type)) {
3993 PyErr_Format(PyExc_TypeError,
3994 "%s.__new__(%s): %s is not a subtype of %s",
3995 type->tp_name,
3996 subtype->tp_name,
3997 subtype->tp_name,
3998 type->tp_name);
3999 return NULL;
4002 /* Check that the use doesn't do something silly and unsafe like
4003 object.__new__(dict). To do this, we check that the
4004 most derived base that's not a heap type is this type. */
4005 staticbase = subtype;
4006 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4007 staticbase = staticbase->tp_base;
4008 /* If staticbase is NULL now, it is a really weird type.
4009 In the same of backwards compatibility (?), just shut up. */
4010 if (staticbase && staticbase->tp_new != type->tp_new) {
4011 PyErr_Format(PyExc_TypeError,
4012 "%s.__new__(%s) is not safe, use %s.__new__()",
4013 type->tp_name,
4014 subtype->tp_name,
4015 staticbase == NULL ? "?" : staticbase->tp_name);
4016 return NULL;
4019 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4020 if (args == NULL)
4021 return NULL;
4022 res = type->tp_new(subtype, args, kwds);
4023 Py_DECREF(args);
4024 return res;
4027 static struct PyMethodDef tp_new_methoddef[] = {
4028 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
4029 PyDoc_STR("T.__new__(S, ...) -> "
4030 "a new object with type S, a subtype of T")},
4034 static int
4035 add_tp_new_wrapper(PyTypeObject *type)
4037 PyObject *func;
4039 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4040 return 0;
4041 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4042 if (func == NULL)
4043 return -1;
4044 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4045 Py_DECREF(func);
4046 return -1;
4048 Py_DECREF(func);
4049 return 0;
4052 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4053 below at override_slots() for more explanation. */
4055 #define SLOT0(FUNCNAME, OPSTR) \
4056 static PyObject * \
4057 FUNCNAME(PyObject *self) \
4059 static PyObject *cache_str; \
4060 return call_method(self, OPSTR, &cache_str, "()"); \
4063 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4064 static PyObject * \
4065 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4067 static PyObject *cache_str; \
4068 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4071 /* Boolean helper for SLOT1BINFULL().
4072 right.__class__ is a nontrivial subclass of left.__class__. */
4073 static int
4074 method_is_overloaded(PyObject *left, PyObject *right, char *name)
4076 PyObject *a, *b;
4077 int ok;
4079 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4080 if (b == NULL) {
4081 PyErr_Clear();
4082 /* If right doesn't have it, it's not overloaded */
4083 return 0;
4086 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4087 if (a == NULL) {
4088 PyErr_Clear();
4089 Py_DECREF(b);
4090 /* If right has it but left doesn't, it's overloaded */
4091 return 1;
4094 ok = PyObject_RichCompareBool(a, b, Py_NE);
4095 Py_DECREF(a);
4096 Py_DECREF(b);
4097 if (ok < 0) {
4098 PyErr_Clear();
4099 return 0;
4102 return ok;
4106 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4107 static PyObject * \
4108 FUNCNAME(PyObject *self, PyObject *other) \
4110 static PyObject *cache_str, *rcache_str; \
4111 int do_other = self->ob_type != other->ob_type && \
4112 other->ob_type->tp_as_number != NULL && \
4113 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4114 if (self->ob_type->tp_as_number != NULL && \
4115 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4116 PyObject *r; \
4117 if (do_other && \
4118 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4119 method_is_overloaded(self, other, ROPSTR)) { \
4120 r = call_maybe( \
4121 other, ROPSTR, &rcache_str, "(O)", self); \
4122 if (r != Py_NotImplemented) \
4123 return r; \
4124 Py_DECREF(r); \
4125 do_other = 0; \
4127 r = call_maybe( \
4128 self, OPSTR, &cache_str, "(O)", other); \
4129 if (r != Py_NotImplemented || \
4130 other->ob_type == self->ob_type) \
4131 return r; \
4132 Py_DECREF(r); \
4134 if (do_other) { \
4135 return call_maybe( \
4136 other, ROPSTR, &rcache_str, "(O)", self); \
4138 Py_INCREF(Py_NotImplemented); \
4139 return Py_NotImplemented; \
4142 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4143 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4145 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4146 static PyObject * \
4147 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4149 static PyObject *cache_str; \
4150 return call_method(self, OPSTR, &cache_str, \
4151 "(" ARGCODES ")", arg1, arg2); \
4154 static Py_ssize_t
4155 slot_sq_length(PyObject *self)
4157 static PyObject *len_str;
4158 PyObject *res = call_method(self, "__len__", &len_str, "()");
4159 Py_ssize_t len;
4161 if (res == NULL)
4162 return -1;
4163 len = PyInt_AsSsize_t(res);
4164 Py_DECREF(res);
4165 if (len < 0) {
4166 if (!PyErr_Occurred())
4167 PyErr_SetString(PyExc_ValueError,
4168 "__len__() should return >= 0");
4169 return -1;
4171 return len;
4174 /* Super-optimized version of slot_sq_item.
4175 Other slots could do the same... */
4176 static PyObject *
4177 slot_sq_item(PyObject *self, Py_ssize_t i)
4179 static PyObject *getitem_str;
4180 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4181 descrgetfunc f;
4183 if (getitem_str == NULL) {
4184 getitem_str = PyString_InternFromString("__getitem__");
4185 if (getitem_str == NULL)
4186 return NULL;
4188 func = _PyType_Lookup(self->ob_type, getitem_str);
4189 if (func != NULL) {
4190 if ((f = func->ob_type->tp_descr_get) == NULL)
4191 Py_INCREF(func);
4192 else {
4193 func = f(func, self, (PyObject *)(self->ob_type));
4194 if (func == NULL) {
4195 return NULL;
4198 ival = PyInt_FromSsize_t(i);
4199 if (ival != NULL) {
4200 args = PyTuple_New(1);
4201 if (args != NULL) {
4202 PyTuple_SET_ITEM(args, 0, ival);
4203 retval = PyObject_Call(func, args, NULL);
4204 Py_XDECREF(args);
4205 Py_XDECREF(func);
4206 return retval;
4210 else {
4211 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4213 Py_XDECREF(args);
4214 Py_XDECREF(ival);
4215 Py_XDECREF(func);
4216 return NULL;
4219 SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
4221 static int
4222 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
4224 PyObject *res;
4225 static PyObject *delitem_str, *setitem_str;
4227 if (value == NULL)
4228 res = call_method(self, "__delitem__", &delitem_str,
4229 "(n)", index);
4230 else
4231 res = call_method(self, "__setitem__", &setitem_str,
4232 "(nO)", index, value);
4233 if (res == NULL)
4234 return -1;
4235 Py_DECREF(res);
4236 return 0;
4239 static int
4240 slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
4242 PyObject *res;
4243 static PyObject *delslice_str, *setslice_str;
4245 if (value == NULL)
4246 res = call_method(self, "__delslice__", &delslice_str,
4247 "(nn)", i, j);
4248 else
4249 res = call_method(self, "__setslice__", &setslice_str,
4250 "(nnO)", i, j, value);
4251 if (res == NULL)
4252 return -1;
4253 Py_DECREF(res);
4254 return 0;
4257 static int
4258 slot_sq_contains(PyObject *self, PyObject *value)
4260 PyObject *func, *res, *args;
4261 int result = -1;
4263 static PyObject *contains_str;
4265 func = lookup_maybe(self, "__contains__", &contains_str);
4266 if (func != NULL) {
4267 args = PyTuple_Pack(1, value);
4268 if (args == NULL)
4269 res = NULL;
4270 else {
4271 res = PyObject_Call(func, args, NULL);
4272 Py_DECREF(args);
4274 Py_DECREF(func);
4275 if (res != NULL) {
4276 result = PyObject_IsTrue(res);
4277 Py_DECREF(res);
4280 else if (! PyErr_Occurred()) {
4281 /* Possible results: -1 and 1 */
4282 result = (int)_PySequence_IterSearch(self, value,
4283 PY_ITERSEARCH_CONTAINS);
4285 return result;
4288 #define slot_mp_length slot_sq_length
4290 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4292 static int
4293 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4295 PyObject *res;
4296 static PyObject *delitem_str, *setitem_str;
4298 if (value == NULL)
4299 res = call_method(self, "__delitem__", &delitem_str,
4300 "(O)", key);
4301 else
4302 res = call_method(self, "__setitem__", &setitem_str,
4303 "(OO)", key, value);
4304 if (res == NULL)
4305 return -1;
4306 Py_DECREF(res);
4307 return 0;
4310 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4311 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4312 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4313 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4314 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4315 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4317 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4319 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4320 nb_power, "__pow__", "__rpow__")
4322 static PyObject *
4323 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4325 static PyObject *pow_str;
4327 if (modulus == Py_None)
4328 return slot_nb_power_binary(self, other);
4329 /* Three-arg power doesn't use __rpow__. But ternary_op
4330 can call this when the second argument's type uses
4331 slot_nb_power, so check before calling self.__pow__. */
4332 if (self->ob_type->tp_as_number != NULL &&
4333 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4334 return call_method(self, "__pow__", &pow_str,
4335 "(OO)", other, modulus);
4337 Py_INCREF(Py_NotImplemented);
4338 return Py_NotImplemented;
4341 SLOT0(slot_nb_negative, "__neg__")
4342 SLOT0(slot_nb_positive, "__pos__")
4343 SLOT0(slot_nb_absolute, "__abs__")
4345 static int
4346 slot_nb_nonzero(PyObject *self)
4348 PyObject *func, *args;
4349 static PyObject *nonzero_str, *len_str;
4350 int result = -1;
4352 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4353 if (func == NULL) {
4354 if (PyErr_Occurred())
4355 return -1;
4356 func = lookup_maybe(self, "__len__", &len_str);
4357 if (func == NULL)
4358 return PyErr_Occurred() ? -1 : 1;
4360 args = PyTuple_New(0);
4361 if (args != NULL) {
4362 PyObject *temp = PyObject_Call(func, args, NULL);
4363 Py_DECREF(args);
4364 if (temp != NULL) {
4365 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4366 result = PyObject_IsTrue(temp);
4367 else {
4368 PyErr_Format(PyExc_TypeError,
4369 "__nonzero__ should return "
4370 "bool or int, returned %s",
4371 temp->ob_type->tp_name);
4372 result = -1;
4374 Py_DECREF(temp);
4377 Py_DECREF(func);
4378 return result;
4382 static PyObject *
4383 slot_nb_index(PyObject *self)
4385 static PyObject *index_str;
4386 return call_method(self, "__index__", &index_str, "()");
4390 SLOT0(slot_nb_invert, "__invert__")
4391 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4392 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4393 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4394 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4395 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4397 static int
4398 slot_nb_coerce(PyObject **a, PyObject **b)
4400 static PyObject *coerce_str;
4401 PyObject *self = *a, *other = *b;
4403 if (self->ob_type->tp_as_number != NULL &&
4404 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4405 PyObject *r;
4406 r = call_maybe(
4407 self, "__coerce__", &coerce_str, "(O)", other);
4408 if (r == NULL)
4409 return -1;
4410 if (r == Py_NotImplemented) {
4411 Py_DECREF(r);
4413 else {
4414 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4415 PyErr_SetString(PyExc_TypeError,
4416 "__coerce__ didn't return a 2-tuple");
4417 Py_DECREF(r);
4418 return -1;
4420 *a = PyTuple_GET_ITEM(r, 0);
4421 Py_INCREF(*a);
4422 *b = PyTuple_GET_ITEM(r, 1);
4423 Py_INCREF(*b);
4424 Py_DECREF(r);
4425 return 0;
4428 if (other->ob_type->tp_as_number != NULL &&
4429 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4430 PyObject *r;
4431 r = call_maybe(
4432 other, "__coerce__", &coerce_str, "(O)", self);
4433 if (r == NULL)
4434 return -1;
4435 if (r == Py_NotImplemented) {
4436 Py_DECREF(r);
4437 return 1;
4439 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4440 PyErr_SetString(PyExc_TypeError,
4441 "__coerce__ didn't return a 2-tuple");
4442 Py_DECREF(r);
4443 return -1;
4445 *a = PyTuple_GET_ITEM(r, 1);
4446 Py_INCREF(*a);
4447 *b = PyTuple_GET_ITEM(r, 0);
4448 Py_INCREF(*b);
4449 Py_DECREF(r);
4450 return 0;
4452 return 1;
4455 SLOT0(slot_nb_int, "__int__")
4456 SLOT0(slot_nb_long, "__long__")
4457 SLOT0(slot_nb_float, "__float__")
4458 SLOT0(slot_nb_oct, "__oct__")
4459 SLOT0(slot_nb_hex, "__hex__")
4460 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4461 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4462 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4463 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4464 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4465 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
4466 static PyObject *
4467 slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4469 static PyObject *cache_str;
4470 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4472 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4473 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4474 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4475 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4476 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4477 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4478 "__floordiv__", "__rfloordiv__")
4479 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4480 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4481 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4483 static int
4484 half_compare(PyObject *self, PyObject *other)
4486 PyObject *func, *args, *res;
4487 static PyObject *cmp_str;
4488 Py_ssize_t c;
4490 func = lookup_method(self, "__cmp__", &cmp_str);
4491 if (func == NULL) {
4492 PyErr_Clear();
4494 else {
4495 args = PyTuple_Pack(1, other);
4496 if (args == NULL)
4497 res = NULL;
4498 else {
4499 res = PyObject_Call(func, args, NULL);
4500 Py_DECREF(args);
4502 Py_DECREF(func);
4503 if (res != Py_NotImplemented) {
4504 if (res == NULL)
4505 return -2;
4506 c = PyInt_AsLong(res);
4507 Py_DECREF(res);
4508 if (c == -1 && PyErr_Occurred())
4509 return -2;
4510 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4512 Py_DECREF(res);
4514 return 2;
4517 /* This slot is published for the benefit of try_3way_compare in object.c */
4519 _PyObject_SlotCompare(PyObject *self, PyObject *other)
4521 int c;
4523 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4524 c = half_compare(self, other);
4525 if (c <= 1)
4526 return c;
4528 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4529 c = half_compare(other, self);
4530 if (c < -1)
4531 return -2;
4532 if (c <= 1)
4533 return -c;
4535 return (void *)self < (void *)other ? -1 :
4536 (void *)self > (void *)other ? 1 : 0;
4539 static PyObject *
4540 slot_tp_repr(PyObject *self)
4542 PyObject *func, *res;
4543 static PyObject *repr_str;
4545 func = lookup_method(self, "__repr__", &repr_str);
4546 if (func != NULL) {
4547 res = PyEval_CallObject(func, NULL);
4548 Py_DECREF(func);
4549 return res;
4551 PyErr_Clear();
4552 return PyString_FromFormat("<%s object at %p>",
4553 self->ob_type->tp_name, self);
4556 static PyObject *
4557 slot_tp_str(PyObject *self)
4559 PyObject *func, *res;
4560 static PyObject *str_str;
4562 func = lookup_method(self, "__str__", &str_str);
4563 if (func != NULL) {
4564 res = PyEval_CallObject(func, NULL);
4565 Py_DECREF(func);
4566 return res;
4568 else {
4569 PyErr_Clear();
4570 return slot_tp_repr(self);
4574 static long
4575 slot_tp_hash(PyObject *self)
4577 PyObject *func;
4578 static PyObject *hash_str, *eq_str, *cmp_str;
4579 long h;
4581 func = lookup_method(self, "__hash__", &hash_str);
4583 if (func != NULL) {
4584 PyObject *res = PyEval_CallObject(func, NULL);
4585 Py_DECREF(func);
4586 if (res == NULL)
4587 return -1;
4588 if (PyLong_Check(res))
4589 h = PyLong_Type.tp_hash(res);
4590 else
4591 h = PyInt_AsLong(res);
4592 Py_DECREF(res);
4594 else {
4595 PyErr_Clear();
4596 func = lookup_method(self, "__eq__", &eq_str);
4597 if (func == NULL) {
4598 PyErr_Clear();
4599 func = lookup_method(self, "__cmp__", &cmp_str);
4601 if (func != NULL) {
4602 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
4603 self->ob_type->tp_name);
4604 Py_DECREF(func);
4605 return -1;
4607 PyErr_Clear();
4608 h = _Py_HashPointer((void *)self);
4610 if (h == -1 && !PyErr_Occurred())
4611 h = -2;
4612 return h;
4615 static PyObject *
4616 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4618 static PyObject *call_str;
4619 PyObject *meth = lookup_method(self, "__call__", &call_str);
4620 PyObject *res;
4622 if (meth == NULL)
4623 return NULL;
4625 /* PyObject_Call() will end up calling slot_tp_call() again if
4626 the object returned for __call__ has __call__ itself defined
4627 upon it. This can be an infinite recursion if you set
4628 __call__ in a class to an instance of it. */
4629 if (Py_EnterRecursiveCall(" in __call__")) {
4630 Py_DECREF(meth);
4631 return NULL;
4633 res = PyObject_Call(meth, args, kwds);
4634 Py_LeaveRecursiveCall();
4636 Py_DECREF(meth);
4637 return res;
4640 /* There are two slot dispatch functions for tp_getattro.
4642 - slot_tp_getattro() is used when __getattribute__ is overridden
4643 but no __getattr__ hook is present;
4645 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4647 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4648 detects the absence of __getattr__ and then installs the simpler slot if
4649 necessary. */
4651 static PyObject *
4652 slot_tp_getattro(PyObject *self, PyObject *name)
4654 static PyObject *getattribute_str = NULL;
4655 return call_method(self, "__getattribute__", &getattribute_str,
4656 "(O)", name);
4659 static PyObject *
4660 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4662 PyTypeObject *tp = self->ob_type;
4663 PyObject *getattr, *getattribute, *res;
4664 static PyObject *getattribute_str = NULL;
4665 static PyObject *getattr_str = NULL;
4667 if (getattr_str == NULL) {
4668 getattr_str = PyString_InternFromString("__getattr__");
4669 if (getattr_str == NULL)
4670 return NULL;
4672 if (getattribute_str == NULL) {
4673 getattribute_str =
4674 PyString_InternFromString("__getattribute__");
4675 if (getattribute_str == NULL)
4676 return NULL;
4678 getattr = _PyType_Lookup(tp, getattr_str);
4679 if (getattr == NULL) {
4680 /* No __getattr__ hook: use a simpler dispatcher */
4681 tp->tp_getattro = slot_tp_getattro;
4682 return slot_tp_getattro(self, name);
4684 getattribute = _PyType_Lookup(tp, getattribute_str);
4685 if (getattribute == NULL ||
4686 (getattribute->ob_type == &PyWrapperDescr_Type &&
4687 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4688 (void *)PyObject_GenericGetAttr))
4689 res = PyObject_GenericGetAttr(self, name);
4690 else
4691 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
4692 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4693 PyErr_Clear();
4694 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
4696 return res;
4699 static int
4700 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4702 PyObject *res;
4703 static PyObject *delattr_str, *setattr_str;
4705 if (value == NULL)
4706 res = call_method(self, "__delattr__", &delattr_str,
4707 "(O)", name);
4708 else
4709 res = call_method(self, "__setattr__", &setattr_str,
4710 "(OO)", name, value);
4711 if (res == NULL)
4712 return -1;
4713 Py_DECREF(res);
4714 return 0;
4717 /* Map rich comparison operators to their __xx__ namesakes */
4718 static char *name_op[] = {
4719 "__lt__",
4720 "__le__",
4721 "__eq__",
4722 "__ne__",
4723 "__gt__",
4724 "__ge__",
4727 static PyObject *
4728 half_richcompare(PyObject *self, PyObject *other, int op)
4730 PyObject *func, *args, *res;
4731 static PyObject *op_str[6];
4733 func = lookup_method(self, name_op[op], &op_str[op]);
4734 if (func == NULL) {
4735 PyErr_Clear();
4736 Py_INCREF(Py_NotImplemented);
4737 return Py_NotImplemented;
4739 args = PyTuple_Pack(1, other);
4740 if (args == NULL)
4741 res = NULL;
4742 else {
4743 res = PyObject_Call(func, args, NULL);
4744 Py_DECREF(args);
4746 Py_DECREF(func);
4747 return res;
4750 static PyObject *
4751 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4753 PyObject *res;
4755 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4756 res = half_richcompare(self, other, op);
4757 if (res != Py_NotImplemented)
4758 return res;
4759 Py_DECREF(res);
4761 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4762 res = half_richcompare(other, self, _Py_SwappedOp[op]);
4763 if (res != Py_NotImplemented) {
4764 return res;
4766 Py_DECREF(res);
4768 Py_INCREF(Py_NotImplemented);
4769 return Py_NotImplemented;
4772 static PyObject *
4773 slot_tp_iter(PyObject *self)
4775 PyObject *func, *res;
4776 static PyObject *iter_str, *getitem_str;
4778 func = lookup_method(self, "__iter__", &iter_str);
4779 if (func != NULL) {
4780 PyObject *args;
4781 args = res = PyTuple_New(0);
4782 if (args != NULL) {
4783 res = PyObject_Call(func, args, NULL);
4784 Py_DECREF(args);
4786 Py_DECREF(func);
4787 return res;
4789 PyErr_Clear();
4790 func = lookup_method(self, "__getitem__", &getitem_str);
4791 if (func == NULL) {
4792 PyErr_Format(PyExc_TypeError,
4793 "'%.200s' object is not iterable",
4794 self->ob_type->tp_name);
4795 return NULL;
4797 Py_DECREF(func);
4798 return PySeqIter_New(self);
4801 static PyObject *
4802 slot_tp_iternext(PyObject *self)
4804 static PyObject *next_str;
4805 return call_method(self, "next", &next_str, "()");
4808 static PyObject *
4809 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4811 PyTypeObject *tp = self->ob_type;
4812 PyObject *get;
4813 static PyObject *get_str = NULL;
4815 if (get_str == NULL) {
4816 get_str = PyString_InternFromString("__get__");
4817 if (get_str == NULL)
4818 return NULL;
4820 get = _PyType_Lookup(tp, get_str);
4821 if (get == NULL) {
4822 /* Avoid further slowdowns */
4823 if (tp->tp_descr_get == slot_tp_descr_get)
4824 tp->tp_descr_get = NULL;
4825 Py_INCREF(self);
4826 return self;
4828 if (obj == NULL)
4829 obj = Py_None;
4830 if (type == NULL)
4831 type = Py_None;
4832 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
4835 static int
4836 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4838 PyObject *res;
4839 static PyObject *del_str, *set_str;
4841 if (value == NULL)
4842 res = call_method(self, "__delete__", &del_str,
4843 "(O)", target);
4844 else
4845 res = call_method(self, "__set__", &set_str,
4846 "(OO)", target, value);
4847 if (res == NULL)
4848 return -1;
4849 Py_DECREF(res);
4850 return 0;
4853 static int
4854 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4856 static PyObject *init_str;
4857 PyObject *meth = lookup_method(self, "__init__", &init_str);
4858 PyObject *res;
4860 if (meth == NULL)
4861 return -1;
4862 res = PyObject_Call(meth, args, kwds);
4863 Py_DECREF(meth);
4864 if (res == NULL)
4865 return -1;
4866 if (res != Py_None) {
4867 PyErr_Format(PyExc_TypeError,
4868 "__init__() should return None, not '%.200s'",
4869 res->ob_type->tp_name);
4870 Py_DECREF(res);
4871 return -1;
4873 Py_DECREF(res);
4874 return 0;
4877 static PyObject *
4878 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4880 static PyObject *new_str;
4881 PyObject *func;
4882 PyObject *newargs, *x;
4883 Py_ssize_t i, n;
4885 if (new_str == NULL) {
4886 new_str = PyString_InternFromString("__new__");
4887 if (new_str == NULL)
4888 return NULL;
4890 func = PyObject_GetAttr((PyObject *)type, new_str);
4891 if (func == NULL)
4892 return NULL;
4893 assert(PyTuple_Check(args));
4894 n = PyTuple_GET_SIZE(args);
4895 newargs = PyTuple_New(n+1);
4896 if (newargs == NULL)
4897 return NULL;
4898 Py_INCREF(type);
4899 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4900 for (i = 0; i < n; i++) {
4901 x = PyTuple_GET_ITEM(args, i);
4902 Py_INCREF(x);
4903 PyTuple_SET_ITEM(newargs, i+1, x);
4905 x = PyObject_Call(func, newargs, kwds);
4906 Py_DECREF(newargs);
4907 Py_DECREF(func);
4908 return x;
4911 static void
4912 slot_tp_del(PyObject *self)
4914 static PyObject *del_str = NULL;
4915 PyObject *del, *res;
4916 PyObject *error_type, *error_value, *error_traceback;
4918 /* Temporarily resurrect the object. */
4919 assert(self->ob_refcnt == 0);
4920 self->ob_refcnt = 1;
4922 /* Save the current exception, if any. */
4923 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4925 /* Execute __del__ method, if any. */
4926 del = lookup_maybe(self, "__del__", &del_str);
4927 if (del != NULL) {
4928 res = PyEval_CallObject(del, NULL);
4929 if (res == NULL)
4930 PyErr_WriteUnraisable(del);
4931 else
4932 Py_DECREF(res);
4933 Py_DECREF(del);
4936 /* Restore the saved exception. */
4937 PyErr_Restore(error_type, error_value, error_traceback);
4939 /* Undo the temporary resurrection; can't use DECREF here, it would
4940 * cause a recursive call.
4942 assert(self->ob_refcnt > 0);
4943 if (--self->ob_refcnt == 0)
4944 return; /* this is the normal path out */
4946 /* __del__ resurrected it! Make it look like the original Py_DECREF
4947 * never happened.
4950 Py_ssize_t refcnt = self->ob_refcnt;
4951 _Py_NewReference(self);
4952 self->ob_refcnt = refcnt;
4954 assert(!PyType_IS_GC(self->ob_type) ||
4955 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4956 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4957 * we need to undo that. */
4958 _Py_DEC_REFTOTAL;
4959 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4960 * chain, so no more to do there.
4961 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4962 * _Py_NewReference bumped tp_allocs: both of those need to be
4963 * undone.
4965 #ifdef COUNT_ALLOCS
4966 --self->ob_type->tp_frees;
4967 --self->ob_type->tp_allocs;
4968 #endif
4972 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4973 functions. The offsets here are relative to the 'PyHeapTypeObject'
4974 structure, which incorporates the additional structures used for numbers,
4975 sequences and mappings.
4976 Note that multiple names may map to the same slot (e.g. __eq__,
4977 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4978 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4979 terminated with an all-zero entry. (This table is further initialized and
4980 sorted in init_slotdefs() below.) */
4982 typedef struct wrapperbase slotdef;
4984 #undef TPSLOT
4985 #undef FLSLOT
4986 #undef ETSLOT
4987 #undef SQSLOT
4988 #undef MPSLOT
4989 #undef NBSLOT
4990 #undef UNSLOT
4991 #undef IBSLOT
4992 #undef BINSLOT
4993 #undef RBINSLOT
4995 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4996 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4997 PyDoc_STR(DOC)}
4998 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4999 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5000 PyDoc_STR(DOC), FLAGS}
5001 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5002 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5003 PyDoc_STR(DOC)}
5004 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5005 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5006 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5007 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5008 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5009 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5010 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5011 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5012 "x." NAME "() <==> " DOC)
5013 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5014 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5015 "x." NAME "(y) <==> x" DOC "y")
5016 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5017 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5018 "x." NAME "(y) <==> x" DOC "y")
5019 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5020 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5021 "x." NAME "(y) <==> y" DOC "x")
5022 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5023 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5024 "x." NAME "(y) <==> " DOC)
5025 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5026 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5027 "x." NAME "(y) <==> " DOC)
5029 static slotdef slotdefs[] = {
5030 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
5031 "x.__len__() <==> len(x)"),
5032 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5033 The logic in abstract.c always falls back to nb_add/nb_multiply in
5034 this case. Defining both the nb_* and the sq_* slots to call the
5035 user-defined methods has unexpected side-effects, as shown by
5036 test_descr.notimplemented() */
5037 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
5038 "x.__add__(y) <==> x+y"),
5039 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
5040 "x.__mul__(n) <==> x*n"),
5041 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
5042 "x.__rmul__(n) <==> n*x"),
5043 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5044 "x.__getitem__(y) <==> x[y]"),
5045 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
5046 "x.__getslice__(i, j) <==> x[i:j]\n\
5048 Use of negative indices is not supported."),
5049 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
5050 "x.__setitem__(i, y) <==> x[i]=y"),
5051 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
5052 "x.__delitem__(y) <==> del x[y]"),
5053 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
5054 wrap_ssizessizeobjargproc,
5055 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
5057 Use of negative indices is not supported."),
5058 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
5059 "x.__delslice__(i, j) <==> del x[i:j]\n\
5061 Use of negative indices is not supported."),
5062 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5063 "x.__contains__(y) <==> y in x"),
5064 SQSLOT("__iadd__", sq_inplace_concat, NULL,
5065 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
5066 SQSLOT("__imul__", sq_inplace_repeat, NULL,
5067 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
5069 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
5070 "x.__len__() <==> len(x)"),
5071 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
5072 wrap_binaryfunc,
5073 "x.__getitem__(y) <==> x[y]"),
5074 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
5075 wrap_objobjargproc,
5076 "x.__setitem__(i, y) <==> x[i]=y"),
5077 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
5078 wrap_delitem,
5079 "x.__delitem__(y) <==> del x[y]"),
5081 BINSLOT("__add__", nb_add, slot_nb_add,
5082 "+"),
5083 RBINSLOT("__radd__", nb_add, slot_nb_add,
5084 "+"),
5085 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5086 "-"),
5087 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5088 "-"),
5089 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5090 "*"),
5091 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5092 "*"),
5093 BINSLOT("__div__", nb_divide, slot_nb_divide,
5094 "/"),
5095 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
5096 "/"),
5097 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5098 "%"),
5099 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5100 "%"),
5101 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
5102 "divmod(x, y)"),
5103 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
5104 "divmod(y, x)"),
5105 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5106 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5107 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5108 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5109 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5110 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5111 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5112 "abs(x)"),
5113 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
5114 "x != 0"),
5115 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5116 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5117 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5118 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5119 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5120 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5121 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5122 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5123 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5124 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5125 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5126 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5127 "x.__coerce__(y) <==> coerce(x, y)"),
5128 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5129 "int(x)"),
5130 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5131 "long(x)"),
5132 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5133 "float(x)"),
5134 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5135 "oct(x)"),
5136 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5137 "hex(x)"),
5138 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
5139 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5140 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5141 wrap_binaryfunc, "+"),
5142 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5143 wrap_binaryfunc, "-"),
5144 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5145 wrap_binaryfunc, "*"),
5146 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5147 wrap_binaryfunc, "/"),
5148 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5149 wrap_binaryfunc, "%"),
5150 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5151 wrap_binaryfunc, "**"),
5152 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5153 wrap_binaryfunc, "<<"),
5154 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5155 wrap_binaryfunc, ">>"),
5156 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5157 wrap_binaryfunc, "&"),
5158 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5159 wrap_binaryfunc, "^"),
5160 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5161 wrap_binaryfunc, "|"),
5162 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5163 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5164 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5165 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5166 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5167 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5168 IBSLOT("__itruediv__", nb_inplace_true_divide,
5169 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5171 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5172 "x.__str__() <==> str(x)"),
5173 TPSLOT("__str__", tp_print, NULL, NULL, ""),
5174 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5175 "x.__repr__() <==> repr(x)"),
5176 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5177 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5178 "x.__cmp__(y) <==> cmp(x,y)"),
5179 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5180 "x.__hash__() <==> hash(x)"),
5181 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5182 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5183 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5184 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5185 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5186 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5187 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5188 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5189 "x.__setattr__('name', value) <==> x.name = value"),
5190 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5191 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5192 "x.__delattr__('name') <==> del x.name"),
5193 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5194 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5195 "x.__lt__(y) <==> x<y"),
5196 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5197 "x.__le__(y) <==> x<=y"),
5198 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5199 "x.__eq__(y) <==> x==y"),
5200 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5201 "x.__ne__(y) <==> x!=y"),
5202 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5203 "x.__gt__(y) <==> x>y"),
5204 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5205 "x.__ge__(y) <==> x>=y"),
5206 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5207 "x.__iter__() <==> iter(x)"),
5208 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5209 "x.next() -> the next value, or raise StopIteration"),
5210 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5211 "descr.__get__(obj[, type]) -> value"),
5212 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5213 "descr.__set__(obj, value)"),
5214 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5215 wrap_descr_delete, "descr.__delete__(obj)"),
5216 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5217 "x.__init__(...) initializes x; "
5218 "see x.__class__.__doc__ for signature",
5219 PyWrapperFlag_KEYWORDS),
5220 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5221 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5222 {NULL}
5225 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5226 pointer to the actual slot. This is not quite the same as simply adding
5227 the offset to the type pointer, since it takes care to indirect through the
5228 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5229 indirection pointer is NULL. */
5230 static void **
5231 slotptr(PyTypeObject *type, int ioffset)
5233 char *ptr;
5234 long offset = ioffset;
5236 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5237 assert(offset >= 0);
5238 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5239 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5240 ptr = (char *)type->tp_as_sequence;
5241 offset -= offsetof(PyHeapTypeObject, as_sequence);
5243 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5244 ptr = (char *)type->tp_as_mapping;
5245 offset -= offsetof(PyHeapTypeObject, as_mapping);
5247 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5248 ptr = (char *)type->tp_as_number;
5249 offset -= offsetof(PyHeapTypeObject, as_number);
5251 else {
5252 ptr = (char *)type;
5254 if (ptr != NULL)
5255 ptr += offset;
5256 return (void **)ptr;
5259 /* Length of array of slotdef pointers used to store slots with the
5260 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5261 the same __name__, for any __name__. Since that's a static property, it is
5262 appropriate to declare fixed-size arrays for this. */
5263 #define MAX_EQUIV 10
5265 /* Return a slot pointer for a given name, but ONLY if the attribute has
5266 exactly one slot function. The name must be an interned string. */
5267 static void **
5268 resolve_slotdups(PyTypeObject *type, PyObject *name)
5270 /* XXX Maybe this could be optimized more -- but is it worth it? */
5272 /* pname and ptrs act as a little cache */
5273 static PyObject *pname;
5274 static slotdef *ptrs[MAX_EQUIV];
5275 slotdef *p, **pp;
5276 void **res, **ptr;
5278 if (pname != name) {
5279 /* Collect all slotdefs that match name into ptrs. */
5280 pname = name;
5281 pp = ptrs;
5282 for (p = slotdefs; p->name_strobj; p++) {
5283 if (p->name_strobj == name)
5284 *pp++ = p;
5286 *pp = NULL;
5289 /* Look in all matching slots of the type; if exactly one of these has
5290 a filled-in slot, return its value. Otherwise return NULL. */
5291 res = NULL;
5292 for (pp = ptrs; *pp; pp++) {
5293 ptr = slotptr(type, (*pp)->offset);
5294 if (ptr == NULL || *ptr == NULL)
5295 continue;
5296 if (res != NULL)
5297 return NULL;
5298 res = ptr;
5300 return res;
5303 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5304 does some incredibly complex thinking and then sticks something into the
5305 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5306 interests, and then stores a generic wrapper or a specific function into
5307 the slot.) Return a pointer to the next slotdef with a different offset,
5308 because that's convenient for fixup_slot_dispatchers(). */
5309 static slotdef *
5310 update_one_slot(PyTypeObject *type, slotdef *p)
5312 PyObject *descr;
5313 PyWrapperDescrObject *d;
5314 void *generic = NULL, *specific = NULL;
5315 int use_generic = 0;
5316 int offset = p->offset;
5317 void **ptr = slotptr(type, offset);
5319 if (ptr == NULL) {
5320 do {
5321 ++p;
5322 } while (p->offset == offset);
5323 return p;
5325 do {
5326 descr = _PyType_Lookup(type, p->name_strobj);
5327 if (descr == NULL)
5328 continue;
5329 if (descr->ob_type == &PyWrapperDescr_Type) {
5330 void **tptr = resolve_slotdups(type, p->name_strobj);
5331 if (tptr == NULL || tptr == ptr)
5332 generic = p->function;
5333 d = (PyWrapperDescrObject *)descr;
5334 if (d->d_base->wrapper == p->wrapper &&
5335 PyType_IsSubtype(type, d->d_type))
5337 if (specific == NULL ||
5338 specific == d->d_wrapped)
5339 specific = d->d_wrapped;
5340 else
5341 use_generic = 1;
5344 else if (descr->ob_type == &PyCFunction_Type &&
5345 PyCFunction_GET_FUNCTION(descr) ==
5346 (PyCFunction)tp_new_wrapper &&
5347 strcmp(p->name, "__new__") == 0)
5349 /* The __new__ wrapper is not a wrapper descriptor,
5350 so must be special-cased differently.
5351 If we don't do this, creating an instance will
5352 always use slot_tp_new which will look up
5353 __new__ in the MRO which will call tp_new_wrapper
5354 which will look through the base classes looking
5355 for a static base and call its tp_new (usually
5356 PyType_GenericNew), after performing various
5357 sanity checks and constructing a new argument
5358 list. Cut all that nonsense short -- this speeds
5359 up instance creation tremendously. */
5360 specific = (void *)type->tp_new;
5361 /* XXX I'm not 100% sure that there isn't a hole
5362 in this reasoning that requires additional
5363 sanity checks. I'll buy the first person to
5364 point out a bug in this reasoning a beer. */
5366 else {
5367 use_generic = 1;
5368 generic = p->function;
5370 } while ((++p)->offset == offset);
5371 if (specific && !use_generic)
5372 *ptr = specific;
5373 else
5374 *ptr = generic;
5375 return p;
5378 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5379 This is a callback for update_subclasses(). */
5380 static int
5381 update_slots_callback(PyTypeObject *type, void *data)
5383 slotdef **pp = (slotdef **)data;
5385 for (; *pp; pp++)
5386 update_one_slot(type, *pp);
5387 return 0;
5390 /* Comparison function for qsort() to compare slotdefs by their offset, and
5391 for equal offset by their address (to force a stable sort). */
5392 static int
5393 slotdef_cmp(const void *aa, const void *bb)
5395 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5396 int c = a->offset - b->offset;
5397 if (c != 0)
5398 return c;
5399 else
5400 /* Cannot use a-b, as this gives off_t,
5401 which may lose precision when converted to int. */
5402 return (a > b) ? 1 : (a < b) ? -1 : 0;
5405 /* Initialize the slotdefs table by adding interned string objects for the
5406 names and sorting the entries. */
5407 static void
5408 init_slotdefs(void)
5410 slotdef *p;
5411 static int initialized = 0;
5413 if (initialized)
5414 return;
5415 for (p = slotdefs; p->name; p++) {
5416 p->name_strobj = PyString_InternFromString(p->name);
5417 if (!p->name_strobj)
5418 Py_FatalError("Out of memory interning slotdef names");
5420 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5421 slotdef_cmp);
5422 initialized = 1;
5425 /* Update the slots after assignment to a class (type) attribute. */
5426 static int
5427 update_slot(PyTypeObject *type, PyObject *name)
5429 slotdef *ptrs[MAX_EQUIV];
5430 slotdef *p;
5431 slotdef **pp;
5432 int offset;
5434 init_slotdefs();
5435 pp = ptrs;
5436 for (p = slotdefs; p->name; p++) {
5437 /* XXX assume name is interned! */
5438 if (p->name_strobj == name)
5439 *pp++ = p;
5441 *pp = NULL;
5442 for (pp = ptrs; *pp; pp++) {
5443 p = *pp;
5444 offset = p->offset;
5445 while (p > slotdefs && (p-1)->offset == offset)
5446 --p;
5447 *pp = p;
5449 if (ptrs[0] == NULL)
5450 return 0; /* Not an attribute that affects any slots */
5451 return update_subclasses(type, name,
5452 update_slots_callback, (void *)ptrs);
5455 /* Store the proper functions in the slot dispatches at class (type)
5456 definition time, based upon which operations the class overrides in its
5457 dict. */
5458 static void
5459 fixup_slot_dispatchers(PyTypeObject *type)
5461 slotdef *p;
5463 init_slotdefs();
5464 for (p = slotdefs; p->name; )
5465 p = update_one_slot(type, p);
5468 static void
5469 update_all_slots(PyTypeObject* type)
5471 slotdef *p;
5473 init_slotdefs();
5474 for (p = slotdefs; p->name; p++) {
5475 /* update_slot returns int but can't actually fail */
5476 update_slot(type, p->name_strobj);
5480 /* recurse_down_subclasses() and update_subclasses() are mutually
5481 recursive functions to call a callback for all subclasses,
5482 but refraining from recursing into subclasses that define 'name'. */
5484 static int
5485 update_subclasses(PyTypeObject *type, PyObject *name,
5486 update_callback callback, void *data)
5488 if (callback(type, data) < 0)
5489 return -1;
5490 return recurse_down_subclasses(type, name, callback, data);
5493 static int
5494 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5495 update_callback callback, void *data)
5497 PyTypeObject *subclass;
5498 PyObject *ref, *subclasses, *dict;
5499 Py_ssize_t i, n;
5501 subclasses = type->tp_subclasses;
5502 if (subclasses == NULL)
5503 return 0;
5504 assert(PyList_Check(subclasses));
5505 n = PyList_GET_SIZE(subclasses);
5506 for (i = 0; i < n; i++) {
5507 ref = PyList_GET_ITEM(subclasses, i);
5508 assert(PyWeakref_CheckRef(ref));
5509 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5510 assert(subclass != NULL);
5511 if ((PyObject *)subclass == Py_None)
5512 continue;
5513 assert(PyType_Check(subclass));
5514 /* Avoid recursing down into unaffected classes */
5515 dict = subclass->tp_dict;
5516 if (dict != NULL && PyDict_Check(dict) &&
5517 PyDict_GetItem(dict, name) != NULL)
5518 continue;
5519 if (update_subclasses(subclass, name, callback, data) < 0)
5520 return -1;
5522 return 0;
5525 /* This function is called by PyType_Ready() to populate the type's
5526 dictionary with method descriptors for function slots. For each
5527 function slot (like tp_repr) that's defined in the type, one or more
5528 corresponding descriptors are added in the type's tp_dict dictionary
5529 under the appropriate name (like __repr__). Some function slots
5530 cause more than one descriptor to be added (for example, the nb_add
5531 slot adds both __add__ and __radd__ descriptors) and some function
5532 slots compete for the same descriptor (for example both sq_item and
5533 mp_subscript generate a __getitem__ descriptor).
5535 In the latter case, the first slotdef entry encoutered wins. Since
5536 slotdef entries are sorted by the offset of the slot in the
5537 PyHeapTypeObject, this gives us some control over disambiguating
5538 between competing slots: the members of PyHeapTypeObject are listed
5539 from most general to least general, so the most general slot is
5540 preferred. In particular, because as_mapping comes before as_sequence,
5541 for a type that defines both mp_subscript and sq_item, mp_subscript
5542 wins.
5544 This only adds new descriptors and doesn't overwrite entries in
5545 tp_dict that were previously defined. The descriptors contain a
5546 reference to the C function they must call, so that it's safe if they
5547 are copied into a subtype's __dict__ and the subtype has a different
5548 C function in its slot -- calling the method defined by the
5549 descriptor will call the C function that was used to create it,
5550 rather than the C function present in the slot when it is called.
5551 (This is important because a subtype may have a C function in the
5552 slot that calls the method from the dictionary, and we want to avoid
5553 infinite recursion here.) */
5555 static int
5556 add_operators(PyTypeObject *type)
5558 PyObject *dict = type->tp_dict;
5559 slotdef *p;
5560 PyObject *descr;
5561 void **ptr;
5563 init_slotdefs();
5564 for (p = slotdefs; p->name; p++) {
5565 if (p->wrapper == NULL)
5566 continue;
5567 ptr = slotptr(type, p->offset);
5568 if (!ptr || !*ptr)
5569 continue;
5570 if (PyDict_GetItem(dict, p->name_strobj))
5571 continue;
5572 descr = PyDescr_NewWrapper(type, p, *ptr);
5573 if (descr == NULL)
5574 return -1;
5575 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5576 return -1;
5577 Py_DECREF(descr);
5579 if (type->tp_new != NULL) {
5580 if (add_tp_new_wrapper(type) < 0)
5581 return -1;
5583 return 0;
5587 /* Cooperative 'super' */
5589 typedef struct {
5590 PyObject_HEAD
5591 PyTypeObject *type;
5592 PyObject *obj;
5593 PyTypeObject *obj_type;
5594 } superobject;
5596 static PyMemberDef super_members[] = {
5597 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5598 "the class invoking super()"},
5599 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5600 "the instance invoking super(); may be None"},
5601 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5602 "the type of the instance invoking super(); may be None"},
5606 static void
5607 super_dealloc(PyObject *self)
5609 superobject *su = (superobject *)self;
5611 _PyObject_GC_UNTRACK(self);
5612 Py_XDECREF(su->obj);
5613 Py_XDECREF(su->type);
5614 Py_XDECREF(su->obj_type);
5615 self->ob_type->tp_free(self);
5618 static PyObject *
5619 super_repr(PyObject *self)
5621 superobject *su = (superobject *)self;
5623 if (su->obj_type)
5624 return PyString_FromFormat(
5625 "<super: <class '%s'>, <%s object>>",
5626 su->type ? su->type->tp_name : "NULL",
5627 su->obj_type->tp_name);
5628 else
5629 return PyString_FromFormat(
5630 "<super: <class '%s'>, NULL>",
5631 su->type ? su->type->tp_name : "NULL");
5634 static PyObject *
5635 super_getattro(PyObject *self, PyObject *name)
5637 superobject *su = (superobject *)self;
5638 int skip = su->obj_type == NULL;
5640 if (!skip) {
5641 /* We want __class__ to return the class of the super object
5642 (i.e. super, or a subclass), not the class of su->obj. */
5643 skip = (PyString_Check(name) &&
5644 PyString_GET_SIZE(name) == 9 &&
5645 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5648 if (!skip) {
5649 PyObject *mro, *res, *tmp, *dict;
5650 PyTypeObject *starttype;
5651 descrgetfunc f;
5652 Py_ssize_t i, n;
5654 starttype = su->obj_type;
5655 mro = starttype->tp_mro;
5657 if (mro == NULL)
5658 n = 0;
5659 else {
5660 assert(PyTuple_Check(mro));
5661 n = PyTuple_GET_SIZE(mro);
5663 for (i = 0; i < n; i++) {
5664 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
5665 break;
5667 i++;
5668 res = NULL;
5669 for (; i < n; i++) {
5670 tmp = PyTuple_GET_ITEM(mro, i);
5671 if (PyType_Check(tmp))
5672 dict = ((PyTypeObject *)tmp)->tp_dict;
5673 else if (PyClass_Check(tmp))
5674 dict = ((PyClassObject *)tmp)->cl_dict;
5675 else
5676 continue;
5677 res = PyDict_GetItem(dict, name);
5678 if (res != NULL) {
5679 Py_INCREF(res);
5680 f = res->ob_type->tp_descr_get;
5681 if (f != NULL) {
5682 tmp = f(res,
5683 /* Only pass 'obj' param if
5684 this is instance-mode super
5685 (See SF ID #743627)
5687 (su->obj == (PyObject *)
5688 su->obj_type
5689 ? (PyObject *)NULL
5690 : su->obj),
5691 (PyObject *)starttype);
5692 Py_DECREF(res);
5693 res = tmp;
5695 return res;
5699 return PyObject_GenericGetAttr(self, name);
5702 static PyTypeObject *
5703 supercheck(PyTypeObject *type, PyObject *obj)
5705 /* Check that a super() call makes sense. Return a type object.
5707 obj can be a new-style class, or an instance of one:
5709 - If it is a class, it must be a subclass of 'type'. This case is
5710 used for class methods; the return value is obj.
5712 - If it is an instance, it must be an instance of 'type'. This is
5713 the normal case; the return value is obj.__class__.
5715 But... when obj is an instance, we want to allow for the case where
5716 obj->ob_type is not a subclass of type, but obj.__class__ is!
5717 This will allow using super() with a proxy for obj.
5720 /* Check for first bullet above (special case) */
5721 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5722 Py_INCREF(obj);
5723 return (PyTypeObject *)obj;
5726 /* Normal case */
5727 if (PyType_IsSubtype(obj->ob_type, type)) {
5728 Py_INCREF(obj->ob_type);
5729 return obj->ob_type;
5731 else {
5732 /* Try the slow way */
5733 static PyObject *class_str = NULL;
5734 PyObject *class_attr;
5736 if (class_str == NULL) {
5737 class_str = PyString_FromString("__class__");
5738 if (class_str == NULL)
5739 return NULL;
5742 class_attr = PyObject_GetAttr(obj, class_str);
5744 if (class_attr != NULL &&
5745 PyType_Check(class_attr) &&
5746 (PyTypeObject *)class_attr != obj->ob_type)
5748 int ok = PyType_IsSubtype(
5749 (PyTypeObject *)class_attr, type);
5750 if (ok)
5751 return (PyTypeObject *)class_attr;
5754 if (class_attr == NULL)
5755 PyErr_Clear();
5756 else
5757 Py_DECREF(class_attr);
5760 PyErr_SetString(PyExc_TypeError,
5761 "super(type, obj): "
5762 "obj must be an instance or subtype of type");
5763 return NULL;
5766 static PyObject *
5767 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5769 superobject *su = (superobject *)self;
5770 superobject *newobj;
5772 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5773 /* Not binding to an object, or already bound */
5774 Py_INCREF(self);
5775 return self;
5777 if (su->ob_type != &PySuper_Type)
5778 /* If su is an instance of a (strict) subclass of super,
5779 call its type */
5780 return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
5781 su->type, obj, NULL);
5782 else {
5783 /* Inline the common case */
5784 PyTypeObject *obj_type = supercheck(su->type, obj);
5785 if (obj_type == NULL)
5786 return NULL;
5787 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5788 NULL, NULL);
5789 if (newobj == NULL)
5790 return NULL;
5791 Py_INCREF(su->type);
5792 Py_INCREF(obj);
5793 newobj->type = su->type;
5794 newobj->obj = obj;
5795 newobj->obj_type = obj_type;
5796 return (PyObject *)newobj;
5800 static int
5801 super_init(PyObject *self, PyObject *args, PyObject *kwds)
5803 superobject *su = (superobject *)self;
5804 PyTypeObject *type;
5805 PyObject *obj = NULL;
5806 PyTypeObject *obj_type = NULL;
5808 if (!_PyArg_NoKeywords("super", kwds))
5809 return -1;
5810 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5811 return -1;
5812 if (obj == Py_None)
5813 obj = NULL;
5814 if (obj != NULL) {
5815 obj_type = supercheck(type, obj);
5816 if (obj_type == NULL)
5817 return -1;
5818 Py_INCREF(obj);
5820 Py_INCREF(type);
5821 su->type = type;
5822 su->obj = obj;
5823 su->obj_type = obj_type;
5824 return 0;
5827 PyDoc_STRVAR(super_doc,
5828 "super(type) -> unbound super object\n"
5829 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5830 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5831 "Typical use to call a cooperative superclass method:\n"
5832 "class C(B):\n"
5833 " def meth(self, arg):\n"
5834 " super(C, self).meth(arg)");
5836 static int
5837 super_traverse(PyObject *self, visitproc visit, void *arg)
5839 superobject *su = (superobject *)self;
5841 Py_VISIT(su->obj);
5842 Py_VISIT(su->type);
5843 Py_VISIT(su->obj_type);
5845 return 0;
5848 PyTypeObject PySuper_Type = {
5849 PyObject_HEAD_INIT(&PyType_Type)
5850 0, /* ob_size */
5851 "super", /* tp_name */
5852 sizeof(superobject), /* tp_basicsize */
5853 0, /* tp_itemsize */
5854 /* methods */
5855 super_dealloc, /* tp_dealloc */
5856 0, /* tp_print */
5857 0, /* tp_getattr */
5858 0, /* tp_setattr */
5859 0, /* tp_compare */
5860 super_repr, /* tp_repr */
5861 0, /* tp_as_number */
5862 0, /* tp_as_sequence */
5863 0, /* tp_as_mapping */
5864 0, /* tp_hash */
5865 0, /* tp_call */
5866 0, /* tp_str */
5867 super_getattro, /* tp_getattro */
5868 0, /* tp_setattro */
5869 0, /* tp_as_buffer */
5870 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5871 Py_TPFLAGS_BASETYPE, /* tp_flags */
5872 super_doc, /* tp_doc */
5873 super_traverse, /* tp_traverse */
5874 0, /* tp_clear */
5875 0, /* tp_richcompare */
5876 0, /* tp_weaklistoffset */
5877 0, /* tp_iter */
5878 0, /* tp_iternext */
5879 0, /* tp_methods */
5880 super_members, /* tp_members */
5881 0, /* tp_getset */
5882 0, /* tp_base */
5883 0, /* tp_dict */
5884 super_descr_get, /* tp_descr_get */
5885 0, /* tp_descr_set */
5886 0, /* tp_dictoffset */
5887 super_init, /* tp_init */
5888 PyType_GenericAlloc, /* tp_alloc */
5889 PyType_GenericNew, /* tp_new */
5890 PyObject_GC_Del, /* tp_free */