Skip to content

Commit b87aea6

Browse files
[3.13] gh-124871: fix 'visited' tracking in compiler's reachability analysis (GH-124952) (#124977)
gh-124871: fix 'visited' tracking in compiler's reachability analysis (GH-124952) (cherry picked from commit f474391) Co-authored-by: Irit Katriel <[email protected]>
1 parent dd4e62e commit b87aea6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Lib/test/test_compile.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ def test_dead_code_with_except_handler_compiles(self):
476476
x = 2
477477
"""), '<eval>', 'exec')
478478

479+
def test_try_except_in_while_with_chained_condition_compiles(self):
480+
# see gh-124871
481+
compile(textwrap.dedent("""
482+
name_1, name_2, name_3 = 1, 2, 3
483+
while name_3 <= name_2 > name_1:
484+
try:
485+
raise
486+
except:
487+
pass
488+
finally:
489+
pass
490+
"""), '<eval>', 'exec')
491+
479492
def test_compile_invalid_namedexpr(self):
480493
# gh-109351
481494
m = ast.Module(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix compiler bug (in some versions of 3.13) where an assertion fails during reachability
2+
analysis.

Python/flowgraph.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,14 @@ remove_unreachable(basicblock *entryblock) {
960960
basicblock **sp = stack;
961961
entryblock->b_predecessors = 1;
962962
*sp++ = entryblock;
963+
entryblock->b_visited = 1;
963964
while (sp > stack) {
964965
basicblock *b = *(--sp);
965-
b->b_visited = 1;
966966
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
967967
if (!b->b_next->b_visited) {
968968
assert(b->b_next->b_predecessors == 0);
969969
*sp++ = b->b_next;
970+
b->b_next->b_visited = 1;
970971
}
971972
b->b_next->b_predecessors++;
972973
}
@@ -976,8 +977,8 @@ remove_unreachable(basicblock *entryblock) {
976977
if (is_jump(instr) || is_block_push(instr)) {
977978
target = instr->i_target;
978979
if (!target->b_visited) {
979-
assert(target->b_predecessors == 0 || target == b->b_next);
980980
*sp++ = target;
981+
target->b_visited = 1;
981982
}
982983
target->b_predecessors++;
983984
}

0 commit comments

Comments
 (0)