From ebc3fc535dd6000341b8560d68df05fc2022c0ee Mon Sep 17 00:00:00 2001 From: lipengyu Date: Thu, 16 Jul 2026 10:51:01 +0800 Subject: [PATCH] gh-153797: Fix crash when re-importing extension modules after OOM Fix a crash that could occur after an out-of-memory error when re-importing an extension module. --- .../2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst | 2 ++ Python/import.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst 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..6394c5f37ffc4dd --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-16-10-12-32.gh-issue-153797.VqQ8r1.rst @@ -0,0 +1,2 @@ +Fix a crash that could occur after an out-of-memory error when re-importing +an extension module. 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; }