Skip to content

Commit f884b74

Browse files
committed
- PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
NULL, so that you can call PyType_Ready() to initialize a type that is to be separately compiled with C on Windows. inherit_special(): Add a long comment explaining that you have to set tp_new if your base class is PyBaseObject_Type.
1 parent facf24b commit f884b74

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Objects/typeobject.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,16 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
17471747
type->tp_clear = base->tp_clear;
17481748
}
17491749
if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1750+
/* The condition below could use some explanation.
1751+
It appears that tp_new is not inherited for static types
1752+
whose base class is 'object'; this seems to be a precaution
1753+
so that old extension types don't suddenly become
1754+
callable (object.__new__ wouldn't insure the invariants
1755+
that the extension type's own factory function ensures).
1756+
Heap types, of course, are under our control, so they do
1757+
inherit tp_new; static extension types that specify some
1758+
other built-in type as the default are considered
1759+
new-style-aware so they also inherit object.__new__. */
17501760
if (base != &PyBaseObject_Type ||
17511761
(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
17521762
if (type->tp_new == NULL)
@@ -1940,6 +1950,12 @@ PyType_Ready(PyTypeObject *type)
19401950

19411951
type->tp_flags |= Py_TPFLAGS_READYING;
19421952

1953+
/* Initialize ob_type if NULL. This means extensions that want to be
1954+
compilable separately on Windows can call PyType_Ready() instead of
1955+
initializing the ob_type field of their type objects. */
1956+
if (type->ob_type == NULL)
1957+
type->ob_type = &PyType_Type;
1958+
19431959
/* Initialize tp_base (defaults to BaseObject unless that's us) */
19441960
base = type->tp_base;
19451961
if (base == NULL && type != &PyBaseObject_Type)

0 commit comments

Comments
 (0)