[Python-checkins] (no subject)

Batuhan Taşkaya webhook-mailer at python.org
Thu Mar 19 07:32:52 EDT 2020




To: python-checkins at python.org
Subject: bpo-40000: Improve error messages when validating invalid
 ast.Constant nodes (GH-19055)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/0ac59f93c0e3f91fd994d7245578cce65654=
fb22
commit: 0ac59f93c0e3f91fd994d7245578cce65654fb22
branch: master
author: Batuhan Ta=C5=9Fkaya <47358913+isidentical at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-19T11:32:28Z
summary:

bpo-40000: Improve error messages when validating invalid ast.Constant nodes =
(GH-19055)

Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-03-18-12-54-25.bpo-40000.FnsPZC.rst
M Lib/test/test_ast.py
M Python/ast.c

diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 66f83841ce341..d072c338d952e 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -582,6 +582,15 @@ def test_invalid_identifier(self):
             compile(m, "<test>", "exec")
         self.assertIn("identifier must be of type str", str(cm.exception))
=20
+    def test_invalid_constant(self):
+        for invalid_constant in int, (1, 2, int), frozenset((1, 2, int)):
+            e =3D ast.Expression(body=3Dast.Constant(invalid_constant))
+            ast.fix_missing_locations(e)
+            with self.assertRaisesRegex(
+                TypeError, "invalid type in Constant: type"
+            ):
+                compile(e, "<test>", "eval")
+
     def test_empty_yield_from(self):
         # Issue 16546: yield from value is not optional.
         empty_yield_from =3D ast.parse("def f():\n yield from g()")
diff --git a/Misc/NEWS.d/next/Library/2020-03-18-12-54-25.bpo-40000.FnsPZC.rs=
t b/Misc/NEWS.d/next/Library/2020-03-18-12-54-25.bpo-40000.FnsPZC.rst
new file mode 100644
index 0000000000000..208169788411b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-18-12-54-25.bpo-40000.FnsPZC.rst
@@ -0,0 +1,2 @@
+Improved error messages for validation of ``ast.Constant`` nodes. Patch by
+Batuhan Taskaya.
diff --git a/Python/ast.c b/Python/ast.c
index 2e9a8d05f7ea2..2b74ed4dbdefd 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -147,6 +147,11 @@ validate_constant(PyObject *value)
         return 1;
     }
=20
+    if (!PyErr_Occurred()) {
+        PyErr_Format(PyExc_TypeError,
+                     "got an invalid type in Constant: %s",
+                     _PyType_Name(Py_TYPE(value)));
+    }
     return 0;
 }
=20
@@ -261,9 +266,6 @@ 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_Format(PyExc_TypeError,
-                         "got an invalid type in Constant: %s",
-                         _PyType_Name(Py_TYPE(exp->v.Constant.value)));
             return 0;
         }
         return 1;



More information about the Python-checkins mailing list