]> granicus.if.org Git - python/commitdiff
Issue #26146: enhance ast.Constant error message
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 26 Jan 2016 23:39:12 +0000 (00:39 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 26 Jan 2016 23:39:12 +0000 (00:39 +0100)
Mention the name of the invalid type in error message of AST validation for
constants.

Suggestion made by Joseph Jevnik on a review.

Lib/test/test_ast.py
Python/ast.c

index 6d6c9bd26f90afa5e442fd2aee102ef82a7e64e2..57060d833c2fd3702c373bad0b7d28dde576b712 100644 (file)
@@ -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):
index ecfc14cdf84d8d3f1e7cc77941642580a7be6c56..d19546a2f7ec1fc7df0bccaffd5e9444ef6e757f 100644 (file)
@@ -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;