diff --git a/Python/_warnings.c b/Python/_warnings.c index 0077b9b1c3d0c4..e96e154b0d542f 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -63,6 +63,13 @@ get_warnings_attr(const char *attr, int try_import) } } else { + /* if we're so late into Python finalization that the module dict is + gone, then we can't even use PyImport_GetModule without triggering + an interpreter abort. + */ + if (!PyThreadState_GET()->interp->modules) { + return NULL; + } all_modules = PyImport_GetModuleDict(); warnings_module = PyDict_GetItem(all_modules, warnings_str);