]> granicus.if.org Git - python/commitdiff
Issue #24528: Improve error message for awaits in comprehensions
authorYury Selivanov <yselivanov@sprymix.com>
Tue, 30 Jun 2015 16:49:04 +0000 (12:49 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Tue, 30 Jun 2015 16:49:04 +0000 (12:49 -0400)
Lib/test/test_coroutines.py
Python/compile.c

index 8d2b1a34902c1f47b8852ca043c3ceb3d87c1ea9..e869fd216a1403ccddf76d39fd08e5df15eb6556 100644 (file)
@@ -106,6 +106,16 @@ class AsyncBadSyntaxTest(unittest.TestCase):
         with self.assertRaisesRegex(SyntaxError, 'invalid syntax'):
             import test.badsyntax_async9
 
+    def test_badsyntax_10(self):
+        ns = {}
+        for comp in {'(await a for a in b)',
+                     '[await a for a in b]',
+                     '{await a for a in b}',
+                     '{await a: c for a in b}'}:
+
+            with self.assertRaisesRegex( SyntaxError, 'await.*in comprehen'):
+                exec('async def f():\n\t{}'.format(comp), ns, ns)
+
 
 class TokenizerRegrTest(unittest.TestCase):
 
index 1977c3a1bfcd39884ecfe41878b1319b5226e948..2202e8f3a1cacc9e5b57c4764a55014e2d8a41c7 100644 (file)
@@ -3856,7 +3856,10 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
         if (c->u->u_ste->ste_type != FunctionBlock)
             return compiler_error(c, "'await' outside function");
 
-        /* this check won't be triggered while we have AWAIT token */
+        if (c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION)
+            return compiler_error(
+                c, "'await' expressions in comprehensions are not supported");
+
         if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
             return compiler_error(c, "'await' outside async function");