-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-39159: Declare errors that might be raised from literal_eval #19899
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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``. | ||
|
|
||
| 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 | ||
|
|
@@ -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. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Falsedoes something like this suits?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rhettinger or maybe we can put this in an actual test in |
||
| .. versionchanged:: 3.2 | ||
| Now allows bytes and set literals. | ||
|
|
||
|
|
||
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().