]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44531: Fix type_repr() if tp_name is NULL (GH-26948)
authorVictor Stinner <vstinner@python.org>
Tue, 29 Jun 2021 14:39:29 +0000 (16:39 +0200)
committerGitHub <noreply@github.com>
Tue, 29 Jun 2021 14:39:29 +0000 (16:39 +0200)
Allow to call type_repr() on a type which is not fully initialized
yet. This fix helps debugging crashes occurring early at Python
initialization.

Objects/typeobject.c

index 3c766e9230e2b668a50b73663a60065eceda444a..8ee4e813ee5213477876daa6da0eb92e64da49db 100644 (file)
@@ -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);