Skip to content

Commit

Permalink
Exit scope correctly on error
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed May 24, 2024
1 parent a121e1a commit 242301c
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,38 @@ compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
return SUCCESS;
}

static int
compiler_visit_annotations_in_scope(struct compiler *c, location loc,
arguments_ty args, expr_ty returns,
Py_ssize_t *annotations_len)
{
RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->args, annotations_len, loc));

RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->posonlyargs, annotations_len, loc));

if (args->vararg && args->vararg->annotation) {
RETURN_IF_ERROR(
compiler_visit_argannotation(c, args->vararg->arg,
args->vararg->annotation, annotations_len, loc));
}

RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->kwonlyargs, annotations_len, loc));

if (args->kwarg && args->kwarg->annotation) {
RETURN_IF_ERROR(
compiler_visit_argannotation(c, args->kwarg->arg,
args->kwarg->annotation, annotations_len, loc));
}

RETURN_IF_ERROR(
compiler_visit_argannotation(c, &_Py_ID(return), returns, annotations_len, loc));

return 0;
}

static int
compiler_visit_annotations(struct compiler *c, location loc,
arguments_ty args, expr_ty returns)
Expand All @@ -2139,8 +2171,9 @@ compiler_visit_annotations(struct compiler *c, location loc,
return ERROR;
}
assert(ste != NULL);
bool annotations_used = ste->ste_annotations_used;

if (!future_annotations && ste->ste_annotations_used) {
if (!future_annotations && annotations_used) {
if (compiler_setup_annotations_scope(c, loc, (void *)args, raise_notimp,
ste->ste_name) < 0) {
Py_DECREF(ste);
Expand All @@ -2149,39 +2182,21 @@ compiler_visit_annotations(struct compiler *c, location loc,
}
Py_DECREF(ste);

RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->args, &annotations_len, loc));

RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc));

if (args->vararg && args->vararg->annotation) {
RETURN_IF_ERROR(
compiler_visit_argannotation(c, args->vararg->arg,
args->vararg->annotation, &annotations_len, loc));
}

RETURN_IF_ERROR(
compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc));

if (args->kwarg && args->kwarg->annotation) {
RETURN_IF_ERROR(
compiler_visit_argannotation(c, args->kwarg->arg,
args->kwarg->annotation, &annotations_len, loc));
if (compiler_visit_annotations_in_scope(c, loc, args, returns, &annotations_len) < 0) {
if (!future_annotations && annotations_used) {
compiler_exit_scope(c);
}
return ERROR;
}

RETURN_IF_ERROR(
compiler_visit_argannotation(c, &_Py_ID(return), returns, &annotations_len, loc));

if (future_annotations) {
if (annotations_len) {
ADDOP_I(c, loc, BUILD_TUPLE, annotations_len * 2);
return MAKE_FUNCTION_ANNOTATIONS;
}
}
else {
assert(ste != NULL);
if (ste->ste_annotations_used) {
if (annotations_used) {
RETURN_IF_ERROR(
compiler_leave_annotations_scope(c, loc, annotations_len, raise_notimp)
);
Expand Down

0 comments on commit 242301c

Please sign in to comment.