Skip to content

Commit 2e21672

Browse files
efimov-mikhailFidget-Spinnerdevdanzinchris-eibl
authored
[3.14] gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233) (GH-140687)
Co-authored-by: Ken Jin <[email protected]> Co-authored-by: devdanzin <[email protected]> Co-authored-by: Chris Eibl <[email protected]>
1 parent 3bb0eb4 commit 2e21672

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,27 @@ def testfunc(n):
19721972
assert ex is not None
19731973
"""))
19741974

1975+
def test_next_instr_for_exception_handler_set(self):
1976+
# gh-140104: We just want the exception to be caught properly.
1977+
def f():
1978+
for i in range(TIER2_THRESHOLD + 3):
1979+
try:
1980+
undefined_variable(i)
1981+
except Exception:
1982+
pass
1983+
1984+
f()
1985+
1986+
def test_next_instr_for_exception_handler_set_lasts_instr(self):
1987+
# gh-140104: We just want the exception to be caught properly.
1988+
def f():
1989+
a_list = []
1990+
for _ in range(TIER2_THRESHOLD + 3):
1991+
try:
1992+
a_list[""] = 0
1993+
except Exception:
1994+
pass
1995+
19751996

19761997
def global_identity(x):
19771998
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
@@ -368,7 +368,9 @@ do { \
368368
frame = tstate->current_frame; \
369369
stack_pointer = _PyFrame_GetStackPointer(frame); \
370370
if (next_instr == NULL) { \
371-
next_instr = frame->instr_ptr; \
371+
/* gh-140104: The exception handler expects frame->instr_ptr
372+
to after this_instr, not this_instr! */ \
373+
next_instr = frame->instr_ptr + 1; \
372374
JUMP_TO_LABEL(error); \
373375
} \
374376
DISPATCH(); \

0 commit comments

Comments
 (0)