New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
struct module documentation should have more predictable examples/warnings #99146
Comments
|
+1 for the changes. The original text here, and the choice to use big-endian for the examples, dates from over 28 years ago. These days I suspect that it's rather rare that the endianness of the machine you currently happen to be working on is relevant to the data manipulation task at hand. As such, I'd consider it a best practice to always use a struct "sigil" as the first character in your format string, and if we follow that best practice in the docs then it'll help it propagate to struct users. |
|
Thanks @mdickinson. That suggests that I should do a bit more tweaking of both the text and the examples in my PR. |
|
I think some consensus seems to have been reached in this thread. In particular, most examples should be explicit in their layout definitions. @cameron-simpson's suggestion sums things up nicely:
|
|
As I'm working through some examples (and trying to update the documentation text), I find myself confused by some of the behavior. Consider these four similar I would have expected the If I don't understand what's going on here, anything I write will be gibberish... (Edit: update my expectation to be more forceful) |
|
I switched to my Raspberry Pi, which is a little endian 32-bit ARM machine. I get confusing (to me) results there as well. If my reading of the documentation is correct:
the Again, I see nothing to suggest the growing byte string was padded out to a four-byte boundary before the trailing In general, the |
|
So my understanding of what's going on is that for non-native packing and unpacking (e.g., your examples with E.g., we get a simple non-padded 9-byte output from the following (on any machine, in theory, but here on macOS / 64-bit Intel) >>> import struct
>>> struct.pack('>lbl', 1, 2, 3)
b'\x00\x00\x00\x01\x02\x00\x00\x00\x03'For the native examples, it looks to me as though you're getting the padding that you'd expect. |
|
Possibly we could reword note 3 to emphasize the contrast with the "non-native size and alignment" comment in note 2? Something like: |
smontanaro commentedNov 5, 2022
•
edited by bedevere-bot
Documentation
The documentation for the
structmodule isn't explicit about what's expected of the various examples. Working through that in a PR...The text was updated successfully, but these errors were encountered: