8000 [3.9] bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357) by pablogsal · Pull Request #21362 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.9] bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357) #21362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,25 @@ def f(): """doc"""
rv = ns['f']()
self.assertEqual(rv, tuple(expected))

def test_compile_top_level_await_no_coro(self):
"""Make sure top level non-await codes get the correct coroutine flags.
"""
modes = ('single', 'exec')
code_samples = [
'''def f():pass\n''',
'''[x for x in l]'''
]
for mode, code_sample in product(modes, code_samples):
source = dedent(code_sample)
co = compile(source,
'?',
mode,
flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)

self.assertNotEqual(co.co_flags & CO_COROUTINE, CO_COROUTINE,
msg=f"source={source} mode={mode}")


def test_compile_top_level_await(self):
"""Test whether code some top level await can be compiled.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Python 3.8.3 had a regression where compiling with
ast.PyCF_ALLOW_TOP_LEVEL_AWAIT would aggressively mark list comprehension
with CO_COROUTINE. Now only list comprehension making use of async/await
will tagged as so.
10 changes: 6 additions & 4 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4605,10 +4605,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
comprehension_ty outermost;
PyObject *qualname = NULL;
int is_async_generator = 0;
int top_level_await = IS_TOP_LEVEL_AWAIT(c);


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);
Expand All @@ -4620,7 +4619,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,

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

if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
compiler_error(c, "asynchronous comprehension outside of "
"an asynchronous function");
goto error_in_scope;
Expand Down Expand Up @@ -4659,6 +4658,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
qualname = c->u->u_qualname;
Py_INCREF(qualname);
compiler_exit_scope(c);
if (top_level_await && is_async_generator){
c->u->u_ste->ste_coroutine = 1;
}
if (co == NULL)
goto error;

Expand Down
0