https://github.com/python/cpython/commit/64906bb22373dc58b88863424ceef07193b776b0
commit: 64906bb22373dc58b88863424ceef07193b776b0
branch: main
author: Irit Katriel <1055913+iritkatr...@users.noreply.github.com>
committer: iritkatriel <1055913+iritkatr...@users.noreply.github.com>
date: 2025-03-23T13:50:14Z
summary:

gh-130080: do not fold match case constants in unoptimized AST (#131577)

files:
M Lib/test/test_ast/test_ast.py
M Python/ast_opt.c

diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py
index 1b108ceddb1c11..e5fd8f6a295ed2 100644
--- a/Lib/test/test_ast/test_ast.py
+++ b/Lib/test/test_ast/test_ast.py
@@ -3238,6 +3238,18 @@ def get_match_case_values(node):
                 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':
diff --git a/Python/ast_opt.c b/Python/ast_opt.c
index 8ee322fdd15197..99a2418da46e35 100644
--- a/Python/ast_opt.c
+++ b/Python/ast_opt.c
@@ -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:

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to