From 0d4498d71d56bfff149932f3ec63b65aee41943d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 23 Jun 2026 17:30:41 +0300 Subject: [PATCH] gh-151763: Fix `_interpreters.capture_exception` crash on memory errors --- .../Library/2026-06-23-17-30-16.gh-issue-151763.-sTnNi.rst | 2 ++ Modules/_interpretersmodule.c | 4 +++- Python/crossinterp.c | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-23-17-30-16.gh-issue-151763.-sTnNi.rst diff --git a/Misc/NEWS.d/next/Library/2026-06-23-17-30-16.gh-issue-151763.-sTnNi.rst b/Misc/NEWS.d/next/Library/2026-06-23-17-30-16.gh-issue-151763.-sTnNi.rst new file mode 100644 index 00000000000000..09e423da0c591e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-23-17-30-16.gh-issue-151763.-sTnNi.rst @@ -0,0 +1,2 @@ +Fix a crash in :func:`!_interpreters.capture_exception` when +:exc:`MemoryError` happens. diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index e7a91ced48f176..d024dee906ded3 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -1541,7 +1541,9 @@ _interpreters_capture_exception_impl(PyObject *module, PyObject *exc_arg) } finally: - _PyXI_FreeExcInfo(info); + if (info != NULL) { + _PyXI_FreeExcInfo(info); + } if (exc != exc_arg) { if (PyErr_Occurred()) { PyErr_SetRaisedException(exc); diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 6b489bf03f86ec..d52ad4145388ba 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1689,6 +1689,7 @@ _PyXI_NewExcInfo(PyObject *exc) } _PyXI_excinfo *info = PyMem_RawCalloc(1, sizeof(_PyXI_excinfo)); if (info == NULL) { + PyErr_NoMemory(); return NULL; } const char *failure;