From 536ddd2baf75a46ef74e4476471e938e67bf165f Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 19:31:16 -0700 Subject: [PATCH 1/2] gh-159739: Preserve the syntax error if metadata allocation fails (GH-159751) Restore the original exception when optional syntax-error metadata cannot be allocated. (cherry picked from commit 9c239fb50a7f2821359ed3dc82993fba7da7d41c) Co-authored-by: Jelle Zijlstra --- .../2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst | 2 ++ Parser/pegen.c | 1 + 2 files changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst new file mode 100644 index 00000000000000..a02631f2da2f9b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst @@ -0,0 +1,2 @@ +Preserve the original :exc:`SyntaxError` if allocating optional error metadata +fails. diff --git a/Parser/pegen.c b/Parser/pegen.c index 1dc039fccfa249..623f05c2d30658 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -935,6 +935,7 @@ _PyPegen_set_syntax_error_metadata(Parser *p) { if (!metadata) { Py_DECREF(the_source); PyErr_Clear(); + PyErr_SetRaisedException(exc); return; } PySyntaxErrorObject *syntax_error = (PySyntaxErrorObject *)exc; From 51e447c90a90ca233c3d96d3bdf179e03d57d874 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 20 Oct 2026 10:37:06 -0700 Subject: [PATCH 2/2] Apply suggestion from @JelleZijlstra --- Parser/pegen.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Parser/pegen.c b/Parser/pegen.c index 623f05c2d30658..fb8d7977ec0427 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -933,7 +933,6 @@ _PyPegen_set_syntax_error_metadata(Parser *p) { the_source // N gives ownership to metadata ); if (!metadata) { - Py_DECREF(the_source); PyErr_Clear(); PyErr_SetRaisedException(exc); return;