[pypy-commit] pypy py3.6: Some ast validation errors are shown as TypeError

amauryfa pypy.commits at gmail.com
Sat Mar 3 11:09:27 EST 2018


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.6
Changeset: r93945:4f89f769905c
Date: 2018-03-03 17:07 +0100
http://bitbucket.org/pypy/pypy/changeset/4f89f769905c/

Log:	Some ast validation errors are shown as TypeError

diff --git a/pypy/interpreter/astcompiler/test/test_validate.py b/pypy/interpreter/astcompiler/test/test_validate.py
--- a/pypy/interpreter/astcompiler/test/test_validate.py
+++ b/pypy/interpreter/astcompiler/test/test_validate.py
@@ -397,7 +397,8 @@
 
     def test_constant(self):
         node = ast.Constant(self.space.newlist([1]), 0, 0)
-        self.expr(node, "got an invalid type in Constant: list")
+        self.expr(node, "got an invalid type in Constant: list",
+                  exc=validate.ValidationTypeError)
 
     def test_stdlib_validates(self):
         stdlib = os.path.join(os.path.dirname(ast.__file__), '../../../lib-python/3')
diff --git a/pypy/interpreter/astcompiler/validate.py b/pypy/interpreter/astcompiler/validate.py
--- a/pypy/interpreter/astcompiler/validate.py
+++ b/pypy/interpreter/astcompiler/validate.py
@@ -11,12 +11,17 @@
 
 
 class ValidationError(Exception):
+    # Will be seen as a ValueError
     def __init__(self, message):
         self.message = message
 
     def __str__(self):
         return self.message
 
+class ValidationTypeError(ValidationError):
+    # Will be seen as a TypeError
+    pass
+
 
 def expr_context_name(ctx):
     if not 1 <= ctx <= len(ast.expr_context_to_class):
@@ -111,8 +116,8 @@
         for w_item in space.unpackiterable(w_obj):
             validate_constant(space, w_item)
         return
-    raise ValidationError("got an invalid type in Constant: %s" %
-                          space.type(w_obj).name)
+    raise ValidationTypeError("got an invalid type in Constant: %s" %
+                              space.type(w_obj).name)
 
 
 class AstValidator(ast.ASTVisitor):
diff --git a/pypy/interpreter/pycompiler.py b/pypy/interpreter/pycompiler.py
--- a/pypy/interpreter/pycompiler.py
+++ b/pypy/interpreter/pycompiler.py
@@ -138,6 +138,9 @@
     def validate_ast(self, node):
         try:
             validate.validate_ast(self.space, node)
+        except validate.ValidationTypeError as e:
+            raise OperationError(self.space.w_TypeError,
+                                 self.space.newtext(e.message))
         except validate.ValidationError as e:
             raise OperationError(self.space.w_ValueError,
                                  self.space.newtext(e.message))


More information about the pypy-commit mailing list