[Python-checkins] bpo-44531: Fix type_repr() if tp_name is NULL (GH-26948)

vstinner webhook-mailer at python.org
Tue Jun 29 10:39:39 EDT 2021


https://github.com/python/cpython/commit/823460daa9fab3d0cf00ec553d1e35635ef73d40
commit: 823460daa9fab3d0cf00ec553d1e35635ef73d40
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-06-29T16:39:29+02:00
summary:

bpo-44531: Fix type_repr() if tp_name is NULL (GH-26948)

Allow to call type_repr() on a type which is not fully initialized
yet. This fix helps debugging crashes occurring early at Python
initialization.

files:
M Objects/typeobject.c

diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 3c766e9230e2b..8ee4e813ee521 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1065,6 +1065,12 @@ static PyGetSetDef type_getsets[] = {
 static PyObject *
 type_repr(PyTypeObject *type)
 {
+    if (type->tp_name == NULL) {
+        // type_repr() called before the type is fully initialized
+        // by PyType_Ready().
+        return PyUnicode_FromFormat("<class at %p>", type);
+    }
+
     PyObject *mod, *name, *rtn;
 
     mod = type_module(type, NULL);



More information about the Python-checkins mailing list