diff options
-rw-r--r-- | py/modstruct.c | 3 | ||||
-rw-r--r-- | tests/basics/struct_micropython.py | 3 | ||||
-rw-r--r-- | tests/basics/struct_micropython.py.exp | 1 |
3 files changed, 6 insertions, 1 deletions
diff --git a/py/modstruct.c b/py/modstruct.c index 1daa33338..0d4a45f6b 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 4b9dfe137..f203a4666 100644 --- a/tests/basics/struct_micropython.py +++ b/tests/basics/struct_micropython.py @@ -18,6 +18,9 @@ s = struct.pack("<O", o) o2 = struct.unpack("<O", s) print(o is o2[0]) +# pack can accept less arguments than required for the format spec +print(struct.pack('<2I', 1)) + # pack and unpack pointer to a string # This requires uctypes to get the address of the string and instead of # putting this in a dedicated test that can be skipped we simply pass diff --git a/tests/basics/struct_micropython.py.exp b/tests/basics/struct_micropython.py.exp index 0ca95142b..55b7b6623 100644 --- a/tests/basics/struct_micropython.py.exp +++ b/tests/basics/struct_micropython.py.exp @@ -1 +1,2 @@ True +b'\x01\x00\x00\x00\x00\x00\x00\x00' |