Skip to content

Improve struct compatibility with CPython#1478

Merged
tannewt merged 3 commits into
adafruit:masterfrom
dhalbert:struct-compat
Jan 21, 2019
Merged

Improve struct compatibility with CPython#1478
tannewt merged 3 commits into
adafruit:masterfrom
dhalbert:struct-compat

Conversation

@dhalbert
Copy link
Copy Markdown
Collaborator

  • Add 'x' format character to struct format strings. This designates a padding byte that is 0 when packing and is skipped when unpacking.
  • Follow CPython's lead on checking buffer length. struct.unpack() requires the buffer size to match exactly the size required by the format string. struct.unpack_from() requires the buffer size to be at least the size required by the format string (the buffer could be longer).
  • struct.unpack_from(fmt, data, offset=0) now allows offset as a keyword arg. (fixes ustruct.unpack_from(fmt, data, offset=0) doesn't allow offset to be a keyword #984)

I wanted to also add range checking for the arguments for packing (#1451), but that's more difficult, due to how integers are packed internally. Anyway, this gets us closer to compatibility. We had issues with some drivers running on Linux systems with blinka because of the casual checking of struct up to now.

Note that we split off from MicroPython when we copied struct from modstruct.c to shared-bindings / shared-module. I made corresponding changes in modstruct.c but they aren't used in CircuitPython.

Related Micropython issues:
micropython#3736
micropython#1208

@dhalbert dhalbert requested a review from tannewt January 20, 2019 22:12
@ladyada
Copy link
Copy Markdown
Member

ladyada commented Jan 20, 2019

i like this idea - i could use it a lot in libraries! 'x' as well as strictness

@dhalbert
Copy link
Copy Markdown
Collaborator Author

i like this idea - i could use it a lot in libraries! 'x' as well as strictness

I was doing a lot of

_, foo, bar, _ = struct.unpack(...)

and got tired of that. It's in regular Python, so we may as well have it. Didn't take a lot to add.

@dhalbert
Copy link
Copy Markdown
Collaborator Author

dhalbert commented Jan 21, 2019

Travis is 😃 finally. I always forget to do a make translate after a merge.

Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you for more compatibility!

@tannewt tannewt merged commit b369fa9 into adafruit:master Jan 21, 2019
@dhalbert dhalbert deleted the struct-compat branch January 25, 2019 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ustruct.unpack_from(fmt, data, offset=0) doesn't allow offset to be a keyword

3 participants