Bug in struct module?

Andrew Csillag andrew at starmedia.net
Tue Aug 17 14:01:52 EDT 1999

Hoon Yoon wrote:
> Hi,
> >>> calcsize('hd')
> 16
> >>> calcsize('dh')
> 10
> That seems like a bug to me.

You forgot about structure alignment.  If the machine is 1 byte-aligned,
the sizes would be the same.  From the looks of things, your machine
aligns on 8 byte boundaries (Sun machine?).

> BTW: it won't convert
> >>> unpack('dh', s[-10:])
> (2.47032822921e-323, 16424)
> >>> unpack('hd', s[-10:])
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> struct.error: unpack str size does not match format

It shouldn't.  Since the above sizes are what they are, you'd need a
string of 16 bytes for the second call to unpack.

If you want these to work, try the network byte order/alignment option
(prefix your format string with a '>').  On my sun box, both sizes
become 10 and the unpack should also work.
"Programmers are just machines that convert coffee to executable code"

"Drew Csillag" <drew_csillag at geocities.com>

More information about the Python-list mailing list