Skip to content

Commit 862e5bf

Browse files
committed
Let compile.c manage the instrseq
1 parent aaa534a commit 862e5bf

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

Include/internal/pycore_compile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ int _PyCompile_EnterScope(struct _PyCompiler *c, identifier name, int scope_type
133133
void _PyCompile_ExitScope(struct _PyCompiler *c);
134134
Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
135135
_PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
136-
void _PyCompile_SetInstrSequence(struct _PyCompiler *c,
137-
_PyInstructionSequence *instr_sequence);
136+
int _PyCompile_StartAnnotationSetup(struct _PyCompiler *c);
137+
int _PyCompile_EndAnnotationSetup(struct _PyCompiler *c);
138138
int _PyCompile_FutureFeatures(struct _PyCompiler *c);
139139
void _PyCompile_DeferredAnnotations(
140140
struct _PyCompiler *c, PyObject **deferred_annotations,

Python/codegen.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -795,15 +795,12 @@ codegen_process_deferred_annotations(compiler *c, location loc)
795795
return SUCCESS;
796796
}
797797

798-
instr_sequence *old_instr_seq = INSTR_SEQUENCE(c);
799-
instr_sequence *nested_instr_seq = NULL;
800798
int scope_type = SCOPE_TYPE(c);
801-
if (scope_type == COMPILE_SCOPE_MODULE) {
802-
nested_instr_seq = (instr_sequence *)_PyInstructionSequence_New();
803-
if (nested_instr_seq == NULL) {
799+
bool need_separate_block = scope_type == COMPILE_SCOPE_MODULE;
800+
if (need_separate_block) {
801+
if (_PyCompile_StartAnnotationSetup(c) == ERROR) {
804802
goto error;
805803
}
806-
_PyCompile_SetInstrSequence(c, nested_instr_seq);
807804
}
808805

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

836-
if (nested_instr_seq != NULL) {
837-
RETURN_IF_ERROR(
838-
_PyInstructionSequence_SetAnnotationsCode(old_instr_seq, nested_instr_seq));
839-
_PyCompile_SetInstrSequence(c, old_instr_seq);
833+
if (need_separate_block) {
834+
RETURN_IF_ERROR(_PyCompile_EndAnnotationSetup(c));
840835
}
841836

842837
return SUCCESS;
843838
error:
844-
if (nested_instr_seq != NULL) {
845-
PyInstructionSequence_Fini(nested_instr_seq);
846-
_PyCompile_SetInstrSequence(c, old_instr_seq);
847-
}
848839
Py_XDECREF(deferred_anno);
849840
Py_XDECREF(conditional_annotation_indices);
850841
return ERROR;

Python/compile.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct compiler_unit {
6464
long u_next_conditional_annotation_index; /* index of the next conditional annotation */
6565

6666
instr_sequence *u_instr_sequence; /* codegen output */
67+
instr_sequence *u_stashed_instr_sequence; /* temporarily stashed parent instruction sequence */
6768

6869
int u_nfblocks;
6970
int u_in_inlined_comp;
@@ -178,6 +179,7 @@ static void
178179
compiler_unit_free(struct compiler_unit *u)
179180
{
180181
Py_CLEAR(u->u_instr_sequence);
182+
Py_CLEAR(u->u_stashed_instr_sequence);
181183
Py_CLEAR(u->u_ste);
182184
Py_CLEAR(u->u_metadata.u_name);
183185
Py_CLEAR(u->u_metadata.u_qualname);
@@ -681,6 +683,7 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
681683
compiler_unit_free(u);
682684
return ERROR;
683685
}
686+
u->u_stashed_instr_sequence = NULL;
684687

685688
/* Push the old compiler_unit on the stack. */
686689
if (c->u) {
@@ -1231,12 +1234,35 @@ _PyCompile_InstrSequence(compiler *c)
12311234
return c->u->u_instr_sequence;
12321235
}
12331236

1234-
void
1235-
_PyCompile_SetInstrSequence(compiler *c, instr_sequence *seq)
1237+
int
1238+
_PyCompile_StartAnnotationSetup(struct _PyCompiler *c)
12361239
{
1237-
c->u->u_instr_sequence = seq;
1240+
instr_sequence *new_seq = (instr_sequence *)_PyInstructionSequence_New();
1241+
if (new_seq == NULL) {
1242+
return ERROR;
1243+
}
1244+
assert(c->u->u_stashed_instr_sequence == NULL);
1245+
c->u->u_stashed_instr_sequence = c->u->u_instr_sequence;
1246+
c->u->u_instr_sequence = new_seq;
1247+
return SUCCESS;
12381248
}
12391249

1250+
int
1251+
_PyCompile_EndAnnotationSetup(struct _PyCompiler *c)
1252+
{
1253+
assert(c->u->u_stashed_instr_sequence != NULL);
1254+
instr_sequence *parent_seq = c->u->u_stashed_instr_sequence;
1255+
instr_sequence *anno_seq = c->u->u_instr_sequence;
1256+
c->u->u_stashed_instr_sequence = NULL;
1257+
c->u->u_instr_sequence = parent_seq;
1258+
if (_PyInstructionSequence_SetAnnotationsCode(parent_seq, anno_seq) == ERROR) {
1259+
Py_DECREF(anno_seq);
1260+
return ERROR;
1261+
}
1262+
return SUCCESS;
1263+
}
1264+
1265+
12401266
int
12411267
_PyCompile_FutureFeatures(compiler *c)
12421268
{

0 commit comments

Comments
 (0)