|
| 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 |
0 commit comments