Skip to content

Commit 932bf1c

Browse files
committed
py: Fix VM/runtime unpack sequence bug, Issue adafruit#193.
1 parent 8fce5b4 commit 932bf1c

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

py/runtime.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
761761
return set;
762762
}
763763

764-
// unpacked items are stored in order into the array pointed to by items
764+
// unpacked items are stored in reverse order into the array pointed to by items
765765
void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
766766
if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
767767
uint seq_len;
@@ -776,7 +776,9 @@ void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
776776
} else if (seq_len > num) {
777777
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num));
778778
}
779-
memcpy(items, seq_items, num * sizeof(mp_obj_t));
779+
for (uint i = 0; i < num; i++) {
780+
items[i] = seq_items[num - 1 - i];
781+
}
780782
} else {
781783
// TODO call rt_getiter and extract via rt_iternext
782784
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in)));

py/vm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_ob
450450

451451
case MP_BC_UNPACK_SEQUENCE:
452452
DECODE_UINT;
453-
rt_unpack_sequence(sp[0], unum, sp + unum - 1);
453+
rt_unpack_sequence(sp[0], unum, sp);
454454
sp += unum - 1;
455455
break;
456456

0 commit comments

Comments
 (0)