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

Skip to content

Commit 6488a4a

Browse files
pablogsalCarreau
andauthored
[3.9] bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357) (GH-21362)
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> Co-authored-by: Matthias Bussonnier <bussonniermatthias@gmail.com>
1 parent 54f115d commit 6488a4a

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
@@ -4605,10 +4605,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
46054605
comprehension_ty outermost;
46064606
PyObject *qualname = NULL;
46074607
int is_async_generator = 0;
4608+
int top_level_await = IS_TOP_LEVEL_AWAIT(c);
4609+
46084610

4609-
if (IS_TOP_LEVEL_AWAIT(c)) {
4610-
c->u->u_ste->ste_coroutine = 1;
4611-
}
46124611
int is_async_function = c->u->u_ste->ste_coroutine;
46134612

46144613
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
@@ -4620,7 +4619,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
46204619

46214620
is_async_generator = c->u->u_ste->ste_coroutine;
46224621

4623-
if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
4622+
if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
46244623
compiler_error(c, "asynchronous comprehension outside of "
46254624
"an asynchronous function");
46264625
goto error_in_scope;
@@ -4659,6 +4658,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
46594658
qualname = c->u->u_qualname;
46604659
Py_INCREF(qualname);
46614660
compiler_exit_scope(c);
4661+
if (top_level_await && is_async_generator){
4662+
c->u->u_ste->ste_coroutine = 1;
4663+
}
46624664
if (co == NULL)
46634665
goto error;
46644666

0 commit comments

Comments
 (0)
0