@@ -3342,7 +3342,7 @@ codegen_nameop(compiler *c, location loc,
33423342 case COMPILE_OP_DEREF :
33433343 switch (ctx ) {
33443344 case Load :
3345- if (SYMTABLE_ENTRY (c )-> ste_type == ClassBlock && ! _PyCompile_IsInInlinedComp ( c ) ) {
3345+ if (SYMTABLE_ENTRY (c )-> ste_type == ClassBlock ) {
33463346 op = LOAD_FROM_DICT_OR_DEREF ;
33473347 // First load the locals
33483348 if (codegen_addop_noarg (INSTR_SEQUENCE (c ), LOAD_LOCALS , loc ) < 0 ) {
@@ -3395,8 +3395,9 @@ codegen_nameop(compiler *c, location loc,
33953395 case COMPILE_OP_NAME :
33963396 switch (ctx ) {
33973397 case Load :
3398- op = (SYMTABLE_ENTRY (c )-> ste_type == ClassBlock
3399- && _PyCompile_IsInInlinedComp (c ))
3398+ /* LOAD_NAME in a class reads the class dict; inlined comps must not. */
3399+ op = (SCOPE_TYPE (c ) == COMPILE_SCOPE_CLASS
3400+ && SYMTABLE_ENTRY (c )-> ste_type == InlinedComprehensionBlock )
34003401 ? LOAD_GLOBAL
34013402 : LOAD_NAME ;
34023403 break ;
@@ -4917,9 +4918,6 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
49174918 PySTEntryObject * comp ,
49184919 _PyCompile_InlinedComprehensionState * state )
49194920{
4920- int in_class_block = (SYMTABLE_ENTRY (c )-> ste_type == ClassBlock ) &&
4921- !_PyCompile_IsInInlinedComp (c );
4922- PySTEntryObject * outer = SYMTABLE_ENTRY (c );
49234921 // iterate over names bound in the comprehension and ensure we isolate
49244922 // them from the outer scope as needed
49254923 PyObject * k , * v ;
@@ -4930,11 +4928,7 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
49304928 RETURN_IF_ERROR (symbol );
49314929 long scope = SYMBOL_TO_SCOPE (symbol );
49324930
4933- long outsymbol = _PyST_GetSymbol (outer , k );
4934- RETURN_IF_ERROR (outsymbol );
4935- long outsc = SYMBOL_TO_SCOPE (outsymbol );
4936-
4937- if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL )) || in_class_block ) {
4931+ if ((symbol & DEF_LOCAL ) && !(symbol & DEF_NONLOCAL )) {
49384932 // local names bound in comprehension must be isolated from
49394933 // outer scope; push existing value (which may be NULL if
49404934 // not defined) on stack
@@ -4949,15 +4943,17 @@ codegen_push_inlined_comprehension_locals(compiler *c, location loc,
49494943 // comprehension and restore the original one after
49504944 ADDOP_NAME (c , loc , LOAD_FAST_AND_CLEAR , k , varnames );
49514945 if (scope == CELL ) {
4952- if (outsc == FREE ) {
4953- ADDOP_NAME (c , loc , MAKE_CELL , k , freevars );
4954- } else {
4955- ADDOP_NAME (c , loc , MAKE_CELL , k , cellvars );
4956- }
4946+ ADDOP_NAME (c , loc , MAKE_CELL , k , cellvars );
49574947 }
49584948 if (PyList_Append (state -> pushed_locals , k ) < 0 ) {
49594949 return ERROR ;
49604950 }
4951+ if (METADATA (c )-> u_fasthidden != NULL ) {
4952+ /* For Module/Class scopes, assemble needs to set CO_FAST_HIDDEN on these names */
4953+ if (PySet_Add (METADATA (c )-> u_fasthidden , k ) < 0 ) {
4954+ return ERROR ;
4955+ }
4956+ }
49614957 }
49624958 }
49634959 if (state -> pushed_locals ) {
@@ -4986,7 +4982,7 @@ push_inlined_comprehension_state(compiler *c, location loc,
49864982 _PyCompile_InlinedComprehensionState * state )
49874983{
49884984 RETURN_IF_ERROR (
4989- _PyCompile_TweakInlinedComprehensionScopes (c , loc , comp , state ));
4985+ _PyCompile_EnterInlinedComprehensionScope (c , loc , comp , state ));
49904986 RETURN_IF_ERROR (
49914987 codegen_push_inlined_comprehension_locals (c , loc , comp , state ));
49924988 return SUCCESS ;
@@ -5044,7 +5040,7 @@ pop_inlined_comprehension_state(compiler *c, location loc,
50445040 _PyCompile_InlinedComprehensionState * state )
50455041{
50465042 RETURN_IF_ERROR (codegen_pop_inlined_comprehension_locals (c , loc , state ));
5047- RETURN_IF_ERROR (_PyCompile_RevertInlinedComprehensionScopes (c , loc , state ));
5043+ RETURN_IF_ERROR (_PyCompile_ExitInlinedComprehensionScope (c , loc , state ));
50485044 return SUCCESS ;
50495045}
50505046
@@ -5054,13 +5050,13 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
50545050 expr_ty val , bool avoid_creation )
50555051{
50565052 PyCodeObject * co = NULL ;
5057- _PyCompile_InlinedComprehensionState inline_state = {NULL , NULL , NULL , NO_LABEL };
5053+ _PyCompile_InlinedComprehensionState inline_state = {NULL , NO_LABEL , NULL };
50585054 comprehension_ty outermost ;
50595055 PySTEntryObject * entry = _PySymtable_Lookup (SYMTABLE (c ), (void * )e );
50605056 if (entry == NULL ) {
50615057 goto error ;
50625058 }
5063- int is_inlined = entry -> ste_comp_inlined ;
5059+ int is_inlined = ( entry -> ste_type == InlinedComprehensionBlock ) ;
50645060 int is_async_comprehension = entry -> ste_coroutine ;
50655061
50665062 location loc = LOC (e );
@@ -5069,7 +5065,7 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
50695065 IterStackPosition iter_state ;
50705066 if (is_inlined ) {
50715067 VISIT (c , expr , outermost -> iter );
5072- if (push_inlined_comprehension_state (c , loc , entry , & inline_state )) {
5068+ if (push_inlined_comprehension_state (c , loc , entry , & inline_state ) < 0 ) {
50735069 goto error ;
50745070 }
50755071 iter_state = ITERABLE_ON_STACK ;
@@ -5140,8 +5136,8 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
51405136 }
51415137
51425138 if (is_inlined ) {
5143- if (pop_inlined_comprehension_state (c , loc , & inline_state )) {
5144- goto error ;
5139+ if (pop_inlined_comprehension_state (c , loc , & inline_state ) < 0 ) {
5140+ goto error_in_scope ;
51455141 }
51465142 return SUCCESS ;
51475143 }
@@ -5181,15 +5177,18 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
51815177
51825178 return SUCCESS ;
51835179error_in_scope :
5184- if (!is_inlined ) {
5180+ if (is_inlined ) {
5181+ if (inline_state .saved_ste != NULL ) {
5182+ pop_inlined_comprehension_state (c , loc , & inline_state );
5183+ }
5184+ }
5185+ else {
51855186 _PyCompile_ExitScope (c );
51865187 }
51875188error :
51885189 Py_XDECREF (co );
51895190 Py_XDECREF (entry );
51905191 Py_XDECREF (inline_state .pushed_locals );
5191- Py_XDECREF (inline_state .temp_symbols );
5192- Py_XDECREF (inline_state .fast_hidden );
51935192 return ERROR ;
51945193}
51955194
0 commit comments