diff --git a/py/modstruct.c b/py/modstruct.c index 1daa333388..0d4a45f6b6 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -206,7 +206,8 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, byte* end_p, siz memset(p + to_copy, 0, sz - to_copy); p += sz; } else { - while (sz--) { + // If we run out of args then we just finish; CPython would raise struct.error + while (sz-- && i < n_args) { mp_binary_set_val(fmt_type, *fmt, args[i++], &p); } } diff --git a/tests/basics/struct_micropython.py b/tests/basics/struct_micropython.py index 4b9dfe1378..f203a4666f 100644 --- a/tests/basics/struct_micropython.py +++ b/tests/basics/struct_micropython.py @@ -18,6 +18,9 @@ s = struct.pack("