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

@ -1754,6 +1754,13 @@ symtable_enter_type_param_block(struct symtable *st, identifier name,
#define LEAVE_TRY_BLOCK(ST) \
(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() \
if (Py_EnterRecursiveCall(" during compilation")) { \
return 0; \
@ -1826,6 +1833,14 @@ check_lazy_import_context(struct symtable *st, stmt_ty s, const char* import_typ
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 */
if (st->st_cur->ste_type == FunctionBlock) {
PyErr_Format(PyExc_SyntaxError,
@ -2241,8 +2256,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
break;
case With_kind: {
ENTER_CONDITIONAL_BLOCK(st);
ENTER_WITH_BLOCK(st);
VISIT_SEQ(st, withitem, s->v.With.items);
VISIT_SEQ(st, stmt, s->v.With.body);
LEAVE_WITH_BLOCK(st);
LEAVE_CONDITIONAL_BLOCK(st);
break;
}
@ -2307,8 +2324,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
return 0;
}
ENTER_CONDITIONAL_BLOCK(st);
ENTER_WITH_BLOCK(st);
VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
LEAVE_WITH_BLOCK(st);
LEAVE_CONDITIONAL_BLOCK(st);
break;
}