-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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:
|
Thanks @mdickinson. If I understand correctly, |
More correctly, the programmer couldn't precisely control the format definition to match the network data layout. |
There's some control over padding for the non-native modes, via the
But I agree it's not ideal.
Wouldn't data streamed over the network be expected to follow a machine-agnostic format, in general? I'm not sure when one would expect it to match the layout for whatever the C compiler happens to be on your local machine. |
Thanks @mdickinson. Apologies for the late reply. Been away. The longer this thread continues, the more I'm beginning to believe there are two distinct use cases.
Have I got that about right? If I'm starting to think about this in the right way, I think the documentation should be explicit about these distinct use cases. |
On 11Nov2022 11:11, Skip Montanaro ***@***.***> wrote:
The longer this thread continues, the more I'm beginning to believe
there are two distinct use cases.
1. Map data into and out of the format dictated by the compiler used
to build Python. This is typified by the @ prefix and zero-repeat
format codes. Something like llh0l is meaningful in this context.
2. Map data into and out of specific formats needed for other uses
(like network communication). For this, @ and zero-repeat formats
are useless and you need to resort to more explicit format
characters (endianness, specific padding with x). Assuming my
compiler wants longs aligned on four bytes and I need to
send/receive big-endian data across the net, the above format
string would be replaced by >llhxx.
Have I got that about right? If I'm starting to think about this in the
right way, I think the documentation should be explicit about these
distinct use cases.
I'm in full agreement here.
|
I updated the PR and marked it ready for review, should anyone be interested. |
I can't speak for other |
…mples/warnings (GH-99141) * nail down a couple examples to have more predictable output * update a number of things, but this is really just a stash... * added an applications section to describe typical uses for native and machine-independent formats * make sure all format strings use a format prefix character * responding to comments from @gpshead. Not likely finished yet. * This got more involved than I expected... * respond to several PR comments * a lot of wordsmithing * try and be more consistent in use of ``x`` vs ``'x'`` * expand examples a bit * update the "see also" to be more up-to-date * original examples relied on import * so present all examples as if * reformat based on @gpshead comment (missed before) * responding to comments * missed this * one more suggested edit * wordsmithing
…le examples/warnings (pythonGH-99141) * nail down a couple examples to have more predictable output * update a number of things, but this is really just a stash... * added an applications section to describe typical uses for native and machine-independent formats * make sure all format strings use a format prefix character * responding to comments from @gpshead. Not likely finished yet. * This got more involved than I expected... * respond to several PR comments * a lot of wordsmithing * try and be more consistent in use of ``x`` vs ``'x'`` * expand examples a bit * update the "see also" to be more up-to-date * original examples relied on import * so present all examples as if * reformat based on @gpshead comment (missed before) * responding to comments * missed this * one more suggested edit * wordsmithing (cherry picked from commit 22d91c1) Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com>
…le examples/warnings (pythonGH-99141) * nail down a couple examples to have more predictable output * update a number of things, but this is really just a stash... * added an applications section to describe typical uses for native and machine-independent formats * make sure all format strings use a format prefix character * responding to comments from @gpshead. Not likely finished yet. * This got more involved than I expected... * respond to several PR comments * a lot of wordsmithing * try and be more consistent in use of ``x`` vs ``'x'`` * expand examples a bit * update the "see also" to be more up-to-date * original examples relied on import * so present all examples as if * reformat based on @gpshead comment (missed before) * responding to comments * missed this * one more suggested edit * wordsmithing (cherry picked from commit 22d91c1) Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com>
…ble examples/warnings (GH-99141) (GH-99702) gh-99146 struct module documentation should have more predictable examples/warnings (GH-99141) * nail down a couple examples to have more predictable output * update a number of things, but this is really just a stash... * added an applications section to describe typical uses for native and machine-independent formats * make sure all format strings use a format prefix character * responding to comments from @gpshead. Not likely finished yet. * This got more involved than I expected... * respond to several PR comments * a lot of wordsmithing * try and be more consistent in use of ``x`` vs ``'x'`` * expand examples a bit * update the "see also" to be more up-to-date * original examples relied on import * so present all examples as if * reformat based on @gpshead comment (missed before) * responding to comments * missed this * one more suggested edit * wordsmithing (cherry picked from commit 22d91c1) Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com> Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com>
…ble examples/warnings (GH-99141) (GH-99703) gh-99146 struct module documentation should have more predictable examples/warnings (GH-99141) * nail down a couple examples to have more predictable output * update a number of things, but this is really just a stash... * added an applications section to describe typical uses for native and machine-independent formats * make sure all format strings use a format prefix character * responding to comments from @gpshead. Not likely finished yet. * This got more involved than I expected... * respond to several PR comments * a lot of wordsmithing * try and be more consistent in use of ``x`` vs ``'x'`` * expand examples a bit * update the "see also" to be more up-to-date * original examples relied on import * so present all examples as if * reformat based on @gpshead comment (missed before) * responding to comments * missed this * one more suggested edit * wordsmithing (cherry picked from commit 22d91c1) Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com> Co-authored-by: Skip Montanaro <skip.montanaro@gmail.com>
Thanks, looks like the docs PRs have been merged and backported! |
Documentation
The documentation for the
struct
module 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: