Skip to content

Commit a7c02c4

Browse files
committed
vm: Null pointer test when checking for StopIteration optimizations.
When generator raises exception, it is automatically terminated (by setting its code_state.ip to 0), which interferes with this check. Triggered in particular by CPython's test_pep380.py.
1 parent 8fbabab commit a7c02c4

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

py/vm.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,17 @@ unwind_jump:;
12401240
code_state->ip -= 1;
12411241
#endif
12421242

1243-
// check if it's a StopIteration within a for block
1244-
if (*code_state->ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
1245-
const byte *ip = code_state->ip + 1;
1246-
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
1247-
code_state->ip = ip + ulab; // jump to after for-block
1248-
code_state->sp -= 1; // pop the exhausted iterator
1249-
goto outer_dispatch_loop; // continue with dispatch loop
1243+
if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
1244+
if (code_state->ip) {
1245+
// check if it's a StopIteration within a for block
1246+
if (*code_state->ip == MP_BC_FOR_ITER) {
1247+
const byte *ip = code_state->ip + 1;
1248+
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
1249+
code_state->ip = ip + ulab; // jump to after for-block
1250+
code_state->sp -= 1; // pop the exhausted iterator
1251+
goto outer_dispatch_loop; // continue with dispatch loop
1252+
}
1253+
}
12501254
}
12511255

12521256
#if MICROPY_STACKLESS

0 commit comments

Comments
 (0)