Skip to content

Commit 5d6026a

Browse files
committed
Make imports in with blocks syntax errors
1 parent 7c49405 commit 5d6026a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Include/internal/pycore_symtable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ typedef struct _symtable_entry {
127127
unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */
128128
unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */
129129
unsigned ste_in_try_block : 1; /* set while we are inside a try/except block */
130+
unsigned ste_in_with_block : 1; /* set while we are inside a with block */
130131
unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */
131132
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
132133
_Py_SourceLocation ste_loc; /* source location of block */

Python/symtable.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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() \
17581765
if (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

Comments
 (0)