Issue 1455357: Incompatible size for long (int) in Python Win and Linux? (original) (raw)

There appears to be a problem with packing/unpacking long (and int) values when sending data from Windows to Linux. I am not sure if it is a bug or just due to incompatibleness. Normally we think of long as being 4 bytes. However, on Linux, it might be 8 bytes (in Python).

Here are good results in Windows, packing and unpacking a long: $ python Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import struct p = struct.pack("L", 12345) p '90\x00\x00' u = struct.unpack("L",p) u (12345L,)

However, on Linux, I get slightly different results:

Python 2.4.1 (#1, May 16 2005, 15:15:14) [GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information>>> import struct

p = struct.pack("L", 12345) p '90\x00\x00\x00\x00\x00\x00' u = struct.unpack("L",p) u (12345L,)

This is annoying, because I want to be able to send data from Windows to Linux (and vice-versa) over a socket. I am new to Python, and the only way I know of of doing that is by packing and unpacking the data.

Please email me m.yanowitz@kearfott.com if this isn't a bug or there is a known work-around for this problem.

Thanks in advance: Michael Yanowitz

Logged In: YES user_id=31435

I agree with closing this report, but we should point out that Python does solve most of this problem: don't use "native mode" pack/unpack, use "standard mode" pack/unpack, to transport values across architectures. Read the struct docs for details. For example, struct.pack("!L", 12345) uses 4-byte big-endian ("network") format regardless of the platform it runs on.