Skip to content

Commit ec8a973

Browse files
bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)
Co-authored-by: Pablo Galindo <[email protected]> (cherry picked from commit 9052f7a) Co-authored-by: Batuhan Taşkaya <[email protected]>
1 parent da1fe76 commit ec8a973

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/test/test_builtin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,14 @@ async def arange(n):
390390
'''async for i in arange(1):
391391
a = 1''',
392392
'''async with asyncio.Lock() as l:
393-
a = 1'''
393+
a = 1''',
394+
'''a = [x async for x in arange(2)][1]''',
395+
'''a = 1 in {x async for x in arange(2)}''',
396+
'''a = {x:1 async for x in arange(1)}[0]''',
397+
'''a = [x async for x in arange(2) async for x in arange(2)][1]''',
398+
'''a = [x async for x in (x async for x in arange(5))][1]''',
399+
'''a, = [1 for x in {x async for x in arange(1)}]''',
400+
'''a = [await asyncio.sleep(0, x) async for x in arange(2)][1]'''
394401
]
395402
policy = maybe_get_event_loop_policy()
396403
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow executing asynchronous comprehensions on the top level when the
2+
``PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag is given. Patch by Batuhan Taskaya.

Python/compile.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,11 +4469,14 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
44694469
PyCodeObject *co = NULL;
44704470
comprehension_ty outermost;
44714471
PyObject *qualname = NULL;
4472-
int is_async_function = c->u->u_ste->ste_coroutine;
44734472
int is_async_generator = 0;
44744473

4475-
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
4474+
if (IS_TOP_LEVEL_AWAIT(c)) {
4475+
c->u->u_ste->ste_coroutine = 1;
4476+
}
4477+
int is_async_function = c->u->u_ste->ste_coroutine;
44764478

4479+
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
44774480
if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
44784481
(void *)e, e->lineno))
44794482
{

0 commit comments

Comments
 (0)