From: Victor Stinner Date: Tue, 26 Jan 2016 23:39:12 +0000 (+0100) Subject: Issue #26146: enhance ast.Constant error message X-Git-Tag: v3.6.0a1~692 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=be59d1489bdf150e05e67a89cc772628af7e8fd6;p=python Issue #26146: enhance ast.Constant error message Mention the name of the invalid type in error message of AST validation for constants. Suggestion made by Joseph Jevnik on a review. --- diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 6d6c9bd26f..57060d833c 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -951,6 +951,12 @@ class ConstantTests(unittest.TestCase): exec(code, ns) return ns['x'] + def test_validation(self): + with self.assertRaises(TypeError) as cm: + self.compile_constant([1, 2, 3]) + self.assertEqual(str(cm.exception), + "got an invalid type in Constant: list") + def test_singletons(self): for const in (None, False, True, Ellipsis, b'', frozenset()): with self.subTest(const=const): diff --git a/Python/ast.c b/Python/ast.c index ecfc14cdf8..d19546a2f7 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -288,7 +288,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx) validate_keywords(exp->v.Call.keywords); case Constant_kind: if (!validate_constant(exp->v.Constant.value)) { - PyErr_SetString(PyExc_TypeError, "invalid type in Constant"); + PyErr_Format(PyExc_TypeError, + "got an invalid type in Constant: %s", + Py_TYPE(exp->v.Constant.value)->tp_name); return 0; } return 1;