Skip to content

Commit 50fa775

Browse files
[3.13] gh-120367: fix bug where compiler detects redundant jump after pseudo op replacement (GH-120714) (#120716)
1 parent 07145dd commit 50fa775

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Lib/test/test_compile.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,32 @@ def test_compile_redundant_jumps_and_nops_after_moving_cold_blocks(self):
519519

520520
tree = ast.parse(code)
521521

522-
# make all instructions locations the same to create redundancies
522+
# make all instruction locations the same to create redundancies
523+
for node in ast.walk(tree):
524+
if hasattr(node,"lineno"):
525+
del node.lineno
526+
del node.end_lineno
527+
del node.col_offset
528+
del node.end_col_offset
529+
530+
compile(ast.fix_missing_locations(tree), "<file>", "exec")
531+
532+
def test_compile_redundant_jump_after_convert_pseudo_ops(self):
533+
# See gh-120367
534+
code=textwrap.dedent("""
535+
if name_2:
536+
pass
537+
else:
538+
try:
539+
pass
540+
except:
541+
pass
542+
~name_5
543+
""")
544+
545+
tree = ast.parse(code)
546+
547+
# make all instruction locations the same to create redundancies
523548
for node in ast.walk(tree):
524549
if hasattr(node,"lineno"):
525550
del node.lineno
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where compiler creates a redundant jump during pseudo-op replacement. Can only happen with a synthetic AST that has a try on the same line as the instruction following the exception handler.

Python/flowgraph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,7 @@ convert_pseudo_ops(cfg_builder *g)
23612361
}
23622362
}
23632363
}
2364-
return remove_redundant_nops(g);
2364+
return remove_redundant_nops_and_jumps(g);
23652365
}
23662366

23672367
static inline bool

0 commit comments

Comments
 (0)