mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-39965: Correctly raise SyntaxError if await is used outside async functions when PyCF_ALLOW_TOP_LEVEL_AWAIT is set (GH-19010)
This commit is contained in:
parent
33238ec2af
commit
90235810ec
3 changed files with 49 additions and 4 deletions
|
@ -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;
|
||||
|
@ -2743,7 +2747,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");
|
||||
|
@ -4789,7 +4793,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");
|
||||
|
@ -5007,7 +5011,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");
|
||||
}
|
||||
|
@ -5836,7 +5840,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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue