summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-11 13:27:32 (GMT)
committerGitHub <noreply@github.com>2020-11-11 13:27:32 (GMT)
commitba2958ed40d284228836735cbed4a155190e0998 (patch)
tree8266560eea9bd2d980e9f19196703d0dfcfb8513 /Objects
parentf9a8386e44a695551a1e54e709969e90e9b96bc4 (diff)
downloadcpython-ba2958ed40d284228836735cbed4a155190e0998.zip
cpython-ba2958ed40d284228836735cbed4a155190e0998.tar.gz
cpython-ba2958ed40d284228836735cbed4a155190e0998.tar.bz2
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).
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 4d0a3fa..fd018b8 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -5487,8 +5487,13 @@ PyType_Ready(PyTypeObject *type)
/* Initialize tp_base (defaults to BaseObject unless that's us) */
base = type->tp_base;
if (base == NULL && type != &PyBaseObject_Type) {
- base = type->tp_base = &PyBaseObject_Type;
- Py_INCREF(base);
+ base = &PyBaseObject_Type;
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+ type->tp_base = (PyTypeObject*)Py_NewRef((PyObject*)base);
+ }
+ else {
+ type->tp_base = base;
+ }
}
/* Now the only way base can still be NULL is if type is