Skip to content

Commit 71b151d

Browse files
committed
gh-137288: Fix bug where boolean expressions are not associated with the correct exception handler
1 parent 9ced5c4 commit 71b151d

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/test_compile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,21 @@ def test_compound(self):
17231723
self.assertIs(res, v[3])
17241724
self.assertEqual([e.called for e in v], [1, 1, 0, 1, 0])
17251725

1726+
def test_exception(self):
1727+
# See gh-137288
1728+
class Foo:
1729+
def __bool__(self):
1730+
raise NotImplementedError()
1731+
1732+
a = Foo()
1733+
b = Foo()
1734+
1735+
with self.assertRaises(NotImplementedError):
1736+
bool(a)
1737+
1738+
with self.assertRaises(NotImplementedError):
1739+
c = a or b
1740+
17261741
@requires_debug_ranges()
17271742
class TestSourcePositions(unittest.TestCase):
17281743
# Ensure that compiled code snippets have correct line and column numbers
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where some bytecode instructions of a boolean expression are not
2+
associated with the correct exception handler.

Python/flowgraph.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,13 +3477,15 @@ convert_pseudo_conditional_jumps(cfg_builder *g)
34773477
.i_oparg = 1,
34783478
.i_loc = loc,
34793479
.i_target = NULL,
3480+
.i_except = instr->i_except,
34803481
};
34813482
RETURN_IF_ERROR(basicblock_insert_instruction(b, i++, &copy));
34823483
cfg_instr to_bool = {
34833484
.i_opcode = TO_BOOL,
34843485
.i_oparg = 0,
34853486
.i_loc = loc,
34863487
.i_target = NULL,
3488+
.i_except = instr->i_except,
34873489
};
34883490
RETURN_IF_ERROR(basicblock_insert_instruction(b, i++, &to_bool));
34893491
}
@@ -3726,13 +3728,15 @@ insert_prefix_instructions(_PyCompile_CodeUnitMetadata *umd, basicblock *entrybl
37263728
.i_oparg = 0,
37273729
.i_loc = loc,
37283730
.i_target = NULL,
3731+
.i_except = NULL,
37293732
};
37303733
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 0, &make_gen));
37313734
cfg_instr pop_top = {
37323735
.i_opcode = POP_TOP,
37333736
.i_oparg = 0,
37343737
.i_loc = loc,
37353738
.i_target = NULL,
3739+
.i_except = NULL,
37363740
};
37373741
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 1, &pop_top));
37383742
}
@@ -3763,6 +3767,7 @@ insert_prefix_instructions(_PyCompile_CodeUnitMetadata *umd, basicblock *entrybl
37633767
.i_oparg = oldindex,
37643768
.i_loc = NO_LOCATION,
37653769
.i_target = NULL,
3770+
.i_except = NULL,
37663771
};
37673772
if (basicblock_insert_instruction(entryblock, ncellsused, &make_cell) < 0) {
37683773
PyMem_RawFree(sorted);
@@ -3779,6 +3784,7 @@ insert_prefix_instructions(_PyCompile_CodeUnitMetadata *umd, basicblock *entrybl
37793784
.i_oparg = nfreevars,
37803785
.i_loc = NO_LOCATION,
37813786
.i_target = NULL,
3787+
.i_except = NULL,
37823788
};
37833789
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 0, &copy_frees));
37843790
}

0 commit comments

Comments
 (0)