Skip to content

Commit 1ecf7e6

Browse files
committed
Add constant folding option
1 parent 8d04ab5 commit 1ecf7e6

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/python_minifier/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def minify(
105105
:param bool remove_debug: If conditional statements that test '__debug__ is True' should be removed
106106
:param bool remove_explicit_return_none: If explicit return None statements should be replaced with a bare return
107107
:param bool remove_builtin_exception_brackets: If brackets should be removed when raising exceptions with no arguments
108-
:param bool constant_folding: If constant expressions should be evaluated
108+
:param bool constant_folding: If literal expressions should be evaluated
109109
110110
:rtype: str
111111
@@ -155,9 +155,7 @@ def minify(
155155
module = RemoveExplicitReturnNone()(module)
156156

157157
if constant_folding:
158-
#print(print_ast(module))
159158
module = FoldConstants()(module)
160-
#print(print_ast(module))
161159

162160
bind_names(module)
163161
resolve_names(module)
@@ -223,8 +221,6 @@ def unparse(module):
223221
printer = ModulePrinter()
224222
printer(module)
225223

226-
#print(printer.code)
227-
228224
try:
229225
minified_module = ast.parse(printer.code, 'python_minifier.unparse output')
230226
except SyntaxError as syntax_error:

src/python_minifier/__main__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ def parse_args():
190190
help='Disable removing brackets when raising builtin exceptions with no arguments',
191191
dest='remove_exception_brackets',
192192
)
193+
minification_options.add_argument(
194+
'--no-constant-folding',
195+
action='store_false',
196+
help='Disable evaluating literal expressions',
197+
dest='constant_folding',
198+
)
193199

194200
annotation_options = parser.add_argument_group('remove annotations options', 'Options that affect how annotations are removed')
195201
annotation_options.add_argument(
@@ -308,7 +314,8 @@ def do_minify(source, filename, minification_args):
308314
remove_asserts=minification_args.remove_asserts,
309315
remove_debug=minification_args.remove_debug,
310316
remove_explicit_return_none=minification_args.remove_explicit_return_none,
311-
remove_builtin_exception_brackets=minification_args.remove_exception_brackets
317+
remove_builtin_exception_brackets=minification_args.remove_exception_brackets,
318+
constant_folding=minification_args.constant_folding
312319
)
313320

314321

src/python_minifier/transforms/constant_folding.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,23 @@ def visit_BinOp(self, node):
8181
# In python code it should be '1e999+0j', which parses as a BinOp that the expression printer can handle.
8282
# It's not worth fixing the expression printer to handle this case, since it is unlikely to occur in real code.
8383
return node
84-
raise
8584

86-
if isinstance(value, float) and math.isnan(value):
87-
assert math.isnan(folded_value)
88-
else:
89-
assert folded_value == value and type(folded_value) == type(value)
85+
# Some other NameError...
86+
return node
87+
except Exception:
88+
return node
9089

91-
#print(f'{original_expression=}')
92-
#print(f'{folded_expression=}')
90+
# Check the folded value is the same as the original value
91+
if not equal_value_and_type(folded_value, value):
92+
return node
9393

9494
return self.add_child(new_node, node.parent, node.namespace)
95+
96+
def equal_value_and_type(a, b):
97+
if type(a) != type(b):
98+
return False
99+
100+
if isinstance(a, float) and math.isnan(a) and not math.isnan(b):
101+
return False
102+
103+
return a == b

0 commit comments

Comments
 (0)