Skip to content

Commit 9315688

Browse files
authored
pythongh-121272: set ste_coroutine during symtable construction (python#121297)
compiler no longer modifies the symtable after this.
1 parent 51c4a32 commit 9315688

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Python/compile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,7 @@ compiler_async_for(struct compiler *c, stmt_ty s)
30593059
{
30603060
location loc = LOC(s);
30613061
if (IS_TOP_LEVEL_AWAIT(c)){
3062-
c->u->u_ste->ste_coroutine = 1;
3062+
assert(c->u->u_ste->ste_coroutine == 1);
30633063
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
30643064
return compiler_error(c, loc, "'async for' outside async function");
30653065
}
@@ -5782,7 +5782,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
57825782
co = optimize_and_assemble(c, 1);
57835783
compiler_exit_scope(c);
57845784
if (is_top_level_await && is_async_generator){
5785-
c->u->u_ste->ste_coroutine = 1;
5785+
assert(c->u->u_ste->ste_coroutine == 1);
57865786
}
57875787
if (co == NULL) {
57885788
goto error;
@@ -5926,7 +5926,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
59265926

59275927
assert(s->kind == AsyncWith_kind);
59285928
if (IS_TOP_LEVEL_AWAIT(c)){
5929-
c->u->u_ste->ste_coroutine = 1;
5929+
assert(c->u->u_ste->ste_coroutine == 1);
59305930
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
59315931
return compiler_error(c, loc, "'async with' outside async function");
59325932
}

Python/symtable.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,16 @@ check_import_from(struct symtable *st, stmt_ty s)
16811681
return 1;
16821682
}
16831683

1684+
static void
1685+
maybe_set_ste_coroutine_for_module(struct symtable *st, stmt_ty s)
1686+
{
1687+
if ((st->st_future->ff_features & PyCF_ALLOW_TOP_LEVEL_AWAIT) &&
1688+
(st->st_cur->ste_type == ModuleBlock))
1689+
{
1690+
st->st_cur->ste_coroutine = 1;
1691+
}
1692+
}
1693+
16841694
static int
16851695
symtable_visit_stmt(struct symtable *st, stmt_ty s)
16861696
{
@@ -2074,10 +2084,12 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
20742084
break;
20752085
}
20762086
case AsyncWith_kind:
2087+
maybe_set_ste_coroutine_for_module(st, s);
20772088
VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
20782089
VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
20792090
break;
20802091
case AsyncFor_kind:
2092+
maybe_set_ste_coroutine_for_module(st, s);
20812093
VISIT(st, expr, s->v.AsyncFor.target);
20822094
VISIT(st, expr, s->v.AsyncFor.iter);
20832095
VISIT_SEQ(st, stmt, s->v.AsyncFor.body);

0 commit comments

Comments
 (0)