-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Conversation
@@ -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``. |
There was a problem hiding this comment.
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. | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
Ping, @rhettinger? |
Triggering a rebuild. |
https://bugs.python.org/issue39159