Skip to content

Commit d99944a

Browse files
committed
py: Clear state to MP_OBJ_NULL before executing byte code.
1 parent a157e4c commit d99944a

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

py/objgenerator.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,15 @@ mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj
252252
o->state[n_state - 1 - n_args - i] = args2[i];
253253
}
254254

255+
// set rest of state to MP_OBJ_NULL
256+
for (uint i = 0; i < n_state - n_args - n_args2; i++) {
257+
o->state[i] = MP_OBJ_NULL;
258+
}
259+
255260
// bytecode prelude: initialise closed over variables
256261
for (uint n_local = *bytecode++; n_local > 0; n_local--) {
257262
uint local_num = *bytecode++;
258-
if (local_num < n_args + n_args2) {
259-
o->state[n_state - 1 - local_num] = mp_obj_new_cell(o->state[n_state - 1 - local_num]);
260-
} else {
261-
o->state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
262-
}
263+
o->state[n_state - 1 - local_num] = mp_obj_new_cell(o->state[n_state - 1 - local_num]);
263264
}
264265

265266
// set ip to start of actual byte code

py/vm.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args,
9595
state[n_state - 1 - n_args - i] = args2[i];
9696
}
9797

98+
// set rest of state to MP_OBJ_NULL
99+
for (uint i = 0; i < n_state - n_args - n_args2; i++) {
100+
state[i] = MP_OBJ_NULL;
101+
}
102+
98103
// bytecode prelude: initialise closed over variables
99104
for (uint n_local = *ip++; n_local > 0; n_local--) {
100105
uint local_num = *ip++;
101-
if (local_num < n_args + n_args2) {
102-
state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
103-
} else {
104-
state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
105-
}
106+
state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
106107
}
107108

108109
// execute the byte code

0 commit comments

Comments
 (0)