diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst new file mode 100644 index 000000000000000..c82172b2eb40802 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst @@ -0,0 +1 @@ +Fix a fatal error in debug builds when re-importing a single-phase extension module fails with :exc:MemoryError: the cleanup path removed the module from :data:sys.modules while the exception was still set. diff --git a/Python/import.c b/Python/import.c index 63e23e21beb1266..710099151d1f278 100644 --- a/Python/import.c +++ b/Python/import.c @@ -2008,7 +2008,7 @@ reload_singlephase_extension(PyThreadState *tstate, Py_ssize_t index = _get_cached_module_index(cached); if (_modules_by_index_set(tstate->interp, index, mod) < 0) { - PyMapping_DelItem(modules, info->name); + remove_module(tstate, info->name); Py_DECREF(mod); return NULL; }