Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-121272: move async for/with validation from compiler to symtable #121361

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3058,11 +3058,6 @@ static int
compiler_async_for(struct compiler *c, stmt_ty s)
{
location loc = LOC(s);
if (IS_TOP_LEVEL_AWAIT(c)){
assert(c->u->u_ste->ste_coroutine == 1);
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
return compiler_error(c, loc, "'async for' outside async function");
}

NEW_JUMP_TARGET_LABEL(c, start);
NEW_JUMP_TARGET_LABEL(c, except);
Expand Down Expand Up @@ -5781,9 +5776,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,

co = optimize_and_assemble(c, 1);
compiler_exit_scope(c);
if (is_top_level_await && is_async_generator){
assert(c->u->u_ste->ste_coroutine == 1);
}
if (co == NULL) {
goto error;
}
Expand Down Expand Up @@ -5925,11 +5917,6 @@ 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 (IS_TOP_LEVEL_AWAIT(c)){
assert(c->u->u_ste->ste_coroutine == 1);
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
return compiler_error(c, loc, "'async with' outside async function");
}

NEW_JUMP_TARGET_LABEL(c, block);
NEW_JUMP_TARGET_LABEL(c, final);
Expand Down
22 changes: 22 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
#define DUPLICATE_TYPE_PARAM \
"duplicate type parameter '%U'"

#define ASYNC_WITH_OUTISDE_ASYNC_FUNC \
"'async with' outside async function"

#define ASYNC_FOR_OUTISDE_ASYNC_FUNC \
"'async for' outside async function"

#define LOCATION(x) SRC_LOCATION_FROM_AST(x)

Expand Down Expand Up @@ -251,6 +256,7 @@ static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
static int symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc);
Copy link
Contributor

@picnixz picnixz Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other functions have a signature that take the statement or expression as a parameter instead of the location directly. So maybe you could do the same here and just call LOCATION(s) when you call SET_ERROR_LOCATION?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would unnecessarily make this function specific to stmt_ty. There is likely to be an expr_ty luse case for it as well.

In general, a function is ideally passed what it actually needs (unless its purpose is to abstract something out).

static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);

/* For debugging purposes only */
Expand Down Expand Up @@ -2048,11 +2054,17 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
}
case AsyncWith_kind:
maybe_set_ste_coroutine_for_module(st, s);
if (!symtable_raise_if_not_coroutine(st, ASYNC_WITH_OUTISDE_ASYNC_FUNC, LOCATION(s))) {
VISIT_QUIT(st, 0);
}
VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
break;
case AsyncFor_kind:
maybe_set_ste_coroutine_for_module(st, s);
if (!symtable_raise_if_not_coroutine(st, ASYNC_FOR_OUTISDE_ASYNC_FUNC, LOCATION(s))) {
VISIT_QUIT(st, 0);
}
VISIT(st, expr, s->v.AsyncFor.target);
VISIT(st, expr, s->v.AsyncFor.iter);
VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
Expand Down Expand Up @@ -2865,6 +2877,16 @@ symtable_raise_if_comprehension_block(struct symtable *st, expr_ty e) {
VISIT_QUIT(st, 0);
}

static int
symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc) {
if (!st->st_cur->ste_coroutine) {
PyErr_SetString(PyExc_SyntaxError, msg);
SET_ERROR_LOCATION(st->st_filename, loc);
return 0;
}
return 1;
}

struct symtable *
_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
int start, PyCompilerFlags *flags)
Expand Down
Loading