Skip to content

Commit 64906bb

Browse files
authored
gh-130080: do not fold match case constants in unoptimized AST (#131577)
1 parent fd459b1 commit 64906bb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,18 @@ def get_match_case_values(node):
32383238
values = get_match_case_values(case.pattern)
32393239
self.assertListEqual(constants, values)
32403240

3241+
def test_match_case_not_folded_in_unoptimized_ast(self):
3242+
src = textwrap.dedent("""
3243+
match a:
3244+
case 1+2j:
3245+
pass
3246+
""")
3247+
3248+
unfolded = "MatchValue(value=BinOp(left=Constant(value=1), op=Add(), right=Constant(value=2j))"
3249+
folded = "MatchValue(value=Constant(value=(1+2j)))"
3250+
for optval in (0, 1, 2):
3251+
self.assertIn(folded if optval else unfolded, ast.dump(ast.parse(src, optimize=optval)))
3252+
32413253

32423254
if __name__ == '__main__':
32433255
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':

Python/ast_opt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
824824
static int
825825
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *state)
826826
{
827+
if (state->syntax_check_only) {
828+
return 1;
829+
}
827830
switch (node->kind)
828831
{
829832
case UnaryOp_kind:

0 commit comments

Comments
 (0)