Make imports in with blocks syntax errors

This commit is contained in:
Pablo Galindo Salgado 2025-10-02 21:12:52 +01:00
parent 7c494053b0
commit 5d6026a800
2 changed files with 20 additions and 0 deletions

View file

@ -127,6 +127,7 @@ typedef struct _symtable_entry {
unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */ unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */
unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */ unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */
unsigned ste_in_try_block : 1; /* set while we are inside a try/except block */ unsigned ste_in_try_block : 1; /* set while we are inside a try/except block */
unsigned ste_in_with_block : 1; /* set while we are inside a with block */
unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */ unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */ int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
_Py_SourceLocation ste_loc; /* source location of block */ _Py_SourceLocation ste_loc; /* source location of block */

View file

@ -1754,6 +1754,13 @@ symtable_enter_type_param_block(struct symtable *st, identifier name,
#define LEAVE_TRY_BLOCK(ST) \ #define LEAVE_TRY_BLOCK(ST) \
(ST)->st_cur->ste_in_try_block = in_try_block; (ST)->st_cur->ste_in_try_block = in_try_block;
#define ENTER_WITH_BLOCK(ST) \
int in_with_block = (ST)->st_cur->ste_in_with_block; \
(ST)->st_cur->ste_in_with_block = 1;
#define LEAVE_WITH_BLOCK(ST) \
(ST)->st_cur->ste_in_with_block = in_with_block;
#define ENTER_RECURSIVE() \ #define ENTER_RECURSIVE() \
if (Py_EnterRecursiveCall(" during compilation")) { \ if (Py_EnterRecursiveCall(" during compilation")) { \
return 0; \ return 0; \
@ -1826,6 +1833,14 @@ check_lazy_import_context(struct symtable *st, stmt_ty s, const char* import_typ
return 0; return 0;
} }
/* Check if inside with block */
if (st->st_cur->ste_in_with_block) {
PyErr_Format(PyExc_SyntaxError,
"lazy %s not allowed inside with blocks", import_type);
SET_ERROR_LOCATION(st->st_filename, LOCATION(s));
return 0;
}
/* Check if inside function scope */ /* Check if inside function scope */
if (st->st_cur->ste_type == FunctionBlock) { if (st->st_cur->ste_type == FunctionBlock) {
PyErr_Format(PyExc_SyntaxError, PyErr_Format(PyExc_SyntaxError,
@ -2241,8 +2256,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
break; break;
case With_kind: { case With_kind: {
ENTER_CONDITIONAL_BLOCK(st); ENTER_CONDITIONAL_BLOCK(st);
ENTER_WITH_BLOCK(st);
VISIT_SEQ(st, withitem, s->v.With.items); VISIT_SEQ(st, withitem, s->v.With.items);
VISIT_SEQ(st, stmt, s->v.With.body); VISIT_SEQ(st, stmt, s->v.With.body);
LEAVE_WITH_BLOCK(st);
LEAVE_CONDITIONAL_BLOCK(st); LEAVE_CONDITIONAL_BLOCK(st);
break; break;
} }
@ -2307,8 +2324,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
return 0; return 0;
} }
ENTER_CONDITIONAL_BLOCK(st); ENTER_CONDITIONAL_BLOCK(st);
ENTER_WITH_BLOCK(st);
VISIT_SEQ(st, withitem, s->v.AsyncWith.items); VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
VISIT_SEQ(st, stmt, s->v.AsyncWith.body); VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
LEAVE_WITH_BLOCK(st);
LEAVE_CONDITIONAL_BLOCK(st); LEAVE_CONDITIONAL_BLOCK(st);
break; break;
} }