Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 676717d

Browse files
committed
Enable optimizer for Python 3.7 mode
Summary: This actually turns the optimizer on when we're using the Python 3.7 compiler. We run the optimizer before doing the name binding analysis because it can end up removing functions, and we depend upon object identity to resolve scopes. Test Plan: ./python -m test.test_compiler
1 parent 22521a4 commit 676717d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

compiler/pycodegen.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_PRINT_FUNCTION,
1717
CO_COROUTINE, CO_ASYNC_GENERATOR, CO_FUTURE_BARRY_AS_BDFL, CO_FUTURE_GENERATOR_STOP,
1818
CO_FUTURE_ANNOTATIONS)
19+
from compiler.optimizer import AstOptimizer
1920
from compiler.unparse import to_expr
2021
from .visitor import ASTVisitor
2122

@@ -1951,6 +1952,17 @@ def flow_graph(cls, name, filename, args=(), kwonlyargs=(), starargs=(), optimiz
19511952

19521953

19531954
class Python37CodeGenerator(CodeGenerator):
1955+
@classmethod
1956+
def make_code_gen(cls, name, tree, filename):
1957+
tree = AstOptimizer().visit(tree)
1958+
s = symbols.SymbolVisitor()
1959+
walk(tree, s)
1960+
1961+
graph = cls.flow_graph(name, filename)
1962+
code_gen = cls(tree, s.scopes, graph = graph)
1963+
walk(tree, code_gen)
1964+
return code_gen
1965+
19541966
def visitCall(self, node):
19551967
if (node.keywords or
19561968
not isinstance(node.func, ast.Attribute) or

test_compiler/test_py37.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def test_compile_opt_chained_cmp_op(self):
147147
graph = self.to_graph('assert not a > b > c', CodeGenerator)
148148
self.assertInGraph(graph, 'UNARY_NOT')
149149

150+
def test_compile_opt_enabled(self):
151+
graph = self.to_graph('x = -1', Python37CodeGenerator)
152+
self.assertNotInGraph(graph, 'UNARY_NEGATIVE')
153+
154+
graph = self.to_graph('x = -1', CodeGenerator)
155+
self.assertInGraph(graph, 'UNARY_NEGATIVE')
156+
150157
def test_ast_optimizer(self):
151158
cases = [
152159
("+1", "1"),

0 commit comments

Comments
 (0)