@@ -5035,6 +5035,38 @@ pop_inlined_comprehension_state(compiler *c, location loc,
50355035 return SUCCESS ;
50365036}
50375037
5038+ static int
5039+ codegen_comprehension_init_container (compiler * c , location loc , int type ,
5040+ int is_inlined , bool avoid_creation )
5041+ {
5042+ int op ;
5043+ switch (type ) {
5044+ case COMP_LISTCOMP :
5045+ op = BUILD_LIST ;
5046+ break ;
5047+ case COMP_SETCOMP :
5048+ op = BUILD_SET ;
5049+ break ;
5050+ case COMP_DICTCOMP :
5051+ op = BUILD_MAP ;
5052+ break ;
5053+ default :
5054+ PyErr_Format (PyExc_SystemError ,
5055+ "unknown comprehension type %d" , type );
5056+ return ERROR ;
5057+ }
5058+
5059+ if (!avoid_creation ) {
5060+ ADDOP_I (c , loc , op , 0 );
5061+ if (is_inlined ) {
5062+ ADDOP_I (c , loc , SWAP , 2 );
5063+ }
5064+ } else {
5065+ ADDOP_I (c , loc , COPY , 1 );
5066+ }
5067+ return SUCCESS ;
5068+ }
5069+
50385070static int
50395071codegen_comprehension (compiler * c , expr_ty e , int type ,
50405072 identifier name , asdl_comprehension_seq * generators , expr_ty elt ,
@@ -5098,31 +5130,10 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
50985130 Py_CLEAR (entry );
50995131
51005132 if (type != COMP_GENEXP ) {
5101- int op ;
5102- switch (type ) {
5103- case COMP_LISTCOMP :
5104- op = BUILD_LIST ;
5105- break ;
5106- case COMP_SETCOMP :
5107- op = BUILD_SET ;
5108- break ;
5109- case COMP_DICTCOMP :
5110- op = BUILD_MAP ;
5111- break ;
5112- default :
5113- PyErr_Format (PyExc_SystemError ,
5114- "unknown comprehension type %d" , type );
5133+ if (codegen_comprehension_init_container (
5134+ c , loc , type , is_inlined , avoid_creation ) < 0 ) {
51155135 goto error_in_scope ;
51165136 }
5117-
5118- if (!avoid_creation ) {
5119- ADDOP_I (c , loc , op , 0 );
5120- if (is_inlined ) {
5121- ADDOP_I (c , loc , SWAP , 2 );
5122- }
5123- } else {
5124- ADDOP_I (c , loc , COPY , 1 );
5125- }
51265137 }
51275138 if (codegen_comprehension_generator (c , loc , generators , 0 , 0 ,
51285139 elt , val , type , iter_state , avoid_creation ) < 0 ) {
@@ -5137,7 +5148,7 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
51375148 }
51385149
51395150 if (type != COMP_GENEXP ) {
5140- ADDOP (c , LOC (e ), RETURN_VALUE );
5151+ ADDOP_IN_SCOPE (c , LOC (e ), RETURN_VALUE );
51415152 }
51425153 if (type == COMP_GENEXP ) {
51435154 if (codegen_wrap_in_stopiteration_handler (c ) < 0 ) {
0 commit comments