bpo-39159: Declare errors that might be raised from literal_eval#19899
Merged
rhettinger merged 1 commit intopython:masterfrom Dec 22, 2020
Merged
bpo-39159: Declare errors that might be raised from literal_eval#19899rhettinger merged 1 commit intopython:masterfrom
rhettinger merged 1 commit intopython:masterfrom
Conversation
rhettinger
reviewed
May 5, 2020
| 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``. |
Contributor
There was a problem hiding this comment.
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().
rhettinger
reviewed
May 5, 2020
| It can raise :exc:`ValueError`, :exc:`TypeError`, :exc:`SyntaxError`, | ||
| :exc:`MemoryError` and :exc:`RecursionError` depending on the malformed | ||
| input. | ||
|
|
Contributor
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
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 Falsedoes something like this suits?
Member
Author
There was a problem hiding this comment.
@rhettinger or maybe we can put this in an actual test in test_ast and refer it? How does that sounds?
Contributor
|
Ping, @rhettinger? |
Contributor
|
Triggering a rebuild. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://bugs.python.org/issue39159