berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
810 stars 102 forks source link

Can't generate code containing ast.Num(NaN) #85

Closed Kodiologist closed 6 years ago

Kodiologist commented 6 years ago

Somewhat related to #82, the codegen of astor 0.5 would produce nan if given a Num node that contained NaN. In astor 0.6, the following raises an AssertionError in code_gen.py:

import ast, astor

nan = 1e1000 - 1e1000

print(astor.code_gen.to_source(
         ast.Module([ast.Expr(ast.BinOp(
             ast.Num(1.0),
             ast.Add(),
             ast.Num(nan)))])))

While Python has no NaN literal, you could represent ast.Num(nan) as an expression (such as 1e1000 - 1e1000) if you want to generate real Python. See hylang/hy#1447.

pmaupin commented 6 years ago

We can add this if someone submits a PR, but of course, it won't round-trip AST -> Python -> AST properly, so any test for this will have to special-case this.

berkerpeksag commented 6 years ago

@Kodiologist I'm planning to release 0.7.0 this weekend and I can merge a fix for this if you still need it for Hy.

Kodiologist commented 6 years ago

It would be convenient, but it's not necessary. I worked around the issue in Hy commit a074bb9a5c3 by replacing NaN with such expressions in the compiler.