]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233)
authorKen Jin <kenjin@python.org>
Mon, 27 Oct 2025 18:26:47 +0000 (02:26 +0800)
committerGitHub <noreply@github.com>
Mon, 27 Oct 2025 18:26:47 +0000 (18:26 +0000)
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
Misc/NEWS.d/next/Core_and_Builtins/2025-10-16-21-47-00.gh-issue-140104.A8SQIm.rst [new file with mode: 0644]
Python/ceval_macros.h

index f121f27174875e97c6d226d419791951be78f283..4e94f62d35eba29229542299667d7601dbc96ec1 100644 (file)
@@ -2637,6 +2637,30 @@ class TestUopsOptimization(unittest.TestCase):
 
         f2()
 
+    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
+
+        f()
+
+
 
 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 (file)
index 0000000..1c18cbc
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a bug with exception handling in the JIT. Patch by Ken Jin. Bug reported
+by Daniel Diniz.
index 4ed03b7fb01bdfdcbd884234f93c5f6c13fca3f6..8083913b1a1cfc577e25acbb4ac74d18a3e00c81 100644 (file)
@@ -358,7 +358,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();                                        \