diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-01-20 15:10:09 -0500 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-01-20 15:12:34 -0500 |
| commit | 7a09af73ec7f4a11a1404dff6276e09072d06983 (patch) | |
| tree | 0cc43310b88a0dae09464aa9ad7a9e2c2bcf75a9 /py/modstruct.c | |
| parent | d2a7cd6c5aac4ef01b4503409a4bf47d4cdf31c3 (diff) | |
Improve struct compatibility with CPython
Diffstat (limited to 'py/modstruct.c')
| -rw-r--r-- | py/modstruct.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/py/modstruct.c b/py/modstruct.c index 3f1b2f8b8..a238d3935 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -97,7 +97,10 @@ STATIC size_t calc_size_items(const char *fmt, size_t *total_sz) { total_cnt += 1; size += cnt; } else { - total_cnt += cnt; + // Pad bytes are skipped and don't get included in the item count. + if (*fmt != 'x') { + total_cnt += cnt; + } mp_uint_t align; size_t sz = mp_binary_get_size(fmt_type, *fmt, &align); while (cnt--) { @@ -166,7 +169,10 @@ STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) { } else { while (cnt--) { item = mp_binary_get_val(fmt_type, *fmt, &p); - res->items[i++] = item; + // Pad bytes ('x') are just skipped. + if (*fmt != 'x') { + res->items[i++] = item; + } } } fmt++; @@ -204,7 +210,11 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c } else { // If we run out of args then we just finish; CPython would raise struct.error while (cnt-- && i < n_args) { - mp_binary_set_val(fmt_type, *fmt, args[i++], &p); + mp_binary_set_val(fmt_type, *fmt, args[i], &p); + // Pad bytes don't have a corresponding argument. + if (*fmt != 'x') { + i++; + } } } fmt++; |
