[issue41887] ast.literal_eval does not accept strings with leading whitespaces

Terry J. Reedy report at bugs.python.org
Fri Oct 2 20:25:02 EDT 2020


Terry J. Reedy <tjreedy at udel.edu> added the comment:

The doc for literal_eval says "evaluate ... a string containing a Python literal or container display."  To me, ' 1' qualifies, just as it does as an expression for eval().

The exception comes from parsing raising IndentationError with leading whitespace even when the mode is 'eval' rather than 'exec'.  This surprised me. Eval() gets strips the beginning of the string before it is parsed.  If parsing remains as is, I agree that doing the same for literal_eval strings.  But why should not parsing remove indents for 'eval' mode?

----------
nosy: +terry.reedy
versions: +Python 3.10 -Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41887>
_______________________________________


More information about the Python-bugs-list mailing list