]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41887: omit leading spaces/tabs on ast.literal_eval (#22469)
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>
Sun, 4 Oct 2020 00:46:44 +0000 (03:46 +0300)
committerGitHub <noreply@github.com>
Sun, 4 Oct 2020 00:46:44 +0000 (17:46 -0700)
Also document that eval() does this (the same way).

Doc/library/ast.rst
Doc/library/functions.rst
Lib/ast.py
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2020-09-30-23-49-42.bpo-41887.-ee2S-.rst [new file with mode: 0644]

index 755c60fba64115ac76efd59b5fec4b61ee5af3c3..62138efcce9110c059a199b8d2a94e3b1078e9e0 100644 (file)
@@ -1586,6 +1586,9 @@ and classes for traversing abstract syntax trees:
    .. versionchanged:: 3.9
       Now supports creating empty sets with ``'set()'``.
 
+   .. versionchanged:: 3.10
+      For string inputs, leading spaces and tabs are now stripped.
+
 
 .. function:: get_docstring(node, clean=True)
 
@@ -1820,4 +1823,4 @@ to stdout.  Otherwise, the content is read from stdin.
     `Parso <https://parso.readthedocs.io>`_ is a Python parser that supports
     error recovery and round-trip parsing for different Python versions (in
     multiple Python versions). Parso is also able to list multiple syntax errors
-    in your python file.
\ No newline at end of file
+    in your python file.
index c49bb0c9de70cacfeefea132763182575d100b28..263c52a63dea848fd321f6838c26155274d650dc 100644 (file)
@@ -506,6 +506,9 @@ are always available.  They are listed here in alphabetical order.
    returns the current global and local dictionary, respectively, which may be
    useful to pass around for use by :func:`eval` or :func:`exec`.
 
+   If the given source is a string, then leading and trailing spaces and tabs
+   are stripped.
+
    See :func:`ast.literal_eval` for a function that can safely evaluate strings
    with expressions containing only literals.
 
index d860917f4d03ae32e2582924c9471d0bfef38cb4..d8bd3373701dec60b5984ecfc2b0e4edb913a18d 100644 (file)
@@ -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):
index 5f57ce8724482af4fcc0611805ded5bf3482e4e9..be4b0f78ce9053e69cf280eb2aeb74cf05ed6453 100644 (file)
@@ -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',
diff --git a/Misc/NEWS.d/next/Library/2020-09-30-23-49-42.bpo-41887.-ee2S-.rst b/Misc/NEWS.d/next/Library/2020-09-30-23-49-42.bpo-41887.-ee2S-.rst
new file mode 100644 (file)
index 0000000..2a43ab3
--- /dev/null
@@ -0,0 +1,2 @@
+Strip leading spaces and tabs on :func:`ast.literal_eval`. Also document
+stripping of spaces and tabs for :func:`eval`.