Skip to content

Commit e92599e

Browse files
DinoViritkatriel
andauthored
gh-138714: Don't assume next block has instructions when propagating line numbers (#138770)
Co-authored-by: Irit Katriel <[email protected]>
1 parent 4b78fe9 commit e92599e

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

Lib/test/test_compile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,17 @@ def test_remove_redundant_nop_edge_case(self):
16151615
def f():
16161616
a if (1 if b else c) else d
16171617

1618+
def test_lineno_propagation_empty_blocks(self):
1619+
# Smoke test. See gh-138714.
1620+
def f():
1621+
while name:
1622+
try:
1623+
break
1624+
except:
1625+
pass
1626+
else:
1627+
1 if 1 else 1
1628+
16181629
def test_global_declaration_in_except_used_in_else(self):
16191630
# See gh-111123
16201631
code = textwrap.dedent("""\

Python/flowgraph.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,8 +3591,19 @@ duplicate_exits_without_lineno(cfg_builder *g)
35913591
* Also reduces the size of the line number table,
35923592
* but has no impact on the generated line number events.
35933593
*/
3594+
3595+
static inline void
3596+
maybe_propagate_location(basicblock *b, int i, location loc)
3597+
{
3598+
assert(b->b_iused > i);
3599+
if (b->b_instr[i].i_loc.lineno == NO_LOCATION.lineno) {
3600+
b->b_instr[i].i_loc = loc;
3601+
}
3602+
}
3603+
35943604
static void
3595-
propagate_line_numbers(basicblock *entryblock) {
3605+
propagate_line_numbers(basicblock *entryblock)
3606+
{
35963607
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
35973608
cfg_instr *last = basicblock_last_instr(b);
35983609
if (last == NULL) {
@@ -3601,26 +3612,21 @@ propagate_line_numbers(basicblock *entryblock) {
36013612

36023613
location prev_location = NO_LOCATION;
36033614
for (int i = 0; i < b->b_iused; i++) {
3604-
if (b->b_instr[i].i_loc.lineno == NO_LOCATION.lineno) {
3605-
b->b_instr[i].i_loc = prev_location;
3606-
}
3607-
else {
3608-
prev_location = b->b_instr[i].i_loc;
3609-
}
3615+
maybe_propagate_location(b, i, prev_location);
3616+
prev_location = b->b_instr[i].i_loc;
36103617
}
36113618
if (BB_HAS_FALLTHROUGH(b) && b->b_next->b_predecessors == 1) {
36123619
if (b->b_next->b_iused > 0) {
3613-
if (b->b_next->b_instr[0].i_loc.lineno == NO_LOCATION.lineno) {
3614-
b->b_next->b_instr[0].i_loc = prev_location;
3615-
}
3620+
maybe_propagate_location(b->b_next, 0, prev_location);
36163621
}
36173622
}
36183623
if (is_jump(last)) {
36193624
basicblock *target = last->i_target;
3625+
while (target->b_iused == 0 && target->b_predecessors == 1) {
3626+
target = target->b_next;
3627+
}
36203628
if (target->b_predecessors == 1) {
3621-
if (target->b_instr[0].i_loc.lineno == NO_LOCATION.lineno) {
3622-
target->b_instr[0].i_loc = prev_location;
3623-
}
3629+
maybe_propagate_location(target, 0, prev_location);
36243630
}
36253631
}
36263632
}

0 commit comments

Comments
 (0)