[3.15] gh-151842: Fix crash upon OOM in _interpreters.capture_exception (GH-151843) (GH-152031)

(cherry picked from commit 5e0747db2f)

Co-authored-by: Amrutha <amruthamodela06@gmail.com>
This commit is contained in:
Miss Islington (bot) 2026-06-23 22:58:10 +02:00 committed by GitHub
parent bd39eea0db
commit 415e218ed5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 1 deletions

View file

@ -0,0 +1,2 @@
Fix a crash in :func:`!_interpreters.capture_exception` when
:exc:`MemoryError` happens. Patch by Amrutha Modela.

View file

@ -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);

View file

@ -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;
@ -1709,6 +1710,7 @@ _PyXI_NewExcInfo(PyObject *exc)
void
_PyXI_FreeExcInfo(_PyXI_excinfo *info)
{
assert(info != NULL);
_PyXI_excinfo_clear(info);
PyMem_RawFree(info);
}