Skip to content

Commit d7576c4

Browse files
committed
py/compile2: Fix bug with break/continue in else of optimised for-range.
A port of 4c5f108.
1 parent d88eab7 commit d7576c4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

py/compile2.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,19 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, const byte *pn_va
14551455
// break/continue apply to outer loop (if any) in the else block
14561456
END_BREAK_CONTINUE_BLOCK
14571457

1458+
// Compile the else block. We must pop the iterator variables before
1459+
// executing the else code because it may contain break/continue statements.
1460+
uint end_label = 0;
14581461
if (pn_else != NULL) {
1462+
// discard final value of "var", and possible "end" value
1463+
EMIT(pop_top);
1464+
if (end_on_stack) {
1465+
EMIT(pop_top);
1466+
}
14591467
compile_node(comp, pn_else);
1468+
end_label = comp_next_label(comp);
1469+
EMIT_ARG(jump, end_label);
1470+
EMIT_ARG(adjust_stack_size, 1 + end_on_stack);
14601471
}
14611472

14621473
EMIT_ARG(label_assign, break_label);
@@ -1468,6 +1479,10 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, const byte *pn_va
14681479
if (end_on_stack) {
14691480
EMIT(pop_top);
14701481
}
1482+
1483+
if (pn_else != NULL) {
1484+
EMIT_ARG(label_assign, end_label);
1485+
}
14711486
}
14721487

14731488
STATIC void compile_for_stmt(compiler_t *comp, const byte *p, const byte *ptop) {

0 commit comments

Comments
 (0)