Skip to content

Commit f42e112

Browse files
authored
gh-115420: Fix translation of exception hander targets by _testinternalcapi.optimize_cfg. (#115425)
1 parent 3a9e67a commit f42e112

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/test/test_peepholer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,22 @@ def test_conditional_jump_backward_const_condition(self):
10651065
]
10661066
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
10671067

1068+
def test_except_handler_label(self):
1069+
insts = [
1070+
('SETUP_FINALLY', handler := self.Label(), 10),
1071+
('POP_BLOCK', 0, -1),
1072+
('RETURN_CONST', 1, 11),
1073+
handler,
1074+
('RETURN_CONST', 2, 12),
1075+
]
1076+
expected_insts = [
1077+
('SETUP_FINALLY', handler := self.Label(), 10),
1078+
('RETURN_CONST', 1, 11),
1079+
handler,
1080+
('RETURN_CONST', 2, 12),
1081+
]
1082+
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
1083+
10681084
def test_no_unsafe_static_swap(self):
10691085
# We can't change order of two stores to the same location
10701086
insts = [
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix translation of exception hander targets by
2+
``_testinternalcapi.optimize_cfg``.

Python/flowgraph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2729,7 +2729,7 @@ _PyCfg_ToInstructionSequence(cfg_builder *g, _PyCompile_InstructionSequence *seq
27292729
RETURN_IF_ERROR(_PyCompile_InstructionSequence_UseLabel(seq, b->b_label.id));
27302730
for (int i = 0; i < b->b_iused; i++) {
27312731
cfg_instr *instr = &b->b_instr[i];
2732-
if (OPCODE_HAS_JUMP(instr->i_opcode)) {
2732+
if (OPCODE_HAS_JUMP(instr->i_opcode) || is_block_push(instr)) {
27332733
instr->i_oparg = instr->i_target->b_label.id;
27342734
}
27352735
RETURN_IF_ERROR(

0 commit comments

Comments
 (0)