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

Commit 977a699

Browse files
committed
Add __debug__ load support
Summary: Loading __debug__ turns into an ast Constant node in the AST optimizer Test Plan: ./python -m test.test_compiler
1 parent 32d3a70 commit 977a699

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

compiler/optimizer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Py37Limits:
6060

6161

6262
class AstOptimizer(ASTRewriter):
63+
def __init__(self, optimize = False):
64+
super().__init__()
65+
self.optimize = optimize
66+
6367
def visitUnaryOp(self, node: ast.UnaryOp) -> ast.expr:
6468
op = self.visit(node.operand)
6569
if is_const(op):
@@ -180,3 +184,9 @@ def visitCompare(self, node: ast.Compare) -> ast.expr:
180184
comparators[-1] = new_iter
181185

182186
return self.update_node(node, left=left, comparators=comparators)
187+
188+
def visitName(self, node: ast.Name):
189+
if node.id == "__debug__":
190+
return copy_location(Constant(not self.optimize), node)
191+
192+
return self.generic_visit(node)

test_compiler/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def dump_graph(self, graph):
5858
graph.dump(io)
5959
return io.getvalue()
6060

61+
def graph_to_instrs(self, graph):
62+
for block in graph.getBlocks():
63+
yield from block.getInstructions()
64+
6165
def assertNotInGraph(self, graph, opname, argval=_UNSPECIFIED):
6266
for block in graph.getBlocks():
6367
for instr in block.getInstructions():

test_compiler/test_py37.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ def test_compile_opt_enabled(self):
154154
graph = self.to_graph('x = -1', CodeGenerator)
155155
self.assertInGraph(graph, 'UNARY_NEGATIVE')
156156

157+
def test_opt_debug(self):
158+
graph = self.to_graph('if not __debug__:\n x = 42', Python37CodeGenerator)
159+
self.assertNotInGraph(graph, 'STORE_NAME')
160+
161+
graph = self.to_graph('if not __debug__:\n x = 42', CodeGenerator)
162+
self.assertInGraph(graph, 'STORE_NAME')
163+
164+
def test_opt_debug_del(self):
165+
code = 'def f(): del __debug__'
166+
outer_graph = self.to_graph(code, Python37CodeGenerator)
167+
for outer_instr in self.graph_to_instrs(outer_graph):
168+
if outer_instr.opname == "LOAD_CONST" and isinstance(outer_instr.oparg, CodeGenerator):
169+
graph = outer_instr.oparg.graph
170+
self.assertInGraph(graph, 'LOAD_CONST', True)
171+
self.assertNotInGraph(graph, 'DELETE_FAST', '__debug__')
172+
173+
outer_graph = self.to_graph(code, CodeGenerator)
174+
for outer_instr in self.graph_to_instrs(outer_graph):
175+
if outer_instr.opname == "LOAD_CONST" and isinstance(outer_instr.oparg, CodeGenerator):
176+
graph = outer_instr.oparg.graph
177+
self.assertNotInGraph(graph, 'LOAD_CONST', True)
178+
self.assertInGraph(graph, 'DELETE_FAST', '__debug__')
179+
180+
157181
def test_ast_optimizer(self):
158182
cases = [
159183
("+1", "1"),

0 commit comments

Comments
 (0)