FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-130080: do not fold match case constants in unoptimized AST by iritkatriel · Pull Request #131577 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_ast/test_ast.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
3 changes: 3 additions & 0 deletions Python/ast_opt.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

Back | FazBrowse Home | New Git URL