[pypy-commit] pypy py3.5: Improve error message in some cases, like CPython. Use '< >' around

arigo pypy.commits at gmail.com
Tue Nov 1 06:57:56 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r88022:ccec49a07062
Date: 2016-11-01 11:57 +0100
http://bitbucket.org/pypy/pypy/changeset/ccec49a07062/

Log:	Improve error message in some cases, like CPython. Use '< >' around
	auto-generated scope names even though these names are not really
	public.

diff --git a/pypy/interpreter/astcompiler/symtable.py b/pypy/interpreter/astcompiler/symtable.py
--- a/pypy/interpreter/astcompiler/symtable.py
+++ b/pypy/interpreter/astcompiler/symtable.py
@@ -232,11 +232,7 @@
 class ModuleScope(Scope):
 
     def __init__(self):
-        Scope.__init__(self, "top")
-
-    def note_await(self, await_node):
-        raise SyntaxError("'await' outside async function", await_node.lineno,
-                          await_node.col_offset)
+        Scope.__init__(self, "<top-level>")
 
 
 class FunctionScope(Scope):
@@ -270,8 +266,11 @@
             self.has_yield_inside_try = True
             
     def note_await(self, await_node):
-        raise SyntaxError("'await' outside async function", await_node.lineno,
-                          await_node.col_offset)
+        if self.name == '<genexpr>':
+            msg = "'await' expressions in comprehensions are not supported"
+        else:
+            msg = "'await' outside async function"
+        raise SyntaxError(msg, await_node.lineno, await_node.col_offset)
 
     def note_return(self, ret):
         if ret.value:
@@ -305,11 +304,9 @@
         if (self.has_free or self.child_has_free) and not self.optimized:
             raise AssertionError("unknown reason for unoptimization")
 
+
 class AsyncFunctionScope(FunctionScope):
 
-    def __init__(self, name, lineno, col_offset):
-        FunctionScope.__init__(self, name, lineno, col_offset)
-
     def note_yield(self, yield_node):
         raise SyntaxError("'yield' inside async function", yield_node.lineno,
                           yield_node.col_offset)
@@ -521,7 +518,7 @@
         assert isinstance(args, ast.arguments)
         self.visit_sequence(args.defaults)
         self.visit_kwonlydefaults(args.kw_defaults)
-        new_scope = FunctionScope("lambda", lamb.lineno, lamb.col_offset)
+        new_scope = FunctionScope("<lambda>", lamb.lineno, lamb.col_offset)
         self.push_scope(new_scope, lamb)
         lamb.args.walkabout(self)
         lamb.body.walkabout(self)
@@ -531,7 +528,7 @@
         outer = comps[0]
         assert isinstance(outer, ast.comprehension)
         outer.iter.walkabout(self)
-        new_scope = FunctionScope("genexp", node.lineno, node.col_offset)
+        new_scope = FunctionScope("<genexpr>", node.lineno, node.col_offset)
         self.push_scope(new_scope, node)
         self.implicit_arg(0)
         outer.target.walkabout(self)
diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -1132,6 +1132,15 @@
         """
         py.test.raises(SyntaxError, self.simple_test, source, None, None)
 
+    def test_error_message_1(self):
+        source = """if 1:
+        async def f():
+            {await a for a in b}
+        """
+        e = py.test.raises(SyntaxError, self.simple_test, source, None, None)
+        assert e.value.msg == (
+            "'await' expressions in comprehensions are not supported")
+
 
 class AppTestCompiler:
 
diff --git a/pypy/interpreter/astcompiler/test/test_symtable.py b/pypy/interpreter/astcompiler/test/test_symtable.py
--- a/pypy/interpreter/astcompiler/test/test_symtable.py
+++ b/pypy/interpreter/astcompiler/test/test_symtable.py
@@ -44,7 +44,7 @@
         gen_scope = mod_scope.children[0]
         assert isinstance(gen_scope, symtable.FunctionScope)
         assert not gen_scope.children
-        assert gen_scope.name == "genexp"
+        assert gen_scope.name == "<genexpr>"
         return mod_scope, gen_scope
 
     def check_unknown(self, scp, *names):
@@ -251,7 +251,7 @@
         assert len(scp.children) == 1
         lscp = scp.children[0]
         assert isinstance(lscp, symtable.FunctionScope)
-        assert lscp.name == "lambda"
+        assert lscp.name == "<lambda>"
         assert lscp.lookup("x") == symtable.SCOPE_LOCAL
         assert lscp.lookup("y") == symtable.SCOPE_GLOBAL_IMPLICIT
         scp = self.mod_scope("lambda x=a: b")


More information about the pypy-commit mailing list