-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathconstant_folding.py
More file actions
157 lines (120 loc) · 5.53 KB
/
constant_folding.py
File metadata and controls
157 lines (120 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import math
import sys
import python_minifier.ast_compat as ast
from python_minifier.ast_annotation import get_parent
from python_minifier.ast_compare import compare_ast
from python_minifier.expression_printer import ExpressionPrinter
from python_minifier.transforms.suite_transformer import SuiteTransformer
from python_minifier.util import is_constant_node
def is_foldable_constant(node):
"""
Check if a node is a constant expression that can participate in folding.
We can asume that children have already been folded, so foldable constants are either:
- Simple literals (Num, NameConstant)
- UnaryOp(USub/Invert) on a Num - these don't fold to shorter forms,
so they remain after child visiting. UAdd and Not would have been
folded away since they always produce shorter results.
"""
if is_constant_node(node, (ast.Num, ast.NameConstant)):
return True
if isinstance(node, ast.UnaryOp):
if isinstance(node.op, (ast.USub, ast.Invert)):
return is_constant_node(node.operand, ast.Num)
return False
class FoldConstants(SuiteTransformer):
"""
Fold Constants if it would reduce the size of the source
"""
def __init__(self):
super(FoldConstants, self).__init__()
def fold(self, node):
# Evaluate the expression
try:
original_expression = unparse_expression(node)
original_value = safe_eval(original_expression)
except Exception:
return node
# Choose the best representation of the value
if isinstance(original_value, float) and math.isnan(original_value):
# There is no nan literal.
# we could use float('nan'), but that complicates folding as it's not a Constant
return node
elif isinstance(original_value, bool):
new_node = ast.NameConstant(value=original_value)
elif isinstance(original_value, (int, float, complex)):
try:
if repr(original_value).startswith('-') and not sys.version_info < (3, 0):
# Represent negative numbers as a USub UnaryOp, so that the ast roundtrip is correct
new_node = ast.UnaryOp(op=ast.USub(), operand=ast.Num(n=-original_value))
else:
new_node = ast.Num(n=original_value)
except Exception:
# repr(value) failed, most likely due to some limit
return node
else:
return node
# Evaluate the new value representation
try:
folded_expression = unparse_expression(new_node)
folded_value = safe_eval(folded_expression)
except Exception:
# This can happen if the value is too large to be represented as a literal
# or if the value is unparsed as nan, inf or -inf - which are not valid python literals
return node
if len(folded_expression) >= len(original_expression):
# Result is not shorter than original expression
return node
# Check the folded expression parses back to the same AST
try:
folded_ast = ast.parse(folded_expression, 'folded expression', mode='eval')
compare_ast(new_node, folded_ast.body)
except Exception:
# This can happen if the printed value doesn't parse back to the same AST
# e.g. complex numbers can be parsed as BinOp
return node
# Check the folded value is the same as the original value
if not equal_value_and_type(folded_value, original_value):
return node
# New representation is shorter and has the same value, so use it
return self.add_child(new_node, get_parent(node), node.namespace)
def visit_BinOp(self, node):
node.left = self.visit(node.left)
node.right = self.visit(node.right)
# Check this is a constant expression that could be folded
# We don't try to fold strings or bytes, since they have probably been arranged this way to make the source shorter and we are unlikely to beat that
if not is_foldable_constant(node.left):
return node
if not is_foldable_constant(node.right):
return node
if isinstance(node.op, ast.Div):
# Folding div is subtle, since it can have different results in Python 2 and Python 3
# Do this once target version options have been implemented
return node
if isinstance(node.op, ast.Pow):
# This can be folded, but it is unlikely to reduce the size of the source
# It can also be slow to evaluate
return node
return self.fold(node)
def visit_UnaryOp(self, node):
node.operand = self.visit(node.operand)
# Only fold if the operand is a foldable constant
if not is_foldable_constant(node.operand):
return node
# Only fold these unary operators
if not isinstance(node.op, (ast.USub, ast.UAdd, ast.Invert, ast.Not)):
return node
return self.fold(node)
def equal_value_and_type(a, b):
if type(a) != type(b):
return False
if isinstance(a, float) and math.isnan(a) and not math.isnan(b):
return False
return a == b
def safe_eval(expression):
empty_globals = {}
empty_locals = {}
# This will return the value, or could raise an exception
return eval(expression, empty_globals, empty_locals)
def unparse_expression(node):
expression_printer = ExpressionPrinter()
return expression_printer(node)