-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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-32888: Improve exception message in ast.literal_eval #340
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 |
---|---|---|
|
@@ -583,6 +583,19 @@ def test_literal_eval_complex(self): | |
self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)') | ||
self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)') | ||
|
||
def test_literal_eval_message(self): | ||
# Issue #32888 | ||
tests = { | ||
"2 * 5": "BinOp not allowed in literal", | ||
"[] + []": "List not allowed in binary +/-", | ||
"+''": "Str not allowed in unary +/-", | ||
ast.BinOp(ast.Num(1), ast.Add(), 'oops'): "malformed node or string: 'oops'", | ||
} | ||
for test, message in tests.items(): | ||
with self.assertRaises(ValueError) as cm: | ||
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. A helpful thing to use in between lines 594 and 595 would be Check out https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests |
||
ast.literal_eval(test) | ||
self.assertIn(message, str(cm.exception)) | ||
|
||
def test_bad_integer(self): | ||
# issue13436: Bad error message with invalid numeric values | ||
body = [ast.ImportFrom(module='time', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Improve wording of error messages from ast.literal_eval, distinguishing | ||
valid-but-unacceptable nodes from those that are actually malformed. | ||
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. Please add "Patch by..." 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. Should that be done by amending the current commit, or adding an additional commit? 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. It is better for review to add a new commit. In any case all commits are squashed before merging into the repository. 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. 👍 |
||
Patch by Chris Angelico. |
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.
In what cases this exception is raised? Is it covered by tests?
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.
The easiest way to trigger it is to use something that isn't permitted in literal_eval, but is valid syntax. There's already one such test - proving that f-strings aren't literal_evallable - and I guess there could be more, but since this is a whitelist, the error is basically "anything else, raise". The only difference in this patch is that the message is different for "syntactically valid but not acceptable by literal_eval" and "syntactically invalid".
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.
I meant what input triggers the "malformed node or string" error now?
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.
Calling literal_eval with an AST node that has non-AST children in it. I don't think it's possible (post-patch) to trigger this error by calling literal_eval with a string, but I can't say for sure, so I don't want to say just "malformed node".