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

Saiyang Gou report at bugs.python.org
Tue Sep 29 21:46:57 EDT 2020


New submission from Saiyang Gou <gousaiyang223 at gmail.com>:

`ast.literal_eval` does not accept code with leading whitespaces, while `eval` accepts them, which is an inconsistency.

```
>>> import ast
>>> eval(' 1')
1
>>> ast.literal_eval(' 1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/ast.py", line 62, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    1
IndentationError: unexpected indent
```

----------
components: Library (Lib)
messages: 377687
nosy: gousaiyang
priority: normal
severity: normal
status: open
title: ast.literal_eval does not accept strings with leading whitespaces
type: enhancement
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

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


More information about the Python-bugs-list mailing list