@@ -792,6 +792,17 @@ codegen_process_deferred_annotations(compiler *c, location loc)
792792 return SUCCESS ;
793793 }
794794
795+ instr_sequence * old_instr_seq = INSTR_SEQUENCE (c );
796+ instr_sequence * nested_instr_seq = NULL ;
797+ int scope_type = SCOPE_TYPE (c );
798+ if (scope_type == COMPILE_SCOPE_MODULE ) {
799+ nested_instr_seq = (instr_sequence * )_PyInstructionSequence_New ();
800+ if (nested_instr_seq == NULL ) {
801+ goto error ;
802+ }
803+ _PyCompile_SetInstrSequence (c , nested_instr_seq );
804+ }
805+
795806 // It's possible that ste_annotations_block is set but
796807 // u_deferred_annotations is not, because the former is still
797808 // set if there are only non-simple annotations (i.e., annotations
@@ -800,7 +811,6 @@ codegen_process_deferred_annotations(compiler *c, location loc)
800811 PySTEntryObject * ste = SYMTABLE_ENTRY (c );
801812 assert (ste -> ste_annotation_block != NULL );
802813 void * key = (void * )((uintptr_t )ste -> ste_id + 1 );
803- int scope_type = SCOPE_TYPE (c );
804814 if (codegen_setup_annotations_scope (c , loc , key ,
805815 ste -> ste_annotation_block -> ste_name ) < 0 ) {
806816 goto error ;
@@ -817,8 +827,19 @@ codegen_process_deferred_annotations(compiler *c, location loc)
817827 RETURN_IF_ERROR (codegen_leave_annotations_scope (c , loc ));
818828 RETURN_IF_ERROR (codegen_nameop (c , loc , & _Py_ID (__annotate__ ), Store ));
819829
830+ if (nested_instr_seq != NULL ) {
831+ RETURN_IF_ERROR (
832+ _PyInstructionSequence_PrependSequence (old_instr_seq , nested_instr_seq ));
833+ _PyCompile_SetInstrSequence (c , old_instr_seq );
834+ PyInstructionSequence_Fini (nested_instr_seq );
835+ }
836+
820837 return SUCCESS ;
821838error :
839+ if (nested_instr_seq != NULL ) {
840+ PyInstructionSequence_Fini (nested_instr_seq );
841+ _PyCompile_SetInstrSequence (c , old_instr_seq );
842+ }
822843 Py_XDECREF (deferred_anno );
823844 Py_XDECREF (conditional_annotation_indices );
824845 return ERROR ;
0 commit comments