Skip to content

Commit 67a05de

Browse files
authored
gh-121272: move async for/with validation from compiler to symtable (#121361)
1 parent 715ec63 commit 67a05de

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

Python/compile.c

-13
Original file line numberDiff line numberDiff line change
@@ -3058,11 +3058,6 @@ static int
30583058
compiler_async_for(struct compiler *c, stmt_ty s)
30593059
{
30603060
location loc = LOC(s);
3061-
if (IS_TOP_LEVEL_AWAIT(c)){
3062-
assert(c->u->u_ste->ste_coroutine == 1);
3063-
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
3064-
return compiler_error(c, loc, "'async for' outside async function");
3065-
}
30663061

30673062
NEW_JUMP_TARGET_LABEL(c, start);
30683063
NEW_JUMP_TARGET_LABEL(c, except);
@@ -5781,9 +5776,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
57815776

57825777
co = optimize_and_assemble(c, 1);
57835778
compiler_exit_scope(c);
5784-
if (is_top_level_await && is_async_generator){
5785-
assert(c->u->u_ste->ste_coroutine == 1);
5786-
}
57875779
if (co == NULL) {
57885780
goto error;
57895781
}
@@ -5925,11 +5917,6 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
59255917
withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
59265918

59275919
assert(s->kind == AsyncWith_kind);
5928-
if (IS_TOP_LEVEL_AWAIT(c)){
5929-
assert(c->u->u_ste->ste_coroutine == 1);
5930-
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
5931-
return compiler_error(c, loc, "'async with' outside async function");
5932-
}
59335920

59345921
NEW_JUMP_TARGET_LABEL(c, block);
59355922
NEW_JUMP_TARGET_LABEL(c, final);

Python/symtable.c

+22
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
#define DUPLICATE_TYPE_PARAM \
7171
"duplicate type parameter '%U'"
7272

73+
#define ASYNC_WITH_OUTISDE_ASYNC_FUNC \
74+
"'async with' outside async function"
75+
76+
#define ASYNC_FOR_OUTISDE_ASYNC_FUNC \
77+
"'async for' outside async function"
7378

7479
#define LOCATION(x) SRC_LOCATION_FROM_AST(x)
7580

@@ -251,6 +256,7 @@ static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
251256
static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
252257
static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
253258
static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
259+
static int symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc);
254260
static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);
255261

256262
/* For debugging purposes only */
@@ -2048,11 +2054,17 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
20482054
}
20492055
case AsyncWith_kind:
20502056
maybe_set_ste_coroutine_for_module(st, s);
2057+
if (!symtable_raise_if_not_coroutine(st, ASYNC_WITH_OUTISDE_ASYNC_FUNC, LOCATION(s))) {
2058+
VISIT_QUIT(st, 0);
2059+
}
20512060
VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
20522061
VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
20532062
break;
20542063
case AsyncFor_kind:
20552064
maybe_set_ste_coroutine_for_module(st, s);
2065+
if (!symtable_raise_if_not_coroutine(st, ASYNC_FOR_OUTISDE_ASYNC_FUNC, LOCATION(s))) {
2066+
VISIT_QUIT(st, 0);
2067+
}
20562068
VISIT(st, expr, s->v.AsyncFor.target);
20572069
VISIT(st, expr, s->v.AsyncFor.iter);
20582070
VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
@@ -2865,6 +2877,16 @@ symtable_raise_if_comprehension_block(struct symtable *st, expr_ty e) {
28652877
VISIT_QUIT(st, 0);
28662878
}
28672879

2880+
static int
2881+
symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc) {
2882+
if (!st->st_cur->ste_coroutine) {
2883+
PyErr_SetString(PyExc_SyntaxError, msg);
2884+
SET_ERROR_LOCATION(st->st_filename, loc);
2885+
return 0;
2886+
}
2887+
return 1;
2888+
}
2889+
28682890
struct symtable *
28692891
_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
28702892
int start, PyCompilerFlags *flags)

0 commit comments

Comments
 (0)