Skip to content

Commit a716091

Browse files
Fidget-Spinnerdevdanzinchris-eibl
authored
pythongh-140104: Set next_instr properly in the JIT during exceptions (pythonGH-140233)
Co-authored-by: devdanzin <[email protected]> Co-authored-by: Chris Eibl <[email protected]>
1 parent 0f0a362 commit a716091

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,30 @@ def f2():
26372637

26382638
f2()
26392639

2640+
def test_next_instr_for_exception_handler_set(self):
2641+
# gh-140104: We just want the exception to be caught properly.
2642+
def f():
2643+
for i in range(TIER2_THRESHOLD + 3):
2644+
try:
2645+
undefined_variable(i)
2646+
except Exception:
2647+
pass
2648+
2649+
f()
2650+
2651+
def test_next_instr_for_exception_handler_set_lasts_instr(self):
2652+
# gh-140104: We just want the exception to be caught properly.
2653+
def f():
2654+
a_list = []
2655+
for _ in range(TIER2_THRESHOLD + 3):
2656+
try:
2657+
a_list[""] = 0
2658+
except Exception:
2659+
pass
2660+
2661+
f()
2662+
2663+
26402664

26412665
def global_identity(x):
26422666
return x
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug with exception handling in the JIT. Patch by Ken Jin. Bug reported
2+
by Daniel Diniz.

Python/ceval_macros.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ do { \
358358
frame = tstate->current_frame; \
359359
stack_pointer = _PyFrame_GetStackPointer(frame); \
360360
if (next_instr == NULL) { \
361-
next_instr = frame->instr_ptr; \
361+
/* gh-140104: The exception handler expects frame->instr_ptr
362+
to after this_instr, not this_instr! */ \
363+
next_instr = frame->instr_ptr + 1; \
362364
JUMP_TO_LABEL(error); \
363365
} \
364366
DISPATCH(); \

0 commit comments

Comments
 (0)