This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: struct.pack() behaves strangely for 'L' on 64bit Linux
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: rz, skrah
Priority: normal Keywords:

Created on 2013-06-08 19:22 by rz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg190817 - (view) Author: rz (rz) Date: 2013-06-08 19:22
Reproduction:

Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.pack('!L', 0x01020304)
'\x01\x02\x03\x04'
>>> struct.pack('>L', 0x01020304)
'\x01\x02\x03\x04'
>>> struct.pack('<L', 0x01020304)
'\x04\x03\x02\x01'
>>> struct.pack('L', 0x01020304)
'\x04\x03\x02\x01\x00\x00\x00\x00' ### WAT??? ###
>>> 

As far as I see at the source code (http://hg.python.org/releasing/2.7.4/file/9290822f2280/Modules/_struct.c#l703), sizeof(long) is used as the size of 'L', which is equal to 8 at 64bit Linux...

The problem is that the results of packing with 'L' returns 8 bytes,
instead of 4 - as was expected from the documentation...
msg190824 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-06-08 20:30
The docs say:

"Native size and alignment are determined using the C compiler’s sizeof expression. This is always combined with native byte order."


sizeof(long) is 8 on your platform, so I don't see anything wrong here.
Or is another part of the documentation unclear?
msg190846 - (view) Author: rz (rz) Date: 2013-06-09 06:16
You are correct - the documentation is right: 
"Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent."

So indeed, there is no problem - just my own misinterpretation of the docs...
History
Date User Action Args
2022-04-11 14:57:46adminsetgithub: 62369
2013-06-09 06:17:28rzsetstatus: open -> closed
resolution: works for me
2013-06-09 06:16:59rzsetstatus: pending -> open
type: behavior
messages: + msg190846
2013-06-08 20:30:48skrahsetstatus: open -> pending

nosy: + skrah
messages: + msg190824

type: behavior -> (no value)
2013-06-08 19:22:47rzcreate