[pypy-commit] pypy py3.6: Using 'await' in other contexts raises a DeprecationWarning

amauryfa pypy.commits at gmail.com
Mon Mar 5 03:52:40 EST 2018


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.6
Changeset: r93957:3da474cc3b78
Date: 2018-03-05 09:04 +0100
http://bitbucket.org/pypy/pypy/changeset/3da474cc3b78/

Log:	Using 'await' in other contexts raises a DeprecationWarning

diff --git a/pypy/interpreter/astcompiler/astbuilder.py b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -125,7 +125,7 @@
 
     def check_forbidden_name(self, name, node):
         try:
-            misc.check_forbidden_name(name)
+            misc.check_forbidden_name(self.space, name)
         except misc.ForbiddenNameAssignment as e:
             self.error("cannot assign to %s" % (e.name,), node)
 
@@ -135,7 +135,7 @@
     def set_context(self, expr, ctx):
         """Set the context of an expression to Store or Del if possible."""
         try:
-            expr.set_context(ctx)
+            expr.set_context(self.space, ctx)
         except ast.UnacceptableExpressionContext as e:
             self.error_ast(e.msg, e.node)
         except misc.ForbiddenNameAssignment as e:
diff --git a/pypy/interpreter/astcompiler/asthelpers.py b/pypy/interpreter/astcompiler/asthelpers.py
--- a/pypy/interpreter/astcompiler/asthelpers.py
+++ b/pypy/interpreter/astcompiler/asthelpers.py
@@ -15,7 +15,7 @@
     def as_node_list(self, space):
         raise AssertionError("only for expressions")
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         raise AssertionError("should only be on expressions")
 
 
@@ -27,7 +27,7 @@
     def as_node_list(self, space):
         return None
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         d = self._description
         if d is None:
             d = "%r" % (self,)
@@ -43,32 +43,32 @@
     def as_node_list(self, space):
         return self.elts
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         if self.elts:
             for elt in self.elts:
-                elt.set_context(ctx)
+                elt.set_context(space, ctx)
         self.ctx = ctx
 
 
 class __extend__(ast.Attribute):
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         if ctx == ast.Store:
-            misc.check_forbidden_name(self.attr, self)
+            misc.check_forbidden_name(space, self.attr, self)
         self.ctx = ctx
 
 
 class __extend__(ast.Subscript):
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         self.ctx = ctx
 
 
 class __extend__(ast.Name):
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         if ctx == ast.Store:
-            misc.check_forbidden_name(self.id, self)
+            misc.check_forbidden_name(space, self.id, self)
         self.ctx = ctx
 
 
@@ -79,14 +79,14 @@
     def as_node_list(self, space):
         return self.elts
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         if self.elts:
             for elt in self.elts:
-                elt.set_context(ctx)
+                elt.set_context(space, ctx)
             self.ctx = ctx
         else:
             # Assignment to () raises an error.
-            ast.expr.set_context(self, ctx)
+            ast.expr.set_context(self, space, ctx)
 
 class __extend__(ast.Lambda):
 
@@ -141,9 +141,9 @@
 
     _description = "starred expression"
 
-    def set_context(self, ctx):
+    def set_context(self, space, ctx):
         self.ctx = ctx
-        self.value.set_context(ctx)
+        self.value.set_context(space, ctx)
 
 class __extend__(ast.IfExp):
 
diff --git a/pypy/interpreter/astcompiler/misc.py b/pypy/interpreter/astcompiler/misc.py
--- a/pypy/interpreter/astcompiler/misc.py
+++ b/pypy/interpreter/astcompiler/misc.py
@@ -68,10 +68,14 @@
         self.node = node
 
 
-def check_forbidden_name(name, node=None):
+def check_forbidden_name(space, name, node=None):
     """Raise an error if the name cannot be assigned to."""
     if name in ("None", "__debug__"):
         raise ForbiddenNameAssignment(name, node)
+    if name in ("async", "await"):
+        space.warn(space.newtext(
+            "'async' and 'await' will become reserved keywords"
+            " in Python 3.7"), space.w_DeprecationWarning)
     # XXX Warn about using True and False
 
 
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
@@ -1300,6 +1300,14 @@
         # compiling the produced AST previously triggered a crash
         compile(ast, '', 'exec')
 
+    def test_await_warning(self):
+        import warnings
+        source = "def f(): await = 5"
+        with warnings.catch_warnings(record=True) as l:
+            warnings.simplefilter("always")
+            compile(source, '', 'exec')
+        assert isinstance(l[0].message, DeprecationWarning)
+
 
 class TestOptimizations:
     def count_instructions(self, source):


More information about the pypy-commit mailing list