bpo-28964: add line number of node (if available) to ast.literal_eval error messages (GH-23677)
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index be4b0f7..451f40d 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1011,6 +1011,18 @@ def test_literal_eval_trailing_ws(self):
self.assertEqual(ast.literal_eval(" \t -1"), -1)
self.assertRaises(IndentationError, ast.literal_eval, "\n -1")
+ def test_literal_eval_malformed_lineno(self):
+ msg = r'malformed node or string on line 3:'
+ with self.assertRaisesRegex(ValueError, msg):
+ ast.literal_eval("{'a': 1,\n'b':2,\n'c':++3,\n'd':4}")
+
+ node = ast.UnaryOp(
+ ast.UAdd(), ast.UnaryOp(ast.UAdd(), ast.Constant(6)))
+ self.assertIsNone(getattr(node, 'lineno', None))
+ msg = r'malformed node or string:'
+ with self.assertRaisesRegex(ValueError, msg):
+ ast.literal_eval(node)
+
def test_bad_integer(self):
# issue13436: Bad error message with invalid numeric values
body = [ast.ImportFrom(module='time',