]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46417: Clear Unicode static types at exit (GH-30806)
authorVictor Stinner <vstinner@python.org>
Sat, 22 Jan 2022 21:55:39 +0000 (22:55 +0100)
committerGitHub <noreply@github.com>
Sat, 22 Jan 2022 21:55:39 +0000 (22:55 +0100)
Add _PyUnicode_FiniTypes() function, called by
finalize_interp_types(). It clears these static types:

* EncodingMapType
* PyFieldNameIter_Type
* PyFormatterIter_Type

_PyStaticType_Dealloc() now does nothing if tp_subclasses
is not NULL.

Include/internal/pycore_unicodeobject.h
Objects/exceptions.c
Objects/object.c
Objects/typeobject.c
Objects/unicodeobject.c
Python/pylifecycle.c

index 3b6dfe9dbbab490d37daa17b4551ff586f74fcd7..fabe522f6fc23bc4f3e4e97e9f29307bda2c7577 100644 (file)
@@ -17,6 +17,7 @@ extern void _PyUnicode_InitState(PyInterpreterState *);
 extern PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *);
 extern PyStatus _PyUnicode_InitTypes(PyInterpreterState *);
 extern void _PyUnicode_Fini(PyInterpreterState *);
+extern void _PyUnicode_FiniTypes(PyInterpreterState *);
 
 
 /* other API */
index 6bf70e2b15c16d352c063d56104aa05dcd0da10f..065503f59d62d4f2c9cf33dc996f9a9b33cd1930 100644 (file)
@@ -3545,12 +3545,6 @@ _PyExc_FiniTypes(PyInterpreterState *interp)
 
     for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
         PyTypeObject *exc = static_exceptions[i].exc;
-
-        // Cannot delete a type if it still has subclasses
-        if (exc->tp_subclasses != NULL) {
-            continue;
-        }
-
         _PyStaticType_Dealloc(exc);
     }
 }
index 27f89e8d75212318205c75aba07857510c4a11b8..3082e70e7e230b6380b4fdc78a16bddc8b10f09c 100644 (file)
@@ -1994,10 +1994,6 @@ _PyTypes_FiniTypes(PyInterpreterState *interp)
     // their base classes.
     for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) {
         PyTypeObject *type = static_types[i];
-        // Cannot delete a type if it still has subclasses
-        if (type->tp_subclasses != NULL) {
-            continue;
-        }
         _PyStaticType_Dealloc(type);
     }
 }
index bf62b5389257f12334185e54d2e58790bab2263a..cc4612f9308d019596a3b5b665c972ac92eb75ac 100644 (file)
@@ -4079,10 +4079,12 @@ type_dealloc_common(PyTypeObject *type)
 void
 _PyStaticType_Dealloc(PyTypeObject *type)
 {
-    // _PyStaticType_Dealloc() must not be called if a type has subtypes.
+    // If a type still has subtypes, it cannot be deallocated.
     // A subtype can inherit attributes and methods of its parent type,
     // and a type must no longer be used once it's deallocated.
-    assert(type->tp_subclasses == NULL);
+    if (type->tp_subclasses != NULL) {
+        return;
+    }
 
     type_dealloc_common(type);
 
index 2e1f8a6ac4e5657404c47577a0ba6913cc505e92..4cea0d8e62e858fc5f7ef6d136ed895bb98b84d7 100644 (file)
@@ -15567,23 +15567,19 @@ _PyUnicode_InitTypes(PyInterpreterState *interp)
         return _PyStatus_OK();
     }
 
-    if (PyType_Ready(&PyUnicode_Type) < 0) {
-        return _PyStatus_ERR("Can't initialize unicode type");
-    }
-    if (PyType_Ready(&PyUnicodeIter_Type) < 0) {
-        return _PyStatus_ERR("Can't initialize unicode iterator type");
-    }
-
     if (PyType_Ready(&EncodingMapType) < 0) {
-         return _PyStatus_ERR("Can't initialize encoding map type");
+        goto error;
     }
     if (PyType_Ready(&PyFieldNameIter_Type) < 0) {
-        return _PyStatus_ERR("Can't initialize field name iterator type");
+        goto error;
     }
     if (PyType_Ready(&PyFormatterIter_Type) < 0) {
-        return _PyStatus_ERR("Can't initialize formatter iter type");
+        goto error;
     }
     return _PyStatus_OK();
+
+error:
+    return _PyStatus_ERR("Can't initialize unicode types");
 }
 
 
@@ -16111,6 +16107,19 @@ unicode_is_finalizing(void)
 #endif
 
 
+void
+_PyUnicode_FiniTypes(PyInterpreterState *interp)
+{
+    if (!_Py_IsMainInterpreter(interp)) {
+        return;
+    }
+
+    _PyStaticType_Dealloc(&EncodingMapType);
+    _PyStaticType_Dealloc(&PyFieldNameIter_Type);
+    _PyStaticType_Dealloc(&PyFormatterIter_Type);
+}
+
+
 void
 _PyUnicode_Fini(PyInterpreterState *interp)
 {
index aca3b1a5fd1a4d4ea0ea8a237f31fe2d1822834b..7fc9d3c94ce51714f8cf600549d4c5d12d37744d 100644 (file)
@@ -1664,6 +1664,7 @@ flush_std_files(void)
 static void
 finalize_interp_types(PyInterpreterState *interp)
 {
+    _PyUnicode_FiniTypes(interp);
     _PySys_Fini(interp);
     _PyExc_Fini(interp);
     _PyFrame_Fini(interp);