This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incompatible size for long (int) in Python Win and Linux?
Type: Stage:
Components: None Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: hyeshik.chang, tim.peters, yanowitz
Priority: normal Keywords:

Created on 2006-03-21 13:08 by yanowitz, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg27836 - (view) Author: Michael Yanowitz (yanowitz) Date: 2006-03-21 13:08
   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

msg27837 - (view) Author: Hyeshik Chang (hyeshik.chang) * (Python committer) Date: 2006-03-21 13:30
Logged In: YES 
user_id=55188

In C, the size of long depends on platform ABI.  If you are
on a 64bit platform, long may be 8 bytes while you got 4
bytes in 32bit platforms.  It's not Python's problem.
msg27838 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-03-21 18:07
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.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43067
2006-03-21 13:08:45yanowitzcreate