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).
This commit is contained in:
parent
b349479a49
commit
793d826d9d
|
@ -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);
|
memset(p + to_copy, 0, sz - to_copy);
|
||||||
p += sz;
|
p += sz;
|
||||||
} else {
|
} 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);
|
mp_binary_set_val(fmt_type, *fmt, args[i++], &p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ s = struct.pack("<O", o)
|
||||||
o2 = struct.unpack("<O", s)
|
o2 = struct.unpack("<O", s)
|
||||||
print(o is o2[0])
|
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
|
# pack and unpack pointer to a string
|
||||||
# This requires uctypes to get the address of the string and instead of
|
# 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
|
# putting this in a dedicated test that can be skipped we simply pass
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
True
|
True
|
||||||
|
b'\x01\x00\x00\x00\x00\x00\x00\x00'
|
||||||
|
|
Loading…
Reference in New Issue