struct.pack inconsistencies between platforms
Hi I have found myself in the following troubling situation. I'm running the following code on a Python 2.6.5 on Linux x86: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import struct len(struct.pack('L',0)) 4
Works as expected and documented (http://docs.python.org/library/struct.html ). I'm running the same code on a MacPro (OS X 10.7.3) and I'm getting the following: Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information.
import struct len(struct.pack('L',0)) 8
Documentation clearly states that the 'L' is a 4 byte integer. Is this a bug? I'm I missing something? Thanks PMon
On Sun, Feb 26, 2012 at 12:33, pmon mail <pmon.mail@gmail.com> wrote:
Hi
I have found myself in the following troubling situation.
I'm running the following code on a Python 2.6.5 on Linux x86: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import struct len(struct.pack('L',0)) 4
Works as expected and documented ( http://docs.python.org/library/struct.html).
I'm running the same code on a MacPro (OS X 10.7.3) and I'm getting the following: Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information.
import struct len(struct.pack('L',0)) 8
Documentation clearly states that the 'L' is a 4 byte integer.
Is this a bug? I'm I missing something?
By default pack uses native size, not standard size. On a 64-bit machine:
struct.pack('=L', 0) '\x00\x00\x00\x00' struct.pack('L', 0) '\x00\x00\x00\x00\x00\x00\x00\x00'
On 26 February 2012 12:34, Eli Bendersky <eliben@gmail.com> wrote:
On Sun, Feb 26, 2012 at 12:33, pmon mail <pmon.mail@gmail.com> wrote:
Documentation clearly states that the 'L' is a 4 byte integer.
Is this a bug? I'm I missing something?
By default pack uses native size, not standard size. On a 64-bit machine:
As the OP points out, the documentation says that the "Standard Size" is 4 bytes (http://docs.python.org/library/struct.html). While "Standard Size" doesn't appear to be defined in the documentation, and the start of the previous section (7.3.2.1. Byte Order, Size, and Alignment) clearly states that C types are represented in native format by default, the documentation could probably do with some clarification. Paul.
On Sun, Feb 26, 2012 at 15:09, Paul Moore <p.f.moore@gmail.com> wrote:
On 26 February 2012 12:34, Eli Bendersky <eliben@gmail.com> wrote:
On Sun, Feb 26, 2012 at 12:33, pmon mail <pmon.mail@gmail.com> wrote:
Documentation clearly states that the 'L' is a 4 byte integer.
Is this a bug? I'm I missing something?
By default pack uses native size, not standard size. On a 64-bit machine:
As the OP points out, the documentation says that the "Standard Size" is 4 bytes (http://docs.python.org/library/struct.html). While "Standard Size" doesn't appear to be defined in the documentation, and the start of the previous section (7.3.2.1. Byte Order, Size, and Alignment) clearly states that C types are represented in native format by default, the documentation could probably do with some clarification.
7.2.3.1 says, shortly after the first table: " Native size and alignment are determined using the C compiler’s sizeofexpression. This is always combined with native byte order. Standard size depends only on the format character; see the table in the *Format Characters* <http://docs.python.org/library/struct.html#format-characters>section. " To me this appears to be a reasonable definition of what "standard size" is. 7.3.2.2 says before the size table: "Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent." Again, taken together with the previous quote, IMHO this defines the difference between standard and native sizes clearly. If you feel differently, feel free to open an issue suggesting a better explanation. Eli
Sounds reasonable for me. Thanks! On Sun, Feb 26, 2012 at 3:16 PM, Eli Bendersky <eliben@gmail.com> wrote:
On Sun, Feb 26, 2012 at 15:09, Paul Moore <p.f.moore@gmail.com> wrote:
On 26 February 2012 12:34, Eli Bendersky <eliben@gmail.com> wrote:
On Sun, Feb 26, 2012 at 12:33, pmon mail <pmon.mail@gmail.com> wrote:
Documentation clearly states that the 'L' is a 4 byte integer.
Is this a bug? I'm I missing something?
By default pack uses native size, not standard size. On a 64-bit machine:
As the OP points out, the documentation says that the "Standard Size" is 4 bytes (http://docs.python.org/library/struct.html). While "Standard Size" doesn't appear to be defined in the documentation, and the start of the previous section (7.3.2.1. Byte Order, Size, and Alignment) clearly states that C types are represented in native format by default, the documentation could probably do with some clarification.
7.2.3.1 says, shortly after the first table:
"
Native size and alignment are determined using the C compiler’s sizeofexpression. This is always combined with native byte order.
Standard size depends only on the format character; see the table in the *Format Characters* <http://docs.python.org/library/struct.html#format-characters>section. "
To me this appears to be a reasonable definition of what "standard size" is.
7.3.2.2 says before the size table:
"Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent."
Again, taken together with the previous quote, IMHO this defines the difference between standard and native sizes clearly. If you feel differently, feel free to open an issue suggesting a better explanation.
Eli
On Sun, Feb 26, 2012 at 3:16 PM, Eli Bendersky <eliben@gmail.com> wrote:
7.2.3.1 says, shortly after the first table:
"
Native size and alignment are determined using the C compiler’s sizeof expression. This is always combined with native byte order.
Standard size depends only on the format character; see the table in the Format Characters section.
"
To me this appears to be a reasonable definition of what "standard size" is.
You're right, my apologies. I skimmed a little too much. Actually, I *could* argue that there is still some ambiguity, but as (a) the OP is happy, and (b) until I went and read the docs, I would have expected the current behaviour anyway, that would just just be me being awkward. So I won't :-) Paul.
participants (3)
-
Eli Bendersky -
Paul Moore -
pmon mail