Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ and classes for traversing abstract syntax trees:
Safely evaluate an expression node or a string containing a Python literal or
container display. The string or node provided may only consist of the
following Python literal structures: strings, bytes, numbers, tuples, lists,
dicts, sets, booleans, and ``None``.
dicts, sets, booleans, ``None`` and ``Ellipsis``.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also take this opportunity to be more specific about what is meant by "numbers". Replace that with "ints, floats, complex numbers".

Also, please make the same update to the docstring for ast.literal_eval().


This can be used for safely evaluating strings containing Python values from
untrusted sources without the need to parse the values oneself. It is not
Expand All @@ -1575,6 +1575,10 @@ and classes for traversing abstract syntax trees:
sufficiently large/complex string due to stack depth limitations
in Python's AST compiler.

It can raise :exc:`ValueError`, :exc:`TypeError`, :exc:`SyntaxError`,
:exc:`MemoryError` and :exc:`RecursionError` depending on the malformed
input.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my edification and for future reference, can you add a non-displayed in-line documentation comment showing an example of how each of these could happen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import ast

recursion_tuple = ast.Tuple(elts=[], ctx=ast.Load())
recursion_tuple.elts.append(recursion_tuple)
INPUTS = [
    ("invalid syntax", SyntaxError),
    ("test", ValueError),
    (ast.List(elts=None, ctx=ast.Load()), TypeError),
    (recursion_tuple, RecursionError),
]

for inp, exc_type in INPUTS:
    try:
        ast.literal_eval(inp)
    except exc_type:
        continue
    else:
        assert False

does something like this suits?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhettinger or maybe we can put this in an actual test in test_ast and refer it? How does that sounds?

.. versionchanged:: 3.2
Now allows bytes and set literals.

Expand Down