1 /* Type object implementation */
4 #include "frameobject.h"
5 #include "structmember.h"
10 /* Support type attribute cache */
12 /* The cache can keep references to the names alive for longer than
13 they normally would. This is why the maximum size is limited to
14 MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
15 strings are used as attribute names. */
16 #define MCACHE_MAX_ATTR_SIZE 100
17 #define MCACHE_SIZE_EXP 10
18 #define MCACHE_HASH(version, name_hash) \
19 (((unsigned int)(version) * (unsigned int)(name_hash)) \
20 >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
21 #define MCACHE_HASH_METHOD(type, name) \
22 MCACHE_HASH((type)->tp_version_tag, \
23 ((PyUnicodeObject *)(name))->hash)
24 #define MCACHE_CACHEABLE_NAME(name) \
25 PyUnicode_CheckExact(name) && \
26 PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
28 struct method_cache_entry
{
30 PyObject
*name
; /* reference to exactly a str or None */
31 PyObject
*value
; /* borrowed */
34 static struct method_cache_entry method_cache
[1 << MCACHE_SIZE_EXP
];
35 static unsigned int next_version_tag
= 0;
38 PyType_ClearCache(void)
41 unsigned int cur_version_tag
= next_version_tag
- 1;
43 for (i
= 0; i
< (1 << MCACHE_SIZE_EXP
); i
++) {
44 method_cache
[i
].version
= 0;
45 Py_CLEAR(method_cache
[i
].name
);
46 method_cache
[i
].value
= NULL
;
49 /* mark all version tags as invalid */
50 PyType_Modified(&PyBaseObject_Type
);
51 return cur_version_tag
;
55 PyType_Modified(PyTypeObject
*type
)
57 /* Invalidate any cached data for the specified type and all
58 subclasses. This function is called after the base
59 classes, mro, or attributes of the type are altered.
63 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
64 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
65 objects coming from non-recompiled extension modules)
67 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
68 it must first be set on all super types.
70 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
71 type (so it must first clear it on all subclasses). The
72 tp_version_tag value is meaningless unless this flag is set.
73 We don't assign new version tags eagerly, but only as
79 if (!PyType_HasFeature(type
, Py_TPFLAGS_VALID_VERSION_TAG
))
82 raw
= type
->tp_subclasses
;
84 n
= PyList_GET_SIZE(raw
);
85 for (i
= 0; i
< n
; i
++) {
86 ref
= PyList_GET_ITEM(raw
, i
);
87 ref
= PyWeakref_GET_OBJECT(ref
);
89 PyType_Modified((PyTypeObject
*)ref
);
93 type
->tp_flags
&= ~Py_TPFLAGS_VALID_VERSION_TAG
;
97 type_mro_modified(PyTypeObject
*type
, PyObject
*bases
) {
99 Check that all base classes or elements of the mro of type are
100 able to be cached. This function is called after the base
101 classes or mro of the type are altered.
103 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
104 inherits from an old-style class, either directly or if it
105 appears in the MRO of a new-style class. No support either for
106 custom MROs that include types that are not officially super
109 Called from mro_internal, which will subsequently be called on
110 each subclass when their mro is recursively updated.
115 if (!PyType_HasFeature(type
, Py_TPFLAGS_HAVE_VERSION_TAG
))
118 n
= PyTuple_GET_SIZE(bases
);
119 for (i
= 0; i
< n
; i
++) {
120 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
123 if (!PyType_Check(b
) ) {
128 cls
= (PyTypeObject
*)b
;
130 if (!PyType_HasFeature(cls
, Py_TPFLAGS_HAVE_VERSION_TAG
) ||
131 !PyType_IsSubtype(type
, cls
)) {
138 type
->tp_flags
&= ~(Py_TPFLAGS_HAVE_VERSION_TAG
|
139 Py_TPFLAGS_VALID_VERSION_TAG
);
143 assign_version_tag(PyTypeObject
*type
)
145 /* Ensure that the tp_version_tag is valid and set
146 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
147 must first be done on all super classes. Return 0 if this
148 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
153 if (PyType_HasFeature(type
, Py_TPFLAGS_VALID_VERSION_TAG
))
155 if (!PyType_HasFeature(type
, Py_TPFLAGS_HAVE_VERSION_TAG
))
157 if (!PyType_HasFeature(type
, Py_TPFLAGS_READY
))
160 type
->tp_version_tag
= next_version_tag
++;
161 /* for stress-testing: next_version_tag &= 0xFF; */
163 if (type
->tp_version_tag
== 0) {
164 /* wrap-around or just starting Python - clear the whole
165 cache by filling names with references to Py_None.
166 Values are also set to NULL for added protection, as they
167 are borrowed reference */
168 for (i
= 0; i
< (1 << MCACHE_SIZE_EXP
); i
++) {
169 method_cache
[i
].value
= NULL
;
170 Py_XDECREF(method_cache
[i
].name
);
171 method_cache
[i
].name
= Py_None
;
174 /* mark all version tags as invalid */
175 PyType_Modified(&PyBaseObject_Type
);
178 bases
= type
->tp_bases
;
179 n
= PyTuple_GET_SIZE(bases
);
180 for (i
= 0; i
< n
; i
++) {
181 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
182 assert(PyType_Check(b
));
183 if (!assign_version_tag((PyTypeObject
*)b
))
186 type
->tp_flags
|= Py_TPFLAGS_VALID_VERSION_TAG
;
191 static PyMemberDef type_members
[] = {
192 {"__basicsize__", T_INT
, offsetof(PyTypeObject
,tp_basicsize
),READONLY
},
193 {"__itemsize__", T_INT
, offsetof(PyTypeObject
, tp_itemsize
), READONLY
},
194 {"__flags__", T_LONG
, offsetof(PyTypeObject
, tp_flags
), READONLY
},
195 {"__weakrefoffset__", T_LONG
,
196 offsetof(PyTypeObject
, tp_weaklistoffset
), READONLY
},
197 {"__base__", T_OBJECT
, offsetof(PyTypeObject
, tp_base
), READONLY
},
198 {"__dictoffset__", T_LONG
,
199 offsetof(PyTypeObject
, tp_dictoffset
), READONLY
},
200 {"__mro__", T_OBJECT
, offsetof(PyTypeObject
, tp_mro
), READONLY
},
205 type_name(PyTypeObject
*type
, void *context
)
209 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
210 PyHeapTypeObject
* et
= (PyHeapTypeObject
*)type
;
212 Py_INCREF(et
->ht_name
);
216 s
= strrchr(type
->tp_name
, '.');
221 return PyUnicode_FromString(s
);
226 type_set_name(PyTypeObject
*type
, PyObject
*value
, void *context
)
228 PyHeapTypeObject
* et
;
232 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
233 PyErr_Format(PyExc_TypeError
,
234 "can't set %s.__name__", type
->tp_name
);
238 PyErr_Format(PyExc_TypeError
,
239 "can't delete %s.__name__", type
->tp_name
);
242 if (!PyUnicode_Check(value
)) {
243 PyErr_Format(PyExc_TypeError
,
244 "can only assign string to %s.__name__, not '%s'",
245 type
->tp_name
, Py_TYPE(value
)->tp_name
);
249 /* Check absence of null characters */
250 tmp
= PyUnicode_FromStringAndSize("\0", 1);
253 if (PyUnicode_Contains(value
, tmp
) != 0) {
255 PyErr_Format(PyExc_ValueError
,
256 "__name__ must not contain null bytes");
261 tp_name
= _PyUnicode_AsString(value
);
265 et
= (PyHeapTypeObject
*)type
;
269 Py_DECREF(et
->ht_name
);
272 type
->tp_name
= tp_name
;
278 type_module(PyTypeObject
*type
, void *context
)
283 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
284 mod
= PyDict_GetItemString(type
->tp_dict
, "__module__");
286 PyErr_Format(PyExc_AttributeError
, "__module__");
293 s
= strrchr(type
->tp_name
, '.');
295 return PyUnicode_FromStringAndSize(
296 type
->tp_name
, (Py_ssize_t
)(s
- type
->tp_name
));
297 return PyUnicode_FromString("builtins");
302 type_set_module(PyTypeObject
*type
, PyObject
*value
, void *context
)
304 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
305 PyErr_Format(PyExc_TypeError
,
306 "can't set %s.__module__", type
->tp_name
);
310 PyErr_Format(PyExc_TypeError
,
311 "can't delete %s.__module__", type
->tp_name
);
315 PyType_Modified(type
);
317 return PyDict_SetItemString(type
->tp_dict
, "__module__", value
);
321 type_abstractmethods(PyTypeObject
*type
, void *context
)
323 PyObject
*mod
= PyDict_GetItemString(type
->tp_dict
,
324 "__abstractmethods__");
326 PyErr_Format(PyExc_AttributeError
, "__abstractmethods__");
334 type_set_abstractmethods(PyTypeObject
*type
, PyObject
*value
, void *context
)
336 /* __abstractmethods__ should only be set once on a type, in
337 abc.ABCMeta.__new__, so this function doesn't do anything
338 special to update subclasses.
340 int res
= PyDict_SetItemString(type
->tp_dict
,
341 "__abstractmethods__", value
);
343 PyType_Modified(type
);
344 if (value
&& PyObject_IsTrue(value
)) {
345 type
->tp_flags
|= Py_TPFLAGS_IS_ABSTRACT
;
348 type
->tp_flags
&= ~Py_TPFLAGS_IS_ABSTRACT
;
355 type_get_bases(PyTypeObject
*type
, void *context
)
357 Py_INCREF(type
->tp_bases
);
358 return type
->tp_bases
;
361 static PyTypeObject
*best_base(PyObject
*);
362 static int mro_internal(PyTypeObject
*);
363 static int compatible_for_assignment(PyTypeObject
*, PyTypeObject
*, char *);
364 static int add_subclass(PyTypeObject
*, PyTypeObject
*);
365 static void remove_subclass(PyTypeObject
*, PyTypeObject
*);
366 static void update_all_slots(PyTypeObject
*);
368 typedef int (*update_callback
)(PyTypeObject
*, void *);
369 static int update_subclasses(PyTypeObject
*type
, PyObject
*name
,
370 update_callback callback
, void *data
);
371 static int recurse_down_subclasses(PyTypeObject
*type
, PyObject
*name
,
372 update_callback callback
, void *data
);
375 mro_subclasses(PyTypeObject
*type
, PyObject
* temp
)
377 PyTypeObject
*subclass
;
378 PyObject
*ref
, *subclasses
, *old_mro
;
381 subclasses
= type
->tp_subclasses
;
382 if (subclasses
== NULL
)
384 assert(PyList_Check(subclasses
));
385 n
= PyList_GET_SIZE(subclasses
);
386 for (i
= 0; i
< n
; i
++) {
387 ref
= PyList_GET_ITEM(subclasses
, i
);
388 assert(PyWeakref_CheckRef(ref
));
389 subclass
= (PyTypeObject
*)PyWeakref_GET_OBJECT(ref
);
390 assert(subclass
!= NULL
);
391 if ((PyObject
*)subclass
== Py_None
)
393 assert(PyType_Check(subclass
));
394 old_mro
= subclass
->tp_mro
;
395 if (mro_internal(subclass
) < 0) {
396 subclass
->tp_mro
= old_mro
;
401 tuple
= PyTuple_Pack(2, subclass
, old_mro
);
405 if (PyList_Append(temp
, tuple
) < 0)
409 if (mro_subclasses(subclass
, temp
) < 0)
416 type_set_bases(PyTypeObject
*type
, PyObject
*value
, void *context
)
421 PyTypeObject
*new_base
, *old_base
;
422 PyObject
*old_bases
, *old_mro
;
424 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
425 PyErr_Format(PyExc_TypeError
,
426 "can't set %s.__bases__", type
->tp_name
);
430 PyErr_Format(PyExc_TypeError
,
431 "can't delete %s.__bases__", type
->tp_name
);
434 if (!PyTuple_Check(value
)) {
435 PyErr_Format(PyExc_TypeError
,
436 "can only assign tuple to %s.__bases__, not %s",
437 type
->tp_name
, Py_TYPE(value
)->tp_name
);
440 if (PyTuple_GET_SIZE(value
) == 0) {
441 PyErr_Format(PyExc_TypeError
,
442 "can only assign non-empty tuple to %s.__bases__, not ()",
446 for (i
= 0; i
< PyTuple_GET_SIZE(value
); i
++) {
447 ob
= PyTuple_GET_ITEM(value
, i
);
448 if (!PyType_Check(ob
)) {
451 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
452 type
->tp_name
, Py_TYPE(ob
)->tp_name
);
455 if (PyType_Check(ob
)) {
456 if (PyType_IsSubtype((PyTypeObject
*)ob
, type
)) {
457 PyErr_SetString(PyExc_TypeError
,
458 "a __bases__ item causes an inheritance cycle");
464 new_base
= best_base(value
);
470 if (!compatible_for_assignment(type
->tp_base
, new_base
, "__bases__"))
476 old_bases
= type
->tp_bases
;
477 old_base
= type
->tp_base
;
478 old_mro
= type
->tp_mro
;
480 type
->tp_bases
= value
;
481 type
->tp_base
= new_base
;
483 if (mro_internal(type
) < 0) {
487 temp
= PyList_New(0);
491 r
= mro_subclasses(type
, temp
);
494 for (i
= 0; i
< PyList_Size(temp
); i
++) {
497 PyArg_UnpackTuple(PyList_GET_ITEM(temp
, i
),
498 "", 2, 2, &cls
, &mro
);
510 /* any base that was in __bases__ but now isn't, we
511 need to remove |type| from its tp_subclasses.
512 conversely, any class now in __bases__ that wasn't
513 needs to have |type| added to its subclasses. */
515 /* for now, sod that: just remove from all old_bases,
516 add to all new_bases */
518 for (i
= PyTuple_GET_SIZE(old_bases
) - 1; i
>= 0; i
--) {
519 ob
= PyTuple_GET_ITEM(old_bases
, i
);
520 if (PyType_Check(ob
)) {
522 (PyTypeObject
*)ob
, type
);
526 for (i
= PyTuple_GET_SIZE(value
) - 1; i
>= 0; i
--) {
527 ob
= PyTuple_GET_ITEM(value
, i
);
528 if (PyType_Check(ob
)) {
529 if (add_subclass((PyTypeObject
*)ob
, type
) < 0)
534 update_all_slots(type
);
536 Py_DECREF(old_bases
);
543 Py_DECREF(type
->tp_bases
);
544 Py_DECREF(type
->tp_base
);
545 if (type
->tp_mro
!= old_mro
) {
546 Py_DECREF(type
->tp_mro
);
549 type
->tp_bases
= old_bases
;
550 type
->tp_base
= old_base
;
551 type
->tp_mro
= old_mro
;
557 type_dict(PyTypeObject
*type
, void *context
)
559 if (type
->tp_dict
== NULL
) {
563 return PyDictProxy_New(type
->tp_dict
);
567 type_get_doc(PyTypeObject
*type
, void *context
)
570 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) && type
->tp_doc
!= NULL
)
571 return PyUnicode_FromString(type
->tp_doc
);
572 result
= PyDict_GetItemString(type
->tp_dict
, "__doc__");
573 if (result
== NULL
) {
577 else if (Py_TYPE(result
)->tp_descr_get
) {
578 result
= Py_TYPE(result
)->tp_descr_get(result
, NULL
,
588 type___instancecheck__(PyObject
*type
, PyObject
*inst
)
590 switch (_PyObject_RealIsInstance(inst
, type
)) {
602 type___subclasscheck__(PyObject
*type
, PyObject
*inst
)
604 switch (_PyObject_RealIsSubclass(inst
, type
)) {
615 static PyGetSetDef type_getsets
[] = {
616 {"__name__", (getter
)type_name
, (setter
)type_set_name
, NULL
},
617 {"__bases__", (getter
)type_get_bases
, (setter
)type_set_bases
, NULL
},
618 {"__module__", (getter
)type_module
, (setter
)type_set_module
, NULL
},
619 {"__abstractmethods__", (getter
)type_abstractmethods
,
620 (setter
)type_set_abstractmethods
, NULL
},
621 {"__dict__", (getter
)type_dict
, NULL
, NULL
},
622 {"__doc__", (getter
)type_get_doc
, NULL
, NULL
},
627 type_repr(PyTypeObject
*type
)
629 PyObject
*mod
, *name
, *rtn
;
631 mod
= type_module(type
, NULL
);
634 else if (!PyUnicode_Check(mod
)) {
638 name
= type_name(type
, NULL
);
642 if (mod
!= NULL
&& PyUnicode_CompareWithASCIIString(mod
, "builtins"))
643 rtn
= PyUnicode_FromFormat("<class '%U.%U'>", mod
, name
);
645 rtn
= PyUnicode_FromFormat("<class '%s'>", type
->tp_name
);
653 type_call(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
657 if (type
->tp_new
== NULL
) {
658 PyErr_Format(PyExc_TypeError
,
659 "cannot create '%.100s' instances",
664 obj
= type
->tp_new(type
, args
, kwds
);
666 /* Ugly exception: when the call was type(something),
667 don't call tp_init on the result. */
668 if (type
== &PyType_Type
&&
669 PyTuple_Check(args
) && PyTuple_GET_SIZE(args
) == 1 &&
671 (PyDict_Check(kwds
) && PyDict_Size(kwds
) == 0)))
673 /* If the returned object is not an instance of type,
674 it won't be initialized. */
675 if (!PyType_IsSubtype(Py_TYPE(obj
), type
))
678 if (type
->tp_init
!= NULL
&&
679 type
->tp_init(obj
, args
, kwds
) < 0) {
688 PyType_GenericAlloc(PyTypeObject
*type
, Py_ssize_t nitems
)
691 const size_t size
= _PyObject_VAR_SIZE(type
, nitems
+1);
692 /* note that we need to add one, for the sentinel */
694 if (PyType_IS_GC(type
))
695 obj
= _PyObject_GC_Malloc(size
);
697 obj
= (PyObject
*)PyObject_MALLOC(size
);
700 return PyErr_NoMemory();
702 memset(obj
, '\0', size
);
704 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
707 if (type
->tp_itemsize
== 0)
708 PyObject_INIT(obj
, type
);
710 (void) PyObject_INIT_VAR((PyVarObject
*)obj
, type
, nitems
);
712 if (PyType_IS_GC(type
))
713 _PyObject_GC_TRACK(obj
);
718 PyType_GenericNew(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
720 return type
->tp_alloc(type
, 0);
723 /* Helpers for subtyping */
726 traverse_slots(PyTypeObject
*type
, PyObject
*self
, visitproc visit
, void *arg
)
732 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
733 for (i
= 0; i
< n
; i
++, mp
++) {
734 if (mp
->type
== T_OBJECT_EX
) {
735 char *addr
= (char *)self
+ mp
->offset
;
736 PyObject
*obj
= *(PyObject
**)addr
;
738 int err
= visit(obj
, arg
);
748 subtype_traverse(PyObject
*self
, visitproc visit
, void *arg
)
750 PyTypeObject
*type
, *base
;
751 traverseproc basetraverse
;
753 /* Find the nearest base with a different tp_traverse,
754 and traverse slots while we're at it */
755 type
= Py_TYPE(self
);
757 while ((basetraverse
= base
->tp_traverse
) == subtype_traverse
) {
759 int err
= traverse_slots(base
, self
, visit
, arg
);
763 base
= base
->tp_base
;
767 if (type
->tp_dictoffset
!= base
->tp_dictoffset
) {
768 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
769 if (dictptr
&& *dictptr
)
773 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
774 /* For a heaptype, the instances count as references
775 to the type. Traverse the type so the collector
776 can find cycles involving this link. */
780 return basetraverse(self
, visit
, arg
);
785 clear_slots(PyTypeObject
*type
, PyObject
*self
)
791 mp
= PyHeapType_GET_MEMBERS((PyHeapTypeObject
*)type
);
792 for (i
= 0; i
< n
; i
++, mp
++) {
793 if (mp
->type
== T_OBJECT_EX
&& !(mp
->flags
& READONLY
)) {
794 char *addr
= (char *)self
+ mp
->offset
;
795 PyObject
*obj
= *(PyObject
**)addr
;
797 *(PyObject
**)addr
= NULL
;
805 subtype_clear(PyObject
*self
)
807 PyTypeObject
*type
, *base
;
810 /* Find the nearest base with a different tp_clear
811 and clear slots while we're at it */
812 type
= Py_TYPE(self
);
814 while ((baseclear
= base
->tp_clear
) == subtype_clear
) {
816 clear_slots(base
, self
);
817 base
= base
->tp_base
;
821 /* There's no need to clear the instance dict (if any);
822 the collector will call its tp_clear handler. */
825 return baseclear(self
);
830 subtype_dealloc(PyObject
*self
)
832 PyTypeObject
*type
, *base
;
833 destructor basedealloc
;
835 /* Extract the type; we expect it to be a heap type */
836 type
= Py_TYPE(self
);
837 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
839 /* Test whether the type has GC exactly once */
841 if (!PyType_IS_GC(type
)) {
842 /* It's really rare to find a dynamic type that doesn't have
843 GC; it can only happen when deriving from 'object' and not
844 adding any slots or instance variables. This allows
845 certain simplifications: there's no need to call
846 clear_slots(), or DECREF the dict, or clear weakrefs. */
848 /* Maybe call finalizer; exit early if resurrected */
851 if (self
->ob_refcnt
> 0)
855 /* Find the nearest base with a different tp_dealloc */
857 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
858 assert(Py_SIZE(base
) == 0);
859 base
= base
->tp_base
;
863 /* Extract the type again; tp_del may have changed it */
864 type
= Py_TYPE(self
);
866 /* Call the base tp_dealloc() */
870 /* Can't reference self beyond this point */
877 /* We get here only if the type has GC */
879 /* UnTrack and re-Track around the trashcan macro, alas */
880 /* See explanation at end of function for full disclosure */
881 PyObject_GC_UnTrack(self
);
882 ++_PyTrash_delete_nesting
;
883 Py_TRASHCAN_SAFE_BEGIN(self
);
884 --_PyTrash_delete_nesting
;
885 /* DO NOT restore GC tracking at this point. weakref callbacks
886 * (if any, and whether directly here or indirectly in something we
887 * call) may trigger GC, and if self is tracked at that point, it
888 * will look like trash to GC and GC will try to delete self again.
891 /* Find the nearest base with a different tp_dealloc */
893 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
894 base
= base
->tp_base
;
898 /* If we added a weaklist, we clear it. Do this *before* calling
899 the finalizer (__del__), clearing slots, or clearing the instance
902 if (type
->tp_weaklistoffset
&& !base
->tp_weaklistoffset
)
903 PyObject_ClearWeakRefs(self
);
905 /* Maybe call finalizer; exit early if resurrected */
907 _PyObject_GC_TRACK(self
);
909 if (self
->ob_refcnt
> 0)
910 goto endlabel
; /* resurrected */
912 _PyObject_GC_UNTRACK(self
);
913 /* New weakrefs could be created during the finalizer call.
914 If this occurs, clear them out without calling their
915 finalizers since they might rely on part of the object
916 being finalized that has already been destroyed. */
917 if (type
->tp_weaklistoffset
&& !base
->tp_weaklistoffset
) {
918 /* Modeled after GET_WEAKREFS_LISTPTR() */
919 PyWeakReference
**list
= (PyWeakReference
**) \
920 PyObject_GET_WEAKREFS_LISTPTR(self
);
922 _PyWeakref_ClearRef(*list
);
926 /* Clear slots up to the nearest base with a different tp_dealloc */
928 while ((basedealloc
= base
->tp_dealloc
) == subtype_dealloc
) {
930 clear_slots(base
, self
);
931 base
= base
->tp_base
;
935 /* If we added a dict, DECREF it */
936 if (type
->tp_dictoffset
&& !base
->tp_dictoffset
) {
937 PyObject
**dictptr
= _PyObject_GetDictPtr(self
);
938 if (dictptr
!= NULL
) {
939 PyObject
*dict
= *dictptr
;
947 /* Extract the type again; tp_del may have changed it */
948 type
= Py_TYPE(self
);
950 /* Call the base tp_dealloc(); first retrack self if
951 * basedealloc knows about gc.
953 if (PyType_IS_GC(base
))
954 _PyObject_GC_TRACK(self
);
958 /* Can't reference self beyond this point */
962 ++_PyTrash_delete_nesting
;
963 Py_TRASHCAN_SAFE_END(self
);
964 --_PyTrash_delete_nesting
;
966 /* Explanation of the weirdness around the trashcan macros:
968 Q. What do the trashcan macros do?
970 A. Read the comment titled "Trashcan mechanism" in object.h.
971 For one, this explains why there must be a call to GC-untrack
972 before the trashcan begin macro. Without understanding the
973 trashcan code, the answers to the following questions don't make
976 Q. Why do we GC-untrack before the trashcan and then immediately
977 GC-track again afterward?
979 A. In the case that the base class is GC-aware, the base class
980 probably GC-untracks the object. If it does that using the
981 UNTRACK macro, this will crash when the object is already
982 untracked. Because we don't know what the base class does, the
983 only safe thing is to make sure the object is tracked when we
984 call the base class dealloc. But... The trashcan begin macro
985 requires that the object is *untracked* before it is called. So
992 Q. Why did the last question say "immediately GC-track again"?
993 It's nowhere near immediately.
995 A. Because the code *used* to re-track immediately. Bad Idea.
996 self has a refcount of 0, and if gc ever gets its hands on it
997 (which can happen if any weakref callback gets invoked), it
998 looks like trash to gc too, and gc also tries to delete self
999 then. But we're already deleting self. Double dealloction is
1002 Q. Why the bizarre (net-zero) manipulation of
1003 _PyTrash_delete_nesting around the trashcan macros?
1005 A. Some base classes (e.g. list) also use the trashcan mechanism.
1006 The following scenario used to be possible:
1008 - suppose the trashcan level is one below the trashcan limit
1010 - subtype_dealloc() is called
1012 - the trashcan limit is not yet reached, so the trashcan level
1013 is incremented and the code between trashcan begin and end is
1016 - this destroys much of the object's contents, including its
1019 - basedealloc() is called; this is really list_dealloc(), or
1020 some other type which also uses the trashcan macros
1022 - the trashcan limit is now reached, so the object is put on the
1023 trashcan's to-be-deleted-later list
1025 - basedealloc() returns
1027 - subtype_dealloc() decrefs the object's type
1029 - subtype_dealloc() returns
1031 - later, the trashcan code starts deleting the objects from its
1032 to-be-deleted-later list
1034 - subtype_dealloc() is called *AGAIN* for the same object
1036 - at the very least (if the destroyed slots and __dict__ don't
1037 cause problems) the object's type gets decref'ed a second
1038 time, which is *BAD*!!!
1040 The remedy is to make sure that if the code between trashcan
1041 begin and end in subtype_dealloc() is called, the code between
1042 trashcan begin and end in basedealloc() will also be called.
1043 This is done by decrementing the level after passing into the
1044 trashcan block, and incrementing it just before leaving the
1047 But now it's possible that a chain of objects consisting solely
1048 of objects whose deallocator is subtype_dealloc() will defeat
1049 the trashcan mechanism completely: the decremented level means
1050 that the effective level never reaches the limit. Therefore, we
1051 *increment* the level *before* entering the trashcan block, and
1052 matchingly decrement it after leaving. This means the trashcan
1053 code will trigger a little early, but that's no big deal.
1055 Q. Are there any live examples of code in need of all this
1058 A. Yes. See SF bug 668433 for code that crashed (when Python was
1059 compiled in debug mode) before the trashcan level manipulations
1060 were added. For more discussion, see SF patches 581742, 575073
1065 static PyTypeObject
*solid_base(PyTypeObject
*type
);
1067 /* type test with subclassing support */
1070 PyType_IsSubtype(PyTypeObject
*a
, PyTypeObject
*b
)
1076 /* Deal with multiple inheritance without recursion
1077 by walking the MRO tuple */
1079 assert(PyTuple_Check(mro
));
1080 n
= PyTuple_GET_SIZE(mro
);
1081 for (i
= 0; i
< n
; i
++) {
1082 if (PyTuple_GET_ITEM(mro
, i
) == (PyObject
*)b
)
1088 /* a is not completely initilized yet; follow tp_base */
1093 } while (a
!= NULL
);
1094 return b
== &PyBaseObject_Type
;
1098 /* Internal routines to do a method lookup in the type
1099 without looking in the instance dictionary
1100 (so we can't use PyObject_GetAttr) but still binding
1101 it to the instance. The arguments are the object,
1102 the method name as a C string, and the address of a
1103 static variable used to cache the interned Python string.
1107 - lookup_maybe() returns NULL without raising an exception
1108 when the _PyType_Lookup() call fails;
1110 - lookup_method() always raises an exception upon errors.
1112 - _PyObject_LookupSpecial() exported for the benefit of other places.
1116 lookup_maybe(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
1120 if (*attrobj
== NULL
) {
1121 *attrobj
= PyUnicode_InternFromString(attrstr
);
1122 if (*attrobj
== NULL
)
1125 res
= _PyType_Lookup(Py_TYPE(self
), *attrobj
);
1128 if ((f
= Py_TYPE(res
)->tp_descr_get
) == NULL
)
1131 res
= f(res
, self
, (PyObject
*)(Py_TYPE(self
)));
1137 lookup_method(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
1139 PyObject
*res
= lookup_maybe(self
, attrstr
, attrobj
);
1140 if (res
== NULL
&& !PyErr_Occurred())
1141 PyErr_SetObject(PyExc_AttributeError
, *attrobj
);
1146 _PyObject_LookupSpecial(PyObject
*self
, char *attrstr
, PyObject
**attrobj
)
1148 return lookup_maybe(self
, attrstr
, attrobj
);
1151 /* A variation of PyObject_CallMethod that uses lookup_method()
1152 instead of PyObject_GetAttrString(). This uses the same convention
1153 as lookup_method to cache the interned name string object. */
1156 call_method(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
1159 PyObject
*args
, *func
= 0, *retval
;
1160 va_start(va
, format
);
1162 func
= lookup_maybe(o
, name
, nameobj
);
1165 if (!PyErr_Occurred())
1166 PyErr_SetObject(PyExc_AttributeError
, *nameobj
);
1170 if (format
&& *format
)
1171 args
= Py_VaBuildValue(format
, va
);
1173 args
= PyTuple_New(0);
1180 assert(PyTuple_Check(args
));
1181 retval
= PyObject_Call(func
, args
, NULL
);
1189 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
1192 call_maybe(PyObject
*o
, char *name
, PyObject
**nameobj
, char *format
, ...)
1195 PyObject
*args
, *func
= 0, *retval
;
1196 va_start(va
, format
);
1198 func
= lookup_maybe(o
, name
, nameobj
);
1201 if (!PyErr_Occurred()) {
1202 Py_INCREF(Py_NotImplemented
);
1203 return Py_NotImplemented
;
1208 if (format
&& *format
)
1209 args
= Py_VaBuildValue(format
, va
);
1211 args
= PyTuple_New(0);
1218 assert(PyTuple_Check(args
));
1219 retval
= PyObject_Call(func
, args
, NULL
);
1228 Method resolution order algorithm C3 described in
1229 "A Monotonic Superclass Linearization for Dylan",
1230 by Kim Barrett, Bob Cassel, Paul Haahr,
1231 David A. Moon, Keith Playford, and P. Tucker Withington.
1234 Some notes about the rules implied by C3:
1237 It isn't legal to repeat a class in a list of base classes.
1239 The next three properties are the 3 constraints in "C3".
1241 Local precendece order.
1242 If A precedes B in C's MRO, then A will precede B in the MRO of all
1246 The MRO of a class must be an extension without reordering of the
1247 MRO of each of its superclasses.
1249 Extended Precedence Graph (EPG).
1250 Linearization is consistent if there is a path in the EPG from
1251 each class to all its successors in the linearization. See
1252 the paper for definition of EPG.
1256 tail_contains(PyObject
*list
, int whence
, PyObject
*o
) {
1258 size
= PyList_GET_SIZE(list
);
1260 for (j
= whence
+1; j
< size
; j
++) {
1261 if (PyList_GET_ITEM(list
, j
) == o
)
1268 class_name(PyObject
*cls
)
1270 PyObject
*name
= PyObject_GetAttrString(cls
, "__name__");
1274 name
= PyObject_Repr(cls
);
1278 if (!PyUnicode_Check(name
)) {
1286 check_duplicates(PyObject
*list
)
1289 /* Let's use a quadratic time algorithm,
1290 assuming that the bases lists is short.
1292 n
= PyList_GET_SIZE(list
);
1293 for (i
= 0; i
< n
; i
++) {
1294 PyObject
*o
= PyList_GET_ITEM(list
, i
);
1295 for (j
= i
+ 1; j
< n
; j
++) {
1296 if (PyList_GET_ITEM(list
, j
) == o
) {
1299 PyErr_Format(PyExc_TypeError
,
1300 "duplicate base class %U",
1304 PyErr_SetString(PyExc_TypeError
,
1305 "duplicate base class");
1314 /* Raise a TypeError for an MRO order disagreement.
1316 It's hard to produce a good error message. In the absence of better
1317 insight into error reporting, report the classes that were candidates
1318 to be put next into the MRO. There is some conflict between the
1319 order in which they should be put in the MRO, but it's hard to
1320 diagnose what constraint can't be satisfied.
1324 set_mro_error(PyObject
*to_merge
, int *remain
)
1326 Py_ssize_t i
, n
, off
, to_merge_size
;
1329 PyObject
*set
= PyDict_New();
1332 to_merge_size
= PyList_GET_SIZE(to_merge
);
1333 for (i
= 0; i
< to_merge_size
; i
++) {
1334 PyObject
*L
= PyList_GET_ITEM(to_merge
, i
);
1335 if (remain
[i
] < PyList_GET_SIZE(L
)) {
1336 PyObject
*c
= PyList_GET_ITEM(L
, remain
[i
]);
1337 if (PyDict_SetItem(set
, c
, Py_None
) < 0) {
1343 n
= PyDict_Size(set
);
1345 off
= PyOS_snprintf(buf
, sizeof(buf
), "Cannot create a \
1346 consistent method resolution\norder (MRO) for bases");
1348 while (PyDict_Next(set
, &i
, &k
, &v
) && (size_t)off
< sizeof(buf
)) {
1349 PyObject
*name
= class_name(k
);
1350 off
+= PyOS_snprintf(buf
+ off
, sizeof(buf
) - off
, " %s",
1351 name
? _PyUnicode_AsString(name
) : "?");
1353 if (--n
&& (size_t)(off
+1) < sizeof(buf
)) {
1358 PyErr_SetString(PyExc_TypeError
, buf
);
1363 pmerge(PyObject
*acc
, PyObject
* to_merge
) {
1364 Py_ssize_t i
, j
, to_merge_size
, empty_cnt
;
1368 to_merge_size
= PyList_GET_SIZE(to_merge
);
1370 /* remain stores an index into each sublist of to_merge.
1371 remain[i] is the index of the next base in to_merge[i]
1372 that is not included in acc.
1374 remain
= (int *)PyMem_MALLOC(SIZEOF_INT
*to_merge_size
);
1377 for (i
= 0; i
< to_merge_size
; i
++)
1382 for (i
= 0; i
< to_merge_size
; i
++) {
1383 PyObject
*candidate
;
1385 PyObject
*cur_list
= PyList_GET_ITEM(to_merge
, i
);
1387 if (remain
[i
] >= PyList_GET_SIZE(cur_list
)) {
1392 /* Choose next candidate for MRO.
1394 The input sequences alone can determine the choice.
1395 If not, choose the class which appears in the MRO
1396 of the earliest direct superclass of the new class.
1399 candidate
= PyList_GET_ITEM(cur_list
, remain
[i
]);
1400 for (j
= 0; j
< to_merge_size
; j
++) {
1401 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1402 if (tail_contains(j_lst
, remain
[j
], candidate
)) {
1403 goto skip
; /* continue outer loop */
1406 ok
= PyList_Append(acc
, candidate
);
1411 for (j
= 0; j
< to_merge_size
; j
++) {
1412 PyObject
*j_lst
= PyList_GET_ITEM(to_merge
, j
);
1413 if (remain
[j
] < PyList_GET_SIZE(j_lst
) &&
1414 PyList_GET_ITEM(j_lst
, remain
[j
]) == candidate
) {
1422 if (empty_cnt
== to_merge_size
) {
1426 set_mro_error(to_merge
, remain
);
1432 mro_implementation(PyTypeObject
*type
)
1436 PyObject
*bases
, *result
;
1437 PyObject
*to_merge
, *bases_aslist
;
1439 if (type
->tp_dict
== NULL
) {
1440 if (PyType_Ready(type
) < 0)
1444 /* Find a superclass linearization that honors the constraints
1445 of the explicit lists of bases and the constraints implied by
1448 to_merge is a list of lists, where each list is a superclass
1449 linearization implied by a base class. The last element of
1450 to_merge is the declared list of bases.
1453 bases
= type
->tp_bases
;
1454 n
= PyTuple_GET_SIZE(bases
);
1456 to_merge
= PyList_New(n
+1);
1457 if (to_merge
== NULL
)
1460 for (i
= 0; i
< n
; i
++) {
1461 PyObject
*base
= PyTuple_GET_ITEM(bases
, i
);
1462 PyObject
*parentMRO
;
1463 parentMRO
= PySequence_List(((PyTypeObject
*)base
)->tp_mro
);
1464 if (parentMRO
== NULL
) {
1465 Py_DECREF(to_merge
);
1469 PyList_SET_ITEM(to_merge
, i
, parentMRO
);
1472 bases_aslist
= PySequence_List(bases
);
1473 if (bases_aslist
== NULL
) {
1474 Py_DECREF(to_merge
);
1477 /* This is just a basic sanity check. */
1478 if (check_duplicates(bases_aslist
) < 0) {
1479 Py_DECREF(to_merge
);
1480 Py_DECREF(bases_aslist
);
1483 PyList_SET_ITEM(to_merge
, n
, bases_aslist
);
1485 result
= Py_BuildValue("[O]", (PyObject
*)type
);
1486 if (result
== NULL
) {
1487 Py_DECREF(to_merge
);
1491 ok
= pmerge(result
, to_merge
);
1492 Py_DECREF(to_merge
);
1502 mro_external(PyObject
*self
)
1504 PyTypeObject
*type
= (PyTypeObject
*)self
;
1506 return mro_implementation(type
);
1510 mro_internal(PyTypeObject
*type
)
1512 PyObject
*mro
, *result
, *tuple
;
1515 if (Py_TYPE(type
) == &PyType_Type
) {
1516 result
= mro_implementation(type
);
1519 static PyObject
*mro_str
;
1521 mro
= lookup_method((PyObject
*)type
, "mro", &mro_str
);
1524 result
= PyObject_CallObject(mro
, NULL
);
1529 tuple
= PySequence_Tuple(result
);
1536 PyTypeObject
*solid
;
1538 solid
= solid_base(type
);
1540 len
= PyTuple_GET_SIZE(tuple
);
1542 for (i
= 0; i
< len
; i
++) {
1544 cls
= PyTuple_GET_ITEM(tuple
, i
);
1545 if (!PyType_Check(cls
)) {
1546 PyErr_Format(PyExc_TypeError
,
1547 "mro() returned a non-class ('%.500s')",
1548 Py_TYPE(cls
)->tp_name
);
1552 t
= (PyTypeObject
*)cls
;
1553 if (!PyType_IsSubtype(solid
, solid_base(t
))) {
1554 PyErr_Format(PyExc_TypeError
,
1555 "mro() returned base with unsuitable layout ('%.500s')",
1562 type
->tp_mro
= tuple
;
1564 type_mro_modified(type
, type
->tp_mro
);
1565 /* corner case: the old-style super class might have been hidden
1566 from the custom MRO */
1567 type_mro_modified(type
, type
->tp_bases
);
1569 PyType_Modified(type
);
1575 /* Calculate the best base amongst multiple base classes.
1576 This is the first one that's on the path to the "solid base". */
1578 static PyTypeObject
*
1579 best_base(PyObject
*bases
)
1582 PyTypeObject
*base
, *winner
, *candidate
, *base_i
;
1583 PyObject
*base_proto
;
1585 assert(PyTuple_Check(bases
));
1586 n
= PyTuple_GET_SIZE(bases
);
1590 for (i
= 0; i
< n
; i
++) {
1591 base_proto
= PyTuple_GET_ITEM(bases
, i
);
1592 if (!PyType_Check(base_proto
)) {
1595 "bases must be types");
1598 base_i
= (PyTypeObject
*)base_proto
;
1599 if (base_i
->tp_dict
== NULL
) {
1600 if (PyType_Ready(base_i
) < 0)
1603 candidate
= solid_base(base_i
);
1604 if (winner
== NULL
) {
1608 else if (PyType_IsSubtype(winner
, candidate
))
1610 else if (PyType_IsSubtype(candidate
, winner
)) {
1617 "multiple bases have "
1618 "instance lay-out conflict");
1623 PyErr_SetString(PyExc_TypeError
,
1624 "a new-style class can't have only classic bases");
1629 extra_ivars(PyTypeObject
*type
, PyTypeObject
*base
)
1631 size_t t_size
= type
->tp_basicsize
;
1632 size_t b_size
= base
->tp_basicsize
;
1634 assert(t_size
>= b_size
); /* Else type smaller than base! */
1635 if (type
->tp_itemsize
|| base
->tp_itemsize
) {
1636 /* If itemsize is involved, stricter rules */
1637 return t_size
!= b_size
||
1638 type
->tp_itemsize
!= base
->tp_itemsize
;
1640 if (type
->tp_weaklistoffset
&& base
->tp_weaklistoffset
== 0 &&
1641 type
->tp_weaklistoffset
+ sizeof(PyObject
*) == t_size
&&
1642 type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
1643 t_size
-= sizeof(PyObject
*);
1644 if (type
->tp_dictoffset
&& base
->tp_dictoffset
== 0 &&
1645 type
->tp_dictoffset
+ sizeof(PyObject
*) == t_size
&&
1646 type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
1647 t_size
-= sizeof(PyObject
*);
1649 return t_size
!= b_size
;
1652 static PyTypeObject
*
1653 solid_base(PyTypeObject
*type
)
1658 base
= solid_base(type
->tp_base
);
1660 base
= &PyBaseObject_Type
;
1661 if (extra_ivars(type
, base
))
1667 static void object_dealloc(PyObject
*);
1668 static int object_init(PyObject
*, PyObject
*, PyObject
*);
1669 static int update_slot(PyTypeObject
*, PyObject
*);
1670 static void fixup_slot_dispatchers(PyTypeObject
*);
1673 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1674 * inherited from various builtin types. The builtin base usually provides
1675 * its own __dict__ descriptor, so we use that when we can.
1677 static PyTypeObject
*
1678 get_builtin_base_with_dict(PyTypeObject
*type
)
1680 while (type
->tp_base
!= NULL
) {
1681 if (type
->tp_dictoffset
!= 0 &&
1682 !(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
1684 type
= type
->tp_base
;
1690 get_dict_descriptor(PyTypeObject
*type
)
1692 static PyObject
*dict_str
;
1695 if (dict_str
== NULL
) {
1696 dict_str
= PyUnicode_InternFromString("__dict__");
1697 if (dict_str
== NULL
)
1700 descr
= _PyType_Lookup(type
, dict_str
);
1701 if (descr
== NULL
|| !PyDescr_IsData(descr
))
1708 raise_dict_descr_error(PyObject
*obj
)
1710 PyErr_Format(PyExc_TypeError
,
1711 "this __dict__ descriptor does not support "
1712 "'%.200s' objects", Py_TYPE(obj
)->tp_name
);
1716 subtype_dict(PyObject
*obj
, void *context
)
1722 base
= get_builtin_base_with_dict(Py_TYPE(obj
));
1725 PyObject
*descr
= get_dict_descriptor(base
);
1726 if (descr
== NULL
) {
1727 raise_dict_descr_error(obj
);
1730 func
= Py_TYPE(descr
)->tp_descr_get
;
1732 raise_dict_descr_error(obj
);
1735 return func(descr
, obj
, (PyObject
*)(Py_TYPE(obj
)));
1738 dictptr
= _PyObject_GetDictPtr(obj
);
1739 if (dictptr
== NULL
) {
1740 PyErr_SetString(PyExc_AttributeError
,
1741 "This object has no __dict__");
1746 *dictptr
= dict
= PyDict_New();
1752 subtype_setdict(PyObject
*obj
, PyObject
*value
, void *context
)
1758 base
= get_builtin_base_with_dict(Py_TYPE(obj
));
1761 PyObject
*descr
= get_dict_descriptor(base
);
1762 if (descr
== NULL
) {
1763 raise_dict_descr_error(obj
);
1766 func
= Py_TYPE(descr
)->tp_descr_set
;
1768 raise_dict_descr_error(obj
);
1771 return func(descr
, obj
, value
);
1774 dictptr
= _PyObject_GetDictPtr(obj
);
1775 if (dictptr
== NULL
) {
1776 PyErr_SetString(PyExc_AttributeError
,
1777 "This object has no __dict__");
1780 if (value
!= NULL
&& !PyDict_Check(value
)) {
1781 PyErr_Format(PyExc_TypeError
,
1782 "__dict__ must be set to a dictionary, "
1783 "not a '%.200s'", Py_TYPE(value
)->tp_name
);
1794 subtype_getweakref(PyObject
*obj
, void *context
)
1796 PyObject
**weaklistptr
;
1799 if (Py_TYPE(obj
)->tp_weaklistoffset
== 0) {
1800 PyErr_SetString(PyExc_AttributeError
,
1801 "This object has no __weakref__");
1804 assert(Py_TYPE(obj
)->tp_weaklistoffset
> 0);
1805 assert(Py_TYPE(obj
)->tp_weaklistoffset
+ sizeof(PyObject
*) <=
1806 (size_t)(Py_TYPE(obj
)->tp_basicsize
));
1807 weaklistptr
= (PyObject
**)
1808 ((char *)obj
+ Py_TYPE(obj
)->tp_weaklistoffset
);
1809 if (*weaklistptr
== NULL
)
1812 result
= *weaklistptr
;
1817 /* Three variants on the subtype_getsets list. */
1819 static PyGetSetDef subtype_getsets_full
[] = {
1820 {"__dict__", subtype_dict
, subtype_setdict
,
1821 PyDoc_STR("dictionary for instance variables (if defined)")},
1822 {"__weakref__", subtype_getweakref
, NULL
,
1823 PyDoc_STR("list of weak references to the object (if defined)")},
1827 static PyGetSetDef subtype_getsets_dict_only
[] = {
1828 {"__dict__", subtype_dict
, subtype_setdict
,
1829 PyDoc_STR("dictionary for instance variables (if defined)")},
1833 static PyGetSetDef subtype_getsets_weakref_only
[] = {
1834 {"__weakref__", subtype_getweakref
, NULL
,
1835 PyDoc_STR("list of weak references to the object (if defined)")},
1840 valid_identifier(PyObject
*s
)
1842 if (!PyUnicode_Check(s
)) {
1843 PyErr_Format(PyExc_TypeError
,
1844 "__slots__ items must be strings, not '%.200s'",
1845 Py_TYPE(s
)->tp_name
);
1848 if (!PyUnicode_IsIdentifier(s
)) {
1849 PyErr_SetString(PyExc_TypeError
,
1850 "__slots__ must be identifiers");
1858 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
);
1861 type_init(PyObject
*cls
, PyObject
*args
, PyObject
*kwds
)
1865 assert(args
!= NULL
&& PyTuple_Check(args
));
1866 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1868 if (kwds
!= NULL
&& PyDict_Check(kwds
) && PyDict_Size(kwds
) != 0) {
1869 PyErr_SetString(PyExc_TypeError
,
1870 "type.__init__() takes no keyword arguments");
1874 if (args
!= NULL
&& PyTuple_Check(args
) &&
1875 (PyTuple_GET_SIZE(args
) != 1 && PyTuple_GET_SIZE(args
) != 3)) {
1876 PyErr_SetString(PyExc_TypeError
,
1877 "type.__init__() takes 1 or 3 arguments");
1881 /* Call object.__init__(self) now. */
1882 /* XXX Could call super(type, cls).__init__() but what's the point? */
1883 args
= PyTuple_GetSlice(args
, 0, 0);
1884 res
= object_init(cls
, args
, NULL
);
1890 type_new(PyTypeObject
*metatype
, PyObject
*args
, PyObject
*kwds
)
1892 PyObject
*name
, *bases
, *dict
;
1893 static char *kwlist
[] = {"name", "bases", "dict", 0};
1894 PyObject
*slots
, *tmp
, *newslots
;
1895 PyTypeObject
*type
, *base
, *tmptype
, *winner
;
1896 PyHeapTypeObject
*et
;
1898 Py_ssize_t i
, nbases
, nslots
, slotoffset
, add_dict
, add_weak
;
1899 int j
, may_add_dict
, may_add_weak
;
1901 assert(args
!= NULL
&& PyTuple_Check(args
));
1902 assert(kwds
== NULL
|| PyDict_Check(kwds
));
1904 /* Special case: type(x) should return x->ob_type */
1906 const Py_ssize_t nargs
= PyTuple_GET_SIZE(args
);
1907 const Py_ssize_t nkwds
= kwds
== NULL
? 0 : PyDict_Size(kwds
);
1909 if (PyType_CheckExact(metatype
) && nargs
== 1 && nkwds
== 0) {
1910 PyObject
*x
= PyTuple_GET_ITEM(args
, 0);
1911 Py_INCREF(Py_TYPE(x
));
1912 return (PyObject
*) Py_TYPE(x
);
1915 /* SF bug 475327 -- if that didn't trigger, we need 3
1916 arguments. but PyArg_ParseTupleAndKeywords below may give
1917 a msg saying type() needs exactly 3. */
1918 if (nargs
+ nkwds
!= 3) {
1919 PyErr_SetString(PyExc_TypeError
,
1920 "type() takes 1 or 3 arguments");
1925 /* Check arguments: (name, bases, dict) */
1926 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "UO!O!:type", kwlist
,
1928 &PyTuple_Type
, &bases
,
1929 &PyDict_Type
, &dict
))
1932 /* Determine the proper metatype to deal with this,
1933 and check for metatype conflicts while we're at it.
1934 Note that if some other metatype wins to contract,
1935 it's possible that its instances are not types. */
1936 nbases
= PyTuple_GET_SIZE(bases
);
1938 for (i
= 0; i
< nbases
; i
++) {
1939 tmp
= PyTuple_GET_ITEM(bases
, i
);
1940 tmptype
= Py_TYPE(tmp
);
1941 if (PyType_IsSubtype(winner
, tmptype
))
1943 if (PyType_IsSubtype(tmptype
, winner
)) {
1947 PyErr_SetString(PyExc_TypeError
,
1948 "metaclass conflict: "
1949 "the metaclass of a derived class "
1950 "must be a (non-strict) subclass "
1951 "of the metaclasses of all its bases");
1954 if (winner
!= metatype
) {
1955 if (winner
->tp_new
!= type_new
) /* Pass it to the winner */
1956 return winner
->tp_new(winner
, args
, kwds
);
1960 /* Adjust for empty tuple bases */
1962 bases
= PyTuple_Pack(1, &PyBaseObject_Type
);
1970 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1972 /* Calculate best base, and check that all bases are type objects */
1973 base
= best_base(bases
);
1978 if (!PyType_HasFeature(base
, Py_TPFLAGS_BASETYPE
)) {
1979 PyErr_Format(PyExc_TypeError
,
1980 "type '%.100s' is not an acceptable base type",
1986 /* Check for a __slots__ sequence variable in dict, and count it */
1987 slots
= PyDict_GetItemString(dict
, "__slots__");
1991 may_add_dict
= base
->tp_dictoffset
== 0;
1992 may_add_weak
= base
->tp_weaklistoffset
== 0 && base
->tp_itemsize
== 0;
1993 if (slots
== NULL
) {
2004 /* Make it into a tuple */
2005 if (PyUnicode_Check(slots
))
2006 slots
= PyTuple_Pack(1, slots
);
2008 slots
= PySequence_Tuple(slots
);
2009 if (slots
== NULL
) {
2013 assert(PyTuple_Check(slots
));
2015 /* Are slots allowed? */
2016 nslots
= PyTuple_GET_SIZE(slots
);
2017 if (nslots
> 0 && base
->tp_itemsize
!= 0) {
2018 PyErr_Format(PyExc_TypeError
,
2019 "nonempty __slots__ "
2020 "not supported for subtype of '%s'",
2028 /* Check for valid slot names and two special cases */
2029 for (i
= 0; i
< nslots
; i
++) {
2030 PyObject
*tmp
= PyTuple_GET_ITEM(slots
, i
);
2031 if (!valid_identifier(tmp
))
2033 assert(PyUnicode_Check(tmp
));
2034 if (PyUnicode_CompareWithASCIIString(tmp
, "__dict__") == 0) {
2035 if (!may_add_dict
|| add_dict
) {
2036 PyErr_SetString(PyExc_TypeError
,
2037 "__dict__ slot disallowed: "
2038 "we already got one");
2043 if (PyUnicode_CompareWithASCIIString(tmp
, "__weakref__") == 0) {
2044 if (!may_add_weak
|| add_weak
) {
2045 PyErr_SetString(PyExc_TypeError
,
2046 "__weakref__ slot disallowed: "
2047 "either we already got one, "
2048 "or __itemsize__ != 0");
2055 /* Copy slots into a list, mangle names and sort them.
2056 Sorted names are needed for __class__ assignment.
2057 Convert them back to tuple at the end.
2059 newslots
= PyList_New(nslots
- add_dict
- add_weak
);
2060 if (newslots
== NULL
)
2062 for (i
= j
= 0; i
< nslots
; i
++) {
2063 tmp
= PyTuple_GET_ITEM(slots
, i
);
2065 PyUnicode_CompareWithASCIIString(tmp
, "__dict__") == 0) ||
2067 PyUnicode_CompareWithASCIIString(tmp
, "__weakref__") == 0))
2069 tmp
=_Py_Mangle(name
, tmp
);
2072 PyList_SET_ITEM(newslots
, j
, tmp
);
2075 assert(j
== nslots
- add_dict
- add_weak
);
2078 if (PyList_Sort(newslots
) == -1) {
2080 Py_DECREF(newslots
);
2083 slots
= PyList_AsTuple(newslots
);
2084 Py_DECREF(newslots
);
2085 if (slots
== NULL
) {
2090 /* Secondary bases may provide weakrefs or dict */
2092 ((may_add_dict
&& !add_dict
) ||
2093 (may_add_weak
&& !add_weak
))) {
2094 for (i
= 0; i
< nbases
; i
++) {
2095 tmp
= PyTuple_GET_ITEM(bases
, i
);
2096 if (tmp
== (PyObject
*)base
)
2097 continue; /* Skip primary base */
2098 assert(PyType_Check(tmp
));
2099 tmptype
= (PyTypeObject
*)tmp
;
2100 if (may_add_dict
&& !add_dict
&&
2101 tmptype
->tp_dictoffset
!= 0)
2103 if (may_add_weak
&& !add_weak
&&
2104 tmptype
->tp_weaklistoffset
!= 0)
2106 if (may_add_dict
&& !add_dict
)
2108 if (may_add_weak
&& !add_weak
)
2110 /* Nothing more to check */
2116 /* XXX From here until type is safely allocated,
2117 "return NULL" may leak slots! */
2119 /* Allocate the type object */
2120 type
= (PyTypeObject
*)metatype
->tp_alloc(metatype
, nslots
);
2127 /* Keep name and slots alive in the extended type object */
2128 et
= (PyHeapTypeObject
*)type
;
2131 et
->ht_slots
= slots
;
2133 /* Initialize tp_flags */
2134 type
->tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HEAPTYPE
|
2135 Py_TPFLAGS_BASETYPE
;
2136 if (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)
2137 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
2139 /* Initialize essential fields */
2140 type
->tp_as_number
= &et
->as_number
;
2141 type
->tp_as_sequence
= &et
->as_sequence
;
2142 type
->tp_as_mapping
= &et
->as_mapping
;
2143 type
->tp_as_buffer
= &et
->as_buffer
;
2144 type
->tp_name
= _PyUnicode_AsString(name
);
2145 if (!type
->tp_name
) {
2150 /* Set tp_base and tp_bases */
2151 type
->tp_bases
= bases
;
2153 type
->tp_base
= base
;
2155 /* Initialize tp_dict from passed-in dict */
2156 type
->tp_dict
= dict
= PyDict_Copy(dict
);
2162 /* Set __module__ in the dict */
2163 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
2164 tmp
= PyEval_GetGlobals();
2166 tmp
= PyDict_GetItemString(tmp
, "__name__");
2168 if (PyDict_SetItemString(dict
, "__module__",
2175 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2176 and is a string. The __doc__ accessor will first look for tp_doc;
2177 if that fails, it will still look into __dict__.
2180 PyObject
*doc
= PyDict_GetItemString(dict
, "__doc__");
2181 if (doc
!= NULL
&& PyUnicode_Check(doc
)) {
2186 doc_str
= _PyUnicode_AsString(doc
);
2187 if (doc_str
== NULL
) {
2191 /* Silently truncate the docstring if it contains null bytes. */
2192 len
= strlen(doc_str
);
2193 tp_doc
= (char *)PyObject_MALLOC(len
+ 1);
2194 if (tp_doc
== NULL
) {
2198 memcpy(tp_doc
, doc_str
, len
+ 1);
2199 type
->tp_doc
= tp_doc
;
2203 /* Special-case __new__: if it's a plain function,
2204 make it a static function */
2205 tmp
= PyDict_GetItemString(dict
, "__new__");
2206 if (tmp
!= NULL
&& PyFunction_Check(tmp
)) {
2207 tmp
= PyStaticMethod_New(tmp
);
2212 PyDict_SetItemString(dict
, "__new__", tmp
);
2216 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2217 mp
= PyHeapType_GET_MEMBERS(et
);
2218 slotoffset
= base
->tp_basicsize
;
2219 if (slots
!= NULL
) {
2220 for (i
= 0; i
< nslots
; i
++, mp
++) {
2221 mp
->name
= _PyUnicode_AsString(
2222 PyTuple_GET_ITEM(slots
, i
));
2223 mp
->type
= T_OBJECT_EX
;
2224 mp
->offset
= slotoffset
;
2226 /* __dict__ and __weakref__ are already filtered out */
2227 assert(strcmp(mp
->name
, "__dict__") != 0);
2228 assert(strcmp(mp
->name
, "__weakref__") != 0);
2230 slotoffset
+= sizeof(PyObject
*);
2234 if (base
->tp_itemsize
)
2235 type
->tp_dictoffset
= -(long)sizeof(PyObject
*);
2237 type
->tp_dictoffset
= slotoffset
;
2238 slotoffset
+= sizeof(PyObject
*);
2241 assert(!base
->tp_itemsize
);
2242 type
->tp_weaklistoffset
= slotoffset
;
2243 slotoffset
+= sizeof(PyObject
*);
2245 type
->tp_basicsize
= slotoffset
;
2246 type
->tp_itemsize
= base
->tp_itemsize
;
2247 type
->tp_members
= PyHeapType_GET_MEMBERS(et
);
2249 if (type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2250 type
->tp_getset
= subtype_getsets_full
;
2251 else if (type
->tp_weaklistoffset
&& !type
->tp_dictoffset
)
2252 type
->tp_getset
= subtype_getsets_weakref_only
;
2253 else if (!type
->tp_weaklistoffset
&& type
->tp_dictoffset
)
2254 type
->tp_getset
= subtype_getsets_dict_only
;
2256 type
->tp_getset
= NULL
;
2258 /* Special case some slots */
2259 if (type
->tp_dictoffset
!= 0 || nslots
> 0) {
2260 if (base
->tp_getattr
== NULL
&& base
->tp_getattro
== NULL
)
2261 type
->tp_getattro
= PyObject_GenericGetAttr
;
2262 if (base
->tp_setattr
== NULL
&& base
->tp_setattro
== NULL
)
2263 type
->tp_setattro
= PyObject_GenericSetAttr
;
2265 type
->tp_dealloc
= subtype_dealloc
;
2267 /* Enable GC unless there are really no instance variables possible */
2268 if (!(type
->tp_basicsize
== sizeof(PyObject
) &&
2269 type
->tp_itemsize
== 0))
2270 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
2272 /* Always override allocation strategy to use regular heap */
2273 type
->tp_alloc
= PyType_GenericAlloc
;
2274 if (type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) {
2275 type
->tp_free
= PyObject_GC_Del
;
2276 type
->tp_traverse
= subtype_traverse
;
2277 type
->tp_clear
= subtype_clear
;
2280 type
->tp_free
= PyObject_Del
;
2282 /* Initialize the rest */
2283 if (PyType_Ready(type
) < 0) {
2288 /* Put the proper slots in place */
2289 fixup_slot_dispatchers(type
);
2291 return (PyObject
*)type
;
2294 /* Internal API to look for a name through the MRO.
2295 This returns a borrowed reference, and doesn't set an exception! */
2297 _PyType_Lookup(PyTypeObject
*type
, PyObject
*name
)
2300 PyObject
*mro
, *res
, *base
, *dict
;
2303 if (MCACHE_CACHEABLE_NAME(name
) &&
2304 PyType_HasFeature(type
, Py_TPFLAGS_VALID_VERSION_TAG
)) {
2306 h
= MCACHE_HASH_METHOD(type
, name
);
2307 if (method_cache
[h
].version
== type
->tp_version_tag
&&
2308 method_cache
[h
].name
== name
)
2309 return method_cache
[h
].value
;
2312 /* Look in tp_dict of types in MRO */
2315 /* If mro is NULL, the type is either not yet initialized
2316 by PyType_Ready(), or already cleared by type_clear().
2317 Either way the safest thing to do is to return NULL. */
2322 assert(PyTuple_Check(mro
));
2323 n
= PyTuple_GET_SIZE(mro
);
2324 for (i
= 0; i
< n
; i
++) {
2325 base
= PyTuple_GET_ITEM(mro
, i
);
2326 assert(PyType_Check(base
));
2327 dict
= ((PyTypeObject
*)base
)->tp_dict
;
2328 assert(dict
&& PyDict_Check(dict
));
2329 res
= PyDict_GetItem(dict
, name
);
2334 if (MCACHE_CACHEABLE_NAME(name
) && assign_version_tag(type
)) {
2335 h
= MCACHE_HASH_METHOD(type
, name
);
2336 method_cache
[h
].version
= type
->tp_version_tag
;
2337 method_cache
[h
].value
= res
; /* borrowed */
2339 Py_DECREF(method_cache
[h
].name
);
2340 method_cache
[h
].name
= name
;
2345 /* This is similar to PyObject_GenericGetAttr(),
2346 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2348 type_getattro(PyTypeObject
*type
, PyObject
*name
)
2350 PyTypeObject
*metatype
= Py_TYPE(type
);
2351 PyObject
*meta_attribute
, *attribute
;
2352 descrgetfunc meta_get
;
2354 /* Initialize this type (we'll assume the metatype is initialized) */
2355 if (type
->tp_dict
== NULL
) {
2356 if (PyType_Ready(type
) < 0)
2360 /* No readable descriptor found yet */
2363 /* Look for the attribute in the metatype */
2364 meta_attribute
= _PyType_Lookup(metatype
, name
);
2366 if (meta_attribute
!= NULL
) {
2367 meta_get
= Py_TYPE(meta_attribute
)->tp_descr_get
;
2369 if (meta_get
!= NULL
&& PyDescr_IsData(meta_attribute
)) {
2370 /* Data descriptors implement tp_descr_set to intercept
2371 * writes. Assume the attribute is not overridden in
2372 * type's tp_dict (and bases): call the descriptor now.
2374 return meta_get(meta_attribute
, (PyObject
*)type
,
2375 (PyObject
*)metatype
);
2377 Py_INCREF(meta_attribute
);
2380 /* No data descriptor found on metatype. Look in tp_dict of this
2381 * type and its bases */
2382 attribute
= _PyType_Lookup(type
, name
);
2383 if (attribute
!= NULL
) {
2384 /* Implement descriptor functionality, if any */
2385 descrgetfunc local_get
= Py_TYPE(attribute
)->tp_descr_get
;
2387 Py_XDECREF(meta_attribute
);
2389 if (local_get
!= NULL
) {
2390 /* NULL 2nd argument indicates the descriptor was
2391 * found on the target object itself (or a base) */
2392 return local_get(attribute
, (PyObject
*)NULL
,
2396 Py_INCREF(attribute
);
2400 /* No attribute found in local __dict__ (or bases): use the
2401 * descriptor from the metatype, if any */
2402 if (meta_get
!= NULL
) {
2404 res
= meta_get(meta_attribute
, (PyObject
*)type
,
2405 (PyObject
*)metatype
);
2406 Py_DECREF(meta_attribute
);
2410 /* If an ordinary attribute was found on the metatype, return it now */
2411 if (meta_attribute
!= NULL
) {
2412 return meta_attribute
;
2416 PyErr_Format(PyExc_AttributeError
,
2417 "type object '%.50s' has no attribute '%U'",
2418 type
->tp_name
, name
);
2423 type_setattro(PyTypeObject
*type
, PyObject
*name
, PyObject
*value
)
2425 if (!(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
2428 "can't set attributes of built-in/extension type '%s'",
2432 if (PyObject_GenericSetAttr((PyObject
*)type
, name
, value
) < 0)
2434 return update_slot(type
, name
);
2438 type_dealloc(PyTypeObject
*type
)
2440 PyHeapTypeObject
*et
;
2442 /* Assert this is a heap-allocated type object */
2443 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2444 _PyObject_GC_UNTRACK(type
);
2445 PyObject_ClearWeakRefs((PyObject
*)type
);
2446 et
= (PyHeapTypeObject
*)type
;
2447 Py_XDECREF(type
->tp_base
);
2448 Py_XDECREF(type
->tp_dict
);
2449 Py_XDECREF(type
->tp_bases
);
2450 Py_XDECREF(type
->tp_mro
);
2451 Py_XDECREF(type
->tp_cache
);
2452 Py_XDECREF(type
->tp_subclasses
);
2453 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2454 * of most other objects. It's okay to cast it to char *.
2456 PyObject_Free((char *)type
->tp_doc
);
2457 Py_XDECREF(et
->ht_name
);
2458 Py_XDECREF(et
->ht_slots
);
2459 Py_TYPE(type
)->tp_free((PyObject
*)type
);
2463 type_subclasses(PyTypeObject
*type
, PyObject
*args_ignored
)
2465 PyObject
*list
, *raw
, *ref
;
2468 list
= PyList_New(0);
2471 raw
= type
->tp_subclasses
;
2474 assert(PyList_Check(raw
));
2475 n
= PyList_GET_SIZE(raw
);
2476 for (i
= 0; i
< n
; i
++) {
2477 ref
= PyList_GET_ITEM(raw
, i
);
2478 assert(PyWeakref_CheckRef(ref
));
2479 ref
= PyWeakref_GET_OBJECT(ref
);
2480 if (ref
!= Py_None
) {
2481 if (PyList_Append(list
, ref
) < 0) {
2491 type_prepare(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2493 return PyDict_New();
2496 static PyMethodDef type_methods
[] = {
2497 {"mro", (PyCFunction
)mro_external
, METH_NOARGS
,
2498 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2499 {"__subclasses__", (PyCFunction
)type_subclasses
, METH_NOARGS
,
2500 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2501 {"__prepare__", (PyCFunction
)type_prepare
,
2502 METH_VARARGS
| METH_KEYWORDS
| METH_CLASS
,
2503 PyDoc_STR("__prepare__() -> dict\n"
2504 "used to create the namespace for the class statement")},
2505 {"__instancecheck__", type___instancecheck__
, METH_O
,
2506 PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
2507 {"__subclasscheck__", type___subclasscheck__
, METH_O
,
2508 PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
2512 PyDoc_STRVAR(type_doc
,
2513 "type(object) -> the object's type\n"
2514 "type(name, bases, dict) -> a new type");
2517 type_traverse(PyTypeObject
*type
, visitproc visit
, void *arg
)
2519 /* Because of type_is_gc(), the collector only calls this
2521 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2523 Py_VISIT(type
->tp_dict
);
2524 Py_VISIT(type
->tp_cache
);
2525 Py_VISIT(type
->tp_mro
);
2526 Py_VISIT(type
->tp_bases
);
2527 Py_VISIT(type
->tp_base
);
2529 /* There's no need to visit type->tp_subclasses or
2530 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2531 in cycles; tp_subclasses is a list of weak references,
2532 and slots is a tuple of strings. */
2538 type_clear(PyTypeObject
*type
)
2540 /* Because of type_is_gc(), the collector only calls this
2542 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
2544 /* The only field we need to clear is tp_mro, which is part of a
2545 hard cycle (its first element is the class itself) that won't
2546 be broken otherwise (it's a tuple and tuples don't have a
2547 tp_clear handler). None of the other fields need to be
2548 cleared, and here's why:
2551 It is a dict, so the collector will call its tp_clear.
2554 Not used; if it were, it would be a dict.
2557 If these are involved in a cycle, there must be at least
2558 one other, mutable object in the cycle, e.g. a base
2559 class's dict; the cycle will be broken that way.
2562 A list of weak references can't be part of a cycle; and
2563 lists have their own tp_clear.
2565 slots (in PyHeapTypeObject):
2566 A tuple of strings can't be part of a cycle.
2569 Py_CLEAR(type
->tp_mro
);
2575 type_is_gc(PyTypeObject
*type
)
2577 return type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
;
2580 PyTypeObject PyType_Type
= {
2581 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2582 "type", /* tp_name */
2583 sizeof(PyHeapTypeObject
), /* tp_basicsize */
2584 sizeof(PyMemberDef
), /* tp_itemsize */
2585 (destructor
)type_dealloc
, /* tp_dealloc */
2589 0, /* tp_reserved */
2590 (reprfunc
)type_repr
, /* tp_repr */
2591 0, /* tp_as_number */
2592 0, /* tp_as_sequence */
2593 0, /* tp_as_mapping */
2595 (ternaryfunc
)type_call
, /* tp_call */
2597 (getattrofunc
)type_getattro
, /* tp_getattro */
2598 (setattrofunc
)type_setattro
, /* tp_setattro */
2599 0, /* tp_as_buffer */
2600 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2601 Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_TYPE_SUBCLASS
, /* tp_flags */
2602 type_doc
, /* tp_doc */
2603 (traverseproc
)type_traverse
, /* tp_traverse */
2604 (inquiry
)type_clear
, /* tp_clear */
2605 0, /* tp_richcompare */
2606 offsetof(PyTypeObject
, tp_weaklist
), /* tp_weaklistoffset */
2608 0, /* tp_iternext */
2609 type_methods
, /* tp_methods */
2610 type_members
, /* tp_members */
2611 type_getsets
, /* tp_getset */
2614 0, /* tp_descr_get */
2615 0, /* tp_descr_set */
2616 offsetof(PyTypeObject
, tp_dict
), /* tp_dictoffset */
2617 type_init
, /* tp_init */
2619 type_new
, /* tp_new */
2620 PyObject_GC_Del
, /* tp_free */
2621 (inquiry
)type_is_gc
, /* tp_is_gc */
2625 /* The base type of all types (eventually)... except itself. */
2627 /* You may wonder why object.__new__() only complains about arguments
2628 when object.__init__() is not overridden, and vice versa.
2630 Consider the use cases:
2632 1. When neither is overridden, we want to hear complaints about
2633 excess (i.e., any) arguments, since their presence could
2634 indicate there's a bug.
2636 2. When defining an Immutable type, we are likely to override only
2637 __new__(), since __init__() is called too late to initialize an
2638 Immutable object. Since __new__() defines the signature for the
2639 type, it would be a pain to have to override __init__() just to
2640 stop it from complaining about excess arguments.
2642 3. When defining a Mutable type, we are likely to override only
2643 __init__(). So here the converse reasoning applies: we don't
2644 want to have to override __new__() just to stop it from
2647 4. When __init__() is overridden, and the subclass __init__() calls
2648 object.__init__(), the latter should complain about excess
2649 arguments; ditto for __new__().
2651 Use cases 2 and 3 make it unattractive to unconditionally check for
2652 excess arguments. The best solution that addresses all four use
2653 cases is as follows: __init__() complains about excess arguments
2654 unless __new__() is overridden and __init__() is not overridden
2655 (IOW, if __init__() is overridden or __new__() is not overridden);
2656 symmetrically, __new__() complains about excess arguments unless
2657 __init__() is overridden and __new__() is not overridden
2658 (IOW, if __new__() is overridden or __init__() is not overridden).
2660 However, for backwards compatibility, this breaks too much code.
2661 Therefore, in 2.6, we'll *warn* about excess arguments when both
2662 methods are overridden; for all other cases we'll use the above
2669 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
2672 excess_args(PyObject
*args
, PyObject
*kwds
)
2674 return PyTuple_GET_SIZE(args
) ||
2675 (kwds
&& PyDict_Check(kwds
) && PyDict_Size(kwds
));
2679 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2682 if (excess_args(args
, kwds
)) {
2683 PyTypeObject
*type
= Py_TYPE(self
);
2684 if (type
->tp_init
!= object_init
&&
2685 type
->tp_new
!= object_new
)
2687 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2688 "object.__init__() takes no parameters",
2691 else if (type
->tp_init
!= object_init
||
2692 type
->tp_new
== object_new
)
2694 PyErr_SetString(PyExc_TypeError
,
2695 "object.__init__() takes no parameters");
2703 object_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2706 if (excess_args(args
, kwds
)) {
2707 if (type
->tp_new
!= object_new
&&
2708 type
->tp_init
!= object_init
)
2710 err
= PyErr_WarnEx(PyExc_DeprecationWarning
,
2711 "object.__new__() takes no parameters",
2714 else if (type
->tp_new
!= object_new
||
2715 type
->tp_init
== object_init
)
2717 PyErr_SetString(PyExc_TypeError
,
2718 "object.__new__() takes no parameters");
2725 if (type
->tp_flags
& Py_TPFLAGS_IS_ABSTRACT
) {
2726 static PyObject
*comma
= NULL
;
2727 PyObject
*abstract_methods
= NULL
;
2730 PyObject
*sorted_methods
= NULL
;
2731 PyObject
*joined
= NULL
;
2733 /* Compute ", ".join(sorted(type.__abstractmethods__))
2735 abstract_methods
= type_abstractmethods(type
, NULL
);
2736 if (abstract_methods
== NULL
)
2738 builtins
= PyEval_GetBuiltins();
2739 if (builtins
== NULL
)
2741 sorted
= PyDict_GetItemString(builtins
, "sorted");
2744 sorted_methods
= PyObject_CallFunctionObjArgs(sorted
,
2747 if (sorted_methods
== NULL
)
2749 if (comma
== NULL
) {
2750 comma
= PyUnicode_InternFromString(", ");
2754 joined
= PyObject_CallMethod(comma
, "join",
2755 "O", sorted_methods
);
2759 PyErr_Format(PyExc_TypeError
,
2760 "Can't instantiate abstract class %s "
2761 "with abstract methods %U",
2766 Py_XDECREF(sorted_methods
);
2767 Py_XDECREF(abstract_methods
);
2770 return type
->tp_alloc(type
, 0);
2774 object_dealloc(PyObject
*self
)
2776 Py_TYPE(self
)->tp_free(self
);
2780 object_repr(PyObject
*self
)
2783 PyObject
*mod
, *name
, *rtn
;
2785 type
= Py_TYPE(self
);
2786 mod
= type_module(type
, NULL
);
2789 else if (!PyUnicode_Check(mod
)) {
2793 name
= type_name(type
, NULL
);
2796 if (mod
!= NULL
&& PyUnicode_CompareWithASCIIString(mod
, "builtins"))
2797 rtn
= PyUnicode_FromFormat("<%U.%U object at %p>", mod
, name
, self
);
2799 rtn
= PyUnicode_FromFormat("<%s object at %p>",
2800 type
->tp_name
, self
);
2807 object_str(PyObject
*self
)
2811 f
= Py_TYPE(self
)->tp_repr
;
2818 object_richcompare(PyObject
*self
, PyObject
*other
, int op
)
2825 /* Return NotImplemented instead of False, so if two
2826 objects are compared, both get a chance at the
2827 comparison. See issue #1393. */
2828 res
= (self
== other
) ? Py_True
: Py_NotImplemented
;
2833 /* By default, != returns the opposite of ==,
2834 unless the latter returns NotImplemented. */
2835 res
= PyObject_RichCompare(self
, other
, Py_EQ
);
2836 if (res
!= NULL
&& res
!= Py_NotImplemented
) {
2837 int ok
= PyObject_IsTrue(res
);
2852 res
= Py_NotImplemented
;
2861 object_get_class(PyObject
*self
, void *closure
)
2863 Py_INCREF(Py_TYPE(self
));
2864 return (PyObject
*)(Py_TYPE(self
));
2868 equiv_structs(PyTypeObject
*a
, PyTypeObject
*b
)
2873 a
->tp_basicsize
== b
->tp_basicsize
&&
2874 a
->tp_itemsize
== b
->tp_itemsize
&&
2875 a
->tp_dictoffset
== b
->tp_dictoffset
&&
2876 a
->tp_weaklistoffset
== b
->tp_weaklistoffset
&&
2877 ((a
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
2878 (b
->tp_flags
& Py_TPFLAGS_HAVE_GC
)));
2882 same_slots_added(PyTypeObject
*a
, PyTypeObject
*b
)
2884 PyTypeObject
*base
= a
->tp_base
;
2886 PyObject
*slots_a
, *slots_b
;
2888 if (base
!= b
->tp_base
)
2890 if (equiv_structs(a
, base
) && equiv_structs(b
, base
))
2892 size
= base
->tp_basicsize
;
2893 if (a
->tp_dictoffset
== size
&& b
->tp_dictoffset
== size
)
2894 size
+= sizeof(PyObject
*);
2895 if (a
->tp_weaklistoffset
== size
&& b
->tp_weaklistoffset
== size
)
2896 size
+= sizeof(PyObject
*);
2898 /* Check slots compliance */
2899 slots_a
= ((PyHeapTypeObject
*)a
)->ht_slots
;
2900 slots_b
= ((PyHeapTypeObject
*)b
)->ht_slots
;
2901 if (slots_a
&& slots_b
) {
2902 if (PyObject_RichCompareBool(slots_a
, slots_b
, Py_EQ
) != 1)
2904 size
+= sizeof(PyObject
*) * PyTuple_GET_SIZE(slots_a
);
2906 return size
== a
->tp_basicsize
&& size
== b
->tp_basicsize
;
2910 compatible_for_assignment(PyTypeObject
* oldto
, PyTypeObject
* newto
, char* attr
)
2912 PyTypeObject
*newbase
, *oldbase
;
2914 if (newto
->tp_dealloc
!= oldto
->tp_dealloc
||
2915 newto
->tp_free
!= oldto
->tp_free
)
2917 PyErr_Format(PyExc_TypeError
,
2919 "'%s' deallocator differs from '%s'",
2927 while (equiv_structs(newbase
, newbase
->tp_base
))
2928 newbase
= newbase
->tp_base
;
2929 while (equiv_structs(oldbase
, oldbase
->tp_base
))
2930 oldbase
= oldbase
->tp_base
;
2931 if (newbase
!= oldbase
&&
2932 (newbase
->tp_base
!= oldbase
->tp_base
||
2933 !same_slots_added(newbase
, oldbase
))) {
2934 PyErr_Format(PyExc_TypeError
,
2936 "'%s' object layout differs from '%s'",
2947 object_set_class(PyObject
*self
, PyObject
*value
, void *closure
)
2949 PyTypeObject
*oldto
= Py_TYPE(self
);
2950 PyTypeObject
*newto
;
2952 if (value
== NULL
) {
2953 PyErr_SetString(PyExc_TypeError
,
2954 "can't delete __class__ attribute");
2957 if (!PyType_Check(value
)) {
2958 PyErr_Format(PyExc_TypeError
,
2959 "__class__ must be set to new-style class, not '%s' object",
2960 Py_TYPE(value
)->tp_name
);
2963 newto
= (PyTypeObject
*)value
;
2964 if (!(newto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) ||
2965 !(oldto
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
2967 PyErr_Format(PyExc_TypeError
,
2968 "__class__ assignment: only for heap types");
2971 if (compatible_for_assignment(newto
, oldto
, "__class__")) {
2973 Py_TYPE(self
) = newto
;
2982 static PyGetSetDef object_getsets
[] = {
2983 {"__class__", object_get_class
, object_set_class
,
2984 PyDoc_STR("the object's class")},
2989 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2990 We fall back to helpers in copyreg for:
2991 - pickle protocols < 2
2992 - calculating the list of slot names (done only once per class)
2993 - the __newobj__ function (which is used as a token but never called)
2997 import_copyreg(void)
2999 static PyObject
*copyreg_str
;
3002 copyreg_str
= PyUnicode_InternFromString("copyreg");
3003 if (copyreg_str
== NULL
)
3007 return PyImport_Import(copyreg_str
);
3011 slotnames(PyObject
*cls
)
3015 PyObject
*slotnames
;
3017 if (!PyType_Check(cls
)) {
3022 clsdict
= ((PyTypeObject
*)cls
)->tp_dict
;
3023 slotnames
= PyDict_GetItemString(clsdict
, "__slotnames__");
3024 if (slotnames
!= NULL
&& PyList_Check(slotnames
)) {
3025 Py_INCREF(slotnames
);
3029 copyreg
= import_copyreg();
3030 if (copyreg
== NULL
)
3033 slotnames
= PyObject_CallMethod(copyreg
, "_slotnames", "O", cls
);
3035 if (slotnames
!= NULL
&&
3036 slotnames
!= Py_None
&&
3037 !PyList_Check(slotnames
))
3039 PyErr_SetString(PyExc_TypeError
,
3040 "copyreg._slotnames didn't return a list or None");
3041 Py_DECREF(slotnames
);
3049 reduce_2(PyObject
*obj
)
3051 PyObject
*cls
, *getnewargs
;
3052 PyObject
*args
= NULL
, *args2
= NULL
;
3053 PyObject
*getstate
= NULL
, *state
= NULL
, *names
= NULL
;
3054 PyObject
*slots
= NULL
, *listitems
= NULL
, *dictitems
= NULL
;
3055 PyObject
*copyreg
= NULL
, *newobj
= NULL
, *res
= NULL
;
3058 cls
= PyObject_GetAttrString(obj
, "__class__");
3062 getnewargs
= PyObject_GetAttrString(obj
, "__getnewargs__");
3063 if (getnewargs
!= NULL
) {
3064 args
= PyObject_CallObject(getnewargs
, NULL
);
3065 Py_DECREF(getnewargs
);
3066 if (args
!= NULL
&& !PyTuple_Check(args
)) {
3067 PyErr_Format(PyExc_TypeError
,
3068 "__getnewargs__ should return a tuple, "
3069 "not '%.200s'", Py_TYPE(args
)->tp_name
);
3075 args
= PyTuple_New(0);
3080 getstate
= PyObject_GetAttrString(obj
, "__getstate__");
3081 if (getstate
!= NULL
) {
3082 state
= PyObject_CallObject(getstate
, NULL
);
3083 Py_DECREF(getstate
);
3089 state
= PyObject_GetAttrString(obj
, "__dict__");
3090 if (state
== NULL
) {
3095 names
= slotnames(cls
);
3098 if (names
!= Py_None
) {
3099 assert(PyList_Check(names
));
3100 slots
= PyDict_New();
3104 /* Can't pre-compute the list size; the list
3105 is stored on the class so accessible to other
3106 threads, which may be run by DECREF */
3107 for (i
= 0; i
< PyList_GET_SIZE(names
); i
++) {
3108 PyObject
*name
, *value
;
3109 name
= PyList_GET_ITEM(names
, i
);
3110 value
= PyObject_GetAttr(obj
, name
);
3114 int err
= PyDict_SetItem(slots
, name
,
3123 state
= Py_BuildValue("(NO)", state
, slots
);
3130 if (!PyList_Check(obj
)) {
3131 listitems
= Py_None
;
3132 Py_INCREF(listitems
);
3135 listitems
= PyObject_GetIter(obj
);
3136 if (listitems
== NULL
)
3140 if (!PyDict_Check(obj
)) {
3141 dictitems
= Py_None
;
3142 Py_INCREF(dictitems
);
3145 PyObject
*items
= PyObject_CallMethod(obj
, "items", "");
3148 dictitems
= PyObject_GetIter(items
);
3150 if (dictitems
== NULL
)
3154 copyreg
= import_copyreg();
3155 if (copyreg
== NULL
)
3157 newobj
= PyObject_GetAttrString(copyreg
, "__newobj__");
3161 n
= PyTuple_GET_SIZE(args
);
3162 args2
= PyTuple_New(n
+1);
3165 PyTuple_SET_ITEM(args2
, 0, cls
);
3167 for (i
= 0; i
< n
; i
++) {
3168 PyObject
*v
= PyTuple_GET_ITEM(args
, i
);
3170 PyTuple_SET_ITEM(args2
, i
+1, v
);
3173 res
= PyTuple_Pack(5, newobj
, args2
, state
, listitems
, dictitems
);
3182 Py_XDECREF(listitems
);
3183 Py_XDECREF(dictitems
);
3184 Py_XDECREF(copyreg
);
3190 * There were two problems when object.__reduce__ and object.__reduce_ex__
3191 * were implemented in the same function:
3192 * - trying to pickle an object with a custom __reduce__ method that
3193 * fell back to object.__reduce__ in certain circumstances led to
3194 * infinite recursion at Python level and eventual RuntimeError.
3195 * - Pickling objects that lied about their type by overwriting the
3196 * __class__ descriptor could lead to infinite recursion at C level
3197 * and eventual segfault.
3199 * Because of backwards compatibility, the two methods still have to
3200 * behave in the same way, even if this is not required by the pickle
3201 * protocol. This common functionality was moved to the _common_reduce
3205 _common_reduce(PyObject
*self
, int proto
)
3207 PyObject
*copyreg
, *res
;
3210 return reduce_2(self
);
3212 copyreg
= import_copyreg();
3216 res
= PyEval_CallMethod(copyreg
, "_reduce_ex", "(Oi)", self
, proto
);
3223 object_reduce(PyObject
*self
, PyObject
*args
)
3227 if (!PyArg_ParseTuple(args
, "|i:__reduce__", &proto
))
3230 return _common_reduce(self
, proto
);
3234 object_reduce_ex(PyObject
*self
, PyObject
*args
)
3236 PyObject
*reduce
, *res
;
3239 if (!PyArg_ParseTuple(args
, "|i:__reduce_ex__", &proto
))
3242 reduce
= PyObject_GetAttrString(self
, "__reduce__");
3246 PyObject
*cls
, *clsreduce
, *objreduce
;
3248 cls
= PyObject_GetAttrString(self
, "__class__");
3253 clsreduce
= PyObject_GetAttrString(cls
, "__reduce__");
3255 if (clsreduce
== NULL
) {
3259 objreduce
= PyDict_GetItemString(PyBaseObject_Type
.tp_dict
,
3261 override
= (clsreduce
!= objreduce
);
3262 Py_DECREF(clsreduce
);
3264 res
= PyObject_CallObject(reduce
, NULL
);
3272 return _common_reduce(self
, proto
);
3276 object_subclasshook(PyObject
*cls
, PyObject
*args
)
3278 Py_INCREF(Py_NotImplemented
);
3279 return Py_NotImplemented
;
3282 PyDoc_STRVAR(object_subclasshook_doc
,
3283 "Abstract classes can override this to customize issubclass().\n"
3285 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3286 "It should return True, False or NotImplemented. If it returns\n"
3287 "NotImplemented, the normal algorithm is used. Otherwise, it\n"
3288 "overrides the normal algorithm (and the outcome is cached).\n");
3291 from PEP 3101, this code implements:
3294 def __format__(self, format_spec):
3295 return format(str(self), format_spec)
3298 object_format(PyObject
*self
, PyObject
*args
)
3300 PyObject
*format_spec
;
3301 PyObject
*self_as_str
= NULL
;
3302 PyObject
*result
= NULL
;
3303 PyObject
*format_meth
= NULL
;
3305 if (!PyArg_ParseTuple(args
, "U:__format__", &format_spec
))
3308 self_as_str
= PyObject_Str(self
);
3309 if (self_as_str
!= NULL
) {
3310 /* find the format function */
3311 format_meth
= PyObject_GetAttrString(self_as_str
, "__format__");
3312 if (format_meth
!= NULL
) {
3314 result
= PyObject_CallFunctionObjArgs(format_meth
, format_spec
, NULL
);
3318 Py_XDECREF(self_as_str
);
3319 Py_XDECREF(format_meth
);
3325 object_sizeof(PyObject
*self
, PyObject
*args
)
3327 Py_ssize_t res
, isize
;
3330 isize
= self
->ob_type
->tp_itemsize
;
3332 res
= Py_SIZE(self
->ob_type
) * isize
;
3333 res
+= self
->ob_type
->tp_basicsize
;
3335 return PyLong_FromSsize_t(res
);
3338 static PyMethodDef object_methods
[] = {
3339 {"__reduce_ex__", object_reduce_ex
, METH_VARARGS
,
3340 PyDoc_STR("helper for pickle")},
3341 {"__reduce__", object_reduce
, METH_VARARGS
,
3342 PyDoc_STR("helper for pickle")},
3343 {"__subclasshook__", object_subclasshook
, METH_CLASS
| METH_VARARGS
,
3344 object_subclasshook_doc
},
3345 {"__format__", object_format
, METH_VARARGS
,
3346 PyDoc_STR("default object formatter")},
3347 {"__sizeof__", object_sizeof
, METH_NOARGS
,
3348 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
3353 PyTypeObject PyBaseObject_Type
= {
3354 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
3355 "object", /* tp_name */
3356 sizeof(PyObject
), /* tp_basicsize */
3357 0, /* tp_itemsize */
3358 object_dealloc
, /* tp_dealloc */
3362 0, /* tp_reserved */
3363 object_repr
, /* tp_repr */
3364 0, /* tp_as_number */
3365 0, /* tp_as_sequence */
3366 0, /* tp_as_mapping */
3367 (hashfunc
)_Py_HashPointer
, /* tp_hash */
3369 object_str
, /* tp_str */
3370 PyObject_GenericGetAttr
, /* tp_getattro */
3371 PyObject_GenericSetAttr
, /* tp_setattro */
3372 0, /* tp_as_buffer */
3373 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
3374 PyDoc_STR("The most base type"), /* tp_doc */
3375 0, /* tp_traverse */
3377 object_richcompare
, /* tp_richcompare */
3378 0, /* tp_weaklistoffset */
3380 0, /* tp_iternext */
3381 object_methods
, /* tp_methods */
3383 object_getsets
, /* tp_getset */
3386 0, /* tp_descr_get */
3387 0, /* tp_descr_set */
3388 0, /* tp_dictoffset */
3389 object_init
, /* tp_init */
3390 PyType_GenericAlloc
, /* tp_alloc */
3391 object_new
, /* tp_new */
3392 PyObject_Del
, /* tp_free */
3396 /* Add the methods from tp_methods to the __dict__ in a type object */
3399 add_methods(PyTypeObject
*type
, PyMethodDef
*meth
)
3401 PyObject
*dict
= type
->tp_dict
;
3403 for (; meth
->ml_name
!= NULL
; meth
++) {
3405 if (PyDict_GetItemString(dict
, meth
->ml_name
) &&
3406 !(meth
->ml_flags
& METH_COEXIST
))
3408 if (meth
->ml_flags
& METH_CLASS
) {
3409 if (meth
->ml_flags
& METH_STATIC
) {
3410 PyErr_SetString(PyExc_ValueError
,
3411 "method cannot be both class and static");
3414 descr
= PyDescr_NewClassMethod(type
, meth
);
3416 else if (meth
->ml_flags
& METH_STATIC
) {
3417 PyObject
*cfunc
= PyCFunction_New(meth
, NULL
);
3420 descr
= PyStaticMethod_New(cfunc
);
3424 descr
= PyDescr_NewMethod(type
, meth
);
3428 if (PyDict_SetItemString(dict
, meth
->ml_name
, descr
) < 0)
3436 add_members(PyTypeObject
*type
, PyMemberDef
*memb
)
3438 PyObject
*dict
= type
->tp_dict
;
3440 for (; memb
->name
!= NULL
; memb
++) {
3442 if (PyDict_GetItemString(dict
, memb
->name
))
3444 descr
= PyDescr_NewMember(type
, memb
);
3447 if (PyDict_SetItemString(dict
, memb
->name
, descr
) < 0)
3455 add_getset(PyTypeObject
*type
, PyGetSetDef
*gsp
)
3457 PyObject
*dict
= type
->tp_dict
;
3459 for (; gsp
->name
!= NULL
; gsp
++) {
3461 if (PyDict_GetItemString(dict
, gsp
->name
))
3463 descr
= PyDescr_NewGetSet(type
, gsp
);
3467 if (PyDict_SetItemString(dict
, gsp
->name
, descr
) < 0)
3475 inherit_special(PyTypeObject
*type
, PyTypeObject
*base
)
3477 Py_ssize_t oldsize
, newsize
;
3479 /* Copying basicsize is connected to the GC flags */
3480 oldsize
= base
->tp_basicsize
;
3481 newsize
= type
->tp_basicsize
? type
->tp_basicsize
: oldsize
;
3482 if (!(type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3483 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3484 (!type
->tp_traverse
&& !type
->tp_clear
)) {
3485 type
->tp_flags
|= Py_TPFLAGS_HAVE_GC
;
3486 if (type
->tp_traverse
== NULL
)
3487 type
->tp_traverse
= base
->tp_traverse
;
3488 if (type
->tp_clear
== NULL
)
3489 type
->tp_clear
= base
->tp_clear
;
3492 /* The condition below could use some explanation.
3493 It appears that tp_new is not inherited for static types
3494 whose base class is 'object'; this seems to be a precaution
3495 so that old extension types don't suddenly become
3496 callable (object.__new__ wouldn't insure the invariants
3497 that the extension type's own factory function ensures).
3498 Heap types, of course, are under our control, so they do
3499 inherit tp_new; static extension types that specify some
3500 other built-in type as the default are considered
3501 new-style-aware so they also inherit object.__new__. */
3502 if (base
!= &PyBaseObject_Type
||
3503 (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
3504 if (type
->tp_new
== NULL
)
3505 type
->tp_new
= base
->tp_new
;
3508 type
->tp_basicsize
= newsize
;
3510 /* Copy other non-function slots */
3513 #define COPYVAL(SLOT) \
3514 if (type->SLOT == 0) type->SLOT = base->SLOT
3516 COPYVAL(tp_itemsize
);
3517 COPYVAL(tp_weaklistoffset
);
3518 COPYVAL(tp_dictoffset
);
3520 /* Setup fast subclass flags */
3521 if (PyType_IsSubtype(base
, (PyTypeObject
*)PyExc_BaseException
))
3522 type
->tp_flags
|= Py_TPFLAGS_BASE_EXC_SUBCLASS
;
3523 else if (PyType_IsSubtype(base
, &PyType_Type
))
3524 type
->tp_flags
|= Py_TPFLAGS_TYPE_SUBCLASS
;
3525 else if (PyType_IsSubtype(base
, &PyLong_Type
))
3526 type
->tp_flags
|= Py_TPFLAGS_LONG_SUBCLASS
;
3527 else if (PyType_IsSubtype(base
, &PyBytes_Type
))
3528 type
->tp_flags
|= Py_TPFLAGS_BYTES_SUBCLASS
;
3529 else if (PyType_IsSubtype(base
, &PyUnicode_Type
))
3530 type
->tp_flags
|= Py_TPFLAGS_UNICODE_SUBCLASS
;
3531 else if (PyType_IsSubtype(base
, &PyTuple_Type
))
3532 type
->tp_flags
|= Py_TPFLAGS_TUPLE_SUBCLASS
;
3533 else if (PyType_IsSubtype(base
, &PyList_Type
))
3534 type
->tp_flags
|= Py_TPFLAGS_LIST_SUBCLASS
;
3535 else if (PyType_IsSubtype(base
, &PyDict_Type
))
3536 type
->tp_flags
|= Py_TPFLAGS_DICT_SUBCLASS
;
3539 static char *hash_name_op
[] = {
3546 overrides_hash(PyTypeObject
*type
)
3549 PyObject
*dict
= type
->tp_dict
;
3551 assert(dict
!= NULL
);
3552 for (p
= hash_name_op
; *p
; p
++) {
3553 if (PyDict_GetItemString(dict
, *p
) != NULL
)
3560 inherit_slots(PyTypeObject
*type
, PyTypeObject
*base
)
3562 PyTypeObject
*basebase
;
3571 #define SLOTDEFINED(SLOT) \
3572 (base->SLOT != 0 && \
3573 (basebase == NULL || base->SLOT != basebase->SLOT))
3575 #define COPYSLOT(SLOT) \
3576 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3578 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3579 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3580 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3581 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3583 /* This won't inherit indirect slots (from tp_as_number etc.)
3584 if type doesn't provide the space. */
3586 if (type
->tp_as_number
!= NULL
&& base
->tp_as_number
!= NULL
) {
3587 basebase
= base
->tp_base
;
3588 if (basebase
->tp_as_number
== NULL
)
3591 COPYNUM(nb_subtract
);
3592 COPYNUM(nb_multiply
);
3593 COPYNUM(nb_remainder
);
3596 COPYNUM(nb_negative
);
3597 COPYNUM(nb_positive
);
3598 COPYNUM(nb_absolute
);
3608 COPYNUM(nb_inplace_add
);
3609 COPYNUM(nb_inplace_subtract
);
3610 COPYNUM(nb_inplace_multiply
);
3611 COPYNUM(nb_inplace_remainder
);
3612 COPYNUM(nb_inplace_power
);
3613 COPYNUM(nb_inplace_lshift
);
3614 COPYNUM(nb_inplace_rshift
);
3615 COPYNUM(nb_inplace_and
);
3616 COPYNUM(nb_inplace_xor
);
3617 COPYNUM(nb_inplace_or
);
3618 COPYNUM(nb_true_divide
);
3619 COPYNUM(nb_floor_divide
);
3620 COPYNUM(nb_inplace_true_divide
);
3621 COPYNUM(nb_inplace_floor_divide
);
3625 if (type
->tp_as_sequence
!= NULL
&& base
->tp_as_sequence
!= NULL
) {
3626 basebase
= base
->tp_base
;
3627 if (basebase
->tp_as_sequence
== NULL
)
3633 COPYSEQ(sq_ass_item
);
3634 COPYSEQ(sq_contains
);
3635 COPYSEQ(sq_inplace_concat
);
3636 COPYSEQ(sq_inplace_repeat
);
3639 if (type
->tp_as_mapping
!= NULL
&& base
->tp_as_mapping
!= NULL
) {
3640 basebase
= base
->tp_base
;
3641 if (basebase
->tp_as_mapping
== NULL
)
3644 COPYMAP(mp_subscript
);
3645 COPYMAP(mp_ass_subscript
);
3648 if (type
->tp_as_buffer
!= NULL
&& base
->tp_as_buffer
!= NULL
) {
3649 basebase
= base
->tp_base
;
3650 if (basebase
->tp_as_buffer
== NULL
)
3652 COPYBUF(bf_getbuffer
);
3653 COPYBUF(bf_releasebuffer
);
3656 basebase
= base
->tp_base
;
3658 COPYSLOT(tp_dealloc
);
3659 if (type
->tp_getattr
== NULL
&& type
->tp_getattro
== NULL
) {
3660 type
->tp_getattr
= base
->tp_getattr
;
3661 type
->tp_getattro
= base
->tp_getattro
;
3663 if (type
->tp_setattr
== NULL
&& type
->tp_setattro
== NULL
) {
3664 type
->tp_setattr
= base
->tp_setattr
;
3665 type
->tp_setattro
= base
->tp_setattro
;
3667 /* tp_reserved is ignored */
3669 /* tp_hash see tp_richcompare */
3673 /* Copy comparison-related slots only when
3674 not overriding them anywhere */
3675 if (type
->tp_richcompare
== NULL
&&
3676 type
->tp_hash
== NULL
&&
3677 !overrides_hash(type
))
3679 type
->tp_richcompare
= base
->tp_richcompare
;
3680 type
->tp_hash
= base
->tp_hash
;
3685 COPYSLOT(tp_iternext
);
3688 COPYSLOT(tp_descr_get
);
3689 COPYSLOT(tp_descr_set
);
3690 COPYSLOT(tp_dictoffset
);
3694 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) ==
3695 (base
->tp_flags
& Py_TPFLAGS_HAVE_GC
)) {
3696 /* They agree about gc. */
3699 else if ((type
->tp_flags
& Py_TPFLAGS_HAVE_GC
) &&
3700 type
->tp_free
== NULL
&&
3701 base
->tp_free
== PyObject_Free
) {
3702 /* A bit of magic to plug in the correct default
3703 * tp_free function when a derived class adds gc,
3704 * didn't define tp_free, and the base uses the
3705 * default non-gc tp_free.
3707 type
->tp_free
= PyObject_GC_Del
;
3709 /* else they didn't agree about gc, and there isn't something
3710 * obvious to be done -- the type is on its own.
3715 static int add_operators(PyTypeObject
*);
3718 PyType_Ready(PyTypeObject
*type
)
3720 PyObject
*dict
, *bases
;
3724 if (type
->tp_flags
& Py_TPFLAGS_READY
) {
3725 assert(type
->tp_dict
!= NULL
);
3728 assert((type
->tp_flags
& Py_TPFLAGS_READYING
) == 0);
3730 type
->tp_flags
|= Py_TPFLAGS_READYING
;
3732 #ifdef Py_TRACE_REFS
3733 /* PyType_Ready is the closest thing we have to a choke point
3734 * for type objects, so is the best place I can think of to try
3735 * to get type objects into the doubly-linked list of all objects.
3736 * Still, not all type objects go thru PyType_Ready.
3738 _Py_AddToAllObjects((PyObject
*)type
, 0);
3741 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3742 base
= type
->tp_base
;
3743 if (base
== NULL
&& type
!= &PyBaseObject_Type
) {
3744 base
= type
->tp_base
= &PyBaseObject_Type
;
3748 /* Now the only way base can still be NULL is if type is
3749 * &PyBaseObject_Type.
3752 /* Initialize the base class */
3753 if (base
!= NULL
&& base
->tp_dict
== NULL
) {
3754 if (PyType_Ready(base
) < 0)
3758 /* Initialize ob_type if NULL. This means extensions that want to be
3759 compilable separately on Windows can call PyType_Ready() instead of
3760 initializing the ob_type field of their type objects. */
3761 /* The test for base != NULL is really unnecessary, since base is only
3762 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3763 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3765 if (Py_TYPE(type
) == NULL
&& base
!= NULL
)
3766 Py_TYPE(type
) = Py_TYPE(base
);
3768 /* Initialize tp_bases */
3769 bases
= type
->tp_bases
;
3770 if (bases
== NULL
) {
3772 bases
= PyTuple_New(0);
3774 bases
= PyTuple_Pack(1, base
);
3777 type
->tp_bases
= bases
;
3780 /* Initialize tp_dict */
3781 dict
= type
->tp_dict
;
3783 dict
= PyDict_New();
3786 type
->tp_dict
= dict
;
3789 /* Add type-specific descriptors to tp_dict */
3790 if (add_operators(type
) < 0)
3792 if (type
->tp_methods
!= NULL
) {
3793 if (add_methods(type
, type
->tp_methods
) < 0)
3796 if (type
->tp_members
!= NULL
) {
3797 if (add_members(type
, type
->tp_members
) < 0)
3800 if (type
->tp_getset
!= NULL
) {
3801 if (add_getset(type
, type
->tp_getset
) < 0)
3805 /* Calculate method resolution order */
3806 if (mro_internal(type
) < 0) {
3810 /* Inherit special flags from dominant base */
3811 if (type
->tp_base
!= NULL
)
3812 inherit_special(type
, type
->tp_base
);
3814 /* Initialize tp_dict properly */
3815 bases
= type
->tp_mro
;
3816 assert(bases
!= NULL
);
3817 assert(PyTuple_Check(bases
));
3818 n
= PyTuple_GET_SIZE(bases
);
3819 for (i
= 1; i
< n
; i
++) {
3820 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3821 if (PyType_Check(b
))
3822 inherit_slots(type
, (PyTypeObject
*)b
);
3825 /* Sanity check for tp_free. */
3826 if (PyType_IS_GC(type
) && (type
->tp_flags
& Py_TPFLAGS_BASETYPE
) &&
3827 (type
->tp_free
== NULL
|| type
->tp_free
== PyObject_Del
)) {
3828 /* This base class needs to call tp_free, but doesn't have
3829 * one, or its tp_free is for non-gc'ed objects.
3831 PyErr_Format(PyExc_TypeError
, "type '%.100s' participates in "
3832 "gc and is a base type but has inappropriate "
3838 /* if the type dictionary doesn't contain a __doc__, set it from
3841 if (PyDict_GetItemString(type
->tp_dict
, "__doc__") == NULL
) {
3842 if (type
->tp_doc
!= NULL
) {
3843 PyObject
*doc
= PyUnicode_FromString(type
->tp_doc
);
3846 PyDict_SetItemString(type
->tp_dict
, "__doc__", doc
);
3849 PyDict_SetItemString(type
->tp_dict
,
3850 "__doc__", Py_None
);
3854 /* Hack for tp_hash and __hash__.
3855 If after all that, tp_hash is still NULL, and __hash__ is not in
3856 tp_dict, set tp_hash to PyObject_HashNotImplemented and
3857 tp_dict['__hash__'] equal to None.
3858 This signals that __hash__ is not inherited.
3860 if (type
->tp_hash
== NULL
) {
3861 if (PyDict_GetItemString(type
->tp_dict
, "__hash__") == NULL
) {
3862 if (PyDict_SetItemString(type
->tp_dict
, "__hash__", Py_None
) < 0)
3864 type
->tp_hash
= PyObject_HashNotImplemented
;
3868 /* Some more special stuff */
3869 base
= type
->tp_base
;
3871 if (type
->tp_as_number
== NULL
)
3872 type
->tp_as_number
= base
->tp_as_number
;
3873 if (type
->tp_as_sequence
== NULL
)
3874 type
->tp_as_sequence
= base
->tp_as_sequence
;
3875 if (type
->tp_as_mapping
== NULL
)
3876 type
->tp_as_mapping
= base
->tp_as_mapping
;
3877 if (type
->tp_as_buffer
== NULL
)
3878 type
->tp_as_buffer
= base
->tp_as_buffer
;
3881 /* Link into each base class's list of subclasses */
3882 bases
= type
->tp_bases
;
3883 n
= PyTuple_GET_SIZE(bases
);
3884 for (i
= 0; i
< n
; i
++) {
3885 PyObject
*b
= PyTuple_GET_ITEM(bases
, i
);
3886 if (PyType_Check(b
) &&
3887 add_subclass((PyTypeObject
*)b
, type
) < 0)
3891 /* Warn for a type that implements tp_compare (now known as
3892 tp_reserved) but not tp_richcompare. */
3893 if (type
->tp_reserved
&& !type
->tp_richcompare
) {
3896 PyOS_snprintf(msg
, sizeof(msg
),
3897 "Type %.100s defines tp_reserved (formerly "
3898 "tp_compare) but not tp_richcompare. "
3899 "Comparisons may not behave as intended.",
3901 error
= PyErr_WarnEx(PyExc_DeprecationWarning
, msg
, 1);
3906 /* All done -- set the ready flag */
3907 assert(type
->tp_dict
!= NULL
);
3909 (type
->tp_flags
& ~Py_TPFLAGS_READYING
) | Py_TPFLAGS_READY
;
3913 type
->tp_flags
&= ~Py_TPFLAGS_READYING
;
3918 add_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3922 PyObject
*list
, *ref
, *newobj
;
3924 list
= base
->tp_subclasses
;
3926 base
->tp_subclasses
= list
= PyList_New(0);
3930 assert(PyList_Check(list
));
3931 newobj
= PyWeakref_NewRef((PyObject
*)type
, NULL
);
3932 i
= PyList_GET_SIZE(list
);
3934 ref
= PyList_GET_ITEM(list
, i
);
3935 assert(PyWeakref_CheckRef(ref
));
3936 if (PyWeakref_GET_OBJECT(ref
) == Py_None
)
3937 return PyList_SetItem(list
, i
, newobj
);
3939 result
= PyList_Append(list
, newobj
);
3945 remove_subclass(PyTypeObject
*base
, PyTypeObject
*type
)
3948 PyObject
*list
, *ref
;
3950 list
= base
->tp_subclasses
;
3954 assert(PyList_Check(list
));
3955 i
= PyList_GET_SIZE(list
);
3957 ref
= PyList_GET_ITEM(list
, i
);
3958 assert(PyWeakref_CheckRef(ref
));
3959 if (PyWeakref_GET_OBJECT(ref
) == (PyObject
*)type
) {
3960 /* this can't fail, right? */
3961 PySequence_DelItem(list
, i
);
3968 check_num_args(PyObject
*ob
, int n
)
3970 if (!PyTuple_CheckExact(ob
)) {
3971 PyErr_SetString(PyExc_SystemError
,
3972 "PyArg_UnpackTuple() argument list is not a tuple");
3975 if (n
== PyTuple_GET_SIZE(ob
))
3979 "expected %d arguments, got %zd", n
, PyTuple_GET_SIZE(ob
));
3983 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
3985 /* There's a wrapper *function* for each distinct function typedef used
3986 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3987 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3988 Most tables have only one entry; the tables for binary operators have two
3989 entries, one regular and one with reversed arguments. */
3992 wrap_lenfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
3994 lenfunc func
= (lenfunc
)wrapped
;
3997 if (!check_num_args(args
, 0))
3999 res
= (*func
)(self
);
4000 if (res
== -1 && PyErr_Occurred())
4002 return PyLong_FromLong((long)res
);
4006 wrap_inquirypred(PyObject
*self
, PyObject
*args
, void *wrapped
)
4008 inquiry func
= (inquiry
)wrapped
;
4011 if (!check_num_args(args
, 0))
4013 res
= (*func
)(self
);
4014 if (res
== -1 && PyErr_Occurred())
4016 return PyBool_FromLong((long)res
);
4020 wrap_binaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4022 binaryfunc func
= (binaryfunc
)wrapped
;
4025 if (!check_num_args(args
, 1))
4027 other
= PyTuple_GET_ITEM(args
, 0);
4028 return (*func
)(self
, other
);
4032 wrap_binaryfunc_l(PyObject
*self
, PyObject
*args
, void *wrapped
)
4034 binaryfunc func
= (binaryfunc
)wrapped
;
4037 if (!check_num_args(args
, 1))
4039 other
= PyTuple_GET_ITEM(args
, 0);
4040 return (*func
)(self
, other
);
4044 wrap_binaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
4046 binaryfunc func
= (binaryfunc
)wrapped
;
4049 if (!check_num_args(args
, 1))
4051 other
= PyTuple_GET_ITEM(args
, 0);
4052 if (!PyType_IsSubtype(Py_TYPE(other
), Py_TYPE(self
))) {
4053 Py_INCREF(Py_NotImplemented
);
4054 return Py_NotImplemented
;
4056 return (*func
)(other
, self
);
4060 wrap_ternaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4062 ternaryfunc func
= (ternaryfunc
)wrapped
;
4064 PyObject
*third
= Py_None
;
4066 /* Note: This wrapper only works for __pow__() */
4068 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
4070 return (*func
)(self
, other
, third
);
4074 wrap_ternaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
4076 ternaryfunc func
= (ternaryfunc
)wrapped
;
4078 PyObject
*third
= Py_None
;
4080 /* Note: This wrapper only works for __pow__() */
4082 if (!PyArg_UnpackTuple(args
, "", 1, 2, &other
, &third
))
4084 return (*func
)(other
, self
, third
);
4088 wrap_unaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4090 unaryfunc func
= (unaryfunc
)wrapped
;
4092 if (!check_num_args(args
, 0))
4094 return (*func
)(self
);
4098 wrap_indexargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4100 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
4104 if (!PyArg_UnpackTuple(args
, "", 1, 1, &o
))
4106 i
= PyNumber_AsSsize_t(o
, PyExc_OverflowError
);
4107 if (i
== -1 && PyErr_Occurred())
4109 return (*func
)(self
, i
);
4113 getindex(PyObject
*self
, PyObject
*arg
)
4117 i
= PyNumber_AsSsize_t(arg
, PyExc_OverflowError
);
4118 if (i
== -1 && PyErr_Occurred())
4121 PySequenceMethods
*sq
= Py_TYPE(self
)->tp_as_sequence
;
4122 if (sq
&& sq
->sq_length
) {
4123 Py_ssize_t n
= (*sq
->sq_length
)(self
);
4133 wrap_sq_item(PyObject
*self
, PyObject
*args
, void *wrapped
)
4135 ssizeargfunc func
= (ssizeargfunc
)wrapped
;
4139 if (PyTuple_GET_SIZE(args
) == 1) {
4140 arg
= PyTuple_GET_ITEM(args
, 0);
4141 i
= getindex(self
, arg
);
4142 if (i
== -1 && PyErr_Occurred())
4144 return (*func
)(self
, i
);
4146 check_num_args(args
, 1);
4147 assert(PyErr_Occurred());
4152 wrap_sq_setitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
4154 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
4157 PyObject
*arg
, *value
;
4159 if (!PyArg_UnpackTuple(args
, "", 2, 2, &arg
, &value
))
4161 i
= getindex(self
, arg
);
4162 if (i
== -1 && PyErr_Occurred())
4164 res
= (*func
)(self
, i
, value
);
4165 if (res
== -1 && PyErr_Occurred())
4172 wrap_sq_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
4174 ssizeobjargproc func
= (ssizeobjargproc
)wrapped
;
4179 if (!check_num_args(args
, 1))
4181 arg
= PyTuple_GET_ITEM(args
, 0);
4182 i
= getindex(self
, arg
);
4183 if (i
== -1 && PyErr_Occurred())
4185 res
= (*func
)(self
, i
, NULL
);
4186 if (res
== -1 && PyErr_Occurred())
4192 /* XXX objobjproc is a misnomer; should be objargpred */
4194 wrap_objobjproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4196 objobjproc func
= (objobjproc
)wrapped
;
4200 if (!check_num_args(args
, 1))
4202 value
= PyTuple_GET_ITEM(args
, 0);
4203 res
= (*func
)(self
, value
);
4204 if (res
== -1 && PyErr_Occurred())
4207 return PyBool_FromLong(res
);
4211 wrap_objobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4213 objobjargproc func
= (objobjargproc
)wrapped
;
4215 PyObject
*key
, *value
;
4217 if (!PyArg_UnpackTuple(args
, "", 2, 2, &key
, &value
))
4219 res
= (*func
)(self
, key
, value
);
4220 if (res
== -1 && PyErr_Occurred())
4227 wrap_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
4229 objobjargproc func
= (objobjargproc
)wrapped
;
4233 if (!check_num_args(args
, 1))
4235 key
= PyTuple_GET_ITEM(args
, 0);
4236 res
= (*func
)(self
, key
, NULL
);
4237 if (res
== -1 && PyErr_Occurred())
4243 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4244 This is called the Carlo Verre hack after its discoverer. */
4246 hackcheck(PyObject
*self
, setattrofunc func
, char *what
)
4248 PyTypeObject
*type
= Py_TYPE(self
);
4249 while (type
&& type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
4250 type
= type
->tp_base
;
4251 /* If type is NULL now, this is a really weird type.
4252 In the spirit of backwards compatibility (?), just shut up. */
4253 if (type
&& type
->tp_setattro
!= func
) {
4254 PyErr_Format(PyExc_TypeError
,
4255 "can't apply this %s to %s object",
4264 wrap_setattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
4266 setattrofunc func
= (setattrofunc
)wrapped
;
4268 PyObject
*name
, *value
;
4270 if (!PyArg_UnpackTuple(args
, "", 2, 2, &name
, &value
))
4272 if (!hackcheck(self
, func
, "__setattr__"))
4274 res
= (*func
)(self
, name
, value
);
4282 wrap_delattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
4284 setattrofunc func
= (setattrofunc
)wrapped
;
4288 if (!check_num_args(args
, 1))
4290 name
= PyTuple_GET_ITEM(args
, 0);
4291 if (!hackcheck(self
, func
, "__delattr__"))
4293 res
= (*func
)(self
, name
, NULL
);
4301 wrap_hashfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
4303 hashfunc func
= (hashfunc
)wrapped
;
4306 if (!check_num_args(args
, 0))
4308 res
= (*func
)(self
);
4309 if (res
== -1 && PyErr_Occurred())
4311 return PyLong_FromLong(res
);
4315 wrap_call(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4317 ternaryfunc func
= (ternaryfunc
)wrapped
;
4319 return (*func
)(self
, args
, kwds
);
4323 wrap_richcmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
, int op
)
4325 richcmpfunc func
= (richcmpfunc
)wrapped
;
4328 if (!check_num_args(args
, 1))
4330 other
= PyTuple_GET_ITEM(args
, 0);
4331 return (*func
)(self
, other
, op
);
4334 #undef RICHCMP_WRAPPER
4335 #define RICHCMP_WRAPPER(NAME, OP) \
4337 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4339 return wrap_richcmpfunc(self, args, wrapped, OP); \
4342 RICHCMP_WRAPPER(lt
, Py_LT
)
4343 RICHCMP_WRAPPER(le
, Py_LE
)
4344 RICHCMP_WRAPPER(eq
, Py_EQ
)
4345 RICHCMP_WRAPPER(ne
, Py_NE
)
4346 RICHCMP_WRAPPER(gt
, Py_GT
)
4347 RICHCMP_WRAPPER(ge
, Py_GE
)
4350 wrap_next(PyObject
*self
, PyObject
*args
, void *wrapped
)
4352 unaryfunc func
= (unaryfunc
)wrapped
;
4355 if (!check_num_args(args
, 0))
4357 res
= (*func
)(self
);
4358 if (res
== NULL
&& !PyErr_Occurred())
4359 PyErr_SetNone(PyExc_StopIteration
);
4364 wrap_descr_get(PyObject
*self
, PyObject
*args
, void *wrapped
)
4366 descrgetfunc func
= (descrgetfunc
)wrapped
;
4368 PyObject
*type
= NULL
;
4370 if (!PyArg_UnpackTuple(args
, "", 1, 2, &obj
, &type
))
4374 if (type
== Py_None
)
4376 if (type
== NULL
&&obj
== NULL
) {
4377 PyErr_SetString(PyExc_TypeError
,
4378 "__get__(None, None) is invalid");
4381 return (*func
)(self
, obj
, type
);
4385 wrap_descr_set(PyObject
*self
, PyObject
*args
, void *wrapped
)
4387 descrsetfunc func
= (descrsetfunc
)wrapped
;
4388 PyObject
*obj
, *value
;
4391 if (!PyArg_UnpackTuple(args
, "", 2, 2, &obj
, &value
))
4393 ret
= (*func
)(self
, obj
, value
);
4401 wrap_descr_delete(PyObject
*self
, PyObject
*args
, void *wrapped
)
4403 descrsetfunc func
= (descrsetfunc
)wrapped
;
4407 if (!check_num_args(args
, 1))
4409 obj
= PyTuple_GET_ITEM(args
, 0);
4410 ret
= (*func
)(self
, obj
, NULL
);
4418 wrap_init(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwds
)
4420 initproc func
= (initproc
)wrapped
;
4422 if (func(self
, args
, kwds
) < 0)
4429 tp_new_wrapper(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4431 PyTypeObject
*type
, *subtype
, *staticbase
;
4432 PyObject
*arg0
, *res
;
4434 if (self
== NULL
|| !PyType_Check(self
))
4435 Py_FatalError("__new__() called with non-type 'self'");
4436 type
= (PyTypeObject
*)self
;
4437 if (!PyTuple_Check(args
) || PyTuple_GET_SIZE(args
) < 1) {
4438 PyErr_Format(PyExc_TypeError
,
4439 "%s.__new__(): not enough arguments",
4443 arg0
= PyTuple_GET_ITEM(args
, 0);
4444 if (!PyType_Check(arg0
)) {
4445 PyErr_Format(PyExc_TypeError
,
4446 "%s.__new__(X): X is not a type object (%s)",
4448 Py_TYPE(arg0
)->tp_name
);
4451 subtype
= (PyTypeObject
*)arg0
;
4452 if (!PyType_IsSubtype(subtype
, type
)) {
4453 PyErr_Format(PyExc_TypeError
,
4454 "%s.__new__(%s): %s is not a subtype of %s",
4462 /* Check that the use doesn't do something silly and unsafe like
4463 object.__new__(dict). To do this, we check that the
4464 most derived base that's not a heap type is this type. */
4465 staticbase
= subtype
;
4466 while (staticbase
&& (staticbase
->tp_flags
& Py_TPFLAGS_HEAPTYPE
))
4467 staticbase
= staticbase
->tp_base
;
4468 /* If staticbase is NULL now, it is a really weird type.
4469 In the spirit of backwards compatibility (?), just shut up. */
4470 if (staticbase
&& staticbase
->tp_new
!= type
->tp_new
) {
4471 PyErr_Format(PyExc_TypeError
,
4472 "%s.__new__(%s) is not safe, use %s.__new__()",
4475 staticbase
== NULL
? "?" : staticbase
->tp_name
);
4479 args
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
4482 res
= type
->tp_new(subtype
, args
, kwds
);
4487 static struct PyMethodDef tp_new_methoddef
[] = {
4488 {"__new__", (PyCFunction
)tp_new_wrapper
, METH_VARARGS
|METH_KEYWORDS
,
4489 PyDoc_STR("T.__new__(S, ...) -> "
4490 "a new object with type S, a subtype of T")},
4495 add_tp_new_wrapper(PyTypeObject
*type
)
4499 if (PyDict_GetItemString(type
->tp_dict
, "__new__") != NULL
)
4501 func
= PyCFunction_New(tp_new_methoddef
, (PyObject
*)type
);
4504 if (PyDict_SetItemString(type
->tp_dict
, "__new__", func
)) {
4512 /* Slot wrappers that call the corresponding __foo__ slot. See comments
4513 below at override_slots() for more explanation. */
4515 #define SLOT0(FUNCNAME, OPSTR) \
4517 FUNCNAME(PyObject *self) \
4519 static PyObject *cache_str; \
4520 return call_method(self, OPSTR, &cache_str, "()"); \
4523 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4525 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4527 static PyObject *cache_str; \
4528 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4531 /* Boolean helper for SLOT1BINFULL().
4532 right.__class__ is a nontrivial subclass of left.__class__. */
4534 method_is_overloaded(PyObject
*left
, PyObject
*right
, char *name
)
4539 b
= PyObject_GetAttrString((PyObject
*)(Py_TYPE(right
)), name
);
4542 /* If right doesn't have it, it's not overloaded */
4546 a
= PyObject_GetAttrString((PyObject
*)(Py_TYPE(left
)), name
);
4550 /* If right has it but left doesn't, it's overloaded */
4554 ok
= PyObject_RichCompareBool(a
, b
, Py_NE
);
4566 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4568 FUNCNAME(PyObject *self, PyObject *other) \
4570 static PyObject *cache_str, *rcache_str; \
4571 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
4572 Py_TYPE(other)->tp_as_number != NULL && \
4573 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4574 if (Py_TYPE(self)->tp_as_number != NULL && \
4575 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
4578 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
4579 method_is_overloaded(self, other, ROPSTR)) { \
4581 other, ROPSTR, &rcache_str, "(O)", self); \
4582 if (r != Py_NotImplemented) \
4588 self, OPSTR, &cache_str, "(O)", other); \
4589 if (r != Py_NotImplemented || \
4590 Py_TYPE(other) == Py_TYPE(self)) \
4595 return call_maybe( \
4596 other, ROPSTR, &rcache_str, "(O)", self); \
4598 Py_INCREF(Py_NotImplemented); \
4599 return Py_NotImplemented; \
4602 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4603 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4605 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4607 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4609 static PyObject *cache_str; \
4610 return call_method(self, OPSTR, &cache_str, \
4611 "(" ARGCODES ")", arg1, arg2); \
4615 slot_sq_length(PyObject
*self
)
4617 static PyObject
*len_str
;
4618 PyObject
*res
= call_method(self
, "__len__", &len_str
, "()");
4623 len
= PyNumber_AsSsize_t(res
, PyExc_OverflowError
);
4626 if (!PyErr_Occurred())
4627 PyErr_SetString(PyExc_ValueError
,
4628 "__len__() should return >= 0");
4634 /* Super-optimized version of slot_sq_item.
4635 Other slots could do the same... */
4637 slot_sq_item(PyObject
*self
, Py_ssize_t i
)
4639 static PyObject
*getitem_str
;
4640 PyObject
*func
, *args
= NULL
, *ival
= NULL
, *retval
= NULL
;
4643 if (getitem_str
== NULL
) {
4644 getitem_str
= PyUnicode_InternFromString("__getitem__");
4645 if (getitem_str
== NULL
)
4648 func
= _PyType_Lookup(Py_TYPE(self
), getitem_str
);
4650 if ((f
= Py_TYPE(func
)->tp_descr_get
) == NULL
)
4653 func
= f(func
, self
, (PyObject
*)(Py_TYPE(self
)));
4658 ival
= PyLong_FromSsize_t(i
);
4660 args
= PyTuple_New(1);
4662 PyTuple_SET_ITEM(args
, 0, ival
);
4663 retval
= PyObject_Call(func
, args
, NULL
);
4671 PyErr_SetObject(PyExc_AttributeError
, getitem_str
);
4680 slot_sq_ass_item(PyObject
*self
, Py_ssize_t index
, PyObject
*value
)
4683 static PyObject
*delitem_str
, *setitem_str
;
4686 res
= call_method(self
, "__delitem__", &delitem_str
,
4689 res
= call_method(self
, "__setitem__", &setitem_str
,
4690 "(nO)", index
, value
);
4698 slot_sq_contains(PyObject
*self
, PyObject
*value
)
4700 PyObject
*func
, *res
, *args
;
4703 static PyObject
*contains_str
;
4705 func
= lookup_maybe(self
, "__contains__", &contains_str
);
4707 args
= PyTuple_Pack(1, value
);
4711 res
= PyObject_Call(func
, args
, NULL
);
4716 result
= PyObject_IsTrue(res
);
4720 else if (! PyErr_Occurred()) {
4721 /* Possible results: -1 and 1 */
4722 result
= (int)_PySequence_IterSearch(self
, value
,
4723 PY_ITERSEARCH_CONTAINS
);
4728 #define slot_mp_length slot_sq_length
4730 SLOT1(slot_mp_subscript
, "__getitem__", PyObject
*, "O")
4733 slot_mp_ass_subscript(PyObject
*self
, PyObject
*key
, PyObject
*value
)
4736 static PyObject
*delitem_str
, *setitem_str
;
4739 res
= call_method(self
, "__delitem__", &delitem_str
,
4742 res
= call_method(self
, "__setitem__", &setitem_str
,
4743 "(OO)", key
, value
);
4750 SLOT1BIN(slot_nb_add
, nb_add
, "__add__", "__radd__")
4751 SLOT1BIN(slot_nb_subtract
, nb_subtract
, "__sub__", "__rsub__")
4752 SLOT1BIN(slot_nb_multiply
, nb_multiply
, "__mul__", "__rmul__")
4753 SLOT1BIN(slot_nb_remainder
, nb_remainder
, "__mod__", "__rmod__")
4754 SLOT1BIN(slot_nb_divmod
, nb_divmod
, "__divmod__", "__rdivmod__")
4756 static PyObject
*slot_nb_power(PyObject
*, PyObject
*, PyObject
*);
4758 SLOT1BINFULL(slot_nb_power_binary
, slot_nb_power
,
4759 nb_power
, "__pow__", "__rpow__")
4762 slot_nb_power(PyObject
*self
, PyObject
*other
, PyObject
*modulus
)
4764 static PyObject
*pow_str
;
4766 if (modulus
== Py_None
)
4767 return slot_nb_power_binary(self
, other
);
4768 /* Three-arg power doesn't use __rpow__. But ternary_op
4769 can call this when the second argument's type uses
4770 slot_nb_power, so check before calling self.__pow__. */
4771 if (Py_TYPE(self
)->tp_as_number
!= NULL
&&
4772 Py_TYPE(self
)->tp_as_number
->nb_power
== slot_nb_power
) {
4773 return call_method(self
, "__pow__", &pow_str
,
4774 "(OO)", other
, modulus
);
4776 Py_INCREF(Py_NotImplemented
);
4777 return Py_NotImplemented
;
4780 SLOT0(slot_nb_negative
, "__neg__")
4781 SLOT0(slot_nb_positive
, "__pos__")
4782 SLOT0(slot_nb_absolute
, "__abs__")
4785 slot_nb_bool(PyObject
*self
)
4787 PyObject
*func
, *args
;
4788 static PyObject
*bool_str
, *len_str
;
4792 func
= lookup_maybe(self
, "__bool__", &bool_str
);
4794 if (PyErr_Occurred())
4796 func
= lookup_maybe(self
, "__len__", &len_str
);
4798 return PyErr_Occurred() ? -1 : 1;
4801 args
= PyTuple_New(0);
4803 PyObject
*temp
= PyObject_Call(func
, args
, NULL
);
4807 /* enforced by slot_nb_len */
4808 result
= PyObject_IsTrue(temp
);
4810 else if (PyBool_Check(temp
)) {
4811 result
= PyObject_IsTrue(temp
);
4814 PyErr_Format(PyExc_TypeError
,
4815 "__bool__ should return "
4816 "bool, returned %s",
4817 Py_TYPE(temp
)->tp_name
);
4829 slot_nb_index(PyObject
*self
)
4831 static PyObject
*index_str
;
4832 return call_method(self
, "__index__", &index_str
, "()");
4836 SLOT0(slot_nb_invert
, "__invert__")
4837 SLOT1BIN(slot_nb_lshift
, nb_lshift
, "__lshift__", "__rlshift__")
4838 SLOT1BIN(slot_nb_rshift
, nb_rshift
, "__rshift__", "__rrshift__")
4839 SLOT1BIN(slot_nb_and
, nb_and
, "__and__", "__rand__")
4840 SLOT1BIN(slot_nb_xor
, nb_xor
, "__xor__", "__rxor__")
4841 SLOT1BIN(slot_nb_or
, nb_or
, "__or__", "__ror__")
4843 SLOT0(slot_nb_int
, "__int__")
4844 SLOT0(slot_nb_float
, "__float__")
4845 SLOT1(slot_nb_inplace_add
, "__iadd__", PyObject
*, "O")
4846 SLOT1(slot_nb_inplace_subtract
, "__isub__", PyObject
*, "O")
4847 SLOT1(slot_nb_inplace_multiply
, "__imul__", PyObject
*, "O")
4848 SLOT1(slot_nb_inplace_remainder
, "__imod__", PyObject
*, "O")
4849 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
4851 slot_nb_inplace_power(PyObject
*self
, PyObject
* arg1
, PyObject
*arg2
)
4853 static PyObject
*cache_str
;
4854 return call_method(self
, "__ipow__", &cache_str
, "(" "O" ")", arg1
);
4856 SLOT1(slot_nb_inplace_lshift
, "__ilshift__", PyObject
*, "O")
4857 SLOT1(slot_nb_inplace_rshift
, "__irshift__", PyObject
*, "O")
4858 SLOT1(slot_nb_inplace_and
, "__iand__", PyObject
*, "O")
4859 SLOT1(slot_nb_inplace_xor
, "__ixor__", PyObject
*, "O")
4860 SLOT1(slot_nb_inplace_or
, "__ior__", PyObject
*, "O")
4861 SLOT1BIN(slot_nb_floor_divide
, nb_floor_divide
,
4862 "__floordiv__", "__rfloordiv__")
4863 SLOT1BIN(slot_nb_true_divide
, nb_true_divide
, "__truediv__", "__rtruediv__")
4864 SLOT1(slot_nb_inplace_floor_divide
, "__ifloordiv__", PyObject
*, "O")
4865 SLOT1(slot_nb_inplace_true_divide
, "__itruediv__", PyObject
*, "O")
4868 slot_tp_repr(PyObject
*self
)
4870 PyObject
*func
, *res
;
4871 static PyObject
*repr_str
;
4873 func
= lookup_method(self
, "__repr__", &repr_str
);
4875 res
= PyEval_CallObject(func
, NULL
);
4880 return PyUnicode_FromFormat("<%s object at %p>",
4881 Py_TYPE(self
)->tp_name
, self
);
4885 slot_tp_str(PyObject
*self
)
4887 PyObject
*func
, *res
;
4888 static PyObject
*str_str
;
4890 func
= lookup_method(self
, "__str__", &str_str
);
4892 res
= PyEval_CallObject(func
, NULL
);
4899 res
= slot_tp_repr(self
);
4902 ress
= _PyUnicode_AsDefaultEncodedString(res
, NULL
);
4909 slot_tp_hash(PyObject
*self
)
4911 PyObject
*func
, *res
;
4912 static PyObject
*hash_str
;
4915 func
= lookup_method(self
, "__hash__", &hash_str
);
4917 if (func
== Py_None
) {
4923 return PyObject_HashNotImplemented(self
);
4926 res
= PyEval_CallObject(func
, NULL
);
4930 if (PyLong_Check(res
))
4931 h
= PyLong_Type
.tp_hash(res
);
4933 h
= PyLong_AsLong(res
);
4935 if (h
== -1 && !PyErr_Occurred())
4941 slot_tp_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
4943 static PyObject
*call_str
;
4944 PyObject
*meth
= lookup_method(self
, "__call__", &call_str
);
4950 res
= PyObject_Call(meth
, args
, kwds
);
4956 /* There are two slot dispatch functions for tp_getattro.
4958 - slot_tp_getattro() is used when __getattribute__ is overridden
4959 but no __getattr__ hook is present;
4961 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4963 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4964 detects the absence of __getattr__ and then installs the simpler slot if
4968 slot_tp_getattro(PyObject
*self
, PyObject
*name
)
4970 static PyObject
*getattribute_str
= NULL
;
4971 return call_method(self
, "__getattribute__", &getattribute_str
,
4976 call_attribute(PyObject
*self
, PyObject
*attr
, PyObject
*name
)
4978 PyObject
*res
, *descr
= NULL
;
4979 descrgetfunc f
= Py_TYPE(attr
)->tp_descr_get
;
4982 descr
= f(attr
, self
, (PyObject
*)(Py_TYPE(self
)));
4988 res
= PyObject_CallFunctionObjArgs(attr
, name
, NULL
);
4994 slot_tp_getattr_hook(PyObject
*self
, PyObject
*name
)
4996 PyTypeObject
*tp
= Py_TYPE(self
);
4997 PyObject
*getattr
, *getattribute
, *res
;
4998 static PyObject
*getattribute_str
= NULL
;
4999 static PyObject
*getattr_str
= NULL
;
5001 if (getattr_str
== NULL
) {
5002 getattr_str
= PyUnicode_InternFromString("__getattr__");
5003 if (getattr_str
== NULL
)
5006 if (getattribute_str
== NULL
) {
5008 PyUnicode_InternFromString("__getattribute__");
5009 if (getattribute_str
== NULL
)
5012 /* speed hack: we could use lookup_maybe, but that would resolve the
5013 method fully for each attribute lookup for classes with
5014 __getattr__, even when the attribute is present. So we use
5015 _PyType_Lookup and create the method only when needed, with
5017 getattr
= _PyType_Lookup(tp
, getattr_str
);
5018 if (getattr
== NULL
) {
5019 /* No __getattr__ hook: use a simpler dispatcher */
5020 tp
->tp_getattro
= slot_tp_getattro
;
5021 return slot_tp_getattro(self
, name
);
5024 /* speed hack: we could use lookup_maybe, but that would resolve the
5025 method fully for each attribute lookup for classes with
5026 __getattr__, even when self has the default __getattribute__
5027 method. So we use _PyType_Lookup and create the method only when
5028 needed, with call_attribute. */
5029 getattribute
= _PyType_Lookup(tp
, getattribute_str
);
5030 if (getattribute
== NULL
||
5031 (Py_TYPE(getattribute
) == &PyWrapperDescr_Type
&&
5032 ((PyWrapperDescrObject
*)getattribute
)->d_wrapped
==
5033 (void *)PyObject_GenericGetAttr
))
5034 res
= PyObject_GenericGetAttr(self
, name
);
5036 Py_INCREF(getattribute
);
5037 res
= call_attribute(self
, getattribute
, name
);
5038 Py_DECREF(getattribute
);
5040 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
5042 res
= call_attribute(self
, getattr
, name
);
5049 slot_tp_setattro(PyObject
*self
, PyObject
*name
, PyObject
*value
)
5052 static PyObject
*delattr_str
, *setattr_str
;
5055 res
= call_method(self
, "__delattr__", &delattr_str
,
5058 res
= call_method(self
, "__setattr__", &setattr_str
,
5059 "(OO)", name
, value
);
5066 static char *name_op
[] = {
5076 half_richcompare(PyObject
*self
, PyObject
*other
, int op
)
5078 PyObject
*func
, *args
, *res
;
5079 static PyObject
*op_str
[6];
5081 func
= lookup_method(self
, name_op
[op
], &op_str
[op
]);
5084 Py_INCREF(Py_NotImplemented
);
5085 return Py_NotImplemented
;
5087 args
= PyTuple_Pack(1, other
);
5091 res
= PyObject_Call(func
, args
, NULL
);
5099 slot_tp_richcompare(PyObject
*self
, PyObject
*other
, int op
)
5103 if (Py_TYPE(self
)->tp_richcompare
== slot_tp_richcompare
) {
5104 res
= half_richcompare(self
, other
, op
);
5105 if (res
!= Py_NotImplemented
)
5109 if (Py_TYPE(other
)->tp_richcompare
== slot_tp_richcompare
) {
5110 res
= half_richcompare(other
, self
, _Py_SwappedOp
[op
]);
5111 if (res
!= Py_NotImplemented
) {
5116 Py_INCREF(Py_NotImplemented
);
5117 return Py_NotImplemented
;
5121 slot_tp_iter(PyObject
*self
)
5123 PyObject
*func
, *res
;
5124 static PyObject
*iter_str
, *getitem_str
;
5126 func
= lookup_method(self
, "__iter__", &iter_str
);
5129 args
= res
= PyTuple_New(0);
5131 res
= PyObject_Call(func
, args
, NULL
);
5138 func
= lookup_method(self
, "__getitem__", &getitem_str
);
5140 PyErr_Format(PyExc_TypeError
,
5141 "'%.200s' object is not iterable",
5142 Py_TYPE(self
)->tp_name
);
5146 return PySeqIter_New(self
);
5150 slot_tp_iternext(PyObject
*self
)
5152 static PyObject
*next_str
;
5153 return call_method(self
, "__next__", &next_str
, "()");
5157 slot_tp_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
5159 PyTypeObject
*tp
= Py_TYPE(self
);
5161 static PyObject
*get_str
= NULL
;
5163 if (get_str
== NULL
) {
5164 get_str
= PyUnicode_InternFromString("__get__");
5165 if (get_str
== NULL
)
5168 get
= _PyType_Lookup(tp
, get_str
);
5170 /* Avoid further slowdowns */
5171 if (tp
->tp_descr_get
== slot_tp_descr_get
)
5172 tp
->tp_descr_get
= NULL
;
5180 return PyObject_CallFunctionObjArgs(get
, self
, obj
, type
, NULL
);
5184 slot_tp_descr_set(PyObject
*self
, PyObject
*target
, PyObject
*value
)
5187 static PyObject
*del_str
, *set_str
;
5190 res
= call_method(self
, "__delete__", &del_str
,
5193 res
= call_method(self
, "__set__", &set_str
,
5194 "(OO)", target
, value
);
5202 slot_tp_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5204 static PyObject
*init_str
;
5205 PyObject
*meth
= lookup_method(self
, "__init__", &init_str
);
5210 res
= PyObject_Call(meth
, args
, kwds
);
5214 if (res
!= Py_None
) {
5215 PyErr_Format(PyExc_TypeError
,
5216 "__init__() should return None, not '%.200s'",
5217 Py_TYPE(res
)->tp_name
);
5226 slot_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
5228 static PyObject
*new_str
;
5230 PyObject
*newargs
, *x
;
5233 if (new_str
== NULL
) {
5234 new_str
= PyUnicode_InternFromString("__new__");
5235 if (new_str
== NULL
)
5238 func
= PyObject_GetAttr((PyObject
*)type
, new_str
);
5241 assert(PyTuple_Check(args
));
5242 n
= PyTuple_GET_SIZE(args
);
5243 newargs
= PyTuple_New(n
+1);
5244 if (newargs
== NULL
)
5247 PyTuple_SET_ITEM(newargs
, 0, (PyObject
*)type
);
5248 for (i
= 0; i
< n
; i
++) {
5249 x
= PyTuple_GET_ITEM(args
, i
);
5251 PyTuple_SET_ITEM(newargs
, i
+1, x
);
5253 x
= PyObject_Call(func
, newargs
, kwds
);
5260 slot_tp_del(PyObject
*self
)
5262 static PyObject
*del_str
= NULL
;
5263 PyObject
*del
, *res
;
5264 PyObject
*error_type
, *error_value
, *error_traceback
;
5266 /* Temporarily resurrect the object. */
5267 assert(self
->ob_refcnt
== 0);
5268 self
->ob_refcnt
= 1;
5270 /* Save the current exception, if any. */
5271 PyErr_Fetch(&error_type
, &error_value
, &error_traceback
);
5273 /* Execute __del__ method, if any. */
5274 del
= lookup_maybe(self
, "__del__", &del_str
);
5276 res
= PyEval_CallObject(del
, NULL
);
5278 PyErr_WriteUnraisable(del
);
5284 /* Restore the saved exception. */
5285 PyErr_Restore(error_type
, error_value
, error_traceback
);
5287 /* Undo the temporary resurrection; can't use DECREF here, it would
5288 * cause a recursive call.
5290 assert(self
->ob_refcnt
> 0);
5291 if (--self
->ob_refcnt
== 0)
5292 return; /* this is the normal path out */
5294 /* __del__ resurrected it! Make it look like the original Py_DECREF
5298 Py_ssize_t refcnt
= self
->ob_refcnt
;
5299 _Py_NewReference(self
);
5300 self
->ob_refcnt
= refcnt
;
5302 assert(!PyType_IS_GC(Py_TYPE(self
)) ||
5303 _Py_AS_GC(self
)->gc
.gc_refs
!= _PyGC_REFS_UNTRACKED
);
5304 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5305 * we need to undo that. */
5307 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5308 * chain, so no more to do there.
5309 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5310 * _Py_NewReference bumped tp_allocs: both of those need to be
5314 --Py_TYPE(self
)->tp_frees
;
5315 --Py_TYPE(self
)->tp_allocs
;
5320 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
5321 functions. The offsets here are relative to the 'PyHeapTypeObject'
5322 structure, which incorporates the additional structures used for numbers,
5323 sequences and mappings.
5324 Note that multiple names may map to the same slot (e.g. __eq__,
5325 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
5326 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5327 terminated with an all-zero entry. (This table is further initialized and
5328 sorted in init_slotdefs() below.) */
5330 typedef struct wrapperbase slotdef
;
5343 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5344 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5346 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5347 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5348 PyDoc_STR(DOC), FLAGS}
5349 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5350 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5352 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5353 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5354 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5355 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5356 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5357 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5358 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5359 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5360 "x." NAME "() <==> " DOC)
5361 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5362 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5363 "x." NAME "(y) <==> x" DOC "y")
5364 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5365 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5366 "x." NAME "(y) <==> x" DOC "y")
5367 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5368 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5369 "x." NAME "(y) <==> y" DOC "x")
5370 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5371 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5372 "x." NAME "(y) <==> " DOC)
5373 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5374 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5375 "x." NAME "(y) <==> " DOC)
5377 static slotdef slotdefs
[] = {
5378 SQSLOT("__len__", sq_length
, slot_sq_length
, wrap_lenfunc
,
5379 "x.__len__() <==> len(x)"),
5380 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5381 The logic in abstract.c always falls back to nb_add/nb_multiply in
5382 this case. Defining both the nb_* and the sq_* slots to call the
5383 user-defined methods has unexpected side-effects, as shown by
5384 test_descr.notimplemented() */
5385 SQSLOT("__add__", sq_concat
, NULL
, wrap_binaryfunc
,
5386 "x.__add__(y) <==> x+y"),
5387 SQSLOT("__mul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5388 "x.__mul__(n) <==> x*n"),
5389 SQSLOT("__rmul__", sq_repeat
, NULL
, wrap_indexargfunc
,
5390 "x.__rmul__(n) <==> n*x"),
5391 SQSLOT("__getitem__", sq_item
, slot_sq_item
, wrap_sq_item
,
5392 "x.__getitem__(y) <==> x[y]"),
5393 SQSLOT("__setitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_setitem
,
5394 "x.__setitem__(i, y) <==> x[i]=y"),
5395 SQSLOT("__delitem__", sq_ass_item
, slot_sq_ass_item
, wrap_sq_delitem
,
5396 "x.__delitem__(y) <==> del x[y]"),
5397 SQSLOT("__contains__", sq_contains
, slot_sq_contains
, wrap_objobjproc
,
5398 "x.__contains__(y) <==> y in x"),
5399 SQSLOT("__iadd__", sq_inplace_concat
, NULL
,
5400 wrap_binaryfunc
, "x.__iadd__(y) <==> x+=y"),
5401 SQSLOT("__imul__", sq_inplace_repeat
, NULL
,
5402 wrap_indexargfunc
, "x.__imul__(y) <==> x*=y"),
5404 MPSLOT("__len__", mp_length
, slot_mp_length
, wrap_lenfunc
,
5405 "x.__len__() <==> len(x)"),
5406 MPSLOT("__getitem__", mp_subscript
, slot_mp_subscript
,
5408 "x.__getitem__(y) <==> x[y]"),
5409 MPSLOT("__setitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5411 "x.__setitem__(i, y) <==> x[i]=y"),
5412 MPSLOT("__delitem__", mp_ass_subscript
, slot_mp_ass_subscript
,
5414 "x.__delitem__(y) <==> del x[y]"),
5416 BINSLOT("__add__", nb_add
, slot_nb_add
,
5418 RBINSLOT("__radd__", nb_add
, slot_nb_add
,
5420 BINSLOT("__sub__", nb_subtract
, slot_nb_subtract
,
5422 RBINSLOT("__rsub__", nb_subtract
, slot_nb_subtract
,
5424 BINSLOT("__mul__", nb_multiply
, slot_nb_multiply
,
5426 RBINSLOT("__rmul__", nb_multiply
, slot_nb_multiply
,
5428 BINSLOT("__mod__", nb_remainder
, slot_nb_remainder
,
5430 RBINSLOT("__rmod__", nb_remainder
, slot_nb_remainder
,
5432 BINSLOTNOTINFIX("__divmod__", nb_divmod
, slot_nb_divmod
,
5434 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod
, slot_nb_divmod
,
5436 NBSLOT("__pow__", nb_power
, slot_nb_power
, wrap_ternaryfunc
,
5437 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5438 NBSLOT("__rpow__", nb_power
, slot_nb_power
, wrap_ternaryfunc_r
,
5439 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5440 UNSLOT("__neg__", nb_negative
, slot_nb_negative
, wrap_unaryfunc
, "-x"),
5441 UNSLOT("__pos__", nb_positive
, slot_nb_positive
, wrap_unaryfunc
, "+x"),
5442 UNSLOT("__abs__", nb_absolute
, slot_nb_absolute
, wrap_unaryfunc
,
5444 UNSLOT("__bool__", nb_bool
, slot_nb_bool
, wrap_inquirypred
,
5446 UNSLOT("__invert__", nb_invert
, slot_nb_invert
, wrap_unaryfunc
, "~x"),
5447 BINSLOT("__lshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5448 RBINSLOT("__rlshift__", nb_lshift
, slot_nb_lshift
, "<<"),
5449 BINSLOT("__rshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5450 RBINSLOT("__rrshift__", nb_rshift
, slot_nb_rshift
, ">>"),
5451 BINSLOT("__and__", nb_and
, slot_nb_and
, "&"),
5452 RBINSLOT("__rand__", nb_and
, slot_nb_and
, "&"),
5453 BINSLOT("__xor__", nb_xor
, slot_nb_xor
, "^"),
5454 RBINSLOT("__rxor__", nb_xor
, slot_nb_xor
, "^"),
5455 BINSLOT("__or__", nb_or
, slot_nb_or
, "|"),
5456 RBINSLOT("__ror__", nb_or
, slot_nb_or
, "|"),
5457 UNSLOT("__int__", nb_int
, slot_nb_int
, wrap_unaryfunc
,
5459 UNSLOT("__float__", nb_float
, slot_nb_float
, wrap_unaryfunc
,
5461 NBSLOT("__index__", nb_index
, slot_nb_index
, wrap_unaryfunc
,
5462 "x[y:z] <==> x[y.__index__():z.__index__()]"),
5463 IBSLOT("__iadd__", nb_inplace_add
, slot_nb_inplace_add
,
5464 wrap_binaryfunc
, "+"),
5465 IBSLOT("__isub__", nb_inplace_subtract
, slot_nb_inplace_subtract
,
5466 wrap_binaryfunc
, "-"),
5467 IBSLOT("__imul__", nb_inplace_multiply
, slot_nb_inplace_multiply
,
5468 wrap_binaryfunc
, "*"),
5469 IBSLOT("__imod__", nb_inplace_remainder
, slot_nb_inplace_remainder
,
5470 wrap_binaryfunc
, "%"),
5471 IBSLOT("__ipow__", nb_inplace_power
, slot_nb_inplace_power
,
5472 wrap_binaryfunc
, "**"),
5473 IBSLOT("__ilshift__", nb_inplace_lshift
, slot_nb_inplace_lshift
,
5474 wrap_binaryfunc
, "<<"),
5475 IBSLOT("__irshift__", nb_inplace_rshift
, slot_nb_inplace_rshift
,
5476 wrap_binaryfunc
, ">>"),
5477 IBSLOT("__iand__", nb_inplace_and
, slot_nb_inplace_and
,
5478 wrap_binaryfunc
, "&"),
5479 IBSLOT("__ixor__", nb_inplace_xor
, slot_nb_inplace_xor
,
5480 wrap_binaryfunc
, "^"),
5481 IBSLOT("__ior__", nb_inplace_or
, slot_nb_inplace_or
,
5482 wrap_binaryfunc
, "|"),
5483 BINSLOT("__floordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5484 RBINSLOT("__rfloordiv__", nb_floor_divide
, slot_nb_floor_divide
, "//"),
5485 BINSLOT("__truediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5486 RBINSLOT("__rtruediv__", nb_true_divide
, slot_nb_true_divide
, "/"),
5487 IBSLOT("__ifloordiv__", nb_inplace_floor_divide
,
5488 slot_nb_inplace_floor_divide
, wrap_binaryfunc
, "//"),
5489 IBSLOT("__itruediv__", nb_inplace_true_divide
,
5490 slot_nb_inplace_true_divide
, wrap_binaryfunc
, "/"),
5492 TPSLOT("__str__", tp_str
, slot_tp_str
, wrap_unaryfunc
,
5493 "x.__str__() <==> str(x)"),
5494 TPSLOT("__repr__", tp_repr
, slot_tp_repr
, wrap_unaryfunc
,
5495 "x.__repr__() <==> repr(x)"),
5496 TPSLOT("__hash__", tp_hash
, slot_tp_hash
, wrap_hashfunc
,
5497 "x.__hash__() <==> hash(x)"),
5498 FLSLOT("__call__", tp_call
, slot_tp_call
, (wrapperfunc
)wrap_call
,
5499 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS
),
5500 TPSLOT("__getattribute__", tp_getattro
, slot_tp_getattr_hook
,
5501 wrap_binaryfunc
, "x.__getattribute__('name') <==> x.name"),
5502 TPSLOT("__getattribute__", tp_getattr
, NULL
, NULL
, ""),
5503 TPSLOT("__getattr__", tp_getattro
, slot_tp_getattr_hook
, NULL
, ""),
5504 TPSLOT("__getattr__", tp_getattr
, NULL
, NULL
, ""),
5505 TPSLOT("__setattr__", tp_setattro
, slot_tp_setattro
, wrap_setattr
,
5506 "x.__setattr__('name', value) <==> x.name = value"),
5507 TPSLOT("__setattr__", tp_setattr
, NULL
, NULL
, ""),
5508 TPSLOT("__delattr__", tp_setattro
, slot_tp_setattro
, wrap_delattr
,
5509 "x.__delattr__('name') <==> del x.name"),
5510 TPSLOT("__delattr__", tp_setattr
, NULL
, NULL
, ""),
5511 TPSLOT("__lt__", tp_richcompare
, slot_tp_richcompare
, richcmp_lt
,
5512 "x.__lt__(y) <==> x<y"),
5513 TPSLOT("__le__", tp_richcompare
, slot_tp_richcompare
, richcmp_le
,
5514 "x.__le__(y) <==> x<=y"),
5515 TPSLOT("__eq__", tp_richcompare
, slot_tp_richcompare
, richcmp_eq
,
5516 "x.__eq__(y) <==> x==y"),
5517 TPSLOT("__ne__", tp_richcompare
, slot_tp_richcompare
, richcmp_ne
,
5518 "x.__ne__(y) <==> x!=y"),
5519 TPSLOT("__gt__", tp_richcompare
, slot_tp_richcompare
, richcmp_gt
,
5520 "x.__gt__(y) <==> x>y"),
5521 TPSLOT("__ge__", tp_richcompare
, slot_tp_richcompare
, richcmp_ge
,
5522 "x.__ge__(y) <==> x>=y"),
5523 TPSLOT("__iter__", tp_iter
, slot_tp_iter
, wrap_unaryfunc
,
5524 "x.__iter__() <==> iter(x)"),
5525 TPSLOT("__next__", tp_iternext
, slot_tp_iternext
, wrap_next
,
5526 "x.__next__() <==> next(x)"),
5527 TPSLOT("__get__", tp_descr_get
, slot_tp_descr_get
, wrap_descr_get
,
5528 "descr.__get__(obj[, type]) -> value"),
5529 TPSLOT("__set__", tp_descr_set
, slot_tp_descr_set
, wrap_descr_set
,
5530 "descr.__set__(obj, value)"),
5531 TPSLOT("__delete__", tp_descr_set
, slot_tp_descr_set
,
5532 wrap_descr_delete
, "descr.__delete__(obj)"),
5533 FLSLOT("__init__", tp_init
, slot_tp_init
, (wrapperfunc
)wrap_init
,
5534 "x.__init__(...) initializes x; "
5535 "see x.__class__.__doc__ for signature",
5536 PyWrapperFlag_KEYWORDS
),
5537 TPSLOT("__new__", tp_new
, slot_tp_new
, NULL
, ""),
5538 TPSLOT("__del__", tp_del
, slot_tp_del
, NULL
, ""),
5542 /* Given a type pointer and an offset gotten from a slotdef entry, return a
5543 pointer to the actual slot. This is not quite the same as simply adding
5544 the offset to the type pointer, since it takes care to indirect through the
5545 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5546 indirection pointer is NULL. */
5548 slotptr(PyTypeObject
*type
, int ioffset
)
5551 long offset
= ioffset
;
5553 /* Note: this depends on the order of the members of PyHeapTypeObject! */
5554 assert(offset
>= 0);
5555 assert((size_t)offset
< offsetof(PyHeapTypeObject
, as_buffer
));
5556 if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_sequence
)) {
5557 ptr
= (char *)type
->tp_as_sequence
;
5558 offset
-= offsetof(PyHeapTypeObject
, as_sequence
);
5560 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_mapping
)) {
5561 ptr
= (char *)type
->tp_as_mapping
;
5562 offset
-= offsetof(PyHeapTypeObject
, as_mapping
);
5564 else if ((size_t)offset
>= offsetof(PyHeapTypeObject
, as_number
)) {
5565 ptr
= (char *)type
->tp_as_number
;
5566 offset
-= offsetof(PyHeapTypeObject
, as_number
);
5573 return (void **)ptr
;
5576 /* Length of array of slotdef pointers used to store slots with the
5577 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5578 the same __name__, for any __name__. Since that's a static property, it is
5579 appropriate to declare fixed-size arrays for this. */
5580 #define MAX_EQUIV 10
5582 /* Return a slot pointer for a given name, but ONLY if the attribute has
5583 exactly one slot function. The name must be an interned string. */
5585 resolve_slotdups(PyTypeObject
*type
, PyObject
*name
)
5587 /* XXX Maybe this could be optimized more -- but is it worth it? */
5589 /* pname and ptrs act as a little cache */
5590 static PyObject
*pname
;
5591 static slotdef
*ptrs
[MAX_EQUIV
];
5595 if (pname
!= name
) {
5596 /* Collect all slotdefs that match name into ptrs. */
5599 for (p
= slotdefs
; p
->name_strobj
; p
++) {
5600 if (p
->name_strobj
== name
)
5606 /* Look in all matching slots of the type; if exactly one of these has
5607 a filled-in slot, return its value. Otherwise return NULL. */
5609 for (pp
= ptrs
; *pp
; pp
++) {
5610 ptr
= slotptr(type
, (*pp
)->offset
);
5611 if (ptr
== NULL
|| *ptr
== NULL
)
5620 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
5621 does some incredibly complex thinking and then sticks something into the
5622 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5623 interests, and then stores a generic wrapper or a specific function into
5624 the slot.) Return a pointer to the next slotdef with a different offset,
5625 because that's convenient for fixup_slot_dispatchers(). */
5627 update_one_slot(PyTypeObject
*type
, slotdef
*p
)
5630 PyWrapperDescrObject
*d
;
5631 void *generic
= NULL
, *specific
= NULL
;
5632 int use_generic
= 0;
5633 int offset
= p
->offset
;
5634 void **ptr
= slotptr(type
, offset
);
5639 } while (p
->offset
== offset
);
5643 descr
= _PyType_Lookup(type
, p
->name_strobj
);
5644 if (descr
== NULL
) {
5645 if (ptr
== (void**)&type
->tp_iternext
) {
5646 specific
= _PyObject_NextNotImplemented
;
5650 if (Py_TYPE(descr
) == &PyWrapperDescr_Type
) {
5651 void **tptr
= resolve_slotdups(type
, p
->name_strobj
);
5652 if (tptr
== NULL
|| tptr
== ptr
)
5653 generic
= p
->function
;
5654 d
= (PyWrapperDescrObject
*)descr
;
5655 if (d
->d_base
->wrapper
== p
->wrapper
&&
5656 PyType_IsSubtype(type
, d
->d_type
))
5658 if (specific
== NULL
||
5659 specific
== d
->d_wrapped
)
5660 specific
= d
->d_wrapped
;
5665 else if (Py_TYPE(descr
) == &PyCFunction_Type
&&
5666 PyCFunction_GET_FUNCTION(descr
) ==
5667 (PyCFunction
)tp_new_wrapper
&&
5668 ptr
== (void**)&type
->tp_new
)
5670 /* The __new__ wrapper is not a wrapper descriptor,
5671 so must be special-cased differently.
5672 If we don't do this, creating an instance will
5673 always use slot_tp_new which will look up
5674 __new__ in the MRO which will call tp_new_wrapper
5675 which will look through the base classes looking
5676 for a static base and call its tp_new (usually
5677 PyType_GenericNew), after performing various
5678 sanity checks and constructing a new argument
5679 list. Cut all that nonsense short -- this speeds
5680 up instance creation tremendously. */
5681 specific
= (void *)type
->tp_new
;
5682 /* XXX I'm not 100% sure that there isn't a hole
5683 in this reasoning that requires additional
5684 sanity checks. I'll buy the first person to
5685 point out a bug in this reasoning a beer. */
5687 else if (descr
== Py_None
&&
5688 ptr
== (void**)&type
->tp_hash
) {
5689 /* We specifically allow __hash__ to be set to None
5690 to prevent inheritance of the default
5691 implementation from object.__hash__ */
5692 specific
= PyObject_HashNotImplemented
;
5696 generic
= p
->function
;
5698 } while ((++p
)->offset
== offset
);
5699 if (specific
&& !use_generic
)
5706 /* In the type, update the slots whose slotdefs are gathered in the pp array.
5707 This is a callback for update_subclasses(). */
5709 update_slots_callback(PyTypeObject
*type
, void *data
)
5711 slotdef
**pp
= (slotdef
**)data
;
5714 update_one_slot(type
, *pp
);
5718 /* Comparison function for qsort() to compare slotdefs by their offset, and
5719 for equal offset by their address (to force a stable sort). */
5721 slotdef_cmp(const void *aa
, const void *bb
)
5723 const slotdef
*a
= (const slotdef
*)aa
, *b
= (const slotdef
*)bb
;
5724 int c
= a
->offset
- b
->offset
;
5728 /* Cannot use a-b, as this gives off_t,
5729 which may lose precision when converted to int. */
5730 return (a
> b
) ? 1 : (a
< b
) ? -1 : 0;
5733 /* Initialize the slotdefs table by adding interned string objects for the
5734 names and sorting the entries. */
5739 static int initialized
= 0;
5743 for (p
= slotdefs
; p
->name
; p
++) {
5744 p
->name_strobj
= PyUnicode_InternFromString(p
->name
);
5745 if (!p
->name_strobj
)
5746 Py_FatalError("Out of memory interning slotdef names");
5748 qsort((void *)slotdefs
, (size_t)(p
-slotdefs
), sizeof(slotdef
),
5753 /* Update the slots after assignment to a class (type) attribute. */
5755 update_slot(PyTypeObject
*type
, PyObject
*name
)
5757 slotdef
*ptrs
[MAX_EQUIV
];
5762 /* Clear the VALID_VERSION flag of 'type' and all its
5763 subclasses. This could possibly be unified with the
5764 update_subclasses() recursion below, but carefully:
5765 they each have their own conditions on which to stop
5766 recursing into subclasses. */
5767 PyType_Modified(type
);
5771 for (p
= slotdefs
; p
->name
; p
++) {
5772 /* XXX assume name is interned! */
5773 if (p
->name_strobj
== name
)
5777 for (pp
= ptrs
; *pp
; pp
++) {
5780 while (p
> slotdefs
&& (p
-1)->offset
== offset
)
5784 if (ptrs
[0] == NULL
)
5785 return 0; /* Not an attribute that affects any slots */
5786 return update_subclasses(type
, name
,
5787 update_slots_callback
, (void *)ptrs
);
5790 /* Store the proper functions in the slot dispatches at class (type)
5791 definition time, based upon which operations the class overrides in its
5794 fixup_slot_dispatchers(PyTypeObject
*type
)
5799 for (p
= slotdefs
; p
->name
; )
5800 p
= update_one_slot(type
, p
);
5804 update_all_slots(PyTypeObject
* type
)
5809 for (p
= slotdefs
; p
->name
; p
++) {
5810 /* update_slot returns int but can't actually fail */
5811 update_slot(type
, p
->name_strobj
);
5815 /* recurse_down_subclasses() and update_subclasses() are mutually
5816 recursive functions to call a callback for all subclasses,
5817 but refraining from recursing into subclasses that define 'name'. */
5820 update_subclasses(PyTypeObject
*type
, PyObject
*name
,
5821 update_callback callback
, void *data
)
5823 if (callback(type
, data
) < 0)
5825 return recurse_down_subclasses(type
, name
, callback
, data
);
5829 recurse_down_subclasses(PyTypeObject
*type
, PyObject
*name
,
5830 update_callback callback
, void *data
)
5832 PyTypeObject
*subclass
;
5833 PyObject
*ref
, *subclasses
, *dict
;
5836 subclasses
= type
->tp_subclasses
;
5837 if (subclasses
== NULL
)
5839 assert(PyList_Check(subclasses
));
5840 n
= PyList_GET_SIZE(subclasses
);
5841 for (i
= 0; i
< n
; i
++) {
5842 ref
= PyList_GET_ITEM(subclasses
, i
);
5843 assert(PyWeakref_CheckRef(ref
));
5844 subclass
= (PyTypeObject
*)PyWeakref_GET_OBJECT(ref
);
5845 assert(subclass
!= NULL
);
5846 if ((PyObject
*)subclass
== Py_None
)
5848 assert(PyType_Check(subclass
));
5849 /* Avoid recursing down into unaffected classes */
5850 dict
= subclass
->tp_dict
;
5851 if (dict
!= NULL
&& PyDict_Check(dict
) &&
5852 PyDict_GetItem(dict
, name
) != NULL
)
5854 if (update_subclasses(subclass
, name
, callback
, data
) < 0)
5860 /* This function is called by PyType_Ready() to populate the type's
5861 dictionary with method descriptors for function slots. For each
5862 function slot (like tp_repr) that's defined in the type, one or more
5863 corresponding descriptors are added in the type's tp_dict dictionary
5864 under the appropriate name (like __repr__). Some function slots
5865 cause more than one descriptor to be added (for example, the nb_add
5866 slot adds both __add__ and __radd__ descriptors) and some function
5867 slots compete for the same descriptor (for example both sq_item and
5868 mp_subscript generate a __getitem__ descriptor).
5870 In the latter case, the first slotdef entry encoutered wins. Since
5871 slotdef entries are sorted by the offset of the slot in the
5872 PyHeapTypeObject, this gives us some control over disambiguating
5873 between competing slots: the members of PyHeapTypeObject are listed
5874 from most general to least general, so the most general slot is
5875 preferred. In particular, because as_mapping comes before as_sequence,
5876 for a type that defines both mp_subscript and sq_item, mp_subscript
5879 This only adds new descriptors and doesn't overwrite entries in
5880 tp_dict that were previously defined. The descriptors contain a
5881 reference to the C function they must call, so that it's safe if they
5882 are copied into a subtype's __dict__ and the subtype has a different
5883 C function in its slot -- calling the method defined by the
5884 descriptor will call the C function that was used to create it,
5885 rather than the C function present in the slot when it is called.
5886 (This is important because a subtype may have a C function in the
5887 slot that calls the method from the dictionary, and we want to avoid
5888 infinite recursion here.) */
5891 add_operators(PyTypeObject
*type
)
5893 PyObject
*dict
= type
->tp_dict
;
5899 for (p
= slotdefs
; p
->name
; p
++) {
5900 if (p
->wrapper
== NULL
)
5902 ptr
= slotptr(type
, p
->offset
);
5905 if (PyDict_GetItem(dict
, p
->name_strobj
))
5907 if (*ptr
== PyObject_HashNotImplemented
) {
5908 /* Classes may prevent the inheritance of the tp_hash
5909 slot by storing PyObject_HashNotImplemented in it. Make it
5910 visible as a None value for the __hash__ attribute. */
5911 if (PyDict_SetItem(dict
, p
->name_strobj
, Py_None
) < 0)
5915 descr
= PyDescr_NewWrapper(type
, p
, *ptr
);
5918 if (PyDict_SetItem(dict
, p
->name_strobj
, descr
) < 0)
5923 if (type
->tp_new
!= NULL
) {
5924 if (add_tp_new_wrapper(type
) < 0)
5931 /* Cooperative 'super' */
5937 PyTypeObject
*obj_type
;
5940 static PyMemberDef super_members
[] = {
5941 {"__thisclass__", T_OBJECT
, offsetof(superobject
, type
), READONLY
,
5942 "the class invoking super()"},
5943 {"__self__", T_OBJECT
, offsetof(superobject
, obj
), READONLY
,
5944 "the instance invoking super(); may be None"},
5945 {"__self_class__", T_OBJECT
, offsetof(superobject
, obj_type
), READONLY
,
5946 "the type of the instance invoking super(); may be None"},
5951 super_dealloc(PyObject
*self
)
5953 superobject
*su
= (superobject
*)self
;
5955 _PyObject_GC_UNTRACK(self
);
5956 Py_XDECREF(su
->obj
);
5957 Py_XDECREF(su
->type
);
5958 Py_XDECREF(su
->obj_type
);
5959 Py_TYPE(self
)->tp_free(self
);
5963 super_repr(PyObject
*self
)
5965 superobject
*su
= (superobject
*)self
;
5968 return PyUnicode_FromFormat(
5969 "<super: <class '%s'>, <%s object>>",
5970 su
->type
? su
->type
->tp_name
: "NULL",
5971 su
->obj_type
->tp_name
);
5973 return PyUnicode_FromFormat(
5974 "<super: <class '%s'>, NULL>",
5975 su
->type
? su
->type
->tp_name
: "NULL");
5979 super_getattro(PyObject
*self
, PyObject
*name
)
5981 superobject
*su
= (superobject
*)self
;
5982 int skip
= su
->obj_type
== NULL
;
5985 /* We want __class__ to return the class of the super object
5986 (i.e. super, or a subclass), not the class of su->obj. */
5987 skip
= (PyUnicode_Check(name
) &&
5988 PyUnicode_GET_SIZE(name
) == 9 &&
5989 PyUnicode_CompareWithASCIIString(name
, "__class__") == 0);
5993 PyObject
*mro
, *res
, *tmp
, *dict
;
5994 PyTypeObject
*starttype
;
5998 starttype
= su
->obj_type
;
5999 mro
= starttype
->tp_mro
;
6004 assert(PyTuple_Check(mro
));
6005 n
= PyTuple_GET_SIZE(mro
);
6007 for (i
= 0; i
< n
; i
++) {
6008 if ((PyObject
*)(su
->type
) == PyTuple_GET_ITEM(mro
, i
))
6013 for (; i
< n
; i
++) {
6014 tmp
= PyTuple_GET_ITEM(mro
, i
);
6015 if (PyType_Check(tmp
))
6016 dict
= ((PyTypeObject
*)tmp
)->tp_dict
;
6019 res
= PyDict_GetItem(dict
, name
);
6022 f
= Py_TYPE(res
)->tp_descr_get
;
6025 /* Only pass 'obj' param if
6026 this is instance-mode super
6029 (su
->obj
== (PyObject
*)
6033 (PyObject
*)starttype
);
6041 return PyObject_GenericGetAttr(self
, name
);
6044 static PyTypeObject
*
6045 supercheck(PyTypeObject
*type
, PyObject
*obj
)
6047 /* Check that a super() call makes sense. Return a type object.
6049 obj can be a new-style class, or an instance of one:
6051 - If it is a class, it must be a subclass of 'type'. This case is
6052 used for class methods; the return value is obj.
6054 - If it is an instance, it must be an instance of 'type'. This is
6055 the normal case; the return value is obj.__class__.
6057 But... when obj is an instance, we want to allow for the case where
6058 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
6059 This will allow using super() with a proxy for obj.
6062 /* Check for first bullet above (special case) */
6063 if (PyType_Check(obj
) && PyType_IsSubtype((PyTypeObject
*)obj
, type
)) {
6065 return (PyTypeObject
*)obj
;
6069 if (PyType_IsSubtype(Py_TYPE(obj
), type
)) {
6070 Py_INCREF(Py_TYPE(obj
));
6071 return Py_TYPE(obj
);
6074 /* Try the slow way */
6075 static PyObject
*class_str
= NULL
;
6076 PyObject
*class_attr
;
6078 if (class_str
== NULL
) {
6079 class_str
= PyUnicode_FromString("__class__");
6080 if (class_str
== NULL
)
6084 class_attr
= PyObject_GetAttr(obj
, class_str
);
6086 if (class_attr
!= NULL
&&
6087 PyType_Check(class_attr
) &&
6088 (PyTypeObject
*)class_attr
!= Py_TYPE(obj
))
6090 int ok
= PyType_IsSubtype(
6091 (PyTypeObject
*)class_attr
, type
);
6093 return (PyTypeObject
*)class_attr
;
6096 if (class_attr
== NULL
)
6099 Py_DECREF(class_attr
);
6102 PyErr_SetString(PyExc_TypeError
,
6103 "super(type, obj): "
6104 "obj must be an instance or subtype of type");
6109 super_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
6111 superobject
*su
= (superobject
*)self
;
6112 superobject
*newobj
;
6114 if (obj
== NULL
|| obj
== Py_None
|| su
->obj
!= NULL
) {
6115 /* Not binding to an object, or already bound */
6119 if (Py_TYPE(su
) != &PySuper_Type
)
6120 /* If su is an instance of a (strict) subclass of super,
6122 return PyObject_CallFunctionObjArgs((PyObject
*)Py_TYPE(su
),
6123 su
->type
, obj
, NULL
);
6125 /* Inline the common case */
6126 PyTypeObject
*obj_type
= supercheck(su
->type
, obj
);
6127 if (obj_type
== NULL
)
6129 newobj
= (superobject
*)PySuper_Type
.tp_new(&PySuper_Type
,
6133 Py_INCREF(su
->type
);
6135 newobj
->type
= su
->type
;
6137 newobj
->obj_type
= obj_type
;
6138 return (PyObject
*)newobj
;
6143 super_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
6145 superobject
*su
= (superobject
*)self
;
6146 PyTypeObject
*type
= NULL
;
6147 PyObject
*obj
= NULL
;
6148 PyTypeObject
*obj_type
= NULL
;
6150 if (!_PyArg_NoKeywords("super", kwds
))
6152 if (!PyArg_ParseTuple(args
, "|O!O:super", &PyType_Type
, &type
, &obj
))
6156 /* Call super(), without args -- fill in from __class__
6157 and first local variable on the stack. */
6158 PyFrameObject
*f
= PyThreadState_GET()->frame
;
6159 PyCodeObject
*co
= f
->f_code
;
6162 PyErr_SetString(PyExc_SystemError
,
6163 "super(): no code object");
6166 if (co
->co_argcount
== 0) {
6167 PyErr_SetString(PyExc_SystemError
,
6168 "super(): no arguments");
6171 obj
= f
->f_localsplus
[0];
6173 PyErr_SetString(PyExc_SystemError
,
6174 "super(): arg[0] deleted");
6177 if (co
->co_freevars
== NULL
)
6180 assert(PyTuple_Check(co
->co_freevars
));
6181 n
= PyTuple_GET_SIZE(co
->co_freevars
);
6183 for (i
= 0; i
< n
; i
++) {
6184 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
6185 assert(PyUnicode_Check(name
));
6186 if (!PyUnicode_CompareWithASCIIString(name
,
6188 Py_ssize_t index
= co
->co_nlocals
+
6189 PyTuple_GET_SIZE(co
->co_cellvars
) + i
;
6190 PyObject
*cell
= f
->f_localsplus
[index
];
6191 if (cell
== NULL
|| !PyCell_Check(cell
)) {
6192 PyErr_SetString(PyExc_SystemError
,
6193 "super(): bad __class__ cell");
6196 type
= (PyTypeObject
*) PyCell_GET(cell
);
6198 PyErr_SetString(PyExc_SystemError
,
6199 "super(): empty __class__ cell");
6202 if (!PyType_Check(type
)) {
6203 PyErr_Format(PyExc_SystemError
,
6204 "super(): __class__ is not a type (%s)",
6205 Py_TYPE(type
)->tp_name
);
6212 PyErr_SetString(PyExc_SystemError
,
6213 "super(): __class__ cell not found");
6221 obj_type
= supercheck(type
, obj
);
6222 if (obj_type
== NULL
)
6229 su
->obj_type
= obj_type
;
6233 PyDoc_STRVAR(super_doc
,
6234 "super() -> same as super(__class__, <first argument>)\n"
6235 "super(type) -> unbound super object\n"
6236 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
6237 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
6238 "Typical use to call a cooperative superclass method:\n"
6240 " def meth(self, arg):\n"
6241 " super().meth(arg)\n"
6242 "This works for class methods too:\n"
6245 " def cmeth(cls, arg):\n"
6246 " super().cmeth(arg)\n");
6249 super_traverse(PyObject
*self
, visitproc visit
, void *arg
)
6251 superobject
*su
= (superobject
*)self
;
6255 Py_VISIT(su
->obj_type
);
6260 PyTypeObject PySuper_Type
= {
6261 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
6262 "super", /* tp_name */
6263 sizeof(superobject
), /* tp_basicsize */
6264 0, /* tp_itemsize */
6266 super_dealloc
, /* tp_dealloc */
6270 0, /* tp_reserved */
6271 super_repr
, /* tp_repr */
6272 0, /* tp_as_number */
6273 0, /* tp_as_sequence */
6274 0, /* tp_as_mapping */
6278 super_getattro
, /* tp_getattro */
6279 0, /* tp_setattro */
6280 0, /* tp_as_buffer */
6281 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
6282 Py_TPFLAGS_BASETYPE
, /* tp_flags */
6283 super_doc
, /* tp_doc */
6284 super_traverse
, /* tp_traverse */
6286 0, /* tp_richcompare */
6287 0, /* tp_weaklistoffset */
6289 0, /* tp_iternext */
6291 super_members
, /* tp_members */
6295 super_descr_get
, /* tp_descr_get */
6296 0, /* tp_descr_set */
6297 0, /* tp_dictoffset */
6298 super_init
, /* tp_init */
6299 PyType_GenericAlloc
, /* tp_alloc */
6300 PyType_GenericNew
, /* tp_new */
6301 PyObject_GC_Del
, /* tp_free */