Looks like someone renamed (or something) md5c.c to md5.c.
[python.git] / Objects / typeobject.c
blob39f76e9ca62e97d8f0e965f515a50ce601f2028f
1 /* Type object implementation */
3 #include "Python.h"
4 #include "structmember.h"
6 #include <ctype.h>
8 static PyMemberDef type_members[] = {
9 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
12 {"__weakrefoffset__", T_LONG,
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
21 static PyObject *
22 type_name(PyTypeObject *type, void *context)
24 const char *s;
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
29 Py_INCREF(et->ht_name);
30 return et->ht_name;
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
42 static int
43 type_set_name(PyTypeObject *type, PyObject *value, void *context)
45 PyHeapTypeObject* et;
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
63 if (strlen(PyString_AS_STRING(value))
64 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
70 et = (PyHeapTypeObject*)type;
72 Py_INCREF(value);
74 Py_DECREF(et->ht_name);
75 et->ht_name = value;
77 type->tp_name = PyString_AS_STRING(value);
79 return 0;
82 static PyObject *
83 type_module(PyTypeObject *type, void *context)
85 PyObject *mod;
86 char *s;
88 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
90 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
94 Py_XINCREF(mod);
95 return mod;
97 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
101 type->tp_name, (int)(s - type->tp_name));
102 return PyString_FromString("__builtin__");
106 static int
107 type_set_module(PyTypeObject *type, PyObject *value, void *context)
109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
123 static PyObject *
124 type_get_bases(PyTypeObject *type, void *context)
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
130 static PyTypeObject *best_base(PyObject *);
131 static int mro_internal(PyTypeObject *);
132 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133 static int add_subclass(PyTypeObject*, PyTypeObject*);
134 static void remove_subclass(PyTypeObject *, PyTypeObject *);
135 static void update_all_slots(PyTypeObject *);
137 typedef int (*update_callback)(PyTypeObject *, void *);
138 static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
143 static int
144 mro_subclasses(PyTypeObject *type, PyObject* temp)
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
148 Py_ssize_t i, n;
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
166 return -1;
168 else {
169 PyObject* tuple;
170 tuple = PyTuple_Pack(2, subclass, old_mro);
171 Py_DECREF(old_mro);
172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
176 Py_DECREF(tuple);
178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
181 return 0;
184 static int
185 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
187 Py_ssize_t i;
188 int r = 0;
189 PyObject *ob, *temp;
190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
217 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
233 new_base = best_base(value);
235 if (!new_base) {
236 return -1;
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
242 Py_INCREF(new_base);
243 Py_INCREF(value);
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
249 type->tp_bases = value;
250 type->tp_base = new_base;
252 if (mro_internal(type) < 0) {
253 goto bail;
256 temp = PyList_New(0);
257 if (!temp)
258 goto bail;
260 r = mro_subclasses(type, temp);
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
272 Py_DECREF(temp);
273 goto bail;
276 Py_DECREF(temp);
278 /* any base that was in __bases__ but now isn't, we
279 need to remove |type| from its tp_subclasses.
280 conversely, any class now in __bases__ that wasn't
281 needs to have |type| added to its subclasses. */
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
302 update_all_slots(type);
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
308 return r;
310 bail:
311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
321 return -1;
324 static PyObject *
325 type_dict(PyTypeObject *type, void *context)
327 if (type->tp_dict == NULL) {
328 Py_INCREF(Py_None);
329 return Py_None;
331 return PyDictProxy_New(type->tp_dict);
334 static PyObject *
335 type_get_doc(PyTypeObject *type, void *context)
337 PyObject *result;
338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
339 return PyString_FromString(type->tp_doc);
340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
345 else if (result->ob_type->tp_descr_get) {
346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
349 else {
350 Py_INCREF(result);
352 return result;
355 static PyGetSetDef type_getsets[] = {
356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
359 {"__dict__", (getter)type_dict, NULL, NULL},
360 {"__doc__", (getter)type_get_doc, NULL, NULL},
364 static int
365 type_compare(PyObject *v, PyObject *w)
367 /* This is called with type objects only. So we
368 can just compare the addresses. */
369 Py_uintptr_t vv = (Py_uintptr_t)v;
370 Py_uintptr_t ww = (Py_uintptr_t)w;
371 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
374 static PyObject *
375 type_repr(PyTypeObject *type)
377 PyObject *mod, *name, *rtn;
378 char *kind;
380 mod = type_module(type, NULL);
381 if (mod == NULL)
382 PyErr_Clear();
383 else if (!PyString_Check(mod)) {
384 Py_DECREF(mod);
385 mod = NULL;
387 name = type_name(type, NULL);
388 if (name == NULL)
389 return NULL;
391 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
392 kind = "class";
393 else
394 kind = "type";
396 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
397 rtn = PyString_FromFormat("<%s '%s.%s'>",
398 kind,
399 PyString_AS_STRING(mod),
400 PyString_AS_STRING(name));
402 else
403 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
405 Py_XDECREF(mod);
406 Py_DECREF(name);
407 return rtn;
410 static PyObject *
411 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
413 PyObject *obj;
415 if (type->tp_new == NULL) {
416 PyErr_Format(PyExc_TypeError,
417 "cannot create '%.100s' instances",
418 type->tp_name);
419 return NULL;
422 obj = type->tp_new(type, args, kwds);
423 if (obj != NULL) {
424 /* Ugly exception: when the call was type(something),
425 don't call tp_init on the result. */
426 if (type == &PyType_Type &&
427 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
428 (kwds == NULL ||
429 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
430 return obj;
431 /* If the returned object is not an instance of type,
432 it won't be initialized. */
433 if (!PyType_IsSubtype(obj->ob_type, type))
434 return obj;
435 type = obj->ob_type;
436 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
437 type->tp_init != NULL &&
438 type->tp_init(obj, args, kwds) < 0) {
439 Py_DECREF(obj);
440 obj = NULL;
443 return obj;
446 PyObject *
447 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
449 PyObject *obj;
450 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
451 /* note that we need to add one, for the sentinel */
453 if (PyType_IS_GC(type))
454 obj = _PyObject_GC_Malloc(size);
455 else
456 obj = PyObject_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 int err = visit(*dictptr, arg);
530 if (err)
531 return err;
535 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
536 /* For a heaptype, the instances count as references
537 to the type. Traverse the type so the collector
538 can find cycles involving this link. */
539 int err = visit((PyObject *)type, arg);
540 if (err)
541 return err;
544 if (basetraverse)
545 return basetraverse(self, visit, arg);
546 return 0;
549 static void
550 clear_slots(PyTypeObject *type, PyObject *self)
552 Py_ssize_t i, n;
553 PyMemberDef *mp;
555 n = type->ob_size;
556 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
557 for (i = 0; i < n; i++, mp++) {
558 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
559 char *addr = (char *)self + mp->offset;
560 PyObject *obj = *(PyObject **)addr;
561 if (obj != NULL) {
562 Py_DECREF(obj);
563 *(PyObject **)addr = NULL;
569 static int
570 subtype_clear(PyObject *self)
572 PyTypeObject *type, *base;
573 inquiry baseclear;
575 /* Find the nearest base with a different tp_clear
576 and clear slots while we're at it */
577 type = self->ob_type;
578 base = type;
579 while ((baseclear = base->tp_clear) == subtype_clear) {
580 if (base->ob_size)
581 clear_slots(base, self);
582 base = base->tp_base;
583 assert(base);
586 /* There's no need to clear the instance dict (if any);
587 the collector will call its tp_clear handler. */
589 if (baseclear)
590 return baseclear(self);
591 return 0;
594 static void
595 subtype_dealloc(PyObject *self)
597 PyTypeObject *type, *base;
598 destructor basedealloc;
600 /* Extract the type; we expect it to be a heap type */
601 type = self->ob_type;
602 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
604 /* Test whether the type has GC exactly once */
606 if (!PyType_IS_GC(type)) {
607 /* It's really rare to find a dynamic type that doesn't have
608 GC; it can only happen when deriving from 'object' and not
609 adding any slots or instance variables. This allows
610 certain simplifications: there's no need to call
611 clear_slots(), or DECREF the dict, or clear weakrefs. */
613 /* Maybe call finalizer; exit early if resurrected */
614 if (type->tp_del) {
615 type->tp_del(self);
616 if (self->ob_refcnt > 0)
617 return;
620 /* Find the nearest base with a different tp_dealloc */
621 base = type;
622 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
623 assert(base->ob_size == 0);
624 base = base->tp_base;
625 assert(base);
628 /* Call the base tp_dealloc() */
629 assert(basedealloc);
630 basedealloc(self);
632 /* Can't reference self beyond this point */
633 Py_DECREF(type);
635 /* Done */
636 return;
639 /* We get here only if the type has GC */
641 /* UnTrack and re-Track around the trashcan macro, alas */
642 /* See explanation at end of function for full disclosure */
643 PyObject_GC_UnTrack(self);
644 ++_PyTrash_delete_nesting;
645 Py_TRASHCAN_SAFE_BEGIN(self);
646 --_PyTrash_delete_nesting;
647 /* DO NOT restore GC tracking at this point. weakref callbacks
648 * (if any, and whether directly here or indirectly in something we
649 * call) may trigger GC, and if self is tracked at that point, it
650 * will look like trash to GC and GC will try to delete self again.
653 /* Find the nearest base with a different tp_dealloc */
654 base = type;
655 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
656 base = base->tp_base;
657 assert(base);
660 /* If we added a weaklist, we clear it. Do this *before* calling
661 the finalizer (__del__), clearing slots, or clearing the instance
662 dict. */
664 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
665 PyObject_ClearWeakRefs(self);
667 /* Maybe call finalizer; exit early if resurrected */
668 if (type->tp_del) {
669 _PyObject_GC_TRACK(self);
670 type->tp_del(self);
671 if (self->ob_refcnt > 0)
672 goto endlabel; /* resurrected */
673 else
674 _PyObject_GC_UNTRACK(self);
677 /* Clear slots up to the nearest base with a different tp_dealloc */
678 base = type;
679 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
680 if (base->ob_size)
681 clear_slots(base, self);
682 base = base->tp_base;
683 assert(base);
686 /* If we added a dict, DECREF it */
687 if (type->tp_dictoffset && !base->tp_dictoffset) {
688 PyObject **dictptr = _PyObject_GetDictPtr(self);
689 if (dictptr != NULL) {
690 PyObject *dict = *dictptr;
691 if (dict != NULL) {
692 Py_DECREF(dict);
693 *dictptr = NULL;
698 /* Call the base tp_dealloc(); first retrack self if
699 * basedealloc knows about gc.
701 if (PyType_IS_GC(base))
702 _PyObject_GC_TRACK(self);
703 assert(basedealloc);
704 basedealloc(self);
706 /* Can't reference self beyond this point */
707 Py_DECREF(type);
709 endlabel:
710 ++_PyTrash_delete_nesting;
711 Py_TRASHCAN_SAFE_END(self);
712 --_PyTrash_delete_nesting;
714 /* Explanation of the weirdness around the trashcan macros:
716 Q. What do the trashcan macros do?
718 A. Read the comment titled "Trashcan mechanism" in object.h.
719 For one, this explains why there must be a call to GC-untrack
720 before the trashcan begin macro. Without understanding the
721 trashcan code, the answers to the following questions don't make
722 sense.
724 Q. Why do we GC-untrack before the trashcan and then immediately
725 GC-track again afterward?
727 A. In the case that the base class is GC-aware, the base class
728 probably GC-untracks the object. If it does that using the
729 UNTRACK macro, this will crash when the object is already
730 untracked. Because we don't know what the base class does, the
731 only safe thing is to make sure the object is tracked when we
732 call the base class dealloc. But... The trashcan begin macro
733 requires that the object is *untracked* before it is called. So
734 the dance becomes:
736 GC untrack
737 trashcan begin
738 GC track
740 Q. Why did the last question say "immediately GC-track again"?
741 It's nowhere near immediately.
743 A. Because the code *used* to re-track immediately. Bad Idea.
744 self has a refcount of 0, and if gc ever gets its hands on it
745 (which can happen if any weakref callback gets invoked), it
746 looks like trash to gc too, and gc also tries to delete self
747 then. But we're already deleting self. Double dealloction is
748 a subtle disaster.
750 Q. Why the bizarre (net-zero) manipulation of
751 _PyTrash_delete_nesting around the trashcan macros?
753 A. Some base classes (e.g. list) also use the trashcan mechanism.
754 The following scenario used to be possible:
756 - suppose the trashcan level is one below the trashcan limit
758 - subtype_dealloc() is called
760 - the trashcan limit is not yet reached, so the trashcan level
761 is incremented and the code between trashcan begin and end is
762 executed
764 - this destroys much of the object's contents, including its
765 slots and __dict__
767 - basedealloc() is called; this is really list_dealloc(), or
768 some other type which also uses the trashcan macros
770 - the trashcan limit is now reached, so the object is put on the
771 trashcan's to-be-deleted-later list
773 - basedealloc() returns
775 - subtype_dealloc() decrefs the object's type
777 - subtype_dealloc() returns
779 - later, the trashcan code starts deleting the objects from its
780 to-be-deleted-later list
782 - subtype_dealloc() is called *AGAIN* for the same object
784 - at the very least (if the destroyed slots and __dict__ don't
785 cause problems) the object's type gets decref'ed a second
786 time, which is *BAD*!!!
788 The remedy is to make sure that if the code between trashcan
789 begin and end in subtype_dealloc() is called, the code between
790 trashcan begin and end in basedealloc() will also be called.
791 This is done by decrementing the level after passing into the
792 trashcan block, and incrementing it just before leaving the
793 block.
795 But now it's possible that a chain of objects consisting solely
796 of objects whose deallocator is subtype_dealloc() will defeat
797 the trashcan mechanism completely: the decremented level means
798 that the effective level never reaches the limit. Therefore, we
799 *increment* the level *before* entering the trashcan block, and
800 matchingly decrement it after leaving. This means the trashcan
801 code will trigger a little early, but that's no big deal.
803 Q. Are there any live examples of code in need of all this
804 complexity?
806 A. Yes. See SF bug 668433 for code that crashed (when Python was
807 compiled in debug mode) before the trashcan level manipulations
808 were added. For more discussion, see SF patches 581742, 575073
809 and bug 574207.
813 static PyTypeObject *solid_base(PyTypeObject *type);
815 /* type test with subclassing support */
818 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
820 PyObject *mro;
822 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
823 return b == a || b == &PyBaseObject_Type;
825 mro = a->tp_mro;
826 if (mro != NULL) {
827 /* Deal with multiple inheritance without recursion
828 by walking the MRO tuple */
829 Py_ssize_t i, n;
830 assert(PyTuple_Check(mro));
831 n = PyTuple_GET_SIZE(mro);
832 for (i = 0; i < n; i++) {
833 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
834 return 1;
836 return 0;
838 else {
839 /* a is not completely initilized yet; follow tp_base */
840 do {
841 if (a == b)
842 return 1;
843 a = a->tp_base;
844 } while (a != NULL);
845 return b == &PyBaseObject_Type;
849 /* Internal routines to do a method lookup in the type
850 without looking in the instance dictionary
851 (so we can't use PyObject_GetAttr) but still binding
852 it to the instance. The arguments are the object,
853 the method name as a C string, and the address of a
854 static variable used to cache the interned Python string.
856 Two variants:
858 - lookup_maybe() returns NULL without raising an exception
859 when the _PyType_Lookup() call fails;
861 - lookup_method() always raises an exception upon errors.
864 static PyObject *
865 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
867 PyObject *res;
869 if (*attrobj == NULL) {
870 *attrobj = PyString_InternFromString(attrstr);
871 if (*attrobj == NULL)
872 return NULL;
874 res = _PyType_Lookup(self->ob_type, *attrobj);
875 if (res != NULL) {
876 descrgetfunc f;
877 if ((f = res->ob_type->tp_descr_get) == NULL)
878 Py_INCREF(res);
879 else
880 res = f(res, self, (PyObject *)(self->ob_type));
882 return res;
885 static PyObject *
886 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
888 PyObject *res = lookup_maybe(self, attrstr, attrobj);
889 if (res == NULL && !PyErr_Occurred())
890 PyErr_SetObject(PyExc_AttributeError, *attrobj);
891 return res;
894 /* A variation of PyObject_CallMethod that uses lookup_method()
895 instead of PyObject_GetAttrString(). This uses the same convention
896 as lookup_method to cache the interned name string object. */
898 static PyObject *
899 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
901 va_list va;
902 PyObject *args, *func = 0, *retval;
903 va_start(va, format);
905 func = lookup_maybe(o, name, nameobj);
906 if (func == NULL) {
907 va_end(va);
908 if (!PyErr_Occurred())
909 PyErr_SetObject(PyExc_AttributeError, *nameobj);
910 return NULL;
913 if (format && *format)
914 args = Py_VaBuildValue(format, va);
915 else
916 args = PyTuple_New(0);
918 va_end(va);
920 if (args == NULL)
921 return NULL;
923 assert(PyTuple_Check(args));
924 retval = PyObject_Call(func, args, NULL);
926 Py_DECREF(args);
927 Py_DECREF(func);
929 return retval;
932 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
934 static PyObject *
935 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
937 va_list va;
938 PyObject *args, *func = 0, *retval;
939 va_start(va, format);
941 func = lookup_maybe(o, name, nameobj);
942 if (func == NULL) {
943 va_end(va);
944 if (!PyErr_Occurred()) {
945 Py_INCREF(Py_NotImplemented);
946 return Py_NotImplemented;
948 return NULL;
951 if (format && *format)
952 args = Py_VaBuildValue(format, va);
953 else
954 args = PyTuple_New(0);
956 va_end(va);
958 if (args == NULL)
959 return NULL;
961 assert(PyTuple_Check(args));
962 retval = PyObject_Call(func, args, NULL);
964 Py_DECREF(args);
965 Py_DECREF(func);
967 return retval;
970 static int
971 fill_classic_mro(PyObject *mro, PyObject *cls)
973 PyObject *bases, *base;
974 Py_ssize_t i, n;
976 assert(PyList_Check(mro));
977 assert(PyClass_Check(cls));
978 i = PySequence_Contains(mro, cls);
979 if (i < 0)
980 return -1;
981 if (!i) {
982 if (PyList_Append(mro, cls) < 0)
983 return -1;
985 bases = ((PyClassObject *)cls)->cl_bases;
986 assert(bases && PyTuple_Check(bases));
987 n = PyTuple_GET_SIZE(bases);
988 for (i = 0; i < n; i++) {
989 base = PyTuple_GET_ITEM(bases, i);
990 if (fill_classic_mro(mro, base) < 0)
991 return -1;
993 return 0;
996 static PyObject *
997 classic_mro(PyObject *cls)
999 PyObject *mro;
1001 assert(PyClass_Check(cls));
1002 mro = PyList_New(0);
1003 if (mro != NULL) {
1004 if (fill_classic_mro(mro, cls) == 0)
1005 return mro;
1006 Py_DECREF(mro);
1008 return NULL;
1012 Method resolution order algorithm C3 described in
1013 "A Monotonic Superclass Linearization for Dylan",
1014 by Kim Barrett, Bob Cassel, Paul Haahr,
1015 David A. Moon, Keith Playford, and P. Tucker Withington.
1016 (OOPSLA 1996)
1018 Some notes about the rules implied by C3:
1020 No duplicate bases.
1021 It isn't legal to repeat a class in a list of base classes.
1023 The next three properties are the 3 constraints in "C3".
1025 Local precendece order.
1026 If A precedes B in C's MRO, then A will precede B in the MRO of all
1027 subclasses of C.
1029 Monotonicity.
1030 The MRO of a class must be an extension without reordering of the
1031 MRO of each of its superclasses.
1033 Extended Precedence Graph (EPG).
1034 Linearization is consistent if there is a path in the EPG from
1035 each class to all its successors in the linearization. See
1036 the paper for definition of EPG.
1039 static int
1040 tail_contains(PyObject *list, int whence, PyObject *o) {
1041 Py_ssize_t j, size;
1042 size = PyList_GET_SIZE(list);
1044 for (j = whence+1; j < size; j++) {
1045 if (PyList_GET_ITEM(list, j) == o)
1046 return 1;
1048 return 0;
1051 static PyObject *
1052 class_name(PyObject *cls)
1054 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1055 if (name == NULL) {
1056 PyErr_Clear();
1057 Py_XDECREF(name);
1058 name = PyObject_Repr(cls);
1060 if (name == NULL)
1061 return NULL;
1062 if (!PyString_Check(name)) {
1063 Py_DECREF(name);
1064 return NULL;
1066 return name;
1069 static int
1070 check_duplicates(PyObject *list)
1072 Py_ssize_t i, j, n;
1073 /* Let's use a quadratic time algorithm,
1074 assuming that the bases lists is short.
1076 n = PyList_GET_SIZE(list);
1077 for (i = 0; i < n; i++) {
1078 PyObject *o = PyList_GET_ITEM(list, i);
1079 for (j = i + 1; j < n; j++) {
1080 if (PyList_GET_ITEM(list, j) == o) {
1081 o = class_name(o);
1082 PyErr_Format(PyExc_TypeError,
1083 "duplicate base class %s",
1084 o ? PyString_AS_STRING(o) : "?");
1085 Py_XDECREF(o);
1086 return -1;
1090 return 0;
1093 /* Raise a TypeError for an MRO order disagreement.
1095 It's hard to produce a good error message. In the absence of better
1096 insight into error reporting, report the classes that were candidates
1097 to be put next into the MRO. There is some conflict between the
1098 order in which they should be put in the MRO, but it's hard to
1099 diagnose what constraint can't be satisfied.
1102 static void
1103 set_mro_error(PyObject *to_merge, int *remain)
1105 Py_ssize_t i, n, off, to_merge_size;
1106 char buf[1000];
1107 PyObject *k, *v;
1108 PyObject *set = PyDict_New();
1109 if (!set) return;
1111 to_merge_size = PyList_GET_SIZE(to_merge);
1112 for (i = 0; i < to_merge_size; i++) {
1113 PyObject *L = PyList_GET_ITEM(to_merge, i);
1114 if (remain[i] < PyList_GET_SIZE(L)) {
1115 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1116 if (PyDict_SetItem(set, c, Py_None) < 0) {
1117 Py_DECREF(set);
1118 return;
1122 n = PyDict_Size(set);
1124 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1125 consistent method resolution\norder (MRO) for bases");
1126 i = 0;
1127 while (PyDict_Next(set, &i, &k, &v) && off < sizeof(buf)) {
1128 PyObject *name = class_name(k);
1129 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1130 name ? PyString_AS_STRING(name) : "?");
1131 Py_XDECREF(name);
1132 if (--n && off+1 < sizeof(buf)) {
1133 buf[off++] = ',';
1134 buf[off] = '\0';
1137 PyErr_SetString(PyExc_TypeError, buf);
1138 Py_DECREF(set);
1141 static int
1142 pmerge(PyObject *acc, PyObject* to_merge) {
1143 Py_ssize_t i, j, to_merge_size, empty_cnt;
1144 int *remain;
1145 int ok;
1147 to_merge_size = PyList_GET_SIZE(to_merge);
1149 /* remain stores an index into each sublist of to_merge.
1150 remain[i] is the index of the next base in to_merge[i]
1151 that is not included in acc.
1153 remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1154 if (remain == NULL)
1155 return -1;
1156 for (i = 0; i < to_merge_size; i++)
1157 remain[i] = 0;
1159 again:
1160 empty_cnt = 0;
1161 for (i = 0; i < to_merge_size; i++) {
1162 PyObject *candidate;
1164 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1166 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1167 empty_cnt++;
1168 continue;
1171 /* Choose next candidate for MRO.
1173 The input sequences alone can determine the choice.
1174 If not, choose the class which appears in the MRO
1175 of the earliest direct superclass of the new class.
1178 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1179 for (j = 0; j < to_merge_size; j++) {
1180 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1181 if (tail_contains(j_lst, remain[j], candidate)) {
1182 goto skip; /* continue outer loop */
1185 ok = PyList_Append(acc, candidate);
1186 if (ok < 0) {
1187 PyMem_Free(remain);
1188 return -1;
1190 for (j = 0; j < to_merge_size; j++) {
1191 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1192 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1193 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1194 remain[j]++;
1197 goto again;
1198 skip: ;
1201 if (empty_cnt == to_merge_size) {
1202 PyMem_FREE(remain);
1203 return 0;
1205 set_mro_error(to_merge, remain);
1206 PyMem_FREE(remain);
1207 return -1;
1210 static PyObject *
1211 mro_implementation(PyTypeObject *type)
1213 Py_ssize_t i, n;
1214 int ok;
1215 PyObject *bases, *result;
1216 PyObject *to_merge, *bases_aslist;
1218 if(type->tp_dict == NULL) {
1219 if(PyType_Ready(type) < 0)
1220 return NULL;
1223 /* Find a superclass linearization that honors the constraints
1224 of the explicit lists of bases and the constraints implied by
1225 each base class.
1227 to_merge is a list of lists, where each list is a superclass
1228 linearization implied by a base class. The last element of
1229 to_merge is the declared list of bases.
1232 bases = type->tp_bases;
1233 n = PyTuple_GET_SIZE(bases);
1235 to_merge = PyList_New(n+1);
1236 if (to_merge == NULL)
1237 return NULL;
1239 for (i = 0; i < n; i++) {
1240 PyObject *base = PyTuple_GET_ITEM(bases, i);
1241 PyObject *parentMRO;
1242 if (PyType_Check(base))
1243 parentMRO = PySequence_List(
1244 ((PyTypeObject*)base)->tp_mro);
1245 else
1246 parentMRO = classic_mro(base);
1247 if (parentMRO == NULL) {
1248 Py_DECREF(to_merge);
1249 return NULL;
1252 PyList_SET_ITEM(to_merge, i, parentMRO);
1255 bases_aslist = PySequence_List(bases);
1256 if (bases_aslist == NULL) {
1257 Py_DECREF(to_merge);
1258 return NULL;
1260 /* This is just a basic sanity check. */
1261 if (check_duplicates(bases_aslist) < 0) {
1262 Py_DECREF(to_merge);
1263 Py_DECREF(bases_aslist);
1264 return NULL;
1266 PyList_SET_ITEM(to_merge, n, bases_aslist);
1268 result = Py_BuildValue("[O]", (PyObject *)type);
1269 if (result == NULL) {
1270 Py_DECREF(to_merge);
1271 return NULL;
1274 ok = pmerge(result, to_merge);
1275 Py_DECREF(to_merge);
1276 if (ok < 0) {
1277 Py_DECREF(result);
1278 return NULL;
1281 return result;
1284 static PyObject *
1285 mro_external(PyObject *self)
1287 PyTypeObject *type = (PyTypeObject *)self;
1289 return mro_implementation(type);
1292 static int
1293 mro_internal(PyTypeObject *type)
1295 PyObject *mro, *result, *tuple;
1296 int checkit = 0;
1298 if (type->ob_type == &PyType_Type) {
1299 result = mro_implementation(type);
1301 else {
1302 static PyObject *mro_str;
1303 checkit = 1;
1304 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1305 if (mro == NULL)
1306 return -1;
1307 result = PyObject_CallObject(mro, NULL);
1308 Py_DECREF(mro);
1310 if (result == NULL)
1311 return -1;
1312 tuple = PySequence_Tuple(result);
1313 Py_DECREF(result);
1314 if (tuple == NULL)
1315 return -1;
1316 if (checkit) {
1317 Py_ssize_t i, len;
1318 PyObject *cls;
1319 PyTypeObject *solid;
1321 solid = solid_base(type);
1323 len = PyTuple_GET_SIZE(tuple);
1325 for (i = 0; i < len; i++) {
1326 PyTypeObject *t;
1327 cls = PyTuple_GET_ITEM(tuple, i);
1328 if (PyClass_Check(cls))
1329 continue;
1330 else if (!PyType_Check(cls)) {
1331 PyErr_Format(PyExc_TypeError,
1332 "mro() returned a non-class ('%.500s')",
1333 cls->ob_type->tp_name);
1334 Py_DECREF(tuple);
1335 return -1;
1337 t = (PyTypeObject*)cls;
1338 if (!PyType_IsSubtype(solid, solid_base(t))) {
1339 PyErr_Format(PyExc_TypeError,
1340 "mro() returned base with unsuitable layout ('%.500s')",
1341 t->tp_name);
1342 Py_DECREF(tuple);
1343 return -1;
1347 type->tp_mro = tuple;
1348 return 0;
1352 /* Calculate the best base amongst multiple base classes.
1353 This is the first one that's on the path to the "solid base". */
1355 static PyTypeObject *
1356 best_base(PyObject *bases)
1358 Py_ssize_t i, n;
1359 PyTypeObject *base, *winner, *candidate, *base_i;
1360 PyObject *base_proto;
1362 assert(PyTuple_Check(bases));
1363 n = PyTuple_GET_SIZE(bases);
1364 assert(n > 0);
1365 base = NULL;
1366 winner = NULL;
1367 for (i = 0; i < n; i++) {
1368 base_proto = PyTuple_GET_ITEM(bases, i);
1369 if (PyClass_Check(base_proto))
1370 continue;
1371 if (!PyType_Check(base_proto)) {
1372 PyErr_SetString(
1373 PyExc_TypeError,
1374 "bases must be types");
1375 return NULL;
1377 base_i = (PyTypeObject *)base_proto;
1378 if (base_i->tp_dict == NULL) {
1379 if (PyType_Ready(base_i) < 0)
1380 return NULL;
1382 candidate = solid_base(base_i);
1383 if (winner == NULL) {
1384 winner = candidate;
1385 base = base_i;
1387 else if (PyType_IsSubtype(winner, candidate))
1389 else if (PyType_IsSubtype(candidate, winner)) {
1390 winner = candidate;
1391 base = base_i;
1393 else {
1394 PyErr_SetString(
1395 PyExc_TypeError,
1396 "multiple bases have "
1397 "instance lay-out conflict");
1398 return NULL;
1401 if (base == NULL)
1402 PyErr_SetString(PyExc_TypeError,
1403 "a new-style class can't have only classic bases");
1404 return base;
1407 static int
1408 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1410 size_t t_size = type->tp_basicsize;
1411 size_t b_size = base->tp_basicsize;
1413 assert(t_size >= b_size); /* Else type smaller than base! */
1414 if (type->tp_itemsize || base->tp_itemsize) {
1415 /* If itemsize is involved, stricter rules */
1416 return t_size != b_size ||
1417 type->tp_itemsize != base->tp_itemsize;
1419 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1420 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1421 t_size -= sizeof(PyObject *);
1422 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1423 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1424 t_size -= sizeof(PyObject *);
1426 return t_size != b_size;
1429 static PyTypeObject *
1430 solid_base(PyTypeObject *type)
1432 PyTypeObject *base;
1434 if (type->tp_base)
1435 base = solid_base(type->tp_base);
1436 else
1437 base = &PyBaseObject_Type;
1438 if (extra_ivars(type, base))
1439 return type;
1440 else
1441 return base;
1444 static void object_dealloc(PyObject *);
1445 static int object_init(PyObject *, PyObject *, PyObject *);
1446 static int update_slot(PyTypeObject *, PyObject *);
1447 static void fixup_slot_dispatchers(PyTypeObject *);
1449 static PyObject *
1450 subtype_dict(PyObject *obj, void *context)
1452 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1453 PyObject *dict;
1455 if (dictptr == NULL) {
1456 PyErr_SetString(PyExc_AttributeError,
1457 "This object has no __dict__");
1458 return NULL;
1460 dict = *dictptr;
1461 if (dict == NULL)
1462 *dictptr = dict = PyDict_New();
1463 Py_XINCREF(dict);
1464 return dict;
1467 static int
1468 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1470 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1471 PyObject *dict;
1473 if (dictptr == NULL) {
1474 PyErr_SetString(PyExc_AttributeError,
1475 "This object has no __dict__");
1476 return -1;
1478 if (value != NULL && !PyDict_Check(value)) {
1479 PyErr_SetString(PyExc_TypeError,
1480 "__dict__ must be set to a dictionary");
1481 return -1;
1483 dict = *dictptr;
1484 Py_XINCREF(value);
1485 *dictptr = value;
1486 Py_XDECREF(dict);
1487 return 0;
1490 static PyObject *
1491 subtype_getweakref(PyObject *obj, void *context)
1493 PyObject **weaklistptr;
1494 PyObject *result;
1496 if (obj->ob_type->tp_weaklistoffset == 0) {
1497 PyErr_SetString(PyExc_AttributeError,
1498 "This object has no __weaklist__");
1499 return NULL;
1501 assert(obj->ob_type->tp_weaklistoffset > 0);
1502 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1503 (size_t)(obj->ob_type->tp_basicsize));
1504 weaklistptr = (PyObject **)
1505 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1506 if (*weaklistptr == NULL)
1507 result = Py_None;
1508 else
1509 result = *weaklistptr;
1510 Py_INCREF(result);
1511 return result;
1514 /* Three variants on the subtype_getsets list. */
1516 static PyGetSetDef subtype_getsets_full[] = {
1517 {"__dict__", subtype_dict, subtype_setdict,
1518 PyDoc_STR("dictionary for instance variables (if defined)")},
1519 {"__weakref__", subtype_getweakref, NULL,
1520 PyDoc_STR("list of weak references to the object (if defined)")},
1524 static PyGetSetDef subtype_getsets_dict_only[] = {
1525 {"__dict__", subtype_dict, subtype_setdict,
1526 PyDoc_STR("dictionary for instance variables (if defined)")},
1530 static PyGetSetDef subtype_getsets_weakref_only[] = {
1531 {"__weakref__", subtype_getweakref, NULL,
1532 PyDoc_STR("list of weak references to the object (if defined)")},
1536 static int
1537 valid_identifier(PyObject *s)
1539 unsigned char *p;
1540 Py_ssize_t i, n;
1542 if (!PyString_Check(s)) {
1543 PyErr_SetString(PyExc_TypeError,
1544 "__slots__ must be strings");
1545 return 0;
1547 p = (unsigned char *) PyString_AS_STRING(s);
1548 n = PyString_GET_SIZE(s);
1549 /* We must reject an empty name. As a hack, we bump the
1550 length to 1 so that the loop will balk on the trailing \0. */
1551 if (n == 0)
1552 n = 1;
1553 for (i = 0; i < n; i++, p++) {
1554 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1555 PyErr_SetString(PyExc_TypeError,
1556 "__slots__ must be identifiers");
1557 return 0;
1560 return 1;
1563 #ifdef Py_USING_UNICODE
1564 /* Replace Unicode objects in slots. */
1566 static PyObject *
1567 _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
1569 PyObject *tmp = slots;
1570 PyObject *o, *o1;
1571 Py_ssize_t i;
1572 ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1573 for (i = 0; i < nslots; i++) {
1574 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1575 if (tmp == slots) {
1576 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1577 if (tmp == NULL)
1578 return NULL;
1580 o1 = _PyUnicode_AsDefaultEncodedString
1581 (o, NULL);
1582 if (o1 == NULL) {
1583 Py_DECREF(tmp);
1584 return 0;
1586 Py_INCREF(o1);
1587 Py_DECREF(o);
1588 PyTuple_SET_ITEM(tmp, i, o1);
1591 return tmp;
1593 #endif
1595 static PyObject *
1596 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1598 PyObject *name, *bases, *dict;
1599 static char *kwlist[] = {"name", "bases", "dict", 0};
1600 PyObject *slots, *tmp, *newslots;
1601 PyTypeObject *type, *base, *tmptype, *winner;
1602 PyHeapTypeObject *et;
1603 PyMemberDef *mp;
1604 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
1605 int j, may_add_dict, may_add_weak;
1607 assert(args != NULL && PyTuple_Check(args));
1608 assert(kwds == NULL || PyDict_Check(kwds));
1610 /* Special case: type(x) should return x->ob_type */
1612 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1613 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1615 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1616 PyObject *x = PyTuple_GET_ITEM(args, 0);
1617 Py_INCREF(x->ob_type);
1618 return (PyObject *) x->ob_type;
1621 /* SF bug 475327 -- if that didn't trigger, we need 3
1622 arguments. but PyArg_ParseTupleAndKeywords below may give
1623 a msg saying type() needs exactly 3. */
1624 if (nargs + nkwds != 3) {
1625 PyErr_SetString(PyExc_TypeError,
1626 "type() takes 1 or 3 arguments");
1627 return NULL;
1631 /* Check arguments: (name, bases, dict) */
1632 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1633 &name,
1634 &PyTuple_Type, &bases,
1635 &PyDict_Type, &dict))
1636 return NULL;
1638 /* Determine the proper metatype to deal with this,
1639 and check for metatype conflicts while we're at it.
1640 Note that if some other metatype wins to contract,
1641 it's possible that its instances are not types. */
1642 nbases = PyTuple_GET_SIZE(bases);
1643 winner = metatype;
1644 for (i = 0; i < nbases; i++) {
1645 tmp = PyTuple_GET_ITEM(bases, i);
1646 tmptype = tmp->ob_type;
1647 if (tmptype == &PyClass_Type)
1648 continue; /* Special case classic classes */
1649 if (PyType_IsSubtype(winner, tmptype))
1650 continue;
1651 if (PyType_IsSubtype(tmptype, winner)) {
1652 winner = tmptype;
1653 continue;
1655 PyErr_SetString(PyExc_TypeError,
1656 "metaclass conflict: "
1657 "the metaclass of a derived class "
1658 "must be a (non-strict) subclass "
1659 "of the metaclasses of all its bases");
1660 return NULL;
1662 if (winner != metatype) {
1663 if (winner->tp_new != type_new) /* Pass it to the winner */
1664 return winner->tp_new(winner, args, kwds);
1665 metatype = winner;
1668 /* Adjust for empty tuple bases */
1669 if (nbases == 0) {
1670 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1671 if (bases == NULL)
1672 return NULL;
1673 nbases = 1;
1675 else
1676 Py_INCREF(bases);
1678 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1680 /* Calculate best base, and check that all bases are type objects */
1681 base = best_base(bases);
1682 if (base == NULL) {
1683 Py_DECREF(bases);
1684 return NULL;
1686 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1687 PyErr_Format(PyExc_TypeError,
1688 "type '%.100s' is not an acceptable base type",
1689 base->tp_name);
1690 Py_DECREF(bases);
1691 return NULL;
1694 /* Check for a __slots__ sequence variable in dict, and count it */
1695 slots = PyDict_GetItemString(dict, "__slots__");
1696 nslots = 0;
1697 add_dict = 0;
1698 add_weak = 0;
1699 may_add_dict = base->tp_dictoffset == 0;
1700 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1701 if (slots == NULL) {
1702 if (may_add_dict) {
1703 add_dict++;
1705 if (may_add_weak) {
1706 add_weak++;
1709 else {
1710 /* Have slots */
1712 /* Make it into a tuple */
1713 if (PyString_Check(slots))
1714 slots = PyTuple_Pack(1, slots);
1715 else
1716 slots = PySequence_Tuple(slots);
1717 if (slots == NULL) {
1718 Py_DECREF(bases);
1719 return NULL;
1721 assert(PyTuple_Check(slots));
1723 /* Are slots allowed? */
1724 nslots = PyTuple_GET_SIZE(slots);
1725 if (nslots > 0 && base->tp_itemsize != 0) {
1726 PyErr_Format(PyExc_TypeError,
1727 "nonempty __slots__ "
1728 "not supported for subtype of '%s'",
1729 base->tp_name);
1730 bad_slots:
1731 Py_DECREF(bases);
1732 Py_DECREF(slots);
1733 return NULL;
1736 #ifdef Py_USING_UNICODE
1737 tmp = _unicode_to_string(slots, nslots);
1738 if (tmp != slots) {
1739 Py_DECREF(slots);
1740 slots = tmp;
1742 if (!tmp)
1743 return NULL;
1744 #endif
1745 /* Check for valid slot names and two special cases */
1746 for (i = 0; i < nslots; i++) {
1747 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1748 char *s;
1749 if (!valid_identifier(tmp))
1750 goto bad_slots;
1751 assert(PyString_Check(tmp));
1752 s = PyString_AS_STRING(tmp);
1753 if (strcmp(s, "__dict__") == 0) {
1754 if (!may_add_dict || add_dict) {
1755 PyErr_SetString(PyExc_TypeError,
1756 "__dict__ slot disallowed: "
1757 "we already got one");
1758 goto bad_slots;
1760 add_dict++;
1762 if (strcmp(s, "__weakref__") == 0) {
1763 if (!may_add_weak || add_weak) {
1764 PyErr_SetString(PyExc_TypeError,
1765 "__weakref__ slot disallowed: "
1766 "either we already got one, "
1767 "or __itemsize__ != 0");
1768 goto bad_slots;
1770 add_weak++;
1774 /* Copy slots into yet another tuple, demangling names */
1775 newslots = PyTuple_New(nslots - add_dict - add_weak);
1776 if (newslots == NULL)
1777 goto bad_slots;
1778 for (i = j = 0; i < nslots; i++) {
1779 char *s;
1780 tmp = PyTuple_GET_ITEM(slots, i);
1781 s = PyString_AS_STRING(tmp);
1782 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1783 (add_weak && strcmp(s, "__weakref__") == 0))
1784 continue;
1785 tmp =_Py_Mangle(name, tmp);
1786 if (!tmp)
1787 goto bad_slots;
1788 PyTuple_SET_ITEM(newslots, j, tmp);
1789 j++;
1791 assert(j == nslots - add_dict - add_weak);
1792 nslots = j;
1793 Py_DECREF(slots);
1794 slots = newslots;
1796 /* Secondary bases may provide weakrefs or dict */
1797 if (nbases > 1 &&
1798 ((may_add_dict && !add_dict) ||
1799 (may_add_weak && !add_weak))) {
1800 for (i = 0; i < nbases; i++) {
1801 tmp = PyTuple_GET_ITEM(bases, i);
1802 if (tmp == (PyObject *)base)
1803 continue; /* Skip primary base */
1804 if (PyClass_Check(tmp)) {
1805 /* Classic base class provides both */
1806 if (may_add_dict && !add_dict)
1807 add_dict++;
1808 if (may_add_weak && !add_weak)
1809 add_weak++;
1810 break;
1812 assert(PyType_Check(tmp));
1813 tmptype = (PyTypeObject *)tmp;
1814 if (may_add_dict && !add_dict &&
1815 tmptype->tp_dictoffset != 0)
1816 add_dict++;
1817 if (may_add_weak && !add_weak &&
1818 tmptype->tp_weaklistoffset != 0)
1819 add_weak++;
1820 if (may_add_dict && !add_dict)
1821 continue;
1822 if (may_add_weak && !add_weak)
1823 continue;
1824 /* Nothing more to check */
1825 break;
1830 /* XXX From here until type is safely allocated,
1831 "return NULL" may leak slots! */
1833 /* Allocate the type object */
1834 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1835 if (type == NULL) {
1836 Py_XDECREF(slots);
1837 Py_DECREF(bases);
1838 return NULL;
1841 /* Keep name and slots alive in the extended type object */
1842 et = (PyHeapTypeObject *)type;
1843 Py_INCREF(name);
1844 et->ht_name = name;
1845 et->ht_slots = slots;
1847 /* Initialize tp_flags */
1848 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1849 Py_TPFLAGS_BASETYPE;
1850 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1851 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1853 /* It's a new-style number unless it specifically inherits any
1854 old-style numeric behavior */
1855 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1856 (base->tp_as_number == NULL))
1857 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1859 /* Initialize essential fields */
1860 type->tp_as_number = &et->as_number;
1861 type->tp_as_sequence = &et->as_sequence;
1862 type->tp_as_mapping = &et->as_mapping;
1863 type->tp_as_buffer = &et->as_buffer;
1864 type->tp_name = PyString_AS_STRING(name);
1866 /* Set tp_base and tp_bases */
1867 type->tp_bases = bases;
1868 Py_INCREF(base);
1869 type->tp_base = base;
1871 /* Initialize tp_dict from passed-in dict */
1872 type->tp_dict = dict = PyDict_Copy(dict);
1873 if (dict == NULL) {
1874 Py_DECREF(type);
1875 return NULL;
1878 /* Set __module__ in the dict */
1879 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1880 tmp = PyEval_GetGlobals();
1881 if (tmp != NULL) {
1882 tmp = PyDict_GetItemString(tmp, "__name__");
1883 if (tmp != NULL) {
1884 if (PyDict_SetItemString(dict, "__module__",
1885 tmp) < 0)
1886 return NULL;
1891 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1892 and is a string. The __doc__ accessor will first look for tp_doc;
1893 if that fails, it will still look into __dict__.
1896 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1897 if (doc != NULL && PyString_Check(doc)) {
1898 const size_t n = (size_t)PyString_GET_SIZE(doc);
1899 char *tp_doc = PyObject_MALLOC(n+1);
1900 if (tp_doc == NULL) {
1901 Py_DECREF(type);
1902 return NULL;
1904 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1905 type->tp_doc = tp_doc;
1909 /* Special-case __new__: if it's a plain function,
1910 make it a static function */
1911 tmp = PyDict_GetItemString(dict, "__new__");
1912 if (tmp != NULL && PyFunction_Check(tmp)) {
1913 tmp = PyStaticMethod_New(tmp);
1914 if (tmp == NULL) {
1915 Py_DECREF(type);
1916 return NULL;
1918 PyDict_SetItemString(dict, "__new__", tmp);
1919 Py_DECREF(tmp);
1922 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1923 mp = PyHeapType_GET_MEMBERS(et);
1924 slotoffset = base->tp_basicsize;
1925 if (slots != NULL) {
1926 for (i = 0; i < nslots; i++, mp++) {
1927 mp->name = PyString_AS_STRING(
1928 PyTuple_GET_ITEM(slots, i));
1929 mp->type = T_OBJECT_EX;
1930 mp->offset = slotoffset;
1931 if (base->tp_weaklistoffset == 0 &&
1932 strcmp(mp->name, "__weakref__") == 0) {
1933 add_weak++;
1934 mp->type = T_OBJECT;
1935 mp->flags = READONLY;
1936 type->tp_weaklistoffset = slotoffset;
1938 slotoffset += sizeof(PyObject *);
1941 if (add_dict) {
1942 if (base->tp_itemsize)
1943 type->tp_dictoffset = -(long)sizeof(PyObject *);
1944 else
1945 type->tp_dictoffset = slotoffset;
1946 slotoffset += sizeof(PyObject *);
1948 if (add_weak) {
1949 assert(!base->tp_itemsize);
1950 type->tp_weaklistoffset = slotoffset;
1951 slotoffset += sizeof(PyObject *);
1953 type->tp_basicsize = slotoffset;
1954 type->tp_itemsize = base->tp_itemsize;
1955 type->tp_members = PyHeapType_GET_MEMBERS(et);
1957 if (type->tp_weaklistoffset && type->tp_dictoffset)
1958 type->tp_getset = subtype_getsets_full;
1959 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1960 type->tp_getset = subtype_getsets_weakref_only;
1961 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1962 type->tp_getset = subtype_getsets_dict_only;
1963 else
1964 type->tp_getset = NULL;
1966 /* Special case some slots */
1967 if (type->tp_dictoffset != 0 || nslots > 0) {
1968 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1969 type->tp_getattro = PyObject_GenericGetAttr;
1970 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1971 type->tp_setattro = PyObject_GenericSetAttr;
1973 type->tp_dealloc = subtype_dealloc;
1975 /* Enable GC unless there are really no instance variables possible */
1976 if (!(type->tp_basicsize == sizeof(PyObject) &&
1977 type->tp_itemsize == 0))
1978 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1980 /* Always override allocation strategy to use regular heap */
1981 type->tp_alloc = PyType_GenericAlloc;
1982 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1983 type->tp_free = PyObject_GC_Del;
1984 type->tp_traverse = subtype_traverse;
1985 type->tp_clear = subtype_clear;
1987 else
1988 type->tp_free = PyObject_Del;
1990 /* Initialize the rest */
1991 if (PyType_Ready(type) < 0) {
1992 Py_DECREF(type);
1993 return NULL;
1996 /* Put the proper slots in place */
1997 fixup_slot_dispatchers(type);
1999 return (PyObject *)type;
2002 /* Internal API to look for a name through the MRO.
2003 This returns a borrowed reference, and doesn't set an exception! */
2004 PyObject *
2005 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2007 Py_ssize_t i, n;
2008 PyObject *mro, *res, *base, *dict;
2010 /* Look in tp_dict of types in MRO */
2011 mro = type->tp_mro;
2013 /* If mro is NULL, the type is either not yet initialized
2014 by PyType_Ready(), or already cleared by type_clear().
2015 Either way the safest thing to do is to return NULL. */
2016 if (mro == NULL)
2017 return NULL;
2019 assert(PyTuple_Check(mro));
2020 n = PyTuple_GET_SIZE(mro);
2021 for (i = 0; i < n; i++) {
2022 base = PyTuple_GET_ITEM(mro, i);
2023 if (PyClass_Check(base))
2024 dict = ((PyClassObject *)base)->cl_dict;
2025 else {
2026 assert(PyType_Check(base));
2027 dict = ((PyTypeObject *)base)->tp_dict;
2029 assert(dict && PyDict_Check(dict));
2030 res = PyDict_GetItem(dict, name);
2031 if (res != NULL)
2032 return res;
2034 return NULL;
2037 /* This is similar to PyObject_GenericGetAttr(),
2038 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2039 static PyObject *
2040 type_getattro(PyTypeObject *type, PyObject *name)
2042 PyTypeObject *metatype = type->ob_type;
2043 PyObject *meta_attribute, *attribute;
2044 descrgetfunc meta_get;
2046 /* Initialize this type (we'll assume the metatype is initialized) */
2047 if (type->tp_dict == NULL) {
2048 if (PyType_Ready(type) < 0)
2049 return NULL;
2052 /* No readable descriptor found yet */
2053 meta_get = NULL;
2055 /* Look for the attribute in the metatype */
2056 meta_attribute = _PyType_Lookup(metatype, name);
2058 if (meta_attribute != NULL) {
2059 meta_get = meta_attribute->ob_type->tp_descr_get;
2061 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2062 /* Data descriptors implement tp_descr_set to intercept
2063 * writes. Assume the attribute is not overridden in
2064 * type's tp_dict (and bases): call the descriptor now.
2066 return meta_get(meta_attribute, (PyObject *)type,
2067 (PyObject *)metatype);
2069 Py_INCREF(meta_attribute);
2072 /* No data descriptor found on metatype. Look in tp_dict of this
2073 * type and its bases */
2074 attribute = _PyType_Lookup(type, name);
2075 if (attribute != NULL) {
2076 /* Implement descriptor functionality, if any */
2077 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2079 Py_XDECREF(meta_attribute);
2081 if (local_get != NULL) {
2082 /* NULL 2nd argument indicates the descriptor was
2083 * found on the target object itself (or a base) */
2084 return local_get(attribute, (PyObject *)NULL,
2085 (PyObject *)type);
2088 Py_INCREF(attribute);
2089 return attribute;
2092 /* No attribute found in local __dict__ (or bases): use the
2093 * descriptor from the metatype, if any */
2094 if (meta_get != NULL) {
2095 PyObject *res;
2096 res = meta_get(meta_attribute, (PyObject *)type,
2097 (PyObject *)metatype);
2098 Py_DECREF(meta_attribute);
2099 return res;
2102 /* If an ordinary attribute was found on the metatype, return it now */
2103 if (meta_attribute != NULL) {
2104 return meta_attribute;
2107 /* Give up */
2108 PyErr_Format(PyExc_AttributeError,
2109 "type object '%.50s' has no attribute '%.400s'",
2110 type->tp_name, PyString_AS_STRING(name));
2111 return NULL;
2114 static int
2115 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2117 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2118 PyErr_Format(
2119 PyExc_TypeError,
2120 "can't set attributes of built-in/extension type '%s'",
2121 type->tp_name);
2122 return -1;
2124 /* XXX Example of how I expect this to be used...
2125 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2126 return -1;
2128 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2129 return -1;
2130 return update_slot(type, name);
2133 static void
2134 type_dealloc(PyTypeObject *type)
2136 PyHeapTypeObject *et;
2138 /* Assert this is a heap-allocated type object */
2139 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2140 _PyObject_GC_UNTRACK(type);
2141 PyObject_ClearWeakRefs((PyObject *)type);
2142 et = (PyHeapTypeObject *)type;
2143 Py_XDECREF(type->tp_base);
2144 Py_XDECREF(type->tp_dict);
2145 Py_XDECREF(type->tp_bases);
2146 Py_XDECREF(type->tp_mro);
2147 Py_XDECREF(type->tp_cache);
2148 Py_XDECREF(type->tp_subclasses);
2149 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2150 * of most other objects. It's okay to cast it to char *.
2152 PyObject_Free((char *)type->tp_doc);
2153 Py_XDECREF(et->ht_name);
2154 Py_XDECREF(et->ht_slots);
2155 type->ob_type->tp_free((PyObject *)type);
2158 static PyObject *
2159 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2161 PyObject *list, *raw, *ref;
2162 Py_ssize_t i, n;
2164 list = PyList_New(0);
2165 if (list == NULL)
2166 return NULL;
2167 raw = type->tp_subclasses;
2168 if (raw == NULL)
2169 return list;
2170 assert(PyList_Check(raw));
2171 n = PyList_GET_SIZE(raw);
2172 for (i = 0; i < n; i++) {
2173 ref = PyList_GET_ITEM(raw, i);
2174 assert(PyWeakref_CheckRef(ref));
2175 ref = PyWeakref_GET_OBJECT(ref);
2176 if (ref != Py_None) {
2177 if (PyList_Append(list, ref) < 0) {
2178 Py_DECREF(list);
2179 return NULL;
2183 return list;
2186 static PyMethodDef type_methods[] = {
2187 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2188 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2189 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2190 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2194 PyDoc_STRVAR(type_doc,
2195 "type(object) -> the object's type\n"
2196 "type(name, bases, dict) -> a new type");
2198 static int
2199 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2201 int err;
2203 /* Because of type_is_gc(), the collector only calls this
2204 for heaptypes. */
2205 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2207 #define VISIT(SLOT) \
2208 if (SLOT) { \
2209 err = visit((PyObject *)(SLOT), arg); \
2210 if (err) \
2211 return err; \
2214 VISIT(type->tp_dict);
2215 VISIT(type->tp_cache);
2216 VISIT(type->tp_mro);
2217 VISIT(type->tp_bases);
2218 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 #undef VISIT
2227 return 0;
2230 static int
2231 type_clear(PyTypeObject *type)
2233 PyObject *tmp;
2235 /* Because of type_is_gc(), the collector only calls this
2236 for heaptypes. */
2237 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2239 #define CLEAR(SLOT) \
2240 if (SLOT) { \
2241 tmp = (PyObject *)(SLOT); \
2242 SLOT = NULL; \
2243 Py_DECREF(tmp); \
2246 /* The only field we need to clear is tp_mro, which is part of a
2247 hard cycle (its first element is the class itself) that won't
2248 be broken otherwise (it's a tuple and tuples don't have a
2249 tp_clear handler). None of the other fields need to be
2250 cleared, and here's why:
2252 tp_dict:
2253 It is a dict, so the collector will call its tp_clear.
2255 tp_cache:
2256 Not used; if it were, it would be a dict.
2258 tp_bases, tp_base:
2259 If these are involved in a cycle, there must be at least
2260 one other, mutable object in the cycle, e.g. a base
2261 class's dict; the cycle will be broken that way.
2263 tp_subclasses:
2264 A list of weak references can't be part of a cycle; and
2265 lists have their own tp_clear.
2267 slots (in PyHeapTypeObject):
2268 A tuple of strings can't be part of a cycle.
2271 CLEAR(type->tp_mro);
2273 #undef CLEAR
2275 return 0;
2278 static int
2279 type_is_gc(PyTypeObject *type)
2281 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2284 PyTypeObject PyType_Type = {
2285 PyObject_HEAD_INIT(&PyType_Type)
2286 0, /* ob_size */
2287 "type", /* tp_name */
2288 sizeof(PyHeapTypeObject), /* tp_basicsize */
2289 sizeof(PyMemberDef), /* tp_itemsize */
2290 (destructor)type_dealloc, /* tp_dealloc */
2291 0, /* tp_print */
2292 0, /* tp_getattr */
2293 0, /* tp_setattr */
2294 type_compare, /* tp_compare */
2295 (reprfunc)type_repr, /* tp_repr */
2296 0, /* tp_as_number */
2297 0, /* tp_as_sequence */
2298 0, /* tp_as_mapping */
2299 (hashfunc)_Py_HashPointer, /* tp_hash */
2300 (ternaryfunc)type_call, /* tp_call */
2301 0, /* tp_str */
2302 (getattrofunc)type_getattro, /* tp_getattro */
2303 (setattrofunc)type_setattro, /* tp_setattro */
2304 0, /* tp_as_buffer */
2305 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2306 Py_TPFLAGS_BASETYPE, /* tp_flags */
2307 type_doc, /* tp_doc */
2308 (traverseproc)type_traverse, /* tp_traverse */
2309 (inquiry)type_clear, /* tp_clear */
2310 0, /* tp_richcompare */
2311 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2312 0, /* tp_iter */
2313 0, /* tp_iternext */
2314 type_methods, /* tp_methods */
2315 type_members, /* tp_members */
2316 type_getsets, /* tp_getset */
2317 0, /* tp_base */
2318 0, /* tp_dict */
2319 0, /* tp_descr_get */
2320 0, /* tp_descr_set */
2321 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2322 0, /* tp_init */
2323 0, /* tp_alloc */
2324 type_new, /* tp_new */
2325 PyObject_GC_Del, /* tp_free */
2326 (inquiry)type_is_gc, /* tp_is_gc */
2330 /* The base type of all types (eventually)... except itself. */
2332 static int
2333 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2335 return 0;
2338 /* If we don't have a tp_new for a new-style class, new will use this one.
2339 Therefore this should take no arguments/keywords. However, this new may
2340 also be inherited by objects that define a tp_init but no tp_new. These
2341 objects WILL pass argumets to tp_new, because it gets the same args as
2342 tp_init. So only allow arguments if we aren't using the default init, in
2343 which case we expect init to handle argument parsing. */
2344 static PyObject *
2345 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2347 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2348 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2349 PyErr_SetString(PyExc_TypeError,
2350 "default __new__ takes no parameters");
2351 return NULL;
2353 return type->tp_alloc(type, 0);
2356 static void
2357 object_dealloc(PyObject *self)
2359 self->ob_type->tp_free(self);
2362 static PyObject *
2363 object_repr(PyObject *self)
2365 PyTypeObject *type;
2366 PyObject *mod, *name, *rtn;
2368 type = self->ob_type;
2369 mod = type_module(type, NULL);
2370 if (mod == NULL)
2371 PyErr_Clear();
2372 else if (!PyString_Check(mod)) {
2373 Py_DECREF(mod);
2374 mod = NULL;
2376 name = type_name(type, NULL);
2377 if (name == NULL)
2378 return NULL;
2379 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2380 rtn = PyString_FromFormat("<%s.%s object at %p>",
2381 PyString_AS_STRING(mod),
2382 PyString_AS_STRING(name),
2383 self);
2384 else
2385 rtn = PyString_FromFormat("<%s object at %p>",
2386 type->tp_name, self);
2387 Py_XDECREF(mod);
2388 Py_DECREF(name);
2389 return rtn;
2392 static PyObject *
2393 object_str(PyObject *self)
2395 unaryfunc f;
2397 f = self->ob_type->tp_repr;
2398 if (f == NULL)
2399 f = object_repr;
2400 return f(self);
2403 static long
2404 object_hash(PyObject *self)
2406 return _Py_HashPointer(self);
2409 static PyObject *
2410 object_get_class(PyObject *self, void *closure)
2412 Py_INCREF(self->ob_type);
2413 return (PyObject *)(self->ob_type);
2416 static int
2417 equiv_structs(PyTypeObject *a, PyTypeObject *b)
2419 return a == b ||
2420 (a != NULL &&
2421 b != NULL &&
2422 a->tp_basicsize == b->tp_basicsize &&
2423 a->tp_itemsize == b->tp_itemsize &&
2424 a->tp_dictoffset == b->tp_dictoffset &&
2425 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2426 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2427 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2430 static int
2431 same_slots_added(PyTypeObject *a, PyTypeObject *b)
2433 PyTypeObject *base = a->tp_base;
2434 Py_ssize_t size;
2436 if (base != b->tp_base)
2437 return 0;
2438 if (equiv_structs(a, base) && equiv_structs(b, base))
2439 return 1;
2440 size = base->tp_basicsize;
2441 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2442 size += sizeof(PyObject *);
2443 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2444 size += sizeof(PyObject *);
2445 return size == a->tp_basicsize && size == b->tp_basicsize;
2448 static int
2449 compatible_for_assignment(PyTypeObject* old, PyTypeObject* new, char* attr)
2451 PyTypeObject *newbase, *oldbase;
2453 if (new->tp_dealloc != old->tp_dealloc ||
2454 new->tp_free != old->tp_free)
2456 PyErr_Format(PyExc_TypeError,
2457 "%s assignment: "
2458 "'%s' deallocator differs from '%s'",
2459 attr,
2460 new->tp_name,
2461 old->tp_name);
2462 return 0;
2464 newbase = new;
2465 oldbase = old;
2466 while (equiv_structs(newbase, newbase->tp_base))
2467 newbase = newbase->tp_base;
2468 while (equiv_structs(oldbase, oldbase->tp_base))
2469 oldbase = oldbase->tp_base;
2470 if (newbase != oldbase &&
2471 (newbase->tp_base != oldbase->tp_base ||
2472 !same_slots_added(newbase, oldbase))) {
2473 PyErr_Format(PyExc_TypeError,
2474 "%s assignment: "
2475 "'%s' object layout differs from '%s'",
2476 attr,
2477 new->tp_name,
2478 old->tp_name);
2479 return 0;
2482 return 1;
2485 static int
2486 object_set_class(PyObject *self, PyObject *value, void *closure)
2488 PyTypeObject *old = self->ob_type;
2489 PyTypeObject *new;
2491 if (value == NULL) {
2492 PyErr_SetString(PyExc_TypeError,
2493 "can't delete __class__ attribute");
2494 return -1;
2496 if (!PyType_Check(value)) {
2497 PyErr_Format(PyExc_TypeError,
2498 "__class__ must be set to new-style class, not '%s' object",
2499 value->ob_type->tp_name);
2500 return -1;
2502 new = (PyTypeObject *)value;
2503 if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2504 !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
2506 PyErr_Format(PyExc_TypeError,
2507 "__class__ assignment: only for heap types");
2508 return -1;
2510 if (compatible_for_assignment(new, old, "__class__")) {
2511 Py_INCREF(new);
2512 self->ob_type = new;
2513 Py_DECREF(old);
2514 return 0;
2516 else {
2517 return -1;
2521 static PyGetSetDef object_getsets[] = {
2522 {"__class__", object_get_class, object_set_class,
2523 PyDoc_STR("the object's class")},
2528 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2529 We fall back to helpers in copy_reg for:
2530 - pickle protocols < 2
2531 - calculating the list of slot names (done only once per class)
2532 - the __newobj__ function (which is used as a token but never called)
2535 static PyObject *
2536 import_copy_reg(void)
2538 static PyObject *copy_reg_str;
2540 if (!copy_reg_str) {
2541 copy_reg_str = PyString_InternFromString("copy_reg");
2542 if (copy_reg_str == NULL)
2543 return NULL;
2546 return PyImport_Import(copy_reg_str);
2549 static PyObject *
2550 slotnames(PyObject *cls)
2552 PyObject *clsdict;
2553 PyObject *copy_reg;
2554 PyObject *slotnames;
2556 if (!PyType_Check(cls)) {
2557 Py_INCREF(Py_None);
2558 return Py_None;
2561 clsdict = ((PyTypeObject *)cls)->tp_dict;
2562 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2563 if (slotnames != NULL && PyList_Check(slotnames)) {
2564 Py_INCREF(slotnames);
2565 return slotnames;
2568 copy_reg = import_copy_reg();
2569 if (copy_reg == NULL)
2570 return NULL;
2572 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2573 Py_DECREF(copy_reg);
2574 if (slotnames != NULL &&
2575 slotnames != Py_None &&
2576 !PyList_Check(slotnames))
2578 PyErr_SetString(PyExc_TypeError,
2579 "copy_reg._slotnames didn't return a list or None");
2580 Py_DECREF(slotnames);
2581 slotnames = NULL;
2584 return slotnames;
2587 static PyObject *
2588 reduce_2(PyObject *obj)
2590 PyObject *cls, *getnewargs;
2591 PyObject *args = NULL, *args2 = NULL;
2592 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2593 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2594 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2595 Py_ssize_t i, n;
2597 cls = PyObject_GetAttrString(obj, "__class__");
2598 if (cls == NULL)
2599 return NULL;
2601 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2602 if (getnewargs != NULL) {
2603 args = PyObject_CallObject(getnewargs, NULL);
2604 Py_DECREF(getnewargs);
2605 if (args != NULL && !PyTuple_Check(args)) {
2606 PyErr_SetString(PyExc_TypeError,
2607 "__getnewargs__ should return a tuple");
2608 goto end;
2611 else {
2612 PyErr_Clear();
2613 args = PyTuple_New(0);
2615 if (args == NULL)
2616 goto end;
2618 getstate = PyObject_GetAttrString(obj, "__getstate__");
2619 if (getstate != NULL) {
2620 state = PyObject_CallObject(getstate, NULL);
2621 Py_DECREF(getstate);
2622 if (state == NULL)
2623 goto end;
2625 else {
2626 PyErr_Clear();
2627 state = PyObject_GetAttrString(obj, "__dict__");
2628 if (state == NULL) {
2629 PyErr_Clear();
2630 state = Py_None;
2631 Py_INCREF(state);
2633 names = slotnames(cls);
2634 if (names == NULL)
2635 goto end;
2636 if (names != Py_None) {
2637 assert(PyList_Check(names));
2638 slots = PyDict_New();
2639 if (slots == NULL)
2640 goto end;
2641 n = 0;
2642 /* Can't pre-compute the list size; the list
2643 is stored on the class so accessible to other
2644 threads, which may be run by DECREF */
2645 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2646 PyObject *name, *value;
2647 name = PyList_GET_ITEM(names, i);
2648 value = PyObject_GetAttr(obj, name);
2649 if (value == NULL)
2650 PyErr_Clear();
2651 else {
2652 int err = PyDict_SetItem(slots, name,
2653 value);
2654 Py_DECREF(value);
2655 if (err)
2656 goto end;
2657 n++;
2660 if (n) {
2661 state = Py_BuildValue("(NO)", state, slots);
2662 if (state == NULL)
2663 goto end;
2668 if (!PyList_Check(obj)) {
2669 listitems = Py_None;
2670 Py_INCREF(listitems);
2672 else {
2673 listitems = PyObject_GetIter(obj);
2674 if (listitems == NULL)
2675 goto end;
2678 if (!PyDict_Check(obj)) {
2679 dictitems = Py_None;
2680 Py_INCREF(dictitems);
2682 else {
2683 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2684 if (dictitems == NULL)
2685 goto end;
2688 copy_reg = import_copy_reg();
2689 if (copy_reg == NULL)
2690 goto end;
2691 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2692 if (newobj == NULL)
2693 goto end;
2695 n = PyTuple_GET_SIZE(args);
2696 args2 = PyTuple_New(n+1);
2697 if (args2 == NULL)
2698 goto end;
2699 PyTuple_SET_ITEM(args2, 0, cls);
2700 cls = NULL;
2701 for (i = 0; i < n; i++) {
2702 PyObject *v = PyTuple_GET_ITEM(args, i);
2703 Py_INCREF(v);
2704 PyTuple_SET_ITEM(args2, i+1, v);
2707 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2709 end:
2710 Py_XDECREF(cls);
2711 Py_XDECREF(args);
2712 Py_XDECREF(args2);
2713 Py_XDECREF(slots);
2714 Py_XDECREF(state);
2715 Py_XDECREF(names);
2716 Py_XDECREF(listitems);
2717 Py_XDECREF(dictitems);
2718 Py_XDECREF(copy_reg);
2719 Py_XDECREF(newobj);
2720 return res;
2723 static PyObject *
2724 object_reduce_ex(PyObject *self, PyObject *args)
2726 /* Call copy_reg._reduce_ex(self, proto) */
2727 PyObject *reduce, *copy_reg, *res;
2728 int proto = 0;
2730 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2731 return NULL;
2733 reduce = PyObject_GetAttrString(self, "__reduce__");
2734 if (reduce == NULL)
2735 PyErr_Clear();
2736 else {
2737 PyObject *cls, *clsreduce, *objreduce;
2738 int override;
2739 cls = PyObject_GetAttrString(self, "__class__");
2740 if (cls == NULL) {
2741 Py_DECREF(reduce);
2742 return NULL;
2744 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2745 Py_DECREF(cls);
2746 if (clsreduce == NULL) {
2747 Py_DECREF(reduce);
2748 return NULL;
2750 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2751 "__reduce__");
2752 override = (clsreduce != objreduce);
2753 Py_DECREF(clsreduce);
2754 if (override) {
2755 res = PyObject_CallObject(reduce, NULL);
2756 Py_DECREF(reduce);
2757 return res;
2759 else
2760 Py_DECREF(reduce);
2763 if (proto >= 2)
2764 return reduce_2(self);
2766 copy_reg = import_copy_reg();
2767 if (!copy_reg)
2768 return NULL;
2770 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2771 Py_DECREF(copy_reg);
2773 return res;
2776 static PyMethodDef object_methods[] = {
2777 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2778 PyDoc_STR("helper for pickle")},
2779 {"__reduce__", object_reduce_ex, METH_VARARGS,
2780 PyDoc_STR("helper for pickle")},
2785 PyTypeObject PyBaseObject_Type = {
2786 PyObject_HEAD_INIT(&PyType_Type)
2787 0, /* ob_size */
2788 "object", /* tp_name */
2789 sizeof(PyObject), /* tp_basicsize */
2790 0, /* tp_itemsize */
2791 object_dealloc, /* tp_dealloc */
2792 0, /* tp_print */
2793 0, /* tp_getattr */
2794 0, /* tp_setattr */
2795 0, /* tp_compare */
2796 object_repr, /* tp_repr */
2797 0, /* tp_as_number */
2798 0, /* tp_as_sequence */
2799 0, /* tp_as_mapping */
2800 object_hash, /* tp_hash */
2801 0, /* tp_call */
2802 object_str, /* tp_str */
2803 PyObject_GenericGetAttr, /* tp_getattro */
2804 PyObject_GenericSetAttr, /* tp_setattro */
2805 0, /* tp_as_buffer */
2806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2807 PyDoc_STR("The most base type"), /* tp_doc */
2808 0, /* tp_traverse */
2809 0, /* tp_clear */
2810 0, /* tp_richcompare */
2811 0, /* tp_weaklistoffset */
2812 0, /* tp_iter */
2813 0, /* tp_iternext */
2814 object_methods, /* tp_methods */
2815 0, /* tp_members */
2816 object_getsets, /* tp_getset */
2817 0, /* tp_base */
2818 0, /* tp_dict */
2819 0, /* tp_descr_get */
2820 0, /* tp_descr_set */
2821 0, /* tp_dictoffset */
2822 object_init, /* tp_init */
2823 PyType_GenericAlloc, /* tp_alloc */
2824 object_new, /* tp_new */
2825 PyObject_Del, /* tp_free */
2829 /* Initialize the __dict__ in a type object */
2831 static int
2832 add_methods(PyTypeObject *type, PyMethodDef *meth)
2834 PyObject *dict = type->tp_dict;
2836 for (; meth->ml_name != NULL; meth++) {
2837 PyObject *descr;
2838 if (PyDict_GetItemString(dict, meth->ml_name) &&
2839 !(meth->ml_flags & METH_COEXIST))
2840 continue;
2841 if (meth->ml_flags & METH_CLASS) {
2842 if (meth->ml_flags & METH_STATIC) {
2843 PyErr_SetString(PyExc_ValueError,
2844 "method cannot be both class and static");
2845 return -1;
2847 descr = PyDescr_NewClassMethod(type, meth);
2849 else if (meth->ml_flags & METH_STATIC) {
2850 PyObject *cfunc = PyCFunction_New(meth, NULL);
2851 if (cfunc == NULL)
2852 return -1;
2853 descr = PyStaticMethod_New(cfunc);
2854 Py_DECREF(cfunc);
2856 else {
2857 descr = PyDescr_NewMethod(type, meth);
2859 if (descr == NULL)
2860 return -1;
2861 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2862 return -1;
2863 Py_DECREF(descr);
2865 return 0;
2868 static int
2869 add_members(PyTypeObject *type, PyMemberDef *memb)
2871 PyObject *dict = type->tp_dict;
2873 for (; memb->name != NULL; memb++) {
2874 PyObject *descr;
2875 if (PyDict_GetItemString(dict, memb->name))
2876 continue;
2877 descr = PyDescr_NewMember(type, memb);
2878 if (descr == NULL)
2879 return -1;
2880 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2881 return -1;
2882 Py_DECREF(descr);
2884 return 0;
2887 static int
2888 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2890 PyObject *dict = type->tp_dict;
2892 for (; gsp->name != NULL; gsp++) {
2893 PyObject *descr;
2894 if (PyDict_GetItemString(dict, gsp->name))
2895 continue;
2896 descr = PyDescr_NewGetSet(type, gsp);
2898 if (descr == NULL)
2899 return -1;
2900 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2901 return -1;
2902 Py_DECREF(descr);
2904 return 0;
2907 static void
2908 inherit_special(PyTypeObject *type, PyTypeObject *base)
2910 Py_ssize_t oldsize, newsize;
2912 /* Special flag magic */
2913 if (!type->tp_as_buffer && base->tp_as_buffer) {
2914 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2915 type->tp_flags |=
2916 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2918 if (!type->tp_as_sequence && base->tp_as_sequence) {
2919 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2920 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2922 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2923 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2924 if ((!type->tp_as_number && base->tp_as_number) ||
2925 (!type->tp_as_sequence && base->tp_as_sequence)) {
2926 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2927 if (!type->tp_as_number && !type->tp_as_sequence) {
2928 type->tp_flags |= base->tp_flags &
2929 Py_TPFLAGS_HAVE_INPLACEOPS;
2932 /* Wow */
2934 if (!type->tp_as_number && base->tp_as_number) {
2935 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2936 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2939 /* Copying basicsize is connected to the GC flags */
2940 oldsize = base->tp_basicsize;
2941 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2942 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2943 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2944 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2945 (!type->tp_traverse && !type->tp_clear)) {
2946 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2947 if (type->tp_traverse == NULL)
2948 type->tp_traverse = base->tp_traverse;
2949 if (type->tp_clear == NULL)
2950 type->tp_clear = base->tp_clear;
2952 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2953 /* The condition below could use some explanation.
2954 It appears that tp_new is not inherited for static types
2955 whose base class is 'object'; this seems to be a precaution
2956 so that old extension types don't suddenly become
2957 callable (object.__new__ wouldn't insure the invariants
2958 that the extension type's own factory function ensures).
2959 Heap types, of course, are under our control, so they do
2960 inherit tp_new; static extension types that specify some
2961 other built-in type as the default are considered
2962 new-style-aware so they also inherit object.__new__. */
2963 if (base != &PyBaseObject_Type ||
2964 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2965 if (type->tp_new == NULL)
2966 type->tp_new = base->tp_new;
2969 type->tp_basicsize = newsize;
2971 /* Copy other non-function slots */
2973 #undef COPYVAL
2974 #define COPYVAL(SLOT) \
2975 if (type->SLOT == 0) type->SLOT = base->SLOT
2977 COPYVAL(tp_itemsize);
2978 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2979 COPYVAL(tp_weaklistoffset);
2981 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2982 COPYVAL(tp_dictoffset);
2986 static void
2987 inherit_slots(PyTypeObject *type, PyTypeObject *base)
2989 PyTypeObject *basebase;
2991 #undef SLOTDEFINED
2992 #undef COPYSLOT
2993 #undef COPYNUM
2994 #undef COPYSEQ
2995 #undef COPYMAP
2996 #undef COPYBUF
2998 #define SLOTDEFINED(SLOT) \
2999 (base->SLOT != 0 && \
3000 (basebase == NULL || base->SLOT != basebase->SLOT))
3002 #define COPYSLOT(SLOT) \
3003 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3005 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3006 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3007 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3008 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3010 /* This won't inherit indirect slots (from tp_as_number etc.)
3011 if type doesn't provide the space. */
3013 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3014 basebase = base->tp_base;
3015 if (basebase->tp_as_number == NULL)
3016 basebase = NULL;
3017 COPYNUM(nb_add);
3018 COPYNUM(nb_subtract);
3019 COPYNUM(nb_multiply);
3020 COPYNUM(nb_divide);
3021 COPYNUM(nb_remainder);
3022 COPYNUM(nb_divmod);
3023 COPYNUM(nb_power);
3024 COPYNUM(nb_negative);
3025 COPYNUM(nb_positive);
3026 COPYNUM(nb_absolute);
3027 COPYNUM(nb_nonzero);
3028 COPYNUM(nb_invert);
3029 COPYNUM(nb_lshift);
3030 COPYNUM(nb_rshift);
3031 COPYNUM(nb_and);
3032 COPYNUM(nb_xor);
3033 COPYNUM(nb_or);
3034 COPYNUM(nb_coerce);
3035 COPYNUM(nb_int);
3036 COPYNUM(nb_long);
3037 COPYNUM(nb_float);
3038 COPYNUM(nb_oct);
3039 COPYNUM(nb_hex);
3040 COPYNUM(nb_inplace_add);
3041 COPYNUM(nb_inplace_subtract);
3042 COPYNUM(nb_inplace_multiply);
3043 COPYNUM(nb_inplace_divide);
3044 COPYNUM(nb_inplace_remainder);
3045 COPYNUM(nb_inplace_power);
3046 COPYNUM(nb_inplace_lshift);
3047 COPYNUM(nb_inplace_rshift);
3048 COPYNUM(nb_inplace_and);
3049 COPYNUM(nb_inplace_xor);
3050 COPYNUM(nb_inplace_or);
3051 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3052 COPYNUM(nb_true_divide);
3053 COPYNUM(nb_floor_divide);
3054 COPYNUM(nb_inplace_true_divide);
3055 COPYNUM(nb_inplace_floor_divide);
3057 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3058 COPYNUM(nb_index);
3062 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3063 basebase = base->tp_base;
3064 if (basebase->tp_as_sequence == NULL)
3065 basebase = NULL;
3066 COPYSEQ(sq_length);
3067 COPYSEQ(sq_concat);
3068 COPYSEQ(sq_repeat);
3069 COPYSEQ(sq_item);
3070 COPYSEQ(sq_slice);
3071 COPYSEQ(sq_ass_item);
3072 COPYSEQ(sq_ass_slice);
3073 COPYSEQ(sq_contains);
3074 COPYSEQ(sq_inplace_concat);
3075 COPYSEQ(sq_inplace_repeat);
3078 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3079 basebase = base->tp_base;
3080 if (basebase->tp_as_mapping == NULL)
3081 basebase = NULL;
3082 COPYMAP(mp_length);
3083 COPYMAP(mp_subscript);
3084 COPYMAP(mp_ass_subscript);
3087 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3088 basebase = base->tp_base;
3089 if (basebase->tp_as_buffer == NULL)
3090 basebase = NULL;
3091 COPYBUF(bf_getreadbuffer);
3092 COPYBUF(bf_getwritebuffer);
3093 COPYBUF(bf_getsegcount);
3094 COPYBUF(bf_getcharbuffer);
3097 basebase = base->tp_base;
3099 COPYSLOT(tp_dealloc);
3100 COPYSLOT(tp_print);
3101 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3102 type->tp_getattr = base->tp_getattr;
3103 type->tp_getattro = base->tp_getattro;
3105 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3106 type->tp_setattr = base->tp_setattr;
3107 type->tp_setattro = base->tp_setattro;
3109 /* tp_compare see tp_richcompare */
3110 COPYSLOT(tp_repr);
3111 /* tp_hash see tp_richcompare */
3112 COPYSLOT(tp_call);
3113 COPYSLOT(tp_str);
3114 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3115 if (type->tp_compare == NULL &&
3116 type->tp_richcompare == NULL &&
3117 type->tp_hash == NULL)
3119 type->tp_compare = base->tp_compare;
3120 type->tp_richcompare = base->tp_richcompare;
3121 type->tp_hash = base->tp_hash;
3124 else {
3125 COPYSLOT(tp_compare);
3127 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3128 COPYSLOT(tp_iter);
3129 COPYSLOT(tp_iternext);
3131 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3132 COPYSLOT(tp_descr_get);
3133 COPYSLOT(tp_descr_set);
3134 COPYSLOT(tp_dictoffset);
3135 COPYSLOT(tp_init);
3136 COPYSLOT(tp_alloc);
3137 COPYSLOT(tp_is_gc);
3138 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3139 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3140 /* They agree about gc. */
3141 COPYSLOT(tp_free);
3143 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3144 type->tp_free == NULL &&
3145 base->tp_free == _PyObject_Del) {
3146 /* A bit of magic to plug in the correct default
3147 * tp_free function when a derived class adds gc,
3148 * didn't define tp_free, and the base uses the
3149 * default non-gc tp_free.
3151 type->tp_free = PyObject_GC_Del;
3153 /* else they didn't agree about gc, and there isn't something
3154 * obvious to be done -- the type is on its own.
3159 static int add_operators(PyTypeObject *);
3162 PyType_Ready(PyTypeObject *type)
3164 PyObject *dict, *bases;
3165 PyTypeObject *base;
3166 Py_ssize_t i, n;
3168 if (type->tp_flags & Py_TPFLAGS_READY) {
3169 assert(type->tp_dict != NULL);
3170 return 0;
3172 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3174 type->tp_flags |= Py_TPFLAGS_READYING;
3176 #ifdef Py_TRACE_REFS
3177 /* PyType_Ready is the closest thing we have to a choke point
3178 * for type objects, so is the best place I can think of to try
3179 * to get type objects into the doubly-linked list of all objects.
3180 * Still, not all type objects go thru PyType_Ready.
3182 _Py_AddToAllObjects((PyObject *)type, 0);
3183 #endif
3185 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3186 base = type->tp_base;
3187 if (base == NULL && type != &PyBaseObject_Type) {
3188 base = type->tp_base = &PyBaseObject_Type;
3189 Py_INCREF(base);
3192 /* Now the only way base can still be NULL is if type is
3193 * &PyBaseObject_Type.
3196 /* Initialize the base class */
3197 if (base && base->tp_dict == NULL) {
3198 if (PyType_Ready(base) < 0)
3199 goto error;
3202 /* Initialize ob_type if NULL. This means extensions that want to be
3203 compilable separately on Windows can call PyType_Ready() instead of
3204 initializing the ob_type field of their type objects. */
3205 /* The test for base != NULL is really unnecessary, since base is only
3206 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3207 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3208 know that. */
3209 if (type->ob_type == NULL && base != NULL)
3210 type->ob_type = base->ob_type;
3212 /* Initialize tp_bases */
3213 bases = type->tp_bases;
3214 if (bases == NULL) {
3215 if (base == NULL)
3216 bases = PyTuple_New(0);
3217 else
3218 bases = PyTuple_Pack(1, base);
3219 if (bases == NULL)
3220 goto error;
3221 type->tp_bases = bases;
3224 /* Initialize tp_dict */
3225 dict = type->tp_dict;
3226 if (dict == NULL) {
3227 dict = PyDict_New();
3228 if (dict == NULL)
3229 goto error;
3230 type->tp_dict = dict;
3233 /* Add type-specific descriptors to tp_dict */
3234 if (add_operators(type) < 0)
3235 goto error;
3236 if (type->tp_methods != NULL) {
3237 if (add_methods(type, type->tp_methods) < 0)
3238 goto error;
3240 if (type->tp_members != NULL) {
3241 if (add_members(type, type->tp_members) < 0)
3242 goto error;
3244 if (type->tp_getset != NULL) {
3245 if (add_getset(type, type->tp_getset) < 0)
3246 goto error;
3249 /* Calculate method resolution order */
3250 if (mro_internal(type) < 0) {
3251 goto error;
3254 /* Inherit special flags from dominant base */
3255 if (type->tp_base != NULL)
3256 inherit_special(type, type->tp_base);
3258 /* Initialize tp_dict properly */
3259 bases = type->tp_mro;
3260 assert(bases != NULL);
3261 assert(PyTuple_Check(bases));
3262 n = PyTuple_GET_SIZE(bases);
3263 for (i = 1; i < n; i++) {
3264 PyObject *b = PyTuple_GET_ITEM(bases, i);
3265 if (PyType_Check(b))
3266 inherit_slots(type, (PyTypeObject *)b);
3269 /* Sanity check for tp_free. */
3270 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3271 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3272 /* This base class needs to call tp_free, but doesn't have
3273 * one, or its tp_free is for non-gc'ed objects.
3275 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3276 "gc and is a base type but has inappropriate "
3277 "tp_free slot",
3278 type->tp_name);
3279 goto error;
3282 /* if the type dictionary doesn't contain a __doc__, set it from
3283 the tp_doc slot.
3285 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3286 if (type->tp_doc != NULL) {
3287 PyObject *doc = PyString_FromString(type->tp_doc);
3288 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3289 Py_DECREF(doc);
3290 } else {
3291 PyDict_SetItemString(type->tp_dict,
3292 "__doc__", Py_None);
3296 /* Some more special stuff */
3297 base = type->tp_base;
3298 if (base != NULL) {
3299 if (type->tp_as_number == NULL)
3300 type->tp_as_number = base->tp_as_number;
3301 if (type->tp_as_sequence == NULL)
3302 type->tp_as_sequence = base->tp_as_sequence;
3303 if (type->tp_as_mapping == NULL)
3304 type->tp_as_mapping = base->tp_as_mapping;
3305 if (type->tp_as_buffer == NULL)
3306 type->tp_as_buffer = base->tp_as_buffer;
3309 /* Link into each base class's list of subclasses */
3310 bases = type->tp_bases;
3311 n = PyTuple_GET_SIZE(bases);
3312 for (i = 0; i < n; i++) {
3313 PyObject *b = PyTuple_GET_ITEM(bases, i);
3314 if (PyType_Check(b) &&
3315 add_subclass((PyTypeObject *)b, type) < 0)
3316 goto error;
3319 /* All done -- set the ready flag */
3320 assert(type->tp_dict != NULL);
3321 type->tp_flags =
3322 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3323 return 0;
3325 error:
3326 type->tp_flags &= ~Py_TPFLAGS_READYING;
3327 return -1;
3330 static int
3331 add_subclass(PyTypeObject *base, PyTypeObject *type)
3333 Py_ssize_t i;
3334 int result;
3335 PyObject *list, *ref, *new;
3337 list = base->tp_subclasses;
3338 if (list == NULL) {
3339 base->tp_subclasses = list = PyList_New(0);
3340 if (list == NULL)
3341 return -1;
3343 assert(PyList_Check(list));
3344 new = PyWeakref_NewRef((PyObject *)type, NULL);
3345 i = PyList_GET_SIZE(list);
3346 while (--i >= 0) {
3347 ref = PyList_GET_ITEM(list, i);
3348 assert(PyWeakref_CheckRef(ref));
3349 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3350 return PyList_SetItem(list, i, new);
3352 result = PyList_Append(list, new);
3353 Py_DECREF(new);
3354 return result;
3357 static void
3358 remove_subclass(PyTypeObject *base, PyTypeObject *type)
3360 Py_ssize_t i;
3361 PyObject *list, *ref;
3363 list = base->tp_subclasses;
3364 if (list == NULL) {
3365 return;
3367 assert(PyList_Check(list));
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) == (PyObject*)type) {
3373 /* this can't fail, right? */
3374 PySequence_DelItem(list, i);
3375 return;
3380 static int
3381 check_num_args(PyObject *ob, int n)
3383 if (!PyTuple_CheckExact(ob)) {
3384 PyErr_SetString(PyExc_SystemError,
3385 "PyArg_UnpackTuple() argument list is not a tuple");
3386 return 0;
3388 if (n == PyTuple_GET_SIZE(ob))
3389 return 1;
3390 PyErr_Format(
3391 PyExc_TypeError,
3392 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
3393 return 0;
3396 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3398 /* There's a wrapper *function* for each distinct function typedef used
3399 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3400 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3401 Most tables have only one entry; the tables for binary operators have two
3402 entries, one regular and one with reversed arguments. */
3404 static PyObject *
3405 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
3407 lenfunc func = (lenfunc)wrapped;
3408 Py_ssize_t res;
3410 if (!check_num_args(args, 0))
3411 return NULL;
3412 res = (*func)(self);
3413 if (res == -1 && PyErr_Occurred())
3414 return NULL;
3415 return PyInt_FromLong((long)res);
3418 static PyObject *
3419 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3421 inquiry func = (inquiry)wrapped;
3422 int res;
3424 if (!check_num_args(args, 0))
3425 return NULL;
3426 res = (*func)(self);
3427 if (res == -1 && PyErr_Occurred())
3428 return NULL;
3429 return PyBool_FromLong((long)res);
3432 static PyObject *
3433 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3435 binaryfunc func = (binaryfunc)wrapped;
3436 PyObject *other;
3438 if (!check_num_args(args, 1))
3439 return NULL;
3440 other = PyTuple_GET_ITEM(args, 0);
3441 return (*func)(self, other);
3444 static PyObject *
3445 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3447 binaryfunc func = (binaryfunc)wrapped;
3448 PyObject *other;
3450 if (!check_num_args(args, 1))
3451 return NULL;
3452 other = PyTuple_GET_ITEM(args, 0);
3453 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3454 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3455 Py_INCREF(Py_NotImplemented);
3456 return Py_NotImplemented;
3458 return (*func)(self, other);
3461 static PyObject *
3462 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3464 binaryfunc func = (binaryfunc)wrapped;
3465 PyObject *other;
3467 if (!check_num_args(args, 1))
3468 return NULL;
3469 other = PyTuple_GET_ITEM(args, 0);
3470 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3471 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3472 Py_INCREF(Py_NotImplemented);
3473 return Py_NotImplemented;
3475 return (*func)(other, self);
3478 static PyObject *
3479 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3481 coercion func = (coercion)wrapped;
3482 PyObject *other, *res;
3483 int ok;
3485 if (!check_num_args(args, 1))
3486 return NULL;
3487 other = PyTuple_GET_ITEM(args, 0);
3488 ok = func(&self, &other);
3489 if (ok < 0)
3490 return NULL;
3491 if (ok > 0) {
3492 Py_INCREF(Py_NotImplemented);
3493 return Py_NotImplemented;
3495 res = PyTuple_New(2);
3496 if (res == NULL) {
3497 Py_DECREF(self);
3498 Py_DECREF(other);
3499 return NULL;
3501 PyTuple_SET_ITEM(res, 0, self);
3502 PyTuple_SET_ITEM(res, 1, other);
3503 return res;
3506 static PyObject *
3507 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3509 ternaryfunc func = (ternaryfunc)wrapped;
3510 PyObject *other;
3511 PyObject *third = Py_None;
3513 /* Note: This wrapper only works for __pow__() */
3515 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3516 return NULL;
3517 return (*func)(self, other, third);
3520 static PyObject *
3521 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3523 ternaryfunc func = (ternaryfunc)wrapped;
3524 PyObject *other;
3525 PyObject *third = Py_None;
3527 /* Note: This wrapper only works for __pow__() */
3529 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3530 return NULL;
3531 return (*func)(other, self, third);
3534 static PyObject *
3535 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3537 unaryfunc func = (unaryfunc)wrapped;
3539 if (!check_num_args(args, 0))
3540 return NULL;
3541 return (*func)(self);
3544 static PyObject *
3545 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
3547 ssizeargfunc func = (ssizeargfunc)wrapped;
3548 PyObject* o;
3549 Py_ssize_t i;
3551 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3552 return NULL;
3553 i = PyNumber_Index(o);
3554 if (i == -1 && PyErr_Occurred())
3555 return NULL;
3556 return (*func)(self, i);
3559 static Py_ssize_t
3560 getindex(PyObject *self, PyObject *arg)
3562 Py_ssize_t i;
3564 i = PyNumber_Index(arg);
3565 if (i == -1 && PyErr_Occurred())
3566 return -1;
3567 if (i < 0) {
3568 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3569 if (sq && sq->sq_length) {
3570 Py_ssize_t n = (*sq->sq_length)(self);
3571 if (n < 0)
3572 return -1;
3573 i += n;
3576 return i;
3579 static PyObject *
3580 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3582 ssizeargfunc func = (ssizeargfunc)wrapped;
3583 PyObject *arg;
3584 Py_ssize_t i;
3586 if (PyTuple_GET_SIZE(args) == 1) {
3587 arg = PyTuple_GET_ITEM(args, 0);
3588 i = getindex(self, arg);
3589 if (i == -1 && PyErr_Occurred())
3590 return NULL;
3591 return (*func)(self, i);
3593 check_num_args(args, 1);
3594 assert(PyErr_Occurred());
3595 return NULL;
3598 static PyObject *
3599 wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
3601 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3602 Py_ssize_t i, j;
3604 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3605 return NULL;
3606 return (*func)(self, i, j);
3609 static PyObject *
3610 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3612 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3613 Py_ssize_t i;
3614 int res;
3615 PyObject *arg, *value;
3617 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3618 return NULL;
3619 i = getindex(self, arg);
3620 if (i == -1 && PyErr_Occurred())
3621 return NULL;
3622 res = (*func)(self, i, value);
3623 if (res == -1 && PyErr_Occurred())
3624 return NULL;
3625 Py_INCREF(Py_None);
3626 return Py_None;
3629 static PyObject *
3630 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3632 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3633 Py_ssize_t i;
3634 int res;
3635 PyObject *arg;
3637 if (!check_num_args(args, 1))
3638 return NULL;
3639 arg = PyTuple_GET_ITEM(args, 0);
3640 i = getindex(self, arg);
3641 if (i == -1 && PyErr_Occurred())
3642 return NULL;
3643 res = (*func)(self, i, NULL);
3644 if (res == -1 && PyErr_Occurred())
3645 return NULL;
3646 Py_INCREF(Py_None);
3647 return Py_None;
3650 static PyObject *
3651 wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
3653 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3654 Py_ssize_t i, j;
3655 int res;
3656 PyObject *value;
3658 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
3659 return NULL;
3660 res = (*func)(self, i, j, value);
3661 if (res == -1 && PyErr_Occurred())
3662 return NULL;
3663 Py_INCREF(Py_None);
3664 return Py_None;
3667 static PyObject *
3668 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3670 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3671 Py_ssize_t i, j;
3672 int res;
3674 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3675 return NULL;
3676 res = (*func)(self, i, j, NULL);
3677 if (res == -1 && PyErr_Occurred())
3678 return NULL;
3679 Py_INCREF(Py_None);
3680 return Py_None;
3683 /* XXX objobjproc is a misnomer; should be objargpred */
3684 static PyObject *
3685 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3687 objobjproc func = (objobjproc)wrapped;
3688 int res;
3689 PyObject *value;
3691 if (!check_num_args(args, 1))
3692 return NULL;
3693 value = PyTuple_GET_ITEM(args, 0);
3694 res = (*func)(self, value);
3695 if (res == -1 && PyErr_Occurred())
3696 return NULL;
3697 else
3698 return PyBool_FromLong(res);
3701 static PyObject *
3702 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3704 objobjargproc func = (objobjargproc)wrapped;
3705 int res;
3706 PyObject *key, *value;
3708 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3709 return NULL;
3710 res = (*func)(self, key, value);
3711 if (res == -1 && PyErr_Occurred())
3712 return NULL;
3713 Py_INCREF(Py_None);
3714 return Py_None;
3717 static PyObject *
3718 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3720 objobjargproc func = (objobjargproc)wrapped;
3721 int res;
3722 PyObject *key;
3724 if (!check_num_args(args, 1))
3725 return NULL;
3726 key = PyTuple_GET_ITEM(args, 0);
3727 res = (*func)(self, key, NULL);
3728 if (res == -1 && PyErr_Occurred())
3729 return NULL;
3730 Py_INCREF(Py_None);
3731 return Py_None;
3734 static PyObject *
3735 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3737 cmpfunc func = (cmpfunc)wrapped;
3738 int res;
3739 PyObject *other;
3741 if (!check_num_args(args, 1))
3742 return NULL;
3743 other = PyTuple_GET_ITEM(args, 0);
3744 if (other->ob_type->tp_compare != func &&
3745 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3746 PyErr_Format(
3747 PyExc_TypeError,
3748 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3749 self->ob_type->tp_name,
3750 self->ob_type->tp_name,
3751 other->ob_type->tp_name);
3752 return NULL;
3754 res = (*func)(self, other);
3755 if (PyErr_Occurred())
3756 return NULL;
3757 return PyInt_FromLong((long)res);
3760 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3761 This is called the Carlo Verre hack after its discoverer. */
3762 static int
3763 hackcheck(PyObject *self, setattrofunc func, char *what)
3765 PyTypeObject *type = self->ob_type;
3766 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3767 type = type->tp_base;
3768 /* If type is NULL now, this is a really weird type.
3769 In the same of backwards compatibility (?), just shut up. */
3770 if (type && type->tp_setattro != func) {
3771 PyErr_Format(PyExc_TypeError,
3772 "can't apply this %s to %s object",
3773 what,
3774 type->tp_name);
3775 return 0;
3777 return 1;
3780 static PyObject *
3781 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3783 setattrofunc func = (setattrofunc)wrapped;
3784 int res;
3785 PyObject *name, *value;
3787 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3788 return NULL;
3789 if (!hackcheck(self, func, "__setattr__"))
3790 return NULL;
3791 res = (*func)(self, name, value);
3792 if (res < 0)
3793 return NULL;
3794 Py_INCREF(Py_None);
3795 return Py_None;
3798 static PyObject *
3799 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3801 setattrofunc func = (setattrofunc)wrapped;
3802 int res;
3803 PyObject *name;
3805 if (!check_num_args(args, 1))
3806 return NULL;
3807 name = PyTuple_GET_ITEM(args, 0);
3808 if (!hackcheck(self, func, "__delattr__"))
3809 return NULL;
3810 res = (*func)(self, name, NULL);
3811 if (res < 0)
3812 return NULL;
3813 Py_INCREF(Py_None);
3814 return Py_None;
3817 static PyObject *
3818 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3820 hashfunc func = (hashfunc)wrapped;
3821 long res;
3823 if (!check_num_args(args, 0))
3824 return NULL;
3825 res = (*func)(self);
3826 if (res == -1 && PyErr_Occurred())
3827 return NULL;
3828 return PyInt_FromLong(res);
3831 static PyObject *
3832 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3834 ternaryfunc func = (ternaryfunc)wrapped;
3836 return (*func)(self, args, kwds);
3839 static PyObject *
3840 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3842 richcmpfunc func = (richcmpfunc)wrapped;
3843 PyObject *other;
3845 if (!check_num_args(args, 1))
3846 return NULL;
3847 other = PyTuple_GET_ITEM(args, 0);
3848 return (*func)(self, other, op);
3851 #undef RICHCMP_WRAPPER
3852 #define RICHCMP_WRAPPER(NAME, OP) \
3853 static PyObject * \
3854 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3856 return wrap_richcmpfunc(self, args, wrapped, OP); \
3859 RICHCMP_WRAPPER(lt, Py_LT)
3860 RICHCMP_WRAPPER(le, Py_LE)
3861 RICHCMP_WRAPPER(eq, Py_EQ)
3862 RICHCMP_WRAPPER(ne, Py_NE)
3863 RICHCMP_WRAPPER(gt, Py_GT)
3864 RICHCMP_WRAPPER(ge, Py_GE)
3866 static PyObject *
3867 wrap_next(PyObject *self, PyObject *args, void *wrapped)
3869 unaryfunc func = (unaryfunc)wrapped;
3870 PyObject *res;
3872 if (!check_num_args(args, 0))
3873 return NULL;
3874 res = (*func)(self);
3875 if (res == NULL && !PyErr_Occurred())
3876 PyErr_SetNone(PyExc_StopIteration);
3877 return res;
3880 static PyObject *
3881 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3883 descrgetfunc func = (descrgetfunc)wrapped;
3884 PyObject *obj;
3885 PyObject *type = NULL;
3887 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3888 return NULL;
3889 if (obj == Py_None)
3890 obj = NULL;
3891 if (type == Py_None)
3892 type = NULL;
3893 if (type == NULL &&obj == NULL) {
3894 PyErr_SetString(PyExc_TypeError,
3895 "__get__(None, None) is invalid");
3896 return NULL;
3898 return (*func)(self, obj, type);
3901 static PyObject *
3902 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3904 descrsetfunc func = (descrsetfunc)wrapped;
3905 PyObject *obj, *value;
3906 int ret;
3908 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3909 return NULL;
3910 ret = (*func)(self, obj, value);
3911 if (ret < 0)
3912 return NULL;
3913 Py_INCREF(Py_None);
3914 return Py_None;
3917 static PyObject *
3918 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3920 descrsetfunc func = (descrsetfunc)wrapped;
3921 PyObject *obj;
3922 int ret;
3924 if (!check_num_args(args, 1))
3925 return NULL;
3926 obj = PyTuple_GET_ITEM(args, 0);
3927 ret = (*func)(self, obj, NULL);
3928 if (ret < 0)
3929 return NULL;
3930 Py_INCREF(Py_None);
3931 return Py_None;
3934 static PyObject *
3935 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3937 initproc func = (initproc)wrapped;
3939 if (func(self, args, kwds) < 0)
3940 return NULL;
3941 Py_INCREF(Py_None);
3942 return Py_None;
3945 static PyObject *
3946 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3948 PyTypeObject *type, *subtype, *staticbase;
3949 PyObject *arg0, *res;
3951 if (self == NULL || !PyType_Check(self))
3952 Py_FatalError("__new__() called with non-type 'self'");
3953 type = (PyTypeObject *)self;
3954 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3955 PyErr_Format(PyExc_TypeError,
3956 "%s.__new__(): not enough arguments",
3957 type->tp_name);
3958 return NULL;
3960 arg0 = PyTuple_GET_ITEM(args, 0);
3961 if (!PyType_Check(arg0)) {
3962 PyErr_Format(PyExc_TypeError,
3963 "%s.__new__(X): X is not a type object (%s)",
3964 type->tp_name,
3965 arg0->ob_type->tp_name);
3966 return NULL;
3968 subtype = (PyTypeObject *)arg0;
3969 if (!PyType_IsSubtype(subtype, type)) {
3970 PyErr_Format(PyExc_TypeError,
3971 "%s.__new__(%s): %s is not a subtype of %s",
3972 type->tp_name,
3973 subtype->tp_name,
3974 subtype->tp_name,
3975 type->tp_name);
3976 return NULL;
3979 /* Check that the use doesn't do something silly and unsafe like
3980 object.__new__(dict). To do this, we check that the
3981 most derived base that's not a heap type is this type. */
3982 staticbase = subtype;
3983 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3984 staticbase = staticbase->tp_base;
3985 /* If staticbase is NULL now, it is a really weird type.
3986 In the same of backwards compatibility (?), just shut up. */
3987 if (staticbase && staticbase->tp_new != type->tp_new) {
3988 PyErr_Format(PyExc_TypeError,
3989 "%s.__new__(%s) is not safe, use %s.__new__()",
3990 type->tp_name,
3991 subtype->tp_name,
3992 staticbase == NULL ? "?" : staticbase->tp_name);
3993 return NULL;
3996 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3997 if (args == NULL)
3998 return NULL;
3999 res = type->tp_new(subtype, args, kwds);
4000 Py_DECREF(args);
4001 return res;
4004 static struct PyMethodDef tp_new_methoddef[] = {
4005 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
4006 PyDoc_STR("T.__new__(S, ...) -> "
4007 "a new object with type S, a subtype of T")},
4011 static int
4012 add_tp_new_wrapper(PyTypeObject *type)
4014 PyObject *func;
4016 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4017 return 0;
4018 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4019 if (func == NULL)
4020 return -1;
4021 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4022 Py_DECREF(func);
4023 return -1;
4025 Py_DECREF(func);
4026 return 0;
4029 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4030 below at override_slots() for more explanation. */
4032 #define SLOT0(FUNCNAME, OPSTR) \
4033 static PyObject * \
4034 FUNCNAME(PyObject *self) \
4036 static PyObject *cache_str; \
4037 return call_method(self, OPSTR, &cache_str, "()"); \
4040 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4041 static PyObject * \
4042 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4044 static PyObject *cache_str; \
4045 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4048 /* Boolean helper for SLOT1BINFULL().
4049 right.__class__ is a nontrivial subclass of left.__class__. */
4050 static int
4051 method_is_overloaded(PyObject *left, PyObject *right, char *name)
4053 PyObject *a, *b;
4054 int ok;
4056 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4057 if (b == NULL) {
4058 PyErr_Clear();
4059 /* If right doesn't have it, it's not overloaded */
4060 return 0;
4063 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4064 if (a == NULL) {
4065 PyErr_Clear();
4066 Py_DECREF(b);
4067 /* If right has it but left doesn't, it's overloaded */
4068 return 1;
4071 ok = PyObject_RichCompareBool(a, b, Py_NE);
4072 Py_DECREF(a);
4073 Py_DECREF(b);
4074 if (ok < 0) {
4075 PyErr_Clear();
4076 return 0;
4079 return ok;
4083 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4084 static PyObject * \
4085 FUNCNAME(PyObject *self, PyObject *other) \
4087 static PyObject *cache_str, *rcache_str; \
4088 int do_other = self->ob_type != other->ob_type && \
4089 other->ob_type->tp_as_number != NULL && \
4090 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4091 if (self->ob_type->tp_as_number != NULL && \
4092 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4093 PyObject *r; \
4094 if (do_other && \
4095 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4096 method_is_overloaded(self, other, ROPSTR)) { \
4097 r = call_maybe( \
4098 other, ROPSTR, &rcache_str, "(O)", self); \
4099 if (r != Py_NotImplemented) \
4100 return r; \
4101 Py_DECREF(r); \
4102 do_other = 0; \
4104 r = call_maybe( \
4105 self, OPSTR, &cache_str, "(O)", other); \
4106 if (r != Py_NotImplemented || \
4107 other->ob_type == self->ob_type) \
4108 return r; \
4109 Py_DECREF(r); \
4111 if (do_other) { \
4112 return call_maybe( \
4113 other, ROPSTR, &rcache_str, "(O)", self); \
4115 Py_INCREF(Py_NotImplemented); \
4116 return Py_NotImplemented; \
4119 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4120 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4122 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4123 static PyObject * \
4124 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4126 static PyObject *cache_str; \
4127 return call_method(self, OPSTR, &cache_str, \
4128 "(" ARGCODES ")", arg1, arg2); \
4131 static Py_ssize_t
4132 slot_sq_length(PyObject *self)
4134 static PyObject *len_str;
4135 PyObject *res = call_method(self, "__len__", &len_str, "()");
4136 Py_ssize_t temp;
4137 Py_ssize_t len;
4139 if (res == NULL)
4140 return -1;
4141 temp = PyInt_AsSsize_t(res);
4142 len = (int)temp;
4143 Py_DECREF(res);
4144 if (len == -1 && PyErr_Occurred())
4145 return -1;
4146 #if SIZEOF_SIZE_T < SIZEOF_LONG
4147 /* Overflow check -- range of PyInt is more than C ssize_t */
4148 if (len != temp) {
4149 PyErr_SetString(PyExc_OverflowError,
4150 "__len__() should return 0 <= outcome < 2**31");
4151 return -1;
4153 #endif
4154 if (len < 0) {
4155 PyErr_SetString(PyExc_ValueError,
4156 "__len__() should return >= 0");
4157 return -1;
4159 return len;
4162 /* Super-optimized version of slot_sq_item.
4163 Other slots could do the same... */
4164 static PyObject *
4165 slot_sq_item(PyObject *self, Py_ssize_t i)
4167 static PyObject *getitem_str;
4168 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4169 descrgetfunc f;
4171 if (getitem_str == NULL) {
4172 getitem_str = PyString_InternFromString("__getitem__");
4173 if (getitem_str == NULL)
4174 return NULL;
4176 func = _PyType_Lookup(self->ob_type, getitem_str);
4177 if (func != NULL) {
4178 if ((f = func->ob_type->tp_descr_get) == NULL)
4179 Py_INCREF(func);
4180 else {
4181 func = f(func, self, (PyObject *)(self->ob_type));
4182 if (func == NULL) {
4183 return NULL;
4186 ival = PyInt_FromSsize_t(i);
4187 if (ival != NULL) {
4188 args = PyTuple_New(1);
4189 if (args != NULL) {
4190 PyTuple_SET_ITEM(args, 0, ival);
4191 retval = PyObject_Call(func, args, NULL);
4192 Py_XDECREF(args);
4193 Py_XDECREF(func);
4194 return retval;
4198 else {
4199 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4201 Py_XDECREF(args);
4202 Py_XDECREF(ival);
4203 Py_XDECREF(func);
4204 return NULL;
4207 SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
4209 static int
4210 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
4212 PyObject *res;
4213 static PyObject *delitem_str, *setitem_str;
4215 if (value == NULL)
4216 res = call_method(self, "__delitem__", &delitem_str,
4217 "(i)", index);
4218 else
4219 res = call_method(self, "__setitem__", &setitem_str,
4220 "(iO)", index, value);
4221 if (res == NULL)
4222 return -1;
4223 Py_DECREF(res);
4224 return 0;
4227 static int
4228 slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
4230 PyObject *res;
4231 static PyObject *delslice_str, *setslice_str;
4233 if (value == NULL)
4234 res = call_method(self, "__delslice__", &delslice_str,
4235 "(ii)", i, j);
4236 else
4237 res = call_method(self, "__setslice__", &setslice_str,
4238 "(iiO)", i, j, value);
4239 if (res == NULL)
4240 return -1;
4241 Py_DECREF(res);
4242 return 0;
4245 static int
4246 slot_sq_contains(PyObject *self, PyObject *value)
4248 PyObject *func, *res, *args;
4249 int result = -1;
4251 static PyObject *contains_str;
4253 func = lookup_maybe(self, "__contains__", &contains_str);
4254 if (func != NULL) {
4255 args = PyTuple_Pack(1, value);
4256 if (args == NULL)
4257 res = NULL;
4258 else {
4259 res = PyObject_Call(func, args, NULL);
4260 Py_DECREF(args);
4262 Py_DECREF(func);
4263 if (res != NULL) {
4264 result = PyObject_IsTrue(res);
4265 Py_DECREF(res);
4268 else if (! PyErr_Occurred()) {
4269 /* Possible results: -1 and 1 */
4270 result = (int)_PySequence_IterSearch(self, value,
4271 PY_ITERSEARCH_CONTAINS);
4273 return result;
4276 #define slot_mp_length slot_sq_length
4278 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4280 static int
4281 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4283 PyObject *res;
4284 static PyObject *delitem_str, *setitem_str;
4286 if (value == NULL)
4287 res = call_method(self, "__delitem__", &delitem_str,
4288 "(O)", key);
4289 else
4290 res = call_method(self, "__setitem__", &setitem_str,
4291 "(OO)", key, value);
4292 if (res == NULL)
4293 return -1;
4294 Py_DECREF(res);
4295 return 0;
4298 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4299 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4300 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4301 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4302 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4303 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4305 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4307 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4308 nb_power, "__pow__", "__rpow__")
4310 static PyObject *
4311 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4313 static PyObject *pow_str;
4315 if (modulus == Py_None)
4316 return slot_nb_power_binary(self, other);
4317 /* Three-arg power doesn't use __rpow__. But ternary_op
4318 can call this when the second argument's type uses
4319 slot_nb_power, so check before calling self.__pow__. */
4320 if (self->ob_type->tp_as_number != NULL &&
4321 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4322 return call_method(self, "__pow__", &pow_str,
4323 "(OO)", other, modulus);
4325 Py_INCREF(Py_NotImplemented);
4326 return Py_NotImplemented;
4329 SLOT0(slot_nb_negative, "__neg__")
4330 SLOT0(slot_nb_positive, "__pos__")
4331 SLOT0(slot_nb_absolute, "__abs__")
4333 static int
4334 slot_nb_nonzero(PyObject *self)
4336 PyObject *func, *args;
4337 static PyObject *nonzero_str, *len_str;
4338 int result = -1;
4340 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4341 if (func == NULL) {
4342 if (PyErr_Occurred())
4343 return -1;
4344 func = lookup_maybe(self, "__len__", &len_str);
4345 if (func == NULL)
4346 return PyErr_Occurred() ? -1 : 1;
4348 args = PyTuple_New(0);
4349 if (args != NULL) {
4350 PyObject *temp = PyObject_Call(func, args, NULL);
4351 Py_DECREF(args);
4352 if (temp != NULL) {
4353 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4354 result = PyObject_IsTrue(temp);
4355 else {
4356 PyErr_Format(PyExc_TypeError,
4357 "__nonzero__ should return "
4358 "bool or int, returned %s",
4359 temp->ob_type->tp_name);
4360 result = -1;
4362 Py_DECREF(temp);
4365 Py_DECREF(func);
4366 return result;
4370 static Py_ssize_t
4371 slot_nb_index(PyObject *self)
4373 static PyObject *index_str;
4374 PyObject *temp = call_method(self, "__index__", &index_str, "()");
4375 Py_ssize_t result;
4377 if (temp == NULL)
4378 return -1;
4379 if (PyInt_CheckExact(temp) || PyLong_CheckExact(temp)) {
4380 result = temp->ob_type->tp_as_number->nb_index(temp);
4382 else {
4383 PyErr_SetString(PyExc_TypeError,
4384 "__index__ must return an int or a long");
4385 result = -1;
4387 Py_DECREF(temp);
4388 return result;
4392 SLOT0(slot_nb_invert, "__invert__")
4393 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4394 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4395 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4396 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4397 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4399 static int
4400 slot_nb_coerce(PyObject **a, PyObject **b)
4402 static PyObject *coerce_str;
4403 PyObject *self = *a, *other = *b;
4405 if (self->ob_type->tp_as_number != NULL &&
4406 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4407 PyObject *r;
4408 r = call_maybe(
4409 self, "__coerce__", &coerce_str, "(O)", other);
4410 if (r == NULL)
4411 return -1;
4412 if (r == Py_NotImplemented) {
4413 Py_DECREF(r);
4415 else {
4416 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4417 PyErr_SetString(PyExc_TypeError,
4418 "__coerce__ didn't return a 2-tuple");
4419 Py_DECREF(r);
4420 return -1;
4422 *a = PyTuple_GET_ITEM(r, 0);
4423 Py_INCREF(*a);
4424 *b = PyTuple_GET_ITEM(r, 1);
4425 Py_INCREF(*b);
4426 Py_DECREF(r);
4427 return 0;
4430 if (other->ob_type->tp_as_number != NULL &&
4431 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4432 PyObject *r;
4433 r = call_maybe(
4434 other, "__coerce__", &coerce_str, "(O)", self);
4435 if (r == NULL)
4436 return -1;
4437 if (r == Py_NotImplemented) {
4438 Py_DECREF(r);
4439 return 1;
4441 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4442 PyErr_SetString(PyExc_TypeError,
4443 "__coerce__ didn't return a 2-tuple");
4444 Py_DECREF(r);
4445 return -1;
4447 *a = PyTuple_GET_ITEM(r, 1);
4448 Py_INCREF(*a);
4449 *b = PyTuple_GET_ITEM(r, 0);
4450 Py_INCREF(*b);
4451 Py_DECREF(r);
4452 return 0;
4454 return 1;
4457 SLOT0(slot_nb_int, "__int__")
4458 SLOT0(slot_nb_long, "__long__")
4459 SLOT0(slot_nb_float, "__float__")
4460 SLOT0(slot_nb_oct, "__oct__")
4461 SLOT0(slot_nb_hex, "__hex__")
4462 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4463 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4464 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4465 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4466 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4467 SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
4468 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4469 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4470 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4471 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4472 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4473 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4474 "__floordiv__", "__rfloordiv__")
4475 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4476 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4477 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4479 static int
4480 half_compare(PyObject *self, PyObject *other)
4482 PyObject *func, *args, *res;
4483 static PyObject *cmp_str;
4484 Py_ssize_t c;
4486 func = lookup_method(self, "__cmp__", &cmp_str);
4487 if (func == NULL) {
4488 PyErr_Clear();
4490 else {
4491 args = PyTuple_Pack(1, other);
4492 if (args == NULL)
4493 res = NULL;
4494 else {
4495 res = PyObject_Call(func, args, NULL);
4496 Py_DECREF(args);
4498 Py_DECREF(func);
4499 if (res != Py_NotImplemented) {
4500 if (res == NULL)
4501 return -2;
4502 c = PyInt_AsLong(res);
4503 Py_DECREF(res);
4504 if (c == -1 && PyErr_Occurred())
4505 return -2;
4506 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4508 Py_DECREF(res);
4510 return 2;
4513 /* This slot is published for the benefit of try_3way_compare in object.c */
4515 _PyObject_SlotCompare(PyObject *self, PyObject *other)
4517 int c;
4519 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4520 c = half_compare(self, other);
4521 if (c <= 1)
4522 return c;
4524 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4525 c = half_compare(other, self);
4526 if (c < -1)
4527 return -2;
4528 if (c <= 1)
4529 return -c;
4531 return (void *)self < (void *)other ? -1 :
4532 (void *)self > (void *)other ? 1 : 0;
4535 static PyObject *
4536 slot_tp_repr(PyObject *self)
4538 PyObject *func, *res;
4539 static PyObject *repr_str;
4541 func = lookup_method(self, "__repr__", &repr_str);
4542 if (func != NULL) {
4543 res = PyEval_CallObject(func, NULL);
4544 Py_DECREF(func);
4545 return res;
4547 PyErr_Clear();
4548 return PyString_FromFormat("<%s object at %p>",
4549 self->ob_type->tp_name, self);
4552 static PyObject *
4553 slot_tp_str(PyObject *self)
4555 PyObject *func, *res;
4556 static PyObject *str_str;
4558 func = lookup_method(self, "__str__", &str_str);
4559 if (func != NULL) {
4560 res = PyEval_CallObject(func, NULL);
4561 Py_DECREF(func);
4562 return res;
4564 else {
4565 PyErr_Clear();
4566 return slot_tp_repr(self);
4570 static long
4571 slot_tp_hash(PyObject *self)
4573 PyObject *func;
4574 static PyObject *hash_str, *eq_str, *cmp_str;
4575 long h;
4577 func = lookup_method(self, "__hash__", &hash_str);
4579 if (func != NULL) {
4580 PyObject *res = PyEval_CallObject(func, NULL);
4581 Py_DECREF(func);
4582 if (res == NULL)
4583 return -1;
4584 h = PyInt_AsLong(res);
4585 Py_DECREF(res);
4587 else {
4588 PyErr_Clear();
4589 func = lookup_method(self, "__eq__", &eq_str);
4590 if (func == NULL) {
4591 PyErr_Clear();
4592 func = lookup_method(self, "__cmp__", &cmp_str);
4594 if (func != NULL) {
4595 Py_DECREF(func);
4596 PyErr_SetString(PyExc_TypeError, "unhashable type");
4597 return -1;
4599 PyErr_Clear();
4600 h = _Py_HashPointer((void *)self);
4602 if (h == -1 && !PyErr_Occurred())
4603 h = -2;
4604 return h;
4607 static PyObject *
4608 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4610 static PyObject *call_str;
4611 PyObject *meth = lookup_method(self, "__call__", &call_str);
4612 PyObject *res;
4614 if (meth == NULL)
4615 return NULL;
4616 res = PyObject_Call(meth, args, kwds);
4617 Py_DECREF(meth);
4618 return res;
4621 /* There are two slot dispatch functions for tp_getattro.
4623 - slot_tp_getattro() is used when __getattribute__ is overridden
4624 but no __getattr__ hook is present;
4626 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4628 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4629 detects the absence of __getattr__ and then installs the simpler slot if
4630 necessary. */
4632 static PyObject *
4633 slot_tp_getattro(PyObject *self, PyObject *name)
4635 static PyObject *getattribute_str = NULL;
4636 return call_method(self, "__getattribute__", &getattribute_str,
4637 "(O)", name);
4640 static PyObject *
4641 slot_tp_getattr_hook(PyObject *self, PyObject *name)
4643 PyTypeObject *tp = self->ob_type;
4644 PyObject *getattr, *getattribute, *res;
4645 static PyObject *getattribute_str = NULL;
4646 static PyObject *getattr_str = NULL;
4648 if (getattr_str == NULL) {
4649 getattr_str = PyString_InternFromString("__getattr__");
4650 if (getattr_str == NULL)
4651 return NULL;
4653 if (getattribute_str == NULL) {
4654 getattribute_str =
4655 PyString_InternFromString("__getattribute__");
4656 if (getattribute_str == NULL)
4657 return NULL;
4659 getattr = _PyType_Lookup(tp, getattr_str);
4660 if (getattr == NULL) {
4661 /* No __getattr__ hook: use a simpler dispatcher */
4662 tp->tp_getattro = slot_tp_getattro;
4663 return slot_tp_getattro(self, name);
4665 getattribute = _PyType_Lookup(tp, getattribute_str);
4666 if (getattribute == NULL ||
4667 (getattribute->ob_type == &PyWrapperDescr_Type &&
4668 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4669 (void *)PyObject_GenericGetAttr))
4670 res = PyObject_GenericGetAttr(self, name);
4671 else
4672 res = PyObject_CallFunction(getattribute, "OO", self, name);
4673 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4674 PyErr_Clear();
4675 res = PyObject_CallFunction(getattr, "OO", self, name);
4677 return res;
4680 static int
4681 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4683 PyObject *res;
4684 static PyObject *delattr_str, *setattr_str;
4686 if (value == NULL)
4687 res = call_method(self, "__delattr__", &delattr_str,
4688 "(O)", name);
4689 else
4690 res = call_method(self, "__setattr__", &setattr_str,
4691 "(OO)", name, value);
4692 if (res == NULL)
4693 return -1;
4694 Py_DECREF(res);
4695 return 0;
4698 /* Map rich comparison operators to their __xx__ namesakes */
4699 static char *name_op[] = {
4700 "__lt__",
4701 "__le__",
4702 "__eq__",
4703 "__ne__",
4704 "__gt__",
4705 "__ge__",
4708 static PyObject *
4709 half_richcompare(PyObject *self, PyObject *other, int op)
4711 PyObject *func, *args, *res;
4712 static PyObject *op_str[6];
4714 func = lookup_method(self, name_op[op], &op_str[op]);
4715 if (func == NULL) {
4716 PyErr_Clear();
4717 Py_INCREF(Py_NotImplemented);
4718 return Py_NotImplemented;
4720 args = PyTuple_Pack(1, other);
4721 if (args == NULL)
4722 res = NULL;
4723 else {
4724 res = PyObject_Call(func, args, NULL);
4725 Py_DECREF(args);
4727 Py_DECREF(func);
4728 return res;
4731 static PyObject *
4732 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4734 PyObject *res;
4736 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4737 res = half_richcompare(self, other, op);
4738 if (res != Py_NotImplemented)
4739 return res;
4740 Py_DECREF(res);
4742 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4743 res = half_richcompare(other, self, _Py_SwappedOp[op]);
4744 if (res != Py_NotImplemented) {
4745 return res;
4747 Py_DECREF(res);
4749 Py_INCREF(Py_NotImplemented);
4750 return Py_NotImplemented;
4753 static PyObject *
4754 slot_tp_iter(PyObject *self)
4756 PyObject *func, *res;
4757 static PyObject *iter_str, *getitem_str;
4759 func = lookup_method(self, "__iter__", &iter_str);
4760 if (func != NULL) {
4761 PyObject *args;
4762 args = res = PyTuple_New(0);
4763 if (args != NULL) {
4764 res = PyObject_Call(func, args, NULL);
4765 Py_DECREF(args);
4767 Py_DECREF(func);
4768 return res;
4770 PyErr_Clear();
4771 func = lookup_method(self, "__getitem__", &getitem_str);
4772 if (func == NULL) {
4773 PyErr_SetString(PyExc_TypeError,
4774 "iteration over non-sequence");
4775 return NULL;
4777 Py_DECREF(func);
4778 return PySeqIter_New(self);
4781 static PyObject *
4782 slot_tp_iternext(PyObject *self)
4784 static PyObject *next_str;
4785 return call_method(self, "next", &next_str, "()");
4788 static PyObject *
4789 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4791 PyTypeObject *tp = self->ob_type;
4792 PyObject *get;
4793 static PyObject *get_str = NULL;
4795 if (get_str == NULL) {
4796 get_str = PyString_InternFromString("__get__");
4797 if (get_str == NULL)
4798 return NULL;
4800 get = _PyType_Lookup(tp, get_str);
4801 if (get == NULL) {
4802 /* Avoid further slowdowns */
4803 if (tp->tp_descr_get == slot_tp_descr_get)
4804 tp->tp_descr_get = NULL;
4805 Py_INCREF(self);
4806 return self;
4808 if (obj == NULL)
4809 obj = Py_None;
4810 if (type == NULL)
4811 type = Py_None;
4812 return PyObject_CallFunction(get, "OOO", self, obj, type);
4815 static int
4816 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4818 PyObject *res;
4819 static PyObject *del_str, *set_str;
4821 if (value == NULL)
4822 res = call_method(self, "__delete__", &del_str,
4823 "(O)", target);
4824 else
4825 res = call_method(self, "__set__", &set_str,
4826 "(OO)", target, value);
4827 if (res == NULL)
4828 return -1;
4829 Py_DECREF(res);
4830 return 0;
4833 static int
4834 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4836 static PyObject *init_str;
4837 PyObject *meth = lookup_method(self, "__init__", &init_str);
4838 PyObject *res;
4840 if (meth == NULL)
4841 return -1;
4842 res = PyObject_Call(meth, args, kwds);
4843 Py_DECREF(meth);
4844 if (res == NULL)
4845 return -1;
4846 if (res != Py_None) {
4847 PyErr_SetString(PyExc_TypeError,
4848 "__init__() should return None");
4849 Py_DECREF(res);
4850 return -1;
4852 Py_DECREF(res);
4853 return 0;
4856 static PyObject *
4857 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4859 static PyObject *new_str;
4860 PyObject *func;
4861 PyObject *newargs, *x;
4862 Py_ssize_t i, n;
4864 if (new_str == NULL) {
4865 new_str = PyString_InternFromString("__new__");
4866 if (new_str == NULL)
4867 return NULL;
4869 func = PyObject_GetAttr((PyObject *)type, new_str);
4870 if (func == NULL)
4871 return NULL;
4872 assert(PyTuple_Check(args));
4873 n = PyTuple_GET_SIZE(args);
4874 newargs = PyTuple_New(n+1);
4875 if (newargs == NULL)
4876 return NULL;
4877 Py_INCREF(type);
4878 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4879 for (i = 0; i < n; i++) {
4880 x = PyTuple_GET_ITEM(args, i);
4881 Py_INCREF(x);
4882 PyTuple_SET_ITEM(newargs, i+1, x);
4884 x = PyObject_Call(func, newargs, kwds);
4885 Py_DECREF(newargs);
4886 Py_DECREF(func);
4887 return x;
4890 static void
4891 slot_tp_del(PyObject *self)
4893 static PyObject *del_str = NULL;
4894 PyObject *del, *res;
4895 PyObject *error_type, *error_value, *error_traceback;
4897 /* Temporarily resurrect the object. */
4898 assert(self->ob_refcnt == 0);
4899 self->ob_refcnt = 1;
4901 /* Save the current exception, if any. */
4902 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4904 /* Execute __del__ method, if any. */
4905 del = lookup_maybe(self, "__del__", &del_str);
4906 if (del != NULL) {
4907 res = PyEval_CallObject(del, NULL);
4908 if (res == NULL)
4909 PyErr_WriteUnraisable(del);
4910 else
4911 Py_DECREF(res);
4912 Py_DECREF(del);
4915 /* Restore the saved exception. */
4916 PyErr_Restore(error_type, error_value, error_traceback);
4918 /* Undo the temporary resurrection; can't use DECREF here, it would
4919 * cause a recursive call.
4921 assert(self->ob_refcnt > 0);
4922 if (--self->ob_refcnt == 0)
4923 return; /* this is the normal path out */
4925 /* __del__ resurrected it! Make it look like the original Py_DECREF
4926 * never happened.
4929 Py_ssize_t refcnt = self->ob_refcnt;
4930 _Py_NewReference(self);
4931 self->ob_refcnt = refcnt;
4933 assert(!PyType_IS_GC(self->ob_type) ||
4934 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4935 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4936 * we need to undo that. */
4937 _Py_DEC_REFTOTAL;
4938 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4939 * chain, so no more to do there.
4940 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4941 * _Py_NewReference bumped tp_allocs: both of those need to be
4942 * undone.
4944 #ifdef COUNT_ALLOCS
4945 --self->ob_type->tp_frees;
4946 --self->ob_type->tp_allocs;
4947 #endif
4951 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4952 functions. The offsets here are relative to the 'PyHeapTypeObject'
4953 structure, which incorporates the additional structures used for numbers,
4954 sequences and mappings.
4955 Note that multiple names may map to the same slot (e.g. __eq__,
4956 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4957 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4958 terminated with an all-zero entry. (This table is further initialized and
4959 sorted in init_slotdefs() below.) */
4961 typedef struct wrapperbase slotdef;
4963 #undef TPSLOT
4964 #undef FLSLOT
4965 #undef ETSLOT
4966 #undef SQSLOT
4967 #undef MPSLOT
4968 #undef NBSLOT
4969 #undef UNSLOT
4970 #undef IBSLOT
4971 #undef BINSLOT
4972 #undef RBINSLOT
4974 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4975 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4976 PyDoc_STR(DOC)}
4977 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4978 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4979 PyDoc_STR(DOC), FLAGS}
4980 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4981 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4982 PyDoc_STR(DOC)}
4983 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4984 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4985 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4986 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4987 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4988 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4989 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4990 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4991 "x." NAME "() <==> " DOC)
4992 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4993 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4994 "x." NAME "(y) <==> x" DOC "y")
4995 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4996 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4997 "x." NAME "(y) <==> x" DOC "y")
4998 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4999 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5000 "x." NAME "(y) <==> y" DOC "x")
5001 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5002 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5003 "x." NAME "(y) <==> " DOC)
5004 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5005 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5006 "x." NAME "(y) <==> " DOC)
5008 static slotdef slotdefs[] = {
5009 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
5010 "x.__len__() <==> len(x)"),
5011 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5012 The logic in abstract.c always falls back to nb_add/nb_multiply in
5013 this case. Defining both the nb_* and the sq_* slots to call the
5014 user-defined methods has unexpected side-effects, as shown by
5015 test_descr.notimplemented() */
5016 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
5017 "x.__add__(y) <==> x+y"),
5018 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
5019 "x.__mul__(n) <==> x*n"),
5020 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
5021 "x.__rmul__(n) <==> n*x"),
5022 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5023 "x.__getitem__(y) <==> x[y]"),
5024 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
5025 "x.__getslice__(i, j) <==> x[i:j]\n\
5027 Use of negative indices is not supported."),
5028 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
5029 "x.__setitem__(i, y) <==> x[i]=y"),
5030 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
5031 "x.__delitem__(y) <==> del x[y]"),
5032 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
5033 wrap_ssizessizeobjargproc,
5034 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
5036 Use of negative indices is not supported."),
5037 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
5038 "x.__delslice__(i, j) <==> del x[i:j]\n\
5040 Use of negative indices is not supported."),
5041 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5042 "x.__contains__(y) <==> y in x"),
5043 SQSLOT("__iadd__", sq_inplace_concat, NULL,
5044 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
5045 SQSLOT("__imul__", sq_inplace_repeat, NULL,
5046 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
5048 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
5049 "x.__len__() <==> len(x)"),
5050 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
5051 wrap_binaryfunc,
5052 "x.__getitem__(y) <==> x[y]"),
5053 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
5054 wrap_objobjargproc,
5055 "x.__setitem__(i, y) <==> x[i]=y"),
5056 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
5057 wrap_delitem,
5058 "x.__delitem__(y) <==> del x[y]"),
5060 BINSLOT("__add__", nb_add, slot_nb_add,
5061 "+"),
5062 RBINSLOT("__radd__", nb_add, slot_nb_add,
5063 "+"),
5064 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5065 "-"),
5066 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5067 "-"),
5068 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5069 "*"),
5070 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5071 "*"),
5072 BINSLOT("__div__", nb_divide, slot_nb_divide,
5073 "/"),
5074 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
5075 "/"),
5076 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5077 "%"),
5078 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5079 "%"),
5080 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
5081 "divmod(x, y)"),
5082 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
5083 "divmod(y, x)"),
5084 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5085 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5086 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5087 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5088 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5089 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5090 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5091 "abs(x)"),
5092 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
5093 "x != 0"),
5094 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5095 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5096 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5097 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5098 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5099 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5100 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5101 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5102 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5103 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5104 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5105 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5106 "x.__coerce__(y) <==> coerce(x, y)"),
5107 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5108 "int(x)"),
5109 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5110 "long(x)"),
5111 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5112 "float(x)"),
5113 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5114 "oct(x)"),
5115 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5116 "hex(x)"),
5117 NBSLOT("__index__", nb_index, slot_nb_index, wrap_lenfunc,
5118 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5119 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5120 wrap_binaryfunc, "+"),
5121 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5122 wrap_binaryfunc, "-"),
5123 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5124 wrap_binaryfunc, "*"),
5125 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5126 wrap_binaryfunc, "/"),
5127 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5128 wrap_binaryfunc, "%"),
5129 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
5130 wrap_binaryfunc, "**"),
5131 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5132 wrap_binaryfunc, "<<"),
5133 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5134 wrap_binaryfunc, ">>"),
5135 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5136 wrap_binaryfunc, "&"),
5137 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5138 wrap_binaryfunc, "^"),
5139 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5140 wrap_binaryfunc, "|"),
5141 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5142 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5143 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5144 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5145 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5146 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5147 IBSLOT("__itruediv__", nb_inplace_true_divide,
5148 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
5150 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5151 "x.__str__() <==> str(x)"),
5152 TPSLOT("__str__", tp_print, NULL, NULL, ""),
5153 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5154 "x.__repr__() <==> repr(x)"),
5155 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5156 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5157 "x.__cmp__(y) <==> cmp(x,y)"),
5158 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5159 "x.__hash__() <==> hash(x)"),
5160 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5161 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5162 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5163 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5164 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5165 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5166 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5167 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5168 "x.__setattr__('name', value) <==> x.name = value"),
5169 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5170 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5171 "x.__delattr__('name') <==> del x.name"),
5172 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5173 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5174 "x.__lt__(y) <==> x<y"),
5175 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5176 "x.__le__(y) <==> x<=y"),
5177 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5178 "x.__eq__(y) <==> x==y"),
5179 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5180 "x.__ne__(y) <==> x!=y"),
5181 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5182 "x.__gt__(y) <==> x>y"),
5183 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5184 "x.__ge__(y) <==> x>=y"),
5185 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5186 "x.__iter__() <==> iter(x)"),
5187 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5188 "x.next() -> the next value, or raise StopIteration"),
5189 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5190 "descr.__get__(obj[, type]) -> value"),
5191 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5192 "descr.__set__(obj, value)"),
5193 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5194 wrap_descr_delete, "descr.__delete__(obj)"),
5195 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
5196 "x.__init__(...) initializes x; "
5197 "see x.__class__.__doc__ for signature",
5198 PyWrapperFlag_KEYWORDS),
5199 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
5200 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
5201 {NULL}
5204 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5205 pointer to the actual slot. This is not quite the same as simply adding
5206 the offset to the type pointer, since it takes care to indirect through the
5207 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5208 indirection pointer is NULL. */
5209 static void **
5210 slotptr(PyTypeObject *type, int ioffset)
5212 char *ptr;
5213 long offset = ioffset;
5215 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5216 assert(offset >= 0);
5217 assert(offset < offsetof(PyHeapTypeObject, as_buffer));
5218 if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5219 ptr = (void *)type->tp_as_sequence;
5220 offset -= offsetof(PyHeapTypeObject, as_sequence);
5222 else if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5223 ptr = (void *)type->tp_as_mapping;
5224 offset -= offsetof(PyHeapTypeObject, as_mapping);
5226 else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
5227 ptr = (void *)type->tp_as_number;
5228 offset -= offsetof(PyHeapTypeObject, as_number);
5230 else {
5231 ptr = (void *)type;
5233 if (ptr != NULL)
5234 ptr += offset;
5235 return (void **)ptr;
5238 /* Length of array of slotdef pointers used to store slots with the
5239 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5240 the same __name__, for any __name__. Since that's a static property, it is
5241 appropriate to declare fixed-size arrays for this. */
5242 #define MAX_EQUIV 10
5244 /* Return a slot pointer for a given name, but ONLY if the attribute has
5245 exactly one slot function. The name must be an interned string. */
5246 static void **
5247 resolve_slotdups(PyTypeObject *type, PyObject *name)
5249 /* XXX Maybe this could be optimized more -- but is it worth it? */
5251 /* pname and ptrs act as a little cache */
5252 static PyObject *pname;
5253 static slotdef *ptrs[MAX_EQUIV];
5254 slotdef *p, **pp;
5255 void **res, **ptr;
5257 if (pname != name) {
5258 /* Collect all slotdefs that match name into ptrs. */
5259 pname = name;
5260 pp = ptrs;
5261 for (p = slotdefs; p->name_strobj; p++) {
5262 if (p->name_strobj == name)
5263 *pp++ = p;
5265 *pp = NULL;
5268 /* Look in all matching slots of the type; if exactly one of these has
5269 a filled-in slot, return its value. Otherwise return NULL. */
5270 res = NULL;
5271 for (pp = ptrs; *pp; pp++) {
5272 ptr = slotptr(type, (*pp)->offset);
5273 if (ptr == NULL || *ptr == NULL)
5274 continue;
5275 if (res != NULL)
5276 return NULL;
5277 res = ptr;
5279 return res;
5282 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5283 does some incredibly complex thinking and then sticks something into the
5284 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5285 interests, and then stores a generic wrapper or a specific function into
5286 the slot.) Return a pointer to the next slotdef with a different offset,
5287 because that's convenient for fixup_slot_dispatchers(). */
5288 static slotdef *
5289 update_one_slot(PyTypeObject *type, slotdef *p)
5291 PyObject *descr;
5292 PyWrapperDescrObject *d;
5293 void *generic = NULL, *specific = NULL;
5294 int use_generic = 0;
5295 int offset = p->offset;
5296 void **ptr = slotptr(type, offset);
5298 if (ptr == NULL) {
5299 do {
5300 ++p;
5301 } while (p->offset == offset);
5302 return p;
5304 do {
5305 descr = _PyType_Lookup(type, p->name_strobj);
5306 if (descr == NULL)
5307 continue;
5308 if (descr->ob_type == &PyWrapperDescr_Type) {
5309 void **tptr = resolve_slotdups(type, p->name_strobj);
5310 if (tptr == NULL || tptr == ptr)
5311 generic = p->function;
5312 d = (PyWrapperDescrObject *)descr;
5313 if (d->d_base->wrapper == p->wrapper &&
5314 PyType_IsSubtype(type, d->d_type))
5316 if (specific == NULL ||
5317 specific == d->d_wrapped)
5318 specific = d->d_wrapped;
5319 else
5320 use_generic = 1;
5323 else if (descr->ob_type == &PyCFunction_Type &&
5324 PyCFunction_GET_FUNCTION(descr) ==
5325 (PyCFunction)tp_new_wrapper &&
5326 strcmp(p->name, "__new__") == 0)
5328 /* The __new__ wrapper is not a wrapper descriptor,
5329 so must be special-cased differently.
5330 If we don't do this, creating an instance will
5331 always use slot_tp_new which will look up
5332 __new__ in the MRO which will call tp_new_wrapper
5333 which will look through the base classes looking
5334 for a static base and call its tp_new (usually
5335 PyType_GenericNew), after performing various
5336 sanity checks and constructing a new argument
5337 list. Cut all that nonsense short -- this speeds
5338 up instance creation tremendously. */
5339 specific = (void *)type->tp_new;
5340 /* XXX I'm not 100% sure that there isn't a hole
5341 in this reasoning that requires additional
5342 sanity checks. I'll buy the first person to
5343 point out a bug in this reasoning a beer. */
5345 else {
5346 use_generic = 1;
5347 generic = p->function;
5349 } while ((++p)->offset == offset);
5350 if (specific && !use_generic)
5351 *ptr = specific;
5352 else
5353 *ptr = generic;
5354 return p;
5357 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5358 This is a callback for update_subclasses(). */
5359 static int
5360 update_slots_callback(PyTypeObject *type, void *data)
5362 slotdef **pp = (slotdef **)data;
5364 for (; *pp; pp++)
5365 update_one_slot(type, *pp);
5366 return 0;
5369 /* Comparison function for qsort() to compare slotdefs by their offset, and
5370 for equal offset by their address (to force a stable sort). */
5371 static int
5372 slotdef_cmp(const void *aa, const void *bb)
5374 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5375 int c = a->offset - b->offset;
5376 if (c != 0)
5377 return c;
5378 else
5379 /* Cannot use a-b, as this gives off_t,
5380 which may lose precision when converted to int. */
5381 return (a > b) ? 1 : (a < b) ? -1 : 0;
5384 /* Initialize the slotdefs table by adding interned string objects for the
5385 names and sorting the entries. */
5386 static void
5387 init_slotdefs(void)
5389 slotdef *p;
5390 static int initialized = 0;
5392 if (initialized)
5393 return;
5394 for (p = slotdefs; p->name; p++) {
5395 p->name_strobj = PyString_InternFromString(p->name);
5396 if (!p->name_strobj)
5397 Py_FatalError("Out of memory interning slotdef names");
5399 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5400 slotdef_cmp);
5401 initialized = 1;
5404 /* Update the slots after assignment to a class (type) attribute. */
5405 static int
5406 update_slot(PyTypeObject *type, PyObject *name)
5408 slotdef *ptrs[MAX_EQUIV];
5409 slotdef *p;
5410 slotdef **pp;
5411 int offset;
5413 init_slotdefs();
5414 pp = ptrs;
5415 for (p = slotdefs; p->name; p++) {
5416 /* XXX assume name is interned! */
5417 if (p->name_strobj == name)
5418 *pp++ = p;
5420 *pp = NULL;
5421 for (pp = ptrs; *pp; pp++) {
5422 p = *pp;
5423 offset = p->offset;
5424 while (p > slotdefs && (p-1)->offset == offset)
5425 --p;
5426 *pp = p;
5428 if (ptrs[0] == NULL)
5429 return 0; /* Not an attribute that affects any slots */
5430 return update_subclasses(type, name,
5431 update_slots_callback, (void *)ptrs);
5434 /* Store the proper functions in the slot dispatches at class (type)
5435 definition time, based upon which operations the class overrides in its
5436 dict. */
5437 static void
5438 fixup_slot_dispatchers(PyTypeObject *type)
5440 slotdef *p;
5442 init_slotdefs();
5443 for (p = slotdefs; p->name; )
5444 p = update_one_slot(type, p);
5447 static void
5448 update_all_slots(PyTypeObject* type)
5450 slotdef *p;
5452 init_slotdefs();
5453 for (p = slotdefs; p->name; p++) {
5454 /* update_slot returns int but can't actually fail */
5455 update_slot(type, p->name_strobj);
5459 /* recurse_down_subclasses() and update_subclasses() are mutually
5460 recursive functions to call a callback for all subclasses,
5461 but refraining from recursing into subclasses that define 'name'. */
5463 static int
5464 update_subclasses(PyTypeObject *type, PyObject *name,
5465 update_callback callback, void *data)
5467 if (callback(type, data) < 0)
5468 return -1;
5469 return recurse_down_subclasses(type, name, callback, data);
5472 static int
5473 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5474 update_callback callback, void *data)
5476 PyTypeObject *subclass;
5477 PyObject *ref, *subclasses, *dict;
5478 Py_ssize_t i, n;
5480 subclasses = type->tp_subclasses;
5481 if (subclasses == NULL)
5482 return 0;
5483 assert(PyList_Check(subclasses));
5484 n = PyList_GET_SIZE(subclasses);
5485 for (i = 0; i < n; i++) {
5486 ref = PyList_GET_ITEM(subclasses, i);
5487 assert(PyWeakref_CheckRef(ref));
5488 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5489 assert(subclass != NULL);
5490 if ((PyObject *)subclass == Py_None)
5491 continue;
5492 assert(PyType_Check(subclass));
5493 /* Avoid recursing down into unaffected classes */
5494 dict = subclass->tp_dict;
5495 if (dict != NULL && PyDict_Check(dict) &&
5496 PyDict_GetItem(dict, name) != NULL)
5497 continue;
5498 if (update_subclasses(subclass, name, callback, data) < 0)
5499 return -1;
5501 return 0;
5504 /* This function is called by PyType_Ready() to populate the type's
5505 dictionary with method descriptors for function slots. For each
5506 function slot (like tp_repr) that's defined in the type, one or more
5507 corresponding descriptors are added in the type's tp_dict dictionary
5508 under the appropriate name (like __repr__). Some function slots
5509 cause more than one descriptor to be added (for example, the nb_add
5510 slot adds both __add__ and __radd__ descriptors) and some function
5511 slots compete for the same descriptor (for example both sq_item and
5512 mp_subscript generate a __getitem__ descriptor).
5514 In the latter case, the first slotdef entry encoutered wins. Since
5515 slotdef entries are sorted by the offset of the slot in the
5516 PyHeapTypeObject, this gives us some control over disambiguating
5517 between competing slots: the members of PyHeapTypeObject are listed
5518 from most general to least general, so the most general slot is
5519 preferred. In particular, because as_mapping comes before as_sequence,
5520 for a type that defines both mp_subscript and sq_item, mp_subscript
5521 wins.
5523 This only adds new descriptors and doesn't overwrite entries in
5524 tp_dict that were previously defined. The descriptors contain a
5525 reference to the C function they must call, so that it's safe if they
5526 are copied into a subtype's __dict__ and the subtype has a different
5527 C function in its slot -- calling the method defined by the
5528 descriptor will call the C function that was used to create it,
5529 rather than the C function present in the slot when it is called.
5530 (This is important because a subtype may have a C function in the
5531 slot that calls the method from the dictionary, and we want to avoid
5532 infinite recursion here.) */
5534 static int
5535 add_operators(PyTypeObject *type)
5537 PyObject *dict = type->tp_dict;
5538 slotdef *p;
5539 PyObject *descr;
5540 void **ptr;
5542 init_slotdefs();
5543 for (p = slotdefs; p->name; p++) {
5544 if (p->wrapper == NULL)
5545 continue;
5546 ptr = slotptr(type, p->offset);
5547 if (!ptr || !*ptr)
5548 continue;
5549 if (PyDict_GetItem(dict, p->name_strobj))
5550 continue;
5551 descr = PyDescr_NewWrapper(type, p, *ptr);
5552 if (descr == NULL)
5553 return -1;
5554 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5555 return -1;
5556 Py_DECREF(descr);
5558 if (type->tp_new != NULL) {
5559 if (add_tp_new_wrapper(type) < 0)
5560 return -1;
5562 return 0;
5566 /* Cooperative 'super' */
5568 typedef struct {
5569 PyObject_HEAD
5570 PyTypeObject *type;
5571 PyObject *obj;
5572 PyTypeObject *obj_type;
5573 } superobject;
5575 static PyMemberDef super_members[] = {
5576 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5577 "the class invoking super()"},
5578 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5579 "the instance invoking super(); may be None"},
5580 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5581 "the type of the instance invoking super(); may be None"},
5585 static void
5586 super_dealloc(PyObject *self)
5588 superobject *su = (superobject *)self;
5590 _PyObject_GC_UNTRACK(self);
5591 Py_XDECREF(su->obj);
5592 Py_XDECREF(su->type);
5593 Py_XDECREF(su->obj_type);
5594 self->ob_type->tp_free(self);
5597 static PyObject *
5598 super_repr(PyObject *self)
5600 superobject *su = (superobject *)self;
5602 if (su->obj_type)
5603 return PyString_FromFormat(
5604 "<super: <class '%s'>, <%s object>>",
5605 su->type ? su->type->tp_name : "NULL",
5606 su->obj_type->tp_name);
5607 else
5608 return PyString_FromFormat(
5609 "<super: <class '%s'>, NULL>",
5610 su->type ? su->type->tp_name : "NULL");
5613 static PyObject *
5614 super_getattro(PyObject *self, PyObject *name)
5616 superobject *su = (superobject *)self;
5617 int skip = su->obj_type == NULL;
5619 if (!skip) {
5620 /* We want __class__ to return the class of the super object
5621 (i.e. super, or a subclass), not the class of su->obj. */
5622 skip = (PyString_Check(name) &&
5623 PyString_GET_SIZE(name) == 9 &&
5624 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5627 if (!skip) {
5628 PyObject *mro, *res, *tmp, *dict;
5629 PyTypeObject *starttype;
5630 descrgetfunc f;
5631 Py_ssize_t i, n;
5633 starttype = su->obj_type;
5634 mro = starttype->tp_mro;
5636 if (mro == NULL)
5637 n = 0;
5638 else {
5639 assert(PyTuple_Check(mro));
5640 n = PyTuple_GET_SIZE(mro);
5642 for (i = 0; i < n; i++) {
5643 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
5644 break;
5646 i++;
5647 res = NULL;
5648 for (; i < n; i++) {
5649 tmp = PyTuple_GET_ITEM(mro, i);
5650 if (PyType_Check(tmp))
5651 dict = ((PyTypeObject *)tmp)->tp_dict;
5652 else if (PyClass_Check(tmp))
5653 dict = ((PyClassObject *)tmp)->cl_dict;
5654 else
5655 continue;
5656 res = PyDict_GetItem(dict, name);
5657 if (res != NULL) {
5658 Py_INCREF(res);
5659 f = res->ob_type->tp_descr_get;
5660 if (f != NULL) {
5661 tmp = f(res,
5662 /* Only pass 'obj' param if
5663 this is instance-mode super
5664 (See SF ID #743627)
5666 (su->obj == (PyObject *)
5667 su->obj_type
5668 ? (PyObject *)NULL
5669 : su->obj),
5670 (PyObject *)starttype);
5671 Py_DECREF(res);
5672 res = tmp;
5674 return res;
5678 return PyObject_GenericGetAttr(self, name);
5681 static PyTypeObject *
5682 supercheck(PyTypeObject *type, PyObject *obj)
5684 /* Check that a super() call makes sense. Return a type object.
5686 obj can be a new-style class, or an instance of one:
5688 - If it is a class, it must be a subclass of 'type'. This case is
5689 used for class methods; the return value is obj.
5691 - If it is an instance, it must be an instance of 'type'. This is
5692 the normal case; the return value is obj.__class__.
5694 But... when obj is an instance, we want to allow for the case where
5695 obj->ob_type is not a subclass of type, but obj.__class__ is!
5696 This will allow using super() with a proxy for obj.
5699 /* Check for first bullet above (special case) */
5700 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5701 Py_INCREF(obj);
5702 return (PyTypeObject *)obj;
5705 /* Normal case */
5706 if (PyType_IsSubtype(obj->ob_type, type)) {
5707 Py_INCREF(obj->ob_type);
5708 return obj->ob_type;
5710 else {
5711 /* Try the slow way */
5712 static PyObject *class_str = NULL;
5713 PyObject *class_attr;
5715 if (class_str == NULL) {
5716 class_str = PyString_FromString("__class__");
5717 if (class_str == NULL)
5718 return NULL;
5721 class_attr = PyObject_GetAttr(obj, class_str);
5723 if (class_attr != NULL &&
5724 PyType_Check(class_attr) &&
5725 (PyTypeObject *)class_attr != obj->ob_type)
5727 int ok = PyType_IsSubtype(
5728 (PyTypeObject *)class_attr, type);
5729 if (ok)
5730 return (PyTypeObject *)class_attr;
5733 if (class_attr == NULL)
5734 PyErr_Clear();
5735 else
5736 Py_DECREF(class_attr);
5739 PyErr_SetString(PyExc_TypeError,
5740 "super(type, obj): "
5741 "obj must be an instance or subtype of type");
5742 return NULL;
5745 static PyObject *
5746 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5748 superobject *su = (superobject *)self;
5749 superobject *new;
5751 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5752 /* Not binding to an object, or already bound */
5753 Py_INCREF(self);
5754 return self;
5756 if (su->ob_type != &PySuper_Type)
5757 /* If su is an instance of a (strict) subclass of super,
5758 call its type */
5759 return PyObject_CallFunction((PyObject *)su->ob_type,
5760 "OO", su->type, obj);
5761 else {
5762 /* Inline the common case */
5763 PyTypeObject *obj_type = supercheck(su->type, obj);
5764 if (obj_type == NULL)
5765 return NULL;
5766 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5767 NULL, NULL);
5768 if (new == NULL)
5769 return NULL;
5770 Py_INCREF(su->type);
5771 Py_INCREF(obj);
5772 new->type = su->type;
5773 new->obj = obj;
5774 new->obj_type = obj_type;
5775 return (PyObject *)new;
5779 static int
5780 super_init(PyObject *self, PyObject *args, PyObject *kwds)
5782 superobject *su = (superobject *)self;
5783 PyTypeObject *type;
5784 PyObject *obj = NULL;
5785 PyTypeObject *obj_type = NULL;
5787 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5788 return -1;
5789 if (obj == Py_None)
5790 obj = NULL;
5791 if (obj != NULL) {
5792 obj_type = supercheck(type, obj);
5793 if (obj_type == NULL)
5794 return -1;
5795 Py_INCREF(obj);
5797 Py_INCREF(type);
5798 su->type = type;
5799 su->obj = obj;
5800 su->obj_type = obj_type;
5801 return 0;
5804 PyDoc_STRVAR(super_doc,
5805 "super(type) -> unbound super object\n"
5806 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5807 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5808 "Typical use to call a cooperative superclass method:\n"
5809 "class C(B):\n"
5810 " def meth(self, arg):\n"
5811 " super(C, self).meth(arg)");
5813 static int
5814 super_traverse(PyObject *self, visitproc visit, void *arg)
5816 superobject *su = (superobject *)self;
5817 int err;
5819 #define VISIT(SLOT) \
5820 if (SLOT) { \
5821 err = visit((PyObject *)(SLOT), arg); \
5822 if (err) \
5823 return err; \
5826 VISIT(su->obj);
5827 VISIT(su->type);
5828 VISIT(su->obj_type);
5830 #undef VISIT
5832 return 0;
5835 PyTypeObject PySuper_Type = {
5836 PyObject_HEAD_INIT(&PyType_Type)
5837 0, /* ob_size */
5838 "super", /* tp_name */
5839 sizeof(superobject), /* tp_basicsize */
5840 0, /* tp_itemsize */
5841 /* methods */
5842 super_dealloc, /* tp_dealloc */
5843 0, /* tp_print */
5844 0, /* tp_getattr */
5845 0, /* tp_setattr */
5846 0, /* tp_compare */
5847 super_repr, /* tp_repr */
5848 0, /* tp_as_number */
5849 0, /* tp_as_sequence */
5850 0, /* tp_as_mapping */
5851 0, /* tp_hash */
5852 0, /* tp_call */
5853 0, /* tp_str */
5854 super_getattro, /* tp_getattro */
5855 0, /* tp_setattro */
5856 0, /* tp_as_buffer */
5857 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5858 Py_TPFLAGS_BASETYPE, /* tp_flags */
5859 super_doc, /* tp_doc */
5860 super_traverse, /* tp_traverse */
5861 0, /* tp_clear */
5862 0, /* tp_richcompare */
5863 0, /* tp_weaklistoffset */
5864 0, /* tp_iter */
5865 0, /* tp_iternext */
5866 0, /* tp_methods */
5867 super_members, /* tp_members */
5868 0, /* tp_getset */
5869 0, /* tp_base */
5870 0, /* tp_dict */
5871 super_descr_get, /* tp_descr_get */
5872 0, /* tp_descr_set */
5873 0, /* tp_dictoffset */
5874 super_init, /* tp_init */
5875 PyType_GenericAlloc, /* tp_alloc */
5876 PyType_GenericNew, /* tp_new */
5877 PyObject_GC_Del, /* tp_free */