From f459c4bdf1263973d18686f31af2a266038c380d Mon Sep 17 00:00:00 2001 From: Vamsi-klu Date: Sun, 26 Jul 2026 07:10:54 +0000 Subject: [PATCH] gh-151763: Restore exception if parser recovery path leaves none set After the diagnostic second parse pass, call PyErr_NoMemory() when no exception is set so callers do not hit _Py_CheckFunctionResult fatals (OOM-0021). --- .../2026-07-26-07-01-33.gh-issue-151763.RacVgg.rst | 2 ++ Parser/pegen.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-07-01-33.gh-issue-151763.RacVgg.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-07-01-33.gh-issue-151763.RacVgg.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-07-01-33.gh-issue-151763.RacVgg.rst new file mode 100644 index 00000000000000..f8eefcecec88ff --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-07-01-33.gh-issue-151763.RacVgg.rst @@ -0,0 +1,2 @@ +Fix a crash in the parser recovery path when an allocation failure leaves no +exception set. diff --git a/Parser/pegen.c b/Parser/pegen.c index fcec810037e98d..92a1f59191d684 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -1026,7 +1026,12 @@ _PyPegen_run_parser(Parser *p) if (PyErr_ExceptionMatches(PyExc_SyntaxError)) { _PyPegen_set_syntax_error_metadata(p); } - return NULL; + if (!PyErr_Occurred()) { + /* Under OOM the recovery pass can clear the exception and fail + silently; restore the result/error contract. */ + PyErr_NoMemory(); + } + return NULL; } if (p->start_rule == Py_single_input && bad_single_statement(p)) {