[Python-checkins] bpo-41218: Only mark async code with CO_COROUTINE. (GH-21357)

Miss Islington (bot) webhook-mailer at python.org
Mon Jul 6 17:44:25 EDT 2020


https://github.com/python/cpython/commit/41db8ffc59566b8552f9cce4452ee8afad00aa63
commit: 41db8ffc59566b8552f9cce4452ee8afad00aa63
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-07-06T14:44:16-07:00
summary:

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 bd46174a5a09a54e5ae1077909f923f56a7cf710)

Co-authored-by: Matthias Bussonnier <bussonniermatthias at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2020-07-06-13-35-17.bpo-41218.oKnSr2.rst
M Lib/test/test_builtin.py
M Python/compile.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 48b0e33af59ed..f47689dfdea41 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -370,6 +370,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.
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-07-06-13-35-17.bpo-41218.oKnSr2.rst b/Misc/NEWS.d/next/Core and Builtins/2020-07-06-13-35-17.bpo-41218.oKnSr2.rst
new file mode 100644
index 0000000000000..d98b3433ef05f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-07-06-13-35-17.bpo-41218.oKnSr2.rst	
@@ -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.
diff --git a/Python/compile.c b/Python/compile.c
index 913ec992395a0..3259e8a47f2d8 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4470,10 +4470,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);
@@ -4485,7 +4484,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;
@@ -4524,6 +4523,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;
 



More information about the Python-checkins mailing list