Skip to content

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
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