mirror of https://github.com/python/cpython
bpo-33363: raise SyntaxError for async for/with outside async functions (#6616)
This commit is contained in:
parent
078c4e3519
commit
e239650660
|
@ -362,7 +362,22 @@ class AsyncBadSyntaxTest(unittest.TestCase):
|
||||||
"""def foo():
|
"""def foo():
|
||||||
async def bar():
|
async def bar():
|
||||||
pass\nawait a
|
pass\nawait a
|
||||||
"""]
|
""",
|
||||||
|
"""def foo():
|
||||||
|
async for i in arange(2):
|
||||||
|
pass
|
||||||
|
""",
|
||||||
|
"""def foo():
|
||||||
|
async with resource:
|
||||||
|
pass
|
||||||
|
""",
|
||||||
|
"""async with resource:
|
||||||
|
pass
|
||||||
|
""",
|
||||||
|
"""async for i in arange(2):
|
||||||
|
pass
|
||||||
|
""",
|
||||||
|
]
|
||||||
|
|
||||||
for code in samples:
|
for code in samples:
|
||||||
with self.subTest(code=code), self.assertRaises(SyntaxError):
|
with self.subTest(code=code), self.assertRaises(SyntaxError):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Raise a SyntaxError for ``async with`` and ``async for`` statements outside
|
||||||
|
of async functions.
|
|
@ -2447,6 +2447,10 @@ static int
|
||||||
compiler_async_for(struct compiler *c, stmt_ty s)
|
compiler_async_for(struct compiler *c, stmt_ty s)
|
||||||
{
|
{
|
||||||
basicblock *start, *except, *end;
|
basicblock *start, *except, *end;
|
||||||
|
if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
|
||||||
|
return compiler_error(c, "'async for' outside async function");
|
||||||
|
}
|
||||||
|
|
||||||
start = compiler_new_block(c);
|
start = compiler_new_block(c);
|
||||||
except = compiler_new_block(c);
|
except = compiler_new_block(c);
|
||||||
end = compiler_new_block(c);
|
end = compiler_new_block(c);
|
||||||
|
@ -4262,6 +4266,9 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
|
||||||
withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
|
withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
|
||||||
|
|
||||||
assert(s->kind == AsyncWith_kind);
|
assert(s->kind == AsyncWith_kind);
|
||||||
|
if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
|
||||||
|
return compiler_error(c, "'async with' outside async function");
|
||||||
|
}
|
||||||
|
|
||||||
block = compiler_new_block(c);
|
block = compiler_new_block(c);
|
||||||
finally = compiler_new_block(c);
|
finally = compiler_new_block(c);
|
||||||
|
|
Loading…
Reference in New Issue