[Python-checkins] bpo-33363: raise SyntaxError for async for/with outside async functions (#6616)

Yury Selivanov webhook-mailer at python.org
Fri Apr 27 11:59:00 EDT 2018


https://github.com/python/cpython/commit/e2396506606115e785c94ec129eb86e2ed0aa744
commit: e2396506606115e785c94ec129eb86e2ed0aa744
branch: master
author: Zsolt Dollenstein <zsol.zsol at gmail.com>
committer: Yury Selivanov <yury at magic.io>
date: 2018-04-27T11:58:56-04:00
summary:

bpo-33363: raise SyntaxError for async for/with outside async functions (#6616)

files:
A Misc/NEWS.d/next/Core and Builtins/2018-04-26-22-48-28.bpo-33363.8RCnN2.rst
M Lib/test/test_coroutines.py
M Python/compile.c

diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index 10f7cca50473..47753e2ef03f 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -362,7 +362,22 @@ def bar():
             """def foo():
                    async def bar():
                         pass\nawait a
-            """]
+            """,
+            """def foo():
+                   async for i in arange(2):
+                       pass
+            """,
+            """def foo():
+                   async with resource:
+                       pass
+            """,
+            """async with resource:
+                   pass
+            """,
+            """async for i in arange(2):
+                   pass
+            """,
+            ]
 
         for code in samples:
             with self.subTest(code=code), self.assertRaises(SyntaxError):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-04-26-22-48-28.bpo-33363.8RCnN2.rst b/Misc/NEWS.d/next/Core and Builtins/2018-04-26-22-48-28.bpo-33363.8RCnN2.rst
new file mode 100644
index 000000000000..ad8d24895343
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-04-26-22-48-28.bpo-33363.8RCnN2.rst	
@@ -0,0 +1,2 @@
+Raise a SyntaxError for ``async with`` and ``async for`` statements outside
+of async functions.
diff --git a/Python/compile.c b/Python/compile.c
index cc0988f68e25..7960f09319e7 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2447,6 +2447,10 @@ static int
 compiler_async_for(struct compiler *c, stmt_ty s)
 {
     basicblock *start, *except, *end;
+    if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
+        return compiler_error(c, "'async for' outside async function");
+    }
+
     start = compiler_new_block(c);
     except = compiler_new_block(c);
     end = compiler_new_block(c);
@@ -4262,6 +4266,9 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
     withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
 
     assert(s->kind == AsyncWith_kind);
+    if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
+        return compiler_error(c, "'async with' outside async function");
+    }
 
     block = compiler_new_block(c);
     finally = compiler_new_block(c);



More information about the Python-checkins mailing list