summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-03-15 04:46:26 (GMT)
committerGitHub <noreply@github.com>2020-03-15 04:46:26 (GMT)
commitf7e32fcbd65490c921e1836c2399827d14d0eb7b (patch)
treef141bfcbdce54cd8d8694e99d3d9b2e4619a0c28 /Lib/test
parent4abe77c725b1d0a2187b7160924258c9810b824c (diff)
downloadcpython-f7e32fcbd65490c921e1836c2399827d14d0eb7b.zip
cpython-f7e32fcbd65490c921e1836c2399827d14d0eb7b.tar.gz
cpython-f7e32fcbd65490c921e1836c2399827d14d0eb7b.tar.bz2
bpo-39965: Correctly raise SyntaxError if await is used outside async functions when PyCF_ALLOW_TOP_LEVEL_AWAIT is set (GH-19010)
(cherry picked from commit 90235810ec28ca954bbf4b61a5ae5df7a00db409) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_builtin.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index cc20551..0d24158 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -421,6 +421,44 @@ class BuiltinTest(unittest.TestCase):
finally:
asyncio.set_event_loop_policy(policy)
+ def test_compile_top_level_await_invalid_cases(self):
+ # helper function just to check we can run top=level async-for
+ async def arange(n):
+ for i in range(n):
+ yield i
+
+ modes = ('single', 'exec')
+ code_samples = [
+ '''def f(): await arange(10)\n''',
+ '''def f(): [x async for x in arange(10)]\n''',
+ '''def f(): [await x async for x in arange(10)]\n''',
+ '''def f():
+ async for i in arange(1):
+ a = 1
+ ''',
+ '''def f():
+ async with asyncio.Lock() as l:
+ a = 1
+ '''
+ ]
+ policy = maybe_get_event_loop_policy()
+ try:
+ for mode, code_sample in product(modes, code_samples):
+ source = dedent(code_sample)
+ with self.assertRaises(
+ SyntaxError, msg=f"source={source} mode={mode}"):
+ compile(source, '?', mode)
+
+ with self.assertRaises(
+ SyntaxError, msg=f"source={source} mode={mode}"):
+ co = compile(source,
+ '?',
+ mode,
+ flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
+ finally:
+ asyncio.set_event_loop_policy(policy)
+
+
def test_compile_async_generator(self):
"""
With the PyCF_ALLOW_TOP_LEVEL_AWAIT flag added in 3.8, we want to