Skip to content

Commit 826e813

Browse files
committed
Add dead branch removal
- Add constant folding for BoolOp and Compare nodes - Add RemoveDeadBranches transform to safely remove dead If branches and in-line the surviving branch, only when it doesn't change program behaviour - Re-write RemoveDebug transform to explicitly mark __debug__ as False, and rely on the dead branch removal to trim it This fixes some problems with the previous RemoveDebug transform: - The whole branch was removed with accounting for how it affects enclosing function symbol table - The else branch would be removed along with the body Also fixes SuiteTransformer not transforming some suites like exception handler blocks.
1 parent 3f4c582 commit 826e813

6 files changed

Lines changed: 332 additions & 14 deletions

File tree

‎src/python_minifier/__init__.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from python_minifier.ast_annotation import add_parent
1111

1212
from python_minifier.ast_compare import CompareError, compare_ast
13+
from python_minifier.ast_printer import print_ast
1314
from python_minifier.module_printer import ModulePrinter
1415
from python_minifier.rename import (
1516
add_namespace,
@@ -25,6 +26,7 @@
2526
from python_minifier.transforms.remove_annotations import RemoveAnnotations
2627
from python_minifier.transforms.remove_annotations_options import RemoveAnnotationsOptions
2728
from python_minifier.transforms.remove_asserts import RemoveAsserts
29+
from python_minifier.transforms.remove_dead_branches import RemoveDeadBranches
2830
from python_minifier.transforms.remove_debug import RemoveDebug
2931
from python_minifier.transforms.remove_exception_brackets import remove_no_arg_exception_call
3032
from python_minifier.transforms.remove_explicit_return_none import RemoveExplicitReturnNone
@@ -74,6 +76,7 @@ def minify(
7476
remove_builtin_exception_brackets=True,
7577
constant_folding=True,
7678
prefer_single_line=False,
79+
remove_dead_branches=True
7780
):
7881
"""
7982
Minify a python module
@@ -109,6 +112,7 @@ def minify(
109112
:param bool remove_builtin_exception_brackets: If brackets should be removed when raising exceptions with no arguments
110113
:param bool constant_folding: If literal expressions should be evaluated
111114
:param bool prefer_single_line: If semi-colons should be preferred over newlines where there is no difference in output size
115+
:param bool remove_dead_branches: If if-statements with a constant False test should be removed
112116
113117
:rtype: str
114118
@@ -155,12 +159,15 @@ def minify(
155159
if remove_debug:
156160
module = RemoveDebug()(module)
157161

158-
if remove_explicit_return_none:
159-
module = RemoveExplicitReturnNone()(module)
160-
161162
if constant_folding:
162163
module = FoldConstants()(module)
163164

165+
if remove_dead_branches:
166+
module = RemoveDeadBranches()(module)
167+
168+
if remove_explicit_return_none:
169+
module = RemoveExplicitReturnNone()(module)
170+
164171
bind_names(module)
165172
resolve_names(module)
166173

‎src/python_minifier/__main__.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ def parse_args():
250250
help='Disable evaluating literal expressions',
251251
dest='constant_folding',
252252
)
253+
minification_options.add_argument(
254+
'--no-remove-dead-branches',
255+
action='store_false',
256+
help='Disable removing branches that are never executed',
257+
dest='remove_dead_branches',
258+
)
253259

254260
annotation_options = parser.add_argument_group('remove annotations options', 'Options that affect how annotations are removed')
255261
annotation_options.add_argument(
@@ -305,6 +311,10 @@ def parse_args():
305311
sys.stderr.write('error: --remove-class-attribute-annotations would do nothing when used with --no-remove-annotations\n')
306312
sys.exit(1)
307313

314+
if args.remove_debug and not args.remove_dead_branches:
315+
sys.stderr.write('error: --remove-debug would do nothing when used with --no-remove-dead-branches\n')
316+
sys.exit(1)
317+
308318
return args
309319

310320

‎src/python_minifier/transforms/constant_folding.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def is_foldable_constant(node):
1414
"""
1515
Check if a node is a constant expression that can participate in folding.
1616
17-
We can asume that children have already been folded, so foldable constants are either:
17+
We can assume that children have already been folded, so foldable constants are either:
1818
- Simple literals (Num, NameConstant)
1919
- UnaryOp(USub/Invert) on a Num - these don't fold to shorter forms,
2020
so they remain after child visiting. UAdd and Not would have been
@@ -119,6 +119,30 @@ def visit_BinOp(self, node):
119119

120120
return self.fold(node)
121121

122+
def visit_BoolOp(self, node):
123+
node.values = [self.visit(v) for v in node.values]
124+
125+
# Check this is a constant expression that could be folded
126+
if not all(is_foldable_constant(v) for v in node.values):
127+
return node
128+
129+
return self.fold(node)
130+
131+
def visit_Compare(self, node):
132+
node.left = self.visit(node.left)
133+
node.comparators = [self.visit(c) for c in node.comparators]
134+
135+
operands = [node.left] + node.comparators
136+
if not all(is_foldable_constant(n) for n in operands):
137+
return node
138+
139+
if any(isinstance(op, (ast.Is, ast.IsNot)) for op in node.ops):
140+
# Identity is only language-guaranteed for the singletons
141+
if not all(is_constant_node(n, ast.NameConstant) for n in operands):
142+
return node
143+
144+
return self.fold(node)
145+
122146
def visit_UnaryOp(self, node):
123147

124148
node.operand = self.visit(node.operand)
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import python_minifier.ast_compat as ast
2+
from python_minifier.rename.util import is_namespace
3+
4+
from python_minifier.transforms.suite_transformer import SuiteTransformer
5+
from python_minifier.util import is_constant_node
6+
7+
class NamespaceProperties(object):
8+
"""
9+
The analysed properties of a namespace, including the names defined in it and whether it is a generator
10+
"""
11+
12+
def __init__(self):
13+
self.local_names = set()
14+
self.nonlocal_names = set()
15+
self.global_names = set()
16+
self.is_generator = False
17+
18+
def __eq__(self, other):
19+
assert isinstance(other, NamespaceProperties)
20+
return (
21+
self.local_names == other.local_names and
22+
self.nonlocal_names == other.nonlocal_names and
23+
self.global_names == other.global_names and
24+
self.is_generator is other.is_generator
25+
)
26+
27+
def __ne__(self, other):
28+
return not self == other
29+
30+
def __repr__(self):
31+
return 'NamespaceProperties(local_names=%r, nonlocal_names=%r, global_names=%r, is_generator=%r)' % (
32+
self.local_names, self.nonlocal_names, self.global_names, self.is_generator
33+
)
34+
35+
36+
def in_function_scope(namespace):
37+
"""
38+
Is this namespace nested in a function scope?
39+
40+
A name bound anywhere in a class body - even in unreachable code - makes
41+
loads of that name in the class body fall through to the global scope,
42+
instead of an enclosing function scope cell. Removing such a binding is
43+
only safe when there is no enclosing function scope to fall through to.
44+
"""
45+
while not isinstance(namespace, ast.Module):
46+
if isinstance(namespace, (ast.FunctionDef, ast.AsyncFunctionDef)):
47+
return True
48+
namespace = namespace.namespace
49+
return False
50+
51+
52+
class RemoveDeadBranches(SuiteTransformer):
53+
"""
54+
Remove if statements where the condition tests False
55+
"""
56+
57+
def __call__(self, node):
58+
# Suites that have been condemned during this run, but may not have been
59+
# detached from the tree yet. Shared across all suites so that decisions
60+
# in nested or sibling suites don't count already-removed bindings.
61+
self._removed_suites = []
62+
return self.visit(node)
63+
64+
def get_namespace_properties(self, namespace, removed_suites=None):
65+
"""
66+
Gather names in the given namespace
67+
68+
Gather three sets of names: local, nonlocal and global.
69+
70+
Doesn't recurse into child namespaces, and doesn't search the except_suite, which is assumed to be a branch that will be removed.
71+
72+
:param namespace:
73+
:param except_branch:
74+
:rtype: set[str]
75+
"""
76+
if removed_suites is None:
77+
removed_suites = []
78+
79+
properties = NamespaceProperties()
80+
81+
def explore_namespace(node):
82+
if isinstance(node, ast.Name):
83+
if isinstance(node.ctx, (ast.Store, ast.Del, ast.Param)):
84+
properties.local_names.add(node.id)
85+
elif isinstance(node, ast.ClassDef):
86+
properties.local_names.add(node.name)
87+
elif isinstance(node, ast.FunctionDef):
88+
properties.local_names.add(node.name)
89+
elif isinstance(node, ast.AsyncFunctionDef):
90+
properties.local_names.add(node.name)
91+
elif isinstance(node, ast.alias):
92+
# What about import *
93+
94+
if node.asname is not None:
95+
properties.local_names.add(node.asname)
96+
else:
97+
properties.local_names.add(node.name.split('.')[0])
98+
elif isinstance(node, ast.arguments):
99+
if isinstance(node.vararg, str):
100+
properties.local_names.add(node.vararg)
101+
if isinstance(node.kwarg, str):
102+
properties.local_names.add(node.kwarg)
103+
elif isinstance(node, ast.arg):
104+
properties.local_names.add(node.arg)
105+
elif isinstance(node, ast.ExceptHandler):
106+
if isinstance(node.name, str):
107+
properties.local_names.add(node.name)
108+
109+
elif isinstance(node, ast.Global):
110+
properties.global_names.update(node.names)
111+
elif isinstance(node, ast.Nonlocal):
112+
properties.nonlocal_names.update(node.names)
113+
114+
elif isinstance(node, ast.MatchAs):
115+
if isinstance(node.name, str):
116+
properties.local_names.add(node.name)
117+
elif isinstance(node, ast.MatchStar):
118+
if isinstance(node.name, str):
119+
properties.local_names.add(node.name)
120+
elif isinstance(node, ast.MatchMapping):
121+
if isinstance(node.rest, str):
122+
properties.local_names.add(node.rest)
123+
124+
elif isinstance(node, ast.TypeVar):
125+
properties.local_names.add(node.name)
126+
elif isinstance(node, ast.TypeVarTuple):
127+
properties.local_names.add(node.name)
128+
elif isinstance(node, ast.ParamSpec):
129+
properties.local_names.add(node.name)
130+
131+
elif isinstance(node, ast.Yield) or isinstance(node, ast.YieldFrom):
132+
properties.is_generator = True
133+
134+
if is_namespace(node) and node is not namespace:
135+
# A nested namespace. The name it binds (recorded above) belongs to
136+
# this namespace, but its contents do not - don't descend into it.
137+
return
138+
139+
for name, field in ast.iter_fields(node):
140+
if field in removed_suites:
141+
continue
142+
143+
if isinstance(field, ast.AST):
144+
explore_namespace(field)
145+
elif isinstance(field, list):
146+
for item in field:
147+
if isinstance(item, ast.AST):
148+
explore_namespace(item)
149+
150+
explore_namespace(namespace)
151+
152+
return properties
153+
154+
def changes_semantics(self, namespace, candidate_suite):
155+
"""
156+
Does removing this branch change the semantics of the program?
157+
158+
:param namespace: The namespace of the If statement
159+
:param candidate_suite: The branch that is being removed
160+
:return: True if removing the branch changes the semantics of the program, False otherwise
161+
"""
162+
163+
# Gather properties in the namespace, excluding suites already condemned
164+
properties = self.get_namespace_properties(namespace, removed_suites=self._removed_suites)
165+
166+
# Gather properties in the namespace, additionally excluding the candidate branch
167+
candidate_properties = self.get_namespace_properties(namespace, removed_suites=[candidate_suite] + self._removed_suites)
168+
169+
if isinstance(namespace, ast.Module):
170+
# The module namespace is dynamic, removing unreachable bindings doesn't
171+
# change their resolution behaviour, so we can safely remove them.
172+
return properties.is_generator != candidate_properties.is_generator or properties.nonlocal_names != candidate_properties.nonlocal_names or properties.global_names != candidate_properties.global_names
173+
elif isinstance(namespace, ast.ClassDef) and not in_function_scope(namespace):
174+
# Class namespaces are also dynamic, but a binding in the class body stops
175+
# loads deferring to an enclosing function scope cell, so unreachable local
176+
# bindings can only be ignored when no function scope encloses the class.
177+
return properties.is_generator != candidate_properties.is_generator or properties.nonlocal_names != candidate_properties.nonlocal_names or properties.global_names != candidate_properties.global_names
178+
else:
179+
return properties != candidate_properties
180+
181+
def remove_false_branches(self, node_list):
182+
suite = []
183+
184+
for node in node_list:
185+
if not isinstance(node, ast.If):
186+
suite.append(self.visit(node))
187+
continue
188+
189+
# This is an If statement
190+
191+
if hasattr(node, 'resolved_test'):
192+
condition = node.resolved_test
193+
elif is_constant_node(node.test, ast.NameConstant) and isinstance(node.test.value, bool):
194+
condition = node.test.value
195+
else:
196+
# The test is not a constant, so we can't remove this branch
197+
suite.append(self.visit(node))
198+
continue
199+
200+
# We can possibly remove one of the branches, but we need to check if it changes the semantics of the program
201+
202+
if condition is True:
203+
if self.changes_semantics(node.namespace, node.orelse):
204+
# Removing the else branch changes the semantics of the program, so we can't remove it
205+
suite.append(self.visit(node))
206+
else:
207+
# The else branch is dead, keep only the body
208+
self._removed_suites.append(node.orelse)
209+
suite.extend(self.remove_false_branches(node.body))
210+
211+
elif condition is False:
212+
if self.changes_semantics(node.namespace, node.body):
213+
# Removing the body branch changes the semantics of the program, so we can't remove it
214+
suite.append(self.visit(node))
215+
else:
216+
# The body is dead, keep only the else branch
217+
self._removed_suites.append(node.body)
218+
suite.extend(self.remove_false_branches(node.orelse))
219+
220+
return suite
221+
222+
def suite(self, node_list, parent):
223+
224+
without_dead_branches = self.remove_false_branches(node_list)
225+
226+
if len(without_dead_branches) == 0:
227+
if isinstance(parent, ast.Module):
228+
return []
229+
else:
230+
return [self.add_child(ast.Expr(value=ast.Num(0)), parent=parent)]
231+
232+
return without_dead_branches

‎src/python_minifier/transforms/remove_debug.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def __call__(self, node):
1818

1919
def constant_value(self, node):
2020
if sys.version_info < (3, 4):
21-
return node.id == 'True'
21+
# True and False are Name nodes before python 3.4.
22+
# The comparator may be any expression, so check it is a Name.
23+
if isinstance(node, ast.Name) and node.id in ('True', 'False'):
24+
return node.id == 'True'
25+
return None
2226
elif is_constant_node(node, ast.NameConstant):
2327
return node.value
2428
return None
@@ -61,14 +65,10 @@ def is_truthy_debug_comparison():
6165
return True
6266
return False
6367

64-
def suite(self, node_list, parent):
68+
def visit_If(self, node):
69+
assert isinstance(node, ast.If)
6570

66-
without_debug = [self.visit(a) for a in filter(lambda n: not self.can_remove(n), node_list)]
71+
if self.can_remove(node):
72+
node.resolved_test = False
6773

68-
if len(without_debug) == 0:
69-
if isinstance(parent, ast.Module):
70-
return []
71-
else:
72-
return [self.add_child(ast.Expr(value=ast.Num(0)), parent=parent)]
73-
74-
return without_debug
74+
return self.generic_visit(node)

0 commit comments

Comments
 (0)