8000 bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357) · python/cpython@41db8ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 41db8ff

Browse files
bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357)
3.8.3 had a regression where compiling with ast.PyCF_ALLOW_TOP_LEVEL_AWAIT woudl agressively mark things are coroutine even if there were not. (cherry picked from commit bd46174) Co-authored-by: Matthias Bussonnier <bussonniermatthias@gmail.com>
1 parent aa7f775 commit 41db8ff

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

Lib/test/test_builtin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,25 @@ def f(): """doc"""
370370
rv = ns['f']()
371371
self.assertEqual(rv, tuple(expected))
372372

373+
def test_compile_top_level_await_no_coro(self):
374+
"""Make sure top level non-await codes get the correct coroutine flags.
375+
"""
376+
modes = ('single', 'exec')
377+
code_samples = [
378+
'''def f():pass\n''',
379+
'''[x for x in l]'''
380+
]
381+
for mode, code_sample in product(modes, code_samples):
382+
source = dedent(code_sample)
383+
co = compile(source,
384+
'?',
385+
mode,
386+
flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
387+
388+
self.assertNotEqual(co.co_flags & CO_COROUTINE, CO_COROUTINE,
389+
msg=f"source={source} mode={mode}")
390+
391+
373392
def test_compile_top_level_await(self):
374393
"""Test whether code some top level await can be compiled.
375394
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Python 3.8.3 had a regression where compiling with
2+
ast.PyCF_ALLOW_TOP_LEVEL_AWAIT would aggressively mark list comprehension
3+
with CO_COROUTINE. Now only list comprehension making use of async/await
4+
will tagged as so.

Python/compile.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,10 +4470,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
44704470
comprehension_ty outermost;
44714471
PyObject *qualname = NULL;
44724472
int is_async_generator = 0;
4473+
int top_level_await = IS_TOP_LEVEL_AWAIT(c);
4474+
44734475

4474-
if (IS_TOP_LEVEL_AWAIT(c)) {
4475-
c->u->u_ste->ste_coroutine = 1;
4476-
}
44774476
int is_async_function = c->u->u_ste->ste_coroutine;
44784477

44794478
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
@@ -4485,7 +4484,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
44854484

44864485
is_async_generator = c->u->u_ste->ste_coroutine;
44874486

4488-
if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
4487+
if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
44894488
compiler_error(c, "asynchronous comprehension outside of "
44904489
"an asynchronous function");
44914490
goto error_in_scope;
@@ -4524,6 +4523,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
45244523
qualname = c->u->u_qualname;
45254524
Py_INCREF(qualname);
45264525
compiler_exit_scope(c);
4526+
if (top_level_await && is_async_generator){
4527+
c->u->u_ste->ste_coroutine = 1;
4528+
}
45274529
if (co == NULL)
45284530
goto error;
45294531

0 commit comments

Comments
 (0)
0