Skip to content

Commit a4293bc

Browse files
Fix wrong instruction pointer for exceptions
1 parent 9a87ce8 commit a4293bc

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,17 @@ 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+
g(i)
2646+
except Exception:
2647+
pass
2648+
2649+
f()
2650+
26402651

26412652
def global_identity(x):
26422653
return x

Python/bytecodes.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5384,7 +5384,10 @@ dummy_func(
53845384

53855385
tier2 op(_ERROR_POP_N, (target/2 --)) {
53865386
assert(oparg == 0);
5387-
frame->instr_ptr = _PyFrame_GetBytecode(frame) + target;
5387+
_Py_CODEUNIT *current_instr = _PyFrame_GetBytecode(frame) + target;
5388+
_Py_CODEUNIT *next_instr = current_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[current_instr->op.code]];
5389+
// gh-140104: The exception handler expects frame->instr_ptr to be pointing to next_instr, not this_instr!
5390+
frame->instr_ptr = next_instr;
53885391
SYNC_SP();
53895392
GOTO_TIER_ONE(NULL);
53905393
}

Python/executor_cases.c.h

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)