[Python-checkins] bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)

Miss Islington (bot) webhook-mailer at python.org
Thu Mar 19 07:54:21 EDT 2020


https://github.com/python/cpython/commit/ec8a973f7cf080d9c0679f058b2371f0b7c7862c
commit: ec8a973f7cf080d9c0679f058b2371f0b7c7862c
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-19T04:54:16-07:00
summary:

bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)


Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>
(cherry picked from commit 9052f7a41b90f2d34011c8da68f9a4facebc8a97)

Co-authored-by: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst
M Lib/test/test_builtin.py
M Python/compile.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 0d241587a8690..f680e02fdf520 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -390,7 +390,14 @@ def test_compile_top_level_await(self):
             '''async for i in arange(1):
                    a = 1''',
             '''async with asyncio.Lock() as l:
-                   a = 1'''
+                   a = 1''',
+            '''a = [x async for x in arange(2)][1]''',
+            '''a = 1 in {x async for x in arange(2)}''',
+            '''a = {x:1 async for x in arange(1)}[0]''',
+            '''a = [x async for x in arange(2) async for x in arange(2)][1]''',
+            '''a = [x async for x in (x async for x in arange(5))][1]''',
+            '''a, = [1 for x in {x async for x in arange(1)}]''',
+            '''a = [await asyncio.sleep(0, x) async for x in arange(2)][1]'''
         ]
         policy = maybe_get_event_loop_policy()
         try:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst
new file mode 100644
index 0000000000000..dbe83c6aa19f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst	
@@ -0,0 +1,2 @@
+Allow executing asynchronous comprehensions on the top level when the
+``PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag is given. Patch by Batuhan Taskaya.
diff --git a/Python/compile.c b/Python/compile.c
index 9cc7654630808..913ec992395a0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4469,11 +4469,14 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
     PyCodeObject *co = NULL;
     comprehension_ty outermost;
     PyObject *qualname = NULL;
-    int is_async_function = c->u->u_ste->ste_coroutine;
     int is_async_generator = 0;
 
-    outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
+    if (IS_TOP_LEVEL_AWAIT(c)) {
+        c->u->u_ste->ste_coroutine = 1;
+    }
+    int is_async_function = c->u->u_ste->ste_coroutine;
 
+    outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
                               (void *)e, e->lineno))
     {



More information about the Python-checkins mailing list