Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f474391

Browse files
authored
gh-124871: fix 'visited' tracking in compiler's reachability analysis (#124952)
1 parent 994051e commit f474391

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
@@ -479,6 +479,19 @@ def test_dead_code_with_except_handler_compiles(self):
479479
x = 2
480480
"""), '<eval>', 'exec')
481481

482+
def test_try_except_in_while_with_chained_condition_compiles(self):
483+
# see gh-124871
484+
compile(textwrap.dedent("""
485+
name_1, name_2, name_3 = 1, 2, 3
486+
while name_3 <= name_2 > name_1:
487+
try:
488+
raise
489+
except:
490+
pass
491+
finally:
492+
pass
493+
"""), '<eval>', 'exec')
494+
482495
def test_compile_invalid_namedexpr(self):
483496
# gh-109351
484497
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
@@ -1001,13 +1001,14 @@ remove_unreachable(basicblock *entryblock) {
10011001
basicblock **sp = stack;
10021002
entryblock->b_predecessors = 1;
10031003
*sp++ = entryblock;
1004+
entryblock->b_visited = 1;
10041005
while (sp > stack) {
10051006
basicblock *b = *(--sp);
1006-
b->b_visited = 1;
10071007
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
10081008
if (!b->b_next->b_visited) {
10091009
assert(b->b_next->b_predecessors == 0);
10101010
*sp++ = b->b_next;
1011+
b->b_next->b_visited = 1;
10111012
}
10121013
b->b_next->b_predecessors++;
10131014
}
@@ -1017,8 +1018,8 @@ remove_unreachable(basicblock *entryblock) {
10171018
if (is_jump(instr) || is_block_push(instr)) {
10181019
target = instr->i_target;
10191020
if (!target->b_visited) {
1020-
assert(target->b_predecessors == 0 || target == b->b_next);
10211021
*sp++ = target;
1022+
target->b_visited = 1;
10221023
}
10231024
target->b_predecessors++;
10241025
}

0 commit comments

Comments
 (0)