@@ -1754,6 +1754,13 @@ symtable_enter_type_param_block(struct symtable *st, identifier name,
17541754#define LEAVE_TRY_BLOCK (ST ) \
17551755 (ST)->st_cur->ste_in_try_block = in_try_block;
17561756
1757+ #define ENTER_WITH_BLOCK (ST ) \
1758+ int in_with_block = (ST)->st_cur->ste_in_with_block; \
1759+ (ST)->st_cur->ste_in_with_block = 1;
1760+
1761+ #define LEAVE_WITH_BLOCK (ST ) \
1762+ (ST)->st_cur->ste_in_with_block = in_with_block;
1763+
17571764#define ENTER_RECURSIVE () \
17581765if (Py_EnterRecursiveCall(" during compilation")) { \
17591766 return 0; \
@@ -1826,6 +1833,14 @@ check_lazy_import_context(struct symtable *st, stmt_ty s, const char* import_typ
18261833 return 0 ;
18271834 }
18281835
1836+ /* Check if inside with block */
1837+ if (st -> st_cur -> ste_in_with_block ) {
1838+ PyErr_Format (PyExc_SyntaxError ,
1839+ "lazy %s not allowed inside with blocks" , import_type );
1840+ SET_ERROR_LOCATION (st -> st_filename , LOCATION (s ));
1841+ return 0 ;
1842+ }
1843+
18291844 /* Check if inside function scope */
18301845 if (st -> st_cur -> ste_type == FunctionBlock ) {
18311846 PyErr_Format (PyExc_SyntaxError ,
@@ -2241,8 +2256,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
22412256 break ;
22422257 case With_kind : {
22432258 ENTER_CONDITIONAL_BLOCK (st );
2259+ ENTER_WITH_BLOCK (st );
22442260 VISIT_SEQ (st , withitem , s -> v .With .items );
22452261 VISIT_SEQ (st , stmt , s -> v .With .body );
2262+ LEAVE_WITH_BLOCK (st );
22462263 LEAVE_CONDITIONAL_BLOCK (st );
22472264 break ;
22482265 }
@@ -2307,8 +2324,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
23072324 return 0 ;
23082325 }
23092326 ENTER_CONDITIONAL_BLOCK (st );
2327+ ENTER_WITH_BLOCK (st );
23102328 VISIT_SEQ (st , withitem , s -> v .AsyncWith .items );
23112329 VISIT_SEQ (st , stmt , s -> v .AsyncWith .body );
2330+ LEAVE_WITH_BLOCK (st );
23122331 LEAVE_CONDITIONAL_BLOCK (st );
23132332 break ;
23142333 }
0 commit comments