Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ast.literal_eval: SyntaxError: unexpected EOF while parsing

Tags:

python

When trying to parse an empty string I get a SyntaxError. Why does it raise a different error than parsing a 'foo'? In the source of ast.literal_eval only ValueError is explicitly raised.

In [1]: import ast

In [2]: ast.literal_eval('foo')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-d8695a7c4a9f> in <module>()
----> 1 ast.literal_eval('foo')

/usr/lib/python2.7/ast.pyc in literal_eval(node_or_string)
     78                 return left - right
     79         raise ValueError('malformed string')
---> 80     return _convert(node_or_string)
     81 
     82 

/usr/lib/python2.7/ast.pyc in _convert(node)
     77             else:
     78                 return left - right
---> 79         raise ValueError('malformed string')
     80     return _convert(node_or_string)
     81 

ValueError: malformed string

In [3]: ast.literal_eval('')
  File "<unknown>", line 0

    ^
SyntaxError: unexpected EOF while parsing
like image 849
root Avatar asked Jan 31 '13 20:01

root


People also ask

What is AST literal_eval in Python?

The ast. literal_eval method is one of the helper functions that helps traverse an abstract syntax tree. This function evaluates an expression node or a string consisting of a Python literal or container display.


1 Answers

ast uses compile to compile the source string (which must be an expression) into an AST.

If the source string is not a valid expression (like an empty string), a SyntaxError will be raised by compile. If, on the other hand, the source string would be a valid expression (e.g. a variable name like foo), compile will succeed but then literal_eval might fail with a ValueError.

Therefore, you should catch both SyntaxError and ValueError when using literal_eval.

like image 92
nneonneo Avatar answered Oct 19 '22 12:10

nneonneo