Title: Length of struct.pack('HL', 1,1) incorrect (8 instead of 6 bytes)
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.4, Python 2.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, dparolin
Priority: normal Keywords:

Created on 2008-08-01 00:17 by dparolin, last changed 2008-08-01 00:40 by amaury.forgeotdarc. This issue is now closed.

Messages (2)
msg70535 - (view) Author: Dominique Parolin (dparolin) Date: 2008-08-01 00:17
Python Versions tested:
Windows 2.5.2 (r252:60911)
Debian Linux Python 2.4.4 

>>> import struct
>>> struct.pack('HL', 1, 1)

results in '\x01\x00\x00\x00\x01\x00\x00\x00'
although '\x01\x00\x01\x00\x00\x00' was expected.

if concatenated or done separately
>>> struct.pack('H', 1) + struct.pack('L', 1) 

result is as expected '\x01\x00\x01\x00\x00\x00'
'\x01\x00' and '\x01\x00\x00\x00' respectively

Length is 8 where it should be 6
This is as well true for "hl", "hL" and "Hl".

Free description:
I could not find another error regarding that, nor any information using
popular search.
Further no explanation found why that might be valid behaviour.

msg70536 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-08-01 00:40
The struct module is correct.
You see here the result of "alignment": the address of a long is always
 a multiple of the size of a long.

The struct module mimics the C compiler: a "struct { short x; long y;
}", will actually occupy 8 bytes in memory (on a 32bit processor).
There are two padding bytes, that are not used.
Date User Action Args
2008-08-01 00:40:32amaury.forgeotdarcsetstatus: open -> closed
resolution: not a bug
messages: + msg70536
nosy: + amaury.forgeotdarc
2008-08-01 00:17:05dparolincreate