From e9e3eab0b868c7d0b48e472705024240d5c39d5c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 21 Jan 2022 01:42:25 +0100 Subject: bpo-46417: Finalize structseq types at exit (GH-30645) Add _PyStructSequence_FiniType() and _PyStaticType_Dealloc() functions to finalize a structseq static type in Py_Finalize(). Currrently, these functions do nothing if Python is built in release mode. Clear static types: * AsyncGenHooksType: sys.set_asyncgen_hooks() * FlagsType: sys.flags * FloatInfoType: sys.float_info * Hash_InfoType: sys.hash_info * Int_InfoType: sys.int_info * ThreadInfoType: sys.thread_info * UnraisableHookArgsType: sys.unraisablehook * VersionInfoType: sys.version * WindowsVersionType: sys.getwindowsversion() --- Python/errors.c | 11 +++++++++++ Python/pylifecycle.c | 6 ++++++ Python/sysmodule.c | 15 +++++++++++++++ Python/thread.c | 11 +++++++++++ 4 files changed, 43 insertions(+) (limited to 'Python') diff --git a/Python/errors.c b/Python/errors.c index 6c5fe41142..211881ca5e 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1241,6 +1241,17 @@ _PyErr_InitTypes(PyInterpreterState *interp) } +void +_PyErr_FiniTypes(PyInterpreterState *interp) +{ + if (!_Py_IsMainInterpreter(interp)) { + return; + } + + _PyStructSequence_FiniType(&UnraisableHookArgsType); +} + + static PyObject * make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb, diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8bcad67e80..0b1f471476 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1666,11 +1666,17 @@ flush_std_files(void) static void finalize_interp_types(PyInterpreterState *interp) { + _PySys_Fini(interp); _PyExc_Fini(interp); _PyFrame_Fini(interp); _PyAsyncGen_Fini(interp); _PyContext_Fini(interp); + _PyFloat_FiniType(interp); + _PyLong_FiniTypes(interp); + _PyThread_FiniType(interp); + _PyErr_FiniTypes(interp); _PyTypes_Fini(interp); + // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses // a dict internally. _PyUnicode_ClearInterned(interp); diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 0b7b61d8b1..515994f049 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -3102,6 +3102,21 @@ error: } +void +_PySys_Fini(PyInterpreterState *interp) +{ + if (_Py_IsMainInterpreter(interp)) { + _PyStructSequence_FiniType(&VersionInfoType); + _PyStructSequence_FiniType(&FlagsType); +#if defined(MS_WINDOWS) + _PyStructSequence_FiniType(&WindowsVersionType); +#endif + _PyStructSequence_FiniType(&Hash_InfoType); + _PyStructSequence_FiniType(&AsyncGenHooksType); + } +} + + static PyObject * makepathobject(const wchar_t *path, wchar_t delim) { diff --git a/Python/thread.c b/Python/thread.c index b1c0cfe84f..c2457c4f8f 100644 --- a/Python/thread.c +++ b/Python/thread.c @@ -243,3 +243,14 @@ PyThread_GetInfo(void) PyStructSequence_SET_ITEM(threadinfo, pos++, value); return threadinfo; } + + +void +_PyThread_FiniType(PyInterpreterState *interp) +{ + if (!_Py_IsMainInterpreter(interp)) { + return; + } + + _PyStructSequence_FiniType(&ThreadInfoType); +} -- cgit v1.2.1