From ca4086670edd09b4c419e7690e7fd371af7b640f Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Thu, 12 Mar 2020 22:15:31 +0300 Subject: [PATCH 1/5] bpo-39562: Run asynchronous comprehensions under asyncio repl --- Lib/test/test_builtin.py | 8 +++++++- .../2020-03-12-22-13-50.bpo-39562.E2u273.rst | 2 ++ Python/compile.c | 7 +++++-- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index d6b9ee1a95a787..55ee89a65d5e95 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -390,7 +390,13 @@ async def arange(n): '''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)}]''', ] 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 00000000000000..e98ca366c645f8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst @@ -0,0 +1,2 @@ +Run asynchronous comprehensions on the top level when +``PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag given. Patch by Batuhan Taskaya. diff --git a/Python/compile.c b/Python/compile.c index b92cb445afb4a9..203304a91410f1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4576,11 +4576,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 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ + 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)) { From 72c492dfa004a3ad524b7a527ff31e16a712d570 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Thu, 12 Mar 2020 22:42:11 +0300 Subject: [PATCH 2/5] Add a test about await inside of a comprehension --- Lib/test/test_builtin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 55ee89a65d5e95..902c1f03c51d5f 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -397,6 +397,7 @@ async def arange(n): '''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.001, x) async for x in arange(2)][1]''' ] policy = maybe_get_event_loop_policy() try: From 81a464a417a099572011a98e640fb28057ef0bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= <47358913+isidentical@users.noreply.github.com> Date: Fri, 13 Mar 2020 11:37:22 +0300 Subject: [PATCH 3/5] Apply suggestions from code review Co-Authored-By: Pablo Galindo --- .../2020-03-12-22-13-50.bpo-39562.E2u273.rst | 4 ++-- Python/compile.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 index e98ca366c645f8..ea870d2801c567 100644 --- 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 @@ -1,2 +1,2 @@ -Run asynchronous comprehensions on the top level when -``PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag given. Patch by Batuhan Taskaya. +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 203304a91410f1..1205e9f1ce6d14 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4578,7 +4578,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, PyObject *qualname = NULL; int is_async_generator = 0; - if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ + if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) { c->u->u_ste->ste_coroutine = 1; } int is_async_function = c->u->u_ste->ste_coroutine; From d99d8e4fa231a2a9096db03333f4cca51401b4e3 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 13 Mar 2020 11:38:40 +0300 Subject: [PATCH 4/5] dont sleep on tests --- Lib/test/test_builtin.py | 2 +- .../Core and Builtins/2020-03-12-22-13-50.bpo-39562.E2u273.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 902c1f03c51d5f..1e9012e7e5ed2e 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -397,7 +397,7 @@ async def arange(n): '''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.001, x) async for x in arange(2)][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 index ea870d2801c567..dbe83c6aa19f94 100644 --- 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 @@ -1,2 +1,2 @@ Allow executing asynchronous comprehensions on the top level when the -PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag is given. Patch by Batuhan Taskaya. +``PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag is given. Patch by Batuhan Taskaya. From 6baa678f8006790d4ede538314eaceb3ec38d6f8 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Sun, 15 Mar 2020 07:42:45 +0300 Subject: [PATCH 5/5] rebase master --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index 1205e9f1ce6d14..057d8ae2a3f025 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4578,7 +4578,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, PyObject *qualname = NULL; int is_async_generator = 0; - if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) { + if (IS_TOP_LEVEL_AWAIT(c)) { c->u->u_ste->ste_coroutine = 1; } int is_async_function = c->u->u_ste->ste_coroutine;