Skip to content

Commit ba2958e

Browse files
authored
bpo-40170: Fix PyType_Ready() refleak on static type (GH-23236)
bpo-1635741, bpo-40170: When called on a static type with NULL tp_base, PyType_Ready() no longer increments the reference count of the PyBaseObject_Type ("object). PyTypeObject.tp_base is a strong reference on a heap type, but it is borrowed reference on a static type. Fix 99 reference leaks at Python exit (showrefcount 18623 => 18524).
1 parent f9a8386 commit ba2958e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Include/cpython/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ struct _typeobject {
244244
struct PyMethodDef *tp_methods;
245245
struct PyMemberDef *tp_members;
246246
struct PyGetSetDef *tp_getset;
247+
// Strong reference on a heap type, borrowed reference on a static type
247248
struct _typeobject *tp_base;
248249
PyObject *tp_dict;
249250
descrgetfunc tp_descr_get;

Objects/typeobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5487,8 +5487,13 @@ PyType_Ready(PyTypeObject *type)
54875487
/* Initialize tp_base (defaults to BaseObject unless that's us) */
54885488
base = type->tp_base;
54895489
if (base == NULL && type != &PyBaseObject_Type) {
5490-
base = type->tp_base = &PyBaseObject_Type;
5491-
Py_INCREF(base);
5490+
base = &PyBaseObject_Type;
5491+
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
5492+
type->tp_base = (PyTypeObject*)Py_NewRef((PyObject*)base);
5493+
}
5494+
else {
5495+
type->tp_base = base;
5496+
}
54925497
}
54935498

54945499
/* Now the only way base can still be NULL is if type is

0 commit comments

Comments
 (0)