]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130080: do not fold match case constants in unoptimized AST (#131577)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sun, 23 Mar 2025 13:50:14 +0000 (13:50 +0000)
committerGitHub <noreply@github.com>
Sun, 23 Mar 2025 13:50:14 +0000 (13:50 +0000)
Lib/test/test_ast/test_ast.py
Python/ast_opt.c

index 1b108ceddb1c1113c496ae3a63ed1056b0e453d3..e5fd8f6a295ed22af673cfed0d7d44ab6453e744 100644 (file)
@@ -3238,6 +3238,18 @@ class ASTOptimiziationTests(unittest.TestCase):
                 values = get_match_case_values(case.pattern)
                 self.assertListEqual(constants, values)
 
+    def test_match_case_not_folded_in_unoptimized_ast(self):
+        src = textwrap.dedent("""
+            match a:
+                case 1+2j:
+                    pass
+            """)
+
+        unfolded = "MatchValue(value=BinOp(left=Constant(value=1), op=Add(), right=Constant(value=2j))"
+        folded = "MatchValue(value=Constant(value=(1+2j)))"
+        for optval in (0, 1, 2):
+            self.assertIn(folded if optval else unfolded, ast.dump(ast.parse(src, optimize=optval)))
+
 
 if __name__ == '__main__':
     if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
index 8ee322fdd15197723a7cfd9f3ed2e50f93e1f359..99a2418da46e35ae6721ff6443e39e450b3bcc6e 100644 (file)
@@ -824,6 +824,9 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
 static int
 fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *state)
 {
+    if (state->syntax_check_only) {
+        return 1;
+    }
     switch (node->kind)
     {
         case UnaryOp_kind: