summaryrefslogtreecommitdiff
path: root/Python/compile.c
diff options
context:
space:
mode:
authorMatthias Bussonnier <bussonniermatthias@gmail.com>2019-05-21 13:12:03 -0700
committerYury Selivanov <yury@magic.io>2019-05-21 16:12:02 -0400
commit565b4f1ac7304d1e690c404ca8316f383ba60862 (patch)
tree193faa5ced2666a1fba1b3715c89946398ccbb12 /Python/compile.c
parentaa32a7e1116f7aaaef9fec453db910e90ab7b101 (diff)
downloadcpython-git-565b4f1ac7304d1e690c404ca8316f383ba60862.tar.gz
bpo-34616: Add PyCF_ALLOW_TOP_LEVEL_AWAIT to allow top-level await (GH-13148)
Co-Authored-By: Yury Selivanov <yury@magic.io>
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 63b2456bb3..734e8401ff 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2609,7 +2609,9 @@ static int
compiler_async_for(struct compiler *c, stmt_ty s)
{
basicblock *start, *except, *end;
- if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
+ if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){
+ c->u->u_ste->ste_coroutine = 1;
+ } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
return compiler_error(c, "'async for' outside async function");
}
@@ -4564,7 +4566,9 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
assert(s->kind == AsyncWith_kind);
- if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
+ if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){
+ c->u->u_ste->ste_coroutine = 1;
+ } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
return compiler_error(c, "'async with' outside async function");
}
@@ -4773,12 +4777,16 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
ADDOP(c, YIELD_FROM);
break;
case Await_kind:
- if (c->u->u_ste->ste_type != FunctionBlock)
- return compiler_error(c, "'await' outside function");
+ if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){
+ if (c->u->u_ste->ste_type != FunctionBlock){
+ return compiler_error(c, "'await' outside function");
+ }
- if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
- c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
- return compiler_error(c, "'await' outside async function");
+ if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
+ c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
+ return compiler_error(c, "'await' outside async function");
+ }
+ }
VISIT(c, expr, e->v.Await.value);
ADDOP(c, GET_AWAITABLE);
@@ -5712,6 +5720,12 @@ compute_code_flags(struct compiler *c)
/* (Only) inherit compilerflags in PyCF_MASK */
flags |= (c->c_flags->cf_flags & PyCF_MASK);
+ if ((c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) &&
+ ste->ste_coroutine &&
+ !ste->ste_generator) {
+ flags |= CO_COROUTINE;
+ }
+
return flags;
}