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

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
Batuhan Taşkaya 2020-03-19 14:35:44 +03:00 committed by GitHub
parent 0ac59f93c0
commit 9052f7a41b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -390,7 +390,14 @@ class BuiltinTest(unittest.TestCase):
'''async for i in arange(1): '''async for i in arange(1):
a = 1''', a = 1''',
'''async with asyncio.Lock() as l: '''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() policy = maybe_get_event_loop_policy()
try: try:

View File

@ -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.

View File

@ -4520,11 +4520,14 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
PyCodeObject *co = NULL; PyCodeObject *co = NULL;
comprehension_ty outermost; comprehension_ty outermost;
PyObject *qualname = NULL; PyObject *qualname = NULL;
int is_async_function = c->u->u_ste->ste_coroutine;
int is_async_generator = 0; 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, if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
(void *)e, e->lineno)) (void *)e, e->lineno))
{ {