Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ int _PyCompile_EnterScope(struct _PyCompiler *c, identifier name, int scope_type
void _PyCompile_ExitScope(struct _PyCompiler *c);
Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
_PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
void _PyCompile_SetInstrSequence(struct _PyCompiler *c,
_PyInstructionSequence *instr_sequence);
int _PyCompile_StartAnnotationSetup(struct _PyCompiler *c);
int _PyCompile_EndAnnotationSetup(struct _PyCompiler *c);
int _PyCompile_FutureFeatures(struct _PyCompiler *c);
void _PyCompile_DeferredAnnotations(
struct _PyCompiler *c, PyObject **deferred_annotations,
Expand Down
19 changes: 5 additions & 14 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,15 +795,12 @@ codegen_process_deferred_annotations(compiler *c, location loc)
return SUCCESS;
}

instr_sequence *old_instr_seq = INSTR_SEQUENCE(c);
instr_sequence *nested_instr_seq = NULL;
int scope_type = SCOPE_TYPE(c);
if (scope_type == COMPILE_SCOPE_MODULE) {
nested_instr_seq = (instr_sequence *)_PyInstructionSequence_New();
if (nested_instr_seq == NULL) {
bool need_separate_block = scope_type == COMPILE_SCOPE_MODULE;
if (need_separate_block) {
if (_PyCompile_StartAnnotationSetup(c) == ERROR) {
Copy link
Member

Choose a reason for hiding this comment

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

Great.

We could go one step further: Call these functions _PyCompile_EnterAnnotationsSubscope/_PyCompile_ExitAnnotationsSubscope and let the compiler check whether we are in COMPILE_SCOPE_MODULE (so need to stash) or not. Up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll likely drop the module condition soon for another (more minor) issue, so I'm going to skip that for now. Thanks!

goto error;
}
_PyCompile_SetInstrSequence(c, nested_instr_seq);
}

// It's possible that ste_annotations_block is set but
Expand Down Expand Up @@ -833,18 +830,12 @@ codegen_process_deferred_annotations(compiler *c, location loc)
ste->ste_type == ClassBlock ? &_Py_ID(__annotate_func__) : &_Py_ID(__annotate__),
Store));

if (nested_instr_seq != NULL) {
RETURN_IF_ERROR(
_PyInstructionSequence_SetAnnotationsCode(old_instr_seq, nested_instr_seq));
_PyCompile_SetInstrSequence(c, old_instr_seq);
if (need_separate_block) {
RETURN_IF_ERROR(_PyCompile_EndAnnotationSetup(c));
}

return SUCCESS;
error:
if (nested_instr_seq != NULL) {
PyInstructionSequence_Fini(nested_instr_seq);
_PyCompile_SetInstrSequence(c, old_instr_seq);
}
Py_XDECREF(deferred_anno);
Py_XDECREF(conditional_annotation_indices);
return ERROR;
Expand Down
32 changes: 29 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct compiler_unit {
long u_next_conditional_annotation_index; /* index of the next conditional annotation */

instr_sequence *u_instr_sequence; /* codegen output */
instr_sequence *u_stashed_instr_sequence; /* temporarily stashed parent instruction sequence */

int u_nfblocks;
int u_in_inlined_comp;
Expand Down Expand Up @@ -178,6 +179,7 @@ static void
compiler_unit_free(struct compiler_unit *u)
{
Py_CLEAR(u->u_instr_sequence);
Py_CLEAR(u->u_stashed_instr_sequence);
Py_CLEAR(u->u_ste);
Py_CLEAR(u->u_metadata.u_name);
Py_CLEAR(u->u_metadata.u_qualname);
Expand Down Expand Up @@ -681,6 +683,7 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
compiler_unit_free(u);
return ERROR;
}
u->u_stashed_instr_sequence = NULL;

/* Push the old compiler_unit on the stack. */
if (c->u) {
Expand Down Expand Up @@ -1231,12 +1234,35 @@ _PyCompile_InstrSequence(compiler *c)
return c->u->u_instr_sequence;
}

void
_PyCompile_SetInstrSequence(compiler *c, instr_sequence *seq)
int
_PyCompile_StartAnnotationSetup(struct _PyCompiler *c)
{
c->u->u_instr_sequence = seq;
instr_sequence *new_seq = (instr_sequence *)_PyInstructionSequence_New();
if (new_seq == NULL) {
return ERROR;
}
assert(c->u->u_stashed_instr_sequence == NULL);
c->u->u_stashed_instr_sequence = c->u->u_instr_sequence;
c->u->u_instr_sequence = new_seq;
return SUCCESS;
}

int
_PyCompile_EndAnnotationSetup(struct _PyCompiler *c)
{
assert(c->u->u_stashed_instr_sequence != NULL);
instr_sequence *parent_seq = c->u->u_stashed_instr_sequence;
instr_sequence *anno_seq = c->u->u_instr_sequence;
c->u->u_stashed_instr_sequence = NULL;
c->u->u_instr_sequence = parent_seq;
if (_PyInstructionSequence_SetAnnotationsCode(parent_seq, anno_seq) == ERROR) {
Py_DECREF(anno_seq);
return ERROR;
}
return SUCCESS;
}


int
_PyCompile_FutureFeatures(compiler *c)
{
Expand Down
Loading