cpython (3.5): Issue #24528: Improve error message for awaits in comprehensions

https://hg.python.org/cpython/rev/30b676e8b21f changeset: 96724:30b676e8b21f branch: 3.5 parent: 96722:6ae8842e9b60 user: Yury Selivanov <yselivanov@sprymix.com> date: Tue Jun 30 12:49:04 2015 -0400 summary: Issue #24528: Improve error message for awaits in comprehensions files: Lib/test/test_coroutines.py | 10 ++++++++++ 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 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -106,6 +106,16 @@ 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 --- a/Python/compile.c +++ b/Python/compile.c @@ -3856,7 +3856,10 @@ 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"); -- Repository URL: https://hg.python.org/cpython
participants (1)
-
yury.selivanov