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
;
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
;
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
, (int)(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
)
189 PyTypeObject
*new_base
, *old_base
;
190 PyObject
*old_bases
, *old_mro
;
192 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
193 PyErr_Format(PyExc_TypeError
,
194 "can't set %s.__bases__", type
->tp_name
);
198 PyErr_Format(PyExc_TypeError
,
199 "can't delete %s.__bases__", type
->tp_name
);
202 if (!PyTuple_Check(value
)) {
203 PyErr_Format(PyExc_TypeError
,
204 "can only assign tuple to %s.__bases__, not %s",
205 type
->tp_name
, value
->ob_type
->tp_name
);
208 if (PyTuple_GET_SIZE(value
) == 0) {
209 PyErr_Format(PyExc_TypeError
,
210 "can only assign non-empty tuple to %s.__bases__, not ()",
214 for (i
= 0; i
< PyTuple_GET_SIZE(value
); i
++) {
215 ob
= PyTuple_GET_ITEM(value
, i
);
216 if (!PyClass_Check(ob
) && !PyType_Check(ob
)) {
219 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
220 type
->tp_name
, ob
->ob_type
->tp_name
);
223 if (PyType_Check(ob
)) {
224 if (PyType_IsSubtype((PyTypeObject
*)ob
, type
)) {
225 PyErr_SetString(PyExc_TypeError
,
226 "a __bases__ item causes an inheritance cycle");
232 new_base
= best_base(value
);
238 if (!compatible_for_assignment(type
->tp_base
, new_base
, "__bases__"))
244 old_bases
= type
->tp_bases
;
245 old_base
= type
->tp_base
;
246 old_mro
= type
->tp_mro
;
248 type
->tp_bases
= value
;
249 type
->tp_base
= new_base
;
251 if (mro_internal(type
) < 0) {
255 temp
= PyList_New(0);
259 r
= mro_subclasses(type
, temp
);
262 for (i
= 0; i
< PyList_Size(temp
); i
++) {
265 PyArg_UnpackTuple(PyList_GET_ITEM(temp
, i
),
266 "", 2, 2, &cls
, &mro
);
267 Py_DECREF(cls
->tp_mro
);
269 Py_INCREF(cls
->tp_mro
);
277 /* any base that was in __bases__ but now isn't, we
278 need to remove |type| from its tp_subclasses.
279 conversely, any class now in __bases__ that wasn't
280 needs to have |type| added to its subclasses. */
282 /* for now, sod that: just remove from all old_bases,
283 add to all new_bases */
285 for (i
= PyTuple_GET_SIZE(old_bases
) - 1; i
>= 0; i
--) {
286 ob
= PyTuple_GET_ITEM(old_bases
, i
);
287 if (PyType_Check(ob
)) {
289 (PyTypeObject
*)ob
, type
);
293 for (i
= PyTuple_GET_SIZE(value
) - 1; i
>= 0; i
--) {
294 ob
= PyTuple_GET_ITEM(value
, i
);
295 if (PyType_Check(ob
)) {
296 if (add_subclass((PyTypeObject
*)ob
, type
) < 0)
301 update_all_slots(type
);
303 Py_DECREF(old_bases
);
310 Py_DECREF(type
->tp_bases
);
311 Py_DECREF(type
->tp_base
);
312 if (type
->tp_mro
!= old_mro
) {
313 Py_DECREF(type
->tp_mro
);
316 type
->tp_bases
= old_bases
;
317 type
->tp_base
= old_base
;
318 type
->tp_mro
= old_mro
;
324 type_dict(PyTypeObject
*type
, void *context
)
326 if (type
->tp_dict
== NULL
) {
330 return PyDictProxy_New(type
->tp_dict
);
334 type_get_doc(PyTypeObject
*type
, void *context
)
337 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) && type
->tp_doc
!= NULL
)
338 return PyString_FromString(type
->tp_doc
);
339 result
= PyDict_GetItemString(type
->tp_dict
, "__doc__");
340 if (result
== NULL
) {
344 else if (result
->ob_type
->tp_descr_get
) {
345 result
= result
->ob_type
->tp_descr_get(result
, NULL
,
354 static PyGetSetDef type_getsets
[] = {
355 {"__name__", (getter
)type_name
, (setter
)type_set_name
, NULL
},
356 {"__bases__", (getter
)type_get_bases
, (setter
)type_set_bases
, NULL
},
357 {"__module__", (getter
)type_module
, (setter
)type_set_module
, NULL
},
358 {"__dict__", (getter
)type_dict
, NULL
, NULL
},
359 {"__doc__", (getter
)type_get_doc
, NULL
, NULL
},
364 type_compare(PyObject
*v
, PyObject
*w
)
366 /* This is called with type objects only. So we
367 can just compare the addresses. */
368 Py_uintptr_t vv
= (Py_uintptr_t
)v
;
369 Py_uintptr_t ww
= (Py_uintptr_t
)w
;
370 return (vv
< ww
) ? -1 : (vv
> ww
) ? 1 : 0;
374 type_repr(PyTypeObject
*type
)
376 PyObject
*mod
, *name
, *rtn
;
379 mod
= type_module(type
, NULL
);
382 else if (!PyString_Check(mod
)) {
386 name
= type_name(type
, NULL
);
390 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
395 if (mod
!= NULL
&& strcmp(PyString_AS_STRING(mod
), "__builtin__")) {
396 rtn
= PyString_FromFormat("<%s '%s.%s'>",
398 PyString_AS_STRING(mod
),
399 PyString_AS_STRING(name
));
402 rtn
= PyString_FromFormat("<%s '%s'>", kind
, type
->tp_name
);
410 type_call(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
414 if (type
->tp_new
== NULL
) {
415 PyErr_Format(PyExc_TypeError
,
416 "cannot create '%.100s' instances",
421 obj
= type
->tp_new(type
, args
, kwds
);
423 /* Ugly exception: when the call was type(something),
424 don't call tp_init on the result. */
425 if (type
== &PyType_Type
&&
426 PyTuple_Check(args
) && PyTuple_GET_SIZE(args
) == 1 &&
428 (PyDict_Check(kwds
) && PyDict_Size(kwds
) == 0)))
430 /* If the returned object is not an instance of type,
431 it won't be initialized. */
432 if (!PyType_IsSubtype(obj
->ob_type
, type
))
435 if (PyType_HasFeature(type
, Py_TPFLAGS_HAVE_CLASS
) &&
436 type
->tp_init
!= NULL
&&
437 type
->tp_init(obj
, args
, kwds
) < 0) {
446 PyType_GenericAlloc(PyTypeObject
*type
, int nitems
)
449 const size_t size
= _PyObject_VAR_SIZE(type
, nitems
+1);
450 /* note that we need to add one, for the sentinel */
452 if (PyType_IS_GC(type
))
453 obj
= _PyObject_GC_Malloc(size
);
455 obj
= PyObject_MALLOC(size
);
458 return PyErr_NoMemory();
460 memset(obj
, '\0', size
);
462 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
465 if (type
->tp_itemsize
== 0)
466 PyObject_INIT(obj
, type
);
468 (void) PyObject_INIT_VAR((PyVarObject
*)obj
, type
, nitems
);
470 if (PyType_IS_GC(type
))
471 _PyObject_GC_TRACK(obj
);
476 PyType_GenericNew(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
478 return type
->tp_alloc(type
, 0);
481 /* Helpers for subtyping */
484 traverse_slots(PyTypeObject
*type
, PyObject
*self
, visitproc visit
, void *arg
)
490 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
491 for (i
= 0; i
< n
; i
++, mp
++) {
492 if (mp
->type
== T_OBJECT_EX
) {
493 char *addr
= (char *)self
+ mp
->offset
;
494 PyObject
*obj
= *(PyObject
**)addr
;
496 int err
= visit(obj
, arg
);
506 subtype_traverse(PyObject
*self
, visitproc visit
, void *arg
)
508 PyTypeObject
*type
, *base
;
509 traverseproc basetraverse
;
511 /* Find the nearest base with a different tp_traverse,
512 and traverse slots while we're at it */
513 type
= self
->ob_type
;
515 while ((basetraverse
= base
->tp_traverse
) == subtype_traverse
) {
517 int err
= traverse_slots(base
, self
, visit
, arg
);
521 base
= base
->tp_base
;
525 if (type
->tp_dictoffset
!= base
->tp_dictoffset
) {
526 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
527 if (dictptr
&& *dictptr
) {
528 int err
= visit(*dictptr
, arg
);
534 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
535 /* For a heaptype, the instances count as references
536 to the type. Traverse the type so the collector
537 can find cycles involving this link. */
538 int err
= visit((PyObject
*)type
, arg
);
544 return basetraverse(self
, visit
, arg
);
549 clear_slots(PyTypeObject
*type
, PyObject
*self
)
555 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
556 for (i
= 0; i
< n
; i
++, mp
++) {
557 if (mp
->type
== T_OBJECT_EX
&& !(mp
->flags
& READONLY
)) {
558 char *addr
= (char *)self
+ mp
->offset
;
559 PyObject
*obj
= *(PyObject
**)addr
;
562 *(PyObject
**)addr
= NULL
;
569 subtype_clear(PyObject
*self
)
571 PyTypeObject
*type
, *base
;
574 /* Find the nearest base with a different tp_clear
575 and clear slots while we're at it */
576 type
= self
->ob_type
;
578 while ((baseclear
= base
->tp_clear
) == subtype_clear
) {
580 clear_slots(base
, self
);
581 base
= base
->tp_base
;
585 /* There's no need to clear the instance dict (if any);
586 the collector will call its tp_clear handler. */
589 return baseclear(self
);
594 subtype_dealloc(PyObject
*self
)
596 PyTypeObject
*type
, *base
;
597 destructor basedealloc
;
599 /* Extract the type; we expect it to be a heap type */
600 type
= self
->ob_type
;
601 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
603 /* Test whether the type has GC exactly once */
605 if (!PyType_IS_GC(type
)) {
606 /* It's really rare to find a dynamic type that doesn't have
607 GC; it can only happen when deriving from 'object' and not
608 adding any slots or instance variables. This allows
609 certain simplifications: there's no need to call
610 clear_slots(), or DECREF the dict, or clear weakrefs. */
612 /* Maybe call finalizer; exit early if resurrected */
615 if (self
->ob_refcnt
> 0)
619 /* Find the nearest base with a different tp_dealloc */
621 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
622 assert(base
->ob_size
== 0);
623 base
= base
->tp_base
;
627 /* Call the base tp_dealloc() */
631 /* Can't reference self beyond this point */
638 /* We get here only if the type has GC */
640 /* UnTrack and re-Track around the trashcan macro, alas */
641 /* See explanation at end of function for full disclosure */
642 PyObject_GC_UnTrack(self
);
643 ++_PyTrash_delete_nesting
;
644 Py_TRASHCAN_SAFE_BEGIN(self
);
645 --_PyTrash_delete_nesting
;
646 /* DO NOT restore GC tracking at this point. weakref callbacks
647 * (if any, and whether directly here or indirectly in something we
648 * call) may trigger GC, and if self is tracked at that point, it
649 * will look like trash to GC and GC will try to delete self again.
652 /* Find the nearest base with a different tp_dealloc */
654 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
655 base
= base
->tp_base
;
659 /* If we added a weaklist, we clear it. Do this *before* calling
660 the finalizer (__del__), clearing slots, or clearing the instance
663 if (type
->tp_weaklistoffset
&& !base
->tp_weaklistoffset
)
664 PyObject_ClearWeakRefs(self
);
666 /* Maybe call finalizer; exit early if resurrected */
668 _PyObject_GC_TRACK(self
);
670 if (self
->ob_refcnt
> 0)
671 goto endlabel
; /* resurrected */
673 _PyObject_GC_UNTRACK(self
);
676 /* Clear slots up to the nearest base with a different tp_dealloc */
678 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
680 clear_slots(base
, self
);
681 base
= base
->tp_base
;
685 /* If we added a dict, DECREF it */
686 if (type
->tp_dictoffset
&& !base
->tp_dictoffset
) {
687 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
688 if (dictptr
!= NULL
) {
689 PyObject
*dict
= *dictptr
;
697 /* Call the base tp_dealloc(); first retrack self if
698 * basedealloc knows about gc.
700 if (PyType_IS_GC(base
))
701 _PyObject_GC_TRACK(self
);
705 /* Can't reference self beyond this point */
709 ++_PyTrash_delete_nesting
;
710 Py_TRASHCAN_SAFE_END(self
);
711 --_PyTrash_delete_nesting
;
713 /* Explanation of the weirdness around the trashcan macros:
715 Q. What do the trashcan macros do?
717 A. Read the comment titled "Trashcan mechanism" in object.h.
718 For one, this explains why there must be a call to GC-untrack
719 before the trashcan begin macro. Without understanding the
720 trashcan code, the answers to the following questions don't make
723 Q. Why do we GC-untrack before the trashcan and then immediately
724 GC-track again afterward?
726 A. In the case that the base class is GC-aware, the base class
727 probably GC-untracks the object. If it does that using the
728 UNTRACK macro, this will crash when the object is already
729 untracked. Because we don't know what the base class does, the
730 only safe thing is to make sure the object is tracked when we
731 call the base class dealloc. But... The trashcan begin macro
732 requires that the object is *untracked* before it is called. So
739 Q. Why did the last question say "immediately GC-track again"?
740 It's nowhere near immediately.
742 A. Because the code *used* to re-track immediately. Bad Idea.
743 self has a refcount of 0, and if gc ever gets its hands on it
744 (which can happen if any weakref callback gets invoked), it
745 looks like trash to gc too, and gc also tries to delete self
746 then. But we're already deleting self. Double dealloction is
749 Q. Why the bizarre (net-zero) manipulation of
750 _PyTrash_delete_nesting around the trashcan macros?
752 A. Some base classes (e.g. list) also use the trashcan mechanism.
753 The following scenario used to be possible:
755 - suppose the trashcan level is one below the trashcan limit
757 - subtype_dealloc() is called
759 - the trashcan limit is not yet reached, so the trashcan level
760 is incremented and the code between trashcan begin and end is
763 - this destroys much of the object's contents, including its
766 - basedealloc() is called; this is really list_dealloc(), or
767 some other type which also uses the trashcan macros
769 - the trashcan limit is now reached, so the object is put on the
770 trashcan's to-be-deleted-later list
772 - basedealloc() returns
774 - subtype_dealloc() decrefs the object's type
776 - subtype_dealloc() returns
778 - later, the trashcan code starts deleting the objects from its
779 to-be-deleted-later list
781 - subtype_dealloc() is called *AGAIN* for the same object
783 - at the very least (if the destroyed slots and __dict__ don't
784 cause problems) the object's type gets decref'ed a second
785 time, which is *BAD*!!!
787 The remedy is to make sure that if the code between trashcan
788 begin and end in subtype_dealloc() is called, the code between
789 trashcan begin and end in basedealloc() will also be called.
790 This is done by decrementing the level after passing into the
791 trashcan block, and incrementing it just before leaving the
794 But now it's possible that a chain of objects consisting solely
795 of objects whose deallocator is subtype_dealloc() will defeat
796 the trashcan mechanism completely: the decremented level means
797 that the effective level never reaches the limit. Therefore, we
798 *increment* the level *before* entering the trashcan block, and
799 matchingly decrement it after leaving. This means the trashcan
800 code will trigger a little early, but that's no big deal.
802 Q. Are there any live examples of code in need of all this
805 A. Yes. See SF bug 668433 for code that crashed (when Python was
806 compiled in debug mode) before the trashcan level manipulations
807 were added. For more discussion, see SF patches 581742, 575073
812 static PyTypeObject
*solid_base(PyTypeObject
*type
);
814 /* type test with subclassing support */
817 PyType_IsSubtype(PyTypeObject
*a
, PyTypeObject
*b
)
821 if (!(a
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
))
822 return b
== a
|| b
== &PyBaseObject_Type
;
826 /* Deal with multiple inheritance without recursion
827 by walking the MRO tuple */
829 assert(PyTuple_Check(mro
));
830 n
= PyTuple_GET_SIZE(mro
);
831 for (i
= 0; i
< n
; i
++) {
832 if (PyTuple_GET_ITEM(mro
, i
) == (PyObject
*)b
)
838 /* a is not completely initilized yet; follow tp_base */
844 return b
== &PyBaseObject_Type
;
848 /* Internal routines to do a method lookup in the type
849 without looking in the instance dictionary
850 (so we can't use PyObject_GetAttr) but still binding
851 it to the instance. The arguments are the object,
852 the method name as a C string, and the address of a
853 static variable used to cache the interned Python string.
857 - lookup_maybe() returns NULL without raising an exception
858 when the _PyType_Lookup() call fails;
860 - lookup_method() always raises an exception upon errors.
864 lookup_maybe(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
868 if (*attrobj
== NULL
) {
869 *attrobj
= PyString_InternFromString(attrstr
);
870 if (*attrobj
== NULL
)
873 res
= _PyType_Lookup(self
->ob_type
, *attrobj
);
876 if ((f
= res
->ob_type
->tp_descr_get
) == NULL
)
879 res
= f(res
, self
, (PyObject
*)(self
->ob_type
));
885 lookup_method(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
887 PyObject
*res
= lookup_maybe(self
, attrstr
, attrobj
);
888 if (res
== NULL
&& !PyErr_Occurred())
889 PyErr_SetObject(PyExc_AttributeError
, *attrobj
);
893 /* A variation of PyObject_CallMethod that uses lookup_method()
894 instead of PyObject_GetAttrString(). This uses the same convention
895 as lookup_method to cache the interned name string object. */
898 call_method(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
901 PyObject
*args
, *func
= 0, *retval
;
902 va_start(va
, format
);
904 func
= lookup_maybe(o
, name
, nameobj
);
907 if (!PyErr_Occurred())
908 PyErr_SetObject(PyExc_AttributeError
, *nameobj
);
912 if (format
&& *format
)
913 args
= Py_VaBuildValue(format
, va
);
915 args
= PyTuple_New(0);
922 assert(PyTuple_Check(args
));
923 retval
= PyObject_Call(func
, args
, NULL
);
931 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
934 call_maybe(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
937 PyObject
*args
, *func
= 0, *retval
;
938 va_start(va
, format
);
940 func
= lookup_maybe(o
, name
, nameobj
);
943 if (!PyErr_Occurred()) {
944 Py_INCREF(Py_NotImplemented
);
945 return Py_NotImplemented
;
950 if (format
&& *format
)
951 args
= Py_VaBuildValue(format
, va
);
953 args
= PyTuple_New(0);
960 assert(PyTuple_Check(args
));
961 retval
= PyObject_Call(func
, args
, NULL
);
970 fill_classic_mro(PyObject
*mro
, PyObject
*cls
)
972 PyObject
*bases
, *base
;
975 assert(PyList_Check(mro
));
976 assert(PyClass_Check(cls
));
977 i
= PySequence_Contains(mro
, cls
);
981 if (PyList_Append(mro
, cls
) < 0)
984 bases
= ((PyClassObject
*)cls
)->cl_bases
;
985 assert(bases
&& PyTuple_Check(bases
));
986 n
= PyTuple_GET_SIZE(bases
);
987 for (i
= 0; i
< n
; i
++) {
988 base
= PyTuple_GET_ITEM(bases
, i
);
989 if (fill_classic_mro(mro
, base
) < 0)
996 classic_mro(PyObject
*cls
)
1000 assert(PyClass_Check(cls
));
1001 mro
= PyList_New(0);
1003 if (fill_classic_mro(mro
, cls
) == 0)
1011 Method resolution order algorithm C3 described in
1012 "A Monotonic Superclass Linearization for Dylan",
1013 by Kim Barrett, Bob Cassel, Paul Haahr,
1014 David A. Moon, Keith Playford, and P. Tucker Withington.
1017 Some notes about the rules implied by C3:
1020 It isn't legal to repeat a class in a list of base classes.
1022 The next three properties are the 3 constraints in "C3".
1024 Local precendece order.
1025 If A precedes B in C's MRO, then A will precede B in the MRO of all
1029 The MRO of a class must be an extension without reordering of the
1030 MRO of each of its superclasses.
1032 Extended Precedence Graph (EPG).
1033 Linearization is consistent if there is a path in the EPG from
1034 each class to all its successors in the linearization. See
1035 the paper for definition of EPG.
1039 tail_contains(PyObject
*list
, int whence
, PyObject
*o
) {
1041 size
= PyList_GET_SIZE(list
);
1043 for (j
= whence
+1; j
< size
; j
++) {
1044 if (PyList_GET_ITEM(list
, j
) == o
)
1051 class_name(PyObject
*cls
)
1053 PyObject
*name
= PyObject_GetAttrString(cls
, "__name__");
1057 name
= PyObject_Repr(cls
);
1061 if (!PyString_Check(name
)) {
1069 check_duplicates(PyObject
*list
)
1072 /* Let's use a quadratic time algorithm,
1073 assuming that the bases lists is short.
1075 n
= PyList_GET_SIZE(list
);
1076 for (i
= 0; i
< n
; i
++) {
1077 PyObject
*o
= PyList_GET_ITEM(list
, i
);
1078 for (j
= i
+ 1; j
< n
; j
++) {
1079 if (PyList_GET_ITEM(list
, j
) == o
) {
1081 PyErr_Format(PyExc_TypeError
,
1082 "duplicate base class %s",
1083 o
? PyString_AS_STRING(o
) : "?");
1092 /* Raise a TypeError for an MRO order disagreement.
1094 It's hard to produce a good error message. In the absence of better
1095 insight into error reporting, report the classes that were candidates
1096 to be put next into the MRO. There is some conflict between the
1097 order in which they should be put in the MRO, but it's hard to
1098 diagnose what constraint can't be satisfied.
1102 set_mro_error(PyObject
*to_merge
, int *remain
)
1104 int i
, n
, off
, to_merge_size
;
1107 PyObject
*set
= PyDict_New();
1109 to_merge_size
= PyList_GET_SIZE(to_merge
);
1110 for (i
= 0; i
< to_merge_size
; i
++) {
1111 PyObject
*L
= PyList_GET_ITEM(to_merge
, i
);
1112 if (remain
[i
] < PyList_GET_SIZE(L
)) {
1113 PyObject
*c
= PyList_GET_ITEM(L
, remain
[i
]);
1114 if (PyDict_SetItem(set
, c
, Py_None
) < 0)
1118 n
= PyDict_Size(set
);
1120 off
= PyOS_snprintf(buf
, sizeof(buf
), "Cannot create a \
1121 consistent method resolution\norder (MRO) for bases");
1123 while (PyDict_Next(set
, &i
, &k
, &v
) && off
< sizeof(buf
)) {
1124 PyObject
*name
= class_name(k
);
1125 off
+= PyOS_snprintf(buf
+ off
, sizeof(buf
) - off
, " %s",
1126 name
? PyString_AS_STRING(name
) : "?");
1128 if (--n
&& off
+1 < sizeof(buf
)) {
1133 PyErr_SetString(PyExc_TypeError
, buf
);
1138 pmerge(PyObject
*acc
, PyObject
* to_merge
) {
1139 int i
, j
, to_merge_size
;
1143 to_merge_size
= PyList_GET_SIZE(to_merge
);
1145 /* remain stores an index into each sublist of to_merge.
1146 remain[i] is the index of the next base in to_merge[i]
1147 that is not included in acc.
1149 remain
= PyMem_MALLOC(SIZEOF_INT
*to_merge_size
);
1152 for (i
= 0; i
< to_merge_size
; i
++)
1157 for (i
= 0; i
< to_merge_size
; i
++) {
1158 PyObject
*candidate
;
1160 PyObject
*cur_list
= PyList_GET_ITEM(to_merge
, i
);
1162 if (remain
[i
] >= PyList_GET_SIZE(cur_list
)) {
1167 /* Choose next candidate for MRO.
1169 The input sequences alone can determine the choice.
1170 If not, choose the class which appears in the MRO
1171 of the earliest direct superclass of the new class.
1174 candidate
= PyList_GET_ITEM(cur_list
, remain
[i
]);
1175 for (j
= 0; j
< to_merge_size
; j
++) {
1176 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1177 if (tail_contains(j_lst
, remain
[j
], candidate
)) {
1178 goto skip
; /* continue outer loop */
1181 ok
= PyList_Append(acc
, candidate
);
1186 for (j
= 0; j
< to_merge_size
; j
++) {
1187 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1188 if (remain
[j
] < PyList_GET_SIZE(j_lst
) &&
1189 PyList_GET_ITEM(j_lst
, remain
[j
]) == candidate
) {
1197 if (empty_cnt
== to_merge_size
) {
1201 set_mro_error(to_merge
, remain
);
1207 mro_implementation(PyTypeObject
*type
)
1210 PyObject
*bases
, *result
;
1211 PyObject
*to_merge
, *bases_aslist
;
1213 if(type
->tp_dict
== NULL
) {
1214 if(PyType_Ready(type
) < 0)
1218 /* Find a superclass linearization that honors the constraints
1219 of the explicit lists of bases and the constraints implied by
1222 to_merge is a list of lists, where each list is a superclass
1223 linearization implied by a base class. The last element of
1224 to_merge is the declared list of bases.
1227 bases
= type
->tp_bases
;
1228 n
= PyTuple_GET_SIZE(bases
);
1230 to_merge
= PyList_New(n
+1);
1231 if (to_merge
== NULL
)
1234 for (i
= 0; i
< n
; i
++) {
1235 PyObject
*base
= PyTuple_GET_ITEM(bases
, i
);
1236 PyObject
*parentMRO
;
1237 if (PyType_Check(base
))
1238 parentMRO
= PySequence_List(
1239 ((PyTypeObject
*)base
)->tp_mro
);
1241 parentMRO
= classic_mro(base
);
1242 if (parentMRO
== NULL
) {
1243 Py_DECREF(to_merge
);
1247 PyList_SET_ITEM(to_merge
, i
, parentMRO
);
1250 bases_aslist
= PySequence_List(bases
);
1251 if (bases_aslist
== NULL
) {
1252 Py_DECREF(to_merge
);
1255 /* This is just a basic sanity check. */
1256 if (check_duplicates(bases_aslist
) < 0) {
1257 Py_DECREF(to_merge
);
1258 Py_DECREF(bases_aslist
);
1261 PyList_SET_ITEM(to_merge
, n
, bases_aslist
);
1263 result
= Py_BuildValue("[O]", (PyObject
*)type
);
1264 if (result
== NULL
) {
1265 Py_DECREF(to_merge
);
1269 ok
= pmerge(result
, to_merge
);
1270 Py_DECREF(to_merge
);
1280 mro_external(PyObject
*self
)
1282 PyTypeObject
*type
= (PyTypeObject
*)self
;
1284 return mro_implementation(type
);
1288 mro_internal(PyTypeObject
*type
)
1290 PyObject
*mro
, *result
, *tuple
;
1293 if (type
->ob_type
== &PyType_Type
) {
1294 result
= mro_implementation(type
);
1297 static PyObject
*mro_str
;
1299 mro
= lookup_method((PyObject
*)type
, "mro", &mro_str
);
1302 result
= PyObject_CallObject(mro
, NULL
);
1307 tuple
= PySequence_Tuple(result
);
1314 PyTypeObject
*solid
;
1316 solid
= solid_base(type
);
1318 len
= PyTuple_GET_SIZE(tuple
);
1320 for (i
= 0; i
< len
; i
++) {
1322 cls
= PyTuple_GET_ITEM(tuple
, i
);
1323 if (PyClass_Check(cls
))
1325 else if (!PyType_Check(cls
)) {
1326 PyErr_Format(PyExc_TypeError
,
1327 "mro() returned a non-class ('%.500s')",
1328 cls
->ob_type
->tp_name
);
1332 t
= (PyTypeObject
*)cls
;
1333 if (!PyType_IsSubtype(solid
, solid_base(t
))) {
1334 PyErr_Format(PyExc_TypeError
,
1335 "mro() returned base with unsuitable layout ('%.500s')",
1342 type
->tp_mro
= tuple
;
1347 /* Calculate the best base amongst multiple base classes.
1348 This is the first one that's on the path to the "solid base". */
1350 static PyTypeObject
*
1351 best_base(PyObject
*bases
)
1354 PyTypeObject
*base
, *winner
, *candidate
, *base_i
;
1355 PyObject
*base_proto
;
1357 assert(PyTuple_Check(bases
));
1358 n
= PyTuple_GET_SIZE(bases
);
1362 for (i
= 0; i
< n
; i
++) {
1363 base_proto
= PyTuple_GET_ITEM(bases
, i
);
1364 if (PyClass_Check(base_proto
))
1366 if (!PyType_Check(base_proto
)) {
1369 "bases must be types");
1372 base_i
= (PyTypeObject
*)base_proto
;
1373 if (base_i
->tp_dict
== NULL
) {
1374 if (PyType_Ready(base_i
) < 0)
1377 candidate
= solid_base(base_i
);
1378 if (winner
== NULL
) {
1382 else if (PyType_IsSubtype(winner
, candidate
))
1384 else if (PyType_IsSubtype(candidate
, winner
)) {
1391 "multiple bases have "
1392 "instance lay-out conflict");
1397 PyErr_SetString(PyExc_TypeError
,
1398 "a new-style class can't have only classic bases");
1403 extra_ivars(PyTypeObject
*type
, PyTypeObject
*base
)
1405 size_t t_size
= type
->tp_basicsize
;
1406 size_t b_size
= base
->tp_basicsize
;
1408 assert(t_size
>= b_size
); /* Else type smaller than base! */
1409 if (type
->tp_itemsize
|| base
->tp_itemsize
) {
1410 /* If itemsize is involved, stricter rules */
1411 return t_size
!= b_size
||
1412 type
->tp_itemsize
!= base
->tp_itemsize
;
1414 if (type
->tp_weaklistoffset
&& base
->tp_weaklistoffset
== 0 &&
1415 type
->tp_weaklistoffset
+ sizeof(PyObject
*) == t_size
)
1416 t_size
-= sizeof(PyObject
*);
1417 if (type
->tp_dictoffset
&& base
->tp_dictoffset
== 0 &&
1418 type
->tp_dictoffset
+ sizeof(PyObject
*) == t_size
)
1419 t_size
-= sizeof(PyObject
*);
1421 return t_size
!= b_size
;
1424 static PyTypeObject
*
1425 solid_base(PyTypeObject
*type
)
1430 base
= solid_base(type
->tp_base
);
1432 base
= &PyBaseObject_Type
;
1433 if (extra_ivars(type
, base
))
1439 static void object_dealloc(PyObject
*);
1440 static int object_init(PyObject
*, PyObject
*, PyObject
*);
1441 static int update_slot(PyTypeObject
*, PyObject
*);
1442 static void fixup_slot_dispatchers(PyTypeObject
*);
1445 subtype_dict(PyObject
*obj
, void *context
)
1447 PyObject
**dictptr
= _PyObject_GetDictPtr(obj
);
1450 if (dictptr
== NULL
) {
1451 PyErr_SetString(PyExc_AttributeError
,
1452 "This object has no __dict__");
1457 *dictptr
= dict
= PyDict_New();
1463 subtype_setdict(PyObject
*obj
, PyObject
*value
, void *context
)
1465 PyObject
**dictptr
= _PyObject_GetDictPtr(obj
);
1468 if (dictptr
== NULL
) {
1469 PyErr_SetString(PyExc_AttributeError
,
1470 "This object has no __dict__");
1473 if (value
!= NULL
&& !PyDict_Check(value
)) {
1474 PyErr_SetString(PyExc_TypeError
,
1475 "__dict__ must be set to a dictionary");
1486 subtype_getweakref(PyObject
*obj
, void *context
)
1488 PyObject
**weaklistptr
;
1491 if (obj
->ob_type
->tp_weaklistoffset
== 0) {
1492 PyErr_SetString(PyExc_AttributeError
,
1493 "This object has no __weaklist__");
1496 assert(obj
->ob_type
->tp_weaklistoffset
> 0);
1497 assert(obj
->ob_type
->tp_weaklistoffset
+ sizeof(PyObject
*) <=
1498 (size_t)(obj
->ob_type
->tp_basicsize
));
1499 weaklistptr
= (PyObject
**)
1500 ((char *)obj
+ obj
->ob_type
->tp_weaklistoffset
);
1501 if (*weaklistptr
== NULL
)
1504 result
= *weaklistptr
;
1509 /* Three variants on the subtype_getsets list. */
1511 static PyGetSetDef subtype_getsets_full
[] = {
1512 {"__dict__", subtype_dict
, subtype_setdict
,
1513 PyDoc_STR("dictionary for instance variables (if defined)")},
1514 {"__weakref__", subtype_getweakref
, NULL
,
1515 PyDoc_STR("list of weak references to the object (if defined)")},
1519 static PyGetSetDef subtype_getsets_dict_only
[] = {
1520 {"__dict__", subtype_dict
, subtype_setdict
,
1521 PyDoc_STR("dictionary for instance variables (if defined)")},
1525 static PyGetSetDef subtype_getsets_weakref_only
[] = {
1526 {"__weakref__", subtype_getweakref
, NULL
,
1527 PyDoc_STR("list of weak references to the object (if defined)")},
1532 valid_identifier(PyObject
*s
)
1537 if (!PyString_Check(s
)) {
1538 PyErr_SetString(PyExc_TypeError
,
1539 "__slots__ must be strings");
1542 p
= (unsigned char *) PyString_AS_STRING(s
);
1543 n
= PyString_GET_SIZE(s
);
1544 /* We must reject an empty name. As a hack, we bump the
1545 length to 1 so that the loop will balk on the trailing \0. */
1548 for (i
= 0; i
< n
; i
++, p
++) {
1549 if (!(i
== 0 ? isalpha(*p
) : isalnum(*p
)) && *p
!= '_') {
1550 PyErr_SetString(PyExc_TypeError
,
1551 "__slots__ must be identifiers");
1558 #ifdef Py_USING_UNICODE
1559 /* Replace Unicode objects in slots. */
1562 _unicode_to_string(PyObject
*slots
, int nslots
)
1564 PyObject
*tmp
= slots
;
1567 intintargfunc copy
= slots
->ob_type
->tp_as_sequence
->sq_slice
;
1568 for (i
= 0; i
< nslots
; i
++) {
1569 if (PyUnicode_Check(o
= PyTuple_GET_ITEM(tmp
, i
))) {
1571 tmp
= copy(slots
, 0, PyTuple_GET_SIZE(slots
));
1575 o1
= _PyUnicode_AsDefaultEncodedString
1583 PyTuple_SET_ITEM(tmp
, i
, o1
);
1591 type_new(PyTypeObject
*metatype
, PyObject
*args
, PyObject
*kwds
)
1593 PyObject
*name
, *bases
, *dict
;
1594 static const char *kwlist
[] = {"name", "bases", "dict", 0};
1595 PyObject
*slots
, *tmp
, *newslots
;
1596 PyTypeObject
*type
, *base
, *tmptype
, *winner
;
1597 PyHeapTypeObject
*et
;
1599 int i
, nbases
, nslots
, slotoffset
, add_dict
, add_weak
;
1600 int j
, may_add_dict
, may_add_weak
;
1602 assert(args
!= NULL
&& PyTuple_Check(args
));
1603 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1605 /* Special case: type(x) should return x->ob_type */
1607 const int nargs
= PyTuple_GET_SIZE(args
);
1608 const int nkwds
= kwds
== NULL
? 0 : PyDict_Size(kwds
);
1610 if (PyType_CheckExact(metatype
) && nargs
== 1 && nkwds
== 0) {
1611 PyObject
*x
= PyTuple_GET_ITEM(args
, 0);
1612 Py_INCREF(x
->ob_type
);
1613 return (PyObject
*) x
->ob_type
;
1616 /* SF bug 475327 -- if that didn't trigger, we need 3
1617 arguments. but PyArg_ParseTupleAndKeywords below may give
1618 a msg saying type() needs exactly 3. */
1619 if (nargs
+ nkwds
!= 3) {
1620 PyErr_SetString(PyExc_TypeError
,
1621 "type() takes 1 or 3 arguments");
1626 /* Check arguments: (name, bases, dict) */
1627 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "SO!O!:type", kwlist
,
1629 &PyTuple_Type
, &bases
,
1630 &PyDict_Type
, &dict
))
1633 /* Determine the proper metatype to deal with this,
1634 and check for metatype conflicts while we're at it.
1635 Note that if some other metatype wins to contract,
1636 it's possible that its instances are not types. */
1637 nbases
= PyTuple_GET_SIZE(bases
);
1639 for (i
= 0; i
< nbases
; i
++) {
1640 tmp
= PyTuple_GET_ITEM(bases
, i
);
1641 tmptype
= tmp
->ob_type
;
1642 if (tmptype
== &PyClass_Type
)
1643 continue; /* Special case classic classes */
1644 if (PyType_IsSubtype(winner
, tmptype
))
1646 if (PyType_IsSubtype(tmptype
, winner
)) {
1650 PyErr_SetString(PyExc_TypeError
,
1651 "metaclass conflict: "
1652 "the metaclass of a derived class "
1653 "must be a (non-strict) subclass "
1654 "of the metaclasses of all its bases");
1657 if (winner
!= metatype
) {
1658 if (winner
->tp_new
!= type_new
) /* Pass it to the winner */
1659 return winner
->tp_new(winner
, args
, kwds
);
1663 /* Adjust for empty tuple bases */
1665 bases
= PyTuple_Pack(1, &PyBaseObject_Type
);
1673 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1675 /* Calculate best base, and check that all bases are type objects */
1676 base
= best_base(bases
);
1681 if (!PyType_HasFeature(base
, Py_TPFLAGS_BASETYPE
)) {
1682 PyErr_Format(PyExc_TypeError
,
1683 "type '%.100s' is not an acceptable base type",
1689 /* Check for a __slots__ sequence variable in dict, and count it */
1690 slots
= PyDict_GetItemString(dict
, "__slots__");
1694 may_add_dict
= base
->tp_dictoffset
== 0;
1695 may_add_weak
= base
->tp_weaklistoffset
== 0 && base
->tp_itemsize
== 0;
1696 if (slots
== NULL
) {
1707 /* Make it into a tuple */
1708 if (PyString_Check(slots
))
1709 slots
= PyTuple_Pack(1, slots
);
1711 slots
= PySequence_Tuple(slots
);
1712 if (slots
== NULL
) {
1716 assert(PyTuple_Check(slots
));
1718 /* Are slots allowed? */
1719 nslots
= PyTuple_GET_SIZE(slots
);
1720 if (nslots
> 0 && base
->tp_itemsize
!= 0) {
1721 PyErr_Format(PyExc_TypeError
,
1722 "nonempty __slots__ "
1723 "not supported for subtype of '%s'",
1731 #ifdef Py_USING_UNICODE
1732 tmp
= _unicode_to_string(slots
, nslots
);
1740 /* Check for valid slot names and two special cases */
1741 for (i
= 0; i
< nslots
; i
++) {
1742 PyObject
*tmp
= PyTuple_GET_ITEM(slots
, i
);
1744 if (!valid_identifier(tmp
))
1746 assert(PyString_Check(tmp
));
1747 s
= PyString_AS_STRING(tmp
);
1748 if (strcmp(s
, "__dict__") == 0) {
1749 if (!may_add_dict
|| add_dict
) {
1750 PyErr_SetString(PyExc_TypeError
,
1751 "__dict__ slot disallowed: "
1752 "we already got one");
1757 if (strcmp(s
, "__weakref__") == 0) {
1758 if (!may_add_weak
|| add_weak
) {
1759 PyErr_SetString(PyExc_TypeError
,
1760 "__weakref__ slot disallowed: "
1761 "either we already got one, "
1762 "or __itemsize__ != 0");
1769 /* Copy slots into yet another tuple, demangling names */
1770 newslots
= PyTuple_New(nslots
- add_dict
- add_weak
);
1771 if (newslots
== NULL
)
1773 for (i
= j
= 0; i
< nslots
; i
++) {
1775 tmp
= PyTuple_GET_ITEM(slots
, i
);
1776 s
= PyString_AS_STRING(tmp
);
1777 if ((add_dict
&& strcmp(s
, "__dict__") == 0) ||
1778 (add_weak
&& strcmp(s
, "__weakref__") == 0))
1780 tmp
=_Py_Mangle(name
, tmp
);
1783 PyTuple_SET_ITEM(newslots
, j
, tmp
);
1786 assert(j
== nslots
- add_dict
- add_weak
);
1791 /* Secondary bases may provide weakrefs or dict */
1793 ((may_add_dict
&& !add_dict
) ||
1794 (may_add_weak
&& !add_weak
))) {
1795 for (i
= 0; i
< nbases
; i
++) {
1796 tmp
= PyTuple_GET_ITEM(bases
, i
);
1797 if (tmp
== (PyObject
*)base
)
1798 continue; /* Skip primary base */
1799 if (PyClass_Check(tmp
)) {
1800 /* Classic base class provides both */
1801 if (may_add_dict
&& !add_dict
)
1803 if (may_add_weak
&& !add_weak
)
1807 assert(PyType_Check(tmp
));
1808 tmptype
= (PyTypeObject
*)tmp
;
1809 if (may_add_dict
&& !add_dict
&&
1810 tmptype
->tp_dictoffset
!= 0)
1812 if (may_add_weak
&& !add_weak
&&
1813 tmptype
->tp_weaklistoffset
!= 0)
1815 if (may_add_dict
&& !add_dict
)
1817 if (may_add_weak
&& !add_weak
)
1819 /* Nothing more to check */
1825 /* XXX From here until type is safely allocated,
1826 "return NULL" may leak slots! */
1828 /* Allocate the type object */
1829 type
= (PyTypeObject
*)metatype
->tp_alloc(metatype
, nslots
);
1836 /* Keep name and slots alive in the extended type object */
1837 et
= (PyHeapTypeObject
*)type
;
1842 /* Initialize tp_flags */
1843 type
->tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HEAPTYPE
|
1844 Py_TPFLAGS_BASETYPE
;
1845 if (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)
1846 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
1848 /* It's a new-style number unless it specifically inherits any
1849 old-style numeric behavior */
1850 if ((base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) ||
1851 (base
->tp_as_number
== NULL
))
1852 type
->tp_flags
|= Py_TPFLAGS_CHECKTYPES
;
1854 /* Initialize essential fields */
1855 type
->tp_as_number
= &et
->as_number
;
1856 type
->tp_as_sequence
= &et
->as_sequence
;
1857 type
->tp_as_mapping
= &et
->as_mapping
;
1858 type
->tp_as_buffer
= &et
->as_buffer
;
1859 type
->tp_name
= PyString_AS_STRING(name
);
1861 /* Set tp_base and tp_bases */
1862 type
->tp_bases
= bases
;
1864 type
->tp_base
= base
;
1866 /* Initialize tp_dict from passed-in dict */
1867 type
->tp_dict
= dict
= PyDict_Copy(dict
);
1873 /* Set __module__ in the dict */
1874 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
1875 tmp
= PyEval_GetGlobals();
1877 tmp
= PyDict_GetItemString(tmp
, "__name__");
1879 if (PyDict_SetItemString(dict
, "__module__",
1886 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1887 and is a string. The __doc__ accessor will first look for tp_doc;
1888 if that fails, it will still look into __dict__.
1891 PyObject
*doc
= PyDict_GetItemString(dict
, "__doc__");
1892 if (doc
!= NULL
&& PyString_Check(doc
)) {
1893 const size_t n
= (size_t)PyString_GET_SIZE(doc
);
1894 char *tp_doc
= PyObject_MALLOC(n
+1);
1895 if (tp_doc
== NULL
) {
1899 memcpy(tp_doc
, PyString_AS_STRING(doc
), n
+1);
1900 type
->tp_doc
= tp_doc
;
1904 /* Special-case __new__: if it's a plain function,
1905 make it a static function */
1906 tmp
= PyDict_GetItemString(dict
, "__new__");
1907 if (tmp
!= NULL
&& PyFunction_Check(tmp
)) {
1908 tmp
= PyStaticMethod_New(tmp
);
1913 PyDict_SetItemString(dict
, "__new__", tmp
);
1917 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1918 mp
= PyHeapType_GET_MEMBERS(et
);
1919 slotoffset
= base
->tp_basicsize
;
1920 if (slots
!= NULL
) {
1921 for (i
= 0; i
< nslots
; i
++, mp
++) {
1922 mp
->name
= PyString_AS_STRING(
1923 PyTuple_GET_ITEM(slots
, i
));
1924 mp
->type
= T_OBJECT_EX
;
1925 mp
->offset
= slotoffset
;
1926 if (base
->tp_weaklistoffset
== 0 &&
1927 strcmp(mp
->name
, "__weakref__") == 0) {
1929 mp
->type
= T_OBJECT
;
1930 mp
->flags
= READONLY
;
1931 type
->tp_weaklistoffset
= slotoffset
;
1933 slotoffset
+= sizeof(PyObject
*);
1937 if (base
->tp_itemsize
)
1938 type
->tp_dictoffset
= -(long)sizeof(PyObject
*);
1940 type
->tp_dictoffset
= slotoffset
;
1941 slotoffset
+= sizeof(PyObject
*);
1944 assert(!base
->tp_itemsize
);
1945 type
->tp_weaklistoffset
= slotoffset
;
1946 slotoffset
+= sizeof(PyObject
*);
1948 type
->tp_basicsize
= slotoffset
;
1949 type
->tp_itemsize
= base
->tp_itemsize
;
1950 type
->tp_members
= PyHeapType_GET_MEMBERS(et
);
1952 if (type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
1953 type
->tp_getset
= subtype_getsets_full
;
1954 else if (type
->tp_weaklistoffset
&& !type
->tp_dictoffset
)
1955 type
->tp_getset
= subtype_getsets_weakref_only
;
1956 else if (!type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
1957 type
->tp_getset
= subtype_getsets_dict_only
;
1959 type
->tp_getset
= NULL
;
1961 /* Special case some slots */
1962 if (type
->tp_dictoffset
!= 0 || nslots
> 0) {
1963 if (base
->tp_getattr
== NULL
&& base
->tp_getattro
== NULL
)
1964 type
->tp_getattro
= PyObject_GenericGetAttr
;
1965 if (base
->tp_setattr
== NULL
&& base
->tp_setattro
== NULL
)
1966 type
->tp_setattro
= PyObject_GenericSetAttr
;
1968 type
->tp_dealloc
= subtype_dealloc
;
1970 /* Enable GC unless there are really no instance variables possible */
1971 if (!(type
->tp_basicsize
== sizeof(PyObject
) &&
1972 type
->tp_itemsize
== 0))
1973 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
1975 /* Always override allocation strategy to use regular heap */
1976 type
->tp_alloc
= PyType_GenericAlloc
;
1977 if (type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) {
1978 type
->tp_free
= PyObject_GC_Del
;
1979 type
->tp_traverse
= subtype_traverse
;
1980 type
->tp_clear
= subtype_clear
;
1983 type
->tp_free
= PyObject_Del
;
1985 /* Initialize the rest */
1986 if (PyType_Ready(type
) < 0) {
1991 /* Put the proper slots in place */
1992 fixup_slot_dispatchers(type
);
1994 return (PyObject
*)type
;
1997 /* Internal API to look for a name through the MRO.
1998 This returns a borrowed reference, and doesn't set an exception! */
2000 _PyType_Lookup(PyTypeObject
*type
, PyObject
*name
)
2003 PyObject
*mro
, *res
, *base
, *dict
;
2005 /* Look in tp_dict of types in MRO */
2008 /* If mro is NULL, the type is either not yet initialized
2009 by PyType_Ready(), or already cleared by type_clear().
2010 Either way the safest thing to do is to return NULL. */
2014 assert(PyTuple_Check(mro
));
2015 n
= PyTuple_GET_SIZE(mro
);
2016 for (i
= 0; i
< n
; i
++) {
2017 base
= PyTuple_GET_ITEM(mro
, i
);
2018 if (PyClass_Check(base
))
2019 dict
= ((PyClassObject
*)base
)->cl_dict
;
2021 assert(PyType_Check(base
));
2022 dict
= ((PyTypeObject
*)base
)->tp_dict
;
2024 assert(dict
&& PyDict_Check(dict
));
2025 res
= PyDict_GetItem(dict
, name
);
2032 /* This is similar to PyObject_GenericGetAttr(),
2033 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2035 type_getattro(PyTypeObject
*type
, PyObject
*name
)
2037 PyTypeObject
*metatype
= type
->ob_type
;
2038 PyObject
*meta_attribute
, *attribute
;
2039 descrgetfunc meta_get
;
2041 /* Initialize this type (we'll assume the metatype is initialized) */
2042 if (type
->tp_dict
== NULL
) {
2043 if (PyType_Ready(type
) < 0)
2047 /* No readable descriptor found yet */
2050 /* Look for the attribute in the metatype */
2051 meta_attribute
= _PyType_Lookup(metatype
, name
);
2053 if (meta_attribute
!= NULL
) {
2054 meta_get
= meta_attribute
->ob_type
->tp_descr_get
;
2056 if (meta_get
!= NULL
&& PyDescr_IsData(meta_attribute
)) {
2057 /* Data descriptors implement tp_descr_set to intercept
2058 * writes. Assume the attribute is not overridden in
2059 * type's tp_dict (and bases): call the descriptor now.
2061 return meta_get(meta_attribute
, (PyObject
*)type
,
2062 (PyObject
*)metatype
);
2064 Py_INCREF(meta_attribute
);
2067 /* No data descriptor found on metatype. Look in tp_dict of this
2068 * type and its bases */
2069 attribute
= _PyType_Lookup(type
, name
);
2070 if (attribute
!= NULL
) {
2071 /* Implement descriptor functionality, if any */
2072 descrgetfunc local_get
= attribute
->ob_type
->tp_descr_get
;
2074 Py_XDECREF(meta_attribute
);
2076 if (local_get
!= NULL
) {
2077 /* NULL 2nd argument indicates the descriptor was
2078 * found on the target object itself (or a base) */
2079 return local_get(attribute
, (PyObject
*)NULL
,
2083 Py_INCREF(attribute
);
2087 /* No attribute found in local __dict__ (or bases): use the
2088 * descriptor from the metatype, if any */
2089 if (meta_get
!= NULL
) {
2091 res
= meta_get(meta_attribute
, (PyObject
*)type
,
2092 (PyObject
*)metatype
);
2093 Py_DECREF(meta_attribute
);
2097 /* If an ordinary attribute was found on the metatype, return it now */
2098 if (meta_attribute
!= NULL
) {
2099 return meta_attribute
;
2103 PyErr_Format(PyExc_AttributeError
,
2104 "type object '%.50s' has no attribute '%.400s'",
2105 type
->tp_name
, PyString_AS_STRING(name
));
2110 type_setattro(PyTypeObject
*type
, PyObject
*name
, PyObject
*value
)
2112 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
2115 "can't set attributes of built-in/extension type '%s'",
2119 /* XXX Example of how I expect this to be used...
2120 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2123 if (PyObject_GenericSetAttr((PyObject
*)type
, name
, value
) < 0)
2125 return update_slot(type
, name
);
2129 type_dealloc(PyTypeObject
*type
)
2131 PyHeapTypeObject
*et
;
2133 /* Assert this is a heap-allocated type object */
2134 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2135 _PyObject_GC_UNTRACK(type
);
2136 PyObject_ClearWeakRefs((PyObject
*)type
);
2137 et
= (PyHeapTypeObject
*)type
;
2138 Py_XDECREF(type
->tp_base
);
2139 Py_XDECREF(type
->tp_dict
);
2140 Py_XDECREF(type
->tp_bases
);
2141 Py_XDECREF(type
->tp_mro
);
2142 Py_XDECREF(type
->tp_cache
);
2143 Py_XDECREF(type
->tp_subclasses
);
2144 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2145 * of most other objects. It's okay to cast it to char *.
2147 PyObject_Free((char *)type
->tp_doc
);
2148 Py_XDECREF(et
->name
);
2149 Py_XDECREF(et
->slots
);
2150 type
->ob_type
->tp_free((PyObject
*)type
);
2154 type_subclasses(PyTypeObject
*type
, PyObject
*args_ignored
)
2156 PyObject
*list
, *raw
, *ref
;
2159 list
= PyList_New(0);
2162 raw
= type
->tp_subclasses
;
2165 assert(PyList_Check(raw
));
2166 n
= PyList_GET_SIZE(raw
);
2167 for (i
= 0; i
< n
; i
++) {
2168 ref
= PyList_GET_ITEM(raw
, i
);
2169 assert(PyWeakref_CheckRef(ref
));
2170 ref
= PyWeakref_GET_OBJECT(ref
);
2171 if (ref
!= Py_None
) {
2172 if (PyList_Append(list
, ref
) < 0) {
2181 static PyMethodDef type_methods
[] = {
2182 {"mro", (PyCFunction
)mro_external
, METH_NOARGS
,
2183 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2184 {"__subclasses__", (PyCFunction
)type_subclasses
, METH_NOARGS
,
2185 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2189 PyDoc_STRVAR(type_doc
,
2190 "type(object) -> the object's type\n"
2191 "type(name, bases, dict) -> a new type");
2194 type_traverse(PyTypeObject
*type
, visitproc visit
, void *arg
)
2198 /* Because of type_is_gc(), the collector only calls this
2200 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2202 #define VISIT(SLOT) \
2204 err = visit((PyObject *)(SLOT), arg); \
2209 VISIT(type
->tp_dict
);
2210 VISIT(type
->tp_cache
);
2211 VISIT(type
->tp_mro
);
2212 VISIT(type
->tp_bases
);
2213 VISIT(type
->tp_base
);
2215 /* There's no need to visit type->tp_subclasses or
2216 ((PyHeapTypeObject *)type)->slots, because they can't be involved
2217 in cycles; tp_subclasses is a list of weak references,
2218 and slots is a tuple of strings. */
2226 type_clear(PyTypeObject
*type
)
2230 /* Because of type_is_gc(), the collector only calls this
2232 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2234 #define CLEAR(SLOT) \
2236 tmp = (PyObject *)(SLOT); \
2241 /* The only field we need to clear is tp_mro, which is part of a
2242 hard cycle (its first element is the class itself) that won't
2243 be broken otherwise (it's a tuple and tuples don't have a
2244 tp_clear handler). None of the other fields need to be
2245 cleared, and here's why:
2248 It is a dict, so the collector will call its tp_clear.
2251 Not used; if it were, it would be a dict.
2254 If these are involved in a cycle, there must be at least
2255 one other, mutable object in the cycle, e.g. a base
2256 class's dict; the cycle will be broken that way.
2259 A list of weak references can't be part of a cycle; and
2260 lists have their own tp_clear.
2262 slots (in PyHeapTypeObject):
2263 A tuple of strings can't be part of a cycle.
2266 CLEAR(type
->tp_mro
);
2274 type_is_gc(PyTypeObject
*type
)
2276 return type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
;
2279 PyTypeObject PyType_Type
= {
2280 PyObject_HEAD_INIT(&PyType_Type
)
2282 "type", /* tp_name */
2283 sizeof(PyHeapTypeObject
), /* tp_basicsize */
2284 sizeof(PyMemberDef
), /* tp_itemsize */
2285 (destructor
)type_dealloc
, /* tp_dealloc */
2289 type_compare
, /* tp_compare */
2290 (reprfunc
)type_repr
, /* tp_repr */
2291 0, /* tp_as_number */
2292 0, /* tp_as_sequence */
2293 0, /* tp_as_mapping */
2294 (hashfunc
)_Py_HashPointer
, /* tp_hash */
2295 (ternaryfunc
)type_call
, /* tp_call */
2297 (getattrofunc
)type_getattro
, /* tp_getattro */
2298 (setattrofunc
)type_setattro
, /* tp_setattro */
2299 0, /* tp_as_buffer */
2300 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2301 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2302 type_doc
, /* tp_doc */
2303 (traverseproc
)type_traverse
, /* tp_traverse */
2304 (inquiry
)type_clear
, /* tp_clear */
2305 0, /* tp_richcompare */
2306 offsetof(PyTypeObject
, tp_weaklist
), /* tp_weaklistoffset */
2308 0, /* tp_iternext */
2309 type_methods
, /* tp_methods */
2310 type_members
, /* tp_members */
2311 type_getsets
, /* tp_getset */
2314 0, /* tp_descr_get */
2315 0, /* tp_descr_set */
2316 offsetof(PyTypeObject
, tp_dict
), /* tp_dictoffset */
2319 type_new
, /* tp_new */
2320 PyObject_GC_Del
, /* tp_free */
2321 (inquiry
)type_is_gc
, /* tp_is_gc */
2325 /* The base type of all types (eventually)... except itself. */
2328 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2333 /* If we don't have a tp_new for a new-style class, new will use this one.
2334 Therefore this should take no arguments/keywords. However, this new may
2335 also be inherited by objects that define a tp_init but no tp_new. These
2336 objects WILL pass argumets to tp_new, because it gets the same args as
2337 tp_init. So only allow arguments if we aren't using the default init, in
2338 which case we expect init to handle argument parsing. */
2340 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2342 if (type
->tp_init
== object_init
&& (PyTuple_GET_SIZE(args
) ||
2343 (kwds
&& PyDict_Check(kwds
) && PyDict_Size(kwds
)))) {
2344 PyErr_SetString(PyExc_TypeError
,
2345 "default __new__ takes no parameters");
2348 return type
->tp_alloc(type
, 0);
2352 object_dealloc(PyObject
*self
)
2354 self
->ob_type
->tp_free(self
);
2358 object_repr(PyObject
*self
)
2361 PyObject
*mod
, *name
, *rtn
;
2363 type
= self
->ob_type
;
2364 mod
= type_module(type
, NULL
);
2367 else if (!PyString_Check(mod
)) {
2371 name
= type_name(type
, NULL
);
2374 if (mod
!= NULL
&& strcmp(PyString_AS_STRING(mod
), "__builtin__"))
2375 rtn
= PyString_FromFormat("<%s.%s object at %p>",
2376 PyString_AS_STRING(mod
),
2377 PyString_AS_STRING(name
),
2380 rtn
= PyString_FromFormat("<%s object at %p>",
2381 type
->tp_name
, self
);
2388 object_str(PyObject
*self
)
2392 f
= self
->ob_type
->tp_repr
;
2399 object_hash(PyObject
*self
)
2401 return _Py_HashPointer(self
);
2405 object_get_class(PyObject
*self
, void *closure
)
2407 Py_INCREF(self
->ob_type
);
2408 return (PyObject
*)(self
->ob_type
);
2412 equiv_structs(PyTypeObject
*a
, PyTypeObject
*b
)
2417 a
->tp_basicsize
== b
->tp_basicsize
&&
2418 a
->tp_itemsize
== b
->tp_itemsize
&&
2419 a
->tp_dictoffset
== b
->tp_dictoffset
&&
2420 a
->tp_weaklistoffset
== b
->tp_weaklistoffset
&&
2421 ((a
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
2422 (b
->tp_flags
& Py_TPFLAGS_HAVE_GC
)));
2426 same_slots_added(PyTypeObject
*a
, PyTypeObject
*b
)
2428 PyTypeObject
*base
= a
->tp_base
;
2431 if (base
!= b
->tp_base
)
2433 if (equiv_structs(a
, base
) && equiv_structs(b
, base
))
2435 size
= base
->tp_basicsize
;
2436 if (a
->tp_dictoffset
== size
&& b
->tp_dictoffset
== size
)
2437 size
+= sizeof(PyObject
*);
2438 if (a
->tp_weaklistoffset
== size
&& b
->tp_weaklistoffset
== size
)
2439 size
+= sizeof(PyObject
*);
2440 return size
== a
->tp_basicsize
&& size
== b
->tp_basicsize
;
2444 compatible_for_assignment(PyTypeObject
* old
, PyTypeObject
* new, char* attr
)
2446 PyTypeObject
*newbase
, *oldbase
;
2448 if (new->tp_dealloc
!= old
->tp_dealloc
||
2449 new->tp_free
!= old
->tp_free
)
2451 PyErr_Format(PyExc_TypeError
,
2453 "'%s' deallocator differs from '%s'",
2461 while (equiv_structs(newbase
, newbase
->tp_base
))
2462 newbase
= newbase
->tp_base
;
2463 while (equiv_structs(oldbase
, oldbase
->tp_base
))
2464 oldbase
= oldbase
->tp_base
;
2465 if (newbase
!= oldbase
&&
2466 (newbase
->tp_base
!= oldbase
->tp_base
||
2467 !same_slots_added(newbase
, oldbase
))) {
2468 PyErr_Format(PyExc_TypeError
,
2470 "'%s' object layout differs from '%s'",
2481 object_set_class(PyObject
*self
, PyObject
*value
, void *closure
)
2483 PyTypeObject
*old
= self
->ob_type
;
2486 if (value
== NULL
) {
2487 PyErr_SetString(PyExc_TypeError
,
2488 "can't delete __class__ attribute");
2491 if (!PyType_Check(value
)) {
2492 PyErr_Format(PyExc_TypeError
,
2493 "__class__ must be set to new-style class, not '%s' object",
2494 value
->ob_type
->tp_name
);
2497 new = (PyTypeObject
*)value
;
2498 if (!(new->tp_flags
& Py_TPFLAGS_HEAPTYPE
) ||
2499 !(old
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
2501 PyErr_Format(PyExc_TypeError
,
2502 "__class__ assignment: only for heap types");
2505 if (compatible_for_assignment(new, old
, "__class__")) {
2507 self
->ob_type
= new;
2516 static PyGetSetDef object_getsets
[] = {
2517 {"__class__", object_get_class
, object_set_class
,
2518 PyDoc_STR("the object's class")},
2523 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2524 We fall back to helpers in copy_reg for:
2525 - pickle protocols < 2
2526 - calculating the list of slot names (done only once per class)
2527 - the __newobj__ function (which is used as a token but never called)
2531 import_copy_reg(void)
2533 static PyObject
*copy_reg_str
;
2535 if (!copy_reg_str
) {
2536 copy_reg_str
= PyString_InternFromString("copy_reg");
2537 if (copy_reg_str
== NULL
)
2541 return PyImport_Import(copy_reg_str
);
2545 slotnames(PyObject
*cls
)
2549 PyObject
*slotnames
;
2551 if (!PyType_Check(cls
)) {
2556 clsdict
= ((PyTypeObject
*)cls
)->tp_dict
;
2557 slotnames
= PyDict_GetItemString(clsdict
, "__slotnames__");
2558 if (slotnames
!= NULL
&& PyList_Check(slotnames
)) {
2559 Py_INCREF(slotnames
);
2563 copy_reg
= import_copy_reg();
2564 if (copy_reg
== NULL
)
2567 slotnames
= PyObject_CallMethod(copy_reg
, "_slotnames", "O", cls
);
2568 Py_DECREF(copy_reg
);
2569 if (slotnames
!= NULL
&&
2570 slotnames
!= Py_None
&&
2571 !PyList_Check(slotnames
))
2573 PyErr_SetString(PyExc_TypeError
,
2574 "copy_reg._slotnames didn't return a list or None");
2575 Py_DECREF(slotnames
);
2583 reduce_2(PyObject
*obj
)
2585 PyObject
*cls
, *getnewargs
;
2586 PyObject
*args
= NULL
, *args2
= NULL
;
2587 PyObject
*getstate
= NULL
, *state
= NULL
, *names
= NULL
;
2588 PyObject
*slots
= NULL
, *listitems
= NULL
, *dictitems
= NULL
;
2589 PyObject
*copy_reg
= NULL
, *newobj
= NULL
, *res
= NULL
;
2592 cls
= PyObject_GetAttrString(obj
, "__class__");
2596 getnewargs
= PyObject_GetAttrString(obj
, "__getnewargs__");
2597 if (getnewargs
!= NULL
) {
2598 args
= PyObject_CallObject(getnewargs
, NULL
);
2599 Py_DECREF(getnewargs
);
2600 if (args
!= NULL
&& !PyTuple_Check(args
)) {
2601 PyErr_SetString(PyExc_TypeError
,
2602 "__getnewargs__ should return a tuple");
2608 args
= PyTuple_New(0);
2613 getstate
= PyObject_GetAttrString(obj
, "__getstate__");
2614 if (getstate
!= NULL
) {
2615 state
= PyObject_CallObject(getstate
, NULL
);
2616 Py_DECREF(getstate
);
2622 state
= PyObject_GetAttrString(obj
, "__dict__");
2623 if (state
== NULL
) {
2628 names
= slotnames(cls
);
2631 if (names
!= Py_None
) {
2632 assert(PyList_Check(names
));
2633 slots
= PyDict_New();
2637 /* Can't pre-compute the list size; the list
2638 is stored on the class so accessible to other
2639 threads, which may be run by DECREF */
2640 for (i
= 0; i
< PyList_GET_SIZE(names
); i
++) {
2641 PyObject
*name
, *value
;
2642 name
= PyList_GET_ITEM(names
, i
);
2643 value
= PyObject_GetAttr(obj
, name
);
2647 int err
= PyDict_SetItem(slots
, name
,
2656 state
= Py_BuildValue("(NO)", state
, slots
);
2663 if (!PyList_Check(obj
)) {
2664 listitems
= Py_None
;
2665 Py_INCREF(listitems
);
2668 listitems
= PyObject_GetIter(obj
);
2669 if (listitems
== NULL
)
2673 if (!PyDict_Check(obj
)) {
2674 dictitems
= Py_None
;
2675 Py_INCREF(dictitems
);
2678 dictitems
= PyObject_CallMethod(obj
, "iteritems", "");
2679 if (dictitems
== NULL
)
2683 copy_reg
= import_copy_reg();
2684 if (copy_reg
== NULL
)
2686 newobj
= PyObject_GetAttrString(copy_reg
, "__newobj__");
2690 n
= PyTuple_GET_SIZE(args
);
2691 args2
= PyTuple_New(n
+1);
2694 PyTuple_SET_ITEM(args2
, 0, cls
);
2696 for (i
= 0; i
< n
; i
++) {
2697 PyObject
*v
= PyTuple_GET_ITEM(args
, i
);
2699 PyTuple_SET_ITEM(args2
, i
+1, v
);
2702 res
= PyTuple_Pack(5, newobj
, args2
, state
, listitems
, dictitems
);
2711 Py_XDECREF(listitems
);
2712 Py_XDECREF(dictitems
);
2713 Py_XDECREF(copy_reg
);
2719 object_reduce_ex(PyObject
*self
, PyObject
*args
)
2721 /* Call copy_reg._reduce_ex(self, proto) */
2722 PyObject
*reduce
, *copy_reg
, *res
;
2725 if (!PyArg_ParseTuple(args
, "|i:__reduce_ex__", &proto
))
2728 reduce
= PyObject_GetAttrString(self
, "__reduce__");
2732 PyObject
*cls
, *clsreduce
, *objreduce
;
2734 cls
= PyObject_GetAttrString(self
, "__class__");
2739 clsreduce
= PyObject_GetAttrString(cls
, "__reduce__");
2741 if (clsreduce
== NULL
) {
2745 objreduce
= PyDict_GetItemString(PyBaseObject_Type
.tp_dict
,
2747 override
= (clsreduce
!= objreduce
);
2748 Py_DECREF(clsreduce
);
2750 res
= PyObject_CallObject(reduce
, NULL
);
2759 return reduce_2(self
);
2761 copy_reg
= import_copy_reg();
2765 res
= PyEval_CallMethod(copy_reg
, "_reduce_ex", "(Oi)", self
, proto
);
2766 Py_DECREF(copy_reg
);
2771 static PyMethodDef object_methods
[] = {
2772 {"__reduce_ex__", object_reduce_ex
, METH_VARARGS
,
2773 PyDoc_STR("helper for pickle")},
2774 {"__reduce__", object_reduce_ex
, METH_VARARGS
,
2775 PyDoc_STR("helper for pickle")},
2780 PyTypeObject PyBaseObject_Type
= {
2781 PyObject_HEAD_INIT(&PyType_Type
)
2783 "object", /* tp_name */
2784 sizeof(PyObject
), /* tp_basicsize */
2785 0, /* tp_itemsize */
2786 (destructor
)object_dealloc
, /* tp_dealloc */
2791 object_repr
, /* tp_repr */
2792 0, /* tp_as_number */
2793 0, /* tp_as_sequence */
2794 0, /* tp_as_mapping */
2795 object_hash
, /* tp_hash */
2797 object_str
, /* tp_str */
2798 PyObject_GenericGetAttr
, /* tp_getattro */
2799 PyObject_GenericSetAttr
, /* tp_setattro */
2800 0, /* tp_as_buffer */
2801 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
2802 PyDoc_STR("The most base type"), /* tp_doc */
2803 0, /* tp_traverse */
2805 0, /* tp_richcompare */
2806 0, /* tp_weaklistoffset */
2808 0, /* tp_iternext */
2809 object_methods
, /* tp_methods */
2811 object_getsets
, /* tp_getset */
2814 0, /* tp_descr_get */
2815 0, /* tp_descr_set */
2816 0, /* tp_dictoffset */
2817 object_init
, /* tp_init */
2818 PyType_GenericAlloc
, /* tp_alloc */
2819 object_new
, /* tp_new */
2820 PyObject_Del
, /* tp_free */
2824 /* Initialize the __dict__ in a type object */
2827 add_methods(PyTypeObject
*type
, PyMethodDef
*meth
)
2829 PyObject
*dict
= type
->tp_dict
;
2831 for (; meth
->ml_name
!= NULL
; meth
++) {
2833 if (PyDict_GetItemString(dict
, meth
->ml_name
) &&
2834 !(meth
->ml_flags
& METH_COEXIST
))
2836 if (meth
->ml_flags
& METH_CLASS
) {
2837 if (meth
->ml_flags
& METH_STATIC
) {
2838 PyErr_SetString(PyExc_ValueError
,
2839 "method cannot be both class and static");
2842 descr
= PyDescr_NewClassMethod(type
, meth
);
2844 else if (meth
->ml_flags
& METH_STATIC
) {
2845 PyObject
*cfunc
= PyCFunction_New(meth
, NULL
);
2848 descr
= PyStaticMethod_New(cfunc
);
2852 descr
= PyDescr_NewMethod(type
, meth
);
2856 if (PyDict_SetItemString(dict
, meth
->ml_name
, descr
) < 0)
2864 add_members(PyTypeObject
*type
, PyMemberDef
*memb
)
2866 PyObject
*dict
= type
->tp_dict
;
2868 for (; memb
->name
!= NULL
; memb
++) {
2870 if (PyDict_GetItemString(dict
, memb
->name
))
2872 descr
= PyDescr_NewMember(type
, memb
);
2875 if (PyDict_SetItemString(dict
, memb
->name
, descr
) < 0)
2883 add_getset(PyTypeObject
*type
, PyGetSetDef
*gsp
)
2885 PyObject
*dict
= type
->tp_dict
;
2887 for (; gsp
->name
!= NULL
; gsp
++) {
2889 if (PyDict_GetItemString(dict
, gsp
->name
))
2891 descr
= PyDescr_NewGetSet(type
, gsp
);
2895 if (PyDict_SetItemString(dict
, gsp
->name
, descr
) < 0)
2903 inherit_special(PyTypeObject
*type
, PyTypeObject
*base
)
2905 int oldsize
, newsize
;
2907 /* Special flag magic */
2908 if (!type
->tp_as_buffer
&& base
->tp_as_buffer
) {
2909 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_GETCHARBUFFER
;
2911 base
->tp_flags
& Py_TPFLAGS_HAVE_GETCHARBUFFER
;
2913 if (!type
->tp_as_sequence
&& base
->tp_as_sequence
) {
2914 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_SEQUENCE_IN
;
2915 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_HAVE_SEQUENCE_IN
;
2917 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
) !=
2918 (base
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
)) {
2919 if ((!type
->tp_as_number
&& base
->tp_as_number
) ||
2920 (!type
->tp_as_sequence
&& base
->tp_as_sequence
)) {
2921 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_INPLACEOPS
;
2922 if (!type
->tp_as_number
&& !type
->tp_as_sequence
) {
2923 type
->tp_flags
|= base
->tp_flags
&
2924 Py_TPFLAGS_HAVE_INPLACEOPS
;
2929 if (!type
->tp_as_number
&& base
->tp_as_number
) {
2930 type
->tp_flags
&= ~Py_TPFLAGS_CHECKTYPES
;
2931 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
;
2934 /* Copying basicsize is connected to the GC flags */
2935 oldsize
= base
->tp_basicsize
;
2936 newsize
= type
->tp_basicsize
? type
->tp_basicsize
: oldsize
;
2937 if (!(type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
2938 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
2939 (type
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
/*GC slots exist*/) &&
2940 (!type
->tp_traverse
&& !type
->tp_clear
)) {
2941 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
2942 if (type
->tp_traverse
== NULL
)
2943 type
->tp_traverse
= base
->tp_traverse
;
2944 if (type
->tp_clear
== NULL
)
2945 type
->tp_clear
= base
->tp_clear
;
2947 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
2948 /* The condition below could use some explanation.
2949 It appears that tp_new is not inherited for static types
2950 whose base class is 'object'; this seems to be a precaution
2951 so that old extension types don't suddenly become
2952 callable (object.__new__ wouldn't insure the invariants
2953 that the extension type's own factory function ensures).
2954 Heap types, of course, are under our control, so they do
2955 inherit tp_new; static extension types that specify some
2956 other built-in type as the default are considered
2957 new-style-aware so they also inherit object.__new__. */
2958 if (base
!= &PyBaseObject_Type
||
2959 (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
2960 if (type
->tp_new
== NULL
)
2961 type
->tp_new
= base
->tp_new
;
2964 type
->tp_basicsize
= newsize
;
2966 /* Copy other non-function slots */
2969 #define COPYVAL(SLOT) \
2970 if (type->SLOT == 0) type->SLOT = base->SLOT
2972 COPYVAL(tp_itemsize
);
2973 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_WEAKREFS
) {
2974 COPYVAL(tp_weaklistoffset
);
2976 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
2977 COPYVAL(tp_dictoffset
);
2982 inherit_slots(PyTypeObject
*type
, PyTypeObject
*base
)
2984 PyTypeObject
*basebase
;
2993 #define SLOTDEFINED(SLOT) \
2994 (base->SLOT != 0 && \
2995 (basebase == NULL || base->SLOT != basebase->SLOT))
2997 #define COPYSLOT(SLOT) \
2998 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3000 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3001 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3002 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3003 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3005 /* This won't inherit indirect slots (from tp_as_number etc.)
3006 if type doesn't provide the space. */
3008 if (type
->tp_as_number
!= NULL
&& base
->tp_as_number
!= NULL
) {
3009 basebase
= base
->tp_base
;
3010 if (basebase
->tp_as_number
== NULL
)
3013 COPYNUM(nb_subtract
);
3014 COPYNUM(nb_multiply
);
3016 COPYNUM(nb_remainder
);
3019 COPYNUM(nb_negative
);
3020 COPYNUM(nb_positive
);
3021 COPYNUM(nb_absolute
);
3022 COPYNUM(nb_nonzero
);
3035 COPYNUM(nb_inplace_add
);
3036 COPYNUM(nb_inplace_subtract
);
3037 COPYNUM(nb_inplace_multiply
);
3038 COPYNUM(nb_inplace_divide
);
3039 COPYNUM(nb_inplace_remainder
);
3040 COPYNUM(nb_inplace_power
);
3041 COPYNUM(nb_inplace_lshift
);
3042 COPYNUM(nb_inplace_rshift
);
3043 COPYNUM(nb_inplace_and
);
3044 COPYNUM(nb_inplace_xor
);
3045 COPYNUM(nb_inplace_or
);
3046 if (base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) {
3047 COPYNUM(nb_true_divide
);
3048 COPYNUM(nb_floor_divide
);
3049 COPYNUM(nb_inplace_true_divide
);
3050 COPYNUM(nb_inplace_floor_divide
);
3054 if (type
->tp_as_sequence
!= NULL
&& base
->tp_as_sequence
!= NULL
) {
3055 basebase
= base
->tp_base
;
3056 if (basebase
->tp_as_sequence
== NULL
)
3063 COPYSEQ(sq_ass_item
);
3064 COPYSEQ(sq_ass_slice
);
3065 COPYSEQ(sq_contains
);
3066 COPYSEQ(sq_inplace_concat
);
3067 COPYSEQ(sq_inplace_repeat
);
3070 if (type
->tp_as_mapping
!= NULL
&& base
->tp_as_mapping
!= NULL
) {
3071 basebase
= base
->tp_base
;
3072 if (basebase
->tp_as_mapping
== NULL
)
3075 COPYMAP(mp_subscript
);
3076 COPYMAP(mp_ass_subscript
);
3079 if (type
->tp_as_buffer
!= NULL
&& base
->tp_as_buffer
!= NULL
) {
3080 basebase
= base
->tp_base
;
3081 if (basebase
->tp_as_buffer
== NULL
)
3083 COPYBUF(bf_getreadbuffer
);
3084 COPYBUF(bf_getwritebuffer
);
3085 COPYBUF(bf_getsegcount
);
3086 COPYBUF(bf_getcharbuffer
);
3089 basebase
= base
->tp_base
;
3091 COPYSLOT(tp_dealloc
);
3093 if (type
->tp_getattr
== NULL
&& type
->tp_getattro
== NULL
) {
3094 type
->tp_getattr
= base
->tp_getattr
;
3095 type
->tp_getattro
= base
->tp_getattro
;
3097 if (type
->tp_setattr
== NULL
&& type
->tp_setattro
== NULL
) {
3098 type
->tp_setattr
= base
->tp_setattr
;
3099 type
->tp_setattro
= base
->tp_setattro
;
3101 /* tp_compare see tp_richcompare */
3103 /* tp_hash see tp_richcompare */
3106 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
) {
3107 if (type
->tp_compare
== NULL
&&
3108 type
->tp_richcompare
== NULL
&&
3109 type
->tp_hash
== NULL
)
3111 type
->tp_compare
= base
->tp_compare
;
3112 type
->tp_richcompare
= base
->tp_richcompare
;
3113 type
->tp_hash
= base
->tp_hash
;
3117 COPYSLOT(tp_compare
);
3119 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_ITER
) {
3121 COPYSLOT(tp_iternext
);
3123 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
3124 COPYSLOT(tp_descr_get
);
3125 COPYSLOT(tp_descr_set
);
3126 COPYSLOT(tp_dictoffset
);
3130 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
3131 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)) {
3132 /* They agree about gc. */
3135 else if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3136 type
->tp_free
== NULL
&&
3137 base
->tp_free
== _PyObject_Del
) {
3138 /* A bit of magic to plug in the correct default
3139 * tp_free function when a derived class adds gc,
3140 * didn't define tp_free, and the base uses the
3141 * default non-gc tp_free.
3143 type
->tp_free
= PyObject_GC_Del
;
3145 /* else they didn't agree about gc, and there isn't something
3146 * obvious to be done -- the type is on its own.
3151 static int add_operators(PyTypeObject
*);
3154 PyType_Ready(PyTypeObject
*type
)
3156 PyObject
*dict
, *bases
;
3160 if (type
->tp_flags
& Py_TPFLAGS_READY
) {
3161 assert(type
->tp_dict
!= NULL
);
3164 assert((type
->tp_flags
& Py_TPFLAGS_READYING
) == 0);
3166 type
->tp_flags
|= Py_TPFLAGS_READYING
;
3168 #ifdef Py_TRACE_REFS
3169 /* PyType_Ready is the closest thing we have to a choke point
3170 * for type objects, so is the best place I can think of to try
3171 * to get type objects into the doubly-linked list of all objects.
3172 * Still, not all type objects go thru PyType_Ready.
3174 _Py_AddToAllObjects((PyObject
*)type
, 0);
3177 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3178 base
= type
->tp_base
;
3179 if (base
== NULL
&& type
!= &PyBaseObject_Type
) {
3180 base
= type
->tp_base
= &PyBaseObject_Type
;
3184 /* Initialize the base class */
3185 if (base
&& base
->tp_dict
== NULL
) {
3186 if (PyType_Ready(base
) < 0)
3190 /* Initialize ob_type if NULL. This means extensions that want to be
3191 compilable separately on Windows can call PyType_Ready() instead of
3192 initializing the ob_type field of their type objects. */
3193 if (type
->ob_type
== NULL
)
3194 type
->ob_type
= base
->ob_type
;
3196 /* Initialize tp_bases */
3197 bases
= type
->tp_bases
;
3198 if (bases
== NULL
) {
3200 bases
= PyTuple_New(0);
3202 bases
= PyTuple_Pack(1, base
);
3205 type
->tp_bases
= bases
;
3208 /* Initialize tp_dict */
3209 dict
= type
->tp_dict
;
3211 dict
= PyDict_New();
3214 type
->tp_dict
= dict
;
3217 /* Add type-specific descriptors to tp_dict */
3218 if (add_operators(type
) < 0)
3220 if (type
->tp_methods
!= NULL
) {
3221 if (add_methods(type
, type
->tp_methods
) < 0)
3224 if (type
->tp_members
!= NULL
) {
3225 if (add_members(type
, type
->tp_members
) < 0)
3228 if (type
->tp_getset
!= NULL
) {
3229 if (add_getset(type
, type
->tp_getset
) < 0)
3233 /* Calculate method resolution order */
3234 if (mro_internal(type
) < 0) {
3238 /* Inherit special flags from dominant base */
3239 if (type
->tp_base
!= NULL
)
3240 inherit_special(type
, type
->tp_base
);
3242 /* Initialize tp_dict properly */
3243 bases
= type
->tp_mro
;
3244 assert(bases
!= NULL
);
3245 assert(PyTuple_Check(bases
));
3246 n
= PyTuple_GET_SIZE(bases
);
3247 for (i
= 1; i
< n
; i
++) {
3248 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3249 if (PyType_Check(b
))
3250 inherit_slots(type
, (PyTypeObject
*)b
);
3253 /* Sanity check for tp_free. */
3254 if (PyType_IS_GC(type
) && (type
->tp_flags
& Py_TPFLAGS_BASETYPE
) &&
3255 (type
->tp_free
== NULL
|| type
->tp_free
== PyObject_Del
)) {
3256 /* This base class needs to call tp_free, but doesn't have
3257 * one, or its tp_free is for non-gc'ed objects.
3259 PyErr_Format(PyExc_TypeError
, "type '%.100s' participates in "
3260 "gc and is a base type but has inappropriate "
3266 /* if the type dictionary doesn't contain a __doc__, set it from
3269 if (PyDict_GetItemString(type
->tp_dict
, "__doc__") == NULL
) {
3270 if (type
->tp_doc
!= NULL
) {
3271 PyObject
*doc
= PyString_FromString(type
->tp_doc
);
3272 PyDict_SetItemString(type
->tp_dict
, "__doc__", doc
);
3275 PyDict_SetItemString(type
->tp_dict
,
3276 "__doc__", Py_None
);
3280 /* Some more special stuff */
3281 base
= type
->tp_base
;
3283 if (type
->tp_as_number
== NULL
)
3284 type
->tp_as_number
= base
->tp_as_number
;
3285 if (type
->tp_as_sequence
== NULL
)
3286 type
->tp_as_sequence
= base
->tp_as_sequence
;
3287 if (type
->tp_as_mapping
== NULL
)
3288 type
->tp_as_mapping
= base
->tp_as_mapping
;
3289 if (type
->tp_as_buffer
== NULL
)
3290 type
->tp_as_buffer
= base
->tp_as_buffer
;
3293 /* Link into each base class's list of subclasses */
3294 bases
= type
->tp_bases
;
3295 n
= PyTuple_GET_SIZE(bases
);
3296 for (i
= 0; i
< n
; i
++) {
3297 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3298 if (PyType_Check(b
) &&
3299 add_subclass((PyTypeObject
*)b
, type
) < 0)
3303 /* All done -- set the ready flag */
3304 assert(type
->tp_dict
!= NULL
);
3306 (type
->tp_flags
& ~Py_TPFLAGS_READYING
) | Py_TPFLAGS_READY
;
3310 type
->tp_flags
&= ~Py_TPFLAGS_READYING
;
3315 add_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3318 PyObject
*list
, *ref
, *new;
3320 list
= base
->tp_subclasses
;
3322 base
->tp_subclasses
= list
= PyList_New(0);
3326 assert(PyList_Check(list
));
3327 new = PyWeakref_NewRef((PyObject
*)type
, NULL
);
3328 i
= PyList_GET_SIZE(list
);
3330 ref
= PyList_GET_ITEM(list
, i
);
3331 assert(PyWeakref_CheckRef(ref
));
3332 if (PyWeakref_GET_OBJECT(ref
) == Py_None
)
3333 return PyList_SetItem(list
, i
, new);
3335 i
= PyList_Append(list
, new);
3341 remove_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3344 PyObject
*list
, *ref
;
3346 list
= base
->tp_subclasses
;
3350 assert(PyList_Check(list
));
3351 i
= PyList_GET_SIZE(list
);
3353 ref
= PyList_GET_ITEM(list
, i
);
3354 assert(PyWeakref_CheckRef(ref
));
3355 if (PyWeakref_GET_OBJECT(ref
) == (PyObject
*)type
) {
3356 /* this can't fail, right? */
3357 PySequence_DelItem(list
, i
);
3364 check_num_args(PyObject
*ob
, int n
)
3366 if (!PyTuple_CheckExact(ob
)) {
3367 PyErr_SetString(PyExc_SystemError
,
3368 "PyArg_UnpackTuple() argument list is not a tuple");
3371 if (n
== PyTuple_GET_SIZE(ob
))
3375 "expected %d arguments, got %d", n
, PyTuple_GET_SIZE(ob
));
3379 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3381 /* There's a wrapper *function* for each distinct function typedef used
3382 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3383 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3384 Most tables have only one entry; the tables for binary operators have two
3385 entries, one regular and one with reversed arguments. */
3388 wrap_inquiry(PyObject
*self
, PyObject
*args
, void *wrapped
)
3390 inquiry func
= (inquiry
)wrapped
;
3393 if (!check_num_args(args
, 0))
3395 res
= (*func
)(self
);
3396 if (res
== -1 && PyErr_Occurred())
3398 return PyInt_FromLong((long)res
);
3402 wrap_inquirypred(PyObject
*self
, PyObject
*args
, void *wrapped
)
3404 inquiry func
= (inquiry
)wrapped
;
3407 if (!check_num_args(args
, 0))
3409 res
= (*func
)(self
);
3410 if (res
== -1 && PyErr_Occurred())
3412 return PyBool_FromLong((long)res
);
3416 wrap_binaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3418 binaryfunc func
= (binaryfunc
)wrapped
;
3421 if (!check_num_args(args
, 1))
3423 other
= PyTuple_GET_ITEM(args
, 0);
3424 return (*func
)(self
, other
);
3428 wrap_binaryfunc_l(PyObject
*self
, PyObject
*args
, void *wrapped
)
3430 binaryfunc func
= (binaryfunc
)wrapped
;
3433 if (!check_num_args(args
, 1))
3435 other
= PyTuple_GET_ITEM(args
, 0);
3436 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3437 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3438 Py_INCREF(Py_NotImplemented
);
3439 return Py_NotImplemented
;
3441 return (*func
)(self
, other
);
3445 wrap_binaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3447 binaryfunc func
= (binaryfunc
)wrapped
;
3450 if (!check_num_args(args
, 1))
3452 other
= PyTuple_GET_ITEM(args
, 0);
3453 if (!(self
->ob_type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
3454 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3455 Py_INCREF(Py_NotImplemented
);
3456 return Py_NotImplemented
;
3458 return (*func
)(other
, self
);
3462 wrap_coercefunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3464 coercion func
= (coercion
)wrapped
;
3465 PyObject
*other
, *res
;
3468 if (!check_num_args(args
, 1))
3470 other
= PyTuple_GET_ITEM(args
, 0);
3471 ok
= func(&self
, &other
);
3475 Py_INCREF(Py_NotImplemented
);
3476 return Py_NotImplemented
;
3478 res
= PyTuple_New(2);
3484 PyTuple_SET_ITEM(res
, 0, self
);
3485 PyTuple_SET_ITEM(res
, 1, other
);
3490 wrap_ternaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3492 ternaryfunc func
= (ternaryfunc
)wrapped
;
3494 PyObject
*third
= Py_None
;
3496 /* Note: This wrapper only works for __pow__() */
3498 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3500 return (*func
)(self
, other
, third
);
3504 wrap_ternaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
3506 ternaryfunc func
= (ternaryfunc
)wrapped
;
3508 PyObject
*third
= Py_None
;
3510 /* Note: This wrapper only works for __pow__() */
3512 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
3514 return (*func
)(other
, self
, third
);
3518 wrap_unaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3520 unaryfunc func
= (unaryfunc
)wrapped
;
3522 if (!check_num_args(args
, 0))
3524 return (*func
)(self
);
3528 wrap_intargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3530 intargfunc func
= (intargfunc
)wrapped
;
3533 if (!PyArg_ParseTuple(args
, "i", &i
))
3535 return (*func
)(self
, i
);
3539 getindex(PyObject
*self
, PyObject
*arg
)
3543 i
= PyInt_AsLong(arg
);
3544 if (i
== -1 && PyErr_Occurred())
3547 PySequenceMethods
*sq
= self
->ob_type
->tp_as_sequence
;
3548 if (sq
&& sq
->sq_length
) {
3549 int n
= (*sq
->sq_length
)(self
);
3559 wrap_sq_item(PyObject
*self
, PyObject
*args
, void *wrapped
)
3561 intargfunc func
= (intargfunc
)wrapped
;
3565 if (PyTuple_GET_SIZE(args
) == 1) {
3566 arg
= PyTuple_GET_ITEM(args
, 0);
3567 i
= getindex(self
, arg
);
3568 if (i
== -1 && PyErr_Occurred())
3570 return (*func
)(self
, i
);
3572 check_num_args(args
, 1);
3573 assert(PyErr_Occurred());
3578 wrap_intintargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3580 intintargfunc func
= (intintargfunc
)wrapped
;
3583 if (!PyArg_ParseTuple(args
, "ii", &i
, &j
))
3585 return (*func
)(self
, i
, j
);
3589 wrap_sq_setitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3591 intobjargproc func
= (intobjargproc
)wrapped
;
3593 PyObject
*arg
, *value
;
3595 if (!PyArg_UnpackTuple(args
, "", 2, 2, &arg
, &value
))
3597 i
= getindex(self
, arg
);
3598 if (i
== -1 && PyErr_Occurred())
3600 res
= (*func
)(self
, i
, value
);
3601 if (res
== -1 && PyErr_Occurred())
3608 wrap_sq_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3610 intobjargproc func
= (intobjargproc
)wrapped
;
3614 if (!check_num_args(args
, 1))
3616 arg
= PyTuple_GET_ITEM(args
, 0);
3617 i
= getindex(self
, arg
);
3618 if (i
== -1 && PyErr_Occurred())
3620 res
= (*func
)(self
, i
, NULL
);
3621 if (res
== -1 && PyErr_Occurred())
3628 wrap_intintobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3630 intintobjargproc func
= (intintobjargproc
)wrapped
;
3634 if (!PyArg_ParseTuple(args
, "iiO", &i
, &j
, &value
))
3636 res
= (*func
)(self
, i
, j
, value
);
3637 if (res
== -1 && PyErr_Occurred())
3644 wrap_delslice(PyObject
*self
, PyObject
*args
, void *wrapped
)
3646 intintobjargproc func
= (intintobjargproc
)wrapped
;
3649 if (!PyArg_ParseTuple(args
, "ii", &i
, &j
))
3651 res
= (*func
)(self
, i
, j
, NULL
);
3652 if (res
== -1 && PyErr_Occurred())
3658 /* XXX objobjproc is a misnomer; should be objargpred */
3660 wrap_objobjproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3662 objobjproc func
= (objobjproc
)wrapped
;
3666 if (!check_num_args(args
, 1))
3668 value
= PyTuple_GET_ITEM(args
, 0);
3669 res
= (*func
)(self
, value
);
3670 if (res
== -1 && PyErr_Occurred())
3673 return PyBool_FromLong(res
);
3677 wrap_objobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3679 objobjargproc func
= (objobjargproc
)wrapped
;
3681 PyObject
*key
, *value
;
3683 if (!PyArg_UnpackTuple(args
, "", 2, 2, &key
, &value
))
3685 res
= (*func
)(self
, key
, value
);
3686 if (res
== -1 && PyErr_Occurred())
3693 wrap_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
3695 objobjargproc func
= (objobjargproc
)wrapped
;
3699 if (!check_num_args(args
, 1))
3701 key
= PyTuple_GET_ITEM(args
, 0);
3702 res
= (*func
)(self
, key
, NULL
);
3703 if (res
== -1 && PyErr_Occurred())
3710 wrap_cmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3712 cmpfunc func
= (cmpfunc
)wrapped
;
3716 if (!check_num_args(args
, 1))
3718 other
= PyTuple_GET_ITEM(args
, 0);
3719 if (other
->ob_type
->tp_compare
!= func
&&
3720 !PyType_IsSubtype(other
->ob_type
, self
->ob_type
)) {
3723 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3724 self
->ob_type
->tp_name
,
3725 self
->ob_type
->tp_name
,
3726 other
->ob_type
->tp_name
);
3729 res
= (*func
)(self
, other
);
3730 if (PyErr_Occurred())
3732 return PyInt_FromLong((long)res
);
3735 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3736 This is called the Carlo Verre hack after its discoverer. */
3738 hackcheck(PyObject
*self
, setattrofunc func
, char *what
)
3740 PyTypeObject
*type
= self
->ob_type
;
3741 while (type
&& type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
3742 type
= type
->tp_base
;
3743 if (type
->tp_setattro
!= func
) {
3744 PyErr_Format(PyExc_TypeError
,
3745 "can't apply this %s to %s object",
3754 wrap_setattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
3756 setattrofunc func
= (setattrofunc
)wrapped
;
3758 PyObject
*name
, *value
;
3760 if (!PyArg_UnpackTuple(args
, "", 2, 2, &name
, &value
))
3762 if (!hackcheck(self
, func
, "__setattr__"))
3764 res
= (*func
)(self
, name
, value
);
3772 wrap_delattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
3774 setattrofunc func
= (setattrofunc
)wrapped
;
3778 if (!check_num_args(args
, 1))
3780 name
= PyTuple_GET_ITEM(args
, 0);
3781 if (!hackcheck(self
, func
, "__delattr__"))
3783 res
= (*func
)(self
, name
, NULL
);
3791 wrap_hashfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3793 hashfunc func
= (hashfunc
)wrapped
;
3796 if (!check_num_args(args
, 0))
3798 res
= (*func
)(self
);
3799 if (res
== -1 && PyErr_Occurred())
3801 return PyInt_FromLong(res
);
3805 wrap_call(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
3807 ternaryfunc func
= (ternaryfunc
)wrapped
;
3809 return (*func
)(self
, args
, kwds
);
3813 wrap_richcmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
, int op
)
3815 richcmpfunc func
= (richcmpfunc
)wrapped
;
3818 if (!check_num_args(args
, 1))
3820 other
= PyTuple_GET_ITEM(args
, 0);
3821 return (*func
)(self
, other
, op
);
3824 #undef RICHCMP_WRAPPER
3825 #define RICHCMP_WRAPPER(NAME, OP) \
3827 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3829 return wrap_richcmpfunc(self, args, wrapped, OP); \
3832 RICHCMP_WRAPPER(lt
, Py_LT
)
3833 RICHCMP_WRAPPER(le
, Py_LE
)
3834 RICHCMP_WRAPPER(eq
, Py_EQ
)
3835 RICHCMP_WRAPPER(ne
, Py_NE
)
3836 RICHCMP_WRAPPER(gt
, Py_GT
)
3837 RICHCMP_WRAPPER(ge
, Py_GE
)
3840 wrap_next(PyObject
*self
, PyObject
*args
, void *wrapped
)
3842 unaryfunc func
= (unaryfunc
)wrapped
;
3845 if (!check_num_args(args
, 0))
3847 res
= (*func
)(self
);
3848 if (res
== NULL
&& !PyErr_Occurred())
3849 PyErr_SetNone(PyExc_StopIteration
);
3854 wrap_descr_get(PyObject
*self
, PyObject
*args
, void *wrapped
)
3856 descrgetfunc func
= (descrgetfunc
)wrapped
;
3858 PyObject
*type
= NULL
;
3860 if (!PyArg_UnpackTuple(args
, "", 1, 2, &obj
, &type
))
3864 if (type
== Py_None
)
3866 if (type
== NULL
&&obj
== NULL
) {
3867 PyErr_SetString(PyExc_TypeError
,
3868 "__get__(None, None) is invalid");
3871 return (*func
)(self
, obj
, type
);
3875 wrap_descr_set(PyObject
*self
, PyObject
*args
, void *wrapped
)
3877 descrsetfunc func
= (descrsetfunc
)wrapped
;
3878 PyObject
*obj
, *value
;
3881 if (!PyArg_UnpackTuple(args
, "", 2, 2, &obj
, &value
))
3883 ret
= (*func
)(self
, obj
, value
);
3891 wrap_descr_delete(PyObject
*self
, PyObject
*args
, void *wrapped
)
3893 descrsetfunc func
= (descrsetfunc
)wrapped
;
3897 if (!check_num_args(args
, 1))
3899 obj
= PyTuple_GET_ITEM(args
, 0);
3900 ret
= (*func
)(self
, obj
, NULL
);
3908 wrap_init(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
3910 initproc func
= (initproc
)wrapped
;
3912 if (func(self
, args
, kwds
) < 0)
3919 tp_new_wrapper(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
3921 PyTypeObject
*type
, *subtype
, *staticbase
;
3922 PyObject
*arg0
, *res
;
3924 if (self
== NULL
|| !PyType_Check(self
))
3925 Py_FatalError("__new__() called with non-type 'self'");
3926 type
= (PyTypeObject
*)self
;
3927 if (!PyTuple_Check(args
) || PyTuple_GET_SIZE(args
) < 1) {
3928 PyErr_Format(PyExc_TypeError
,
3929 "%s.__new__(): not enough arguments",
3933 arg0
= PyTuple_GET_ITEM(args
, 0);
3934 if (!PyType_Check(arg0
)) {
3935 PyErr_Format(PyExc_TypeError
,
3936 "%s.__new__(X): X is not a type object (%s)",
3938 arg0
->ob_type
->tp_name
);
3941 subtype
= (PyTypeObject
*)arg0
;
3942 if (!PyType_IsSubtype(subtype
, type
)) {
3943 PyErr_Format(PyExc_TypeError
,
3944 "%s.__new__(%s): %s is not a subtype of %s",
3952 /* Check that the use doesn't do something silly and unsafe like
3953 object.__new__(dict). To do this, we check that the
3954 most derived base that's not a heap type is this type. */
3955 staticbase
= subtype
;
3956 while (staticbase
&& (staticbase
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
3957 staticbase
= staticbase
->tp_base
;
3958 if (staticbase
->tp_new
!= type
->tp_new
) {
3959 PyErr_Format(PyExc_TypeError
,
3960 "%s.__new__(%s) is not safe, use %s.__new__()",
3963 staticbase
== NULL
? "?" : staticbase
->tp_name
);
3967 args
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
3970 res
= type
->tp_new(subtype
, args
, kwds
);
3975 static struct PyMethodDef tp_new_methoddef
[] = {
3976 {"__new__", (PyCFunction
)tp_new_wrapper
, METH_KEYWORDS
,
3977 PyDoc_STR("T.__new__(S, ...) -> "
3978 "a new object with type S, a subtype of T")},
3983 add_tp_new_wrapper(PyTypeObject
*type
)
3987 if (PyDict_GetItemString(type
->tp_dict
, "__new__") != NULL
)
3989 func
= PyCFunction_New(tp_new_methoddef
, (PyObject
*)type
);
3992 if (PyDict_SetItemString(type
->tp_dict
, "__new__", func
)) {
4000 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4001 below at override_slots() for more explanation. */
4003 #define SLOT0(FUNCNAME, OPSTR) \
4005 FUNCNAME(PyObject *self) \
4007 static PyObject *cache_str; \
4008 return call_method(self, OPSTR, &cache_str, "()"); \
4011 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4013 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4015 static PyObject *cache_str; \
4016 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4019 /* Boolean helper for SLOT1BINFULL().
4020 right.__class__ is a nontrivial subclass of left.__class__. */
4022 method_is_overloaded(PyObject
*left
, PyObject
*right
, char *name
)
4027 b
= PyObject_GetAttrString((PyObject
*)(right
->ob_type
), name
);
4030 /* If right doesn't have it, it's not overloaded */
4034 a
= PyObject_GetAttrString((PyObject
*)(left
->ob_type
), name
);
4038 /* If right has it but left doesn't, it's overloaded */
4042 ok
= PyObject_RichCompareBool(a
, b
, Py_NE
);
4054 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4056 FUNCNAME(PyObject *self, PyObject *other) \
4058 static PyObject *cache_str, *rcache_str; \
4059 int do_other = self->ob_type != other->ob_type && \
4060 other->ob_type->tp_as_number != NULL && \
4061 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4062 if (self->ob_type->tp_as_number != NULL && \
4063 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4066 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4067 method_is_overloaded(self, other, ROPSTR)) { \
4069 other, ROPSTR, &rcache_str, "(O)", self); \
4070 if (r != Py_NotImplemented) \
4076 self, OPSTR, &cache_str, "(O)", other); \
4077 if (r != Py_NotImplemented || \
4078 other->ob_type == self->ob_type) \
4083 return call_maybe( \
4084 other, ROPSTR, &rcache_str, "(O)", self); \
4086 Py_INCREF(Py_NotImplemented); \
4087 return Py_NotImplemented; \
4090 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4091 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4093 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4095 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4097 static PyObject *cache_str; \
4098 return call_method(self, OPSTR, &cache_str, \
4099 "(" ARGCODES ")", arg1, arg2); \
4103 slot_sq_length(PyObject
*self
)
4105 static PyObject
*len_str
;
4106 PyObject
*res
= call_method(self
, "__len__", &len_str
, "()");
4112 temp
= PyInt_AsLong(res
);
4115 if (len
== -1 && PyErr_Occurred())
4117 #if SIZEOF_INT < SIZEOF_LONG
4118 /* Overflow check -- range of PyInt is more than C int */
4120 PyErr_SetString(PyExc_OverflowError
,
4121 "__len__() should return 0 <= outcome < 2**31");
4126 PyErr_SetString(PyExc_ValueError
,
4127 "__len__() should return >= 0");
4133 /* Super-optimized version of slot_sq_item.
4134 Other slots could do the same... */
4136 slot_sq_item(PyObject
*self
, int i
)
4138 static PyObject
*getitem_str
;
4139 PyObject
*func
, *args
= NULL
, *ival
= NULL
, *retval
= NULL
;
4142 if (getitem_str
== NULL
) {
4143 getitem_str
= PyString_InternFromString("__getitem__");
4144 if (getitem_str
== NULL
)
4147 func
= _PyType_Lookup(self
->ob_type
, getitem_str
);
4149 if ((f
= func
->ob_type
->tp_descr_get
) == NULL
)
4152 func
= f(func
, self
, (PyObject
*)(self
->ob_type
));
4157 ival
= PyInt_FromLong(i
);
4159 args
= PyTuple_New(1);
4161 PyTuple_SET_ITEM(args
, 0, ival
);
4162 retval
= PyObject_Call(func
, args
, NULL
);
4170 PyErr_SetObject(PyExc_AttributeError
, getitem_str
);
4178 SLOT2(slot_sq_slice
, "__getslice__", int, int, "ii")
4181 slot_sq_ass_item(PyObject
*self
, int index
, PyObject
*value
)
4184 static PyObject
*delitem_str
, *setitem_str
;
4187 res
= call_method(self
, "__delitem__", &delitem_str
,
4190 res
= call_method(self
, "__setitem__", &setitem_str
,
4191 "(iO)", index
, value
);
4199 slot_sq_ass_slice(PyObject
*self
, int i
, int j
, PyObject
*value
)
4202 static PyObject
*delslice_str
, *setslice_str
;
4205 res
= call_method(self
, "__delslice__", &delslice_str
,
4208 res
= call_method(self
, "__setslice__", &setslice_str
,
4209 "(iiO)", i
, j
, value
);
4217 slot_sq_contains(PyObject
*self
, PyObject
*value
)
4219 PyObject
*func
, *res
, *args
;
4222 static PyObject
*contains_str
;
4224 func
= lookup_maybe(self
, "__contains__", &contains_str
);
4226 args
= PyTuple_Pack(1, value
);
4230 res
= PyObject_Call(func
, args
, NULL
);
4235 result
= PyObject_IsTrue(res
);
4239 else if (! PyErr_Occurred()) {
4240 result
= _PySequence_IterSearch(self
, value
,
4241 PY_ITERSEARCH_CONTAINS
);
4246 #define slot_mp_length slot_sq_length
4248 SLOT1(slot_mp_subscript
, "__getitem__", PyObject
*, "O")
4251 slot_mp_ass_subscript(PyObject
*self
, PyObject
*key
, PyObject
*value
)
4254 static PyObject
*delitem_str
, *setitem_str
;
4257 res
= call_method(self
, "__delitem__", &delitem_str
,
4260 res
= call_method(self
, "__setitem__", &setitem_str
,
4261 "(OO)", key
, value
);
4268 SLOT1BIN(slot_nb_add
, nb_add
, "__add__", "__radd__")
4269 SLOT1BIN(slot_nb_subtract
, nb_subtract
, "__sub__", "__rsub__")
4270 SLOT1BIN(slot_nb_multiply
, nb_multiply
, "__mul__", "__rmul__")
4271 SLOT1BIN(slot_nb_divide
, nb_divide
, "__div__", "__rdiv__")
4272 SLOT1BIN(slot_nb_remainder
, nb_remainder
, "__mod__", "__rmod__")
4273 SLOT1BIN(slot_nb_divmod
, nb_divmod
, "__divmod__", "__rdivmod__")
4275 static PyObject
*slot_nb_power(PyObject
*, PyObject
*, PyObject
*);
4277 SLOT1BINFULL(slot_nb_power_binary
, slot_nb_power
,
4278 nb_power
, "__pow__", "__rpow__")
4281 slot_nb_power(PyObject
*self
, PyObject
*other
, PyObject
*modulus
)
4283 static PyObject
*pow_str
;
4285 if (modulus
== Py_None
)
4286 return slot_nb_power_binary(self
, other
);
4287 /* Three-arg power doesn't use __rpow__. But ternary_op
4288 can call this when the second argument's type uses
4289 slot_nb_power, so check before calling self.__pow__. */
4290 if (self
->ob_type
->tp_as_number
!= NULL
&&
4291 self
->ob_type
->tp_as_number
->nb_power
== slot_nb_power
) {
4292 return call_method(self
, "__pow__", &pow_str
,
4293 "(OO)", other
, modulus
);
4295 Py_INCREF(Py_NotImplemented
);
4296 return Py_NotImplemented
;
4299 SLOT0(slot_nb_negative
, "__neg__")
4300 SLOT0(slot_nb_positive
, "__pos__")
4301 SLOT0(slot_nb_absolute
, "__abs__")
4304 slot_nb_nonzero(PyObject
*self
)
4306 PyObject
*func
, *args
;
4307 static PyObject
*nonzero_str
, *len_str
;
4310 func
= lookup_maybe(self
, "__nonzero__", &nonzero_str
);
4312 if (PyErr_Occurred())
4314 func
= lookup_maybe(self
, "__len__", &len_str
);
4316 return PyErr_Occurred() ? -1 : 1;
4318 args
= PyTuple_New(0);
4320 PyObject
*temp
= PyObject_Call(func
, args
, NULL
);
4323 if (PyInt_CheckExact(temp
) || PyBool_Check(temp
))
4324 result
= PyObject_IsTrue(temp
);
4326 PyErr_Format(PyExc_TypeError
,
4327 "__nonzero__ should return "
4328 "bool or int, returned %s",
4329 temp
->ob_type
->tp_name
);
4339 SLOT0(slot_nb_invert
, "__invert__")
4340 SLOT1BIN(slot_nb_lshift
, nb_lshift
, "__lshift__", "__rlshift__")
4341 SLOT1BIN(slot_nb_rshift
, nb_rshift
, "__rshift__", "__rrshift__")
4342 SLOT1BIN(slot_nb_and
, nb_and
, "__and__", "__rand__")
4343 SLOT1BIN(slot_nb_xor
, nb_xor
, "__xor__", "__rxor__")
4344 SLOT1BIN(slot_nb_or
, nb_or
, "__or__", "__ror__")
4347 slot_nb_coerce(PyObject
**a
, PyObject
**b
)
4349 static PyObject
*coerce_str
;
4350 PyObject
*self
= *a
, *other
= *b
;
4352 if (self
->ob_type
->tp_as_number
!= NULL
&&
4353 self
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4356 self
, "__coerce__", &coerce_str
, "(O)", other
);
4359 if (r
== Py_NotImplemented
) {
4363 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4364 PyErr_SetString(PyExc_TypeError
,
4365 "__coerce__ didn't return a 2-tuple");
4369 *a
= PyTuple_GET_ITEM(r
, 0);
4371 *b
= PyTuple_GET_ITEM(r
, 1);
4377 if (other
->ob_type
->tp_as_number
!= NULL
&&
4378 other
->ob_type
->tp_as_number
->nb_coerce
== slot_nb_coerce
) {
4381 other
, "__coerce__", &coerce_str
, "(O)", self
);
4384 if (r
== Py_NotImplemented
) {
4388 if (!PyTuple_Check(r
) || PyTuple_GET_SIZE(r
) != 2) {
4389 PyErr_SetString(PyExc_TypeError
,
4390 "__coerce__ didn't return a 2-tuple");
4394 *a
= PyTuple_GET_ITEM(r
, 1);
4396 *b
= PyTuple_GET_ITEM(r
, 0);
4404 SLOT0(slot_nb_int
, "__int__")
4405 SLOT0(slot_nb_long
, "__long__")
4406 SLOT0(slot_nb_float
, "__float__")
4407 SLOT0(slot_nb_oct
, "__oct__")
4408 SLOT0(slot_nb_hex
, "__hex__")
4409 SLOT1(slot_nb_inplace_add
, "__iadd__", PyObject
*, "O")
4410 SLOT1(slot_nb_inplace_subtract
, "__isub__", PyObject
*, "O")
4411 SLOT1(slot_nb_inplace_multiply
, "__imul__", PyObject
*, "O")
4412 SLOT1(slot_nb_inplace_divide
, "__idiv__", PyObject
*, "O")
4413 SLOT1(slot_nb_inplace_remainder
, "__imod__", PyObject
*, "O")
4414 SLOT1(slot_nb_inplace_power
, "__ipow__", PyObject
*, "O")
4415 SLOT1(slot_nb_inplace_lshift
, "__ilshift__", PyObject
*, "O")
4416 SLOT1(slot_nb_inplace_rshift
, "__irshift__", PyObject
*, "O")
4417 SLOT1(slot_nb_inplace_and
, "__iand__", PyObject
*, "O")
4418 SLOT1(slot_nb_inplace_xor
, "__ixor__", PyObject
*, "O")
4419 SLOT1(slot_nb_inplace_or
, "__ior__", PyObject
*, "O")
4420 SLOT1BIN(slot_nb_floor_divide
, nb_floor_divide
,
4421 "__floordiv__", "__rfloordiv__")
4422 SLOT1BIN(slot_nb_true_divide
, nb_true_divide
, "__truediv__", "__rtruediv__")
4423 SLOT1(slot_nb_inplace_floor_divide
, "__ifloordiv__", PyObject
*, "O")
4424 SLOT1(slot_nb_inplace_true_divide
, "__itruediv__", PyObject
*, "O")
4427 half_compare(PyObject
*self
, PyObject
*other
)
4429 PyObject
*func
, *args
, *res
;
4430 static PyObject
*cmp_str
;
4433 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4438 args
= PyTuple_Pack(1, other
);
4442 res
= PyObject_Call(func
, args
, NULL
);
4446 if (res
!= Py_NotImplemented
) {
4449 c
= PyInt_AsLong(res
);
4451 if (c
== -1 && PyErr_Occurred())
4453 return (c
< 0) ? -1 : (c
> 0) ? 1 : 0;
4460 /* This slot is published for the benefit of try_3way_compare in object.c */
4462 _PyObject_SlotCompare(PyObject
*self
, PyObject
*other
)
4466 if (self
->ob_type
->tp_compare
== _PyObject_SlotCompare
) {
4467 c
= half_compare(self
, other
);
4471 if (other
->ob_type
->tp_compare
== _PyObject_SlotCompare
) {
4472 c
= half_compare(other
, self
);
4478 return (void *)self
< (void *)other
? -1 :
4479 (void *)self
> (void *)other
? 1 : 0;
4483 slot_tp_repr(PyObject
*self
)
4485 PyObject
*func
, *res
;
4486 static PyObject
*repr_str
;
4488 func
= lookup_method(self
, "__repr__", &repr_str
);
4490 res
= PyEval_CallObject(func
, NULL
);
4495 return PyString_FromFormat("<%s object at %p>",
4496 self
->ob_type
->tp_name
, self
);
4500 slot_tp_str(PyObject
*self
)
4502 PyObject
*func
, *res
;
4503 static PyObject
*str_str
;
4505 func
= lookup_method(self
, "__str__", &str_str
);
4507 res
= PyEval_CallObject(func
, NULL
);
4513 return slot_tp_repr(self
);
4518 slot_tp_hash(PyObject
*self
)
4521 static PyObject
*hash_str
, *eq_str
, *cmp_str
;
4524 func
= lookup_method(self
, "__hash__", &hash_str
);
4527 PyObject
*res
= PyEval_CallObject(func
, NULL
);
4531 h
= PyInt_AsLong(res
);
4536 func
= lookup_method(self
, "__eq__", &eq_str
);
4539 func
= lookup_method(self
, "__cmp__", &cmp_str
);
4543 PyErr_SetString(PyExc_TypeError
, "unhashable type");
4547 h
= _Py_HashPointer((void *)self
);
4549 if (h
== -1 && !PyErr_Occurred())
4555 slot_tp_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4557 static PyObject
*call_str
;
4558 PyObject
*meth
= lookup_method(self
, "__call__", &call_str
);
4563 res
= PyObject_Call(meth
, args
, kwds
);
4568 /* There are two slot dispatch functions for tp_getattro.
4570 - slot_tp_getattro() is used when __getattribute__ is overridden
4571 but no __getattr__ hook is present;
4573 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4575 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4576 detects the absence of __getattr__ and then installs the simpler slot if
4580 slot_tp_getattro(PyObject
*self
, PyObject
*name
)
4582 static PyObject
*getattribute_str
= NULL
;
4583 return call_method(self
, "__getattribute__", &getattribute_str
,
4588 slot_tp_getattr_hook(PyObject
*self
, PyObject
*name
)
4590 PyTypeObject
*tp
= self
->ob_type
;
4591 PyObject
*getattr
, *getattribute
, *res
;
4592 static PyObject
*getattribute_str
= NULL
;
4593 static PyObject
*getattr_str
= NULL
;
4595 if (getattr_str
== NULL
) {
4596 getattr_str
= PyString_InternFromString("__getattr__");
4597 if (getattr_str
== NULL
)
4600 if (getattribute_str
== NULL
) {
4602 PyString_InternFromString("__getattribute__");
4603 if (getattribute_str
== NULL
)
4606 getattr
= _PyType_Lookup(tp
, getattr_str
);
4607 if (getattr
== NULL
) {
4608 /* No __getattr__ hook: use a simpler dispatcher */
4609 tp
->tp_getattro
= slot_tp_getattro
;
4610 return slot_tp_getattro(self
, name
);
4612 getattribute
= _PyType_Lookup(tp
, getattribute_str
);
4613 if (getattribute
== NULL
||
4614 (getattribute
->ob_type
== &PyWrapperDescr_Type
&&
4615 ((PyWrapperDescrObject
*)getattribute
)->d_wrapped
==
4616 (void *)PyObject_GenericGetAttr
))
4617 res
= PyObject_GenericGetAttr(self
, name
);
4619 res
= PyObject_CallFunction(getattribute
, "OO", self
, name
);
4620 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4622 res
= PyObject_CallFunction(getattr
, "OO", self
, name
);
4628 slot_tp_setattro(PyObject
*self
, PyObject
*name
, PyObject
*value
)
4631 static PyObject
*delattr_str
, *setattr_str
;
4634 res
= call_method(self
, "__delattr__", &delattr_str
,
4637 res
= call_method(self
, "__setattr__", &setattr_str
,
4638 "(OO)", name
, value
);
4645 /* Map rich comparison operators to their __xx__ namesakes */
4646 static char *name_op
[] = {
4656 half_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4658 PyObject
*func
, *args
, *res
;
4659 static PyObject
*op_str
[6];
4661 func
= lookup_method(self
, name_op
[op
], &op_str
[op
]);
4664 Py_INCREF(Py_NotImplemented
);
4665 return Py_NotImplemented
;
4667 args
= PyTuple_Pack(1, other
);
4671 res
= PyObject_Call(func
, args
, NULL
);
4679 slot_tp_richcompare(PyObject
*self
, PyObject
*other
, int op
)
4683 if (self
->ob_type
->tp_richcompare
== slot_tp_richcompare
) {
4684 res
= half_richcompare(self
, other
, op
);
4685 if (res
!= Py_NotImplemented
)
4689 if (other
->ob_type
->tp_richcompare
== slot_tp_richcompare
) {
4690 res
= half_richcompare(other
, self
, _Py_SwappedOp
[op
]);
4691 if (res
!= Py_NotImplemented
) {
4696 Py_INCREF(Py_NotImplemented
);
4697 return Py_NotImplemented
;
4701 slot_tp_iter(PyObject
*self
)
4703 PyObject
*func
, *res
;
4704 static PyObject
*iter_str
, *getitem_str
;
4706 func
= lookup_method(self
, "__iter__", &iter_str
);
4709 args
= res
= PyTuple_New(0);
4711 res
= PyObject_Call(func
, args
, NULL
);
4718 func
= lookup_method(self
, "__getitem__", &getitem_str
);
4720 PyErr_SetString(PyExc_TypeError
,
4721 "iteration over non-sequence");
4725 return PySeqIter_New(self
);
4729 slot_tp_iternext(PyObject
*self
)
4731 static PyObject
*next_str
;
4732 return call_method(self
, "next", &next_str
, "()");
4736 slot_tp_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
4738 PyTypeObject
*tp
= self
->ob_type
;
4740 static PyObject
*get_str
= NULL
;
4742 if (get_str
== NULL
) {
4743 get_str
= PyString_InternFromString("__get__");
4744 if (get_str
== NULL
)
4747 get
= _PyType_Lookup(tp
, get_str
);
4749 /* Avoid further slowdowns */
4750 if (tp
->tp_descr_get
== slot_tp_descr_get
)
4751 tp
->tp_descr_get
= NULL
;
4759 return PyObject_CallFunction(get
, "OOO", self
, obj
, type
);
4763 slot_tp_descr_set(PyObject
*self
, PyObject
*target
, PyObject
*value
)
4766 static PyObject
*del_str
, *set_str
;
4769 res
= call_method(self
, "__delete__", &del_str
,
4772 res
= call_method(self
, "__set__", &set_str
,
4773 "(OO)", target
, value
);
4781 slot_tp_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4783 static PyObject
*init_str
;
4784 PyObject
*meth
= lookup_method(self
, "__init__", &init_str
);
4789 res
= PyObject_Call(meth
, args
, kwds
);
4793 if (res
!= Py_None
) {
4794 PyErr_SetString(PyExc_TypeError
,
4795 "__init__() should return None");
4804 slot_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
4806 static PyObject
*new_str
;
4808 PyObject
*newargs
, *x
;
4811 if (new_str
== NULL
) {
4812 new_str
= PyString_InternFromString("__new__");
4813 if (new_str
== NULL
)
4816 func
= PyObject_GetAttr((PyObject
*)type
, new_str
);
4819 assert(PyTuple_Check(args
));
4820 n
= PyTuple_GET_SIZE(args
);
4821 newargs
= PyTuple_New(n
+1);
4822 if (newargs
== NULL
)
4825 PyTuple_SET_ITEM(newargs
, 0, (PyObject
*)type
);
4826 for (i
= 0; i
< n
; i
++) {
4827 x
= PyTuple_GET_ITEM(args
, i
);
4829 PyTuple_SET_ITEM(newargs
, i
+1, x
);
4831 x
= PyObject_Call(func
, newargs
, kwds
);
4838 slot_tp_del(PyObject
*self
)
4840 static PyObject
*del_str
= NULL
;
4841 PyObject
*del
, *res
;
4842 PyObject
*error_type
, *error_value
, *error_traceback
;
4844 /* Temporarily resurrect the object. */
4845 assert(self
->ob_refcnt
== 0);
4846 self
->ob_refcnt
= 1;
4848 /* Save the current exception, if any. */
4849 PyErr_Fetch(&error_type
, &error_value
, &error_traceback
);
4851 /* Execute __del__ method, if any. */
4852 del
= lookup_maybe(self
, "__del__", &del_str
);
4854 res
= PyEval_CallObject(del
, NULL
);
4856 PyErr_WriteUnraisable(del
);
4862 /* Restore the saved exception. */
4863 PyErr_Restore(error_type
, error_value
, error_traceback
);
4865 /* Undo the temporary resurrection; can't use DECREF here, it would
4866 * cause a recursive call.
4868 assert(self
->ob_refcnt
> 0);
4869 if (--self
->ob_refcnt
== 0)
4870 return; /* this is the normal path out */
4872 /* __del__ resurrected it! Make it look like the original Py_DECREF
4876 int refcnt
= self
->ob_refcnt
;
4877 _Py_NewReference(self
);
4878 self
->ob_refcnt
= refcnt
;
4880 assert(!PyType_IS_GC(self
->ob_type
) ||
4881 _Py_AS_GC(self
)->gc
.gc_refs
!= _PyGC_REFS_UNTRACKED
);
4882 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4883 * we need to undo that. */
4885 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4886 * chain, so no more to do there.
4887 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4888 * _Py_NewReference bumped tp_allocs: both of those need to be
4892 --self
->ob_type
->tp_frees
;
4893 --self
->ob_type
->tp_allocs
;
4898 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4899 functions. The offsets here are relative to the 'PyHeapTypeObject'
4900 structure, which incorporates the additional structures used for numbers,
4901 sequences and mappings.
4902 Note that multiple names may map to the same slot (e.g. __eq__,
4903 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4904 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4905 terminated with an all-zero entry. (This table is further initialized and
4906 sorted in init_slotdefs() below.) */
4908 typedef struct wrapperbase slotdef
;
4921 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4922 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4924 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4925 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4926 PyDoc_STR(DOC), FLAGS}
4927 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4928 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4930 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4931 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4932 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4933 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4934 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4935 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4936 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4937 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4938 "x." NAME "() <==> " DOC)
4939 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4940 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4941 "x." NAME "(y) <==> x" DOC "y")
4942 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4943 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4944 "x." NAME "(y) <==> x" DOC "y")
4945 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4946 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4947 "x." NAME "(y) <==> y" DOC "x")
4948 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4949 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4950 "x." NAME "(y) <==> " DOC)
4951 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4952 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4953 "x." NAME "(y) <==> " DOC)
4955 static slotdef slotdefs
[] = {
4956 SQSLOT("__len__", sq_length
, slot_sq_length
, wrap_inquiry
,
4957 "x.__len__() <==> len(x)"),
4958 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
4959 The logic in abstract.c always falls back to nb_add/nb_multiply in
4960 this case. Defining both the nb_* and the sq_* slots to call the
4961 user-defined methods has unexpected side-effects, as shown by
4962 test_descr.notimplemented() */
4963 SQSLOT("__add__", sq_concat
, NULL
, wrap_binaryfunc
,
4964 "x.__add__(y) <==> x+y"),
4965 SQSLOT("__mul__", sq_repeat
, NULL
, wrap_intargfunc
,
4966 "x.__mul__(n) <==> x*n"),
4967 SQSLOT("__rmul__", sq_repeat
, NULL
, wrap_intargfunc
,
4968 "x.__rmul__(n) <==> n*x"),
4969 SQSLOT("__getitem__", sq_item
, slot_sq_item
, wrap_sq_item
,
4970 "x.__getitem__(y) <==> x[y]"),
4971 SQSLOT("__getslice__", sq_slice
, slot_sq_slice
, wrap_intintargfunc
,
4972 "x.__getslice__(i, j) <==> x[i:j]\n\
4974 Use of negative indices is not supported."),
4975 SQSLOT("__setitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_setitem
,
4976 "x.__setitem__(i, y) <==> x[i]=y"),
4977 SQSLOT("__delitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_delitem
,
4978 "x.__delitem__(y) <==> del x[y]"),
4979 SQSLOT("__setslice__", sq_ass_slice
, slot_sq_ass_slice
,
4980 wrap_intintobjargproc
,
4981 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4983 Use of negative indices is not supported."),
4984 SQSLOT("__delslice__", sq_ass_slice
, slot_sq_ass_slice
, wrap_delslice
,
4985 "x.__delslice__(i, j) <==> del x[i:j]\n\
4987 Use of negative indices is not supported."),
4988 SQSLOT("__contains__", sq_contains
, slot_sq_contains
, wrap_objobjproc
,
4989 "x.__contains__(y) <==> y in x"),
4990 SQSLOT("__iadd__", sq_inplace_concat
, NULL
,
4991 wrap_binaryfunc
, "x.__iadd__(y) <==> x+=y"),
4992 SQSLOT("__imul__", sq_inplace_repeat
, NULL
,
4993 wrap_intargfunc
, "x.__imul__(y) <==> x*=y"),
4995 MPSLOT("__len__", mp_length
, slot_mp_length
, wrap_inquiry
,
4996 "x.__len__() <==> len(x)"),
4997 MPSLOT("__getitem__", mp_subscript
, slot_mp_subscript
,
4999 "x.__getitem__(y) <==> x[y]"),
5000 MPSLOT("__setitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5002 "x.__setitem__(i, y) <==> x[i]=y"),
5003 MPSLOT("__delitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5005 "x.__delitem__(y) <==> del x[y]"),
5007 BINSLOT("__add__", nb_add
, slot_nb_add
,
5009 RBINSLOT("__radd__", nb_add
, slot_nb_add
,
5011 BINSLOT("__sub__", nb_subtract
, slot_nb_subtract
,
5013 RBINSLOT("__rsub__", nb_subtract
, slot_nb_subtract
,
5015 BINSLOT("__mul__", nb_multiply
, slot_nb_multiply
,
5017 RBINSLOT("__rmul__", nb_multiply
, slot_nb_multiply
,
5019 BINSLOT("__div__", nb_divide
, slot_nb_divide
,
5021 RBINSLOT("__rdiv__", nb_divide
, slot_nb_divide
,
5023 BINSLOT("__mod__", nb_remainder
, slot_nb_remainder
,
5025 RBINSLOT("__rmod__", nb_remainder
, slot_nb_remainder
,
5027 BINSLOTNOTINFIX("__divmod__", nb_divmod
, slot_nb_divmod
,
5029 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod
, slot_nb_divmod
,
5031 NBSLOT("__pow__", nb_power
, slot_nb_power
, wrap_ternaryfunc
,
5032 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5033 NBSLOT("__rpow__", nb_power
, slot_nb_power
, wrap_ternaryfunc_r
,
5034 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5035 UNSLOT("__neg__", nb_negative
, slot_nb_negative
, wrap_unaryfunc
, "-x"),
5036 UNSLOT("__pos__", nb_positive
, slot_nb_positive
, wrap_unaryfunc
, "+x"),
5037 UNSLOT("__abs__", nb_absolute
, slot_nb_absolute
, wrap_unaryfunc
,
5039 UNSLOT("__nonzero__", nb_nonzero
, slot_nb_nonzero
, wrap_inquirypred
,
5041 UNSLOT("__invert__", nb_invert
, slot_nb_invert
, wrap_unaryfunc
, "~x"),
5042 BINSLOT("__lshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5043 RBINSLOT("__rlshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5044 BINSLOT("__rshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5045 RBINSLOT("__rrshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5046 BINSLOT("__and__", nb_and
, slot_nb_and
, "&"),
5047 RBINSLOT("__rand__", nb_and
, slot_nb_and
, "&"),
5048 BINSLOT("__xor__", nb_xor
, slot_nb_xor
, "^"),
5049 RBINSLOT("__rxor__", nb_xor
, slot_nb_xor
, "^"),
5050 BINSLOT("__or__", nb_or
, slot_nb_or
, "|"),
5051 RBINSLOT("__ror__", nb_or
, slot_nb_or
, "|"),
5052 NBSLOT("__coerce__", nb_coerce
, slot_nb_coerce
, wrap_coercefunc
,
5053 "x.__coerce__(y) <==> coerce(x, y)"),
5054 UNSLOT("__int__", nb_int
, slot_nb_int
, wrap_unaryfunc
,
5056 UNSLOT("__long__", nb_long
, slot_nb_long
, wrap_unaryfunc
,
5058 UNSLOT("__float__", nb_float
, slot_nb_float
, wrap_unaryfunc
,
5060 UNSLOT("__oct__", nb_oct
, slot_nb_oct
, wrap_unaryfunc
,
5062 UNSLOT("__hex__", nb_hex
, slot_nb_hex
, wrap_unaryfunc
,
5064 IBSLOT("__iadd__", nb_inplace_add
, slot_nb_inplace_add
,
5065 wrap_binaryfunc
, "+"),
5066 IBSLOT("__isub__", nb_inplace_subtract
, slot_nb_inplace_subtract
,
5067 wrap_binaryfunc
, "-"),
5068 IBSLOT("__imul__", nb_inplace_multiply
, slot_nb_inplace_multiply
,
5069 wrap_binaryfunc
, "*"),
5070 IBSLOT("__idiv__", nb_inplace_divide
, slot_nb_inplace_divide
,
5071 wrap_binaryfunc
, "/"),
5072 IBSLOT("__imod__", nb_inplace_remainder
, slot_nb_inplace_remainder
,
5073 wrap_binaryfunc
, "%"),
5074 IBSLOT("__ipow__", nb_inplace_power
, slot_nb_inplace_power
,
5075 wrap_binaryfunc
, "**"),
5076 IBSLOT("__ilshift__", nb_inplace_lshift
, slot_nb_inplace_lshift
,
5077 wrap_binaryfunc
, "<<"),
5078 IBSLOT("__irshift__", nb_inplace_rshift
, slot_nb_inplace_rshift
,
5079 wrap_binaryfunc
, ">>"),
5080 IBSLOT("__iand__", nb_inplace_and
, slot_nb_inplace_and
,
5081 wrap_binaryfunc
, "&"),
5082 IBSLOT("__ixor__", nb_inplace_xor
, slot_nb_inplace_xor
,
5083 wrap_binaryfunc
, "^"),
5084 IBSLOT("__ior__", nb_inplace_or
, slot_nb_inplace_or
,
5085 wrap_binaryfunc
, "|"),
5086 BINSLOT("__floordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5087 RBINSLOT("__rfloordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5088 BINSLOT("__truediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5089 RBINSLOT("__rtruediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5090 IBSLOT("__ifloordiv__", nb_inplace_floor_divide
,
5091 slot_nb_inplace_floor_divide
, wrap_binaryfunc
, "//"),
5092 IBSLOT("__itruediv__", nb_inplace_true_divide
,
5093 slot_nb_inplace_true_divide
, wrap_binaryfunc
, "/"),
5095 TPSLOT("__str__", tp_str
, slot_tp_str
, wrap_unaryfunc
,
5096 "x.__str__() <==> str(x)"),
5097 TPSLOT("__str__", tp_print
, NULL
, NULL
, ""),
5098 TPSLOT("__repr__", tp_repr
, slot_tp_repr
, wrap_unaryfunc
,
5099 "x.__repr__() <==> repr(x)"),
5100 TPSLOT("__repr__", tp_print
, NULL
, NULL
, ""),
5101 TPSLOT("__cmp__", tp_compare
, _PyObject_SlotCompare
, wrap_cmpfunc
,
5102 "x.__cmp__(y) <==> cmp(x,y)"),
5103 TPSLOT("__hash__", tp_hash
, slot_tp_hash
, wrap_hashfunc
,
5104 "x.__hash__() <==> hash(x)"),
5105 FLSLOT("__call__", tp_call
, slot_tp_call
, (wrapperfunc
)wrap_call
,
5106 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS
),
5107 TPSLOT("__getattribute__", tp_getattro
, slot_tp_getattr_hook
,
5108 wrap_binaryfunc
, "x.__getattribute__('name') <==> x.name"),
5109 TPSLOT("__getattribute__", tp_getattr
, NULL
, NULL
, ""),
5110 TPSLOT("__getattr__", tp_getattro
, slot_tp_getattr_hook
, NULL
, ""),
5111 TPSLOT("__getattr__", tp_getattr
, NULL
, NULL
, ""),
5112 TPSLOT("__setattr__", tp_setattro
, slot_tp_setattro
, wrap_setattr
,
5113 "x.__setattr__('name', value) <==> x.name = value"),
5114 TPSLOT("__setattr__", tp_setattr
, NULL
, NULL
, ""),
5115 TPSLOT("__delattr__", tp_setattro
, slot_tp_setattro
, wrap_delattr
,
5116 "x.__delattr__('name') <==> del x.name"),
5117 TPSLOT("__delattr__", tp_setattr
, NULL
, NULL
, ""),
5118 TPSLOT("__lt__", tp_richcompare
, slot_tp_richcompare
, richcmp_lt
,
5119 "x.__lt__(y) <==> x<y"),
5120 TPSLOT("__le__", tp_richcompare
, slot_tp_richcompare
, richcmp_le
,
5121 "x.__le__(y) <==> x<=y"),
5122 TPSLOT("__eq__", tp_richcompare
, slot_tp_richcompare
, richcmp_eq
,
5123 "x.__eq__(y) <==> x==y"),
5124 TPSLOT("__ne__", tp_richcompare
, slot_tp_richcompare
, richcmp_ne
,
5125 "x.__ne__(y) <==> x!=y"),
5126 TPSLOT("__gt__", tp_richcompare
, slot_tp_richcompare
, richcmp_gt
,
5127 "x.__gt__(y) <==> x>y"),
5128 TPSLOT("__ge__", tp_richcompare
, slot_tp_richcompare
, richcmp_ge
,
5129 "x.__ge__(y) <==> x>=y"),
5130 TPSLOT("__iter__", tp_iter
, slot_tp_iter
, wrap_unaryfunc
,
5131 "x.__iter__() <==> iter(x)"),
5132 TPSLOT("next", tp_iternext
, slot_tp_iternext
, wrap_next
,
5133 "x.next() -> the next value, or raise StopIteration"),
5134 TPSLOT("__get__", tp_descr_get
, slot_tp_descr_get
, wrap_descr_get
,
5135 "descr.__get__(obj[, type]) -> value"),
5136 TPSLOT("__set__", tp_descr_set
, slot_tp_descr_set
, wrap_descr_set
,
5137 "descr.__set__(obj, value)"),
5138 TPSLOT("__delete__", tp_descr_set
, slot_tp_descr_set
,
5139 wrap_descr_delete
, "descr.__delete__(obj)"),
5140 FLSLOT("__init__", tp_init
, slot_tp_init
, (wrapperfunc
)wrap_init
,
5141 "x.__init__(...) initializes x; "
5142 "see x.__class__.__doc__ for signature",
5143 PyWrapperFlag_KEYWORDS
),
5144 TPSLOT("__new__", tp_new
, slot_tp_new
, NULL
, ""),
5145 TPSLOT("__del__", tp_del
, slot_tp_del
, NULL
, ""),
5149 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5150 pointer to the actual slot. This is not quite the same as simply adding
5151 the offset to the type pointer, since it takes care to indirect through the
5152 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5153 indirection pointer is NULL. */
5155 slotptr(PyTypeObject
*type
, int offset
)
5159 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5160 assert(offset
>= 0);
5161 assert(offset
< offsetof(PyHeapTypeObject
, as_buffer
));
5162 if (offset
>= offsetof(PyHeapTypeObject
, as_sequence
)) {
5163 ptr
= (void *)type
->tp_as_sequence
;
5164 offset
-= offsetof(PyHeapTypeObject
, as_sequence
);
5166 else if (offset
>= offsetof(PyHeapTypeObject
, as_mapping
)) {
5167 ptr
= (void *)type
->tp_as_mapping
;
5168 offset
-= offsetof(PyHeapTypeObject
, as_mapping
);
5170 else if (offset
>= offsetof(PyHeapTypeObject
, as_number
)) {
5171 ptr
= (void *)type
->tp_as_number
;
5172 offset
-= offsetof(PyHeapTypeObject
, as_number
);
5179 return (void **)ptr
;
5182 /* Length of array of slotdef pointers used to store slots with the
5183 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5184 the same __name__, for any __name__. Since that's a static property, it is
5185 appropriate to declare fixed-size arrays for this. */
5186 #define MAX_EQUIV 10
5188 /* Return a slot pointer for a given name, but ONLY if the attribute has
5189 exactly one slot function. The name must be an interned string. */
5191 resolve_slotdups(PyTypeObject
*type
, PyObject
*name
)
5193 /* XXX Maybe this could be optimized more -- but is it worth it? */
5195 /* pname and ptrs act as a little cache */
5196 static PyObject
*pname
;
5197 static slotdef
*ptrs
[MAX_EQUIV
];
5201 if (pname
!= name
) {
5202 /* Collect all slotdefs that match name into ptrs. */
5205 for (p
= slotdefs
; p
->name_strobj
; p
++) {
5206 if (p
->name_strobj
== name
)
5212 /* Look in all matching slots of the type; if exactly one of these has
5213 a filled-in slot, return its value. Otherwise return NULL. */
5215 for (pp
= ptrs
; *pp
; pp
++) {
5216 ptr
= slotptr(type
, (*pp
)->offset
);
5217 if (ptr
== NULL
|| *ptr
== NULL
)
5226 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5227 does some incredibly complex thinking and then sticks something into the
5228 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5229 interests, and then stores a generic wrapper or a specific function into
5230 the slot.) Return a pointer to the next slotdef with a different offset,
5231 because that's convenient for fixup_slot_dispatchers(). */
5233 update_one_slot(PyTypeObject
*type
, slotdef
*p
)
5236 PyWrapperDescrObject
*d
;
5237 void *generic
= NULL
, *specific
= NULL
;
5238 int use_generic
= 0;
5239 int offset
= p
->offset
;
5240 void **ptr
= slotptr(type
, offset
);
5245 } while (p
->offset
== offset
);
5249 descr
= _PyType_Lookup(type
, p
->name_strobj
);
5252 if (descr
->ob_type
== &PyWrapperDescr_Type
) {
5253 void **tptr
= resolve_slotdups(type
, p
->name_strobj
);
5254 if (tptr
== NULL
|| tptr
== ptr
)
5255 generic
= p
->function
;
5256 d
= (PyWrapperDescrObject
*)descr
;
5257 if (d
->d_base
->wrapper
== p
->wrapper
&&
5258 PyType_IsSubtype(type
, d
->d_type
))
5260 if (specific
== NULL
||
5261 specific
== d
->d_wrapped
)
5262 specific
= d
->d_wrapped
;
5267 else if (descr
->ob_type
== &PyCFunction_Type
&&
5268 PyCFunction_GET_FUNCTION(descr
) ==
5269 (PyCFunction
)tp_new_wrapper
&&
5270 strcmp(p
->name
, "__new__") == 0)
5272 /* The __new__ wrapper is not a wrapper descriptor,
5273 so must be special-cased differently.
5274 If we don't do this, creating an instance will
5275 always use slot_tp_new which will look up
5276 __new__ in the MRO which will call tp_new_wrapper
5277 which will look through the base classes looking
5278 for a static base and call its tp_new (usually
5279 PyType_GenericNew), after performing various
5280 sanity checks and constructing a new argument
5281 list. Cut all that nonsense short -- this speeds
5282 up instance creation tremendously. */
5283 specific
= (void *)type
->tp_new
;
5284 /* XXX I'm not 100% sure that there isn't a hole
5285 in this reasoning that requires additional
5286 sanity checks. I'll buy the first person to
5287 point out a bug in this reasoning a beer. */
5291 generic
= p
->function
;
5293 } while ((++p
)->offset
== offset
);
5294 if (specific
&& !use_generic
)
5301 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5302 This is a callback for update_subclasses(). */
5304 update_slots_callback(PyTypeObject
*type
, void *data
)
5306 slotdef
**pp
= (slotdef
**)data
;
5309 update_one_slot(type
, *pp
);
5313 /* Comparison function for qsort() to compare slotdefs by their offset, and
5314 for equal offset by their address (to force a stable sort). */
5316 slotdef_cmp(const void *aa
, const void *bb
)
5318 const slotdef
*a
= (const slotdef
*)aa
, *b
= (const slotdef
*)bb
;
5319 int c
= a
->offset
- b
->offset
;
5326 /* Initialize the slotdefs table by adding interned string objects for the
5327 names and sorting the entries. */
5332 static int initialized
= 0;
5336 for (p
= slotdefs
; p
->name
; p
++) {
5337 p
->name_strobj
= PyString_InternFromString(p
->name
);
5338 if (!p
->name_strobj
)
5339 Py_FatalError("Out of memory interning slotdef names");
5341 qsort((void *)slotdefs
, (size_t)(p
-slotdefs
), sizeof(slotdef
),
5346 /* Update the slots after assignment to a class (type) attribute. */
5348 update_slot(PyTypeObject
*type
, PyObject
*name
)
5350 slotdef
*ptrs
[MAX_EQUIV
];
5357 for (p
= slotdefs
; p
->name
; p
++) {
5358 /* XXX assume name is interned! */
5359 if (p
->name_strobj
== name
)
5363 for (pp
= ptrs
; *pp
; pp
++) {
5366 while (p
> slotdefs
&& (p
-1)->offset
== offset
)
5370 if (ptrs
[0] == NULL
)
5371 return 0; /* Not an attribute that affects any slots */
5372 return update_subclasses(type
, name
,
5373 update_slots_callback
, (void *)ptrs
);
5376 /* Store the proper functions in the slot dispatches at class (type)
5377 definition time, based upon which operations the class overrides in its
5380 fixup_slot_dispatchers(PyTypeObject
*type
)
5385 for (p
= slotdefs
; p
->name
; )
5386 p
= update_one_slot(type
, p
);
5390 update_all_slots(PyTypeObject
* type
)
5395 for (p
= slotdefs
; p
->name
; p
++) {
5396 /* update_slot returns int but can't actually fail */
5397 update_slot(type
, p
->name_strobj
);
5401 /* recurse_down_subclasses() and update_subclasses() are mutually
5402 recursive functions to call a callback for all subclasses,
5403 but refraining from recursing into subclasses that define 'name'. */
5406 update_subclasses(PyTypeObject
*type
, PyObject
*name
,
5407 update_callback callback
, void *data
)
5409 if (callback(type
, data
) < 0)
5411 return recurse_down_subclasses(type
, name
, callback
, data
);
5415 recurse_down_subclasses(PyTypeObject
*type
, PyObject
*name
,
5416 update_callback callback
, void *data
)
5418 PyTypeObject
*subclass
;
5419 PyObject
*ref
, *subclasses
, *dict
;
5422 subclasses
= type
->tp_subclasses
;
5423 if (subclasses
== NULL
)
5425 assert(PyList_Check(subclasses
));
5426 n
= PyList_GET_SIZE(subclasses
);
5427 for (i
= 0; i
< n
; i
++) {
5428 ref
= PyList_GET_ITEM(subclasses
, i
);
5429 assert(PyWeakref_CheckRef(ref
));
5430 subclass
= (PyTypeObject
*)PyWeakref_GET_OBJECT(ref
);
5431 assert(subclass
!= NULL
);
5432 if ((PyObject
*)subclass
== Py_None
)
5434 assert(PyType_Check(subclass
));
5435 /* Avoid recursing down into unaffected classes */
5436 dict
= subclass
->tp_dict
;
5437 if (dict
!= NULL
&& PyDict_Check(dict
) &&
5438 PyDict_GetItem(dict
, name
) != NULL
)
5440 if (update_subclasses(subclass
, name
, callback
, data
) < 0)
5446 /* This function is called by PyType_Ready() to populate the type's
5447 dictionary with method descriptors for function slots. For each
5448 function slot (like tp_repr) that's defined in the type, one or more
5449 corresponding descriptors are added in the type's tp_dict dictionary
5450 under the appropriate name (like __repr__). Some function slots
5451 cause more than one descriptor to be added (for example, the nb_add
5452 slot adds both __add__ and __radd__ descriptors) and some function
5453 slots compete for the same descriptor (for example both sq_item and
5454 mp_subscript generate a __getitem__ descriptor).
5456 In the latter case, the first slotdef entry encoutered wins. Since
5457 slotdef entries are sorted by the offset of the slot in the
5458 PyHeapTypeObject, this gives us some control over disambiguating
5459 between competing slots: the members of PyHeapTypeObject are listed
5460 from most general to least general, so the most general slot is
5461 preferred. In particular, because as_mapping comes before as_sequence,
5462 for a type that defines both mp_subscript and sq_item, mp_subscript
5465 This only adds new descriptors and doesn't overwrite entries in
5466 tp_dict that were previously defined. The descriptors contain a
5467 reference to the C function they must call, so that it's safe if they
5468 are copied into a subtype's __dict__ and the subtype has a different
5469 C function in its slot -- calling the method defined by the
5470 descriptor will call the C function that was used to create it,
5471 rather than the C function present in the slot when it is called.
5472 (This is important because a subtype may have a C function in the
5473 slot that calls the method from the dictionary, and we want to avoid
5474 infinite recursion here.) */
5477 add_operators(PyTypeObject
*type
)
5479 PyObject
*dict
= type
->tp_dict
;
5485 for (p
= slotdefs
; p
->name
; p
++) {
5486 if (p
->wrapper
== NULL
)
5488 ptr
= slotptr(type
, p
->offset
);
5491 if (PyDict_GetItem(dict
, p
->name_strobj
))
5493 descr
= PyDescr_NewWrapper(type
, p
, *ptr
);
5496 if (PyDict_SetItem(dict
, p
->name_strobj
, descr
) < 0)
5500 if (type
->tp_new
!= NULL
) {
5501 if (add_tp_new_wrapper(type
) < 0)
5508 /* Cooperative 'super' */
5514 PyTypeObject
*obj_type
;
5517 static PyMemberDef super_members
[] = {
5518 {"__thisclass__", T_OBJECT
, offsetof(superobject
, type
), READONLY
,
5519 "the class invoking super()"},
5520 {"__self__", T_OBJECT
, offsetof(superobject
, obj
), READONLY
,
5521 "the instance invoking super(); may be None"},
5522 {"__self_class__", T_OBJECT
, offsetof(superobject
, obj_type
), READONLY
,
5523 "the type of the instance invoking super(); may be None"},
5528 super_dealloc(PyObject
*self
)
5530 superobject
*su
= (superobject
*)self
;
5532 _PyObject_GC_UNTRACK(self
);
5533 Py_XDECREF(su
->obj
);
5534 Py_XDECREF(su
->type
);
5535 Py_XDECREF(su
->obj_type
);
5536 self
->ob_type
->tp_free(self
);
5540 super_repr(PyObject
*self
)
5542 superobject
*su
= (superobject
*)self
;
5545 return PyString_FromFormat(
5546 "<super: <class '%s'>, <%s object>>",
5547 su
->type
? su
->type
->tp_name
: "NULL",
5548 su
->obj_type
->tp_name
);
5550 return PyString_FromFormat(
5551 "<super: <class '%s'>, NULL>",
5552 su
->type
? su
->type
->tp_name
: "NULL");
5556 super_getattro(PyObject
*self
, PyObject
*name
)
5558 superobject
*su
= (superobject
*)self
;
5559 int skip
= su
->obj_type
== NULL
;
5562 /* We want __class__ to return the class of the super object
5563 (i.e. super, or a subclass), not the class of su->obj. */
5564 skip
= (PyString_Check(name
) &&
5565 PyString_GET_SIZE(name
) == 9 &&
5566 strcmp(PyString_AS_STRING(name
), "__class__") == 0);
5570 PyObject
*mro
, *res
, *tmp
, *dict
;
5571 PyTypeObject
*starttype
;
5575 starttype
= su
->obj_type
;
5576 mro
= starttype
->tp_mro
;
5581 assert(PyTuple_Check(mro
));
5582 n
= PyTuple_GET_SIZE(mro
);
5584 for (i
= 0; i
< n
; i
++) {
5585 if ((PyObject
*)(su
->type
) == PyTuple_GET_ITEM(mro
, i
))
5590 for (; i
< n
; i
++) {
5591 tmp
= PyTuple_GET_ITEM(mro
, i
);
5592 if (PyType_Check(tmp
))
5593 dict
= ((PyTypeObject
*)tmp
)->tp_dict
;
5594 else if (PyClass_Check(tmp
))
5595 dict
= ((PyClassObject
*)tmp
)->cl_dict
;
5598 res
= PyDict_GetItem(dict
, name
);
5601 f
= res
->ob_type
->tp_descr_get
;
5604 /* Only pass 'obj' param if
5605 this is instance-mode super
5608 (su
->obj
== (PyObject
*)
5612 (PyObject
*)starttype
);
5620 return PyObject_GenericGetAttr(self
, name
);
5623 static PyTypeObject
*
5624 supercheck(PyTypeObject
*type
, PyObject
*obj
)
5626 /* Check that a super() call makes sense. Return a type object.
5628 obj can be a new-style class, or an instance of one:
5630 - If it is a class, it must be a subclass of 'type'. This case is
5631 used for class methods; the return value is obj.
5633 - If it is an instance, it must be an instance of 'type'. This is
5634 the normal case; the return value is obj.__class__.
5636 But... when obj is an instance, we want to allow for the case where
5637 obj->ob_type is not a subclass of type, but obj.__class__ is!
5638 This will allow using super() with a proxy for obj.
5641 /* Check for first bullet above (special case) */
5642 if (PyType_Check(obj
) && PyType_IsSubtype((PyTypeObject
*)obj
, type
)) {
5644 return (PyTypeObject
*)obj
;
5648 if (PyType_IsSubtype(obj
->ob_type
, type
)) {
5649 Py_INCREF(obj
->ob_type
);
5650 return obj
->ob_type
;
5653 /* Try the slow way */
5654 static PyObject
*class_str
= NULL
;
5655 PyObject
*class_attr
;
5657 if (class_str
== NULL
) {
5658 class_str
= PyString_FromString("__class__");
5659 if (class_str
== NULL
)
5663 class_attr
= PyObject_GetAttr(obj
, class_str
);
5665 if (class_attr
!= NULL
&&
5666 PyType_Check(class_attr
) &&
5667 (PyTypeObject
*)class_attr
!= obj
->ob_type
)
5669 int ok
= PyType_IsSubtype(
5670 (PyTypeObject
*)class_attr
, type
);
5672 return (PyTypeObject
*)class_attr
;
5675 if (class_attr
== NULL
)
5678 Py_DECREF(class_attr
);
5681 PyErr_SetString(PyExc_TypeError
,
5682 "super(type, obj): "
5683 "obj must be an instance or subtype of type");
5688 super_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
5690 superobject
*su
= (superobject
*)self
;
5693 if (obj
== NULL
|| obj
== Py_None
|| su
->obj
!= NULL
) {
5694 /* Not binding to an object, or already bound */
5698 if (su
->ob_type
!= &PySuper_Type
)
5699 /* If su is an instance of a (strict) subclass of super,
5701 return PyObject_CallFunction((PyObject
*)su
->ob_type
,
5702 "OO", su
->type
, obj
);
5704 /* Inline the common case */
5705 PyTypeObject
*obj_type
= supercheck(su
->type
, obj
);
5706 if (obj_type
== NULL
)
5708 new = (superobject
*)PySuper_Type
.tp_new(&PySuper_Type
,
5712 Py_INCREF(su
->type
);
5714 new->type
= su
->type
;
5716 new->obj_type
= obj_type
;
5717 return (PyObject
*)new;
5722 super_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5724 superobject
*su
= (superobject
*)self
;
5726 PyObject
*obj
= NULL
;
5727 PyTypeObject
*obj_type
= NULL
;
5729 if (!PyArg_ParseTuple(args
, "O!|O:super", &PyType_Type
, &type
, &obj
))
5734 obj_type
= supercheck(type
, obj
);
5735 if (obj_type
== NULL
)
5742 su
->obj_type
= obj_type
;
5746 PyDoc_STRVAR(super_doc
,
5747 "super(type) -> unbound super object\n"
5748 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
5749 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
5750 "Typical use to call a cooperative superclass method:\n"
5752 " def meth(self, arg):\n"
5753 " super(C, self).meth(arg)");
5756 super_traverse(PyObject
*self
, visitproc visit
, void *arg
)
5758 superobject
*su
= (superobject
*)self
;
5761 #define VISIT(SLOT) \
5763 err = visit((PyObject *)(SLOT), arg); \
5770 VISIT(su
->obj_type
);
5777 PyTypeObject PySuper_Type
= {
5778 PyObject_HEAD_INIT(&PyType_Type
)
5780 "super", /* tp_name */
5781 sizeof(superobject
), /* tp_basicsize */
5782 0, /* tp_itemsize */
5784 super_dealloc
, /* tp_dealloc */
5789 super_repr
, /* tp_repr */
5790 0, /* tp_as_number */
5791 0, /* tp_as_sequence */
5792 0, /* tp_as_mapping */
5796 super_getattro
, /* tp_getattro */
5797 0, /* tp_setattro */
5798 0, /* tp_as_buffer */
5799 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
5800 Py_TPFLAGS_BASETYPE
, /* tp_flags */
5801 super_doc
, /* tp_doc */
5802 super_traverse
, /* tp_traverse */
5804 0, /* tp_richcompare */
5805 0, /* tp_weaklistoffset */
5807 0, /* tp_iternext */
5809 super_members
, /* tp_members */
5813 super_descr_get
, /* tp_descr_get */
5814 0, /* tp_descr_set */
5815 0, /* tp_dictoffset */
5816 super_init
, /* tp_init */
5817 PyType_GenericAlloc
, /* tp_alloc */
5818 PyType_GenericNew
, /* tp_new */
5819 PyObject_GC_Del
, /* tp_free */