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:26:37 EST 2019


On 2019-12-02 09:55:16 -0800, Rob Gaddi wrote:
> The struct situation is, as you said, a bit different.  I believe that with
> the default native alignment @, you're seeing 4-byte data padded to an
> 8-byte alignment, not 8-byte data.

Nope. That's really an 8 byte long:

    Python 3.7.3 (default, Apr  3 2019, 05:39:12)
    [GCC 8.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import struct

    >>> struct.pack('L', 0x0807060504030201)
    b'\x01\x02\x03\x04\x05\x06\x07\x08'

All 8 bytes are serialized. Trying to serialize 9 bytes (actually only 68
non-zero bits) fails:

    >>> struct.pack('L', 0x090807060504030201)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    struct.error: argument out of range

And when you use the "standard size", 8 bytes are also too much:

    >>> struct.pack('=L', 0x0807060504030201)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    struct.error: 'L' format requires 0 <= number <= 4294967295

But 4 bytes are ok:
    >>> struct.pack('=L', 0x04030201)
    b'\x01\x02\x03\x04'


        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/366e0e41/attachment.sig>


More information about the Python-list mailing list