-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-122666: Tests for ast optimizations #122667
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
dccd005
Initial snippets
Eclips4 4ced8d6
More snippets!
Eclips4 a439bb2
Fix linting issues
Eclips4 089a74c
Fix another one linting issue
Eclips4 07919e9
Update Lib/test/test_ast/test_ast.py
Eclips4 ec7062c
Update Lib/test/test_ast/test_ast.py
Eclips4 6299fb9
Move opt snippets to test_ast/opt_snippets.py
Eclips4 1f74250
Remove old code
Eclips4 2c59711
Remove unnecessary newline
Eclips4 198df5f
compile -> ast.parse
Eclips4 c516588
Try another approach
Eclips4 5021111
Remoev unnecessary changes
Eclips4 25719e3
Remove unnecessary changes
Eclips4 5ed0a66
Fix linting issues
Eclips4 b002054
Add ``__name__ == '__main__'`` construction to test_ast.py
Eclips4 5ad2c0f
Fix some linting issues.. (again)
Eclips4 60494bb
Don't forget about frozenset
Eclips4 d49f4f2
Fix linting issue
Eclips4 cdf2422
Apply suggestions from code review
Eclips4 b42f3fc
Address Victor's comments
Eclips4 eaf4a3e
Compare the whole trees
Eclips4 d4d1e52
Address Jelle's comment
Eclips4 8bbbc5c
Apply suggestions from code review
Eclips4 997293b
Address Jelle's comments
Eclips4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3035,3 +3035,216 @@ def test_cli_file_input(self): | |
self.assertEqual(expected.splitlines(), | ||
res.out.decode("utf8").splitlines()) | ||
self.assertEqual(res.rc, 0) | ||
|
||
|
||
class ASTOptimiziationTests(unittest.TestCase): | ||
Eclips4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
binop = { | ||
"+": ast.Add(), | ||
"-": ast.Sub(), | ||
"*": ast.Mult(), | ||
"/": ast.Div(), | ||
"%": ast.Mod(), | ||
"<<": ast.LShift(), | ||
">>": ast.RShift(), | ||
"|": ast.BitOr(), | ||
"^": ast.BitXor(), | ||
"&": ast.BitAnd(), | ||
"//": ast.FloorDiv(), | ||
"**": ast.Pow(), | ||
} | ||
|
||
unaryop = { | ||
"~": ast.Invert(), | ||
"+": ast.UAdd(), | ||
"-": ast.USub(), | ||
} | ||
|
||
def wrap_expr(self, expr): | ||
return ast.Module(body=[ast.Expr(value=expr)]) | ||
|
||
def wrap_for(self, for_statement): | ||
return ast.Module(body=[for_statement]) | ||
|
||
def assert_ast(self, code, non_optimized_target, optimized_target): | ||
Eclips4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
non_optimized_tree = ast.parse(code, optimize=-1) | ||
optimized_tree = ast.parse(code, optimize=1) | ||
|
||
# Is a non-optimized tree equal to a non-optimized target? | ||
self.assertTrue( | ||
ast.compare(non_optimized_tree, non_optimized_target), | ||
f"{ast.dump(non_optimized_target)} must equal " | ||
f"{ast.dump(non_optimized_tree)}", | ||
) | ||
|
||
# Is a optimized tree equal to a non-optimized target? | ||
self.assertFalse( | ||
ast.compare(optimized_tree, non_optimized_target), | ||
f"{ast.dump(non_optimized_target)} must not equal " | ||
f"{ast.dump(non_optimized_tree)}" | ||
) | ||
|
||
# Is a optimized tree is equal to an optimized target? | ||
self.assertTrue( | ||
ast.compare(optimized_tree, optimized_target), | ||
f"{ast.dump(optimized_target)} must equal " | ||
f"{ast.dump(optimized_tree)}", | ||
) | ||
|
||
def test_folding_binop(self): | ||
code = "1 %s 1" | ||
operators = self.binop.keys() | ||
|
||
Eclips4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def create_binop(operand, left=ast.Constant(1), right=ast.Constant(1)): | ||
return ast.BinOp(left=left, op=self.binop[operand], right=right) | ||
|
||
for op in operators: | ||
result_code = code % op | ||
non_optimized_target = self.wrap_expr(create_binop(op)) | ||
optimized_target = self.wrap_expr(ast.Constant(value=eval(result_code))) | ||
|
||
with self.subTest( | ||
result_code=result_code, | ||
non_optimized_target=non_optimized_target, | ||
optimized_target=optimized_target | ||
): | ||
self.assert_ast(result_code, non_optimized_target, optimized_target) | ||
|
||
# Multiplication of constant tuples must be folded | ||
code = "(1,) * 3" | ||
non_optimized_target = self.wrap_expr(create_binop("*", ast.Tuple(elts=[ast.Constant(value=1)]), ast.Constant(value=3))) | ||
optimized_target = self.wrap_expr(ast.Constant(eval(code))) | ||
|
||
self.assert_ast(code, non_optimized_target, optimized_target) | ||
|
||
def test_folding_unaryop(self): | ||
code = "%s1" | ||
operators = self.unaryop.keys() | ||
|
||
def create_unaryop(operand): | ||
return ast.UnaryOp(op=self.unaryop[operand], operand=ast.Constant(1)) | ||
|
||
for op in operators: | ||
result_code = code % op | ||
non_optimized_target = self.wrap_expr(create_unaryop(op)) | ||
optimized_target = self.wrap_expr(ast.Constant(eval(result_code))) | ||
|
||
with self.subTest( | ||
result_code=result_code, | ||
non_optimized_target=non_optimized_target, | ||
optimized_target=optimized_target | ||
): | ||
self.assert_ast(result_code, non_optimized_target, optimized_target) | ||
|
||
def test_folding_not(self): | ||
code = "not (1 %s (1,))" | ||
operators = { | ||
"in": ast.In(), | ||
"is": ast.Is(), | ||
} | ||
opt_operators = { | ||
"is": ast.IsNot(), | ||
"in": ast.NotIn(), | ||
} | ||
|
||
def create_notop(operand): | ||
return ast.UnaryOp(op=ast.Not(), operand=ast.Compare( | ||
left=ast.Constant(value=1), | ||
ops=[operators[operand]], | ||
comparators=[ast.Tuple(elts=[ast.Constant(value=1)])] | ||
)) | ||
|
||
for op in operators.keys(): | ||
result_code = code % op | ||
non_optimized_target = self.wrap_expr(create_notop(op)) | ||
optimized_target = self.wrap_expr( | ||
ast.Compare(left=ast.Constant(1), ops=[opt_operators[op]], comparators=[ast.Constant(value=(1,))]) | ||
) | ||
|
||
with self.subTest( | ||
result_code=result_code, | ||
non_optimized_target=non_optimized_target, | ||
optimized_target=optimized_target | ||
): | ||
self.assert_ast(result_code, non_optimized_target, optimized_target) | ||
|
||
def test_folding_format(self): | ||
code = "'%s' % (a,)" | ||
|
||
non_optimized_target = self.wrap_expr( | ||
ast.BinOp( | ||
left=ast.Constant(value="%s"), | ||
op=ast.Mod(), | ||
right=ast.Tuple(elts=[ast.Name(id='a')])) | ||
) | ||
optimized_target = self.wrap_expr( | ||
ast.JoinedStr( | ||
values=[ | ||
ast.FormattedValue(value=ast.Name(id='a'), conversion=115) | ||
] | ||
) | ||
) | ||
|
||
self.assert_ast(code, non_optimized_target, optimized_target) | ||
|
||
|
||
def test_folding_tuple(self): | ||
code = "(1,)" | ||
|
||
non_optimized_target = self.wrap_expr(ast.Tuple(elts=[ast.Constant(1)])) | ||
optimized_target = self.wrap_expr(ast.Constant(value=(1,))) | ||
|
||
self.assert_ast(code, non_optimized_target, optimized_target) | ||
|
||
def test_folding_comparator(self): | ||
code = "1 %s %s1%s" | ||
operators = [("in", ast.In()), ("not in", ast.NotIn())] | ||
braces = [ | ||
("[", "]", ast.List, (1,)), | ||
("{", "}", ast.Set, frozenset({1})), | ||
] | ||
for left, right, non_optimized_comparator, optimized_comparator in braces: | ||
for op, node in operators: | ||
non_optimized_target = self.wrap_expr(ast.Compare( | ||
left=ast.Constant(1), ops=[node], | ||
comparators=[non_optimized_comparator(elts=[ast.Constant(1)])] | ||
)) | ||
optimized_target = self.wrap_expr(ast.Compare( | ||
left=ast.Constant(1), ops=[node], | ||
comparators=[ast.Constant(value=optimized_comparator)] | ||
)) | ||
self.assert_ast(code % (op, left, right), non_optimized_target, optimized_target) | ||
|
||
def test_folding_iter(self): | ||
code = "for _ in %s1%s: pass" | ||
braces = [ | ||
("[", "]", ast.List, (1,)), | ||
("{", "}", ast.Set, frozenset({1})), | ||
] | ||
|
||
for left, right, ast_cls, optimized_iter in braces: | ||
non_optimized_target = self.wrap_for(ast.For( | ||
target=ast.Name(id="_", ctx=ast.Store()), | ||
iter=ast_cls(elts=[ast.Constant(1)]), | ||
body=[ast.Pass()] | ||
)) | ||
optimized_target = self.wrap_for(ast.For( | ||
target=ast.Name(id="_", ctx=ast.Store()), | ||
iter=ast.Constant(value=optimized_iter), | ||
body=[ast.Pass()] | ||
)) | ||
|
||
self.assert_ast(code % (left, right), non_optimized_target, optimized_target) | ||
|
||
def test_folding_subscript(self): | ||
code = "(1,)[0]" | ||
|
||
non_optimized_target = self.wrap_expr( | ||
ast.Subscript(value=ast.Tuple(elts=[ast.Constant(value=1)]), slice=ast.Constant(value=0)) | ||
) | ||
optimized_target = self.wrap_expr(ast.Constant(value=1)) | ||
|
||
self.assert_ast(code, non_optimized_target, optimized_target) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
Comment on lines
+3249
to
+3250
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somehow get lost during the splitting of ast tests, sorry. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is no longer needed since there's no
unittest
insnippets.py