diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-06-30 16:49:04 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-06-30 16:49:04 (GMT) |
commit | 9dec03571f2ec588dfc15ff438c85fe25989dcc1 (patch) | |
tree | 2bcbc27c1b90c7ea549a24d99ad2ca1a9c514012 | |
parent | 4a01cab89894b157f282b2032862a278c9edb842 (diff) | |
download | cpython-9dec03571f2ec588dfc15ff438c85fe25989dcc1.zip cpython-9dec03571f2ec588dfc15ff438c85fe25989dcc1.tar.gz cpython-9dec03571f2ec588dfc15ff438c85fe25989dcc1.tar.bz2 |
Issue #24528: Improve error message for awaits in comprehensions
-rw-r--r-- | Lib/test/test_coroutines.py | 10 | ||||
-rw-r--r-- | Python/compile.c | 5 |
2 files changed, 14 insertions, 1 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 8d2b1a3..e869fd2 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -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): diff --git a/Python/compile.c b/Python/compile.c index 1977c3a..2202e8f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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"); |