-
-
Notifications
You must be signed in to change notification settings - Fork 29.5k
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.pack(): trailing padding bytes on x64 #70933
Comments
|
On the x64 architecture gcc adds trailing padding bytes after the last >>> import numpy as np
>>>
>>> t = np.dtype([('x', 'u1'), ('y', 'u8'), ('z', 'u1')], align=True)
>>> x = np.array([(1, 2, 3)], dtype=t)
>>> x.tostring()
b'\x01\xf7\xba\xab\x03\x7f\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'The struct module in native mode does not: >>> struct.pack("BQB", 1, 2, 3)
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03'I'm not sure if this is intended -- or if full compatibility to |
|
This behaviour seems to be documented, although it is not very explicit, and a bit surprising to me. See the third note at the end of <https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment\>: “align the end . . . with a repeat count of zero”, and the example >>> pack('llh0l', 1, 2, 3)
b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00' |
|
Thank you. So technically, in the above NumPy example the format >>> m = memoryview(x)
>>> m.format
'T{B:x:xxxxxxxL:y:B:z:}'I find this "0L" thing a very odd notation. Taking care of this |
|
I'm not to crazy about the trailing padding syntax either. The behavior is documented all the way back to Python 2.6. So, I would be hesitant to change it now. If the new 'T{...}' struct syntax from bpo-3132 ever gets added, then maybe we could address this there? FWIW, internal and trailing padding is implementation defined by the C standard. That being said, most compilers I have worked with add the trailing padding. |
|
Hello, Over at numpy I have a proposed fix for the bug you discovered, that numpy drops trailing padding in the 3118 format string. My strategy is going to make numpy interpret format strings exactly the same way as the struct module, let me know if you disagree. See numpy/numpy#7798 |
|
I have just worked on PEP-3118 ==> Datashape translation and I have It seems to me that we should simplify the PEP-3118 struct syntax as much I think generally that numpy's approach is the best for data interchange, so I
Here is my implementation with a grammar: https://github.com/plures/ndtypes/blob/master/libndtypes/compat/bpgrammar.y Some tests against numpy: https://github.com/plures/xnd/blob/master/python/test_xnd.py#L1509 I think the best way forward would be to tweak the above grammar so that |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: