Kaydet (Commit) be59d148 authored tarafından Victor Stinner's avatar Victor Stinner

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.
üst 25219f59
...@@ -951,6 +951,12 @@ class ConstantTests(unittest.TestCase): ...@@ -951,6 +951,12 @@ class ConstantTests(unittest.TestCase):
exec(code, ns) exec(code, ns)
return ns['x'] 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): def test_singletons(self):
for const in (None, False, True, Ellipsis, b'', frozenset()): for const in (None, False, True, Ellipsis, b'', frozenset()):
with self.subTest(const=const): with self.subTest(const=const):
......
...@@ -288,7 +288,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx) ...@@ -288,7 +288,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
validate_keywords(exp->v.Call.keywords); validate_keywords(exp->v.Call.keywords);
case Constant_kind: case Constant_kind:
if (!validate_constant(exp->v.Constant.value)) { 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 0;
} }
return 1; return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment