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

Skip to content

Commit bd46174

Browse files
authored
bpo-41218: Only mark async code with CO_COROUTINE. (#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.
1 parent a103e73 commit bd46174

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
@@ -371,6 +371,25 @@ def f(): """doc"""
371371
rv = ns['f']()
372372
self.assertEqual(rv, tuple(expected))
373373

374+
def test_compile_top_level_await_no_coro(self):
375+
"""Make sure top level non-await codes get the correct coroutine flags.
376+
"""
377+
modes = ('single', 'exec')
378+
code_samples = [
379+
'''def f():pass\n''',
380+
'''[x for x in l]'''
381+
]
382+
for mode, code_sample in product(modes, code_samples):
383+
source = dedent(code_sample)
384+
co = compile(source,
385+
'?',
386+
mode,
387+
flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
388+
389+
self.assertNotEqual(co.co_flags & CO_COROUTINE, CO_COROUTINE,
390+
msg=f"source={source} mode={mode}")
391+
392+
374393
def test_compile_top_level_await(self):
375394
"""Test whether code some top level await can be compiled.
376395
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
@@ -4588,10 +4588,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
45884588
comprehension_ty outermost;
45894589
PyObject *qualname = NULL;
45904590
int is_async_generator = 0;
4591+
int top_level_await = IS_TOP_LEVEL_AWAIT(c);
4592+
45914593

4592-
if (IS_TOP_LEVEL_AWAIT(c)) {
4593-
c->u->u_ste->ste_coroutine = 1;
4594-
}
45954594
int is_async_function = c->u->u_ste->ste_coroutine;
45964595

45974596
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
@@ -4603,7 +4602,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
46034602

46044603
is_async_generator = c->u->u_ste->ste_coroutine;
46054604

4606-
if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
4605+
if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
46074606
compiler_error(c, "asynchronous comprehension outside of "
46084607
"an asynchronous function");
46094608
goto error_in_scope;
@@ -4642,6 +4641,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
46424641
qualname = c->u->u_qualname;
46434642
Py_INCREF(qualname);
46444643
compiler_exit_scope(c);
4644+
if (top_level_await && is_async_generator){
4645+
c->u->u_ste->ste_coroutine = 1;
4646+
}
46454647
if (co == NULL)
46464648
goto error;
46474649

0 commit comments

Comments
 (0)
0