[issue15245] ast.literal_eval fails on some literals

João Bernardo report at bugs.python.org
Thu Jul 5 17:48:05 CEST 2012


João Bernardo <jbvsmo at gmail.com> added the comment:

Of course `nan` and `inf` are part of the syntax! The `ast.parse` function recognize them as `Name`. 

So that works:

>>> ast.dump(ast.parse('True'))
"Module(body=[Expr(value=Name(id='True', ctx=Load()))])"

>>> ast.dump(ast.parse('inf'))
"Module(body=[Expr(value=Name(id='inf', ctx=Load()))])"

>>> inf = float('inf')
>>> eval('inf')
inf

I've run into some literals with `Ellipsis` and `inf` and couldn't load them with literal_eval. That's why I'm proposing that.

The thing about `nan` and `inf` is because they are the *only* representations of float numbers produced by the interpreter that cannot be loaded.

Something like that could solve the problem keeping `literal_eval` safe and allowing other names:

ast.literal_eval('[1.0, 2.0, inf]', {'inf': float('inf')})

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15245>
_______________________________________


More information about the Python-bugs-list mailing list