Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
CR feedback
  • Loading branch information
JelleZijlstra committed Mar 11, 2025
commit 3408c11b23c67afd4c4a5f824c9348b00abbaa3d
10 changes: 5 additions & 5 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,19 @@ codegen_leave_annotations_scope(compiler *c, location loc)
// co->co_localsplusnames = ("format", *co->co_localsplusnames[1:])
const Py_ssize_t size = PyObject_Size(co->co_localsplusnames);
if (size == -1) {
Py_DECREF(co);
return ERROR;
}
PyObject *new_names = PyTuple_New(size);
if (new_names == NULL) {
Py_DECREF(co);
return ERROR;
}
PyTuple_SET_ITEM(new_names, 0, Py_NewRef(&_Py_ID(format)));
for (int i = 1; i < size; i++) {
PyObject *item = PyTuple_GetItem(co->co_localsplusnames, i);
if (item == NULL) {
Py_DECREF(co);
Py_DECREF(new_names);
return ERROR;
}
Expand All @@ -724,9 +727,6 @@ codegen_leave_annotations_scope(compiler *c, location loc)
Py_SETREF(co->co_localsplusnames, new_names);

_PyCompile_ExitScope(c);
if (co == NULL) {
return ERROR;
}
int ret = codegen_make_closure(c, loc, co, 0);
Py_DECREF(co);
RETURN_IF_ERROR(ret);
Expand Down Expand Up @@ -2941,9 +2941,9 @@ codegen_stmt_expr(compiler *c, location loc, expr_ty value)
#define CODEGEN_COND_BLOCK(FUNC, C, S) \
do { \
_PyCompile_EnterConditionalBlock((C)); \
int __result = FUNC((C), (S)); \
int result = FUNC((C), (S)); \
_PyCompile_LeaveConditionalBlock((C)); \
return __result; \
return result; \
} while(0)

static int
Expand Down
4 changes: 2 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ struct compiler_unit {
PyObject *u_private; /* for private name mangling */
PyObject *u_static_attributes; /* for class: attributes accessed via self.X */
PyObject *u_deferred_annotations; /* AnnAssign nodes deferred to the end of compilation */
PyObject *u_conditional_annotation_indices; /* indices of annotations that are conditionally executed (or -1) */
int u_next_conditional_annotation_index; /* index of the next conditional annotation */
PyObject *u_conditional_annotation_indices; /* indices of annotations that are conditionally executed (or -1 for unconditional annotations) */
long u_next_conditional_annotation_index; /* index of the next conditional annotation */

instr_sequence *u_instr_sequence; /* codegen output */

Expand Down
16 changes: 5 additions & 11 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1735,11 +1735,11 @@ symtable_enter_type_param_block(struct symtable *st, identifier name,
} while(0)

#define ENTER_CONDITIONAL_BLOCK(ST) \
int __in_conditional_block = (ST)->st_cur->ste_in_conditional_block; \
int in_conditional_block = (ST)->st_cur->ste_in_conditional_block; \
(ST)->st_cur->ste_in_conditional_block = 1;

#define LEAVE_CONDITIONAL_BLOCK(ST) \
(ST)->st_cur->ste_in_conditional_block = __in_conditional_block;
(ST)->st_cur->ste_in_conditional_block = in_conditional_block;

#define ENTER_RECURSIVE() \
if (Py_EnterRecursiveCall(" during compilation")) { \
Expand Down Expand Up @@ -2728,10 +2728,9 @@ symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
static int
symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
{
bool new_conditional = (st->st_cur->ste_type == ClassBlock || st->st_cur->ste_type == ModuleBlock)
&& st->st_cur->ste_in_conditional_block
&& !st->st_cur->ste_has_conditional_annotations;
if (new_conditional) {
if ((st->st_cur->ste_type == ClassBlock || st->st_cur->ste_type == ModuleBlock)
&& st->st_cur->ste_in_conditional_block
&& !st->st_cur->ste_has_conditional_annotations) {
st->st_cur->ste_has_conditional_annotations = 1;
if (!symtable_add_def(st, &_Py_ID(__conditional_annotations__), USE, LOCATION(annotation))) {
return 0;
Expand Down Expand Up @@ -2759,11 +2758,6 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
return 0;
}
}
if (new_conditional) {
if (!symtable_add_def(st, &_Py_ID(__conditional_annotations__), USE, LOCATION(annotation))) {
return 0;
}
}
VISIT(st, expr, annotation);
if (!symtable_exit_block(st)) {
return 0;
Expand Down