summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-10-04 00:46:44 (GMT)
committerGitHub <noreply@github.com>2020-10-04 00:46:44 (GMT)
commite799aa8b92c195735f379940acd9925961ad04ec (patch)
treea0308940ce67e0535be861bfc94f3d830bba6734 /Lib
parent7f54e563dc150cd670ca8df678437455c3a7f2cd (diff)
downloadcpython-e799aa8b92c195735f379940acd9925961ad04ec.zip
cpython-e799aa8b92c195735f379940acd9925961ad04ec.tar.gz
cpython-e799aa8b92c195735f379940acd9925961ad04ec.tar.bz2
bpo-41887: omit leading spaces/tabs on ast.literal_eval (#22469)
Also document that eval() does this (the same way).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ast.py2
-rw-r--r--Lib/test/test_ast.py6
2 files changed, 7 insertions, 1 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index d860917..d8bd337 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -59,7 +59,7 @@ def literal_eval(node_or_string):
sets, booleans, and None.
"""
if isinstance(node_or_string, str):
- node_or_string = parse(node_or_string, mode='eval')
+ node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
if isinstance(node_or_string, Expression):
node_or_string = node_or_string.body
def _raise_malformed_node(node):
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 5f57ce8..be4b0f7 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1005,6 +1005,12 @@ Module(
malformed = ast.Dict(keys=[ast.Constant(1)], values=[ast.Constant(2), ast.Constant(3)])
self.assertRaises(ValueError, ast.literal_eval, malformed)
+ def test_literal_eval_trailing_ws(self):
+ self.assertEqual(ast.literal_eval(" -1"), -1)
+ self.assertEqual(ast.literal_eval("\t\t-1"), -1)
+ self.assertEqual(ast.literal_eval(" \t -1"), -1)
+ self.assertRaises(IndentationError, ast.literal_eval, "\n -1")
+
def test_bad_integer(self):
# issue13436: Bad error message with invalid numeric values
body = [ast.ImportFrom(module='time',