Skip to content
Open
Changes from 1 commit
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
Next Next commit
refactor codegen_deferred_annotations_body to ensure mangled is decre…
…f'd on error
  • Loading branch information
A0su committed Apr 3, 2026
commit a8c0c60020bbfecc72afde734db975279b3c2784
69 changes: 39 additions & 30 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,41 @@ codegen_leave_annotations_scope(compiler *c, location loc)
return SUCCESS;
}

static int
codegen_deferred_annotations_entry(compiler *c, location loc,
PyObject *conditional_annotation_indices, int scope_type, stmt_ty st, PyObject *mangled, Py_ssize_t i)
{
PyObject *cond_index = PyList_GET_ITEM(conditional_annotation_indices, i);
assert(PyLong_CheckExact(cond_index));
long idx = PyLong_AS_LONG(cond_index);
NEW_JUMP_TARGET_LABEL(c, not_set);

if (idx != -1) {
ADDOP_LOAD_CONST(c, LOC(st), cond_index);
if (scope_type == COMPILE_SCOPE_CLASS) {
ADDOP_NAME(
c, LOC(st), LOAD_DEREF, &_Py_ID(__conditional_annotations__), freevars);
}
else {
ADDOP_NAME(
c, LOC(st), LOAD_GLOBAL, &_Py_ID(__conditional_annotations__), names);
}

ADDOP_I(c, LOC(st), CONTAINS_OP, 0);
ADDOP_JUMP(c, LOC(st), POP_JUMP_IF_FALSE, not_set);
}

VISIT(c, expr, st->v.AnnAssign.annotation);
ADDOP_I(c, LOC(st), COPY, 2);
ADDOP_LOAD_CONST(c, LOC(st), mangled);
// stack now contains <annos> <name> <annos> <value>
ADDOP(c, loc, STORE_SUBSCR);
// stack now contains <annos>

USE_LABEL(c, not_set);
return SUCCESS;
}

static int
codegen_deferred_annotations_body(compiler *c, location loc,
PyObject *deferred_anno, PyObject *conditional_annotation_indices, int scope_type)
Expand All @@ -798,37 +833,11 @@ codegen_deferred_annotations_body(compiler *c, location loc,
if (!mangled) {
return ERROR;
}
// NOTE: ref of mangled can be leaked on ADDOP* and VISIT macros due to early returns
// fixing would require an overhaul of these macros

PyObject *cond_index = PyList_GET_ITEM(conditional_annotation_indices, i);
assert(PyLong_CheckExact(cond_index));
long idx = PyLong_AS_LONG(cond_index);
NEW_JUMP_TARGET_LABEL(c, not_set);

if (idx != -1) {
ADDOP_LOAD_CONST(c, LOC(st), cond_index);
if (scope_type == COMPILE_SCOPE_CLASS) {
ADDOP_NAME(
c, LOC(st), LOAD_DEREF, &_Py_ID(__conditional_annotations__), freevars);
}
else {
ADDOP_NAME(
c, LOC(st), LOAD_GLOBAL, &_Py_ID(__conditional_annotations__), names);
}

ADDOP_I(c, LOC(st), CONTAINS_OP, 0);
ADDOP_JUMP(c, LOC(st), POP_JUMP_IF_FALSE, not_set);
}

VISIT(c, expr, st->v.AnnAssign.annotation);
ADDOP_I(c, LOC(st), COPY, 2);
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
// stack now contains <annos> <name> <annos> <value>
ADDOP(c, loc, STORE_SUBSCR);
// stack now contains <annos>

USE_LABEL(c, not_set);
int ret = codegen_deferred_annotations_entry(c, loc, conditional_annotation_indices,
scope_type, st, mangled, i);
Py_DECREF(mangled);
RETURN_IF_ERROR(ret);
}
return SUCCESS;
}
Expand Down