From 2e216728038a03d387231f39ded091ec318ba61b Mon Sep 17 00:00:00 2001 From: Mikhail Efimov Date: Tue, 28 Oct 2025 02:49:10 +0300 Subject: [PATCH] [3.14] gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233) (GH-140687) Co-authored-by: Ken Jin Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com> Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com> --- Lib/test/test_capi/test_opt.py | 21 +++++++++++++++++++ ...-10-16-21-47-00.gh-issue-140104.A8SQIm.rst | 2 ++ Python/ceval_macros.h | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-10-16-21-47-00.gh-issue-140104.A8SQIm.rst diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index c74c8ee0eef8..bdcda12c76e1 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -1972,6 +1972,27 @@ class TestUopsOptimization(unittest.TestCase): assert ex is not None """)) + def test_next_instr_for_exception_handler_set(self): + # gh-140104: We just want the exception to be caught properly. + def f(): + for i in range(TIER2_THRESHOLD + 3): + try: + undefined_variable(i) + except Exception: + pass + + f() + + def test_next_instr_for_exception_handler_set_lasts_instr(self): + # gh-140104: We just want the exception to be caught properly. + def f(): + a_list = [] + for _ in range(TIER2_THRESHOLD + 3): + try: + a_list[""] = 0 + except Exception: + pass + def global_identity(x): return x diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-16-21-47-00.gh-issue-140104.A8SQIm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-16-21-47-00.gh-issue-140104.A8SQIm.rst new file mode 100644 index 000000000000..1c18cbc9ad05 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-16-21-47-00.gh-issue-140104.A8SQIm.rst @@ -0,0 +1,2 @@ +Fix a bug with exception handling in the JIT. Patch by Ken Jin. Bug reported +by Daniel Diniz. diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 187ec8fdd265..df1950416c2d 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -368,7 +368,9 @@ do { \ frame = tstate->current_frame; \ stack_pointer = _PyFrame_GetStackPointer(frame); \ if (next_instr == NULL) { \ - next_instr = frame->instr_ptr; \ + /* gh-140104: The exception handler expects frame->instr_ptr + to after this_instr, not this_instr! */ \ + next_instr = frame->instr_ptr + 1; \ JUMP_TO_LABEL(error); \ } \ DISPATCH(); \ -- 2.47.3