Skip to content

Commit bcaf96f

Browse files
committed
Ignore huge values
1 parent 5314549 commit bcaf96f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/python_minifier/transforms/constant_folding.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,23 @@ def visit_BinOp(self, node):
3535

3636
if isinstance(value, float) and math.isnan(value):
3737
# There is no nan literal.
38-
new_node = ast.Call(func=ast.Name('float', ctx=ast.Load()), args=[ast.Str('nan')], keywords=[])
38+
new_node = ast.Call(func=ast.Name(id='float', ctx=ast.Load()), args=[ast.Str(s='nan')], keywords=[])
3939
elif isinstance(value, str):
4040
new_node = ast.Str(s=value)
4141
elif isinstance(value, bytes):
4242
new_node = ast.Bytes(s=value)
4343
elif isinstance(value, bool):
4444
new_node = ast.NameConstant(value=value)
4545
elif isinstance(value, (int, float, complex)):
46-
if repr(value).startswith('-'):
47-
# Represent negative numbers as a USub UnaryOp, so that the ast roundtrip is correct
48-
new_node = ast.UnaryOp(op=ast.USub(), operand=ast.Num(n=-value))
49-
else:
50-
new_node = ast.Num(n=value)
46+
try:
47+
if repr(value).startswith('-'):
48+
# Represent negative numbers as a USub UnaryOp, so that the ast roundtrip is correct
49+
new_node = ast.UnaryOp(op=ast.USub(), operand=ast.Num(n=-value))
50+
else:
51+
new_node = ast.Num(n=value)
52+
except Exception:
53+
# repr(value) failed, most likely due to some limit
54+
return node
5155
else:
5256
return node
5357

0 commit comments

Comments
 (0)