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
, value
->ob_type
->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
, value
->ob_type
->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
, ob
->ob_type
->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 (result
->ob_type
->tp_descr_get
) {
347 result
= result
->ob_type
->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
= self
->ob_type
;
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
= self
->ob_type
;
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
= self
->ob_type
;
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(base
->ob_size
== 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(self
->ob_type
, *attrobj
);
883 if ((f
= res
->ob_type
->tp_descr_get
) == NULL
)
886 res
= f(res
, self
, (PyObject
*)(self
->ob_type
));
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 (type
->ob_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 cls
->ob_type
->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 t_size
-= sizeof(PyObject
*);
1428 if (type
->tp_dictoffset
&& base
->tp_dictoffset
== 0 &&
1429 type
->tp_dictoffset
+ sizeof(PyObject
*) == t_size
)
1430 t_size
-= sizeof(PyObject
*);
1432 return t_size
!= b_size
;
1435 static PyTypeObject
*
1436 solid_base(PyTypeObject
*type
)
1441 base
= solid_base(type
->tp_base
);
1443 base
= &PyBaseObject_Type
;
1444 if (extra_ivars(type
, base
))
1450 static void object_dealloc(PyObject
*);
1451 static int object_init(PyObject
*, PyObject
*, PyObject
*);
1452 static int update_slot(PyTypeObject
*, PyObject
*);
1453 static void fixup_slot_dispatchers(PyTypeObject
*);
1456 subtype_dict(PyObject
*obj
, void *context
)
1458 PyObject
**dictptr
= _PyObject_GetDictPtr(obj
);
1461 if (dictptr
== NULL
) {
1462 PyErr_SetString(PyExc_AttributeError
,
1463 "This object has no __dict__");
1468 *dictptr
= dict
= PyDict_New();
1474 subtype_setdict(PyObject
*obj
, PyObject
*value
, void *context
)
1476 PyObject
**dictptr
= _PyObject_GetDictPtr(obj
);
1479 if (dictptr
== NULL
) {
1480 PyErr_SetString(PyExc_AttributeError
,
1481 "This object has no __dict__");
1484 if (value
!= NULL
&& !PyDict_Check(value
)) {
1485 PyErr_Format(PyExc_TypeError
,
1486 "__dict__ must be set to a dictionary, "
1487 "not a '%.200s'", value
->ob_type
->tp_name
);
1498 subtype_getweakref(PyObject
*obj
, void *context
)
1500 PyObject
**weaklistptr
;
1503 if (obj
->ob_type
->tp_weaklistoffset
== 0) {
1504 PyErr_SetString(PyExc_AttributeError
,
1505 "This object has no __weakref__");
1508 assert(obj
->ob_type
->tp_weaklistoffset
> 0);
1509 assert(obj
->ob_type
->tp_weaklistoffset
+ sizeof(PyObject
*) <=
1510 (size_t)(obj
->ob_type
->tp_basicsize
));
1511 weaklistptr
= (PyObject
**)
1512 ((char *)obj
+ obj
->ob_type
->tp_weaklistoffset
);
1513 if (*weaklistptr
== NULL
)
1516 result
= *weaklistptr
;
1521 /* Three variants on the subtype_getsets list. */
1523 static PyGetSetDef subtype_getsets_full
[] = {
1524 {"__dict__", subtype_dict
, subtype_setdict
,
1525 PyDoc_STR("dictionary for instance variables (if defined)")},
1526 {"__weakref__", subtype_getweakref
, NULL
,
1527 PyDoc_STR("list of weak references to the object (if defined)")},
1531 static PyGetSetDef subtype_getsets_dict_only
[] = {
1532 {"__dict__", subtype_dict
, subtype_setdict
,
1533 PyDoc_STR("dictionary for instance variables (if defined)")},
1537 static PyGetSetDef subtype_getsets_weakref_only
[] = {
1538 {"__weakref__", subtype_getweakref
, NULL
,
1539 PyDoc_STR("list of weak references to the object (if defined)")},
1544 valid_identifier(PyObject
*s
)
1549 if (!PyString_Check(s
)) {
1550 PyErr_Format(PyExc_TypeError
,
1551 "__slots__ items must be strings, not '%.200s'",
1552 s
->ob_type
->tp_name
);
1555 p
= (unsigned char *) PyString_AS_STRING(s
);
1556 n
= PyString_GET_SIZE(s
);
1557 /* We must reject an empty name. As a hack, we bump the
1558 length to 1 so that the loop will balk on the trailing \0. */
1561 for (i
= 0; i
< n
; i
++, p
++) {
1562 if (!(i
== 0 ? isalpha(*p
) : isalnum(*p
)) && *p
!= '_') {
1563 PyErr_SetString(PyExc_TypeError
,
1564 "__slots__ must be identifiers");
1571 #ifdef Py_USING_UNICODE
1572 /* Replace Unicode objects in slots. */
1575 _unicode_to_string(PyObject
*slots
, Py_ssize_t nslots
)
1577 PyObject
*tmp
= NULL
;
1578 PyObject
*slot_name
, *new_name
;
1581 for (i
= 0; i
< nslots
; i
++) {
1582 if (PyUnicode_Check(slot_name
= PyTuple_GET_ITEM(slots
, i
))) {
1584 tmp
= PySequence_List(slots
);
1588 new_name
= _PyUnicode_AsDefaultEncodedString(slot_name
,
1590 if (new_name
== NULL
) {
1594 Py_INCREF(new_name
);
1595 PyList_SET_ITEM(tmp
, i
, new_name
);
1596 Py_DECREF(slot_name
);
1600 slots
= PyList_AsTuple(tmp
);
1609 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
);
1612 type_init(PyObject
*cls
, PyObject
*args
, PyObject
*kwds
)
1616 assert(args
!= NULL
&& PyTuple_Check(args
));
1617 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1619 if (kwds
!= NULL
&& PyDict_Check(kwds
) && PyDict_Size(kwds
) != 0) {
1620 PyErr_SetString(PyExc_TypeError
,
1621 "type.__init__() takes no keyword arguments");
1625 if (args
!= NULL
&& PyTuple_Check(args
) &&
1626 (PyTuple_GET_SIZE(args
) != 1 && PyTuple_GET_SIZE(args
) != 3)) {
1627 PyErr_SetString(PyExc_TypeError
,
1628 "type.__init__() takes 1 or 3 arguments");
1632 /* Call object.__init__(self) now. */
1633 /* XXX Could call super(type, cls).__init__() but what's the point? */
1634 args
= PyTuple_GetSlice(args
, 0, 0);
1635 res
= object_init(cls
, args
, NULL
);
1641 type_new(PyTypeObject
*metatype
, PyObject
*args
, PyObject
*kwds
)
1643 PyObject
*name
, *bases
, *dict
;
1644 static char *kwlist
[] = {"name", "bases", "dict", 0};
1645 PyObject
*slots
, *tmp
, *newslots
;
1646 PyTypeObject
*type
, *base
, *tmptype
, *winner
;
1647 PyHeapTypeObject
*et
;
1649 Py_ssize_t i
, nbases
, nslots
, slotoffset
, add_dict
, add_weak
;
1650 int j
, may_add_dict
, may_add_weak
;
1652 assert(args
!= NULL
&& PyTuple_Check(args
));
1653 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1655 /* Special case: type(x) should return x->ob_type */
1657 const Py_ssize_t nargs
= PyTuple_GET_SIZE(args
);
1658 const Py_ssize_t nkwds
= kwds
== NULL
? 0 : PyDict_Size(kwds
);
1660 if (PyType_CheckExact(metatype
) && nargs
== 1 && nkwds
== 0) {
1661 PyObject
*x
= PyTuple_GET_ITEM(args
, 0);
1662 Py_INCREF(x
->ob_type
);
1663 return (PyObject
*) x
->ob_type
;
1666 /* SF bug 475327 -- if that didn't trigger, we need 3
1667 arguments. but PyArg_ParseTupleAndKeywords below may give
1668 a msg saying type() needs exactly 3. */
1669 if (nargs
+ nkwds
!= 3) {
1670 PyErr_SetString(PyExc_TypeError
,
1671 "type() takes 1 or 3 arguments");
1676 /* Check arguments: (name, bases, dict) */
1677 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "SO!O!:type", kwlist
,
1679 &PyTuple_Type
, &bases
,
1680 &PyDict_Type
, &dict
))
1683 /* Determine the proper metatype to deal with this,
1684 and check for metatype conflicts while we're at it.
1685 Note that if some other metatype wins to contract,
1686 it's possible that its instances are not types. */
1687 nbases
= PyTuple_GET_SIZE(bases
);
1689 for (i
= 0; i
< nbases
; i
++) {
1690 tmp
= PyTuple_GET_ITEM(bases
, i
);
1691 tmptype
= tmp
->ob_type
;
1692 if (tmptype
== &PyClass_Type
)
1693 continue; /* Special case classic classes */
1694 if (PyType_IsSubtype(winner
, tmptype
))
1696 if (PyType_IsSubtype(tmptype
, winner
)) {
1700 PyErr_SetString(PyExc_TypeError
,
1701 "metaclass conflict: "
1702 "the metaclass of a derived class "
1703 "must be a (non-strict) subclass "
1704 "of the metaclasses of all its bases");
1707 if (winner
!= metatype
) {
1708 if (winner
->tp_new
!= type_new
) /* Pass it to the winner */
1709 return winner
->tp_new(winner
, args
, kwds
);
1713 /* Adjust for empty tuple bases */
1715 bases
= PyTuple_Pack(1, &PyBaseObject_Type
);
1723 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1725 /* Calculate best base, and check that all bases are type objects */
1726 base
= best_base(bases
);
1731 if (!PyType_HasFeature(base
, Py_TPFLAGS_BASETYPE
)) {
1732 PyErr_Format(PyExc_TypeError
,
1733 "type '%.100s' is not an acceptable base type",
1739 /* Check for a __slots__ sequence variable in dict, and count it */
1740 slots
= PyDict_GetItemString(dict
, "__slots__");
1744 may_add_dict
= base
->tp_dictoffset
== 0;
1745 may_add_weak
= base
->tp_weaklistoffset
== 0 && base
->tp_itemsize
== 0;
1746 if (slots
== NULL
) {
1757 /* Make it into a tuple */
1758 if (PyString_Check(slots
) || PyUnicode_Check(slots
))
1759 slots
= PyTuple_Pack(1, slots
);
1761 slots
= PySequence_Tuple(slots
);
1762 if (slots
== NULL
) {
1766 assert(PyTuple_Check(slots
));
1768 /* Are slots allowed? */
1769 nslots
= PyTuple_GET_SIZE(slots
);
1770 if (nslots
> 0 && base
->tp_itemsize
!= 0) {
1771 PyErr_Format(PyExc_TypeError
,
1772 "nonempty __slots__ "
1773 "not supported for subtype of '%s'",
1781 #ifdef Py_USING_UNICODE
1782 tmp
= _unicode_to_string(slots
, nslots
);
1790 /* Check for valid slot names and two special cases */
1791 for (i
= 0; i
< nslots
; i
++) {
1792 PyObject
*tmp
= PyTuple_GET_ITEM(slots
, i
);
1794 if (!valid_identifier(tmp
))
1796 assert(PyString_Check(tmp
));
1797 s
= PyString_AS_STRING(tmp
);
1798 if (strcmp(s
, "__dict__") == 0) {
1799 if (!may_add_dict
|| add_dict
) {
1800 PyErr_SetString(PyExc_TypeError
,
1801 "__dict__ slot disallowed: "
1802 "we already got one");
1807 if (strcmp(s
, "__weakref__") == 0) {
1808 if (!may_add_weak
|| add_weak
) {
1809 PyErr_SetString(PyExc_TypeError
,
1810 "__weakref__ slot disallowed: "
1811 "either we already got one, "
1812 "or __itemsize__ != 0");
1819 /* Copy slots into a list, mangle names and sort them.
1820 Sorted names are needed for __class__ assignment.
1821 Convert them back to tuple at the end.
1823 newslots
= PyList_New(nslots
- add_dict
- add_weak
);
1824 if (newslots
== NULL
)
1826 for (i
= j
= 0; i
< nslots
; i
++) {
1828 tmp
= PyTuple_GET_ITEM(slots
, i
);
1829 s
= PyString_AS_STRING(tmp
);
1830 if ((add_dict
&& strcmp(s
, "__dict__") == 0) ||
1831 (add_weak
&& strcmp(s
, "__weakref__") == 0))
1833 tmp
=_Py_Mangle(name
, tmp
);
1836 PyList_SET_ITEM(newslots
, j
, tmp
);
1839 assert(j
== nslots
- add_dict
- add_weak
);
1842 if (PyList_Sort(newslots
) == -1) {
1844 Py_DECREF(newslots
);
1847 slots
= PyList_AsTuple(newslots
);
1848 Py_DECREF(newslots
);
1849 if (slots
== NULL
) {
1854 /* Secondary bases may provide weakrefs or dict */
1856 ((may_add_dict
&& !add_dict
) ||
1857 (may_add_weak
&& !add_weak
))) {
1858 for (i
= 0; i
< nbases
; i
++) {
1859 tmp
= PyTuple_GET_ITEM(bases
, i
);
1860 if (tmp
== (PyObject
*)base
)
1861 continue; /* Skip primary base */
1862 if (PyClass_Check(tmp
)) {
1863 /* Classic base class provides both */
1864 if (may_add_dict
&& !add_dict
)
1866 if (may_add_weak
&& !add_weak
)
1870 assert(PyType_Check(tmp
));
1871 tmptype
= (PyTypeObject
*)tmp
;
1872 if (may_add_dict
&& !add_dict
&&
1873 tmptype
->tp_dictoffset
!= 0)
1875 if (may_add_weak
&& !add_weak
&&
1876 tmptype
->tp_weaklistoffset
!= 0)
1878 if (may_add_dict
&& !add_dict
)
1880 if (may_add_weak
&& !add_weak
)
1882 /* Nothing more to check */
1888 /* XXX From here until type is safely allocated,
1889 "return NULL" may leak slots! */
1891 /* Allocate the type object */
1892 type
= (PyTypeObject
*)metatype
->tp_alloc(metatype
, nslots
);
1899 /* Keep name and slots alive in the extended type object */
1900 et
= (PyHeapTypeObject
*)type
;
1903 et
->ht_slots
= slots
;
1905 /* Initialize tp_flags */
1906 type
->tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HEAPTYPE
|
1907 Py_TPFLAGS_BASETYPE
;
1908 if (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)
1909 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
1911 /* It's a new-style number unless it specifically inherits any
1912 old-style numeric behavior */
1913 if ((base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) ||
1914 (base
->tp_as_number
== NULL
))
1915 type
->tp_flags
|= Py_TPFLAGS_CHECKTYPES
;
1917 /* Initialize essential fields */
1918 type
->tp_as_number
= &et
->as_number
;
1919 type
->tp_as_sequence
= &et
->as_sequence
;
1920 type
->tp_as_mapping
= &et
->as_mapping
;
1921 type
->tp_as_buffer
= &et
->as_buffer
;
1922 type
->tp_name
= PyString_AS_STRING(name
);
1924 /* Set tp_base and tp_bases */
1925 type
->tp_bases
= bases
;
1927 type
->tp_base
= base
;
1929 /* Initialize tp_dict from passed-in dict */
1930 type
->tp_dict
= dict
= PyDict_Copy(dict
);
1936 /* Set __module__ in the dict */
1937 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
1938 tmp
= PyEval_GetGlobals();
1940 tmp
= PyDict_GetItemString(tmp
, "__name__");
1942 if (PyDict_SetItemString(dict
, "__module__",
1949 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1950 and is a string. The __doc__ accessor will first look for tp_doc;
1951 if that fails, it will still look into __dict__.
1954 PyObject
*doc
= PyDict_GetItemString(dict
, "__doc__");
1955 if (doc
!= NULL
&& PyString_Check(doc
)) {
1956 const size_t n
= (size_t)PyString_GET_SIZE(doc
);
1957 char *tp_doc
= (char *)PyObject_MALLOC(n
+1);
1958 if (tp_doc
== NULL
) {
1962 memcpy(tp_doc
, PyString_AS_STRING(doc
), n
+1);
1963 type
->tp_doc
= tp_doc
;
1967 /* Special-case __new__: if it's a plain function,
1968 make it a static function */
1969 tmp
= PyDict_GetItemString(dict
, "__new__");
1970 if (tmp
!= NULL
&& PyFunction_Check(tmp
)) {
1971 tmp
= PyStaticMethod_New(tmp
);
1976 PyDict_SetItemString(dict
, "__new__", tmp
);
1980 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1981 mp
= PyHeapType_GET_MEMBERS(et
);
1982 slotoffset
= base
->tp_basicsize
;
1983 if (slots
!= NULL
) {
1984 for (i
= 0; i
< nslots
; i
++, mp
++) {
1985 mp
->name
= PyString_AS_STRING(
1986 PyTuple_GET_ITEM(slots
, i
));
1987 mp
->type
= T_OBJECT_EX
;
1988 mp
->offset
= slotoffset
;
1990 /* __dict__ and __weakref__ are already filtered out */
1991 assert(strcmp(mp
->name
, "__dict__") != 0);
1992 assert(strcmp(mp
->name
, "__weakref__") != 0);
1994 slotoffset
+= sizeof(PyObject
*);
1998 if (base
->tp_itemsize
)
1999 type
->tp_dictoffset
= -(long)sizeof(PyObject
*);
2001 type
->tp_dictoffset
= slotoffset
;
2002 slotoffset
+= sizeof(PyObject
*);
2005 assert(!base
->tp_itemsize
);
2006 type
->tp_weaklistoffset
= slotoffset
;
2007 slotoffset
+= sizeof(PyObject
*);
2009 type
->tp_basicsize
= slotoffset
;
2010 type
->tp_itemsize
= base
->tp_itemsize
;
2011 type
->tp_members
= PyHeapType_GET_MEMBERS(et
);
2013 if (type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2014 type
->tp_getset
= subtype_getsets_full
;
2015 else if (type
->tp_weaklistoffset
&& !type
->tp_dictoffset
)
2016 type
->tp_getset
= subtype_getsets_weakref_only
;
2017 else if (!type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2018 type
->tp_getset
= subtype_getsets_dict_only
;
2020 type
->tp_getset
= NULL
;
2022 /* Special case some slots */
2023 if (type
->tp_dictoffset
!= 0 || nslots
> 0) {
2024 if (base
->tp_getattr
== NULL
&& base
->tp_getattro
== NULL
)
2025 type
->tp_getattro
= PyObject_GenericGetAttr
;
2026 if (base
->tp_setattr
== NULL
&& base
->tp_setattro
== NULL
)
2027 type
->tp_setattro
= PyObject_GenericSetAttr
;
2029 type
->tp_dealloc
= subtype_dealloc
;
2031 /* Enable GC unless there are really no instance variables possible */
2032 if (!(type
->tp_basicsize
== sizeof(PyObject
) &&
2033 type
->tp_itemsize
== 0))
2034 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
2036 /* Always override allocation strategy to use regular heap */
2037 type
->tp_alloc
= PyType_GenericAlloc
;
2038 if (type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) {
2039 type
->tp_free
= PyObject_GC_Del
;
2040 type
->tp_traverse
= subtype_traverse
;
2041 type
->tp_clear
= subtype_clear
;
2044 type
->tp_free
= PyObject_Del
;
2046 /* Initialize the rest */
2047 if (PyType_Ready(type
) < 0) {
2052 /* Put the proper slots in place */
2053 fixup_slot_dispatchers(type
);
2055 return (PyObject
*)type
;
2058 /* Internal API to look for a name through the MRO.
2059 This returns a borrowed reference, and doesn't set an exception! */
2061 _PyType_Lookup(PyTypeObject
*type
, PyObject
*name
)
2064 PyObject
*mro
, *res
, *base
, *dict
;
2066 /* Look in tp_dict of types in MRO */
2069 /* If mro is NULL, the type is either not yet initialized
2070 by PyType_Ready(), or already cleared by type_clear().
2071 Either way the safest thing to do is to return NULL. */
2075 assert(PyTuple_Check(mro
));
2076 n
= PyTuple_GET_SIZE(mro
);
2077 for (i
= 0; i
< n
; i
++) {
2078 base
= PyTuple_GET_ITEM(mro
, i
);
2079 if (PyClass_Check(base
))
2080 dict
= ((PyClassObject
*)base
)->cl_dict
;
2082 assert(PyType_Check(base
));
2083 dict
= ((PyTypeObject
*)base
)->tp_dict
;
2085 assert(dict
&& PyDict_Check(dict
));
2086 res
= PyDict_GetItem(dict
, name
);
2093 /* This is similar to PyObject_GenericGetAttr(),
2094 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2096 type_getattro(PyTypeObject
*type
, PyObject
*name
)
2098 PyTypeObject
*metatype
= type
->ob_type
;
2099 PyObject
*meta_attribute
, *attribute
;
2100 descrgetfunc meta_get
;
2102 /* Initialize this type (we'll assume the metatype is initialized) */
2103 if (type
->tp_dict
== NULL
) {
2104 if (PyType_Ready(type
) < 0)
2108 /* No readable descriptor found yet */
2111 /* Look for the attribute in the metatype */
2112 meta_attribute
= _PyType_Lookup(metatype
, name
);
2114 if (meta_attribute
!= NULL
) {
2115 meta_get
= meta_attribute
->ob_type
->tp_descr_get
;
2117 if (meta_get
!= NULL
&& PyDescr_IsData(meta_attribute
)) {
2118 /* Data descriptors implement tp_descr_set to intercept
2119 * writes. Assume the attribute is not overridden in
2120 * type's tp_dict (and bases): call the descriptor now.
2122 return meta_get(meta_attribute
, (PyObject
*)type
,
2123 (PyObject
*)metatype
);
2125 Py_INCREF(meta_attribute
);
2128 /* No data descriptor found on metatype. Look in tp_dict of this
2129 * type and its bases */
2130 attribute
= _PyType_Lookup(type
, name
);
2131 if (attribute
!= NULL
) {
2132 /* Implement descriptor functionality, if any */
2133 descrgetfunc local_get
= attribute
->ob_type
->tp_descr_get
;
2135 Py_XDECREF(meta_attribute
);
2137 if (local_get
!= NULL
) {
2138 /* NULL 2nd argument indicates the descriptor was
2139 * found on the target object itself (or a base) */
2140 return local_get(attribute
, (PyObject
*)NULL
,
2144 Py_INCREF(attribute
);
2148 /* No attribute found in local __dict__ (or bases): use the
2149 * descriptor from the metatype, if any */
2150 if (meta_get
!= NULL
) {
2152 res
= meta_get(meta_attribute
, (PyObject
*)type
,
2153 (PyObject
*)metatype
);
2154 Py_DECREF(meta_attribute
);
2158 /* If an ordinary attribute was found on the metatype, return it now */
2159 if (meta_attribute
!= NULL
) {
2160 return meta_attribute
;
2164 PyErr_Format(PyExc_AttributeError
,
2165 "type object '%.50s' has no attribute '%.400s'",
2166 type
->tp_name
, PyString_AS_STRING(name
));
2171 type_setattro(PyTypeObject
*type
, PyObject
*name
, PyObject
*value
)
2173 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
2176 "can't set attributes of built-in/extension type '%s'",
2180 /* XXX Example of how I expect this to be used...
2181 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2184 if (PyObject_GenericSetAttr((PyObject
*)type
, name
, value
) < 0)
2186 return update_slot(type
, name
);
2190 type_dealloc(PyTypeObject
*type
)
2192 PyHeapTypeObject
*et
;
2194 /* Assert this is a heap-allocated type object */
2195 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2196 _PyObject_GC_UNTRACK(type
);
2197 PyObject_ClearWeakRefs((PyObject
*)type
);
2198 et
= (PyHeapTypeObject
*)type
;
2199 Py_XDECREF(type
->tp_base
);
2200 Py_XDECREF(type
->tp_dict
);
2201 Py_XDECREF(type
->tp_bases
);
2202 Py_XDECREF(type
->tp_mro
);
2203 Py_XDECREF(type
->tp_cache
);
2204 Py_XDECREF(type
->tp_subclasses
);
2205 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2206 * of most other objects. It's okay to cast it to char *.
2208 PyObject_Free((char *)type
->tp_doc
);
2209 Py_XDECREF(et
->ht_name
);
2210 Py_XDECREF(et
->ht_slots
);
2211 type
->ob_type
->tp_free((PyObject
*)type
);
2215 type_subclasses(PyTypeObject
*type
, PyObject
*args_ignored
)
2217 PyObject
*list
, *raw
, *ref
;
2220 list
= PyList_New(0);
2223 raw
= type
->tp_subclasses
;
2226 assert(PyList_Check(raw
));
2227 n
= PyList_GET_SIZE(raw
);
2228 for (i
= 0; i
< n
; i
++) {
2229 ref
= PyList_GET_ITEM(raw
, i
);
2230 assert(PyWeakref_CheckRef(ref
));
2231 ref
= PyWeakref_GET_OBJECT(ref
);
2232 if (ref
!= Py_None
) {
2233 if (PyList_Append(list
, ref
) < 0) {
2242 static PyMethodDef type_methods
[] = {
2243 {"mro", (PyCFunction
)mro_external
, METH_NOARGS
,
2244 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2245 {"__subclasses__", (PyCFunction
)type_subclasses
, METH_NOARGS
,
2246 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2250 PyDoc_STRVAR(type_doc
,
2251 "type(object) -> the object's type\n"
2252 "type(name, bases, dict) -> a new type");
2255 type_traverse(PyTypeObject
*type
, visitproc visit
, void *arg
)
2257 /* Because of type_is_gc(), the collector only calls this
2259 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2261 Py_VISIT(type
->tp_dict
);
2262 Py_VISIT(type
->tp_cache
);
2263 Py_VISIT(type
->tp_mro
);
2264 Py_VISIT(type
->tp_bases
);
2265 Py_VISIT(type
->tp_base
);
2267 /* There's no need to visit type->tp_subclasses or
2268 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2269 in cycles; tp_subclasses is a list of weak references,
2270 and slots is a tuple of strings. */
2276 type_clear(PyTypeObject
*type
)
2278 /* Because of type_is_gc(), the collector only calls this
2280 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2282 /* The only field we need to clear is tp_mro, which is part of a
2283 hard cycle (its first element is the class itself) that won't
2284 be broken otherwise (it's a tuple and tuples don't have a
2285 tp_clear handler). None of the other fields need to be
2286 cleared, and here's why:
2289 It is a dict, so the collector will call its tp_clear.
2292 Not used; if it were, it would be a dict.
2295 If these are involved in a cycle, there must be at least
2296 one other, mutable object in the cycle, e.g. a base
2297 class's dict; the cycle will be broken that way.
2300 A list of weak references can't be part of a cycle; and
2301 lists have their own tp_clear.
2303 slots (in PyHeapTypeObject):
2304 A tuple of strings can't be part of a cycle.
2307 Py_CLEAR(type
->tp_mro
);
2313 type_is_gc(PyTypeObject
*type
)
2315 return type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
;
2318 PyTypeObject PyType_Type
= {
2319 PyObject_HEAD_INIT(&PyType_Type
)
2321 "type", /* tp_name */
2322 sizeof(PyHeapTypeObject
), /* tp_basicsize */
2323 sizeof(PyMemberDef
), /* tp_itemsize */
2324 (destructor
)type_dealloc
, /* tp_dealloc */
2328 type_compare
, /* tp_compare */
2329 (reprfunc
)type_repr
, /* tp_repr */
2330 0, /* tp_as_number */
2331 0, /* tp_as_sequence */
2332 0, /* tp_as_mapping */
2333 (hashfunc
)_Py_HashPointer
, /* tp_hash */
2334 (ternaryfunc
)type_call
, /* tp_call */
2336 (getattrofunc
)type_getattro
, /* tp_getattro */
2337 (setattrofunc
)type_setattro
, /* tp_setattro */
2338 0, /* tp_as_buffer */
2339 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2340 Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_TYPE_SUBCLASS
, /* tp_flags */
2341 type_doc
, /* tp_doc */
2342 (traverseproc
)type_traverse
, /* tp_traverse */
2343 (inquiry
)type_clear
, /* tp_clear */
2344 0, /* tp_richcompare */
2345 offsetof(PyTypeObject
, tp_weaklist
), /* tp_weaklistoffset */
2347 0, /* tp_iternext */
2348 type_methods
, /* tp_methods */
2349 type_members
, /* tp_members */
2350 type_getsets
, /* tp_getset */
2353 0, /* tp_descr_get */
2354 0, /* tp_descr_set */
2355 offsetof(PyTypeObject
, tp_dict
), /* tp_dictoffset */
2356 type_init
, /* tp_init */
2358 type_new
, /* tp_new */
2359 PyObject_GC_Del
, /* tp_free */
2360 (inquiry
)type_is_gc
, /* tp_is_gc */
2364 /* The base type of all types (eventually)... except itself. */
2366 /* You may wonder why object.__new__() only complains about arguments
2367 when object.__init__() is not overridden, and vice versa.
2369 Consider the use cases:
2371 1. When neither is overridden, we want to hear complaints about
2372 excess (i.e., any) arguments, since their presence could
2373 indicate there's a bug.
2375 2. When defining an Immutable type, we are likely to override only
2376 __new__(), since __init__() is called too late to initialize an
2377 Immutable object. Since __new__() defines the signature for the
2378 type, it would be a pain to have to override __init__() just to
2379 stop it from complaining about excess arguments.
2381 3. When defining a Mutable type, we are likely to override only
2382 __init__(). So here the converse reasoning applies: we don't
2383 want to have to override __new__() just to stop it from
2386 4. When __init__() is overridden, and the subclass __init__() calls
2387 object.__init__(), the latter should complain about excess
2388 arguments; ditto for __new__().
2390 Use cases 2 and 3 make it unattractive to unconditionally check for
2391 excess arguments. The best solution that addresses all four use
2392 cases is as follows: __init__() complains about excess arguments
2393 unless __new__() is overridden and __init__() is not overridden
2394 (IOW, if __init__() is overridden or __new__() is not overridden);
2395 symmetrically, __new__() complains about excess arguments unless
2396 __init__() is overridden and __new__() is not overridden
2397 (IOW, if __new__() is overridden or __init__() is not overridden).
2399 However, for backwards compatibility, this breaks too much code.
2400 Therefore, in 2.6, we'll *warn* about excess arguments when both
2401 methods are overridden; for all other cases we'll use the above
2408 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
2411 excess_args(PyObject
*args
, PyObject
*kwds
)
2413 return PyTuple_GET_SIZE(args
) ||
2414 (kwds
&& PyDict_Check(kwds
) && PyDict_Size(kwds
));
2418 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2421 if (excess_args(args
, kwds
)) {
2422 PyTypeObject
*type
= self
->ob_type
;
2423 if (type
->tp_init
!= object_init
&&
2424 type
->tp_new
!= object_new
)
2426 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2427 "object.__init__() takes no parameters",
2430 else if (type
->tp_init
!= object_init
||
2431 type
->tp_new
== object_new
)
2433 PyErr_SetString(PyExc_TypeError
,
2434 "object.__init__() takes no parameters");
2442 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2445 if (excess_args(args
, kwds
)) {
2446 if (type
->tp_new
!= object_new
&&
2447 type
->tp_init
!= object_init
)
2449 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2450 "object.__new__() takes no parameters",
2453 else if (type
->tp_new
!= object_new
||
2454 type
->tp_init
== object_init
)
2456 PyErr_SetString(PyExc_TypeError
,
2457 "object.__new__() takes no parameters");
2463 return type
->tp_alloc(type
, 0);
2467 object_dealloc(PyObject
*self
)
2469 self
->ob_type
->tp_free(self
);
2473 object_repr(PyObject
*self
)
2476 PyObject
*mod
, *name
, *rtn
;
2478 type
= self
->ob_type
;
2479 mod
= type_module(type
, NULL
);
2482 else if (!PyString_Check(mod
)) {
2486 name
= type_name(type
, NULL
);
2489 if (mod
!= NULL
&& strcmp(PyString_AS_STRING(mod
), "__builtin__"))
2490 rtn
= PyString_FromFormat("<%s.%s object at %p>",
2491 PyString_AS_STRING(mod
),
2492 PyString_AS_STRING(name
),
2495 rtn
= PyString_FromFormat("<%s object at %p>",
2496 type
->tp_name
, self
);
2503 object_str(PyObject
*self
)
2507 f
= self
->ob_type
->tp_repr
;
2514 object_hash(PyObject
*self
)
2516 return _Py_HashPointer(self
);
2520 object_get_class(PyObject
*self
, void *closure
)
2522 Py_INCREF(self
->ob_type
);
2523 return (PyObject
*)(self
->ob_type
);
2527 equiv_structs(PyTypeObject
*a
, PyTypeObject
*b
)
2532 a
->tp_basicsize
== b
->tp_basicsize
&&
2533 a
->tp_itemsize
== b
->tp_itemsize
&&
2534 a
->tp_dictoffset
== b
->tp_dictoffset
&&
2535 a
->tp_weaklistoffset
== b
->tp_weaklistoffset
&&
2536 ((a
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
2537 (b
->tp_flags
& Py_TPFLAGS_HAVE_GC
)));
2541 same_slots_added(PyTypeObject
*a
, PyTypeObject
*b
)
2543 PyTypeObject
*base
= a
->tp_base
;
2545 PyObject
*slots_a
, *slots_b
;
2547 if (base
!= b
->tp_base
)
2549 if (equiv_structs(a
, base
) && equiv_structs(b
, base
))
2551 size
= base
->tp_basicsize
;
2552 if (a
->tp_dictoffset
== size
&& b
->tp_dictoffset
== size
)
2553 size
+= sizeof(PyObject
*);
2554 if (a
->tp_weaklistoffset
== size
&& b
->tp_weaklistoffset
== size
)
2555 size
+= sizeof(PyObject
*);
2557 /* Check slots compliance */
2558 slots_a
= ((PyHeapTypeObject
*)a
)->ht_slots
;
2559 slots_b
= ((PyHeapTypeObject
*)b
)->ht_slots
;
2560 if (slots_a
&& slots_b
) {
2561 if (PyObject_Compare(slots_a
, slots_b
) != 0)
2563 size
+= sizeof(PyObject
*) * PyTuple_GET_SIZE(slots_a
);
2565 return size
== a
->tp_basicsize
&& size
== b
->tp_basicsize
;
2569 compatible_for_assignment(PyTypeObject
* oldto
, PyTypeObject
* newto
, char* attr
)
2571 PyTypeObject
*newbase
, *oldbase
;
2573 if (newto
->tp_dealloc
!= oldto
->tp_dealloc
||
2574 newto
->tp_free
!= oldto
->tp_free
)
2576 PyErr_Format(PyExc_TypeError
,
2578 "'%s' deallocator differs from '%s'",
2586 while (equiv_structs(newbase
, newbase
->tp_base
))
2587 newbase
= newbase
->tp_base
;
2588 while (equiv_structs(oldbase
, oldbase
->tp_base
))
2589 oldbase
= oldbase
->tp_base
;
2590 if (newbase
!= oldbase
&&
2591 (newbase
->tp_base
!= oldbase
->tp_base
||
2592 !same_slots_added(newbase
, oldbase
))) {
2593 PyErr_Format(PyExc_TypeError
,
2595 "'%s' object layout differs from '%s'",
2606 object_set_class(PyObject
*self
, PyObject
*value
, void *closure
)
2608 PyTypeObject
*oldto
= self
->ob_type
;
2609 PyTypeObject
*newto
;
2611 if (value
== NULL
) {
2612 PyErr_SetString(PyExc_TypeError
,
2613 "can't delete __class__ attribute");
2616 if (!PyType_Check(value
)) {
2617 PyErr_Format(PyExc_TypeError
,
2618 "__class__ must be set to new-style class, not '%s' object",
2619 value
->ob_type
->tp_name
);
2622 newto
= (PyTypeObject
*)value
;
2623 if (!(newto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) ||
2624 !(oldto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
2626 PyErr_Format(PyExc_TypeError
,
2627 "__class__ assignment: only for heap types");
2630 if (compatible_for_assignment(newto
, oldto
, "__class__")) {
2632 self
->ob_type
= newto
;
2641 static PyGetSetDef object_getsets
[] = {
2642 {"__class__", object_get_class
, object_set_class
,
2643 PyDoc_STR("the object's class")},
2648 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2649 We fall back to helpers in copy_reg for:
2650 - pickle protocols < 2
2651 - calculating the list of slot names (done only once per class)
2652 - the __newobj__ function (which is used as a token but never called)
2656 import_copy_reg(void)
2658 static PyObject
*copy_reg_str
;
2660 if (!copy_reg_str
) {
2661 copy_reg_str
= PyString_InternFromString("copy_reg");
2662 if (copy_reg_str
== NULL
)
2666 return PyImport_Import(copy_reg_str
);
2670 slotnames(PyObject
*cls
)
2674 PyObject
*slotnames
;
2676 if (!PyType_Check(cls
)) {
2681 clsdict
= ((PyTypeObject
*)cls
)->tp_dict
;
2682 slotnames
= PyDict_GetItemString(clsdict
, "__slotnames__");
2683 if (slotnames
!= NULL
&& PyList_Check(slotnames
)) {
2684 Py_INCREF(slotnames
);
2688 copy_reg
= import_copy_reg();
2689 if (copy_reg
== NULL
)
2692 slotnames
= PyObject_CallMethod(copy_reg
, "_slotnames", "O", cls
);
2693 Py_DECREF(copy_reg
);
2694 if (slotnames
!= NULL
&&
2695 slotnames
!= Py_None
&&
2696 !PyList_Check(slotnames
))
2698 PyErr_SetString(PyExc_TypeError
,
2699 "copy_reg._slotnames didn't return a list or None");
2700 Py_DECREF(slotnames
);
2708 reduce_2(PyObject
*obj
)
2710 PyObject
*cls
, *getnewargs
;
2711 PyObject
*args
= NULL
, *args2
= NULL
;
2712 PyObject
*getstate
= NULL
, *state
= NULL
, *names
= NULL
;
2713 PyObject
*slots
= NULL
, *listitems
= NULL
, *dictitems
= NULL
;
2714 PyObject
*copy_reg
= NULL
, *newobj
= NULL
, *res
= NULL
;
2717 cls
= PyObject_GetAttrString(obj
, "__class__");
2721 getnewargs
= PyObject_GetAttrString(obj
, "__getnewargs__");
2722 if (getnewargs
!= NULL
) {
2723 args
= PyObject_CallObject(getnewargs
, NULL
);
2724 Py_DECREF(getnewargs
);
2725 if (args
!= NULL
&& !PyTuple_Check(args
)) {
2726 PyErr_Format(PyExc_TypeError
,
2727 "__getnewargs__ should return a tuple, "
2728 "not '%.200s'", args
->ob_type
->tp_name
);
2734 args
= PyTuple_New(0);
2739 getstate
= PyObject_GetAttrString(obj
, "__getstate__");
2740 if (getstate
!= NULL
) {
2741 state
= PyObject_CallObject(getstate
, NULL
);
2742 Py_DECREF(getstate
);
2748 state
= PyObject_GetAttrString(obj
, "__dict__");
2749 if (state
== NULL
) {
2754 names
= slotnames(cls
);
2757 if (names
!= Py_None
) {
2758 assert(PyList_Check(names
));
2759 slots
= PyDict_New();
2763 /* Can't pre-compute the list size; the list
2764 is stored on the class so accessible to other
2765 threads, which may be run by DECREF */
2766 for (i
= 0; i
< PyList_GET_SIZE(names
); i
++) {
2767 PyObject
*name
, *value
;
2768 name
= PyList_GET_ITEM(names
, i
);
2769 value
= PyObject_GetAttr(obj
, name
);
2773 int err
= PyDict_SetItem(slots
, name
,
2782 state
= Py_BuildValue("(NO)", state
, slots
);
2789 if (!PyList_Check(obj
)) {
2790 listitems
= Py_None
;
2791 Py_INCREF(listitems
);
2794 listitems
= PyObject_GetIter(obj
);
2795 if (listitems
== NULL
)
2799 if (!PyDict_Check(obj
)) {
2800 dictitems
= Py_None
;
2801 Py_INCREF(dictitems
);
2804 dictitems
= PyObject_CallMethod(obj
, "iteritems", "");
2805 if (dictitems
== NULL
)
2809 copy_reg
= import_copy_reg();
2810 if (copy_reg
== NULL
)
2812 newobj
= PyObject_GetAttrString(copy_reg
, "__newobj__");
2816 n
= PyTuple_GET_SIZE(args
);
2817 args2
= PyTuple_New(n
+1);
2820 PyTuple_SET_ITEM(args2
, 0, cls
);
2822 for (i
= 0; i
< n
; i
++) {
2823 PyObject
*v
= PyTuple_GET_ITEM(args
, i
);
2825 PyTuple_SET_ITEM(args2
, i
+1, v
);
2828 res
= PyTuple_Pack(5, newobj
, args2
, state
, listitems
, dictitems
);
2837 Py_XDECREF(listitems
);
2838 Py_XDECREF(dictitems
);
2839 Py_XDECREF(copy_reg
);
2845 * There were two problems when object.__reduce__ and object.__reduce_ex__
2846 * were implemented in the same function:
2847 * - trying to pickle an object with a custom __reduce__ method that
2848 * fell back to object.__reduce__ in certain circumstances led to
2849 * infinite recursion at Python level and eventual RuntimeError.
2850 * - Pickling objects that lied about their type by overwriting the
2851 * __class__ descriptor could lead to infinite recursion at C level
2852 * and eventual segfault.
2854 * Because of backwards compatibility, the two methods still have to
2855 * behave in the same way, even if this is not required by the pickle
2856 * protocol. This common functionality was moved to the _common_reduce
2860 _common_reduce(PyObject
*self
, int proto
)
2862 PyObject
*copy_reg
, *res
;
2865 return reduce_2(self
);
2867 copy_reg
= import_copy_reg();
2871 res
= PyEval_CallMethod(copy_reg
, "_reduce_ex", "(Oi)", self
, proto
);
2872 Py_DECREF(copy_reg
);
2878 object_reduce(PyObject
*self
, PyObject
*args
)
2882 if (!PyArg_ParseTuple(args
, "|i:__reduce__", &proto
))
2885 return _common_reduce(self
, proto
);
2889 object_reduce_ex(PyObject
*self
, PyObject
*args
)
2891 PyObject
*reduce
, *res
;
2894 if (!PyArg_ParseTuple(args
, "|i:__reduce_ex__", &proto
))
2897 reduce
= PyObject_GetAttrString(self
, "__reduce__");
2901 PyObject
*cls
, *clsreduce
, *objreduce
;
2903 cls
= PyObject_GetAttrString(self
, "__class__");
2908 clsreduce
= PyObject_GetAttrString(cls
, "__reduce__");
2910 if (clsreduce
== NULL
) {
2914 objreduce
= PyDict_GetItemString(PyBaseObject_Type
.tp_dict
,
2916 override
= (clsreduce
!= objreduce
);
2917 Py_DECREF(clsreduce
);
2919 res
= PyObject_CallObject(reduce
, NULL
);
2927 return _common_reduce(self
, proto
);
2930 static PyMethodDef object_methods
[] = {
2931 {"__reduce_ex__", object_reduce_ex
, METH_VARARGS
,
2932 PyDoc_STR("helper for pickle")},
2933 {"__reduce__", object_reduce
, METH_VARARGS
,
2934 PyDoc_STR("helper for pickle")},
2939 PyTypeObject PyBaseObject_Type
= {
2940 PyObject_HEAD_INIT(&PyType_Type
)
2942 "object", /* tp_name */
2943 sizeof(PyObject
), /* tp_basicsize */
2944 0, /* tp_itemsize */
2945 object_dealloc
, /* tp_dealloc */
2950 object_repr
, /* tp_repr */
2951 0, /* tp_as_number */
2952 0, /* tp_as_sequence */
2953 0, /* tp_as_mapping */
2954 object_hash
, /* tp_hash */
2956 object_str
, /* tp_str */
2957 PyObject_GenericGetAttr
, /* tp_getattro */
2958 PyObject_GenericSetAttr
, /* tp_setattro */
2959 0, /* tp_as_buffer */
2960 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
2961 PyDoc_STR("The most base type"), /* tp_doc */
2962 0, /* tp_traverse */
2964 0, /* tp_richcompare */
2965 0, /* tp_weaklistoffset */
2967 0, /* tp_iternext */
2968 object_methods
, /* tp_methods */
2970 object_getsets
, /* tp_getset */
2973 0, /* tp_descr_get */
2974 0, /* tp_descr_set */
2975 0, /* tp_dictoffset */
2976 object_init
, /* tp_init */
2977 PyType_GenericAlloc
, /* tp_alloc */
2978 object_new
, /* tp_new */
2979 PyObject_Del
, /* tp_free */
2983 /* Initialize the __dict__ in a type object */
2986 add_methods(PyTypeObject
*type
, PyMethodDef
*meth
)
2988 PyObject
*dict
= type
->tp_dict
;
2990 for (; meth
->ml_name
!= NULL
; meth
++) {
2992 if (PyDict_GetItemString(dict
, meth
->ml_name
) &&
2993 !(meth
->ml_flags
& METH_COEXIST
))
2995 if (meth
->ml_flags
& METH_CLASS
) {
2996 if (meth
->ml_flags
& METH_STATIC
) {
2997 PyErr_SetString(PyExc_ValueError
,
2998 "method cannot be both class and static");
3001 descr
= PyDescr_NewClassMethod(type
, meth
);
3003 else if (meth
->ml_flags
& METH_STATIC
) {
3004 PyObject
*cfunc
= PyCFunction_New(meth
, NULL
);
3007 descr
= PyStaticMethod_New(cfunc
);
3011 descr
= PyDescr_NewMethod(type
, meth
);
3015 if (PyDict_SetItemString(dict
, meth
->ml_name
, descr
) < 0)
3023 add_members(PyTypeObject
*type
, PyMemberDef
*memb
)
3025 PyObject
*dict
= type
->tp_dict
;
3027 for (; memb
->name
!= NULL
; memb
++) {
3029 if (PyDict_GetItemString(dict
, memb
->name
))
3031 descr
= PyDescr_NewMember(type
, memb
);
3034 if (PyDict_SetItemString(dict
, memb
->name
, descr
) < 0)
3042 add_getset(PyTypeObject
*type
, PyGetSetDef
*gsp
)
3044 PyObject
*dict
= type
->tp_dict
;
3046 for (; gsp
->name
!= NULL
; gsp
++) {
3048 if (PyDict_GetItemString(dict
, gsp
->name
))
3050 descr
= PyDescr_NewGetSet(type
, gsp
);
3054 if (PyDict_SetItemString(dict
, gsp
->name
, descr
) < 0)
3062 inherit_special(PyTypeObject
*type
, PyTypeObject
*base
)
3064 Py_ssize_t oldsize
, newsize
;
3066 /* Special flag magic */
3067 if (!type
->tp_as_buffer
&& base
->tp_as_buffer
) {
3068 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_GETCHARBUFFER
;
3070 base
->tp_flags
& Py_TPFLAGS_HAVE_GETCHARBUFFER
;
3072 if (!type
->tp_as_sequence
&& base
->tp_as_sequence
) {
3073 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_SEQUENCE_IN
;
3074 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_HAVE_SEQUENCE_IN
;
3076 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
) !=
3077 (base
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
)) {
3078 if ((!type
->tp_as_number
&& base
->tp_as_number
) ||
3079 (!type
->tp_as_sequence
&& base
->tp_as_sequence
)) {
3080 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_INPLACEOPS
;
3081 if (!type
->tp_as_number
&& !type
->tp_as_sequence
) {
3082 type
->tp_flags
|= base
->tp_flags
&
3083 Py_TPFLAGS_HAVE_INPLACEOPS
;
3088 if (!type
->tp_as_number
&& base
->tp_as_number
) {
3089 type
->tp_flags
&= ~Py_TPFLAGS_CHECKTYPES
;
3090 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
;
3093 /* Copying basicsize is connected to the GC flags */
3094 oldsize
= base
->tp_basicsize
;
3095 newsize
= type
->tp_basicsize
? type
->tp_basicsize
: oldsize
;
3096 if (!(type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3097 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3098 (type
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
/*GC slots exist*/) &&
3099 (!type
->tp_traverse
&& !type
->tp_clear
)) {
3100 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
3101 if (type
->tp_traverse
== NULL
)
3102 type
->tp_traverse
= base
->tp_traverse
;
3103 if (type
->tp_clear
== NULL
)
3104 type
->tp_clear
= base
->tp_clear
;
3106 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3107 /* The condition below could use some explanation.
3108 It appears that tp_new is not inherited for static types
3109 whose base class is 'object'; this seems to be a precaution
3110 so that old extension types don't suddenly become
3111 callable (object.__new__ wouldn't insure the invariants
3112 that the extension type's own factory function ensures).
3113 Heap types, of course, are under our control, so they do
3114 inherit tp_new; static extension types that specify some
3115 other built-in type as the default are considered
3116 new-style-aware so they also inherit object.__new__. */
3117 if (base
!= &PyBaseObject_Type
||
3118 (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
3119 if (type
->tp_new
== NULL
)
3120 type
->tp_new
= base
->tp_new
;
3123 type
->tp_basicsize
= newsize
;
3125 /* Copy other non-function slots */
3128 #define COPYVAL(SLOT) \
3129 if (type->SLOT == 0) type->SLOT = base->SLOT
3131 COPYVAL(tp_itemsize
);
3132 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_WEAKREFS
) {
3133 COPYVAL(tp_weaklistoffset
);
3135 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3136 COPYVAL(tp_dictoffset
);
3139 /* Setup fast subclass flags */
3140 if (PyType_IsSubtype(base
, (PyTypeObject
*)PyExc_BaseException
))
3141 type
->tp_flags
|= Py_TPFLAGS_BASE_EXC_SUBCLASS
;
3142 else if (PyType_IsSubtype(base
, &PyType_Type
))
3143 type
->tp_flags
|= Py_TPFLAGS_TYPE_SUBCLASS
;
3144 else if (PyType_IsSubtype(base
, &PyInt_Type
))
3145 type
->tp_flags
|= Py_TPFLAGS_INT_SUBCLASS
;
3146 else if (PyType_IsSubtype(base
, &PyLong_Type
))
3147 type
->tp_flags
|= Py_TPFLAGS_LONG_SUBCLASS
;
3148 else if (PyType_IsSubtype(base
, &PyString_Type
))
3149 type
->tp_flags
|= Py_TPFLAGS_STRING_SUBCLASS
;
3150 else if (PyType_IsSubtype(base
, &PyUnicode_Type
))
3151 type
->tp_flags
|= Py_TPFLAGS_UNICODE_SUBCLASS
;
3152 else if (PyType_IsSubtype(base
, &PyTuple_Type
))
3153 type
->tp_flags
|= Py_TPFLAGS_TUPLE_SUBCLASS
;
3154 else if (PyType_IsSubtype(base
, &PyList_Type
))
3155 type
->tp_flags
|= Py_TPFLAGS_LIST_SUBCLASS
;
3156 else if (PyType_IsSubtype(base
, &PyDict_Type
))
3157 type
->tp_flags
|= Py_TPFLAGS_DICT_SUBCLASS
;
3161 inherit_slots(PyTypeObject
*type
, PyTypeObject
*base
)
3163 PyTypeObject
*basebase
;
3172 #define SLOTDEFINED(SLOT) \
3173 (base->SLOT != 0 && \
3174 (basebase == NULL || base->SLOT != basebase->SLOT))
3176 #define COPYSLOT(SLOT) \
3177 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3179 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3180 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3181 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3182 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3184 /* This won't inherit indirect slots (from tp_as_number etc.)
3185 if type doesn't provide the space. */
3187 if (type
->tp_as_number
!= NULL
&& base
->tp_as_number
!= NULL
) {
3188 basebase
= base
->tp_base
;
3189 if (basebase
->tp_as_number
== NULL
)
3192 COPYNUM(nb_subtract
);
3193 COPYNUM(nb_multiply
);
3195 COPYNUM(nb_remainder
);
3198 COPYNUM(nb_negative
);
3199 COPYNUM(nb_positive
);
3200 COPYNUM(nb_absolute
);
3201 COPYNUM(nb_nonzero
);
3214 COPYNUM(nb_inplace_add
);
3215 COPYNUM(nb_inplace_subtract
);
3216 COPYNUM(nb_inplace_multiply
);
3217 COPYNUM(nb_inplace_divide
);
3218 COPYNUM(nb_inplace_remainder
);
3219 COPYNUM(nb_inplace_power
);
3220 COPYNUM(nb_inplace_lshift
);
3221 COPYNUM(nb_inplace_rshift
);
3222 COPYNUM(nb_inplace_and
);
3223 COPYNUM(nb_inplace_xor
);
3224 COPYNUM(nb_inplace_or
);
3225 if (base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) {
3226 COPYNUM(nb_true_divide
);
3227 COPYNUM(nb_floor_divide
);
3228 COPYNUM(nb_inplace_true_divide
);
3229 COPYNUM(nb_inplace_floor_divide
);
3231 if (base
->tp_flags
& Py_TPFLAGS_HAVE_INDEX
) {
3236 if (type
->tp_as_sequence
!= NULL
&& base
->tp_as_sequence
!= NULL
) {
3237 basebase
= base
->tp_base
;
3238 if (basebase
->tp_as_sequence
== NULL
)
3245 COPYSEQ(sq_ass_item
);
3246 COPYSEQ(sq_ass_slice
);
3247 COPYSEQ(sq_contains
);
3248 COPYSEQ(sq_inplace_concat
);
3249 COPYSEQ(sq_inplace_repeat
);
3252 if (type
->tp_as_mapping
!= NULL
&& base
->tp_as_mapping
!= NULL
) {
3253 basebase
= base
->tp_base
;
3254 if (basebase
->tp_as_mapping
== NULL
)
3257 COPYMAP(mp_subscript
);
3258 COPYMAP(mp_ass_subscript
);
3261 if (type
->tp_as_buffer
!= NULL
&& base
->tp_as_buffer
!= NULL
) {
3262 basebase
= base
->tp_base
;
3263 if (basebase
->tp_as_buffer
== NULL
)
3265 COPYBUF(bf_getreadbuffer
);
3266 COPYBUF(bf_getwritebuffer
);
3267 COPYBUF(bf_getsegcount
);
3268 COPYBUF(bf_getcharbuffer
);
3271 basebase
= base
->tp_base
;
3273 COPYSLOT(tp_dealloc
);
3275 if (type
->tp_getattr
== NULL
&& type
->tp_getattro
== NULL
) {
3276 type
->tp_getattr
= base
->tp_getattr
;
3277 type
->tp_getattro
= base
->tp_getattro
;
3279 if (type
->tp_setattr
== NULL
&& type
->tp_setattro
== NULL
) {
3280 type
->tp_setattr
= base
->tp_setattr
;
3281 type
->tp_setattro
= base
->tp_setattro
;
3283 /* tp_compare see tp_richcompare */
3285 /* tp_hash see tp_richcompare */
3288 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
) {
3289 if (type
->tp_compare
== NULL
&&
3290 type
->tp_richcompare
== NULL
&&
3291 type
->tp_hash
== NULL
)
3293 type
->tp_compare
= base
->tp_compare
;
3294 type
->tp_richcompare
= base
->tp_richcompare
;
3295 type
->tp_hash
= base
->tp_hash
;
3299 COPYSLOT(tp_compare
);
3301 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_ITER
) {
3303 COPYSLOT(tp_iternext
);
3305 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3306 COPYSLOT(tp_descr_get
);
3307 COPYSLOT(tp_descr_set
);
3308 COPYSLOT(tp_dictoffset
);
3312 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
3313 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)) {
3314 /* They agree about gc. */
3317 else if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3318 type
->tp_free
== NULL
&&
3319 base
->tp_free
== _PyObject_Del
) {
3320 /* A bit of magic to plug in the correct default
3321 * tp_free function when a derived class adds gc,
3322 * didn't define tp_free, and the base uses the
3323 * default non-gc tp_free.
3325 type
->tp_free
= PyObject_GC_Del
;
3327 /* else they didn't agree about gc, and there isn't something
3328 * obvious to be done -- the type is on its own.
3333 static int add_operators(PyTypeObject
*);
3336 PyType_Ready(PyTypeObject
*type
)
3338 PyObject
*dict
, *bases
;
3342 if (type
->tp_flags
& Py_TPFLAGS_READY
) {
3343 assert(type
->tp_dict
!= NULL
);
3346 assert((type
->tp_flags
& Py_TPFLAGS_READYING
) == 0);
3348 type
->tp_flags
|= Py_TPFLAGS_READYING
;
3350 #ifdef Py_TRACE_REFS
3351 /* PyType_Ready is the closest thing we have to a choke point
3352 * for type objects, so is the best place I can think of to try
3353 * to get type objects into the doubly-linked list of all objects.
3354 * Still, not all type objects go thru PyType_Ready.
3356 _Py_AddToAllObjects((PyObject
*)type
, 0);
3359 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3360 base
= type
->tp_base
;
3361 if (base
== NULL
&& type
!= &PyBaseObject_Type
) {
3362 base
= type
->tp_base
= &PyBaseObject_Type
;
3366 /* Now the only way base can still be NULL is if type is
3367 * &PyBaseObject_Type.
3370 /* Initialize the base class */
3371 if (base
&& base
->tp_dict
== NULL
) {
3372 if (PyType_Ready(base
) < 0)
3376 /* Initialize ob_type if NULL. This means extensions that want to be
3377 compilable separately on Windows can call PyType_Ready() instead of
3378 initializing the ob_type field of their type objects. */
3379 /* The test for base != NULL is really unnecessary, since base is only
3380 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3381 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3383 if (type
->ob_type
== NULL
&& base
!= NULL
)
3384 type
->ob_type
= base
->ob_type
;
3386 /* Initialize tp_bases */
3387 bases
= type
->tp_bases
;
3388 if (bases
== NULL
) {
3390 bases
= PyTuple_New(0);
3392 bases
= PyTuple_Pack(1, base
);
3395 type
->tp_bases
= bases
;
3398 /* Initialize tp_dict */
3399 dict
= type
->tp_dict
;
3401 dict
= PyDict_New();
3404 type
->tp_dict
= dict
;
3407 /* Add type-specific descriptors to tp_dict */
3408 if (add_operators(type
) < 0)
3410 if (type
->tp_methods
!= NULL
) {
3411 if (add_methods(type
, type
->tp_methods
) < 0)
3414 if (type
->tp_members
!= NULL
) {
3415 if (add_members(type
, type
->tp_members
) < 0)
3418 if (type
->tp_getset
!= NULL
) {
3419 if (add_getset(type
, type
->tp_getset
) < 0)
3423 /* Calculate method resolution order */
3424 if (mro_internal(type
) < 0) {
3428 /* Inherit special flags from dominant base */
3429 if (type
->tp_base
!= NULL
)
3430 inherit_special(type
, type
->tp_base
);
3432 /* Initialize tp_dict properly */
3433 bases
= type
->tp_mro
;
3434 assert(bases
!= NULL
);
3435 assert(PyTuple_Check(bases
));
3436 n
= PyTuple_GET_SIZE(bases
);
3437 for (i
= 1; i
< n
; i
++) {
3438 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3439 if (PyType_Check(b
))
3440 inherit_slots(type
, (PyTypeObject
*)b
);
3443 /* Sanity check for tp_free. */
3444 if (PyType_IS_GC(type
) && (type
->tp_flags
& Py_TPFLAGS_BASETYPE
) &&
3445 (type
->tp_free
== NULL
|| type
->tp_free
== PyObject_Del
)) {
3446 /* This base class needs to call tp_free, but doesn't have
3447 * one, or its tp_free is for non-gc'ed objects.
3449 PyErr_Format(PyExc_TypeError
, "type '%.100s' participates in "
3450 "gc and is a base type but has inappropriate "
3456 /* if the type dictionary doesn't contain a __doc__, set it from
3459 if (PyDict_GetItemString(type
->tp_dict
, "__doc__") == NULL
) {
3460 if (type
->tp_doc
!= NULL
) {
3461 PyObject
*doc
= PyString_FromString(type
->tp_doc
);
3464 PyDict_SetItemString(type
->tp_dict
, "__doc__", doc
);
3467 PyDict_SetItemString(type
->tp_dict
,
3468 "__doc__", Py_None
);
3472 /* Some more special stuff */
3473 base
= type
->tp_base
;
3475 if (type
->tp_as_number
== NULL
)
3476 type
->tp_as_number
= base
->tp_as_number
;
3477 if (type
->tp_as_sequence
== NULL
)
3478 type
->tp_as_sequence
= base
->tp_as_sequence
;
3479 if (type
->tp_as_mapping
== NULL
)
3480 type
->tp_as_mapping
= base
->tp_as_mapping
;
3481 if (type
->tp_as_buffer
== NULL
)
3482 type
->tp_as_buffer
= base
->tp_as_buffer
;
3485 /* Link into each base class's list of subclasses */
3486 bases
= type
->tp_bases
;
3487 n
= PyTuple_GET_SIZE(bases
);
3488 for (i
= 0; i
< n
; i
++) {
3489 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3490 if (PyType_Check(b
) &&
3491 add_subclass((PyTypeObject
*)b
, type
) < 0)
3495 /* All done -- set the ready flag */
3496 assert(type
->tp_dict
!= NULL
);
3498 (type
->tp_flags
& ~Py_TPFLAGS_READYING
) | Py_TPFLAGS_READY
;
3502 type
->tp_flags
&= ~Py_TPFLAGS_READYING
;
3507 add_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3511 PyObject
*list
, *ref
, *newobj
;
3513 list
= base
->tp_subclasses
;
3515 base
->tp_subclasses
= list
= PyList_New(0);
3519 assert(PyList_Check(list
));
3520 newobj
= PyWeakref_NewRef((PyObject
*)type
, NULL
);
3521 i
= PyList_GET_SIZE(list
);
3523 ref
= PyList_GET_ITEM(list
, i
);
3524 assert(PyWeakref_CheckRef(ref
));
3525 if (PyWeakref_GET_OBJECT(ref
) == Py_None
)
3526 return PyList_SetItem(list
, i
, newobj
);
3528 result
= PyList_Append(list
, newobj
);
3534 remove_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3537 PyObject
*list
, *ref
;
3539 list
= base
->tp_subclasses
;
3543 assert(PyList_Check(list
));
3544 i
= PyList_GET_SIZE(list
);
3546 ref
= PyList_GET_ITEM(list
, i
);
3547 assert(PyWeakref_CheckRef(ref
));
3548 if (PyWeakref_GET_OBJECT(ref
) == (PyObject
*)type
) {
3549 /* this can't fail, right? */
3550 PySequence_DelItem(list
, i
);
3557 check_num_args(PyObject
*ob
, int n
)
3559 if (!PyTuple_CheckExact(ob
)) {
3560 PyErr_SetString(PyExc_SystemError
,
3561 "PyArg_UnpackTuple() argument list is not a tuple");
3564 if (n
== PyTuple_GET_SIZE(ob
))
3568 "expected %d arguments, got %zd", n
, PyTuple_GET_SIZE(ob
));
3572 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3574 /* There's a wrapper *function* for each distinct function typedef used
3575 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3576 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3577 Most tables have only one entry; the tables for binary operators have two
3578 entries, one regular and one with reversed arguments. */
3581 wrap_lenfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3583 lenfunc func
= (lenfunc
)wrapped
;
3586 if (!check_num_args(args
, 0))
3588 res
= (*func
)(self
);
3589 if (res
== -1 && PyErr_Occurred())
3591 return PyInt_FromLong((long)res
);
3595 wrap_inquirypred(PyObject
*self
, PyObject
*args
, void *wrapped
)
3597 inquiry func
= (inquiry
)wrapped
;
3600 if (!check_num_args(args
, 0))
3602 res
= (*func
)(self
);
3603 if (res
== -1 && PyErr_Occurred())
3605 return PyBool_FromLong((long)res
);
3609 wrap_binaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3611 binaryfunc func
= (binaryfunc
)wrapped
;
3614 if (!check_num_args(args
, 1))
3616 other
= PyTuple_GET_ITEM(args
, 0);
3617 return (*func
)(self
, other
);
3621 wrap_binaryfunc_l(PyObject
*self
, PyObject
*args
, void *wrapped
)
3623 binaryfunc func
= (binaryfunc
)wrapped
;
3626 if (!check_num_args(args
, 1))
3628 other
= PyTuple_GET_ITEM(args
, 0);
3629 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3630 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3631 Py_INCREF(Py_NotImplemented
);
3632 return Py_NotImplemented
;
3634 return (*func
)(self
, other
);
3638 wrap_binaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3640 binaryfunc func
= (binaryfunc
)wrapped
;
3643 if (!check_num_args(args
, 1))
3645 other
= PyTuple_GET_ITEM(args
, 0);
3646 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3647 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3648 Py_INCREF(Py_NotImplemented
);
3649 return Py_NotImplemented
;
3651 return (*func
)(other
, self
);
3655 wrap_coercefunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3657 coercion func
= (coercion
)wrapped
;
3658 PyObject
*other
, *res
;
3661 if (!check_num_args(args
, 1))
3663 other
= PyTuple_GET_ITEM(args
, 0);
3664 ok
= func(&self
, &other
);
3668 Py_INCREF(Py_NotImplemented
);
3669 return Py_NotImplemented
;
3671 res
= PyTuple_New(2);
3677 PyTuple_SET_ITEM(res
, 0, self
);
3678 PyTuple_SET_ITEM(res
, 1, other
);
3683 wrap_ternaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3685 ternaryfunc func
= (ternaryfunc
)wrapped
;
3687 PyObject
*third
= Py_None
;
3689 /* Note: This wrapper only works for __pow__() */
3691 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3693 return (*func
)(self
, other
, third
);
3697 wrap_ternaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3699 ternaryfunc func
= (ternaryfunc
)wrapped
;
3701 PyObject
*third
= Py_None
;
3703 /* Note: This wrapper only works for __pow__() */
3705 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3707 return (*func
)(other
, self
, third
);
3711 wrap_unaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3713 unaryfunc func
= (unaryfunc
)wrapped
;
3715 if (!check_num_args(args
, 0))
3717 return (*func
)(self
);
3721 wrap_indexargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3723 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
3727 if (!PyArg_UnpackTuple(args
, "", 1, 1, &o
))
3729 i
= PyNumber_AsSsize_t(o
, PyExc_OverflowError
);
3730 if (i
== -1 && PyErr_Occurred())
3732 return (*func
)(self
, i
);
3736 getindex(PyObject
*self
, PyObject
*arg
)
3740 i
= PyNumber_AsSsize_t(arg
, PyExc_OverflowError
);
3741 if (i
== -1 && PyErr_Occurred())
3744 PySequenceMethods
*sq
= self
->ob_type
->tp_as_sequence
;
3745 if (sq
&& sq
->sq_length
) {
3746 Py_ssize_t n
= (*sq
->sq_length
)(self
);
3756 wrap_sq_item(PyObject
*self
, PyObject
*args
, void *wrapped
)
3758 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
3762 if (PyTuple_GET_SIZE(args
) == 1) {
3763 arg
= PyTuple_GET_ITEM(args
, 0);
3764 i
= getindex(self
, arg
);
3765 if (i
== -1 && PyErr_Occurred())
3767 return (*func
)(self
, i
);
3769 check_num_args(args
, 1);
3770 assert(PyErr_Occurred());
3775 wrap_ssizessizeargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3777 ssizessizeargfunc func
= (ssizessizeargfunc
)wrapped
;
3780 if (!PyArg_ParseTuple(args
, "nn", &i
, &j
))
3782 return (*func
)(self
, i
, j
);
3786 wrap_sq_setitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3788 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
3791 PyObject
*arg
, *value
;
3793 if (!PyArg_UnpackTuple(args
, "", 2, 2, &arg
, &value
))
3795 i
= getindex(self
, arg
);
3796 if (i
== -1 && PyErr_Occurred())
3798 res
= (*func
)(self
, i
, value
);
3799 if (res
== -1 && PyErr_Occurred())
3806 wrap_sq_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3808 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
3813 if (!check_num_args(args
, 1))
3815 arg
= PyTuple_GET_ITEM(args
, 0);
3816 i
= getindex(self
, arg
);
3817 if (i
== -1 && PyErr_Occurred())
3819 res
= (*func
)(self
, i
, NULL
);
3820 if (res
== -1 && PyErr_Occurred())
3827 wrap_ssizessizeobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3829 ssizessizeobjargproc func
= (ssizessizeobjargproc
)wrapped
;
3834 if (!PyArg_ParseTuple(args
, "nnO", &i
, &j
, &value
))
3836 res
= (*func
)(self
, i
, j
, value
);
3837 if (res
== -1 && PyErr_Occurred())
3844 wrap_delslice(PyObject
*self
, PyObject
*args
, void *wrapped
)
3846 ssizessizeobjargproc func
= (ssizessizeobjargproc
)wrapped
;
3850 if (!PyArg_ParseTuple(args
, "nn", &i
, &j
))
3852 res
= (*func
)(self
, i
, j
, NULL
);
3853 if (res
== -1 && PyErr_Occurred())
3859 /* XXX objobjproc is a misnomer; should be objargpred */
3861 wrap_objobjproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3863 objobjproc func
= (objobjproc
)wrapped
;
3867 if (!check_num_args(args
, 1))
3869 value
= PyTuple_GET_ITEM(args
, 0);
3870 res
= (*func
)(self
, value
);
3871 if (res
== -1 && PyErr_Occurred())
3874 return PyBool_FromLong(res
);
3878 wrap_objobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3880 objobjargproc func
= (objobjargproc
)wrapped
;
3882 PyObject
*key
, *value
;
3884 if (!PyArg_UnpackTuple(args
, "", 2, 2, &key
, &value
))
3886 res
= (*func
)(self
, key
, value
);
3887 if (res
== -1 && PyErr_Occurred())
3894 wrap_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3896 objobjargproc func
= (objobjargproc
)wrapped
;
3900 if (!check_num_args(args
, 1))
3902 key
= PyTuple_GET_ITEM(args
, 0);
3903 res
= (*func
)(self
, key
, NULL
);
3904 if (res
== -1 && PyErr_Occurred())
3911 wrap_cmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3913 cmpfunc func
= (cmpfunc
)wrapped
;
3917 if (!check_num_args(args
, 1))
3919 other
= PyTuple_GET_ITEM(args
, 0);
3920 if (other
->ob_type
->tp_compare
!= func
&&
3921 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3924 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3925 self
->ob_type
->tp_name
,
3926 self
->ob_type
->tp_name
,
3927 other
->ob_type
->tp_name
);
3930 res
= (*func
)(self
, other
);
3931 if (PyErr_Occurred())
3933 return PyInt_FromLong((long)res
);
3936 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3937 This is called the Carlo Verre hack after its discoverer. */
3939 hackcheck(PyObject
*self
, setattrofunc func
, char *what
)
3941 PyTypeObject
*type
= self
->ob_type
;
3942 while (type
&& type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
3943 type
= type
->tp_base
;
3944 /* If type is NULL now, this is a really weird type.
3945 In the spirit of backwards compatibility (?), just shut up. */
3946 if (type
&& type
->tp_setattro
!= func
) {
3947 PyErr_Format(PyExc_TypeError
,
3948 "can't apply this %s to %s object",
3957 wrap_setattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
3959 setattrofunc func
= (setattrofunc
)wrapped
;
3961 PyObject
*name
, *value
;
3963 if (!PyArg_UnpackTuple(args
, "", 2, 2, &name
, &value
))
3965 if (!hackcheck(self
, func
, "__setattr__"))
3967 res
= (*func
)(self
, name
, value
);
3975 wrap_delattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
3977 setattrofunc func
= (setattrofunc
)wrapped
;
3981 if (!check_num_args(args
, 1))
3983 name
= PyTuple_GET_ITEM(args
, 0);
3984 if (!hackcheck(self
, func
, "__delattr__"))
3986 res
= (*func
)(self
, name
, NULL
);
3994 wrap_hashfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3996 hashfunc func
= (hashfunc
)wrapped
;
3999 if (!check_num_args(args
, 0))
4001 res
= (*func
)(self
);
4002 if (res
== -1 && PyErr_Occurred())
4004 return PyInt_FromLong(res
);
4008 wrap_call(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4010 ternaryfunc func
= (ternaryfunc
)wrapped
;
4012 return (*func
)(self
, args
, kwds
);
4016 wrap_richcmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
, int op
)
4018 richcmpfunc func
= (richcmpfunc
)wrapped
;
4021 if (!check_num_args(args
, 1))
4023 other
= PyTuple_GET_ITEM(args
, 0);
4024 return (*func
)(self
, other
, op
);
4027 #undef RICHCMP_WRAPPER
4028 #define RICHCMP_WRAPPER(NAME, OP) \
4030 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4032 return wrap_richcmpfunc(self, args, wrapped, OP); \
4035 RICHCMP_WRAPPER(lt
, Py_LT
)
4036 RICHCMP_WRAPPER(le
, Py_LE
)
4037 RICHCMP_WRAPPER(eq
, Py_EQ
)
4038 RICHCMP_WRAPPER(ne
, Py_NE
)
4039 RICHCMP_WRAPPER(gt
, Py_GT
)
4040 RICHCMP_WRAPPER(ge
, Py_GE
)
4043 wrap_next(PyObject
*self
, PyObject
*args
, void *wrapped
)
4045 unaryfunc func
= (unaryfunc
)wrapped
;
4048 if (!check_num_args(args
, 0))
4050 res
= (*func
)(self
);
4051 if (res
== NULL
&& !PyErr_Occurred())
4052 PyErr_SetNone(PyExc_StopIteration
);
4057 wrap_descr_get(PyObject
*self
, PyObject
*args
, void *wrapped
)
4059 descrgetfunc func
= (descrgetfunc
)wrapped
;
4061 PyObject
*type
= NULL
;
4063 if (!PyArg_UnpackTuple(args
, "", 1, 2, &obj
, &type
))
4067 if (type
== Py_None
)
4069 if (type
== NULL
&&obj
== NULL
) {
4070 PyErr_SetString(PyExc_TypeError
,
4071 "__get__(None, None) is invalid");
4074 return (*func
)(self
, obj
, type
);
4078 wrap_descr_set(PyObject
*self
, PyObject
*args
, void *wrapped
)
4080 descrsetfunc func
= (descrsetfunc
)wrapped
;
4081 PyObject
*obj
, *value
;
4084 if (!PyArg_UnpackTuple(args
, "", 2, 2, &obj
, &value
))
4086 ret
= (*func
)(self
, obj
, value
);
4094 wrap_descr_delete(PyObject
*self
, PyObject
*args
, void *wrapped
)
4096 descrsetfunc func
= (descrsetfunc
)wrapped
;
4100 if (!check_num_args(args
, 1))
4102 obj
= PyTuple_GET_ITEM(args
, 0);
4103 ret
= (*func
)(self
, obj
, NULL
);
4111 wrap_init(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4113 initproc func
= (initproc
)wrapped
;
4115 if (func(self
, args
, kwds
) < 0)
4122 tp_new_wrapper(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4124 PyTypeObject
*type
, *subtype
, *staticbase
;
4125 PyObject
*arg0
, *res
;
4127 if (self
== NULL
|| !PyType_Check(self
))
4128 Py_FatalError("__new__() called with non-type 'self'");
4129 type
= (PyTypeObject
*)self
;
4130 if (!PyTuple_Check(args
) || PyTuple_GET_SIZE(args
) < 1) {
4131 PyErr_Format(PyExc_TypeError
,
4132 "%s.__new__(): not enough arguments",
4136 arg0
= PyTuple_GET_ITEM(args
, 0);
4137 if (!PyType_Check(arg0
)) {
4138 PyErr_Format(PyExc_TypeError
,
4139 "%s.__new__(X): X is not a type object (%s)",
4141 arg0
->ob_type
->tp_name
);
4144 subtype
= (PyTypeObject
*)arg0
;
4145 if (!PyType_IsSubtype(subtype
, type
)) {
4146 PyErr_Format(PyExc_TypeError
,
4147 "%s.__new__(%s): %s is not a subtype of %s",
4155 /* Check that the use doesn't do something silly and unsafe like
4156 object.__new__(dict). To do this, we check that the
4157 most derived base that's not a heap type is this type. */
4158 staticbase
= subtype
;
4159 while (staticbase
&& (staticbase
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
4160 staticbase
= staticbase
->tp_base
;
4161 /* If staticbase is NULL now, it is a really weird type.
4162 In the spirit of backwards compatibility (?), just shut up. */
4163 if (staticbase
&& staticbase
->tp_new
!= type
->tp_new
) {
4164 PyErr_Format(PyExc_TypeError
,
4165 "%s.__new__(%s) is not safe, use %s.__new__()",
4168 staticbase
== NULL
? "?" : staticbase
->tp_name
);
4172 args
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
4175 res
= type
->tp_new(subtype
, args
, kwds
);
4180 static struct PyMethodDef tp_new_methoddef
[] = {
4181 {"__new__", (PyCFunction
)tp_new_wrapper
, METH_KEYWORDS
,
4182 PyDoc_STR("T.__new__(S, ...) -> "
4183 "a new object with type S, a subtype of T")},
4188 add_tp_new_wrapper(PyTypeObject
*type
)
4192 if (PyDict_GetItemString(type
->tp_dict
, "__new__") != NULL
)
4194 func
= PyCFunction_New(tp_new_methoddef
, (PyObject
*)type
);
4197 if (PyDict_SetItemString(type
->tp_dict
, "__new__", func
)) {
4205 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4206 below at override_slots() for more explanation. */
4208 #define SLOT0(FUNCNAME, OPSTR) \
4210 FUNCNAME(PyObject *self) \
4212 static PyObject *cache_str; \
4213 return call_method(self, OPSTR, &cache_str, "()"); \
4216 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4218 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4220 static PyObject *cache_str; \
4221 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4224 /* Boolean helper for SLOT1BINFULL().
4225 right.__class__ is a nontrivial subclass of left.__class__. */
4227 method_is_overloaded(PyObject
*left
, PyObject
*right
, char *name
)
4232 b
= PyObject_GetAttrString((PyObject
*)(right
->ob_type
), name
);
4235 /* If right doesn't have it, it's not overloaded */
4239 a
= PyObject_GetAttrString((PyObject
*)(left
->ob_type
), name
);
4243 /* If right has it but left doesn't, it's overloaded */
4247 ok
= PyObject_RichCompareBool(a
, b
, Py_NE
);
4259 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4261 FUNCNAME(PyObject *self, PyObject *other) \
4263 static PyObject *cache_str, *rcache_str; \
4264 int do_other = self->ob_type != other->ob_type && \
4265 other->ob_type->tp_as_number != NULL && \
4266 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4267 if (self->ob_type->tp_as_number != NULL && \
4268 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4271 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4272 method_is_overloaded(self, other, ROPSTR)) { \
4274 other, ROPSTR, &rcache_str, "(O)", self); \
4275 if (r != Py_NotImplemented) \
4281 self, OPSTR, &cache_str, "(O)", other); \
4282 if (r != Py_NotImplemented || \
4283 other->ob_type == self->ob_type) \
4288 return call_maybe( \
4289 other, ROPSTR, &rcache_str, "(O)", self); \
4291 Py_INCREF(Py_NotImplemented); \
4292 return Py_NotImplemented; \
4295 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4296 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4298 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4300 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4302 static PyObject *cache_str; \
4303 return call_method(self, OPSTR, &cache_str, \
4304 "(" ARGCODES ")", arg1, arg2); \
4308 slot_sq_length(PyObject
*self
)
4310 static PyObject
*len_str
;
4311 PyObject
*res
= call_method(self
, "__len__", &len_str
, "()");
4316 len
= PyInt_AsSsize_t(res
);
4319 if (!PyErr_Occurred())
4320 PyErr_SetString(PyExc_ValueError
,
4321 "__len__() should return >= 0");
4327 /* Super-optimized version of slot_sq_item.
4328 Other slots could do the same... */
4330 slot_sq_item(PyObject
*self
, Py_ssize_t i
)
4332 static PyObject
*getitem_str
;
4333 PyObject
*func
, *args
= NULL
, *ival
= NULL
, *retval
= NULL
;
4336 if (getitem_str
== NULL
) {
4337 getitem_str
= PyString_InternFromString("__getitem__");
4338 if (getitem_str
== NULL
)
4341 func
= _PyType_Lookup(self
->ob_type
, getitem_str
);
4343 if ((f
= func
->ob_type
->tp_descr_get
) == NULL
)
4346 func
= f(func
, self
, (PyObject
*)(self
->ob_type
));
4351 ival
= PyInt_FromSsize_t(i
);
4353 args
= PyTuple_New(1);
4355 PyTuple_SET_ITEM(args
, 0, ival
);
4356 retval
= PyObject_Call(func
, args
, NULL
);
4364 PyErr_SetObject(PyExc_AttributeError
, getitem_str
);
4372 SLOT2(slot_sq_slice
, "__getslice__", Py_ssize_t
, Py_ssize_t
, "nn")
4375 slot_sq_ass_item(PyObject
*self
, Py_ssize_t index
, PyObject
*value
)
4378 static PyObject
*delitem_str
, *setitem_str
;
4381 res
= call_method(self
, "__delitem__", &delitem_str
,
4384 res
= call_method(self
, "__setitem__", &setitem_str
,
4385 "(nO)", index
, value
);
4393 slot_sq_ass_slice(PyObject
*self
, Py_ssize_t i
, Py_ssize_t j
, PyObject
*value
)
4396 static PyObject
*delslice_str
, *setslice_str
;
4399 res
= call_method(self
, "__delslice__", &delslice_str
,
4402 res
= call_method(self
, "__setslice__", &setslice_str
,
4403 "(nnO)", i
, j
, value
);
4411 slot_sq_contains(PyObject
*self
, PyObject
*value
)
4413 PyObject
*func
, *res
, *args
;
4416 static PyObject
*contains_str
;
4418 func
= lookup_maybe(self
, "__contains__", &contains_str
);
4420 args
= PyTuple_Pack(1, value
);
4424 res
= PyObject_Call(func
, args
, NULL
);
4429 result
= PyObject_IsTrue(res
);
4433 else if (! PyErr_Occurred()) {
4434 /* Possible results: -1 and 1 */
4435 result
= (int)_PySequence_IterSearch(self
, value
,
4436 PY_ITERSEARCH_CONTAINS
);
4441 #define slot_mp_length slot_sq_length
4443 SLOT1(slot_mp_subscript
, "__getitem__", PyObject
*, "O")
4446 slot_mp_ass_subscript(PyObject
*self
, PyObject
*key
, PyObject
*value
)
4449 static PyObject
*delitem_str
, *setitem_str
;
4452 res
= call_method(self
, "__delitem__", &delitem_str
,
4455 res
= call_method(self
, "__setitem__", &setitem_str
,
4456 "(OO)", key
, value
);
4463 SLOT1BIN(slot_nb_add
, nb_add
, "__add__", "__radd__")
4464 SLOT1BIN(slot_nb_subtract
, nb_subtract
, "__sub__", "__rsub__")
4465 SLOT1BIN(slot_nb_multiply
, nb_multiply
, "__mul__", "__rmul__")
4466 SLOT1BIN(slot_nb_divide
, nb_divide
, "__div__", "__rdiv__")
4467 SLOT1BIN(slot_nb_remainder
, nb_remainder
, "__mod__", "__rmod__")
4468 SLOT1BIN(slot_nb_divmod
, nb_divmod
, "__divmod__", "__rdivmod__")
4470 static PyObject
*slot_nb_power(PyObject
*, PyObject
*, PyObject
*);
4472 SLOT1BINFULL(slot_nb_power_binary
, slot_nb_power
,
4473 nb_power
, "__pow__", "__rpow__")
4476 slot_nb_power(PyObject
*self
, PyObject
*other
, PyObject
*modulus
)
4478 static PyObject
*pow_str
;
4480 if (modulus
== Py_None
)
4481 return slot_nb_power_binary(self
, other
);
4482 /* Three-arg power doesn't use __rpow__. But ternary_op
4483 can call this when the second argument's type uses
4484 slot_nb_power, so check before calling self.__pow__. */
4485 if (self
->ob_type
->tp_as_number
!= NULL
&&
4486 self
->ob_type
->tp_as_number
->nb_power
== slot_nb_power
) {
4487 return call_method(self
, "__pow__", &pow_str
,
4488 "(OO)", other
, modulus
);
4490 Py_INCREF(Py_NotImplemented
);
4491 return Py_NotImplemented
;
4494 SLOT0(slot_nb_negative
, "__neg__")
4495 SLOT0(slot_nb_positive
, "__pos__")
4496 SLOT0(slot_nb_absolute
, "__abs__")
4499 slot_nb_nonzero(PyObject
*self
)
4501 PyObject
*func
, *args
;
4502 static PyObject
*nonzero_str
, *len_str
;
4505 func
= lookup_maybe(self
, "__nonzero__", &nonzero_str
);
4507 if (PyErr_Occurred())
4509 func
= lookup_maybe(self
, "__len__", &len_str
);
4511 return PyErr_Occurred() ? -1 : 1;
4513 args
= PyTuple_New(0);
4515 PyObject
*temp
= PyObject_Call(func
, args
, NULL
);
4518 if (PyInt_CheckExact(temp
) || PyBool_Check(temp
))
4519 result
= PyObject_IsTrue(temp
);
4521 PyErr_Format(PyExc_TypeError
,
4522 "__nonzero__ should return "
4523 "bool or int, returned %s",
4524 temp
->ob_type
->tp_name
);
4536 slot_nb_index(PyObject
*self
)
4538 static PyObject
*index_str
;
4539 return call_method(self
, "__index__", &index_str
, "()");
4543 SLOT0(slot_nb_invert
, "__invert__")
4544 SLOT1BIN(slot_nb_lshift
, nb_lshift
, "__lshift__", "__rlshift__")
4545 SLOT1BIN(slot_nb_rshift
, nb_rshift
, "__rshift__", "__rrshift__")
4546 SLOT1BIN(slot_nb_and
, nb_and
, "__and__", "__rand__")
4547 SLOT1BIN(slot_nb_xor
, nb_xor
, "__xor__", "__rxor__")
4548 SLOT1BIN(slot_nb_or
, nb_or
, "__or__", "__ror__")
4551 slot_nb_coerce(PyObject
**a
, PyObject
**b
)
4553 static PyObject
*coerce_str
;
4554 PyObject
*self
= *a
, *other
= *b
;
4556 if (self
->ob_type
->tp_as_number
!= NULL
&&
4557 self
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4560 self
, "__coerce__", &coerce_str
, "(O)", other
);
4563 if (r
== Py_NotImplemented
) {
4567 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4568 PyErr_SetString(PyExc_TypeError
,
4569 "__coerce__ didn't return a 2-tuple");
4573 *a
= PyTuple_GET_ITEM(r
, 0);
4575 *b
= PyTuple_GET_ITEM(r
, 1);
4581 if (other
->ob_type
->tp_as_number
!= NULL
&&
4582 other
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4585 other
, "__coerce__", &coerce_str
, "(O)", self
);
4588 if (r
== Py_NotImplemented
) {
4592 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4593 PyErr_SetString(PyExc_TypeError
,
4594 "__coerce__ didn't return a 2-tuple");
4598 *a
= PyTuple_GET_ITEM(r
, 1);
4600 *b
= PyTuple_GET_ITEM(r
, 0);
4608 SLOT0(slot_nb_int
, "__int__")
4609 SLOT0(slot_nb_long
, "__long__")
4610 SLOT0(slot_nb_float
, "__float__")
4611 SLOT0(slot_nb_oct
, "__oct__")
4612 SLOT0(slot_nb_hex
, "__hex__")
4613 SLOT1(slot_nb_inplace_add
, "__iadd__", PyObject
*, "O")
4614 SLOT1(slot_nb_inplace_subtract
, "__isub__", PyObject
*, "O")
4615 SLOT1(slot_nb_inplace_multiply
, "__imul__", PyObject
*, "O")
4616 SLOT1(slot_nb_inplace_divide
, "__idiv__", PyObject
*, "O")
4617 SLOT1(slot_nb_inplace_remainder
, "__imod__", PyObject
*, "O")
4618 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
4620 slot_nb_inplace_power(PyObject
*self
, PyObject
* arg1
, PyObject
*arg2
)
4622 static PyObject
*cache_str
;
4623 return call_method(self
, "__ipow__", &cache_str
, "(" "O" ")", arg1
);
4625 SLOT1(slot_nb_inplace_lshift
, "__ilshift__", PyObject
*, "O")
4626 SLOT1(slot_nb_inplace_rshift
, "__irshift__", PyObject
*, "O")
4627 SLOT1(slot_nb_inplace_and
, "__iand__", PyObject
*, "O")
4628 SLOT1(slot_nb_inplace_xor
, "__ixor__", PyObject
*, "O")
4629 SLOT1(slot_nb_inplace_or
, "__ior__", PyObject
*, "O")
4630 SLOT1BIN(slot_nb_floor_divide
, nb_floor_divide
,
4631 "__floordiv__", "__rfloordiv__")
4632 SLOT1BIN(slot_nb_true_divide
, nb_true_divide
, "__truediv__", "__rtruediv__")
4633 SLOT1(slot_nb_inplace_floor_divide
, "__ifloordiv__", PyObject
*, "O")
4634 SLOT1(slot_nb_inplace_true_divide
, "__itruediv__", PyObject
*, "O")
4637 half_compare(PyObject
*self
, PyObject
*other
)
4639 PyObject
*func
, *args
, *res
;
4640 static PyObject
*cmp_str
;
4643 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4648 args
= PyTuple_Pack(1, other
);
4652 res
= PyObject_Call(func
, args
, NULL
);
4656 if (res
!= Py_NotImplemented
) {
4659 c
= PyInt_AsLong(res
);
4661 if (c
== -1 && PyErr_Occurred())
4663 return (c
< 0) ? -1 : (c
> 0) ? 1 : 0;
4670 /* This slot is published for the benefit of try_3way_compare in object.c */
4672 _PyObject_SlotCompare(PyObject
*self
, PyObject
*other
)
4676 if (self
->ob_type
->tp_compare
== _PyObject_SlotCompare
) {
4677 c
= half_compare(self
, other
);
4681 if (other
->ob_type
->tp_compare
== _PyObject_SlotCompare
) {
4682 c
= half_compare(other
, self
);
4688 return (void *)self
< (void *)other
? -1 :
4689 (void *)self
> (void *)other
? 1 : 0;
4693 slot_tp_repr(PyObject
*self
)
4695 PyObject
*func
, *res
;
4696 static PyObject
*repr_str
;
4698 func
= lookup_method(self
, "__repr__", &repr_str
);
4700 res
= PyEval_CallObject(func
, NULL
);
4705 return PyString_FromFormat("<%s object at %p>",
4706 self
->ob_type
->tp_name
, self
);
4710 slot_tp_str(PyObject
*self
)
4712 PyObject
*func
, *res
;
4713 static PyObject
*str_str
;
4715 func
= lookup_method(self
, "__str__", &str_str
);
4717 res
= PyEval_CallObject(func
, NULL
);
4723 return slot_tp_repr(self
);
4728 slot_tp_hash(PyObject
*self
)
4731 static PyObject
*hash_str
, *eq_str
, *cmp_str
;
4734 func
= lookup_method(self
, "__hash__", &hash_str
);
4737 PyObject
*res
= PyEval_CallObject(func
, NULL
);
4741 if (PyLong_Check(res
))
4742 h
= PyLong_Type
.tp_hash(res
);
4744 h
= PyInt_AsLong(res
);
4749 func
= lookup_method(self
, "__eq__", &eq_str
);
4752 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4755 PyErr_Format(PyExc_TypeError
, "unhashable type: '%.200s'",
4756 self
->ob_type
->tp_name
);
4761 h
= _Py_HashPointer((void *)self
);
4763 if (h
== -1 && !PyErr_Occurred())
4769 slot_tp_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4771 static PyObject
*call_str
;
4772 PyObject
*meth
= lookup_method(self
, "__call__", &call_str
);
4778 /* PyObject_Call() will end up calling slot_tp_call() again if
4779 the object returned for __call__ has __call__ itself defined
4780 upon it. This can be an infinite recursion if you set
4781 __call__ in a class to an instance of it. */
4782 if (Py_EnterRecursiveCall(" in __call__")) {
4786 res
= PyObject_Call(meth
, args
, kwds
);
4787 Py_LeaveRecursiveCall();
4793 /* There are two slot dispatch functions for tp_getattro.
4795 - slot_tp_getattro() is used when __getattribute__ is overridden
4796 but no __getattr__ hook is present;
4798 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4800 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4801 detects the absence of __getattr__ and then installs the simpler slot if
4805 slot_tp_getattro(PyObject
*self
, PyObject
*name
)
4807 static PyObject
*getattribute_str
= NULL
;
4808 return call_method(self
, "__getattribute__", &getattribute_str
,
4813 slot_tp_getattr_hook(PyObject
*self
, PyObject
*name
)
4815 PyTypeObject
*tp
= self
->ob_type
;
4816 PyObject
*getattr
, *getattribute
, *res
;
4817 static PyObject
*getattribute_str
= NULL
;
4818 static PyObject
*getattr_str
= NULL
;
4820 if (getattr_str
== NULL
) {
4821 getattr_str
= PyString_InternFromString("__getattr__");
4822 if (getattr_str
== NULL
)
4825 if (getattribute_str
== NULL
) {
4827 PyString_InternFromString("__getattribute__");
4828 if (getattribute_str
== NULL
)
4831 getattr
= _PyType_Lookup(tp
, getattr_str
);
4832 if (getattr
== NULL
) {
4833 /* No __getattr__ hook: use a simpler dispatcher */
4834 tp
->tp_getattro
= slot_tp_getattro
;
4835 return slot_tp_getattro(self
, name
);
4837 getattribute
= _PyType_Lookup(tp
, getattribute_str
);
4838 if (getattribute
== NULL
||
4839 (getattribute
->ob_type
== &PyWrapperDescr_Type
&&
4840 ((PyWrapperDescrObject
*)getattribute
)->d_wrapped
==
4841 (void *)PyObject_GenericGetAttr
))
4842 res
= PyObject_GenericGetAttr(self
, name
);
4844 res
= PyObject_CallFunctionObjArgs(getattribute
, self
, name
, NULL
);
4845 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4847 res
= PyObject_CallFunctionObjArgs(getattr
, self
, name
, NULL
);
4853 slot_tp_setattro(PyObject
*self
, PyObject
*name
, PyObject
*value
)
4856 static PyObject
*delattr_str
, *setattr_str
;
4859 res
= call_method(self
, "__delattr__", &delattr_str
,
4862 res
= call_method(self
, "__setattr__", &setattr_str
,
4863 "(OO)", name
, value
);
4870 /* Map rich comparison operators to their __xx__ namesakes */
4871 static char *name_op
[] = {
4881 half_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4883 PyObject
*func
, *args
, *res
;
4884 static PyObject
*op_str
[6];
4886 func
= lookup_method(self
, name_op
[op
], &op_str
[op
]);
4889 Py_INCREF(Py_NotImplemented
);
4890 return Py_NotImplemented
;
4892 args
= PyTuple_Pack(1, other
);
4896 res
= PyObject_Call(func
, args
, NULL
);
4904 slot_tp_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4908 if (self
->ob_type
->tp_richcompare
== slot_tp_richcompare
) {
4909 res
= half_richcompare(self
, other
, op
);
4910 if (res
!= Py_NotImplemented
)
4914 if (other
->ob_type
->tp_richcompare
== slot_tp_richcompare
) {
4915 res
= half_richcompare(other
, self
, _Py_SwappedOp
[op
]);
4916 if (res
!= Py_NotImplemented
) {
4921 Py_INCREF(Py_NotImplemented
);
4922 return Py_NotImplemented
;
4926 slot_tp_iter(PyObject
*self
)
4928 PyObject
*func
, *res
;
4929 static PyObject
*iter_str
, *getitem_str
;
4931 func
= lookup_method(self
, "__iter__", &iter_str
);
4934 args
= res
= PyTuple_New(0);
4936 res
= PyObject_Call(func
, args
, NULL
);
4943 func
= lookup_method(self
, "__getitem__", &getitem_str
);
4945 PyErr_Format(PyExc_TypeError
,
4946 "'%.200s' object is not iterable",
4947 self
->ob_type
->tp_name
);
4951 return PySeqIter_New(self
);
4955 slot_tp_iternext(PyObject
*self
)
4957 static PyObject
*next_str
;
4958 return call_method(self
, "next", &next_str
, "()");
4962 slot_tp_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
4964 PyTypeObject
*tp
= self
->ob_type
;
4966 static PyObject
*get_str
= NULL
;
4968 if (get_str
== NULL
) {
4969 get_str
= PyString_InternFromString("__get__");
4970 if (get_str
== NULL
)
4973 get
= _PyType_Lookup(tp
, get_str
);
4975 /* Avoid further slowdowns */
4976 if (tp
->tp_descr_get
== slot_tp_descr_get
)
4977 tp
->tp_descr_get
= NULL
;
4985 return PyObject_CallFunctionObjArgs(get
, self
, obj
, type
, NULL
);
4989 slot_tp_descr_set(PyObject
*self
, PyObject
*target
, PyObject
*value
)
4992 static PyObject
*del_str
, *set_str
;
4995 res
= call_method(self
, "__delete__", &del_str
,
4998 res
= call_method(self
, "__set__", &set_str
,
4999 "(OO)", target
, value
);
5007 slot_tp_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5009 static PyObject
*init_str
;
5010 PyObject
*meth
= lookup_method(self
, "__init__", &init_str
);
5015 res
= PyObject_Call(meth
, args
, kwds
);
5019 if (res
!= Py_None
) {
5020 PyErr_Format(PyExc_TypeError
,
5021 "__init__() should return None, not '%.200s'",
5022 res
->ob_type
->tp_name
);
5031 slot_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
5033 static PyObject
*new_str
;
5035 PyObject
*newargs
, *x
;
5038 if (new_str
== NULL
) {
5039 new_str
= PyString_InternFromString("__new__");
5040 if (new_str
== NULL
)
5043 func
= PyObject_GetAttr((PyObject
*)type
, new_str
);
5046 assert(PyTuple_Check(args
));
5047 n
= PyTuple_GET_SIZE(args
);
5048 newargs
= PyTuple_New(n
+1);
5049 if (newargs
== NULL
)
5052 PyTuple_SET_ITEM(newargs
, 0, (PyObject
*)type
);
5053 for (i
= 0; i
< n
; i
++) {
5054 x
= PyTuple_GET_ITEM(args
, i
);
5056 PyTuple_SET_ITEM(newargs
, i
+1, x
);
5058 x
= PyObject_Call(func
, newargs
, kwds
);
5065 slot_tp_del(PyObject
*self
)
5067 static PyObject
*del_str
= NULL
;
5068 PyObject
*del
, *res
;
5069 PyObject
*error_type
, *error_value
, *error_traceback
;
5071 /* Temporarily resurrect the object. */
5072 assert(self
->ob_refcnt
== 0);
5073 self
->ob_refcnt
= 1;
5075 /* Save the current exception, if any. */
5076 PyErr_Fetch(&error_type
, &error_value
, &error_traceback
);
5078 /* Execute __del__ method, if any. */
5079 del
= lookup_maybe(self
, "__del__", &del_str
);
5081 res
= PyEval_CallObject(del
, NULL
);
5083 PyErr_WriteUnraisable(del
);
5089 /* Restore the saved exception. */
5090 PyErr_Restore(error_type
, error_value
, error_traceback
);
5092 /* Undo the temporary resurrection; can't use DECREF here, it would
5093 * cause a recursive call.
5095 assert(self
->ob_refcnt
> 0);
5096 if (--self
->ob_refcnt
== 0)
5097 return; /* this is the normal path out */
5099 /* __del__ resurrected it! Make it look like the original Py_DECREF
5103 Py_ssize_t refcnt
= self
->ob_refcnt
;
5104 _Py_NewReference(self
);
5105 self
->ob_refcnt
= refcnt
;
5107 assert(!PyType_IS_GC(self
->ob_type
) ||
5108 _Py_AS_GC(self
)->gc
.gc_refs
!= _PyGC_REFS_UNTRACKED
);
5109 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5110 * we need to undo that. */
5112 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5113 * chain, so no more to do there.
5114 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5115 * _Py_NewReference bumped tp_allocs: both of those need to be
5119 --self
->ob_type
->tp_frees
;
5120 --self
->ob_type
->tp_allocs
;
5125 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
5126 functions. The offsets here are relative to the 'PyHeapTypeObject'
5127 structure, which incorporates the additional structures used for numbers,
5128 sequences and mappings.
5129 Note that multiple names may map to the same slot (e.g. __eq__,
5130 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
5131 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5132 terminated with an all-zero entry. (This table is further initialized and
5133 sorted in init_slotdefs() below.) */
5135 typedef struct wrapperbase slotdef
;
5148 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5149 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5151 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5152 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5153 PyDoc_STR(DOC), FLAGS}
5154 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5155 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5157 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5158 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5159 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5160 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5161 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5162 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5163 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5164 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5165 "x." NAME "() <==> " DOC)
5166 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5167 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5168 "x." NAME "(y) <==> x" DOC "y")
5169 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5170 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5171 "x." NAME "(y) <==> x" DOC "y")
5172 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5173 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5174 "x." NAME "(y) <==> y" DOC "x")
5175 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5176 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5177 "x." NAME "(y) <==> " DOC)
5178 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5179 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5180 "x." NAME "(y) <==> " DOC)
5182 static slotdef slotdefs
[] = {
5183 SQSLOT("__len__", sq_length
, slot_sq_length
, wrap_lenfunc
,
5184 "x.__len__() <==> len(x)"),
5185 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5186 The logic in abstract.c always falls back to nb_add/nb_multiply in
5187 this case. Defining both the nb_* and the sq_* slots to call the
5188 user-defined methods has unexpected side-effects, as shown by
5189 test_descr.notimplemented() */
5190 SQSLOT("__add__", sq_concat
, NULL
, wrap_binaryfunc
,
5191 "x.__add__(y) <==> x+y"),
5192 SQSLOT("__mul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5193 "x.__mul__(n) <==> x*n"),
5194 SQSLOT("__rmul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5195 "x.__rmul__(n) <==> n*x"),
5196 SQSLOT("__getitem__", sq_item
, slot_sq_item
, wrap_sq_item
,
5197 "x.__getitem__(y) <==> x[y]"),
5198 SQSLOT("__getslice__", sq_slice
, slot_sq_slice
, wrap_ssizessizeargfunc
,
5199 "x.__getslice__(i, j) <==> x[i:j]\n\
5201 Use of negative indices is not supported."),
5202 SQSLOT("__setitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_setitem
,
5203 "x.__setitem__(i, y) <==> x[i]=y"),
5204 SQSLOT("__delitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_delitem
,
5205 "x.__delitem__(y) <==> del x[y]"),
5206 SQSLOT("__setslice__", sq_ass_slice
, slot_sq_ass_slice
,
5207 wrap_ssizessizeobjargproc
,
5208 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
5210 Use of negative indices is not supported."),
5211 SQSLOT("__delslice__", sq_ass_slice
, slot_sq_ass_slice
, wrap_delslice
,
5212 "x.__delslice__(i, j) <==> del x[i:j]\n\
5214 Use of negative indices is not supported."),
5215 SQSLOT("__contains__", sq_contains
, slot_sq_contains
, wrap_objobjproc
,
5216 "x.__contains__(y) <==> y in x"),
5217 SQSLOT("__iadd__", sq_inplace_concat
, NULL
,
5218 wrap_binaryfunc
, "x.__iadd__(y) <==> x+=y"),
5219 SQSLOT("__imul__", sq_inplace_repeat
, NULL
,
5220 wrap_indexargfunc
, "x.__imul__(y) <==> x*=y"),
5222 MPSLOT("__len__", mp_length
, slot_mp_length
, wrap_lenfunc
,
5223 "x.__len__() <==> len(x)"),
5224 MPSLOT("__getitem__", mp_subscript
, slot_mp_subscript
,
5226 "x.__getitem__(y) <==> x[y]"),
5227 MPSLOT("__setitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5229 "x.__setitem__(i, y) <==> x[i]=y"),
5230 MPSLOT("__delitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5232 "x.__delitem__(y) <==> del x[y]"),
5234 BINSLOT("__add__", nb_add
, slot_nb_add
,
5236 RBINSLOT("__radd__", nb_add
, slot_nb_add
,
5238 BINSLOT("__sub__", nb_subtract
, slot_nb_subtract
,
5240 RBINSLOT("__rsub__", nb_subtract
, slot_nb_subtract
,
5242 BINSLOT("__mul__", nb_multiply
, slot_nb_multiply
,
5244 RBINSLOT("__rmul__", nb_multiply
, slot_nb_multiply
,
5246 BINSLOT("__div__", nb_divide
, slot_nb_divide
,
5248 RBINSLOT("__rdiv__", nb_divide
, slot_nb_divide
,
5250 BINSLOT("__mod__", nb_remainder
, slot_nb_remainder
,
5252 RBINSLOT("__rmod__", nb_remainder
, slot_nb_remainder
,
5254 BINSLOTNOTINFIX("__divmod__", nb_divmod
, slot_nb_divmod
,
5256 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod
, slot_nb_divmod
,
5258 NBSLOT("__pow__", nb_power
, slot_nb_power
, wrap_ternaryfunc
,
5259 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5260 NBSLOT("__rpow__", nb_power
, slot_nb_power
, wrap_ternaryfunc_r
,
5261 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5262 UNSLOT("__neg__", nb_negative
, slot_nb_negative
, wrap_unaryfunc
, "-x"),
5263 UNSLOT("__pos__", nb_positive
, slot_nb_positive
, wrap_unaryfunc
, "+x"),
5264 UNSLOT("__abs__", nb_absolute
, slot_nb_absolute
, wrap_unaryfunc
,
5266 UNSLOT("__nonzero__", nb_nonzero
, slot_nb_nonzero
, wrap_inquirypred
,
5268 UNSLOT("__invert__", nb_invert
, slot_nb_invert
, wrap_unaryfunc
, "~x"),
5269 BINSLOT("__lshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5270 RBINSLOT("__rlshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5271 BINSLOT("__rshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5272 RBINSLOT("__rrshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5273 BINSLOT("__and__", nb_and
, slot_nb_and
, "&"),
5274 RBINSLOT("__rand__", nb_and
, slot_nb_and
, "&"),
5275 BINSLOT("__xor__", nb_xor
, slot_nb_xor
, "^"),
5276 RBINSLOT("__rxor__", nb_xor
, slot_nb_xor
, "^"),
5277 BINSLOT("__or__", nb_or
, slot_nb_or
, "|"),
5278 RBINSLOT("__ror__", nb_or
, slot_nb_or
, "|"),
5279 NBSLOT("__coerce__", nb_coerce
, slot_nb_coerce
, wrap_coercefunc
,
5280 "x.__coerce__(y) <==> coerce(x, y)"),
5281 UNSLOT("__int__", nb_int
, slot_nb_int
, wrap_unaryfunc
,
5283 UNSLOT("__long__", nb_long
, slot_nb_long
, wrap_unaryfunc
,
5285 UNSLOT("__float__", nb_float
, slot_nb_float
, wrap_unaryfunc
,
5287 UNSLOT("__oct__", nb_oct
, slot_nb_oct
, wrap_unaryfunc
,
5289 UNSLOT("__hex__", nb_hex
, slot_nb_hex
, wrap_unaryfunc
,
5291 NBSLOT("__index__", nb_index
, slot_nb_index
, wrap_unaryfunc
,
5292 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5293 IBSLOT("__iadd__", nb_inplace_add
, slot_nb_inplace_add
,
5294 wrap_binaryfunc
, "+"),
5295 IBSLOT("__isub__", nb_inplace_subtract
, slot_nb_inplace_subtract
,
5296 wrap_binaryfunc
, "-"),
5297 IBSLOT("__imul__", nb_inplace_multiply
, slot_nb_inplace_multiply
,
5298 wrap_binaryfunc
, "*"),
5299 IBSLOT("__idiv__", nb_inplace_divide
, slot_nb_inplace_divide
,
5300 wrap_binaryfunc
, "/"),
5301 IBSLOT("__imod__", nb_inplace_remainder
, slot_nb_inplace_remainder
,
5302 wrap_binaryfunc
, "%"),
5303 IBSLOT("__ipow__", nb_inplace_power
, slot_nb_inplace_power
,
5304 wrap_binaryfunc
, "**"),
5305 IBSLOT("__ilshift__", nb_inplace_lshift
, slot_nb_inplace_lshift
,
5306 wrap_binaryfunc
, "<<"),
5307 IBSLOT("__irshift__", nb_inplace_rshift
, slot_nb_inplace_rshift
,
5308 wrap_binaryfunc
, ">>"),
5309 IBSLOT("__iand__", nb_inplace_and
, slot_nb_inplace_and
,
5310 wrap_binaryfunc
, "&"),
5311 IBSLOT("__ixor__", nb_inplace_xor
, slot_nb_inplace_xor
,
5312 wrap_binaryfunc
, "^"),
5313 IBSLOT("__ior__", nb_inplace_or
, slot_nb_inplace_or
,
5314 wrap_binaryfunc
, "|"),
5315 BINSLOT("__floordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5316 RBINSLOT("__rfloordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5317 BINSLOT("__truediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5318 RBINSLOT("__rtruediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5319 IBSLOT("__ifloordiv__", nb_inplace_floor_divide
,
5320 slot_nb_inplace_floor_divide
, wrap_binaryfunc
, "//"),
5321 IBSLOT("__itruediv__", nb_inplace_true_divide
,
5322 slot_nb_inplace_true_divide
, wrap_binaryfunc
, "/"),
5324 TPSLOT("__str__", tp_str
, slot_tp_str
, wrap_unaryfunc
,
5325 "x.__str__() <==> str(x)"),
5326 TPSLOT("__str__", tp_print
, NULL
, NULL
, ""),
5327 TPSLOT("__repr__", tp_repr
, slot_tp_repr
, wrap_unaryfunc
,
5328 "x.__repr__() <==> repr(x)"),
5329 TPSLOT("__repr__", tp_print
, NULL
, NULL
, ""),
5330 TPSLOT("__cmp__", tp_compare
, _PyObject_SlotCompare
, wrap_cmpfunc
,
5331 "x.__cmp__(y) <==> cmp(x,y)"),
5332 TPSLOT("__hash__", tp_hash
, slot_tp_hash
, wrap_hashfunc
,
5333 "x.__hash__() <==> hash(x)"),
5334 FLSLOT("__call__", tp_call
, slot_tp_call
, (wrapperfunc
)wrap_call
,
5335 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS
),
5336 TPSLOT("__getattribute__", tp_getattro
, slot_tp_getattr_hook
,
5337 wrap_binaryfunc
, "x.__getattribute__('name') <==> x.name"),
5338 TPSLOT("__getattribute__", tp_getattr
, NULL
, NULL
, ""),
5339 TPSLOT("__getattr__", tp_getattro
, slot_tp_getattr_hook
, NULL
, ""),
5340 TPSLOT("__getattr__", tp_getattr
, NULL
, NULL
, ""),
5341 TPSLOT("__setattr__", tp_setattro
, slot_tp_setattro
, wrap_setattr
,
5342 "x.__setattr__('name', value) <==> x.name = value"),
5343 TPSLOT("__setattr__", tp_setattr
, NULL
, NULL
, ""),
5344 TPSLOT("__delattr__", tp_setattro
, slot_tp_setattro
, wrap_delattr
,
5345 "x.__delattr__('name') <==> del x.name"),
5346 TPSLOT("__delattr__", tp_setattr
, NULL
, NULL
, ""),
5347 TPSLOT("__lt__", tp_richcompare
, slot_tp_richcompare
, richcmp_lt
,
5348 "x.__lt__(y) <==> x<y"),
5349 TPSLOT("__le__", tp_richcompare
, slot_tp_richcompare
, richcmp_le
,
5350 "x.__le__(y) <==> x<=y"),
5351 TPSLOT("__eq__", tp_richcompare
, slot_tp_richcompare
, richcmp_eq
,
5352 "x.__eq__(y) <==> x==y"),
5353 TPSLOT("__ne__", tp_richcompare
, slot_tp_richcompare
, richcmp_ne
,
5354 "x.__ne__(y) <==> x!=y"),
5355 TPSLOT("__gt__", tp_richcompare
, slot_tp_richcompare
, richcmp_gt
,
5356 "x.__gt__(y) <==> x>y"),
5357 TPSLOT("__ge__", tp_richcompare
, slot_tp_richcompare
, richcmp_ge
,
5358 "x.__ge__(y) <==> x>=y"),
5359 TPSLOT("__iter__", tp_iter
, slot_tp_iter
, wrap_unaryfunc
,
5360 "x.__iter__() <==> iter(x)"),
5361 TPSLOT("next", tp_iternext
, slot_tp_iternext
, wrap_next
,
5362 "x.next() -> the next value, or raise StopIteration"),
5363 TPSLOT("__get__", tp_descr_get
, slot_tp_descr_get
, wrap_descr_get
,
5364 "descr.__get__(obj[, type]) -> value"),
5365 TPSLOT("__set__", tp_descr_set
, slot_tp_descr_set
, wrap_descr_set
,
5366 "descr.__set__(obj, value)"),
5367 TPSLOT("__delete__", tp_descr_set
, slot_tp_descr_set
,
5368 wrap_descr_delete
, "descr.__delete__(obj)"),
5369 FLSLOT("__init__", tp_init
, slot_tp_init
, (wrapperfunc
)wrap_init
,
5370 "x.__init__(...) initializes x; "
5371 "see x.__class__.__doc__ for signature",
5372 PyWrapperFlag_KEYWORDS
),
5373 TPSLOT("__new__", tp_new
, slot_tp_new
, NULL
, ""),
5374 TPSLOT("__del__", tp_del
, slot_tp_del
, NULL
, ""),
5378 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5379 pointer to the actual slot. This is not quite the same as simply adding
5380 the offset to the type pointer, since it takes care to indirect through the
5381 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5382 indirection pointer is NULL. */
5384 slotptr(PyTypeObject
*type
, int ioffset
)
5387 long offset
= ioffset
;
5389 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5390 assert(offset
>= 0);
5391 assert((size_t)offset
< offsetof(PyHeapTypeObject
, as_buffer
));
5392 if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_sequence
)) {
5393 ptr
= (char *)type
->tp_as_sequence
;
5394 offset
-= offsetof(PyHeapTypeObject
, as_sequence
);
5396 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_mapping
)) {
5397 ptr
= (char *)type
->tp_as_mapping
;
5398 offset
-= offsetof(PyHeapTypeObject
, as_mapping
);
5400 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_number
)) {
5401 ptr
= (char *)type
->tp_as_number
;
5402 offset
-= offsetof(PyHeapTypeObject
, as_number
);
5409 return (void **)ptr
;
5412 /* Length of array of slotdef pointers used to store slots with the
5413 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5414 the same __name__, for any __name__. Since that's a static property, it is
5415 appropriate to declare fixed-size arrays for this. */
5416 #define MAX_EQUIV 10
5418 /* Return a slot pointer for a given name, but ONLY if the attribute has
5419 exactly one slot function. The name must be an interned string. */
5421 resolve_slotdups(PyTypeObject
*type
, PyObject
*name
)
5423 /* XXX Maybe this could be optimized more -- but is it worth it? */
5425 /* pname and ptrs act as a little cache */
5426 static PyObject
*pname
;
5427 static slotdef
*ptrs
[MAX_EQUIV
];
5431 if (pname
!= name
) {
5432 /* Collect all slotdefs that match name into ptrs. */
5435 for (p
= slotdefs
; p
->name_strobj
; p
++) {
5436 if (p
->name_strobj
== name
)
5442 /* Look in all matching slots of the type; if exactly one of these has
5443 a filled-in slot, return its value. Otherwise return NULL. */
5445 for (pp
= ptrs
; *pp
; pp
++) {
5446 ptr
= slotptr(type
, (*pp
)->offset
);
5447 if (ptr
== NULL
|| *ptr
== NULL
)
5456 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5457 does some incredibly complex thinking and then sticks something into the
5458 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5459 interests, and then stores a generic wrapper or a specific function into
5460 the slot.) Return a pointer to the next slotdef with a different offset,
5461 because that's convenient for fixup_slot_dispatchers(). */
5463 update_one_slot(PyTypeObject
*type
, slotdef
*p
)
5466 PyWrapperDescrObject
*d
;
5467 void *generic
= NULL
, *specific
= NULL
;
5468 int use_generic
= 0;
5469 int offset
= p
->offset
;
5470 void **ptr
= slotptr(type
, offset
);
5475 } while (p
->offset
== offset
);
5479 descr
= _PyType_Lookup(type
, p
->name_strobj
);
5482 if (descr
->ob_type
== &PyWrapperDescr_Type
) {
5483 void **tptr
= resolve_slotdups(type
, p
->name_strobj
);
5484 if (tptr
== NULL
|| tptr
== ptr
)
5485 generic
= p
->function
;
5486 d
= (PyWrapperDescrObject
*)descr
;
5487 if (d
->d_base
->wrapper
== p
->wrapper
&&
5488 PyType_IsSubtype(type
, d
->d_type
))
5490 if (specific
== NULL
||
5491 specific
== d
->d_wrapped
)
5492 specific
= d
->d_wrapped
;
5497 else if (descr
->ob_type
== &PyCFunction_Type
&&
5498 PyCFunction_GET_FUNCTION(descr
) ==
5499 (PyCFunction
)tp_new_wrapper
&&
5500 strcmp(p
->name
, "__new__") == 0)
5502 /* The __new__ wrapper is not a wrapper descriptor,
5503 so must be special-cased differently.
5504 If we don't do this, creating an instance will
5505 always use slot_tp_new which will look up
5506 __new__ in the MRO which will call tp_new_wrapper
5507 which will look through the base classes looking
5508 for a static base and call its tp_new (usually
5509 PyType_GenericNew), after performing various
5510 sanity checks and constructing a new argument
5511 list. Cut all that nonsense short -- this speeds
5512 up instance creation tremendously. */
5513 specific
= (void *)type
->tp_new
;
5514 /* XXX I'm not 100% sure that there isn't a hole
5515 in this reasoning that requires additional
5516 sanity checks. I'll buy the first person to
5517 point out a bug in this reasoning a beer. */
5521 generic
= p
->function
;
5523 } while ((++p
)->offset
== offset
);
5524 if (specific
&& !use_generic
)
5531 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5532 This is a callback for update_subclasses(). */
5534 update_slots_callback(PyTypeObject
*type
, void *data
)
5536 slotdef
**pp
= (slotdef
**)data
;
5539 update_one_slot(type
, *pp
);
5543 /* Comparison function for qsort() to compare slotdefs by their offset, and
5544 for equal offset by their address (to force a stable sort). */
5546 slotdef_cmp(const void *aa
, const void *bb
)
5548 const slotdef
*a
= (const slotdef
*)aa
, *b
= (const slotdef
*)bb
;
5549 int c
= a
->offset
- b
->offset
;
5553 /* Cannot use a-b, as this gives off_t,
5554 which may lose precision when converted to int. */
5555 return (a
> b
) ? 1 : (a
< b
) ? -1 : 0;
5558 /* Initialize the slotdefs table by adding interned string objects for the
5559 names and sorting the entries. */
5564 static int initialized
= 0;
5568 for (p
= slotdefs
; p
->name
; p
++) {
5569 p
->name_strobj
= PyString_InternFromString(p
->name
);
5570 if (!p
->name_strobj
)
5571 Py_FatalError("Out of memory interning slotdef names");
5573 qsort((void *)slotdefs
, (size_t)(p
-slotdefs
), sizeof(slotdef
),
5578 /* Update the slots after assignment to a class (type) attribute. */
5580 update_slot(PyTypeObject
*type
, PyObject
*name
)
5582 slotdef
*ptrs
[MAX_EQUIV
];
5589 for (p
= slotdefs
; p
->name
; p
++) {
5590 /* XXX assume name is interned! */
5591 if (p
->name_strobj
== name
)
5595 for (pp
= ptrs
; *pp
; pp
++) {
5598 while (p
> slotdefs
&& (p
-1)->offset
== offset
)
5602 if (ptrs
[0] == NULL
)
5603 return 0; /* Not an attribute that affects any slots */
5604 return update_subclasses(type
, name
,
5605 update_slots_callback
, (void *)ptrs
);
5608 /* Store the proper functions in the slot dispatches at class (type)
5609 definition time, based upon which operations the class overrides in its
5612 fixup_slot_dispatchers(PyTypeObject
*type
)
5617 for (p
= slotdefs
; p
->name
; )
5618 p
= update_one_slot(type
, p
);
5622 update_all_slots(PyTypeObject
* type
)
5627 for (p
= slotdefs
; p
->name
; p
++) {
5628 /* update_slot returns int but can't actually fail */
5629 update_slot(type
, p
->name_strobj
);
5633 /* recurse_down_subclasses() and update_subclasses() are mutually
5634 recursive functions to call a callback for all subclasses,
5635 but refraining from recursing into subclasses that define 'name'. */
5638 update_subclasses(PyTypeObject
*type
, PyObject
*name
,
5639 update_callback callback
, void *data
)
5641 if (callback(type
, data
) < 0)
5643 return recurse_down_subclasses(type
, name
, callback
, data
);
5647 recurse_down_subclasses(PyTypeObject
*type
, PyObject
*name
,
5648 update_callback callback
, void *data
)
5650 PyTypeObject
*subclass
;
5651 PyObject
*ref
, *subclasses
, *dict
;
5654 subclasses
= type
->tp_subclasses
;
5655 if (subclasses
== NULL
)
5657 assert(PyList_Check(subclasses
));
5658 n
= PyList_GET_SIZE(subclasses
);
5659 for (i
= 0; i
< n
; i
++) {
5660 ref
= PyList_GET_ITEM(subclasses
, i
);
5661 assert(PyWeakref_CheckRef(ref
));
5662 subclass
= (PyTypeObject
*)PyWeakref_GET_OBJECT(ref
);
5663 assert(subclass
!= NULL
);
5664 if ((PyObject
*)subclass
== Py_None
)
5666 assert(PyType_Check(subclass
));
5667 /* Avoid recursing down into unaffected classes */
5668 dict
= subclass
->tp_dict
;
5669 if (dict
!= NULL
&& PyDict_Check(dict
) &&
5670 PyDict_GetItem(dict
, name
) != NULL
)
5672 if (update_subclasses(subclass
, name
, callback
, data
) < 0)
5678 /* This function is called by PyType_Ready() to populate the type's
5679 dictionary with method descriptors for function slots. For each
5680 function slot (like tp_repr) that's defined in the type, one or more
5681 corresponding descriptors are added in the type's tp_dict dictionary
5682 under the appropriate name (like __repr__). Some function slots
5683 cause more than one descriptor to be added (for example, the nb_add
5684 slot adds both __add__ and __radd__ descriptors) and some function
5685 slots compete for the same descriptor (for example both sq_item and
5686 mp_subscript generate a __getitem__ descriptor).
5688 In the latter case, the first slotdef entry encoutered wins. Since
5689 slotdef entries are sorted by the offset of the slot in the
5690 PyHeapTypeObject, this gives us some control over disambiguating
5691 between competing slots: the members of PyHeapTypeObject are listed
5692 from most general to least general, so the most general slot is
5693 preferred. In particular, because as_mapping comes before as_sequence,
5694 for a type that defines both mp_subscript and sq_item, mp_subscript
5697 This only adds new descriptors and doesn't overwrite entries in
5698 tp_dict that were previously defined. The descriptors contain a
5699 reference to the C function they must call, so that it's safe if they
5700 are copied into a subtype's __dict__ and the subtype has a different
5701 C function in its slot -- calling the method defined by the
5702 descriptor will call the C function that was used to create it,
5703 rather than the C function present in the slot when it is called.
5704 (This is important because a subtype may have a C function in the
5705 slot that calls the method from the dictionary, and we want to avoid
5706 infinite recursion here.) */
5709 add_operators(PyTypeObject
*type
)
5711 PyObject
*dict
= type
->tp_dict
;
5717 for (p
= slotdefs
; p
->name
; p
++) {
5718 if (p
->wrapper
== NULL
)
5720 ptr
= slotptr(type
, p
->offset
);
5723 if (PyDict_GetItem(dict
, p
->name_strobj
))
5725 descr
= PyDescr_NewWrapper(type
, p
, *ptr
);
5728 if (PyDict_SetItem(dict
, p
->name_strobj
, descr
) < 0)
5732 if (type
->tp_new
!= NULL
) {
5733 if (add_tp_new_wrapper(type
) < 0)
5740 /* Cooperative 'super' */
5746 PyTypeObject
*obj_type
;
5749 static PyMemberDef super_members
[] = {
5750 {"__thisclass__", T_OBJECT
, offsetof(superobject
, type
), READONLY
,
5751 "the class invoking super()"},
5752 {"__self__", T_OBJECT
, offsetof(superobject
, obj
), READONLY
,
5753 "the instance invoking super(); may be None"},
5754 {"__self_class__", T_OBJECT
, offsetof(superobject
, obj_type
), READONLY
,
5755 "the type of the instance invoking super(); may be None"},
5760 super_dealloc(PyObject
*self
)
5762 superobject
*su
= (superobject
*)self
;
5764 _PyObject_GC_UNTRACK(self
);
5765 Py_XDECREF(su
->obj
);
5766 Py_XDECREF(su
->type
);
5767 Py_XDECREF(su
->obj_type
);
5768 self
->ob_type
->tp_free(self
);
5772 super_repr(PyObject
*self
)
5774 superobject
*su
= (superobject
*)self
;
5777 return PyString_FromFormat(
5778 "<super: <class '%s'>, <%s object>>",
5779 su
->type
? su
->type
->tp_name
: "NULL",
5780 su
->obj_type
->tp_name
);
5782 return PyString_FromFormat(
5783 "<super: <class '%s'>, NULL>",
5784 su
->type
? su
->type
->tp_name
: "NULL");
5788 super_getattro(PyObject
*self
, PyObject
*name
)
5790 superobject
*su
= (superobject
*)self
;
5791 int skip
= su
->obj_type
== NULL
;
5794 /* We want __class__ to return the class of the super object
5795 (i.e. super, or a subclass), not the class of su->obj. */
5796 skip
= (PyString_Check(name
) &&
5797 PyString_GET_SIZE(name
) == 9 &&
5798 strcmp(PyString_AS_STRING(name
), "__class__") == 0);
5802 PyObject
*mro
, *res
, *tmp
, *dict
;
5803 PyTypeObject
*starttype
;
5807 starttype
= su
->obj_type
;
5808 mro
= starttype
->tp_mro
;
5813 assert(PyTuple_Check(mro
));
5814 n
= PyTuple_GET_SIZE(mro
);
5816 for (i
= 0; i
< n
; i
++) {
5817 if ((PyObject
*)(su
->type
) == PyTuple_GET_ITEM(mro
, i
))
5822 for (; i
< n
; i
++) {
5823 tmp
= PyTuple_GET_ITEM(mro
, i
);
5824 if (PyType_Check(tmp
))
5825 dict
= ((PyTypeObject
*)tmp
)->tp_dict
;
5826 else if (PyClass_Check(tmp
))
5827 dict
= ((PyClassObject
*)tmp
)->cl_dict
;
5830 res
= PyDict_GetItem(dict
, name
);
5833 f
= res
->ob_type
->tp_descr_get
;
5836 /* Only pass 'obj' param if
5837 this is instance-mode super
5840 (su
->obj
== (PyObject
*)
5844 (PyObject
*)starttype
);
5852 return PyObject_GenericGetAttr(self
, name
);
5855 static PyTypeObject
*
5856 supercheck(PyTypeObject
*type
, PyObject
*obj
)
5858 /* Check that a super() call makes sense. Return a type object.
5860 obj can be a new-style class, or an instance of one:
5862 - If it is a class, it must be a subclass of 'type'. This case is
5863 used for class methods; the return value is obj.
5865 - If it is an instance, it must be an instance of 'type'. This is
5866 the normal case; the return value is obj.__class__.
5868 But... when obj is an instance, we want to allow for the case where
5869 obj->ob_type is not a subclass of type, but obj.__class__ is!
5870 This will allow using super() with a proxy for obj.
5873 /* Check for first bullet above (special case) */
5874 if (PyType_Check(obj
) && PyType_IsSubtype((PyTypeObject
*)obj
, type
)) {
5876 return (PyTypeObject
*)obj
;
5880 if (PyType_IsSubtype(obj
->ob_type
, type
)) {
5881 Py_INCREF(obj
->ob_type
);
5882 return obj
->ob_type
;
5885 /* Try the slow way */
5886 static PyObject
*class_str
= NULL
;
5887 PyObject
*class_attr
;
5889 if (class_str
== NULL
) {
5890 class_str
= PyString_FromString("__class__");
5891 if (class_str
== NULL
)
5895 class_attr
= PyObject_GetAttr(obj
, class_str
);
5897 if (class_attr
!= NULL
&&
5898 PyType_Check(class_attr
) &&
5899 (PyTypeObject
*)class_attr
!= obj
->ob_type
)
5901 int ok
= PyType_IsSubtype(
5902 (PyTypeObject
*)class_attr
, type
);
5904 return (PyTypeObject
*)class_attr
;
5907 if (class_attr
== NULL
)
5910 Py_DECREF(class_attr
);
5913 PyErr_SetString(PyExc_TypeError
,
5914 "super(type, obj): "
5915 "obj must be an instance or subtype of type");
5920 super_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
5922 superobject
*su
= (superobject
*)self
;
5923 superobject
*newobj
;
5925 if (obj
== NULL
|| obj
== Py_None
|| su
->obj
!= NULL
) {
5926 /* Not binding to an object, or already bound */
5930 if (su
->ob_type
!= &PySuper_Type
)
5931 /* If su is an instance of a (strict) subclass of super,
5933 return PyObject_CallFunctionObjArgs((PyObject
*)su
->ob_type
,
5934 su
->type
, obj
, NULL
);
5936 /* Inline the common case */
5937 PyTypeObject
*obj_type
= supercheck(su
->type
, obj
);
5938 if (obj_type
== NULL
)
5940 newobj
= (superobject
*)PySuper_Type
.tp_new(&PySuper_Type
,
5944 Py_INCREF(su
->type
);
5946 newobj
->type
= su
->type
;
5948 newobj
->obj_type
= obj_type
;
5949 return (PyObject
*)newobj
;
5954 super_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5956 superobject
*su
= (superobject
*)self
;
5958 PyObject
*obj
= NULL
;
5959 PyTypeObject
*obj_type
= NULL
;
5961 if (!_PyArg_NoKeywords("super", kwds
))
5963 if (!PyArg_ParseTuple(args
, "O!|O:super", &PyType_Type
, &type
, &obj
))
5968 obj_type
= supercheck(type
, obj
);
5969 if (obj_type
== NULL
)
5976 su
->obj_type
= obj_type
;
5980 PyDoc_STRVAR(super_doc
,
5981 "super(type) -> unbound super object\n"
5982 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5983 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5984 "Typical use to call a cooperative superclass method:\n"
5986 " def meth(self, arg):\n"
5987 " super(C, self).meth(arg)");
5990 super_traverse(PyObject
*self
, visitproc visit
, void *arg
)
5992 superobject
*su
= (superobject
*)self
;
5996 Py_VISIT(su
->obj_type
);
6001 PyTypeObject PySuper_Type
= {
6002 PyObject_HEAD_INIT(&PyType_Type
)
6004 "super", /* tp_name */
6005 sizeof(superobject
), /* tp_basicsize */
6006 0, /* tp_itemsize */
6008 super_dealloc
, /* tp_dealloc */
6013 super_repr
, /* tp_repr */
6014 0, /* tp_as_number */
6015 0, /* tp_as_sequence */
6016 0, /* tp_as_mapping */
6020 super_getattro
, /* tp_getattro */
6021 0, /* tp_setattro */
6022 0, /* tp_as_buffer */
6023 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
6024 Py_TPFLAGS_BASETYPE
, /* tp_flags */
6025 super_doc
, /* tp_doc */
6026 super_traverse
, /* tp_traverse */
6028 0, /* tp_richcompare */
6029 0, /* tp_weaklistoffset */
6031 0, /* tp_iternext */
6033 super_members
, /* tp_members */
6037 super_descr_get
, /* tp_descr_get */
6038 0, /* tp_descr_set */
6039 0, /* tp_dictoffset */
6040 super_init
, /* tp_init */
6041 PyType_GenericAlloc
, /* tp_alloc */
6042 PyType_GenericNew
, /* tp_new */
6043 PyObject_GC_Del
, /* tp_free */