Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39159: Declare errors that might be raised from literal_eval #19899

Merged
merged 1 commit into from
Dec 22, 2020

Conversation

isidentical
Copy link
Member

@isidentical isidentical commented May 4, 2020

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels May 4, 2020
@isidentical isidentical changed the title bpo-39159: Declare error that might be raised from literal_eval bpo-39159: Declare errors that might be raised from literal_eval May 4, 2020
@isidentical isidentical requested a review from rhettinger May 4, 2020 18:24
@@ -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
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().

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

Copy link
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
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
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?

@taleinat
Copy link
Contributor

Ping, @rhettinger?

@rhettinger
Copy link
Contributor

Triggering a rebuild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants