[pypy-commit] pypy py3.7: check for other async constructs outside of async functions too

cfbolz pypy.commits at gmail.com
Thu Feb 6 11:15:17 EST 2020


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.7
Changeset: r98677:e9e80323c851
Date: 2020-02-06 17:13 +0100
http://bitbucket.org/pypy/pypy/changeset/e9e80323c851/

Log:	check for other async constructs outside of async functions too

diff --git a/lib-python/3/test/test_coroutines.py b/lib-python/3/test/test_coroutines.py
--- a/lib-python/3/test/test_coroutines.py
+++ b/lib-python/3/test/test_coroutines.py
@@ -385,6 +385,12 @@
             ]
 
         for code in samples:
+            try:
+                compile(code, "<text>", "exec")
+            except SyntaxError:
+                pass
+            else:
+                print(code)
             with self.subTest(code=code), self.assertRaises(SyntaxError):
                 compile(code, "<test>", "exec")
 
diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -622,6 +622,8 @@
         self.use_next_block(end)
 
     def visit_AsyncFor(self, fr):
+        if not self._check_async_function():
+            self.error("'async for' outside async function", fr)
         self.update_position(fr.lineno, True)
         b_try = self.new_block()
         b_except = self.new_block()
@@ -1067,6 +1069,8 @@
         self.pop_frame_block(F_BLOCK_FINALLY_END, cleanup)
 
     def visit_AsyncWith(self, wih):
+        if not self._check_async_function():
+            self.error("'async with' outside async function", wih)
         self.update_position(wih.lineno, True)
         self.handle_withitem(wih, 0, is_async=True)
 
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
@@ -1258,6 +1258,37 @@
         # ok!
         self.simple_test(source, "None", None)
 
+    def test_not_async_function_error(self):
+        source = """
+async with x:
+    pass
+"""
+        with py.test.raises(SyntaxError):
+            self.simple_test(source, "None", None)
+
+        source = """
+async for i in x:
+    pass
+"""
+        with py.test.raises(SyntaxError):
+            self.simple_test(source, "None", None)
+
+        source = """
+def f():
+    async with x:
+        pass
+"""
+        with py.test.raises(SyntaxError):
+            self.simple_test(source, "None", None)
+
+        source = """
+def f():
+    async for i in x:
+        pass
+"""
+        with py.test.raises(SyntaxError):
+            self.simple_test(source, "None", None)
+
     def test_load_classderef(self):
         source = """if 1:
         def f():


More information about the pypy-commit mailing list