]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120367: fix bug where compiler detects redundant jump after pseudo op replacement...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 18 Jun 2024 22:09:23 +0000 (23:09 +0100)
committerGitHub <noreply@github.com>
Tue, 18 Jun 2024 22:09:23 +0000 (22:09 +0000)
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2024-06-18-21-34-30.gh-issue-120367.zDwffP.rst [new file with mode: 0644]
Python/flowgraph.c

index 40295236eb7d92b681dbb99e1809de2011964216..e3e86b892466489fc47d1adebaf867444baeb853 100644 (file)
@@ -519,7 +519,32 @@ class TestSpecifics(unittest.TestCase):
 
         tree = ast.parse(code)
 
-        # make all instructions locations the same to create redundancies
+        # make all instruction locations the same to create redundancies
+        for node in ast.walk(tree):
+            if hasattr(node,"lineno"):
+                 del node.lineno
+                 del node.end_lineno
+                 del node.col_offset
+                 del node.end_col_offset
+
+        compile(ast.fix_missing_locations(tree), "<file>", "exec")
+
+    def test_compile_redundant_jump_after_convert_pseudo_ops(self):
+        # See gh-120367
+        code=textwrap.dedent("""
+            if name_2:
+                pass
+            else:
+                try:
+                    pass
+                except:
+                    pass
+            ~name_5
+            """)
+
+        tree = ast.parse(code)
+
+        # make all instruction locations the same to create redundancies
         for node in ast.walk(tree):
             if hasattr(node,"lineno"):
                  del node.lineno
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-18-21-34-30.gh-issue-120367.zDwffP.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-18-21-34-30.gh-issue-120367.zDwffP.rst
new file mode 100644 (file)
index 0000000..087640e
--- /dev/null
@@ -0,0 +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.
index 6f30dfcd33e0b442678d9e1cd00972a858b33014..8c1c20a0583c8c6a992011583cb8241aa4a539e9 100644 (file)
@@ -2389,7 +2389,7 @@ convert_pseudo_ops(cfg_builder *g)
             }
         }
     }
-    return remove_redundant_nops(g);
+    return remove_redundant_nops_and_jumps(g);
 }
 
 static inline bool