diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-03-15 04:46:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-15 04:46:26 (GMT) |
commit | f7e32fcbd65490c921e1836c2399827d14d0eb7b (patch) | |
tree | f141bfcbdce54cd8d8694e99d3d9b2e4619a0c28 /Python | |
parent | 4abe77c725b1d0a2187b7160924258c9810b824c (diff) | |
download | cpython-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 'Python')
-rw-r--r-- | Python/compile.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Python/compile.c b/Python/compile.c index 11974c3..9cc7654 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -41,6 +41,10 @@ #define COMP_SETCOMP 2 #define COMP_DICTCOMP 3 +#define IS_TOP_LEVEL_AWAIT(c) ( \ + (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ + && (c->u->u_ste->ste_type == ModuleBlock)) + struct instr { unsigned i_jabs : 1; unsigned i_jrel : 1; @@ -2682,7 +2686,7 @@ static int compiler_async_for(struct compiler *c, stmt_ty s) { basicblock *start, *except, *end; - if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ + if (IS_TOP_LEVEL_AWAIT(c)){ 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"); @@ -4666,7 +4670,7 @@ 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->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ + if (IS_TOP_LEVEL_AWAIT(c)){ 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"); @@ -4877,7 +4881,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) ADDOP(c, YIELD_FROM); break; case Await_kind: - if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){ + if (!IS_TOP_LEVEL_AWAIT(c)){ if (c->u->u_ste->ste_type != FunctionBlock){ return compiler_error(c, "'await' outside function"); } @@ -5820,7 +5824,7 @@ 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) && + if ((IS_TOP_LEVEL_AWAIT(c)) && ste->ste_coroutine && !ste->ste_generator) { flags |= CO_COROUTINE; |