array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

Peter J. Holzer hjp-python at hjp.at
Sat Dec 7 06:21:16 EST 2019


On 2019-12-05 09:27:43 +0000, Barry Scott wrote:
> On 3 Dec 2019, at 01:50, Richard Damon <Richard at Damon-Family.org> wrote:
> > On 12/2/19 4:25 PM, Barry Scott wrote:
> >>>>> x=struct.pack('L',0x102030405)
> >>>>> x
> >> b'\x05\x04\x03\x02\x01\x00\x00\x00'
> >> 
> >> Given I have exact control with b, h, i, and q but L is not fixed
> >> in size I'm not sure how it can be used with certainty across OS
> >> and versions.
> >> 
> >> Barry
> >> 
> > Actually, you DON'T have exact control with those sizes, it just happens
> > that all the platforms you are using happen to have the same size for
> > those types.
> 
> According to the docs for struct (python 2.7 and python 3.8) I do have
> exact control for the types I listed.

You also have exact control over the size of the 'L' type.

But only if you use the prefixes with a standard size (see the table in
https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment),
not with "@" (which is the default). 

This is also true for the other sizes. A platform where short is 32 bits
or int is 64 bits might exist (in fact I've used the former (not with
Python, though), and while I've never used the latter, I've certainly
heard of one machine where that was the case (the Cray was quite famous
in its time)).

When you are reading and writing files which "can be used with certainty
across OS and versions" you need to use one of the prefixes "<", ">" or
"!" anyway because you also want to control the byte order, not just the
size (although big-endian architectures may be history in a few years).

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20191207/0a278c23/attachment.sig>


More information about the Python-list mailing list