1 /* Type object implementation */
4 #include "structmember.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
},
22 type_name(PyTypeObject
*type
, void *context
)
26 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
27 PyHeapTypeObject
* et
= (PyHeapTypeObject
*)type
;
29 Py_INCREF(et
->ht_name
);
33 s
= strrchr(type
->tp_name
, '.');
38 return PyString_FromString(s
);
43 type_set_name(PyTypeObject
*type
, PyObject
*value
, void *context
)
47 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
48 PyErr_Format(PyExc_TypeError
,
49 "can't set %s.__name__", type
->tp_name
);
53 PyErr_Format(PyExc_TypeError
,
54 "can't delete %s.__name__", type
->tp_name
);
57 if (!PyString_Check(value
)) {
58 PyErr_Format(PyExc_TypeError
,
59 "can only assign string to %s.__name__, not '%s'",
60 type
->tp_name
, Py_Type(value
)->tp_name
);
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");
70 et
= (PyHeapTypeObject
*)type
;
74 Py_DECREF(et
->ht_name
);
77 type
->tp_name
= PyString_AS_STRING(value
);
83 type_module(PyTypeObject
*type
, void *context
)
88 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
89 mod
= PyDict_GetItemString(type
->tp_dict
, "__module__");
91 PyErr_Format(PyExc_AttributeError
, "__module__");
98 s
= strrchr(type
->tp_name
, '.');
100 return PyString_FromStringAndSize(
101 type
->tp_name
, (Py_ssize_t
)(s
- type
->tp_name
));
102 return PyString_FromString("__builtin__");
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
);
115 PyErr_Format(PyExc_TypeError
,
116 "can't delete %s.__module__", type
->tp_name
);
120 return PyDict_SetItemString(type
->tp_dict
, "__module__", value
);
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
);
144 mro_subclasses(PyTypeObject
*type
, PyObject
* temp
)
146 PyTypeObject
*subclass
;
147 PyObject
*ref
, *subclasses
, *old_mro
;
150 subclasses
= type
->tp_subclasses
;
151 if (subclasses
== NULL
)
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
)
162 assert(PyType_Check(subclass
));
163 old_mro
= subclass
->tp_mro
;
164 if (mro_internal(subclass
) < 0) {
165 subclass
->tp_mro
= old_mro
;
170 tuple
= PyTuple_Pack(2, subclass
, old_mro
);
174 if (PyList_Append(temp
, tuple
) < 0)
178 if (mro_subclasses(subclass
, temp
) < 0)
185 type_set_bases(PyTypeObject
*type
, PyObject
*value
, void *context
)
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
);
199 PyErr_Format(PyExc_TypeError
,
200 "can't delete %s.__bases__", type
->tp_name
);
203 if (!PyTuple_Check(value
)) {
204 PyErr_Format(PyExc_TypeError
,
205 "can only assign tuple to %s.__bases__, not %s",
206 type
->tp_name
, Py_Type(value
)->tp_name
);
209 if (PyTuple_GET_SIZE(value
) == 0) {
210 PyErr_Format(PyExc_TypeError
,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
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
)) {
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type
->tp_name
, Py_Type(ob
)->tp_name
);
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");
233 new_base
= best_base(value
);
239 if (!compatible_for_assignment(type
->tp_base
, new_base
, "__bases__"))
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) {
256 temp
= PyList_New(0);
260 r
= mro_subclasses(type
, temp
);
263 for (i
= 0; i
< PyList_Size(temp
); i
++) {
266 PyArg_UnpackTuple(PyList_GET_ITEM(temp
, i
),
267 "", 2, 2, &cls
, &mro
);
279 /* any base that was in __bases__ but now isn't, we
280 need to remove |type| from its tp_subclasses.
281 conversely, any class now in __bases__ that wasn't
282 needs to have |type| added to its subclasses. */
284 /* for now, sod that: just remove from all old_bases,
285 add to all new_bases */
287 for (i
= PyTuple_GET_SIZE(old_bases
) - 1; i
>= 0; i
--) {
288 ob
= PyTuple_GET_ITEM(old_bases
, i
);
289 if (PyType_Check(ob
)) {
291 (PyTypeObject
*)ob
, type
);
295 for (i
= PyTuple_GET_SIZE(value
) - 1; i
>= 0; i
--) {
296 ob
= PyTuple_GET_ITEM(value
, i
);
297 if (PyType_Check(ob
)) {
298 if (add_subclass((PyTypeObject
*)ob
, type
) < 0)
303 update_all_slots(type
);
305 Py_DECREF(old_bases
);
312 Py_DECREF(type
->tp_bases
);
313 Py_DECREF(type
->tp_base
);
314 if (type
->tp_mro
!= old_mro
) {
315 Py_DECREF(type
->tp_mro
);
318 type
->tp_bases
= old_bases
;
319 type
->tp_base
= old_base
;
320 type
->tp_mro
= old_mro
;
326 type_dict(PyTypeObject
*type
, void *context
)
328 if (type
->tp_dict
== NULL
) {
332 return PyDictProxy_New(type
->tp_dict
);
336 type_get_doc(PyTypeObject
*type
, void *context
)
339 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) && type
->tp_doc
!= NULL
)
340 return PyString_FromString(type
->tp_doc
);
341 result
= PyDict_GetItemString(type
->tp_dict
, "__doc__");
342 if (result
== NULL
) {
346 else if (Py_Type(result
)->tp_descr_get
) {
347 result
= Py_Type(result
)->tp_descr_get(result
, NULL
,
356 static PyGetSetDef type_getsets
[] = {
357 {"__name__", (getter
)type_name
, (setter
)type_set_name
, NULL
},
358 {"__bases__", (getter
)type_get_bases
, (setter
)type_set_bases
, NULL
},
359 {"__module__", (getter
)type_module
, (setter
)type_set_module
, NULL
},
360 {"__dict__", (getter
)type_dict
, NULL
, NULL
},
361 {"__doc__", (getter
)type_get_doc
, NULL
, NULL
},
366 type_compare(PyObject
*v
, PyObject
*w
)
368 /* This is called with type objects only. So we
369 can just compare the addresses. */
370 Py_uintptr_t vv
= (Py_uintptr_t
)v
;
371 Py_uintptr_t ww
= (Py_uintptr_t
)w
;
372 return (vv
< ww
) ? -1 : (vv
> ww
) ? 1 : 0;
376 type_repr(PyTypeObject
*type
)
378 PyObject
*mod
, *name
, *rtn
;
381 mod
= type_module(type
, NULL
);
384 else if (!PyString_Check(mod
)) {
388 name
= type_name(type
, NULL
);
392 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
397 if (mod
!= NULL
&& strcmp(PyString_AS_STRING(mod
), "__builtin__")) {
398 rtn
= PyString_FromFormat("<%s '%s.%s'>",
400 PyString_AS_STRING(mod
),
401 PyString_AS_STRING(name
));
404 rtn
= PyString_FromFormat("<%s '%s'>", kind
, type
->tp_name
);
412 type_call(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
416 if (type
->tp_new
== NULL
) {
417 PyErr_Format(PyExc_TypeError
,
418 "cannot create '%.100s' instances",
423 obj
= type
->tp_new(type
, args
, kwds
);
425 /* Ugly exception: when the call was type(something),
426 don't call tp_init on the result. */
427 if (type
== &PyType_Type
&&
428 PyTuple_Check(args
) && PyTuple_GET_SIZE(args
) == 1 &&
430 (PyDict_Check(kwds
) && PyDict_Size(kwds
) == 0)))
432 /* If the returned object is not an instance of type,
433 it won't be initialized. */
434 if (!PyType_IsSubtype(obj
->ob_type
, type
))
437 if (PyType_HasFeature(type
, Py_TPFLAGS_HAVE_CLASS
) &&
438 type
->tp_init
!= NULL
&&
439 type
->tp_init(obj
, args
, kwds
) < 0) {
448 PyType_GenericAlloc(PyTypeObject
*type
, Py_ssize_t nitems
)
451 const size_t size
= _PyObject_VAR_SIZE(type
, nitems
+1);
452 /* note that we need to add one, for the sentinel */
454 if (PyType_IS_GC(type
))
455 obj
= _PyObject_GC_Malloc(size
);
457 obj
= (PyObject
*)PyObject_MALLOC(size
);
460 return PyErr_NoMemory();
462 memset(obj
, '\0', size
);
464 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
467 if (type
->tp_itemsize
== 0)
468 PyObject_INIT(obj
, type
);
470 (void) PyObject_INIT_VAR((PyVarObject
*)obj
, type
, nitems
);
472 if (PyType_IS_GC(type
))
473 _PyObject_GC_TRACK(obj
);
478 PyType_GenericNew(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
480 return type
->tp_alloc(type
, 0);
483 /* Helpers for subtyping */
486 traverse_slots(PyTypeObject
*type
, PyObject
*self
, visitproc visit
, void *arg
)
492 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
493 for (i
= 0; i
< n
; i
++, mp
++) {
494 if (mp
->type
== T_OBJECT_EX
) {
495 char *addr
= (char *)self
+ mp
->offset
;
496 PyObject
*obj
= *(PyObject
**)addr
;
498 int err
= visit(obj
, arg
);
508 subtype_traverse(PyObject
*self
, visitproc visit
, void *arg
)
510 PyTypeObject
*type
, *base
;
511 traverseproc basetraverse
;
513 /* Find the nearest base with a different tp_traverse,
514 and traverse slots while we're at it */
515 type
= Py_Type(self
);
517 while ((basetraverse
= base
->tp_traverse
) == subtype_traverse
) {
519 int err
= traverse_slots(base
, self
, visit
, arg
);
523 base
= base
->tp_base
;
527 if (type
->tp_dictoffset
!= base
->tp_dictoffset
) {
528 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
529 if (dictptr
&& *dictptr
)
533 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
534 /* For a heaptype, the instances count as references
535 to the type. Traverse the type so the collector
536 can find cycles involving this link. */
540 return basetraverse(self
, visit
, arg
);
545 clear_slots(PyTypeObject
*type
, PyObject
*self
)
551 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
552 for (i
= 0; i
< n
; i
++, mp
++) {
553 if (mp
->type
== T_OBJECT_EX
&& !(mp
->flags
& READONLY
)) {
554 char *addr
= (char *)self
+ mp
->offset
;
555 PyObject
*obj
= *(PyObject
**)addr
;
557 *(PyObject
**)addr
= NULL
;
565 subtype_clear(PyObject
*self
)
567 PyTypeObject
*type
, *base
;
570 /* Find the nearest base with a different tp_clear
571 and clear slots while we're at it */
572 type
= Py_Type(self
);
574 while ((baseclear
= base
->tp_clear
) == subtype_clear
) {
576 clear_slots(base
, self
);
577 base
= base
->tp_base
;
581 /* There's no need to clear the instance dict (if any);
582 the collector will call its tp_clear handler. */
585 return baseclear(self
);
590 subtype_dealloc(PyObject
*self
)
592 PyTypeObject
*type
, *base
;
593 destructor basedealloc
;
595 /* Extract the type; we expect it to be a heap type */
596 type
= Py_Type(self
);
597 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
599 /* Test whether the type has GC exactly once */
601 if (!PyType_IS_GC(type
)) {
602 /* It's really rare to find a dynamic type that doesn't have
603 GC; it can only happen when deriving from 'object' and not
604 adding any slots or instance variables. This allows
605 certain simplifications: there's no need to call
606 clear_slots(), or DECREF the dict, or clear weakrefs. */
608 /* Maybe call finalizer; exit early if resurrected */
611 if (self
->ob_refcnt
> 0)
615 /* Find the nearest base with a different tp_dealloc */
617 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
618 assert(Py_Size(base
) == 0);
619 base
= base
->tp_base
;
623 /* Call the base tp_dealloc() */
627 /* Can't reference self beyond this point */
634 /* We get here only if the type has GC */
636 /* UnTrack and re-Track around the trashcan macro, alas */
637 /* See explanation at end of function for full disclosure */
638 PyObject_GC_UnTrack(self
);
639 ++_PyTrash_delete_nesting
;
640 Py_TRASHCAN_SAFE_BEGIN(self
);
641 --_PyTrash_delete_nesting
;
642 /* DO NOT restore GC tracking at this point. weakref callbacks
643 * (if any, and whether directly here or indirectly in something we
644 * call) may trigger GC, and if self is tracked at that point, it
645 * will look like trash to GC and GC will try to delete self again.
648 /* Find the nearest base with a different tp_dealloc */
650 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
651 base
= base
->tp_base
;
655 /* If we added a weaklist, we clear it. Do this *before* calling
656 the finalizer (__del__), clearing slots, or clearing the instance
659 if (type
->tp_weaklistoffset
&& !base
->tp_weaklistoffset
)
660 PyObject_ClearWeakRefs(self
);
662 /* Maybe call finalizer; exit early if resurrected */
664 _PyObject_GC_TRACK(self
);
666 if (self
->ob_refcnt
> 0)
667 goto endlabel
; /* resurrected */
669 _PyObject_GC_UNTRACK(self
);
670 /* New weakrefs could be created during the finalizer call.
671 If this occurs, clear them out without calling their
672 finalizers since they might rely on part of the object
673 being finalized that has already been destroyed. */
674 if (type
->tp_weaklistoffset
&& !base
->tp_weaklistoffset
) {
675 /* Modeled after GET_WEAKREFS_LISTPTR() */
676 PyWeakReference
**list
= (PyWeakReference
**) \
677 PyObject_GET_WEAKREFS_LISTPTR(self
);
679 _PyWeakref_ClearRef(*list
);
683 /* Clear slots up to the nearest base with a different tp_dealloc */
685 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
687 clear_slots(base
, self
);
688 base
= base
->tp_base
;
692 /* If we added a dict, DECREF it */
693 if (type
->tp_dictoffset
&& !base
->tp_dictoffset
) {
694 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
695 if (dictptr
!= NULL
) {
696 PyObject
*dict
= *dictptr
;
704 /* Call the base tp_dealloc(); first retrack self if
705 * basedealloc knows about gc.
707 if (PyType_IS_GC(base
))
708 _PyObject_GC_TRACK(self
);
712 /* Can't reference self beyond this point */
716 ++_PyTrash_delete_nesting
;
717 Py_TRASHCAN_SAFE_END(self
);
718 --_PyTrash_delete_nesting
;
720 /* Explanation of the weirdness around the trashcan macros:
722 Q. What do the trashcan macros do?
724 A. Read the comment titled "Trashcan mechanism" in object.h.
725 For one, this explains why there must be a call to GC-untrack
726 before the trashcan begin macro. Without understanding the
727 trashcan code, the answers to the following questions don't make
730 Q. Why do we GC-untrack before the trashcan and then immediately
731 GC-track again afterward?
733 A. In the case that the base class is GC-aware, the base class
734 probably GC-untracks the object. If it does that using the
735 UNTRACK macro, this will crash when the object is already
736 untracked. Because we don't know what the base class does, the
737 only safe thing is to make sure the object is tracked when we
738 call the base class dealloc. But... The trashcan begin macro
739 requires that the object is *untracked* before it is called. So
746 Q. Why did the last question say "immediately GC-track again"?
747 It's nowhere near immediately.
749 A. Because the code *used* to re-track immediately. Bad Idea.
750 self has a refcount of 0, and if gc ever gets its hands on it
751 (which can happen if any weakref callback gets invoked), it
752 looks like trash to gc too, and gc also tries to delete self
753 then. But we're already deleting self. Double dealloction is
756 Q. Why the bizarre (net-zero) manipulation of
757 _PyTrash_delete_nesting around the trashcan macros?
759 A. Some base classes (e.g. list) also use the trashcan mechanism.
760 The following scenario used to be possible:
762 - suppose the trashcan level is one below the trashcan limit
764 - subtype_dealloc() is called
766 - the trashcan limit is not yet reached, so the trashcan level
767 is incremented and the code between trashcan begin and end is
770 - this destroys much of the object's contents, including its
773 - basedealloc() is called; this is really list_dealloc(), or
774 some other type which also uses the trashcan macros
776 - the trashcan limit is now reached, so the object is put on the
777 trashcan's to-be-deleted-later list
779 - basedealloc() returns
781 - subtype_dealloc() decrefs the object's type
783 - subtype_dealloc() returns
785 - later, the trashcan code starts deleting the objects from its
786 to-be-deleted-later list
788 - subtype_dealloc() is called *AGAIN* for the same object
790 - at the very least (if the destroyed slots and __dict__ don't
791 cause problems) the object's type gets decref'ed a second
792 time, which is *BAD*!!!
794 The remedy is to make sure that if the code between trashcan
795 begin and end in subtype_dealloc() is called, the code between
796 trashcan begin and end in basedealloc() will also be called.
797 This is done by decrementing the level after passing into the
798 trashcan block, and incrementing it just before leaving the
801 But now it's possible that a chain of objects consisting solely
802 of objects whose deallocator is subtype_dealloc() will defeat
803 the trashcan mechanism completely: the decremented level means
804 that the effective level never reaches the limit. Therefore, we
805 *increment* the level *before* entering the trashcan block, and
806 matchingly decrement it after leaving. This means the trashcan
807 code will trigger a little early, but that's no big deal.
809 Q. Are there any live examples of code in need of all this
812 A. Yes. See SF bug 668433 for code that crashed (when Python was
813 compiled in debug mode) before the trashcan level manipulations
814 were added. For more discussion, see SF patches 581742, 575073
819 static PyTypeObject
*solid_base(PyTypeObject
*type
);
821 /* type test with subclassing support */
824 PyType_IsSubtype(PyTypeObject
*a
, PyTypeObject
*b
)
828 if (!(a
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
))
829 return b
== a
|| b
== &PyBaseObject_Type
;
833 /* Deal with multiple inheritance without recursion
834 by walking the MRO tuple */
836 assert(PyTuple_Check(mro
));
837 n
= PyTuple_GET_SIZE(mro
);
838 for (i
= 0; i
< n
; i
++) {
839 if (PyTuple_GET_ITEM(mro
, i
) == (PyObject
*)b
)
845 /* a is not completely initilized yet; follow tp_base */
851 return b
== &PyBaseObject_Type
;
855 /* Internal routines to do a method lookup in the type
856 without looking in the instance dictionary
857 (so we can't use PyObject_GetAttr) but still binding
858 it to the instance. The arguments are the object,
859 the method name as a C string, and the address of a
860 static variable used to cache the interned Python string.
864 - lookup_maybe() returns NULL without raising an exception
865 when the _PyType_Lookup() call fails;
867 - lookup_method() always raises an exception upon errors.
871 lookup_maybe(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
875 if (*attrobj
== NULL
) {
876 *attrobj
= PyString_InternFromString(attrstr
);
877 if (*attrobj
== NULL
)
880 res
= _PyType_Lookup(Py_Type(self
), *attrobj
);
883 if ((f
= Py_Type(res
)->tp_descr_get
) == NULL
)
886 res
= f(res
, self
, (PyObject
*)(Py_Type(self
)));
892 lookup_method(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
894 PyObject
*res
= lookup_maybe(self
, attrstr
, attrobj
);
895 if (res
== NULL
&& !PyErr_Occurred())
896 PyErr_SetObject(PyExc_AttributeError
, *attrobj
);
900 /* A variation of PyObject_CallMethod that uses lookup_method()
901 instead of PyObject_GetAttrString(). This uses the same convention
902 as lookup_method to cache the interned name string object. */
905 call_method(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
908 PyObject
*args
, *func
= 0, *retval
;
909 va_start(va
, format
);
911 func
= lookup_maybe(o
, name
, nameobj
);
914 if (!PyErr_Occurred())
915 PyErr_SetObject(PyExc_AttributeError
, *nameobj
);
919 if (format
&& *format
)
920 args
= Py_VaBuildValue(format
, va
);
922 args
= PyTuple_New(0);
929 assert(PyTuple_Check(args
));
930 retval
= PyObject_Call(func
, args
, NULL
);
938 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
941 call_maybe(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
944 PyObject
*args
, *func
= 0, *retval
;
945 va_start(va
, format
);
947 func
= lookup_maybe(o
, name
, nameobj
);
950 if (!PyErr_Occurred()) {
951 Py_INCREF(Py_NotImplemented
);
952 return Py_NotImplemented
;
957 if (format
&& *format
)
958 args
= Py_VaBuildValue(format
, va
);
960 args
= PyTuple_New(0);
967 assert(PyTuple_Check(args
));
968 retval
= PyObject_Call(func
, args
, NULL
);
977 fill_classic_mro(PyObject
*mro
, PyObject
*cls
)
979 PyObject
*bases
, *base
;
982 assert(PyList_Check(mro
));
983 assert(PyClass_Check(cls
));
984 i
= PySequence_Contains(mro
, cls
);
988 if (PyList_Append(mro
, cls
) < 0)
991 bases
= ((PyClassObject
*)cls
)->cl_bases
;
992 assert(bases
&& PyTuple_Check(bases
));
993 n
= PyTuple_GET_SIZE(bases
);
994 for (i
= 0; i
< n
; i
++) {
995 base
= PyTuple_GET_ITEM(bases
, i
);
996 if (fill_classic_mro(mro
, base
) < 0)
1003 classic_mro(PyObject
*cls
)
1007 assert(PyClass_Check(cls
));
1008 mro
= PyList_New(0);
1010 if (fill_classic_mro(mro
, cls
) == 0)
1018 Method resolution order algorithm C3 described in
1019 "A Monotonic Superclass Linearization for Dylan",
1020 by Kim Barrett, Bob Cassel, Paul Haahr,
1021 David A. Moon, Keith Playford, and P. Tucker Withington.
1024 Some notes about the rules implied by C3:
1027 It isn't legal to repeat a class in a list of base classes.
1029 The next three properties are the 3 constraints in "C3".
1031 Local precendece order.
1032 If A precedes B in C's MRO, then A will precede B in the MRO of all
1036 The MRO of a class must be an extension without reordering of the
1037 MRO of each of its superclasses.
1039 Extended Precedence Graph (EPG).
1040 Linearization is consistent if there is a path in the EPG from
1041 each class to all its successors in the linearization. See
1042 the paper for definition of EPG.
1046 tail_contains(PyObject
*list
, int whence
, PyObject
*o
) {
1048 size
= PyList_GET_SIZE(list
);
1050 for (j
= whence
+1; j
< size
; j
++) {
1051 if (PyList_GET_ITEM(list
, j
) == o
)
1058 class_name(PyObject
*cls
)
1060 PyObject
*name
= PyObject_GetAttrString(cls
, "__name__");
1064 name
= PyObject_Repr(cls
);
1068 if (!PyString_Check(name
)) {
1076 check_duplicates(PyObject
*list
)
1079 /* Let's use a quadratic time algorithm,
1080 assuming that the bases lists is short.
1082 n
= PyList_GET_SIZE(list
);
1083 for (i
= 0; i
< n
; i
++) {
1084 PyObject
*o
= PyList_GET_ITEM(list
, i
);
1085 for (j
= i
+ 1; j
< n
; j
++) {
1086 if (PyList_GET_ITEM(list
, j
) == o
) {
1088 PyErr_Format(PyExc_TypeError
,
1089 "duplicate base class %s",
1090 o
? PyString_AS_STRING(o
) : "?");
1099 /* Raise a TypeError for an MRO order disagreement.
1101 It's hard to produce a good error message. In the absence of better
1102 insight into error reporting, report the classes that were candidates
1103 to be put next into the MRO. There is some conflict between the
1104 order in which they should be put in the MRO, but it's hard to
1105 diagnose what constraint can't be satisfied.
1109 set_mro_error(PyObject
*to_merge
, int *remain
)
1111 Py_ssize_t i
, n
, off
, to_merge_size
;
1114 PyObject
*set
= PyDict_New();
1117 to_merge_size
= PyList_GET_SIZE(to_merge
);
1118 for (i
= 0; i
< to_merge_size
; i
++) {
1119 PyObject
*L
= PyList_GET_ITEM(to_merge
, i
);
1120 if (remain
[i
] < PyList_GET_SIZE(L
)) {
1121 PyObject
*c
= PyList_GET_ITEM(L
, remain
[i
]);
1122 if (PyDict_SetItem(set
, c
, Py_None
) < 0) {
1128 n
= PyDict_Size(set
);
1130 off
= PyOS_snprintf(buf
, sizeof(buf
), "Cannot create a \
1131 consistent method resolution\norder (MRO) for bases");
1133 while (PyDict_Next(set
, &i
, &k
, &v
) && (size_t)off
< sizeof(buf
)) {
1134 PyObject
*name
= class_name(k
);
1135 off
+= PyOS_snprintf(buf
+ off
, sizeof(buf
) - off
, " %s",
1136 name
? PyString_AS_STRING(name
) : "?");
1138 if (--n
&& (size_t)(off
+1) < sizeof(buf
)) {
1143 PyErr_SetString(PyExc_TypeError
, buf
);
1148 pmerge(PyObject
*acc
, PyObject
* to_merge
) {
1149 Py_ssize_t i
, j
, to_merge_size
, empty_cnt
;
1153 to_merge_size
= PyList_GET_SIZE(to_merge
);
1155 /* remain stores an index into each sublist of to_merge.
1156 remain[i] is the index of the next base in to_merge[i]
1157 that is not included in acc.
1159 remain
= (int *)PyMem_MALLOC(SIZEOF_INT
*to_merge_size
);
1162 for (i
= 0; i
< to_merge_size
; i
++)
1167 for (i
= 0; i
< to_merge_size
; i
++) {
1168 PyObject
*candidate
;
1170 PyObject
*cur_list
= PyList_GET_ITEM(to_merge
, i
);
1172 if (remain
[i
] >= PyList_GET_SIZE(cur_list
)) {
1177 /* Choose next candidate for MRO.
1179 The input sequences alone can determine the choice.
1180 If not, choose the class which appears in the MRO
1181 of the earliest direct superclass of the new class.
1184 candidate
= PyList_GET_ITEM(cur_list
, remain
[i
]);
1185 for (j
= 0; j
< to_merge_size
; j
++) {
1186 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1187 if (tail_contains(j_lst
, remain
[j
], candidate
)) {
1188 goto skip
; /* continue outer loop */
1191 ok
= PyList_Append(acc
, candidate
);
1196 for (j
= 0; j
< to_merge_size
; j
++) {
1197 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1198 if (remain
[j
] < PyList_GET_SIZE(j_lst
) &&
1199 PyList_GET_ITEM(j_lst
, remain
[j
]) == candidate
) {
1207 if (empty_cnt
== to_merge_size
) {
1211 set_mro_error(to_merge
, remain
);
1217 mro_implementation(PyTypeObject
*type
)
1221 PyObject
*bases
, *result
;
1222 PyObject
*to_merge
, *bases_aslist
;
1224 if(type
->tp_dict
== NULL
) {
1225 if(PyType_Ready(type
) < 0)
1229 /* Find a superclass linearization that honors the constraints
1230 of the explicit lists of bases and the constraints implied by
1233 to_merge is a list of lists, where each list is a superclass
1234 linearization implied by a base class. The last element of
1235 to_merge is the declared list of bases.
1238 bases
= type
->tp_bases
;
1239 n
= PyTuple_GET_SIZE(bases
);
1241 to_merge
= PyList_New(n
+1);
1242 if (to_merge
== NULL
)
1245 for (i
= 0; i
< n
; i
++) {
1246 PyObject
*base
= PyTuple_GET_ITEM(bases
, i
);
1247 PyObject
*parentMRO
;
1248 if (PyType_Check(base
))
1249 parentMRO
= PySequence_List(
1250 ((PyTypeObject
*)base
)->tp_mro
);
1252 parentMRO
= classic_mro(base
);
1253 if (parentMRO
== NULL
) {
1254 Py_DECREF(to_merge
);
1258 PyList_SET_ITEM(to_merge
, i
, parentMRO
);
1261 bases_aslist
= PySequence_List(bases
);
1262 if (bases_aslist
== NULL
) {
1263 Py_DECREF(to_merge
);
1266 /* This is just a basic sanity check. */
1267 if (check_duplicates(bases_aslist
) < 0) {
1268 Py_DECREF(to_merge
);
1269 Py_DECREF(bases_aslist
);
1272 PyList_SET_ITEM(to_merge
, n
, bases_aslist
);
1274 result
= Py_BuildValue("[O]", (PyObject
*)type
);
1275 if (result
== NULL
) {
1276 Py_DECREF(to_merge
);
1280 ok
= pmerge(result
, to_merge
);
1281 Py_DECREF(to_merge
);
1291 mro_external(PyObject
*self
)
1293 PyTypeObject
*type
= (PyTypeObject
*)self
;
1295 return mro_implementation(type
);
1299 mro_internal(PyTypeObject
*type
)
1301 PyObject
*mro
, *result
, *tuple
;
1304 if (Py_Type(type
) == &PyType_Type
) {
1305 result
= mro_implementation(type
);
1308 static PyObject
*mro_str
;
1310 mro
= lookup_method((PyObject
*)type
, "mro", &mro_str
);
1313 result
= PyObject_CallObject(mro
, NULL
);
1318 tuple
= PySequence_Tuple(result
);
1325 PyTypeObject
*solid
;
1327 solid
= solid_base(type
);
1329 len
= PyTuple_GET_SIZE(tuple
);
1331 for (i
= 0; i
< len
; i
++) {
1333 cls
= PyTuple_GET_ITEM(tuple
, i
);
1334 if (PyClass_Check(cls
))
1336 else if (!PyType_Check(cls
)) {
1337 PyErr_Format(PyExc_TypeError
,
1338 "mro() returned a non-class ('%.500s')",
1339 Py_Type(cls
)->tp_name
);
1343 t
= (PyTypeObject
*)cls
;
1344 if (!PyType_IsSubtype(solid
, solid_base(t
))) {
1345 PyErr_Format(PyExc_TypeError
,
1346 "mro() returned base with unsuitable layout ('%.500s')",
1353 type
->tp_mro
= tuple
;
1358 /* Calculate the best base amongst multiple base classes.
1359 This is the first one that's on the path to the "solid base". */
1361 static PyTypeObject
*
1362 best_base(PyObject
*bases
)
1365 PyTypeObject
*base
, *winner
, *candidate
, *base_i
;
1366 PyObject
*base_proto
;
1368 assert(PyTuple_Check(bases
));
1369 n
= PyTuple_GET_SIZE(bases
);
1373 for (i
= 0; i
< n
; i
++) {
1374 base_proto
= PyTuple_GET_ITEM(bases
, i
);
1375 if (PyClass_Check(base_proto
))
1377 if (!PyType_Check(base_proto
)) {
1380 "bases must be types");
1383 base_i
= (PyTypeObject
*)base_proto
;
1384 if (base_i
->tp_dict
== NULL
) {
1385 if (PyType_Ready(base_i
) < 0)
1388 candidate
= solid_base(base_i
);
1389 if (winner
== NULL
) {
1393 else if (PyType_IsSubtype(winner
, candidate
))
1395 else if (PyType_IsSubtype(candidate
, winner
)) {
1402 "multiple bases have "
1403 "instance lay-out conflict");
1408 PyErr_SetString(PyExc_TypeError
,
1409 "a new-style class can't have only classic bases");
1414 extra_ivars(PyTypeObject
*type
, PyTypeObject
*base
)
1416 size_t t_size
= type
->tp_basicsize
;
1417 size_t b_size
= base
->tp_basicsize
;
1419 assert(t_size
>= b_size
); /* Else type smaller than base! */
1420 if (type
->tp_itemsize
|| base
->tp_itemsize
) {
1421 /* If itemsize is involved, stricter rules */
1422 return t_size
!= b_size
||
1423 type
->tp_itemsize
!= base
->tp_itemsize
;
1425 if (type
->tp_weaklistoffset
&& base
->tp_weaklistoffset
== 0 &&
1426 type
->tp_weaklistoffset
+ sizeof(PyObject
*) == t_size
&&
1427 type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
1428 t_size
-= sizeof(PyObject
*);
1429 if (type
->tp_dictoffset
&& base
->tp_dictoffset
== 0 &&
1430 type
->tp_dictoffset
+ sizeof(PyObject
*) == t_size
&&
1431 type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
1432 t_size
-= sizeof(PyObject
*);
1434 return t_size
!= b_size
;
1437 static PyTypeObject
*
1438 solid_base(PyTypeObject
*type
)
1443 base
= solid_base(type
->tp_base
);
1445 base
= &PyBaseObject_Type
;
1446 if (extra_ivars(type
, base
))
1452 static void object_dealloc(PyObject
*);
1453 static int object_init(PyObject
*, PyObject
*, PyObject
*);
1454 static int update_slot(PyTypeObject
*, PyObject
*);
1455 static void fixup_slot_dispatchers(PyTypeObject
*);
1458 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1459 * inherited from various builtin types. The builtin base usually provides
1460 * its own __dict__ descriptor, so we use that when we can.
1462 static PyTypeObject
*
1463 get_builtin_base_with_dict(PyTypeObject
*type
)
1465 while (type
->tp_base
!= NULL
) {
1466 if (type
->tp_dictoffset
!= 0 &&
1467 !(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
1469 type
= type
->tp_base
;
1475 get_dict_descriptor(PyTypeObject
*type
)
1477 static PyObject
*dict_str
;
1480 if (dict_str
== NULL
) {
1481 dict_str
= PyString_InternFromString("__dict__");
1482 if (dict_str
== NULL
)
1485 descr
= _PyType_Lookup(type
, dict_str
);
1486 if (descr
== NULL
|| !PyDescr_IsData(descr
))
1493 raise_dict_descr_error(PyObject
*obj
)
1495 PyErr_Format(PyExc_TypeError
,
1496 "this __dict__ descriptor does not support "
1497 "'%.200s' objects", obj
->ob_type
->tp_name
);
1501 subtype_dict(PyObject
*obj
, void *context
)
1507 base
= get_builtin_base_with_dict(obj
->ob_type
);
1510 PyObject
*descr
= get_dict_descriptor(base
);
1511 if (descr
== NULL
) {
1512 raise_dict_descr_error(obj
);
1515 func
= descr
->ob_type
->tp_descr_get
;
1517 raise_dict_descr_error(obj
);
1520 return func(descr
, obj
, (PyObject
*)(obj
->ob_type
));
1523 dictptr
= _PyObject_GetDictPtr(obj
);
1524 if (dictptr
== NULL
) {
1525 PyErr_SetString(PyExc_AttributeError
,
1526 "This object has no __dict__");
1531 *dictptr
= dict
= PyDict_New();
1537 subtype_setdict(PyObject
*obj
, PyObject
*value
, void *context
)
1543 base
= get_builtin_base_with_dict(obj
->ob_type
);
1546 PyObject
*descr
= get_dict_descriptor(base
);
1547 if (descr
== NULL
) {
1548 raise_dict_descr_error(obj
);
1551 func
= descr
->ob_type
->tp_descr_set
;
1553 raise_dict_descr_error(obj
);
1556 return func(descr
, obj
, value
);
1559 dictptr
= _PyObject_GetDictPtr(obj
);
1560 if (dictptr
== NULL
) {
1561 PyErr_SetString(PyExc_AttributeError
,
1562 "This object has no __dict__");
1565 if (value
!= NULL
&& !PyDict_Check(value
)) {
1566 PyErr_Format(PyExc_TypeError
,
1567 "__dict__ must be set to a dictionary, "
1568 "not a '%.200s'", Py_Type(value
)->tp_name
);
1579 subtype_getweakref(PyObject
*obj
, void *context
)
1581 PyObject
**weaklistptr
;
1584 if (Py_Type(obj
)->tp_weaklistoffset
== 0) {
1585 PyErr_SetString(PyExc_AttributeError
,
1586 "This object has no __weakref__");
1589 assert(Py_Type(obj
)->tp_weaklistoffset
> 0);
1590 assert(Py_Type(obj
)->tp_weaklistoffset
+ sizeof(PyObject
*) <=
1591 (size_t)(Py_Type(obj
)->tp_basicsize
));
1592 weaklistptr
= (PyObject
**)
1593 ((char *)obj
+ Py_Type(obj
)->tp_weaklistoffset
);
1594 if (*weaklistptr
== NULL
)
1597 result
= *weaklistptr
;
1602 /* Three variants on the subtype_getsets list. */
1604 static PyGetSetDef subtype_getsets_full
[] = {
1605 {"__dict__", subtype_dict
, subtype_setdict
,
1606 PyDoc_STR("dictionary for instance variables (if defined)")},
1607 {"__weakref__", subtype_getweakref
, NULL
,
1608 PyDoc_STR("list of weak references to the object (if defined)")},
1612 static PyGetSetDef subtype_getsets_dict_only
[] = {
1613 {"__dict__", subtype_dict
, subtype_setdict
,
1614 PyDoc_STR("dictionary for instance variables (if defined)")},
1618 static PyGetSetDef subtype_getsets_weakref_only
[] = {
1619 {"__weakref__", subtype_getweakref
, NULL
,
1620 PyDoc_STR("list of weak references to the object (if defined)")},
1625 valid_identifier(PyObject
*s
)
1630 if (!PyString_Check(s
)) {
1631 PyErr_Format(PyExc_TypeError
,
1632 "__slots__ items must be strings, not '%.200s'",
1633 Py_Type(s
)->tp_name
);
1636 p
= (unsigned char *) PyString_AS_STRING(s
);
1637 n
= PyString_GET_SIZE(s
);
1638 /* We must reject an empty name. As a hack, we bump the
1639 length to 1 so that the loop will balk on the trailing \0. */
1642 for (i
= 0; i
< n
; i
++, p
++) {
1643 if (!(i
== 0 ? isalpha(*p
) : isalnum(*p
)) && *p
!= '_') {
1644 PyErr_SetString(PyExc_TypeError
,
1645 "__slots__ must be identifiers");
1652 #ifdef Py_USING_UNICODE
1653 /* Replace Unicode objects in slots. */
1656 _unicode_to_string(PyObject
*slots
, Py_ssize_t nslots
)
1658 PyObject
*tmp
= NULL
;
1659 PyObject
*slot_name
, *new_name
;
1662 for (i
= 0; i
< nslots
; i
++) {
1663 if (PyUnicode_Check(slot_name
= PyTuple_GET_ITEM(slots
, i
))) {
1665 tmp
= PySequence_List(slots
);
1669 new_name
= _PyUnicode_AsDefaultEncodedString(slot_name
,
1671 if (new_name
== NULL
) {
1675 Py_INCREF(new_name
);
1676 PyList_SET_ITEM(tmp
, i
, new_name
);
1677 Py_DECREF(slot_name
);
1681 slots
= PyList_AsTuple(tmp
);
1690 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
);
1693 type_init(PyObject
*cls
, PyObject
*args
, PyObject
*kwds
)
1697 assert(args
!= NULL
&& PyTuple_Check(args
));
1698 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1700 if (kwds
!= NULL
&& PyDict_Check(kwds
) && PyDict_Size(kwds
) != 0) {
1701 PyErr_SetString(PyExc_TypeError
,
1702 "type.__init__() takes no keyword arguments");
1706 if (args
!= NULL
&& PyTuple_Check(args
) &&
1707 (PyTuple_GET_SIZE(args
) != 1 && PyTuple_GET_SIZE(args
) != 3)) {
1708 PyErr_SetString(PyExc_TypeError
,
1709 "type.__init__() takes 1 or 3 arguments");
1713 /* Call object.__init__(self) now. */
1714 /* XXX Could call super(type, cls).__init__() but what's the point? */
1715 args
= PyTuple_GetSlice(args
, 0, 0);
1716 res
= object_init(cls
, args
, NULL
);
1722 type_new(PyTypeObject
*metatype
, PyObject
*args
, PyObject
*kwds
)
1724 PyObject
*name
, *bases
, *dict
;
1725 static char *kwlist
[] = {"name", "bases", "dict", 0};
1726 PyObject
*slots
, *tmp
, *newslots
;
1727 PyTypeObject
*type
, *base
, *tmptype
, *winner
;
1728 PyHeapTypeObject
*et
;
1730 Py_ssize_t i
, nbases
, nslots
, slotoffset
, add_dict
, add_weak
;
1731 int j
, may_add_dict
, may_add_weak
;
1733 assert(args
!= NULL
&& PyTuple_Check(args
));
1734 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1736 /* Special case: type(x) should return x->ob_type */
1738 const Py_ssize_t nargs
= PyTuple_GET_SIZE(args
);
1739 const Py_ssize_t nkwds
= kwds
== NULL
? 0 : PyDict_Size(kwds
);
1741 if (PyType_CheckExact(metatype
) && nargs
== 1 && nkwds
== 0) {
1742 PyObject
*x
= PyTuple_GET_ITEM(args
, 0);
1743 Py_INCREF(Py_Type(x
));
1744 return (PyObject
*) Py_Type(x
);
1747 /* SF bug 475327 -- if that didn't trigger, we need 3
1748 arguments. but PyArg_ParseTupleAndKeywords below may give
1749 a msg saying type() needs exactly 3. */
1750 if (nargs
+ nkwds
!= 3) {
1751 PyErr_SetString(PyExc_TypeError
,
1752 "type() takes 1 or 3 arguments");
1757 /* Check arguments: (name, bases, dict) */
1758 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "SO!O!:type", kwlist
,
1760 &PyTuple_Type
, &bases
,
1761 &PyDict_Type
, &dict
))
1764 /* Determine the proper metatype to deal with this,
1765 and check for metatype conflicts while we're at it.
1766 Note that if some other metatype wins to contract,
1767 it's possible that its instances are not types. */
1768 nbases
= PyTuple_GET_SIZE(bases
);
1770 for (i
= 0; i
< nbases
; i
++) {
1771 tmp
= PyTuple_GET_ITEM(bases
, i
);
1772 tmptype
= tmp
->ob_type
;
1773 if (tmptype
== &PyClass_Type
)
1774 continue; /* Special case classic classes */
1775 if (PyType_IsSubtype(winner
, tmptype
))
1777 if (PyType_IsSubtype(tmptype
, winner
)) {
1781 PyErr_SetString(PyExc_TypeError
,
1782 "metaclass conflict: "
1783 "the metaclass of a derived class "
1784 "must be a (non-strict) subclass "
1785 "of the metaclasses of all its bases");
1788 if (winner
!= metatype
) {
1789 if (winner
->tp_new
!= type_new
) /* Pass it to the winner */
1790 return winner
->tp_new(winner
, args
, kwds
);
1794 /* Adjust for empty tuple bases */
1796 bases
= PyTuple_Pack(1, &PyBaseObject_Type
);
1804 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1806 /* Calculate best base, and check that all bases are type objects */
1807 base
= best_base(bases
);
1812 if (!PyType_HasFeature(base
, Py_TPFLAGS_BASETYPE
)) {
1813 PyErr_Format(PyExc_TypeError
,
1814 "type '%.100s' is not an acceptable base type",
1820 /* Check for a __slots__ sequence variable in dict, and count it */
1821 slots
= PyDict_GetItemString(dict
, "__slots__");
1825 may_add_dict
= base
->tp_dictoffset
== 0;
1826 may_add_weak
= base
->tp_weaklistoffset
== 0 && base
->tp_itemsize
== 0;
1827 if (slots
== NULL
) {
1838 /* Make it into a tuple */
1839 if (PyString_Check(slots
) || PyUnicode_Check(slots
))
1840 slots
= PyTuple_Pack(1, slots
);
1842 slots
= PySequence_Tuple(slots
);
1843 if (slots
== NULL
) {
1847 assert(PyTuple_Check(slots
));
1849 /* Are slots allowed? */
1850 nslots
= PyTuple_GET_SIZE(slots
);
1851 if (nslots
> 0 && base
->tp_itemsize
!= 0) {
1852 PyErr_Format(PyExc_TypeError
,
1853 "nonempty __slots__ "
1854 "not supported for subtype of '%s'",
1862 #ifdef Py_USING_UNICODE
1863 tmp
= _unicode_to_string(slots
, nslots
);
1871 /* Check for valid slot names and two special cases */
1872 for (i
= 0; i
< nslots
; i
++) {
1873 PyObject
*tmp
= PyTuple_GET_ITEM(slots
, i
);
1875 if (!valid_identifier(tmp
))
1877 assert(PyString_Check(tmp
));
1878 s
= PyString_AS_STRING(tmp
);
1879 if (strcmp(s
, "__dict__") == 0) {
1880 if (!may_add_dict
|| add_dict
) {
1881 PyErr_SetString(PyExc_TypeError
,
1882 "__dict__ slot disallowed: "
1883 "we already got one");
1888 if (strcmp(s
, "__weakref__") == 0) {
1889 if (!may_add_weak
|| add_weak
) {
1890 PyErr_SetString(PyExc_TypeError
,
1891 "__weakref__ slot disallowed: "
1892 "either we already got one, "
1893 "or __itemsize__ != 0");
1900 /* Copy slots into a list, mangle names and sort them.
1901 Sorted names are needed for __class__ assignment.
1902 Convert them back to tuple at the end.
1904 newslots
= PyList_New(nslots
- add_dict
- add_weak
);
1905 if (newslots
== NULL
)
1907 for (i
= j
= 0; i
< nslots
; i
++) {
1909 tmp
= PyTuple_GET_ITEM(slots
, i
);
1910 s
= PyString_AS_STRING(tmp
);
1911 if ((add_dict
&& strcmp(s
, "__dict__") == 0) ||
1912 (add_weak
&& strcmp(s
, "__weakref__") == 0))
1914 tmp
=_Py_Mangle(name
, tmp
);
1917 PyList_SET_ITEM(newslots
, j
, tmp
);
1920 assert(j
== nslots
- add_dict
- add_weak
);
1923 if (PyList_Sort(newslots
) == -1) {
1925 Py_DECREF(newslots
);
1928 slots
= PyList_AsTuple(newslots
);
1929 Py_DECREF(newslots
);
1930 if (slots
== NULL
) {
1935 /* Secondary bases may provide weakrefs or dict */
1937 ((may_add_dict
&& !add_dict
) ||
1938 (may_add_weak
&& !add_weak
))) {
1939 for (i
= 0; i
< nbases
; i
++) {
1940 tmp
= PyTuple_GET_ITEM(bases
, i
);
1941 if (tmp
== (PyObject
*)base
)
1942 continue; /* Skip primary base */
1943 if (PyClass_Check(tmp
)) {
1944 /* Classic base class provides both */
1945 if (may_add_dict
&& !add_dict
)
1947 if (may_add_weak
&& !add_weak
)
1951 assert(PyType_Check(tmp
));
1952 tmptype
= (PyTypeObject
*)tmp
;
1953 if (may_add_dict
&& !add_dict
&&
1954 tmptype
->tp_dictoffset
!= 0)
1956 if (may_add_weak
&& !add_weak
&&
1957 tmptype
->tp_weaklistoffset
!= 0)
1959 if (may_add_dict
&& !add_dict
)
1961 if (may_add_weak
&& !add_weak
)
1963 /* Nothing more to check */
1969 /* XXX From here until type is safely allocated,
1970 "return NULL" may leak slots! */
1972 /* Allocate the type object */
1973 type
= (PyTypeObject
*)metatype
->tp_alloc(metatype
, nslots
);
1980 /* Keep name and slots alive in the extended type object */
1981 et
= (PyHeapTypeObject
*)type
;
1984 et
->ht_slots
= slots
;
1986 /* Initialize tp_flags */
1987 type
->tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HEAPTYPE
|
1988 Py_TPFLAGS_BASETYPE
;
1989 if (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)
1990 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
1992 /* It's a new-style number unless it specifically inherits any
1993 old-style numeric behavior */
1994 if ((base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) ||
1995 (base
->tp_as_number
== NULL
))
1996 type
->tp_flags
|= Py_TPFLAGS_CHECKTYPES
;
1998 /* Initialize essential fields */
1999 type
->tp_as_number
= &et
->as_number
;
2000 type
->tp_as_sequence
= &et
->as_sequence
;
2001 type
->tp_as_mapping
= &et
->as_mapping
;
2002 type
->tp_as_buffer
= &et
->as_buffer
;
2003 type
->tp_name
= PyString_AS_STRING(name
);
2005 /* Set tp_base and tp_bases */
2006 type
->tp_bases
= bases
;
2008 type
->tp_base
= base
;
2010 /* Initialize tp_dict from passed-in dict */
2011 type
->tp_dict
= dict
= PyDict_Copy(dict
);
2017 /* Set __module__ in the dict */
2018 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
2019 tmp
= PyEval_GetGlobals();
2021 tmp
= PyDict_GetItemString(tmp
, "__name__");
2023 if (PyDict_SetItemString(dict
, "__module__",
2030 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2031 and is a string. The __doc__ accessor will first look for tp_doc;
2032 if that fails, it will still look into __dict__.
2035 PyObject
*doc
= PyDict_GetItemString(dict
, "__doc__");
2036 if (doc
!= NULL
&& PyString_Check(doc
)) {
2037 const size_t n
= (size_t)PyString_GET_SIZE(doc
);
2038 char *tp_doc
= (char *)PyObject_MALLOC(n
+1);
2039 if (tp_doc
== NULL
) {
2043 memcpy(tp_doc
, PyString_AS_STRING(doc
), n
+1);
2044 type
->tp_doc
= tp_doc
;
2048 /* Special-case __new__: if it's a plain function,
2049 make it a static function */
2050 tmp
= PyDict_GetItemString(dict
, "__new__");
2051 if (tmp
!= NULL
&& PyFunction_Check(tmp
)) {
2052 tmp
= PyStaticMethod_New(tmp
);
2057 PyDict_SetItemString(dict
, "__new__", tmp
);
2061 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2062 mp
= PyHeapType_GET_MEMBERS(et
);
2063 slotoffset
= base
->tp_basicsize
;
2064 if (slots
!= NULL
) {
2065 for (i
= 0; i
< nslots
; i
++, mp
++) {
2066 mp
->name
= PyString_AS_STRING(
2067 PyTuple_GET_ITEM(slots
, i
));
2068 mp
->type
= T_OBJECT_EX
;
2069 mp
->offset
= slotoffset
;
2071 /* __dict__ and __weakref__ are already filtered out */
2072 assert(strcmp(mp
->name
, "__dict__") != 0);
2073 assert(strcmp(mp
->name
, "__weakref__") != 0);
2075 slotoffset
+= sizeof(PyObject
*);
2079 if (base
->tp_itemsize
)
2080 type
->tp_dictoffset
= -(long)sizeof(PyObject
*);
2082 type
->tp_dictoffset
= slotoffset
;
2083 slotoffset
+= sizeof(PyObject
*);
2086 assert(!base
->tp_itemsize
);
2087 type
->tp_weaklistoffset
= slotoffset
;
2088 slotoffset
+= sizeof(PyObject
*);
2090 type
->tp_basicsize
= slotoffset
;
2091 type
->tp_itemsize
= base
->tp_itemsize
;
2092 type
->tp_members
= PyHeapType_GET_MEMBERS(et
);
2094 if (type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2095 type
->tp_getset
= subtype_getsets_full
;
2096 else if (type
->tp_weaklistoffset
&& !type
->tp_dictoffset
)
2097 type
->tp_getset
= subtype_getsets_weakref_only
;
2098 else if (!type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2099 type
->tp_getset
= subtype_getsets_dict_only
;
2101 type
->tp_getset
= NULL
;
2103 /* Special case some slots */
2104 if (type
->tp_dictoffset
!= 0 || nslots
> 0) {
2105 if (base
->tp_getattr
== NULL
&& base
->tp_getattro
== NULL
)
2106 type
->tp_getattro
= PyObject_GenericGetAttr
;
2107 if (base
->tp_setattr
== NULL
&& base
->tp_setattro
== NULL
)
2108 type
->tp_setattro
= PyObject_GenericSetAttr
;
2110 type
->tp_dealloc
= subtype_dealloc
;
2112 /* Enable GC unless there are really no instance variables possible */
2113 if (!(type
->tp_basicsize
== sizeof(PyObject
) &&
2114 type
->tp_itemsize
== 0))
2115 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
2117 /* Always override allocation strategy to use regular heap */
2118 type
->tp_alloc
= PyType_GenericAlloc
;
2119 if (type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) {
2120 type
->tp_free
= PyObject_GC_Del
;
2121 type
->tp_traverse
= subtype_traverse
;
2122 type
->tp_clear
= subtype_clear
;
2125 type
->tp_free
= PyObject_Del
;
2127 /* Initialize the rest */
2128 if (PyType_Ready(type
) < 0) {
2133 /* Put the proper slots in place */
2134 fixup_slot_dispatchers(type
);
2136 return (PyObject
*)type
;
2139 /* Internal API to look for a name through the MRO.
2140 This returns a borrowed reference, and doesn't set an exception! */
2142 _PyType_Lookup(PyTypeObject
*type
, PyObject
*name
)
2145 PyObject
*mro
, *res
, *base
, *dict
;
2147 /* Look in tp_dict of types in MRO */
2150 /* If mro is NULL, the type is either not yet initialized
2151 by PyType_Ready(), or already cleared by type_clear().
2152 Either way the safest thing to do is to return NULL. */
2156 assert(PyTuple_Check(mro
));
2157 n
= PyTuple_GET_SIZE(mro
);
2158 for (i
= 0; i
< n
; i
++) {
2159 base
= PyTuple_GET_ITEM(mro
, i
);
2160 if (PyClass_Check(base
))
2161 dict
= ((PyClassObject
*)base
)->cl_dict
;
2163 assert(PyType_Check(base
));
2164 dict
= ((PyTypeObject
*)base
)->tp_dict
;
2166 assert(dict
&& PyDict_Check(dict
));
2167 res
= PyDict_GetItem(dict
, name
);
2174 /* This is similar to PyObject_GenericGetAttr(),
2175 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2177 type_getattro(PyTypeObject
*type
, PyObject
*name
)
2179 PyTypeObject
*metatype
= Py_Type(type
);
2180 PyObject
*meta_attribute
, *attribute
;
2181 descrgetfunc meta_get
;
2183 /* Initialize this type (we'll assume the metatype is initialized) */
2184 if (type
->tp_dict
== NULL
) {
2185 if (PyType_Ready(type
) < 0)
2189 /* No readable descriptor found yet */
2192 /* Look for the attribute in the metatype */
2193 meta_attribute
= _PyType_Lookup(metatype
, name
);
2195 if (meta_attribute
!= NULL
) {
2196 meta_get
= Py_Type(meta_attribute
)->tp_descr_get
;
2198 if (meta_get
!= NULL
&& PyDescr_IsData(meta_attribute
)) {
2199 /* Data descriptors implement tp_descr_set to intercept
2200 * writes. Assume the attribute is not overridden in
2201 * type's tp_dict (and bases): call the descriptor now.
2203 return meta_get(meta_attribute
, (PyObject
*)type
,
2204 (PyObject
*)metatype
);
2206 Py_INCREF(meta_attribute
);
2209 /* No data descriptor found on metatype. Look in tp_dict of this
2210 * type and its bases */
2211 attribute
= _PyType_Lookup(type
, name
);
2212 if (attribute
!= NULL
) {
2213 /* Implement descriptor functionality, if any */
2214 descrgetfunc local_get
= Py_Type(attribute
)->tp_descr_get
;
2216 Py_XDECREF(meta_attribute
);
2218 if (local_get
!= NULL
) {
2219 /* NULL 2nd argument indicates the descriptor was
2220 * found on the target object itself (or a base) */
2221 return local_get(attribute
, (PyObject
*)NULL
,
2225 Py_INCREF(attribute
);
2229 /* No attribute found in local __dict__ (or bases): use the
2230 * descriptor from the metatype, if any */
2231 if (meta_get
!= NULL
) {
2233 res
= meta_get(meta_attribute
, (PyObject
*)type
,
2234 (PyObject
*)metatype
);
2235 Py_DECREF(meta_attribute
);
2239 /* If an ordinary attribute was found on the metatype, return it now */
2240 if (meta_attribute
!= NULL
) {
2241 return meta_attribute
;
2245 PyErr_Format(PyExc_AttributeError
,
2246 "type object '%.50s' has no attribute '%.400s'",
2247 type
->tp_name
, PyString_AS_STRING(name
));
2252 type_setattro(PyTypeObject
*type
, PyObject
*name
, PyObject
*value
)
2254 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
2257 "can't set attributes of built-in/extension type '%s'",
2261 /* XXX Example of how I expect this to be used...
2262 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2265 if (PyObject_GenericSetAttr((PyObject
*)type
, name
, value
) < 0)
2267 return update_slot(type
, name
);
2271 type_dealloc(PyTypeObject
*type
)
2273 PyHeapTypeObject
*et
;
2275 /* Assert this is a heap-allocated type object */
2276 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2277 _PyObject_GC_UNTRACK(type
);
2278 PyObject_ClearWeakRefs((PyObject
*)type
);
2279 et
= (PyHeapTypeObject
*)type
;
2280 Py_XDECREF(type
->tp_base
);
2281 Py_XDECREF(type
->tp_dict
);
2282 Py_XDECREF(type
->tp_bases
);
2283 Py_XDECREF(type
->tp_mro
);
2284 Py_XDECREF(type
->tp_cache
);
2285 Py_XDECREF(type
->tp_subclasses
);
2286 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2287 * of most other objects. It's okay to cast it to char *.
2289 PyObject_Free((char *)type
->tp_doc
);
2290 Py_XDECREF(et
->ht_name
);
2291 Py_XDECREF(et
->ht_slots
);
2292 Py_Type(type
)->tp_free((PyObject
*)type
);
2296 type_subclasses(PyTypeObject
*type
, PyObject
*args_ignored
)
2298 PyObject
*list
, *raw
, *ref
;
2301 list
= PyList_New(0);
2304 raw
= type
->tp_subclasses
;
2307 assert(PyList_Check(raw
));
2308 n
= PyList_GET_SIZE(raw
);
2309 for (i
= 0; i
< n
; i
++) {
2310 ref
= PyList_GET_ITEM(raw
, i
);
2311 assert(PyWeakref_CheckRef(ref
));
2312 ref
= PyWeakref_GET_OBJECT(ref
);
2313 if (ref
!= Py_None
) {
2314 if (PyList_Append(list
, ref
) < 0) {
2323 static PyMethodDef type_methods
[] = {
2324 {"mro", (PyCFunction
)mro_external
, METH_NOARGS
,
2325 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2326 {"__subclasses__", (PyCFunction
)type_subclasses
, METH_NOARGS
,
2327 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2331 PyDoc_STRVAR(type_doc
,
2332 "type(object) -> the object's type\n"
2333 "type(name, bases, dict) -> a new type");
2336 type_traverse(PyTypeObject
*type
, visitproc visit
, void *arg
)
2338 /* Because of type_is_gc(), the collector only calls this
2340 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2342 Py_VISIT(type
->tp_dict
);
2343 Py_VISIT(type
->tp_cache
);
2344 Py_VISIT(type
->tp_mro
);
2345 Py_VISIT(type
->tp_bases
);
2346 Py_VISIT(type
->tp_base
);
2348 /* There's no need to visit type->tp_subclasses or
2349 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2350 in cycles; tp_subclasses is a list of weak references,
2351 and slots is a tuple of strings. */
2357 type_clear(PyTypeObject
*type
)
2359 /* Because of type_is_gc(), the collector only calls this
2361 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2363 /* The only field we need to clear is tp_mro, which is part of a
2364 hard cycle (its first element is the class itself) that won't
2365 be broken otherwise (it's a tuple and tuples don't have a
2366 tp_clear handler). None of the other fields need to be
2367 cleared, and here's why:
2370 It is a dict, so the collector will call its tp_clear.
2373 Not used; if it were, it would be a dict.
2376 If these are involved in a cycle, there must be at least
2377 one other, mutable object in the cycle, e.g. a base
2378 class's dict; the cycle will be broken that way.
2381 A list of weak references can't be part of a cycle; and
2382 lists have their own tp_clear.
2384 slots (in PyHeapTypeObject):
2385 A tuple of strings can't be part of a cycle.
2388 Py_CLEAR(type
->tp_mro
);
2394 type_is_gc(PyTypeObject
*type
)
2396 return type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
;
2399 PyTypeObject PyType_Type
= {
2400 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2401 "type", /* tp_name */
2402 sizeof(PyHeapTypeObject
), /* tp_basicsize */
2403 sizeof(PyMemberDef
), /* tp_itemsize */
2404 (destructor
)type_dealloc
, /* tp_dealloc */
2408 type_compare
, /* tp_compare */
2409 (reprfunc
)type_repr
, /* tp_repr */
2410 0, /* tp_as_number */
2411 0, /* tp_as_sequence */
2412 0, /* tp_as_mapping */
2413 (hashfunc
)_Py_HashPointer
, /* tp_hash */
2414 (ternaryfunc
)type_call
, /* tp_call */
2416 (getattrofunc
)type_getattro
, /* tp_getattro */
2417 (setattrofunc
)type_setattro
, /* tp_setattro */
2418 0, /* tp_as_buffer */
2419 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2420 Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_TYPE_SUBCLASS
, /* tp_flags */
2421 type_doc
, /* tp_doc */
2422 (traverseproc
)type_traverse
, /* tp_traverse */
2423 (inquiry
)type_clear
, /* tp_clear */
2424 0, /* tp_richcompare */
2425 offsetof(PyTypeObject
, tp_weaklist
), /* tp_weaklistoffset */
2427 0, /* tp_iternext */
2428 type_methods
, /* tp_methods */
2429 type_members
, /* tp_members */
2430 type_getsets
, /* tp_getset */
2433 0, /* tp_descr_get */
2434 0, /* tp_descr_set */
2435 offsetof(PyTypeObject
, tp_dict
), /* tp_dictoffset */
2436 type_init
, /* tp_init */
2438 type_new
, /* tp_new */
2439 PyObject_GC_Del
, /* tp_free */
2440 (inquiry
)type_is_gc
, /* tp_is_gc */
2444 /* The base type of all types (eventually)... except itself. */
2446 /* You may wonder why object.__new__() only complains about arguments
2447 when object.__init__() is not overridden, and vice versa.
2449 Consider the use cases:
2451 1. When neither is overridden, we want to hear complaints about
2452 excess (i.e., any) arguments, since their presence could
2453 indicate there's a bug.
2455 2. When defining an Immutable type, we are likely to override only
2456 __new__(), since __init__() is called too late to initialize an
2457 Immutable object. Since __new__() defines the signature for the
2458 type, it would be a pain to have to override __init__() just to
2459 stop it from complaining about excess arguments.
2461 3. When defining a Mutable type, we are likely to override only
2462 __init__(). So here the converse reasoning applies: we don't
2463 want to have to override __new__() just to stop it from
2466 4. When __init__() is overridden, and the subclass __init__() calls
2467 object.__init__(), the latter should complain about excess
2468 arguments; ditto for __new__().
2470 Use cases 2 and 3 make it unattractive to unconditionally check for
2471 excess arguments. The best solution that addresses all four use
2472 cases is as follows: __init__() complains about excess arguments
2473 unless __new__() is overridden and __init__() is not overridden
2474 (IOW, if __init__() is overridden or __new__() is not overridden);
2475 symmetrically, __new__() complains about excess arguments unless
2476 __init__() is overridden and __new__() is not overridden
2477 (IOW, if __new__() is overridden or __init__() is not overridden).
2479 However, for backwards compatibility, this breaks too much code.
2480 Therefore, in 2.6, we'll *warn* about excess arguments when both
2481 methods are overridden; for all other cases we'll use the above
2488 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
2491 excess_args(PyObject
*args
, PyObject
*kwds
)
2493 return PyTuple_GET_SIZE(args
) ||
2494 (kwds
&& PyDict_Check(kwds
) && PyDict_Size(kwds
));
2498 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2501 if (excess_args(args
, kwds
)) {
2502 PyTypeObject
*type
= Py_Type(self
);
2503 if (type
->tp_init
!= object_init
&&
2504 type
->tp_new
!= object_new
)
2506 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2507 "object.__init__() takes no parameters",
2510 else if (type
->tp_init
!= object_init
||
2511 type
->tp_new
== object_new
)
2513 PyErr_SetString(PyExc_TypeError
,
2514 "object.__init__() takes no parameters");
2522 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2525 if (excess_args(args
, kwds
)) {
2526 if (type
->tp_new
!= object_new
&&
2527 type
->tp_init
!= object_init
)
2529 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2530 "object.__new__() takes no parameters",
2533 else if (type
->tp_new
!= object_new
||
2534 type
->tp_init
== object_init
)
2536 PyErr_SetString(PyExc_TypeError
,
2537 "object.__new__() takes no parameters");
2543 return type
->tp_alloc(type
, 0);
2547 object_dealloc(PyObject
*self
)
2549 Py_Type(self
)->tp_free(self
);
2553 object_repr(PyObject
*self
)
2556 PyObject
*mod
, *name
, *rtn
;
2558 type
= Py_Type(self
);
2559 mod
= type_module(type
, NULL
);
2562 else if (!PyString_Check(mod
)) {
2566 name
= type_name(type
, NULL
);
2569 if (mod
!= NULL
&& strcmp(PyString_AS_STRING(mod
), "__builtin__"))
2570 rtn
= PyString_FromFormat("<%s.%s object at %p>",
2571 PyString_AS_STRING(mod
),
2572 PyString_AS_STRING(name
),
2575 rtn
= PyString_FromFormat("<%s object at %p>",
2576 type
->tp_name
, self
);
2583 object_str(PyObject
*self
)
2587 f
= Py_Type(self
)->tp_repr
;
2594 object_hash(PyObject
*self
)
2596 return _Py_HashPointer(self
);
2600 object_get_class(PyObject
*self
, void *closure
)
2602 Py_INCREF(Py_Type(self
));
2603 return (PyObject
*)(Py_Type(self
));
2607 equiv_structs(PyTypeObject
*a
, PyTypeObject
*b
)
2612 a
->tp_basicsize
== b
->tp_basicsize
&&
2613 a
->tp_itemsize
== b
->tp_itemsize
&&
2614 a
->tp_dictoffset
== b
->tp_dictoffset
&&
2615 a
->tp_weaklistoffset
== b
->tp_weaklistoffset
&&
2616 ((a
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
2617 (b
->tp_flags
& Py_TPFLAGS_HAVE_GC
)));
2621 same_slots_added(PyTypeObject
*a
, PyTypeObject
*b
)
2623 PyTypeObject
*base
= a
->tp_base
;
2625 PyObject
*slots_a
, *slots_b
;
2627 if (base
!= b
->tp_base
)
2629 if (equiv_structs(a
, base
) && equiv_structs(b
, base
))
2631 size
= base
->tp_basicsize
;
2632 if (a
->tp_dictoffset
== size
&& b
->tp_dictoffset
== size
)
2633 size
+= sizeof(PyObject
*);
2634 if (a
->tp_weaklistoffset
== size
&& b
->tp_weaklistoffset
== size
)
2635 size
+= sizeof(PyObject
*);
2637 /* Check slots compliance */
2638 slots_a
= ((PyHeapTypeObject
*)a
)->ht_slots
;
2639 slots_b
= ((PyHeapTypeObject
*)b
)->ht_slots
;
2640 if (slots_a
&& slots_b
) {
2641 if (PyObject_Compare(slots_a
, slots_b
) != 0)
2643 size
+= sizeof(PyObject
*) * PyTuple_GET_SIZE(slots_a
);
2645 return size
== a
->tp_basicsize
&& size
== b
->tp_basicsize
;
2649 compatible_for_assignment(PyTypeObject
* oldto
, PyTypeObject
* newto
, char* attr
)
2651 PyTypeObject
*newbase
, *oldbase
;
2653 if (newto
->tp_dealloc
!= oldto
->tp_dealloc
||
2654 newto
->tp_free
!= oldto
->tp_free
)
2656 PyErr_Format(PyExc_TypeError
,
2658 "'%s' deallocator differs from '%s'",
2666 while (equiv_structs(newbase
, newbase
->tp_base
))
2667 newbase
= newbase
->tp_base
;
2668 while (equiv_structs(oldbase
, oldbase
->tp_base
))
2669 oldbase
= oldbase
->tp_base
;
2670 if (newbase
!= oldbase
&&
2671 (newbase
->tp_base
!= oldbase
->tp_base
||
2672 !same_slots_added(newbase
, oldbase
))) {
2673 PyErr_Format(PyExc_TypeError
,
2675 "'%s' object layout differs from '%s'",
2686 object_set_class(PyObject
*self
, PyObject
*value
, void *closure
)
2688 PyTypeObject
*oldto
= Py_Type(self
);
2689 PyTypeObject
*newto
;
2691 if (value
== NULL
) {
2692 PyErr_SetString(PyExc_TypeError
,
2693 "can't delete __class__ attribute");
2696 if (!PyType_Check(value
)) {
2697 PyErr_Format(PyExc_TypeError
,
2698 "__class__ must be set to new-style class, not '%s' object",
2699 Py_Type(value
)->tp_name
);
2702 newto
= (PyTypeObject
*)value
;
2703 if (!(newto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) ||
2704 !(oldto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
2706 PyErr_Format(PyExc_TypeError
,
2707 "__class__ assignment: only for heap types");
2710 if (compatible_for_assignment(newto
, oldto
, "__class__")) {
2712 Py_Type(self
) = newto
;
2721 static PyGetSetDef object_getsets
[] = {
2722 {"__class__", object_get_class
, object_set_class
,
2723 PyDoc_STR("the object's class")},
2728 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2729 We fall back to helpers in copy_reg for:
2730 - pickle protocols < 2
2731 - calculating the list of slot names (done only once per class)
2732 - the __newobj__ function (which is used as a token but never called)
2736 import_copy_reg(void)
2738 static PyObject
*copy_reg_str
;
2740 if (!copy_reg_str
) {
2741 copy_reg_str
= PyString_InternFromString("copy_reg");
2742 if (copy_reg_str
== NULL
)
2746 return PyImport_Import(copy_reg_str
);
2750 slotnames(PyObject
*cls
)
2754 PyObject
*slotnames
;
2756 if (!PyType_Check(cls
)) {
2761 clsdict
= ((PyTypeObject
*)cls
)->tp_dict
;
2762 slotnames
= PyDict_GetItemString(clsdict
, "__slotnames__");
2763 if (slotnames
!= NULL
&& PyList_Check(slotnames
)) {
2764 Py_INCREF(slotnames
);
2768 copy_reg
= import_copy_reg();
2769 if (copy_reg
== NULL
)
2772 slotnames
= PyObject_CallMethod(copy_reg
, "_slotnames", "O", cls
);
2773 Py_DECREF(copy_reg
);
2774 if (slotnames
!= NULL
&&
2775 slotnames
!= Py_None
&&
2776 !PyList_Check(slotnames
))
2778 PyErr_SetString(PyExc_TypeError
,
2779 "copy_reg._slotnames didn't return a list or None");
2780 Py_DECREF(slotnames
);
2788 reduce_2(PyObject
*obj
)
2790 PyObject
*cls
, *getnewargs
;
2791 PyObject
*args
= NULL
, *args2
= NULL
;
2792 PyObject
*getstate
= NULL
, *state
= NULL
, *names
= NULL
;
2793 PyObject
*slots
= NULL
, *listitems
= NULL
, *dictitems
= NULL
;
2794 PyObject
*copy_reg
= NULL
, *newobj
= NULL
, *res
= NULL
;
2797 cls
= PyObject_GetAttrString(obj
, "__class__");
2801 getnewargs
= PyObject_GetAttrString(obj
, "__getnewargs__");
2802 if (getnewargs
!= NULL
) {
2803 args
= PyObject_CallObject(getnewargs
, NULL
);
2804 Py_DECREF(getnewargs
);
2805 if (args
!= NULL
&& !PyTuple_Check(args
)) {
2806 PyErr_Format(PyExc_TypeError
,
2807 "__getnewargs__ should return a tuple, "
2808 "not '%.200s'", Py_Type(args
)->tp_name
);
2814 args
= PyTuple_New(0);
2819 getstate
= PyObject_GetAttrString(obj
, "__getstate__");
2820 if (getstate
!= NULL
) {
2821 state
= PyObject_CallObject(getstate
, NULL
);
2822 Py_DECREF(getstate
);
2828 state
= PyObject_GetAttrString(obj
, "__dict__");
2829 if (state
== NULL
) {
2834 names
= slotnames(cls
);
2837 if (names
!= Py_None
) {
2838 assert(PyList_Check(names
));
2839 slots
= PyDict_New();
2843 /* Can't pre-compute the list size; the list
2844 is stored on the class so accessible to other
2845 threads, which may be run by DECREF */
2846 for (i
= 0; i
< PyList_GET_SIZE(names
); i
++) {
2847 PyObject
*name
, *value
;
2848 name
= PyList_GET_ITEM(names
, i
);
2849 value
= PyObject_GetAttr(obj
, name
);
2853 int err
= PyDict_SetItem(slots
, name
,
2862 state
= Py_BuildValue("(NO)", state
, slots
);
2869 if (!PyList_Check(obj
)) {
2870 listitems
= Py_None
;
2871 Py_INCREF(listitems
);
2874 listitems
= PyObject_GetIter(obj
);
2875 if (listitems
== NULL
)
2879 if (!PyDict_Check(obj
)) {
2880 dictitems
= Py_None
;
2881 Py_INCREF(dictitems
);
2884 dictitems
= PyObject_CallMethod(obj
, "iteritems", "");
2885 if (dictitems
== NULL
)
2889 copy_reg
= import_copy_reg();
2890 if (copy_reg
== NULL
)
2892 newobj
= PyObject_GetAttrString(copy_reg
, "__newobj__");
2896 n
= PyTuple_GET_SIZE(args
);
2897 args2
= PyTuple_New(n
+1);
2900 PyTuple_SET_ITEM(args2
, 0, cls
);
2902 for (i
= 0; i
< n
; i
++) {
2903 PyObject
*v
= PyTuple_GET_ITEM(args
, i
);
2905 PyTuple_SET_ITEM(args2
, i
+1, v
);
2908 res
= PyTuple_Pack(5, newobj
, args2
, state
, listitems
, dictitems
);
2917 Py_XDECREF(listitems
);
2918 Py_XDECREF(dictitems
);
2919 Py_XDECREF(copy_reg
);
2925 * There were two problems when object.__reduce__ and object.__reduce_ex__
2926 * were implemented in the same function:
2927 * - trying to pickle an object with a custom __reduce__ method that
2928 * fell back to object.__reduce__ in certain circumstances led to
2929 * infinite recursion at Python level and eventual RuntimeError.
2930 * - Pickling objects that lied about their type by overwriting the
2931 * __class__ descriptor could lead to infinite recursion at C level
2932 * and eventual segfault.
2934 * Because of backwards compatibility, the two methods still have to
2935 * behave in the same way, even if this is not required by the pickle
2936 * protocol. This common functionality was moved to the _common_reduce
2940 _common_reduce(PyObject
*self
, int proto
)
2942 PyObject
*copy_reg
, *res
;
2945 return reduce_2(self
);
2947 copy_reg
= import_copy_reg();
2951 res
= PyEval_CallMethod(copy_reg
, "_reduce_ex", "(Oi)", self
, proto
);
2952 Py_DECREF(copy_reg
);
2958 object_reduce(PyObject
*self
, PyObject
*args
)
2962 if (!PyArg_ParseTuple(args
, "|i:__reduce__", &proto
))
2965 return _common_reduce(self
, proto
);
2969 object_reduce_ex(PyObject
*self
, PyObject
*args
)
2971 PyObject
*reduce
, *res
;
2974 if (!PyArg_ParseTuple(args
, "|i:__reduce_ex__", &proto
))
2977 reduce
= PyObject_GetAttrString(self
, "__reduce__");
2981 PyObject
*cls
, *clsreduce
, *objreduce
;
2983 cls
= PyObject_GetAttrString(self
, "__class__");
2988 clsreduce
= PyObject_GetAttrString(cls
, "__reduce__");
2990 if (clsreduce
== NULL
) {
2994 objreduce
= PyDict_GetItemString(PyBaseObject_Type
.tp_dict
,
2996 override
= (clsreduce
!= objreduce
);
2997 Py_DECREF(clsreduce
);
2999 res
= PyObject_CallObject(reduce
, NULL
);
3007 return _common_reduce(self
, proto
);
3010 static PyMethodDef object_methods
[] = {
3011 {"__reduce_ex__", object_reduce_ex
, METH_VARARGS
,
3012 PyDoc_STR("helper for pickle")},
3013 {"__reduce__", object_reduce
, METH_VARARGS
,
3014 PyDoc_STR("helper for pickle")},
3019 PyTypeObject PyBaseObject_Type
= {
3020 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
3021 "object", /* tp_name */
3022 sizeof(PyObject
), /* tp_basicsize */
3023 0, /* tp_itemsize */
3024 object_dealloc
, /* tp_dealloc */
3029 object_repr
, /* tp_repr */
3030 0, /* tp_as_number */
3031 0, /* tp_as_sequence */
3032 0, /* tp_as_mapping */
3033 object_hash
, /* tp_hash */
3035 object_str
, /* tp_str */
3036 PyObject_GenericGetAttr
, /* tp_getattro */
3037 PyObject_GenericSetAttr
, /* tp_setattro */
3038 0, /* tp_as_buffer */
3039 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
3040 PyDoc_STR("The most base type"), /* tp_doc */
3041 0, /* tp_traverse */
3043 0, /* tp_richcompare */
3044 0, /* tp_weaklistoffset */
3046 0, /* tp_iternext */
3047 object_methods
, /* tp_methods */
3049 object_getsets
, /* tp_getset */
3052 0, /* tp_descr_get */
3053 0, /* tp_descr_set */
3054 0, /* tp_dictoffset */
3055 object_init
, /* tp_init */
3056 PyType_GenericAlloc
, /* tp_alloc */
3057 object_new
, /* tp_new */
3058 PyObject_Del
, /* tp_free */
3062 /* Initialize the __dict__ in a type object */
3065 add_methods(PyTypeObject
*type
, PyMethodDef
*meth
)
3067 PyObject
*dict
= type
->tp_dict
;
3069 for (; meth
->ml_name
!= NULL
; meth
++) {
3071 if (PyDict_GetItemString(dict
, meth
->ml_name
) &&
3072 !(meth
->ml_flags
& METH_COEXIST
))
3074 if (meth
->ml_flags
& METH_CLASS
) {
3075 if (meth
->ml_flags
& METH_STATIC
) {
3076 PyErr_SetString(PyExc_ValueError
,
3077 "method cannot be both class and static");
3080 descr
= PyDescr_NewClassMethod(type
, meth
);
3082 else if (meth
->ml_flags
& METH_STATIC
) {
3083 PyObject
*cfunc
= PyCFunction_New(meth
, NULL
);
3086 descr
= PyStaticMethod_New(cfunc
);
3090 descr
= PyDescr_NewMethod(type
, meth
);
3094 if (PyDict_SetItemString(dict
, meth
->ml_name
, descr
) < 0)
3102 add_members(PyTypeObject
*type
, PyMemberDef
*memb
)
3104 PyObject
*dict
= type
->tp_dict
;
3106 for (; memb
->name
!= NULL
; memb
++) {
3108 if (PyDict_GetItemString(dict
, memb
->name
))
3110 descr
= PyDescr_NewMember(type
, memb
);
3113 if (PyDict_SetItemString(dict
, memb
->name
, descr
) < 0)
3121 add_getset(PyTypeObject
*type
, PyGetSetDef
*gsp
)
3123 PyObject
*dict
= type
->tp_dict
;
3125 for (; gsp
->name
!= NULL
; gsp
++) {
3127 if (PyDict_GetItemString(dict
, gsp
->name
))
3129 descr
= PyDescr_NewGetSet(type
, gsp
);
3133 if (PyDict_SetItemString(dict
, gsp
->name
, descr
) < 0)
3141 inherit_special(PyTypeObject
*type
, PyTypeObject
*base
)
3143 Py_ssize_t oldsize
, newsize
;
3145 /* Special flag magic */
3146 if (!type
->tp_as_buffer
&& base
->tp_as_buffer
) {
3147 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_GETCHARBUFFER
;
3149 base
->tp_flags
& Py_TPFLAGS_HAVE_GETCHARBUFFER
;
3151 if (!type
->tp_as_sequence
&& base
->tp_as_sequence
) {
3152 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_SEQUENCE_IN
;
3153 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_HAVE_SEQUENCE_IN
;
3155 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
) !=
3156 (base
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
)) {
3157 if ((!type
->tp_as_number
&& base
->tp_as_number
) ||
3158 (!type
->tp_as_sequence
&& base
->tp_as_sequence
)) {
3159 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_INPLACEOPS
;
3160 if (!type
->tp_as_number
&& !type
->tp_as_sequence
) {
3161 type
->tp_flags
|= base
->tp_flags
&
3162 Py_TPFLAGS_HAVE_INPLACEOPS
;
3167 if (!type
->tp_as_number
&& base
->tp_as_number
) {
3168 type
->tp_flags
&= ~Py_TPFLAGS_CHECKTYPES
;
3169 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
;
3172 /* Copying basicsize is connected to the GC flags */
3173 oldsize
= base
->tp_basicsize
;
3174 newsize
= type
->tp_basicsize
? type
->tp_basicsize
: oldsize
;
3175 if (!(type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3176 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3177 (type
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
/*GC slots exist*/) &&
3178 (!type
->tp_traverse
&& !type
->tp_clear
)) {
3179 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
3180 if (type
->tp_traverse
== NULL
)
3181 type
->tp_traverse
= base
->tp_traverse
;
3182 if (type
->tp_clear
== NULL
)
3183 type
->tp_clear
= base
->tp_clear
;
3185 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3186 /* The condition below could use some explanation.
3187 It appears that tp_new is not inherited for static types
3188 whose base class is 'object'; this seems to be a precaution
3189 so that old extension types don't suddenly become
3190 callable (object.__new__ wouldn't insure the invariants
3191 that the extension type's own factory function ensures).
3192 Heap types, of course, are under our control, so they do
3193 inherit tp_new; static extension types that specify some
3194 other built-in type as the default are considered
3195 new-style-aware so they also inherit object.__new__. */
3196 if (base
!= &PyBaseObject_Type
||
3197 (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
3198 if (type
->tp_new
== NULL
)
3199 type
->tp_new
= base
->tp_new
;
3202 type
->tp_basicsize
= newsize
;
3204 /* Copy other non-function slots */
3207 #define COPYVAL(SLOT) \
3208 if (type->SLOT == 0) type->SLOT = base->SLOT
3210 COPYVAL(tp_itemsize
);
3211 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_WEAKREFS
) {
3212 COPYVAL(tp_weaklistoffset
);
3214 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3215 COPYVAL(tp_dictoffset
);
3218 /* Setup fast subclass flags */
3219 if (PyType_IsSubtype(base
, (PyTypeObject
*)PyExc_BaseException
))
3220 type
->tp_flags
|= Py_TPFLAGS_BASE_EXC_SUBCLASS
;
3221 else if (PyType_IsSubtype(base
, &PyType_Type
))
3222 type
->tp_flags
|= Py_TPFLAGS_TYPE_SUBCLASS
;
3223 else if (PyType_IsSubtype(base
, &PyInt_Type
))
3224 type
->tp_flags
|= Py_TPFLAGS_INT_SUBCLASS
;
3225 else if (PyType_IsSubtype(base
, &PyLong_Type
))
3226 type
->tp_flags
|= Py_TPFLAGS_LONG_SUBCLASS
;
3227 else if (PyType_IsSubtype(base
, &PyString_Type
))
3228 type
->tp_flags
|= Py_TPFLAGS_STRING_SUBCLASS
;
3229 else if (PyType_IsSubtype(base
, &PyUnicode_Type
))
3230 type
->tp_flags
|= Py_TPFLAGS_UNICODE_SUBCLASS
;
3231 else if (PyType_IsSubtype(base
, &PyTuple_Type
))
3232 type
->tp_flags
|= Py_TPFLAGS_TUPLE_SUBCLASS
;
3233 else if (PyType_IsSubtype(base
, &PyList_Type
))
3234 type
->tp_flags
|= Py_TPFLAGS_LIST_SUBCLASS
;
3235 else if (PyType_IsSubtype(base
, &PyDict_Type
))
3236 type
->tp_flags
|= Py_TPFLAGS_DICT_SUBCLASS
;
3240 inherit_slots(PyTypeObject
*type
, PyTypeObject
*base
)
3242 PyTypeObject
*basebase
;
3251 #define SLOTDEFINED(SLOT) \
3252 (base->SLOT != 0 && \
3253 (basebase == NULL || base->SLOT != basebase->SLOT))
3255 #define COPYSLOT(SLOT) \
3256 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3258 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3259 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3260 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3261 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3263 /* This won't inherit indirect slots (from tp_as_number etc.)
3264 if type doesn't provide the space. */
3266 if (type
->tp_as_number
!= NULL
&& base
->tp_as_number
!= NULL
) {
3267 basebase
= base
->tp_base
;
3268 if (basebase
->tp_as_number
== NULL
)
3271 COPYNUM(nb_subtract
);
3272 COPYNUM(nb_multiply
);
3274 COPYNUM(nb_remainder
);
3277 COPYNUM(nb_negative
);
3278 COPYNUM(nb_positive
);
3279 COPYNUM(nb_absolute
);
3280 COPYNUM(nb_nonzero
);
3293 COPYNUM(nb_inplace_add
);
3294 COPYNUM(nb_inplace_subtract
);
3295 COPYNUM(nb_inplace_multiply
);
3296 COPYNUM(nb_inplace_divide
);
3297 COPYNUM(nb_inplace_remainder
);
3298 COPYNUM(nb_inplace_power
);
3299 COPYNUM(nb_inplace_lshift
);
3300 COPYNUM(nb_inplace_rshift
);
3301 COPYNUM(nb_inplace_and
);
3302 COPYNUM(nb_inplace_xor
);
3303 COPYNUM(nb_inplace_or
);
3304 if (base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) {
3305 COPYNUM(nb_true_divide
);
3306 COPYNUM(nb_floor_divide
);
3307 COPYNUM(nb_inplace_true_divide
);
3308 COPYNUM(nb_inplace_floor_divide
);
3310 if (base
->tp_flags
& Py_TPFLAGS_HAVE_INDEX
) {
3315 if (type
->tp_as_sequence
!= NULL
&& base
->tp_as_sequence
!= NULL
) {
3316 basebase
= base
->tp_base
;
3317 if (basebase
->tp_as_sequence
== NULL
)
3324 COPYSEQ(sq_ass_item
);
3325 COPYSEQ(sq_ass_slice
);
3326 COPYSEQ(sq_contains
);
3327 COPYSEQ(sq_inplace_concat
);
3328 COPYSEQ(sq_inplace_repeat
);
3331 if (type
->tp_as_mapping
!= NULL
&& base
->tp_as_mapping
!= NULL
) {
3332 basebase
= base
->tp_base
;
3333 if (basebase
->tp_as_mapping
== NULL
)
3336 COPYMAP(mp_subscript
);
3337 COPYMAP(mp_ass_subscript
);
3340 if (type
->tp_as_buffer
!= NULL
&& base
->tp_as_buffer
!= NULL
) {
3341 basebase
= base
->tp_base
;
3342 if (basebase
->tp_as_buffer
== NULL
)
3344 COPYBUF(bf_getreadbuffer
);
3345 COPYBUF(bf_getwritebuffer
);
3346 COPYBUF(bf_getsegcount
);
3347 COPYBUF(bf_getcharbuffer
);
3350 basebase
= base
->tp_base
;
3352 COPYSLOT(tp_dealloc
);
3354 if (type
->tp_getattr
== NULL
&& type
->tp_getattro
== NULL
) {
3355 type
->tp_getattr
= base
->tp_getattr
;
3356 type
->tp_getattro
= base
->tp_getattro
;
3358 if (type
->tp_setattr
== NULL
&& type
->tp_setattro
== NULL
) {
3359 type
->tp_setattr
= base
->tp_setattr
;
3360 type
->tp_setattro
= base
->tp_setattro
;
3362 /* tp_compare see tp_richcompare */
3364 /* tp_hash see tp_richcompare */
3367 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
) {
3368 if (type
->tp_compare
== NULL
&&
3369 type
->tp_richcompare
== NULL
&&
3370 type
->tp_hash
== NULL
)
3372 type
->tp_compare
= base
->tp_compare
;
3373 type
->tp_richcompare
= base
->tp_richcompare
;
3374 type
->tp_hash
= base
->tp_hash
;
3378 COPYSLOT(tp_compare
);
3380 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_ITER
) {
3382 COPYSLOT(tp_iternext
);
3384 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3385 COPYSLOT(tp_descr_get
);
3386 COPYSLOT(tp_descr_set
);
3387 COPYSLOT(tp_dictoffset
);
3391 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
3392 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)) {
3393 /* They agree about gc. */
3396 else if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3397 type
->tp_free
== NULL
&&
3398 base
->tp_free
== _PyObject_Del
) {
3399 /* A bit of magic to plug in the correct default
3400 * tp_free function when a derived class adds gc,
3401 * didn't define tp_free, and the base uses the
3402 * default non-gc tp_free.
3404 type
->tp_free
= PyObject_GC_Del
;
3406 /* else they didn't agree about gc, and there isn't something
3407 * obvious to be done -- the type is on its own.
3412 static int add_operators(PyTypeObject
*);
3415 PyType_Ready(PyTypeObject
*type
)
3417 PyObject
*dict
, *bases
;
3421 if (type
->tp_flags
& Py_TPFLAGS_READY
) {
3422 assert(type
->tp_dict
!= NULL
);
3425 assert((type
->tp_flags
& Py_TPFLAGS_READYING
) == 0);
3427 type
->tp_flags
|= Py_TPFLAGS_READYING
;
3429 #ifdef Py_TRACE_REFS
3430 /* PyType_Ready is the closest thing we have to a choke point
3431 * for type objects, so is the best place I can think of to try
3432 * to get type objects into the doubly-linked list of all objects.
3433 * Still, not all type objects go thru PyType_Ready.
3435 _Py_AddToAllObjects((PyObject
*)type
, 0);
3438 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3439 base
= type
->tp_base
;
3440 if (base
== NULL
&& type
!= &PyBaseObject_Type
) {
3441 base
= type
->tp_base
= &PyBaseObject_Type
;
3445 /* Now the only way base can still be NULL is if type is
3446 * &PyBaseObject_Type.
3449 /* Initialize the base class */
3450 if (base
&& base
->tp_dict
== NULL
) {
3451 if (PyType_Ready(base
) < 0)
3455 /* Initialize ob_type if NULL. This means extensions that want to be
3456 compilable separately on Windows can call PyType_Ready() instead of
3457 initializing the ob_type field of their type objects. */
3458 /* The test for base != NULL is really unnecessary, since base is only
3459 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3460 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3462 if (Py_Type(type
) == NULL
&& base
!= NULL
)
3463 Py_Type(type
) = Py_Type(base
);
3465 /* Initialize tp_bases */
3466 bases
= type
->tp_bases
;
3467 if (bases
== NULL
) {
3469 bases
= PyTuple_New(0);
3471 bases
= PyTuple_Pack(1, base
);
3474 type
->tp_bases
= bases
;
3477 /* Initialize tp_dict */
3478 dict
= type
->tp_dict
;
3480 dict
= PyDict_New();
3483 type
->tp_dict
= dict
;
3486 /* Add type-specific descriptors to tp_dict */
3487 if (add_operators(type
) < 0)
3489 if (type
->tp_methods
!= NULL
) {
3490 if (add_methods(type
, type
->tp_methods
) < 0)
3493 if (type
->tp_members
!= NULL
) {
3494 if (add_members(type
, type
->tp_members
) < 0)
3497 if (type
->tp_getset
!= NULL
) {
3498 if (add_getset(type
, type
->tp_getset
) < 0)
3502 /* Calculate method resolution order */
3503 if (mro_internal(type
) < 0) {
3507 /* Inherit special flags from dominant base */
3508 if (type
->tp_base
!= NULL
)
3509 inherit_special(type
, type
->tp_base
);
3511 /* Initialize tp_dict properly */
3512 bases
= type
->tp_mro
;
3513 assert(bases
!= NULL
);
3514 assert(PyTuple_Check(bases
));
3515 n
= PyTuple_GET_SIZE(bases
);
3516 for (i
= 1; i
< n
; i
++) {
3517 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3518 if (PyType_Check(b
))
3519 inherit_slots(type
, (PyTypeObject
*)b
);
3522 /* Sanity check for tp_free. */
3523 if (PyType_IS_GC(type
) && (type
->tp_flags
& Py_TPFLAGS_BASETYPE
) &&
3524 (type
->tp_free
== NULL
|| type
->tp_free
== PyObject_Del
)) {
3525 /* This base class needs to call tp_free, but doesn't have
3526 * one, or its tp_free is for non-gc'ed objects.
3528 PyErr_Format(PyExc_TypeError
, "type '%.100s' participates in "
3529 "gc and is a base type but has inappropriate "
3535 /* if the type dictionary doesn't contain a __doc__, set it from
3538 if (PyDict_GetItemString(type
->tp_dict
, "__doc__") == NULL
) {
3539 if (type
->tp_doc
!= NULL
) {
3540 PyObject
*doc
= PyString_FromString(type
->tp_doc
);
3543 PyDict_SetItemString(type
->tp_dict
, "__doc__", doc
);
3546 PyDict_SetItemString(type
->tp_dict
,
3547 "__doc__", Py_None
);
3551 /* Some more special stuff */
3552 base
= type
->tp_base
;
3554 if (type
->tp_as_number
== NULL
)
3555 type
->tp_as_number
= base
->tp_as_number
;
3556 if (type
->tp_as_sequence
== NULL
)
3557 type
->tp_as_sequence
= base
->tp_as_sequence
;
3558 if (type
->tp_as_mapping
== NULL
)
3559 type
->tp_as_mapping
= base
->tp_as_mapping
;
3560 if (type
->tp_as_buffer
== NULL
)
3561 type
->tp_as_buffer
= base
->tp_as_buffer
;
3564 /* Link into each base class's list of subclasses */
3565 bases
= type
->tp_bases
;
3566 n
= PyTuple_GET_SIZE(bases
);
3567 for (i
= 0; i
< n
; i
++) {
3568 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3569 if (PyType_Check(b
) &&
3570 add_subclass((PyTypeObject
*)b
, type
) < 0)
3574 /* All done -- set the ready flag */
3575 assert(type
->tp_dict
!= NULL
);
3577 (type
->tp_flags
& ~Py_TPFLAGS_READYING
) | Py_TPFLAGS_READY
;
3581 type
->tp_flags
&= ~Py_TPFLAGS_READYING
;
3586 add_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3590 PyObject
*list
, *ref
, *newobj
;
3592 list
= base
->tp_subclasses
;
3594 base
->tp_subclasses
= list
= PyList_New(0);
3598 assert(PyList_Check(list
));
3599 newobj
= PyWeakref_NewRef((PyObject
*)type
, NULL
);
3600 i
= PyList_GET_SIZE(list
);
3602 ref
= PyList_GET_ITEM(list
, i
);
3603 assert(PyWeakref_CheckRef(ref
));
3604 if (PyWeakref_GET_OBJECT(ref
) == Py_None
)
3605 return PyList_SetItem(list
, i
, newobj
);
3607 result
= PyList_Append(list
, newobj
);
3613 remove_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3616 PyObject
*list
, *ref
;
3618 list
= base
->tp_subclasses
;
3622 assert(PyList_Check(list
));
3623 i
= PyList_GET_SIZE(list
);
3625 ref
= PyList_GET_ITEM(list
, i
);
3626 assert(PyWeakref_CheckRef(ref
));
3627 if (PyWeakref_GET_OBJECT(ref
) == (PyObject
*)type
) {
3628 /* this can't fail, right? */
3629 PySequence_DelItem(list
, i
);
3636 check_num_args(PyObject
*ob
, int n
)
3638 if (!PyTuple_CheckExact(ob
)) {
3639 PyErr_SetString(PyExc_SystemError
,
3640 "PyArg_UnpackTuple() argument list is not a tuple");
3643 if (n
== PyTuple_GET_SIZE(ob
))
3647 "expected %d arguments, got %zd", n
, PyTuple_GET_SIZE(ob
));
3651 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3653 /* There's a wrapper *function* for each distinct function typedef used
3654 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3655 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3656 Most tables have only one entry; the tables for binary operators have two
3657 entries, one regular and one with reversed arguments. */
3660 wrap_lenfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3662 lenfunc func
= (lenfunc
)wrapped
;
3665 if (!check_num_args(args
, 0))
3667 res
= (*func
)(self
);
3668 if (res
== -1 && PyErr_Occurred())
3670 return PyInt_FromLong((long)res
);
3674 wrap_inquirypred(PyObject
*self
, PyObject
*args
, void *wrapped
)
3676 inquiry func
= (inquiry
)wrapped
;
3679 if (!check_num_args(args
, 0))
3681 res
= (*func
)(self
);
3682 if (res
== -1 && PyErr_Occurred())
3684 return PyBool_FromLong((long)res
);
3688 wrap_binaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3690 binaryfunc func
= (binaryfunc
)wrapped
;
3693 if (!check_num_args(args
, 1))
3695 other
= PyTuple_GET_ITEM(args
, 0);
3696 return (*func
)(self
, other
);
3700 wrap_binaryfunc_l(PyObject
*self
, PyObject
*args
, void *wrapped
)
3702 binaryfunc func
= (binaryfunc
)wrapped
;
3705 if (!check_num_args(args
, 1))
3707 other
= PyTuple_GET_ITEM(args
, 0);
3708 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3709 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3710 Py_INCREF(Py_NotImplemented
);
3711 return Py_NotImplemented
;
3713 return (*func
)(self
, other
);
3717 wrap_binaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3719 binaryfunc func
= (binaryfunc
)wrapped
;
3722 if (!check_num_args(args
, 1))
3724 other
= PyTuple_GET_ITEM(args
, 0);
3725 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3726 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3727 Py_INCREF(Py_NotImplemented
);
3728 return Py_NotImplemented
;
3730 return (*func
)(other
, self
);
3734 wrap_coercefunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3736 coercion func
= (coercion
)wrapped
;
3737 PyObject
*other
, *res
;
3740 if (!check_num_args(args
, 1))
3742 other
= PyTuple_GET_ITEM(args
, 0);
3743 ok
= func(&self
, &other
);
3747 Py_INCREF(Py_NotImplemented
);
3748 return Py_NotImplemented
;
3750 res
= PyTuple_New(2);
3756 PyTuple_SET_ITEM(res
, 0, self
);
3757 PyTuple_SET_ITEM(res
, 1, other
);
3762 wrap_ternaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3764 ternaryfunc func
= (ternaryfunc
)wrapped
;
3766 PyObject
*third
= Py_None
;
3768 /* Note: This wrapper only works for __pow__() */
3770 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3772 return (*func
)(self
, other
, third
);
3776 wrap_ternaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3778 ternaryfunc func
= (ternaryfunc
)wrapped
;
3780 PyObject
*third
= Py_None
;
3782 /* Note: This wrapper only works for __pow__() */
3784 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3786 return (*func
)(other
, self
, third
);
3790 wrap_unaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3792 unaryfunc func
= (unaryfunc
)wrapped
;
3794 if (!check_num_args(args
, 0))
3796 return (*func
)(self
);
3800 wrap_indexargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3802 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
3806 if (!PyArg_UnpackTuple(args
, "", 1, 1, &o
))
3808 i
= PyNumber_AsSsize_t(o
, PyExc_OverflowError
);
3809 if (i
== -1 && PyErr_Occurred())
3811 return (*func
)(self
, i
);
3815 getindex(PyObject
*self
, PyObject
*arg
)
3819 i
= PyNumber_AsSsize_t(arg
, PyExc_OverflowError
);
3820 if (i
== -1 && PyErr_Occurred())
3823 PySequenceMethods
*sq
= Py_Type(self
)->tp_as_sequence
;
3824 if (sq
&& sq
->sq_length
) {
3825 Py_ssize_t n
= (*sq
->sq_length
)(self
);
3835 wrap_sq_item(PyObject
*self
, PyObject
*args
, void *wrapped
)
3837 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
3841 if (PyTuple_GET_SIZE(args
) == 1) {
3842 arg
= PyTuple_GET_ITEM(args
, 0);
3843 i
= getindex(self
, arg
);
3844 if (i
== -1 && PyErr_Occurred())
3846 return (*func
)(self
, i
);
3848 check_num_args(args
, 1);
3849 assert(PyErr_Occurred());
3854 wrap_ssizessizeargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3856 ssizessizeargfunc func
= (ssizessizeargfunc
)wrapped
;
3859 if (!PyArg_ParseTuple(args
, "nn", &i
, &j
))
3861 return (*func
)(self
, i
, j
);
3865 wrap_sq_setitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3867 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
3870 PyObject
*arg
, *value
;
3872 if (!PyArg_UnpackTuple(args
, "", 2, 2, &arg
, &value
))
3874 i
= getindex(self
, arg
);
3875 if (i
== -1 && PyErr_Occurred())
3877 res
= (*func
)(self
, i
, value
);
3878 if (res
== -1 && PyErr_Occurred())
3885 wrap_sq_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3887 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
3892 if (!check_num_args(args
, 1))
3894 arg
= PyTuple_GET_ITEM(args
, 0);
3895 i
= getindex(self
, arg
);
3896 if (i
== -1 && PyErr_Occurred())
3898 res
= (*func
)(self
, i
, NULL
);
3899 if (res
== -1 && PyErr_Occurred())
3906 wrap_ssizessizeobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3908 ssizessizeobjargproc func
= (ssizessizeobjargproc
)wrapped
;
3913 if (!PyArg_ParseTuple(args
, "nnO", &i
, &j
, &value
))
3915 res
= (*func
)(self
, i
, j
, value
);
3916 if (res
== -1 && PyErr_Occurred())
3923 wrap_delslice(PyObject
*self
, PyObject
*args
, void *wrapped
)
3925 ssizessizeobjargproc func
= (ssizessizeobjargproc
)wrapped
;
3929 if (!PyArg_ParseTuple(args
, "nn", &i
, &j
))
3931 res
= (*func
)(self
, i
, j
, NULL
);
3932 if (res
== -1 && PyErr_Occurred())
3938 /* XXX objobjproc is a misnomer; should be objargpred */
3940 wrap_objobjproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3942 objobjproc func
= (objobjproc
)wrapped
;
3946 if (!check_num_args(args
, 1))
3948 value
= PyTuple_GET_ITEM(args
, 0);
3949 res
= (*func
)(self
, value
);
3950 if (res
== -1 && PyErr_Occurred())
3953 return PyBool_FromLong(res
);
3957 wrap_objobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3959 objobjargproc func
= (objobjargproc
)wrapped
;
3961 PyObject
*key
, *value
;
3963 if (!PyArg_UnpackTuple(args
, "", 2, 2, &key
, &value
))
3965 res
= (*func
)(self
, key
, value
);
3966 if (res
== -1 && PyErr_Occurred())
3973 wrap_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3975 objobjargproc func
= (objobjargproc
)wrapped
;
3979 if (!check_num_args(args
, 1))
3981 key
= PyTuple_GET_ITEM(args
, 0);
3982 res
= (*func
)(self
, key
, NULL
);
3983 if (res
== -1 && PyErr_Occurred())
3990 wrap_cmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3992 cmpfunc func
= (cmpfunc
)wrapped
;
3996 if (!check_num_args(args
, 1))
3998 other
= PyTuple_GET_ITEM(args
, 0);
3999 if (Py_Type(other
)->tp_compare
!= func
&&
4000 !PyType_IsSubtype(Py_Type(other
), Py_Type(self
))) {
4003 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
4004 Py_Type(self
)->tp_name
,
4005 Py_Type(self
)->tp_name
,
4006 Py_Type(other
)->tp_name
);
4009 res
= (*func
)(self
, other
);
4010 if (PyErr_Occurred())
4012 return PyInt_FromLong((long)res
);
4015 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4016 This is called the Carlo Verre hack after its discoverer. */
4018 hackcheck(PyObject
*self
, setattrofunc func
, char *what
)
4020 PyTypeObject
*type
= Py_Type(self
);
4021 while (type
&& type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
4022 type
= type
->tp_base
;
4023 /* If type is NULL now, this is a really weird type.
4024 In the spirit of backwards compatibility (?), just shut up. */
4025 if (type
&& type
->tp_setattro
!= func
) {
4026 PyErr_Format(PyExc_TypeError
,
4027 "can't apply this %s to %s object",
4036 wrap_setattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
4038 setattrofunc func
= (setattrofunc
)wrapped
;
4040 PyObject
*name
, *value
;
4042 if (!PyArg_UnpackTuple(args
, "", 2, 2, &name
, &value
))
4044 if (!hackcheck(self
, func
, "__setattr__"))
4046 res
= (*func
)(self
, name
, value
);
4054 wrap_delattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
4056 setattrofunc func
= (setattrofunc
)wrapped
;
4060 if (!check_num_args(args
, 1))
4062 name
= PyTuple_GET_ITEM(args
, 0);
4063 if (!hackcheck(self
, func
, "__delattr__"))
4065 res
= (*func
)(self
, name
, NULL
);
4073 wrap_hashfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4075 hashfunc func
= (hashfunc
)wrapped
;
4078 if (!check_num_args(args
, 0))
4080 res
= (*func
)(self
);
4081 if (res
== -1 && PyErr_Occurred())
4083 return PyInt_FromLong(res
);
4087 wrap_call(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4089 ternaryfunc func
= (ternaryfunc
)wrapped
;
4091 return (*func
)(self
, args
, kwds
);
4095 wrap_richcmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
, int op
)
4097 richcmpfunc func
= (richcmpfunc
)wrapped
;
4100 if (!check_num_args(args
, 1))
4102 other
= PyTuple_GET_ITEM(args
, 0);
4103 return (*func
)(self
, other
, op
);
4106 #undef RICHCMP_WRAPPER
4107 #define RICHCMP_WRAPPER(NAME, OP) \
4109 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4111 return wrap_richcmpfunc(self, args, wrapped, OP); \
4114 RICHCMP_WRAPPER(lt
, Py_LT
)
4115 RICHCMP_WRAPPER(le
, Py_LE
)
4116 RICHCMP_WRAPPER(eq
, Py_EQ
)
4117 RICHCMP_WRAPPER(ne
, Py_NE
)
4118 RICHCMP_WRAPPER(gt
, Py_GT
)
4119 RICHCMP_WRAPPER(ge
, Py_GE
)
4122 wrap_next(PyObject
*self
, PyObject
*args
, void *wrapped
)
4124 unaryfunc func
= (unaryfunc
)wrapped
;
4127 if (!check_num_args(args
, 0))
4129 res
= (*func
)(self
);
4130 if (res
== NULL
&& !PyErr_Occurred())
4131 PyErr_SetNone(PyExc_StopIteration
);
4136 wrap_descr_get(PyObject
*self
, PyObject
*args
, void *wrapped
)
4138 descrgetfunc func
= (descrgetfunc
)wrapped
;
4140 PyObject
*type
= NULL
;
4142 if (!PyArg_UnpackTuple(args
, "", 1, 2, &obj
, &type
))
4146 if (type
== Py_None
)
4148 if (type
== NULL
&&obj
== NULL
) {
4149 PyErr_SetString(PyExc_TypeError
,
4150 "__get__(None, None) is invalid");
4153 return (*func
)(self
, obj
, type
);
4157 wrap_descr_set(PyObject
*self
, PyObject
*args
, void *wrapped
)
4159 descrsetfunc func
= (descrsetfunc
)wrapped
;
4160 PyObject
*obj
, *value
;
4163 if (!PyArg_UnpackTuple(args
, "", 2, 2, &obj
, &value
))
4165 ret
= (*func
)(self
, obj
, value
);
4173 wrap_descr_delete(PyObject
*self
, PyObject
*args
, void *wrapped
)
4175 descrsetfunc func
= (descrsetfunc
)wrapped
;
4179 if (!check_num_args(args
, 1))
4181 obj
= PyTuple_GET_ITEM(args
, 0);
4182 ret
= (*func
)(self
, obj
, NULL
);
4190 wrap_init(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4192 initproc func
= (initproc
)wrapped
;
4194 if (func(self
, args
, kwds
) < 0)
4201 tp_new_wrapper(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4203 PyTypeObject
*type
, *subtype
, *staticbase
;
4204 PyObject
*arg0
, *res
;
4206 if (self
== NULL
|| !PyType_Check(self
))
4207 Py_FatalError("__new__() called with non-type 'self'");
4208 type
= (PyTypeObject
*)self
;
4209 if (!PyTuple_Check(args
) || PyTuple_GET_SIZE(args
) < 1) {
4210 PyErr_Format(PyExc_TypeError
,
4211 "%s.__new__(): not enough arguments",
4215 arg0
= PyTuple_GET_ITEM(args
, 0);
4216 if (!PyType_Check(arg0
)) {
4217 PyErr_Format(PyExc_TypeError
,
4218 "%s.__new__(X): X is not a type object (%s)",
4220 Py_Type(arg0
)->tp_name
);
4223 subtype
= (PyTypeObject
*)arg0
;
4224 if (!PyType_IsSubtype(subtype
, type
)) {
4225 PyErr_Format(PyExc_TypeError
,
4226 "%s.__new__(%s): %s is not a subtype of %s",
4234 /* Check that the use doesn't do something silly and unsafe like
4235 object.__new__(dict). To do this, we check that the
4236 most derived base that's not a heap type is this type. */
4237 staticbase
= subtype
;
4238 while (staticbase
&& (staticbase
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
4239 staticbase
= staticbase
->tp_base
;
4240 /* If staticbase is NULL now, it is a really weird type.
4241 In the spirit of backwards compatibility (?), just shut up. */
4242 if (staticbase
&& staticbase
->tp_new
!= type
->tp_new
) {
4243 PyErr_Format(PyExc_TypeError
,
4244 "%s.__new__(%s) is not safe, use %s.__new__()",
4247 staticbase
== NULL
? "?" : staticbase
->tp_name
);
4251 args
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
4254 res
= type
->tp_new(subtype
, args
, kwds
);
4259 static struct PyMethodDef tp_new_methoddef
[] = {
4260 {"__new__", (PyCFunction
)tp_new_wrapper
, METH_VARARGS
|METH_KEYWORDS
,
4261 PyDoc_STR("T.__new__(S, ...) -> "
4262 "a new object with type S, a subtype of T")},
4267 add_tp_new_wrapper(PyTypeObject
*type
)
4271 if (PyDict_GetItemString(type
->tp_dict
, "__new__") != NULL
)
4273 func
= PyCFunction_New(tp_new_methoddef
, (PyObject
*)type
);
4276 if (PyDict_SetItemString(type
->tp_dict
, "__new__", func
)) {
4284 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4285 below at override_slots() for more explanation. */
4287 #define SLOT0(FUNCNAME, OPSTR) \
4289 FUNCNAME(PyObject *self) \
4291 static PyObject *cache_str; \
4292 return call_method(self, OPSTR, &cache_str, "()"); \
4295 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4297 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4299 static PyObject *cache_str; \
4300 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4303 /* Boolean helper for SLOT1BINFULL().
4304 right.__class__ is a nontrivial subclass of left.__class__. */
4306 method_is_overloaded(PyObject
*left
, PyObject
*right
, char *name
)
4311 b
= PyObject_GetAttrString((PyObject
*)(Py_Type(right
)), name
);
4314 /* If right doesn't have it, it's not overloaded */
4318 a
= PyObject_GetAttrString((PyObject
*)(Py_Type(left
)), name
);
4322 /* If right has it but left doesn't, it's overloaded */
4326 ok
= PyObject_RichCompareBool(a
, b
, Py_NE
);
4338 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4340 FUNCNAME(PyObject *self, PyObject *other) \
4342 static PyObject *cache_str, *rcache_str; \
4343 int do_other = Py_Type(self) != Py_Type(other) && \
4344 Py_Type(other)->tp_as_number != NULL && \
4345 Py_Type(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4346 if (Py_Type(self)->tp_as_number != NULL && \
4347 Py_Type(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
4350 PyType_IsSubtype(Py_Type(other), Py_Type(self)) && \
4351 method_is_overloaded(self, other, ROPSTR)) { \
4353 other, ROPSTR, &rcache_str, "(O)", self); \
4354 if (r != Py_NotImplemented) \
4360 self, OPSTR, &cache_str, "(O)", other); \
4361 if (r != Py_NotImplemented || \
4362 Py_Type(other) == Py_Type(self)) \
4367 return call_maybe( \
4368 other, ROPSTR, &rcache_str, "(O)", self); \
4370 Py_INCREF(Py_NotImplemented); \
4371 return Py_NotImplemented; \
4374 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4375 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4377 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4379 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4381 static PyObject *cache_str; \
4382 return call_method(self, OPSTR, &cache_str, \
4383 "(" ARGCODES ")", arg1, arg2); \
4387 slot_sq_length(PyObject
*self
)
4389 static PyObject
*len_str
;
4390 PyObject
*res
= call_method(self
, "__len__", &len_str
, "()");
4395 len
= PyInt_AsSsize_t(res
);
4398 if (!PyErr_Occurred())
4399 PyErr_SetString(PyExc_ValueError
,
4400 "__len__() should return >= 0");
4406 /* Super-optimized version of slot_sq_item.
4407 Other slots could do the same... */
4409 slot_sq_item(PyObject
*self
, Py_ssize_t i
)
4411 static PyObject
*getitem_str
;
4412 PyObject
*func
, *args
= NULL
, *ival
= NULL
, *retval
= NULL
;
4415 if (getitem_str
== NULL
) {
4416 getitem_str
= PyString_InternFromString("__getitem__");
4417 if (getitem_str
== NULL
)
4420 func
= _PyType_Lookup(Py_Type(self
), getitem_str
);
4422 if ((f
= Py_Type(func
)->tp_descr_get
) == NULL
)
4425 func
= f(func
, self
, (PyObject
*)(Py_Type(self
)));
4430 ival
= PyInt_FromSsize_t(i
);
4432 args
= PyTuple_New(1);
4434 PyTuple_SET_ITEM(args
, 0, ival
);
4435 retval
= PyObject_Call(func
, args
, NULL
);
4443 PyErr_SetObject(PyExc_AttributeError
, getitem_str
);
4451 SLOT2(slot_sq_slice
, "__getslice__", Py_ssize_t
, Py_ssize_t
, "nn")
4454 slot_sq_ass_item(PyObject
*self
, Py_ssize_t index
, PyObject
*value
)
4457 static PyObject
*delitem_str
, *setitem_str
;
4460 res
= call_method(self
, "__delitem__", &delitem_str
,
4463 res
= call_method(self
, "__setitem__", &setitem_str
,
4464 "(nO)", index
, value
);
4472 slot_sq_ass_slice(PyObject
*self
, Py_ssize_t i
, Py_ssize_t j
, PyObject
*value
)
4475 static PyObject
*delslice_str
, *setslice_str
;
4478 res
= call_method(self
, "__delslice__", &delslice_str
,
4481 res
= call_method(self
, "__setslice__", &setslice_str
,
4482 "(nnO)", i
, j
, value
);
4490 slot_sq_contains(PyObject
*self
, PyObject
*value
)
4492 PyObject
*func
, *res
, *args
;
4495 static PyObject
*contains_str
;
4497 func
= lookup_maybe(self
, "__contains__", &contains_str
);
4499 args
= PyTuple_Pack(1, value
);
4503 res
= PyObject_Call(func
, args
, NULL
);
4508 result
= PyObject_IsTrue(res
);
4512 else if (! PyErr_Occurred()) {
4513 /* Possible results: -1 and 1 */
4514 result
= (int)_PySequence_IterSearch(self
, value
,
4515 PY_ITERSEARCH_CONTAINS
);
4520 #define slot_mp_length slot_sq_length
4522 SLOT1(slot_mp_subscript
, "__getitem__", PyObject
*, "O")
4525 slot_mp_ass_subscript(PyObject
*self
, PyObject
*key
, PyObject
*value
)
4528 static PyObject
*delitem_str
, *setitem_str
;
4531 res
= call_method(self
, "__delitem__", &delitem_str
,
4534 res
= call_method(self
, "__setitem__", &setitem_str
,
4535 "(OO)", key
, value
);
4542 SLOT1BIN(slot_nb_add
, nb_add
, "__add__", "__radd__")
4543 SLOT1BIN(slot_nb_subtract
, nb_subtract
, "__sub__", "__rsub__")
4544 SLOT1BIN(slot_nb_multiply
, nb_multiply
, "__mul__", "__rmul__")
4545 SLOT1BIN(slot_nb_divide
, nb_divide
, "__div__", "__rdiv__")
4546 SLOT1BIN(slot_nb_remainder
, nb_remainder
, "__mod__", "__rmod__")
4547 SLOT1BIN(slot_nb_divmod
, nb_divmod
, "__divmod__", "__rdivmod__")
4549 static PyObject
*slot_nb_power(PyObject
*, PyObject
*, PyObject
*);
4551 SLOT1BINFULL(slot_nb_power_binary
, slot_nb_power
,
4552 nb_power
, "__pow__", "__rpow__")
4555 slot_nb_power(PyObject
*self
, PyObject
*other
, PyObject
*modulus
)
4557 static PyObject
*pow_str
;
4559 if (modulus
== Py_None
)
4560 return slot_nb_power_binary(self
, other
);
4561 /* Three-arg power doesn't use __rpow__. But ternary_op
4562 can call this when the second argument's type uses
4563 slot_nb_power, so check before calling self.__pow__. */
4564 if (Py_Type(self
)->tp_as_number
!= NULL
&&
4565 Py_Type(self
)->tp_as_number
->nb_power
== slot_nb_power
) {
4566 return call_method(self
, "__pow__", &pow_str
,
4567 "(OO)", other
, modulus
);
4569 Py_INCREF(Py_NotImplemented
);
4570 return Py_NotImplemented
;
4573 SLOT0(slot_nb_negative
, "__neg__")
4574 SLOT0(slot_nb_positive
, "__pos__")
4575 SLOT0(slot_nb_absolute
, "__abs__")
4578 slot_nb_nonzero(PyObject
*self
)
4580 PyObject
*func
, *args
;
4581 static PyObject
*nonzero_str
, *len_str
;
4584 func
= lookup_maybe(self
, "__nonzero__", &nonzero_str
);
4586 if (PyErr_Occurred())
4588 func
= lookup_maybe(self
, "__len__", &len_str
);
4590 return PyErr_Occurred() ? -1 : 1;
4592 args
= PyTuple_New(0);
4594 PyObject
*temp
= PyObject_Call(func
, args
, NULL
);
4597 if (PyInt_CheckExact(temp
) || PyBool_Check(temp
))
4598 result
= PyObject_IsTrue(temp
);
4600 PyErr_Format(PyExc_TypeError
,
4601 "__nonzero__ should return "
4602 "bool or int, returned %s",
4603 temp
->ob_type
->tp_name
);
4615 slot_nb_index(PyObject
*self
)
4617 static PyObject
*index_str
;
4618 return call_method(self
, "__index__", &index_str
, "()");
4622 SLOT0(slot_nb_invert
, "__invert__")
4623 SLOT1BIN(slot_nb_lshift
, nb_lshift
, "__lshift__", "__rlshift__")
4624 SLOT1BIN(slot_nb_rshift
, nb_rshift
, "__rshift__", "__rrshift__")
4625 SLOT1BIN(slot_nb_and
, nb_and
, "__and__", "__rand__")
4626 SLOT1BIN(slot_nb_xor
, nb_xor
, "__xor__", "__rxor__")
4627 SLOT1BIN(slot_nb_or
, nb_or
, "__or__", "__ror__")
4630 slot_nb_coerce(PyObject
**a
, PyObject
**b
)
4632 static PyObject
*coerce_str
;
4633 PyObject
*self
= *a
, *other
= *b
;
4635 if (self
->ob_type
->tp_as_number
!= NULL
&&
4636 self
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4639 self
, "__coerce__", &coerce_str
, "(O)", other
);
4642 if (r
== Py_NotImplemented
) {
4646 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4647 PyErr_SetString(PyExc_TypeError
,
4648 "__coerce__ didn't return a 2-tuple");
4652 *a
= PyTuple_GET_ITEM(r
, 0);
4654 *b
= PyTuple_GET_ITEM(r
, 1);
4660 if (other
->ob_type
->tp_as_number
!= NULL
&&
4661 other
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4664 other
, "__coerce__", &coerce_str
, "(O)", self
);
4667 if (r
== Py_NotImplemented
) {
4671 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4672 PyErr_SetString(PyExc_TypeError
,
4673 "__coerce__ didn't return a 2-tuple");
4677 *a
= PyTuple_GET_ITEM(r
, 1);
4679 *b
= PyTuple_GET_ITEM(r
, 0);
4687 SLOT0(slot_nb_int
, "__int__")
4688 SLOT0(slot_nb_long
, "__long__")
4689 SLOT0(slot_nb_float
, "__float__")
4690 SLOT0(slot_nb_oct
, "__oct__")
4691 SLOT0(slot_nb_hex
, "__hex__")
4692 SLOT1(slot_nb_inplace_add
, "__iadd__", PyObject
*, "O")
4693 SLOT1(slot_nb_inplace_subtract
, "__isub__", PyObject
*, "O")
4694 SLOT1(slot_nb_inplace_multiply
, "__imul__", PyObject
*, "O")
4695 SLOT1(slot_nb_inplace_divide
, "__idiv__", PyObject
*, "O")
4696 SLOT1(slot_nb_inplace_remainder
, "__imod__", PyObject
*, "O")
4697 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
4699 slot_nb_inplace_power(PyObject
*self
, PyObject
* arg1
, PyObject
*arg2
)
4701 static PyObject
*cache_str
;
4702 return call_method(self
, "__ipow__", &cache_str
, "(" "O" ")", arg1
);
4704 SLOT1(slot_nb_inplace_lshift
, "__ilshift__", PyObject
*, "O")
4705 SLOT1(slot_nb_inplace_rshift
, "__irshift__", PyObject
*, "O")
4706 SLOT1(slot_nb_inplace_and
, "__iand__", PyObject
*, "O")
4707 SLOT1(slot_nb_inplace_xor
, "__ixor__", PyObject
*, "O")
4708 SLOT1(slot_nb_inplace_or
, "__ior__", PyObject
*, "O")
4709 SLOT1BIN(slot_nb_floor_divide
, nb_floor_divide
,
4710 "__floordiv__", "__rfloordiv__")
4711 SLOT1BIN(slot_nb_true_divide
, nb_true_divide
, "__truediv__", "__rtruediv__")
4712 SLOT1(slot_nb_inplace_floor_divide
, "__ifloordiv__", PyObject
*, "O")
4713 SLOT1(slot_nb_inplace_true_divide
, "__itruediv__", PyObject
*, "O")
4716 half_compare(PyObject
*self
, PyObject
*other
)
4718 PyObject
*func
, *args
, *res
;
4719 static PyObject
*cmp_str
;
4722 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4727 args
= PyTuple_Pack(1, other
);
4731 res
= PyObject_Call(func
, args
, NULL
);
4735 if (res
!= Py_NotImplemented
) {
4738 c
= PyInt_AsLong(res
);
4740 if (c
== -1 && PyErr_Occurred())
4742 return (c
< 0) ? -1 : (c
> 0) ? 1 : 0;
4749 /* This slot is published for the benefit of try_3way_compare in object.c */
4751 _PyObject_SlotCompare(PyObject
*self
, PyObject
*other
)
4755 if (Py_Type(self
)->tp_compare
== _PyObject_SlotCompare
) {
4756 c
= half_compare(self
, other
);
4760 if (Py_Type(other
)->tp_compare
== _PyObject_SlotCompare
) {
4761 c
= half_compare(other
, self
);
4767 return (void *)self
< (void *)other
? -1 :
4768 (void *)self
> (void *)other
? 1 : 0;
4772 slot_tp_repr(PyObject
*self
)
4774 PyObject
*func
, *res
;
4775 static PyObject
*repr_str
;
4777 func
= lookup_method(self
, "__repr__", &repr_str
);
4779 res
= PyEval_CallObject(func
, NULL
);
4784 return PyString_FromFormat("<%s object at %p>",
4785 Py_Type(self
)->tp_name
, self
);
4789 slot_tp_str(PyObject
*self
)
4791 PyObject
*func
, *res
;
4792 static PyObject
*str_str
;
4794 func
= lookup_method(self
, "__str__", &str_str
);
4796 res
= PyEval_CallObject(func
, NULL
);
4802 return slot_tp_repr(self
);
4807 slot_tp_hash(PyObject
*self
)
4810 static PyObject
*hash_str
, *eq_str
, *cmp_str
;
4813 func
= lookup_method(self
, "__hash__", &hash_str
);
4816 PyObject
*res
= PyEval_CallObject(func
, NULL
);
4820 if (PyLong_Check(res
))
4821 h
= PyLong_Type
.tp_hash(res
);
4823 h
= PyInt_AsLong(res
);
4828 func
= lookup_method(self
, "__eq__", &eq_str
);
4831 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4834 PyErr_Format(PyExc_TypeError
, "unhashable type: '%.200s'",
4835 self
->ob_type
->tp_name
);
4840 h
= _Py_HashPointer((void *)self
);
4842 if (h
== -1 && !PyErr_Occurred())
4848 slot_tp_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4850 static PyObject
*call_str
;
4851 PyObject
*meth
= lookup_method(self
, "__call__", &call_str
);
4857 res
= PyObject_Call(meth
, args
, kwds
);
4863 /* There are two slot dispatch functions for tp_getattro.
4865 - slot_tp_getattro() is used when __getattribute__ is overridden
4866 but no __getattr__ hook is present;
4868 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4870 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4871 detects the absence of __getattr__ and then installs the simpler slot if
4875 slot_tp_getattro(PyObject
*self
, PyObject
*name
)
4877 static PyObject
*getattribute_str
= NULL
;
4878 return call_method(self
, "__getattribute__", &getattribute_str
,
4883 slot_tp_getattr_hook(PyObject
*self
, PyObject
*name
)
4885 PyTypeObject
*tp
= Py_Type(self
);
4886 PyObject
*getattr
, *getattribute
, *res
;
4887 static PyObject
*getattribute_str
= NULL
;
4888 static PyObject
*getattr_str
= NULL
;
4890 if (getattr_str
== NULL
) {
4891 getattr_str
= PyString_InternFromString("__getattr__");
4892 if (getattr_str
== NULL
)
4895 if (getattribute_str
== NULL
) {
4897 PyString_InternFromString("__getattribute__");
4898 if (getattribute_str
== NULL
)
4901 getattr
= _PyType_Lookup(tp
, getattr_str
);
4902 if (getattr
== NULL
) {
4903 /* No __getattr__ hook: use a simpler dispatcher */
4904 tp
->tp_getattro
= slot_tp_getattro
;
4905 return slot_tp_getattro(self
, name
);
4907 getattribute
= _PyType_Lookup(tp
, getattribute_str
);
4908 if (getattribute
== NULL
||
4909 (Py_Type(getattribute
) == &PyWrapperDescr_Type
&&
4910 ((PyWrapperDescrObject
*)getattribute
)->d_wrapped
==
4911 (void *)PyObject_GenericGetAttr
))
4912 res
= PyObject_GenericGetAttr(self
, name
);
4914 res
= PyObject_CallFunctionObjArgs(getattribute
, self
, name
, NULL
);
4915 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4917 res
= PyObject_CallFunctionObjArgs(getattr
, self
, name
, NULL
);
4923 slot_tp_setattro(PyObject
*self
, PyObject
*name
, PyObject
*value
)
4926 static PyObject
*delattr_str
, *setattr_str
;
4929 res
= call_method(self
, "__delattr__", &delattr_str
,
4932 res
= call_method(self
, "__setattr__", &setattr_str
,
4933 "(OO)", name
, value
);
4940 /* Map rich comparison operators to their __xx__ namesakes */
4941 static char *name_op
[] = {
4951 half_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4953 PyObject
*func
, *args
, *res
;
4954 static PyObject
*op_str
[6];
4956 func
= lookup_method(self
, name_op
[op
], &op_str
[op
]);
4959 Py_INCREF(Py_NotImplemented
);
4960 return Py_NotImplemented
;
4962 args
= PyTuple_Pack(1, other
);
4966 res
= PyObject_Call(func
, args
, NULL
);
4974 slot_tp_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4978 if (Py_Type(self
)->tp_richcompare
== slot_tp_richcompare
) {
4979 res
= half_richcompare(self
, other
, op
);
4980 if (res
!= Py_NotImplemented
)
4984 if (Py_Type(other
)->tp_richcompare
== slot_tp_richcompare
) {
4985 res
= half_richcompare(other
, self
, _Py_SwappedOp
[op
]);
4986 if (res
!= Py_NotImplemented
) {
4991 Py_INCREF(Py_NotImplemented
);
4992 return Py_NotImplemented
;
4996 slot_tp_iter(PyObject
*self
)
4998 PyObject
*func
, *res
;
4999 static PyObject
*iter_str
, *getitem_str
;
5001 func
= lookup_method(self
, "__iter__", &iter_str
);
5004 args
= res
= PyTuple_New(0);
5006 res
= PyObject_Call(func
, args
, NULL
);
5013 func
= lookup_method(self
, "__getitem__", &getitem_str
);
5015 PyErr_Format(PyExc_TypeError
,
5016 "'%.200s' object is not iterable",
5017 Py_Type(self
)->tp_name
);
5021 return PySeqIter_New(self
);
5025 slot_tp_iternext(PyObject
*self
)
5027 static PyObject
*next_str
;
5028 return call_method(self
, "next", &next_str
, "()");
5032 slot_tp_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
5034 PyTypeObject
*tp
= Py_Type(self
);
5036 static PyObject
*get_str
= NULL
;
5038 if (get_str
== NULL
) {
5039 get_str
= PyString_InternFromString("__get__");
5040 if (get_str
== NULL
)
5043 get
= _PyType_Lookup(tp
, get_str
);
5045 /* Avoid further slowdowns */
5046 if (tp
->tp_descr_get
== slot_tp_descr_get
)
5047 tp
->tp_descr_get
= NULL
;
5055 return PyObject_CallFunctionObjArgs(get
, self
, obj
, type
, NULL
);
5059 slot_tp_descr_set(PyObject
*self
, PyObject
*target
, PyObject
*value
)
5062 static PyObject
*del_str
, *set_str
;
5065 res
= call_method(self
, "__delete__", &del_str
,
5068 res
= call_method(self
, "__set__", &set_str
,
5069 "(OO)", target
, value
);
5077 slot_tp_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5079 static PyObject
*init_str
;
5080 PyObject
*meth
= lookup_method(self
, "__init__", &init_str
);
5085 res
= PyObject_Call(meth
, args
, kwds
);
5089 if (res
!= Py_None
) {
5090 PyErr_Format(PyExc_TypeError
,
5091 "__init__() should return None, not '%.200s'",
5092 Py_Type(res
)->tp_name
);
5101 slot_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
5103 static PyObject
*new_str
;
5105 PyObject
*newargs
, *x
;
5108 if (new_str
== NULL
) {
5109 new_str
= PyString_InternFromString("__new__");
5110 if (new_str
== NULL
)
5113 func
= PyObject_GetAttr((PyObject
*)type
, new_str
);
5116 assert(PyTuple_Check(args
));
5117 n
= PyTuple_GET_SIZE(args
);
5118 newargs
= PyTuple_New(n
+1);
5119 if (newargs
== NULL
)
5122 PyTuple_SET_ITEM(newargs
, 0, (PyObject
*)type
);
5123 for (i
= 0; i
< n
; i
++) {
5124 x
= PyTuple_GET_ITEM(args
, i
);
5126 PyTuple_SET_ITEM(newargs
, i
+1, x
);
5128 x
= PyObject_Call(func
, newargs
, kwds
);
5135 slot_tp_del(PyObject
*self
)
5137 static PyObject
*del_str
= NULL
;
5138 PyObject
*del
, *res
;
5139 PyObject
*error_type
, *error_value
, *error_traceback
;
5141 /* Temporarily resurrect the object. */
5142 assert(self
->ob_refcnt
== 0);
5143 self
->ob_refcnt
= 1;
5145 /* Save the current exception, if any. */
5146 PyErr_Fetch(&error_type
, &error_value
, &error_traceback
);
5148 /* Execute __del__ method, if any. */
5149 del
= lookup_maybe(self
, "__del__", &del_str
);
5151 res
= PyEval_CallObject(del
, NULL
);
5153 PyErr_WriteUnraisable(del
);
5159 /* Restore the saved exception. */
5160 PyErr_Restore(error_type
, error_value
, error_traceback
);
5162 /* Undo the temporary resurrection; can't use DECREF here, it would
5163 * cause a recursive call.
5165 assert(self
->ob_refcnt
> 0);
5166 if (--self
->ob_refcnt
== 0)
5167 return; /* this is the normal path out */
5169 /* __del__ resurrected it! Make it look like the original Py_DECREF
5173 Py_ssize_t refcnt
= self
->ob_refcnt
;
5174 _Py_NewReference(self
);
5175 self
->ob_refcnt
= refcnt
;
5177 assert(!PyType_IS_GC(Py_Type(self
)) ||
5178 _Py_AS_GC(self
)->gc
.gc_refs
!= _PyGC_REFS_UNTRACKED
);
5179 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5180 * we need to undo that. */
5182 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5183 * chain, so no more to do there.
5184 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5185 * _Py_NewReference bumped tp_allocs: both of those need to be
5189 --Py_Type(self
)->tp_frees
;
5190 --Py_Type(self
)->tp_allocs
;
5195 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
5196 functions. The offsets here are relative to the 'PyHeapTypeObject'
5197 structure, which incorporates the additional structures used for numbers,
5198 sequences and mappings.
5199 Note that multiple names may map to the same slot (e.g. __eq__,
5200 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
5201 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5202 terminated with an all-zero entry. (This table is further initialized and
5203 sorted in init_slotdefs() below.) */
5205 typedef struct wrapperbase slotdef
;
5218 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5219 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5221 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5222 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5223 PyDoc_STR(DOC), FLAGS}
5224 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5225 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5227 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5228 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5229 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5230 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5231 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5232 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5233 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5234 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5235 "x." NAME "() <==> " DOC)
5236 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5237 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5238 "x." NAME "(y) <==> x" DOC "y")
5239 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5240 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5241 "x." NAME "(y) <==> x" DOC "y")
5242 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5243 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5244 "x." NAME "(y) <==> y" DOC "x")
5245 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5246 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5247 "x." NAME "(y) <==> " DOC)
5248 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5249 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5250 "x." NAME "(y) <==> " DOC)
5252 static slotdef slotdefs
[] = {
5253 SQSLOT("__len__", sq_length
, slot_sq_length
, wrap_lenfunc
,
5254 "x.__len__() <==> len(x)"),
5255 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5256 The logic in abstract.c always falls back to nb_add/nb_multiply in
5257 this case. Defining both the nb_* and the sq_* slots to call the
5258 user-defined methods has unexpected side-effects, as shown by
5259 test_descr.notimplemented() */
5260 SQSLOT("__add__", sq_concat
, NULL
, wrap_binaryfunc
,
5261 "x.__add__(y) <==> x+y"),
5262 SQSLOT("__mul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5263 "x.__mul__(n) <==> x*n"),
5264 SQSLOT("__rmul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5265 "x.__rmul__(n) <==> n*x"),
5266 SQSLOT("__getitem__", sq_item
, slot_sq_item
, wrap_sq_item
,
5267 "x.__getitem__(y) <==> x[y]"),
5268 SQSLOT("__getslice__", sq_slice
, slot_sq_slice
, wrap_ssizessizeargfunc
,
5269 "x.__getslice__(i, j) <==> x[i:j]\n\
5271 Use of negative indices is not supported."),
5272 SQSLOT("__setitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_setitem
,
5273 "x.__setitem__(i, y) <==> x[i]=y"),
5274 SQSLOT("__delitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_delitem
,
5275 "x.__delitem__(y) <==> del x[y]"),
5276 SQSLOT("__setslice__", sq_ass_slice
, slot_sq_ass_slice
,
5277 wrap_ssizessizeobjargproc
,
5278 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
5280 Use of negative indices is not supported."),
5281 SQSLOT("__delslice__", sq_ass_slice
, slot_sq_ass_slice
, wrap_delslice
,
5282 "x.__delslice__(i, j) <==> del x[i:j]\n\
5284 Use of negative indices is not supported."),
5285 SQSLOT("__contains__", sq_contains
, slot_sq_contains
, wrap_objobjproc
,
5286 "x.__contains__(y) <==> y in x"),
5287 SQSLOT("__iadd__", sq_inplace_concat
, NULL
,
5288 wrap_binaryfunc
, "x.__iadd__(y) <==> x+=y"),
5289 SQSLOT("__imul__", sq_inplace_repeat
, NULL
,
5290 wrap_indexargfunc
, "x.__imul__(y) <==> x*=y"),
5292 MPSLOT("__len__", mp_length
, slot_mp_length
, wrap_lenfunc
,
5293 "x.__len__() <==> len(x)"),
5294 MPSLOT("__getitem__", mp_subscript
, slot_mp_subscript
,
5296 "x.__getitem__(y) <==> x[y]"),
5297 MPSLOT("__setitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5299 "x.__setitem__(i, y) <==> x[i]=y"),
5300 MPSLOT("__delitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5302 "x.__delitem__(y) <==> del x[y]"),
5304 BINSLOT("__add__", nb_add
, slot_nb_add
,
5306 RBINSLOT("__radd__", nb_add
, slot_nb_add
,
5308 BINSLOT("__sub__", nb_subtract
, slot_nb_subtract
,
5310 RBINSLOT("__rsub__", nb_subtract
, slot_nb_subtract
,
5312 BINSLOT("__mul__", nb_multiply
, slot_nb_multiply
,
5314 RBINSLOT("__rmul__", nb_multiply
, slot_nb_multiply
,
5316 BINSLOT("__div__", nb_divide
, slot_nb_divide
,
5318 RBINSLOT("__rdiv__", nb_divide
, slot_nb_divide
,
5320 BINSLOT("__mod__", nb_remainder
, slot_nb_remainder
,
5322 RBINSLOT("__rmod__", nb_remainder
, slot_nb_remainder
,
5324 BINSLOTNOTINFIX("__divmod__", nb_divmod
, slot_nb_divmod
,
5326 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod
, slot_nb_divmod
,
5328 NBSLOT("__pow__", nb_power
, slot_nb_power
, wrap_ternaryfunc
,
5329 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5330 NBSLOT("__rpow__", nb_power
, slot_nb_power
, wrap_ternaryfunc_r
,
5331 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5332 UNSLOT("__neg__", nb_negative
, slot_nb_negative
, wrap_unaryfunc
, "-x"),
5333 UNSLOT("__pos__", nb_positive
, slot_nb_positive
, wrap_unaryfunc
, "+x"),
5334 UNSLOT("__abs__", nb_absolute
, slot_nb_absolute
, wrap_unaryfunc
,
5336 UNSLOT("__nonzero__", nb_nonzero
, slot_nb_nonzero
, wrap_inquirypred
,
5338 UNSLOT("__invert__", nb_invert
, slot_nb_invert
, wrap_unaryfunc
, "~x"),
5339 BINSLOT("__lshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5340 RBINSLOT("__rlshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5341 BINSLOT("__rshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5342 RBINSLOT("__rrshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5343 BINSLOT("__and__", nb_and
, slot_nb_and
, "&"),
5344 RBINSLOT("__rand__", nb_and
, slot_nb_and
, "&"),
5345 BINSLOT("__xor__", nb_xor
, slot_nb_xor
, "^"),
5346 RBINSLOT("__rxor__", nb_xor
, slot_nb_xor
, "^"),
5347 BINSLOT("__or__", nb_or
, slot_nb_or
, "|"),
5348 RBINSLOT("__ror__", nb_or
, slot_nb_or
, "|"),
5349 NBSLOT("__coerce__", nb_coerce
, slot_nb_coerce
, wrap_coercefunc
,
5350 "x.__coerce__(y) <==> coerce(x, y)"),
5351 UNSLOT("__int__", nb_int
, slot_nb_int
, wrap_unaryfunc
,
5353 UNSLOT("__long__", nb_long
, slot_nb_long
, wrap_unaryfunc
,
5355 UNSLOT("__float__", nb_float
, slot_nb_float
, wrap_unaryfunc
,
5357 UNSLOT("__oct__", nb_oct
, slot_nb_oct
, wrap_unaryfunc
,
5359 UNSLOT("__hex__", nb_hex
, slot_nb_hex
, wrap_unaryfunc
,
5361 NBSLOT("__index__", nb_index
, slot_nb_index
, wrap_unaryfunc
,
5362 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5363 IBSLOT("__iadd__", nb_inplace_add
, slot_nb_inplace_add
,
5364 wrap_binaryfunc
, "+"),
5365 IBSLOT("__isub__", nb_inplace_subtract
, slot_nb_inplace_subtract
,
5366 wrap_binaryfunc
, "-"),
5367 IBSLOT("__imul__", nb_inplace_multiply
, slot_nb_inplace_multiply
,
5368 wrap_binaryfunc
, "*"),
5369 IBSLOT("__idiv__", nb_inplace_divide
, slot_nb_inplace_divide
,
5370 wrap_binaryfunc
, "/"),
5371 IBSLOT("__imod__", nb_inplace_remainder
, slot_nb_inplace_remainder
,
5372 wrap_binaryfunc
, "%"),
5373 IBSLOT("__ipow__", nb_inplace_power
, slot_nb_inplace_power
,
5374 wrap_binaryfunc
, "**"),
5375 IBSLOT("__ilshift__", nb_inplace_lshift
, slot_nb_inplace_lshift
,
5376 wrap_binaryfunc
, "<<"),
5377 IBSLOT("__irshift__", nb_inplace_rshift
, slot_nb_inplace_rshift
,
5378 wrap_binaryfunc
, ">>"),
5379 IBSLOT("__iand__", nb_inplace_and
, slot_nb_inplace_and
,
5380 wrap_binaryfunc
, "&"),
5381 IBSLOT("__ixor__", nb_inplace_xor
, slot_nb_inplace_xor
,
5382 wrap_binaryfunc
, "^"),
5383 IBSLOT("__ior__", nb_inplace_or
, slot_nb_inplace_or
,
5384 wrap_binaryfunc
, "|"),
5385 BINSLOT("__floordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5386 RBINSLOT("__rfloordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5387 BINSLOT("__truediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5388 RBINSLOT("__rtruediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5389 IBSLOT("__ifloordiv__", nb_inplace_floor_divide
,
5390 slot_nb_inplace_floor_divide
, wrap_binaryfunc
, "//"),
5391 IBSLOT("__itruediv__", nb_inplace_true_divide
,
5392 slot_nb_inplace_true_divide
, wrap_binaryfunc
, "/"),
5394 TPSLOT("__str__", tp_str
, slot_tp_str
, wrap_unaryfunc
,
5395 "x.__str__() <==> str(x)"),
5396 TPSLOT("__str__", tp_print
, NULL
, NULL
, ""),
5397 TPSLOT("__repr__", tp_repr
, slot_tp_repr
, wrap_unaryfunc
,
5398 "x.__repr__() <==> repr(x)"),
5399 TPSLOT("__repr__", tp_print
, NULL
, NULL
, ""),
5400 TPSLOT("__cmp__", tp_compare
, _PyObject_SlotCompare
, wrap_cmpfunc
,
5401 "x.__cmp__(y) <==> cmp(x,y)"),
5402 TPSLOT("__hash__", tp_hash
, slot_tp_hash
, wrap_hashfunc
,
5403 "x.__hash__() <==> hash(x)"),
5404 FLSLOT("__call__", tp_call
, slot_tp_call
, (wrapperfunc
)wrap_call
,
5405 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS
),
5406 TPSLOT("__getattribute__", tp_getattro
, slot_tp_getattr_hook
,
5407 wrap_binaryfunc
, "x.__getattribute__('name') <==> x.name"),
5408 TPSLOT("__getattribute__", tp_getattr
, NULL
, NULL
, ""),
5409 TPSLOT("__getattr__", tp_getattro
, slot_tp_getattr_hook
, NULL
, ""),
5410 TPSLOT("__getattr__", tp_getattr
, NULL
, NULL
, ""),
5411 TPSLOT("__setattr__", tp_setattro
, slot_tp_setattro
, wrap_setattr
,
5412 "x.__setattr__('name', value) <==> x.name = value"),
5413 TPSLOT("__setattr__", tp_setattr
, NULL
, NULL
, ""),
5414 TPSLOT("__delattr__", tp_setattro
, slot_tp_setattro
, wrap_delattr
,
5415 "x.__delattr__('name') <==> del x.name"),
5416 TPSLOT("__delattr__", tp_setattr
, NULL
, NULL
, ""),
5417 TPSLOT("__lt__", tp_richcompare
, slot_tp_richcompare
, richcmp_lt
,
5418 "x.__lt__(y) <==> x<y"),
5419 TPSLOT("__le__", tp_richcompare
, slot_tp_richcompare
, richcmp_le
,
5420 "x.__le__(y) <==> x<=y"),
5421 TPSLOT("__eq__", tp_richcompare
, slot_tp_richcompare
, richcmp_eq
,
5422 "x.__eq__(y) <==> x==y"),
5423 TPSLOT("__ne__", tp_richcompare
, slot_tp_richcompare
, richcmp_ne
,
5424 "x.__ne__(y) <==> x!=y"),
5425 TPSLOT("__gt__", tp_richcompare
, slot_tp_richcompare
, richcmp_gt
,
5426 "x.__gt__(y) <==> x>y"),
5427 TPSLOT("__ge__", tp_richcompare
, slot_tp_richcompare
, richcmp_ge
,
5428 "x.__ge__(y) <==> x>=y"),
5429 TPSLOT("__iter__", tp_iter
, slot_tp_iter
, wrap_unaryfunc
,
5430 "x.__iter__() <==> iter(x)"),
5431 TPSLOT("next", tp_iternext
, slot_tp_iternext
, wrap_next
,
5432 "x.next() -> the next value, or raise StopIteration"),
5433 TPSLOT("__get__", tp_descr_get
, slot_tp_descr_get
, wrap_descr_get
,
5434 "descr.__get__(obj[, type]) -> value"),
5435 TPSLOT("__set__", tp_descr_set
, slot_tp_descr_set
, wrap_descr_set
,
5436 "descr.__set__(obj, value)"),
5437 TPSLOT("__delete__", tp_descr_set
, slot_tp_descr_set
,
5438 wrap_descr_delete
, "descr.__delete__(obj)"),
5439 FLSLOT("__init__", tp_init
, slot_tp_init
, (wrapperfunc
)wrap_init
,
5440 "x.__init__(...) initializes x; "
5441 "see x.__class__.__doc__ for signature",
5442 PyWrapperFlag_KEYWORDS
),
5443 TPSLOT("__new__", tp_new
, slot_tp_new
, NULL
, ""),
5444 TPSLOT("__del__", tp_del
, slot_tp_del
, NULL
, ""),
5448 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5449 pointer to the actual slot. This is not quite the same as simply adding
5450 the offset to the type pointer, since it takes care to indirect through the
5451 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5452 indirection pointer is NULL. */
5454 slotptr(PyTypeObject
*type
, int ioffset
)
5457 long offset
= ioffset
;
5459 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5460 assert(offset
>= 0);
5461 assert((size_t)offset
< offsetof(PyHeapTypeObject
, as_buffer
));
5462 if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_sequence
)) {
5463 ptr
= (char *)type
->tp_as_sequence
;
5464 offset
-= offsetof(PyHeapTypeObject
, as_sequence
);
5466 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_mapping
)) {
5467 ptr
= (char *)type
->tp_as_mapping
;
5468 offset
-= offsetof(PyHeapTypeObject
, as_mapping
);
5470 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_number
)) {
5471 ptr
= (char *)type
->tp_as_number
;
5472 offset
-= offsetof(PyHeapTypeObject
, as_number
);
5479 return (void **)ptr
;
5482 /* Length of array of slotdef pointers used to store slots with the
5483 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5484 the same __name__, for any __name__. Since that's a static property, it is
5485 appropriate to declare fixed-size arrays for this. */
5486 #define MAX_EQUIV 10
5488 /* Return a slot pointer for a given name, but ONLY if the attribute has
5489 exactly one slot function. The name must be an interned string. */
5491 resolve_slotdups(PyTypeObject
*type
, PyObject
*name
)
5493 /* XXX Maybe this could be optimized more -- but is it worth it? */
5495 /* pname and ptrs act as a little cache */
5496 static PyObject
*pname
;
5497 static slotdef
*ptrs
[MAX_EQUIV
];
5501 if (pname
!= name
) {
5502 /* Collect all slotdefs that match name into ptrs. */
5505 for (p
= slotdefs
; p
->name_strobj
; p
++) {
5506 if (p
->name_strobj
== name
)
5512 /* Look in all matching slots of the type; if exactly one of these has
5513 a filled-in slot, return its value. Otherwise return NULL. */
5515 for (pp
= ptrs
; *pp
; pp
++) {
5516 ptr
= slotptr(type
, (*pp
)->offset
);
5517 if (ptr
== NULL
|| *ptr
== NULL
)
5526 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5527 does some incredibly complex thinking and then sticks something into the
5528 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5529 interests, and then stores a generic wrapper or a specific function into
5530 the slot.) Return a pointer to the next slotdef with a different offset,
5531 because that's convenient for fixup_slot_dispatchers(). */
5533 update_one_slot(PyTypeObject
*type
, slotdef
*p
)
5536 PyWrapperDescrObject
*d
;
5537 void *generic
= NULL
, *specific
= NULL
;
5538 int use_generic
= 0;
5539 int offset
= p
->offset
;
5540 void **ptr
= slotptr(type
, offset
);
5545 } while (p
->offset
== offset
);
5549 descr
= _PyType_Lookup(type
, p
->name_strobj
);
5552 if (Py_Type(descr
) == &PyWrapperDescr_Type
) {
5553 void **tptr
= resolve_slotdups(type
, p
->name_strobj
);
5554 if (tptr
== NULL
|| tptr
== ptr
)
5555 generic
= p
->function
;
5556 d
= (PyWrapperDescrObject
*)descr
;
5557 if (d
->d_base
->wrapper
== p
->wrapper
&&
5558 PyType_IsSubtype(type
, d
->d_type
))
5560 if (specific
== NULL
||
5561 specific
== d
->d_wrapped
)
5562 specific
= d
->d_wrapped
;
5567 else if (Py_Type(descr
) == &PyCFunction_Type
&&
5568 PyCFunction_GET_FUNCTION(descr
) ==
5569 (PyCFunction
)tp_new_wrapper
&&
5570 strcmp(p
->name
, "__new__") == 0)
5572 /* The __new__ wrapper is not a wrapper descriptor,
5573 so must be special-cased differently.
5574 If we don't do this, creating an instance will
5575 always use slot_tp_new which will look up
5576 __new__ in the MRO which will call tp_new_wrapper
5577 which will look through the base classes looking
5578 for a static base and call its tp_new (usually
5579 PyType_GenericNew), after performing various
5580 sanity checks and constructing a new argument
5581 list. Cut all that nonsense short -- this speeds
5582 up instance creation tremendously. */
5583 specific
= (void *)type
->tp_new
;
5584 /* XXX I'm not 100% sure that there isn't a hole
5585 in this reasoning that requires additional
5586 sanity checks. I'll buy the first person to
5587 point out a bug in this reasoning a beer. */
5591 generic
= p
->function
;
5593 } while ((++p
)->offset
== offset
);
5594 if (specific
&& !use_generic
)
5601 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5602 This is a callback for update_subclasses(). */
5604 update_slots_callback(PyTypeObject
*type
, void *data
)
5606 slotdef
**pp
= (slotdef
**)data
;
5609 update_one_slot(type
, *pp
);
5613 /* Comparison function for qsort() to compare slotdefs by their offset, and
5614 for equal offset by their address (to force a stable sort). */
5616 slotdef_cmp(const void *aa
, const void *bb
)
5618 const slotdef
*a
= (const slotdef
*)aa
, *b
= (const slotdef
*)bb
;
5619 int c
= a
->offset
- b
->offset
;
5623 /* Cannot use a-b, as this gives off_t,
5624 which may lose precision when converted to int. */
5625 return (a
> b
) ? 1 : (a
< b
) ? -1 : 0;
5628 /* Initialize the slotdefs table by adding interned string objects for the
5629 names and sorting the entries. */
5634 static int initialized
= 0;
5638 for (p
= slotdefs
; p
->name
; p
++) {
5639 p
->name_strobj
= PyString_InternFromString(p
->name
);
5640 if (!p
->name_strobj
)
5641 Py_FatalError("Out of memory interning slotdef names");
5643 qsort((void *)slotdefs
, (size_t)(p
-slotdefs
), sizeof(slotdef
),
5648 /* Update the slots after assignment to a class (type) attribute. */
5650 update_slot(PyTypeObject
*type
, PyObject
*name
)
5652 slotdef
*ptrs
[MAX_EQUIV
];
5659 for (p
= slotdefs
; p
->name
; p
++) {
5660 /* XXX assume name is interned! */
5661 if (p
->name_strobj
== name
)
5665 for (pp
= ptrs
; *pp
; pp
++) {
5668 while (p
> slotdefs
&& (p
-1)->offset
== offset
)
5672 if (ptrs
[0] == NULL
)
5673 return 0; /* Not an attribute that affects any slots */
5674 return update_subclasses(type
, name
,
5675 update_slots_callback
, (void *)ptrs
);
5678 /* Store the proper functions in the slot dispatches at class (type)
5679 definition time, based upon which operations the class overrides in its
5682 fixup_slot_dispatchers(PyTypeObject
*type
)
5687 for (p
= slotdefs
; p
->name
; )
5688 p
= update_one_slot(type
, p
);
5692 update_all_slots(PyTypeObject
* type
)
5697 for (p
= slotdefs
; p
->name
; p
++) {
5698 /* update_slot returns int but can't actually fail */
5699 update_slot(type
, p
->name_strobj
);
5703 /* recurse_down_subclasses() and update_subclasses() are mutually
5704 recursive functions to call a callback for all subclasses,
5705 but refraining from recursing into subclasses that define 'name'. */
5708 update_subclasses(PyTypeObject
*type
, PyObject
*name
,
5709 update_callback callback
, void *data
)
5711 if (callback(type
, data
) < 0)
5713 return recurse_down_subclasses(type
, name
, callback
, data
);
5717 recurse_down_subclasses(PyTypeObject
*type
, PyObject
*name
,
5718 update_callback callback
, void *data
)
5720 PyTypeObject
*subclass
;
5721 PyObject
*ref
, *subclasses
, *dict
;
5724 subclasses
= type
->tp_subclasses
;
5725 if (subclasses
== NULL
)
5727 assert(PyList_Check(subclasses
));
5728 n
= PyList_GET_SIZE(subclasses
);
5729 for (i
= 0; i
< n
; i
++) {
5730 ref
= PyList_GET_ITEM(subclasses
, i
);
5731 assert(PyWeakref_CheckRef(ref
));
5732 subclass
= (PyTypeObject
*)PyWeakref_GET_OBJECT(ref
);
5733 assert(subclass
!= NULL
);
5734 if ((PyObject
*)subclass
== Py_None
)
5736 assert(PyType_Check(subclass
));
5737 /* Avoid recursing down into unaffected classes */
5738 dict
= subclass
->tp_dict
;
5739 if (dict
!= NULL
&& PyDict_Check(dict
) &&
5740 PyDict_GetItem(dict
, name
) != NULL
)
5742 if (update_subclasses(subclass
, name
, callback
, data
) < 0)
5748 /* This function is called by PyType_Ready() to populate the type's
5749 dictionary with method descriptors for function slots. For each
5750 function slot (like tp_repr) that's defined in the type, one or more
5751 corresponding descriptors are added in the type's tp_dict dictionary
5752 under the appropriate name (like __repr__). Some function slots
5753 cause more than one descriptor to be added (for example, the nb_add
5754 slot adds both __add__ and __radd__ descriptors) and some function
5755 slots compete for the same descriptor (for example both sq_item and
5756 mp_subscript generate a __getitem__ descriptor).
5758 In the latter case, the first slotdef entry encoutered wins. Since
5759 slotdef entries are sorted by the offset of the slot in the
5760 PyHeapTypeObject, this gives us some control over disambiguating
5761 between competing slots: the members of PyHeapTypeObject are listed
5762 from most general to least general, so the most general slot is
5763 preferred. In particular, because as_mapping comes before as_sequence,
5764 for a type that defines both mp_subscript and sq_item, mp_subscript
5767 This only adds new descriptors and doesn't overwrite entries in
5768 tp_dict that were previously defined. The descriptors contain a
5769 reference to the C function they must call, so that it's safe if they
5770 are copied into a subtype's __dict__ and the subtype has a different
5771 C function in its slot -- calling the method defined by the
5772 descriptor will call the C function that was used to create it,
5773 rather than the C function present in the slot when it is called.
5774 (This is important because a subtype may have a C function in the
5775 slot that calls the method from the dictionary, and we want to avoid
5776 infinite recursion here.) */
5779 add_operators(PyTypeObject
*type
)
5781 PyObject
*dict
= type
->tp_dict
;
5787 for (p
= slotdefs
; p
->name
; p
++) {
5788 if (p
->wrapper
== NULL
)
5790 ptr
= slotptr(type
, p
->offset
);
5793 if (PyDict_GetItem(dict
, p
->name_strobj
))
5795 descr
= PyDescr_NewWrapper(type
, p
, *ptr
);
5798 if (PyDict_SetItem(dict
, p
->name_strobj
, descr
) < 0)
5802 if (type
->tp_new
!= NULL
) {
5803 if (add_tp_new_wrapper(type
) < 0)
5810 /* Cooperative 'super' */
5816 PyTypeObject
*obj_type
;
5819 static PyMemberDef super_members
[] = {
5820 {"__thisclass__", T_OBJECT
, offsetof(superobject
, type
), READONLY
,
5821 "the class invoking super()"},
5822 {"__self__", T_OBJECT
, offsetof(superobject
, obj
), READONLY
,
5823 "the instance invoking super(); may be None"},
5824 {"__self_class__", T_OBJECT
, offsetof(superobject
, obj_type
), READONLY
,
5825 "the type of the instance invoking super(); may be None"},
5830 super_dealloc(PyObject
*self
)
5832 superobject
*su
= (superobject
*)self
;
5834 _PyObject_GC_UNTRACK(self
);
5835 Py_XDECREF(su
->obj
);
5836 Py_XDECREF(su
->type
);
5837 Py_XDECREF(su
->obj_type
);
5838 Py_Type(self
)->tp_free(self
);
5842 super_repr(PyObject
*self
)
5844 superobject
*su
= (superobject
*)self
;
5847 return PyString_FromFormat(
5848 "<super: <class '%s'>, <%s object>>",
5849 su
->type
? su
->type
->tp_name
: "NULL",
5850 su
->obj_type
->tp_name
);
5852 return PyString_FromFormat(
5853 "<super: <class '%s'>, NULL>",
5854 su
->type
? su
->type
->tp_name
: "NULL");
5858 super_getattro(PyObject
*self
, PyObject
*name
)
5860 superobject
*su
= (superobject
*)self
;
5861 int skip
= su
->obj_type
== NULL
;
5864 /* We want __class__ to return the class of the super object
5865 (i.e. super, or a subclass), not the class of su->obj. */
5866 skip
= (PyString_Check(name
) &&
5867 PyString_GET_SIZE(name
) == 9 &&
5868 strcmp(PyString_AS_STRING(name
), "__class__") == 0);
5872 PyObject
*mro
, *res
, *tmp
, *dict
;
5873 PyTypeObject
*starttype
;
5877 starttype
= su
->obj_type
;
5878 mro
= starttype
->tp_mro
;
5883 assert(PyTuple_Check(mro
));
5884 n
= PyTuple_GET_SIZE(mro
);
5886 for (i
= 0; i
< n
; i
++) {
5887 if ((PyObject
*)(su
->type
) == PyTuple_GET_ITEM(mro
, i
))
5892 for (; i
< n
; i
++) {
5893 tmp
= PyTuple_GET_ITEM(mro
, i
);
5894 if (PyType_Check(tmp
))
5895 dict
= ((PyTypeObject
*)tmp
)->tp_dict
;
5896 else if (PyClass_Check(tmp
))
5897 dict
= ((PyClassObject
*)tmp
)->cl_dict
;
5900 res
= PyDict_GetItem(dict
, name
);
5903 f
= Py_Type(res
)->tp_descr_get
;
5906 /* Only pass 'obj' param if
5907 this is instance-mode super
5910 (su
->obj
== (PyObject
*)
5914 (PyObject
*)starttype
);
5922 return PyObject_GenericGetAttr(self
, name
);
5925 static PyTypeObject
*
5926 supercheck(PyTypeObject
*type
, PyObject
*obj
)
5928 /* Check that a super() call makes sense. Return a type object.
5930 obj can be a new-style class, or an instance of one:
5932 - If it is a class, it must be a subclass of 'type'. This case is
5933 used for class methods; the return value is obj.
5935 - If it is an instance, it must be an instance of 'type'. This is
5936 the normal case; the return value is obj.__class__.
5938 But... when obj is an instance, we want to allow for the case where
5939 Py_Type(obj) is not a subclass of type, but obj.__class__ is!
5940 This will allow using super() with a proxy for obj.
5943 /* Check for first bullet above (special case) */
5944 if (PyType_Check(obj
) && PyType_IsSubtype((PyTypeObject
*)obj
, type
)) {
5946 return (PyTypeObject
*)obj
;
5950 if (PyType_IsSubtype(Py_Type(obj
), type
)) {
5951 Py_INCREF(Py_Type(obj
));
5952 return Py_Type(obj
);
5955 /* Try the slow way */
5956 static PyObject
*class_str
= NULL
;
5957 PyObject
*class_attr
;
5959 if (class_str
== NULL
) {
5960 class_str
= PyString_FromString("__class__");
5961 if (class_str
== NULL
)
5965 class_attr
= PyObject_GetAttr(obj
, class_str
);
5967 if (class_attr
!= NULL
&&
5968 PyType_Check(class_attr
) &&
5969 (PyTypeObject
*)class_attr
!= Py_Type(obj
))
5971 int ok
= PyType_IsSubtype(
5972 (PyTypeObject
*)class_attr
, type
);
5974 return (PyTypeObject
*)class_attr
;
5977 if (class_attr
== NULL
)
5980 Py_DECREF(class_attr
);
5983 PyErr_SetString(PyExc_TypeError
,
5984 "super(type, obj): "
5985 "obj must be an instance or subtype of type");
5990 super_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
5992 superobject
*su
= (superobject
*)self
;
5993 superobject
*newobj
;
5995 if (obj
== NULL
|| obj
== Py_None
|| su
->obj
!= NULL
) {
5996 /* Not binding to an object, or already bound */
6000 if (Py_Type(su
) != &PySuper_Type
)
6001 /* If su is an instance of a (strict) subclass of super,
6003 return PyObject_CallFunctionObjArgs((PyObject
*)Py_Type(su
),
6004 su
->type
, obj
, NULL
);
6006 /* Inline the common case */
6007 PyTypeObject
*obj_type
= supercheck(su
->type
, obj
);
6008 if (obj_type
== NULL
)
6010 newobj
= (superobject
*)PySuper_Type
.tp_new(&PySuper_Type
,
6014 Py_INCREF(su
->type
);
6016 newobj
->type
= su
->type
;
6018 newobj
->obj_type
= obj_type
;
6019 return (PyObject
*)newobj
;
6024 super_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
6026 superobject
*su
= (superobject
*)self
;
6028 PyObject
*obj
= NULL
;
6029 PyTypeObject
*obj_type
= NULL
;
6031 if (!_PyArg_NoKeywords("super", kwds
))
6033 if (!PyArg_ParseTuple(args
, "O!|O:super", &PyType_Type
, &type
, &obj
))
6038 obj_type
= supercheck(type
, obj
);
6039 if (obj_type
== NULL
)
6046 su
->obj_type
= obj_type
;
6050 PyDoc_STRVAR(super_doc
,
6051 "super(type) -> unbound super object\n"
6052 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
6053 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
6054 "Typical use to call a cooperative superclass method:\n"
6056 " def meth(self, arg):\n"
6057 " super(C, self).meth(arg)");
6060 super_traverse(PyObject
*self
, visitproc visit
, void *arg
)
6062 superobject
*su
= (superobject
*)self
;
6066 Py_VISIT(su
->obj_type
);
6071 PyTypeObject PySuper_Type
= {
6072 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
6073 "super", /* tp_name */
6074 sizeof(superobject
), /* tp_basicsize */
6075 0, /* tp_itemsize */
6077 super_dealloc
, /* tp_dealloc */
6082 super_repr
, /* tp_repr */
6083 0, /* tp_as_number */
6084 0, /* tp_as_sequence */
6085 0, /* tp_as_mapping */
6089 super_getattro
, /* tp_getattro */
6090 0, /* tp_setattro */
6091 0, /* tp_as_buffer */
6092 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
6093 Py_TPFLAGS_BASETYPE
, /* tp_flags */
6094 super_doc
, /* tp_doc */
6095 super_traverse
, /* tp_traverse */
6097 0, /* tp_richcompare */
6098 0, /* tp_weaklistoffset */
6100 0, /* tp_iternext */
6102 super_members
, /* tp_members */
6106 super_descr_get
, /* tp_descr_get */
6107 0, /* tp_descr_set */
6108 0, /* tp_dictoffset */
6109 super_init
, /* tp_init */
6110 PyType_GenericAlloc
, /* tp_alloc */
6111 PyType_GenericNew
, /* tp_new */
6112 PyObject_GC_Del
, /* tp_free */