Skip to content

Commit 793d826

Browse files
committed
py/modstruct: In struct.pack, stop converting if there are no args left.
This patch makes a repeat counter behave the same as repeating the typecode, when there are not enough args. For example: struct.pack('2I', 1) now behave the same as struct.pack('II', 1).
1 parent b349479 commit 793d826

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

py/modstruct.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, byte* end_p, siz
206206
memset(p + to_copy, 0, sz - to_copy);
207207
p += sz;
208208
} else {
209-
while (sz--) {
209+
// If we run out of args then we just finish; CPython would raise struct.error
210+
while (sz-- && i < n_args) {
210211
mp_binary_set_val(fmt_type, *fmt, args[i++], &p);
211212
}
212213
}

tests/basics/struct_micropython.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class A():
1818
o2 = struct.unpack("<O", s)
1919
print(o is o2[0])
2020

21+
# pack can accept less arguments than required for the format spec
22+
print(struct.pack('<2I', 1))
23+
2124
# pack and unpack pointer to a string
2225
# This requires uctypes to get the address of the string and instead of
2326
# putting this in a dedicated test that can be skipped we simply pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
True
2+
b'\x01\x00\x00\x00\x00\x00\x00\x00'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy