[Python-Dev] struct module docs vs reality

Anders J. Munch ajm at flonidan.dk
Thu Jan 24 09:10:31 CET 2008


Gregory P. Smith wrote:
> 
> The documentation for the struct module says:
> 
>  http://docs.python.org/dev/library/struct.html#module-struct
> 
>  " short is 2 bytes; int and long are 4 bytes; long long ( __int64 on Windows) is 8 bytes"
> 
> and lists 'l' and 'L' as the pack code for a C long.
> 
> As its implemented today, the documentation is incorrect.  On an LP64
> host (pretty much any 64-bit linux, bsd or unixish thing) a long is 8
> bytes.

You overlooked the words "Standard size and alignment are as follows"
that start the quoted paragraph.  It's a little confusing because
standard size is not the default.  The default is platform-specific
sizes.  Only if you start the format string with >, <, ! or = do you
get standard sizes.

The reference documentation is correct as it stands, and, I suspect,
so is the LP64 implementation.  Doesn't struct.pack('>l',42) produce a
4-byte string on LP64?

The tutorial at
http://docs.python.org/tut/node13.html#SECTION0013300000000000000000%3E
has a bug though: the format string should use '<'.

I believe zipfile.py correctly uses '<' throughout.

regards, Anders


More information about the Python-Dev mailing list