8000 bpo-39562: Allow executing asynchronous comprehensions in the asyncio… · python/cpython@9052f7a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9052f7a

Browse files
bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
1 parent 0ac59f9 commit 9052f7a

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
@@ -4520,11 +4520,14 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
45204520
PyCodeObject *co = NULL;
45214521
comprehension_ty outermost;
45224522
PyObject *qualname = NULL;
4523-
int is_async_function = c->u->u_ste->ste_coroutine;
45244523
int is_async_generator = 0;
45254524

4526-
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
4525+
if (IS_TOP_LEVEL_AWAIT(c)) {
4526+
c->u->u_ste->ste_coroutine = 1;
4527+
}
4528+
int is_async_function = c->u->u_ste->ste_coroutine;
45274529

4530+
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
45284531
if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
45294532
(void *)e, e->lineno))
45304533
{

0 commit comments

Comments
 (0)
0