]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233...
authorMikhail Efimov <efimov.mikhail@gmail.com>
Thu, 13 Nov 2025 18:47:25 +0000 (21:47 +0300)
committerGitHub <noreply@github.com>
Thu, 13 Nov 2025 18:47:25 +0000 (18:47 +0000)
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 1860325cba616d2640ea786f3ed0c1b1c79cfaea..be3f8895db5d093bced3511cf16b7f73e01714a6 100644 (file)
@@ -1992,6 +1992,27 @@ class TestUopsOptimization(unittest.TestCase):
             next(g)
         """ % _testinternalcapi.SPECIALIZATION_THRESHOLD))
 
+    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 (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 37c4b00f86df25db3179426efe29620e79ad7d1a..4a878d6dff4353b663c4d9190320a07128c91a63 100644 (file)
@@ -376,7 +376,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();                                        \
@@ -404,7 +406,9 @@ do { \
         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;                        \
             goto error;                                               \
         }                                                             \
         DISPATCH();                                                   \