Skip to content

Commit

Permalink
pythongh-109627: duplicated smalll exit blocks need to be assigned ju…
Browse files Browse the repository at this point in the history
…mp target labels
  • Loading branch information
iritkatriel committed Sep 20, 2023
1 parent 14cdefa commit 1e4f4a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,15 @@ def f():
return a, b
self.assertEqual(f(), (54, 96))

def test_duplicated_exit_block_which_is_jump_target(self):
# See gh-109627
def f():
while element and something:
try:
return something
except:
pass


@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
Expand Down
17 changes: 17 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,19 @@ is_exit_without_lineno(basicblock *b) {
return true;
}


static int
max_label(cfg_builder *g)
{
int lbl = -1;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
if (b->b_label.id > lbl) {
lbl = b->b_label.id;
}
}
return lbl;
}

/* PEP 626 mandates that the f_lineno of a frame is correct
* after a frame terminates. It would be prohibitively expensive
* to continuously update the f_lineno field at runtime,
Expand All @@ -2242,6 +2255,9 @@ static int
duplicate_exits_without_lineno(cfg_builder *g)
{
assert(no_empty_basic_blocks(g));

int next_lbl = max_label(g) + 1;

/* Copy all exit blocks without line number that are targets of a jump.
*/
basicblock *entryblock = g->g_entryblock;
Expand All @@ -2260,6 +2276,7 @@ duplicate_exits_without_lineno(cfg_builder *g)
target->b_predecessors--;
new_target->b_predecessors = 1;
new_target->b_next = target->b_next;
new_target->b_label.id = next_lbl++;
target->b_next = new_target;
}
}
Expand Down

0 comments on commit 1e4f4a1

Please sign in to comment.