Visitar URL original
bpo-32888: Improve exception message in ast.literal_eval by Rosuav · Pull Request #340 · python/cpython · GitHub
Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test of different forms of exception message
  • Loading branch information
Rosuav committed Feb 20, 2018
commit cce0c6c5935c4bc122eb37f104ec76f473598253
13 changes: 13 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 with self.subTest(test=test, message=message): and then indent the parts below it. That way each of these end up being tested independently rather than potentially having the items loop have a case fail and cause the rest of the cases to fail to execute.

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',
Expand Down