[Python-bugs-list] [ python-Bugs-404328 ] struct.calcsize returns wrong size
nobody
nobody@sourceforge.net
Wed, 28 Feb 2001 11:51:48 -0800
Bugs #404328, was updated on 2001-02-26 08:21
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=404328&group_id=5470
Category: Python Library
Group: Not a Bug
Status: Closed
Priority: 5
Submitted By: Nobody/Anonymous
Assigned to: Nobody/Anonymous
Summary: struct.calcsize returns wrong size
Initial Comment:
struct.calcsize returns the wrong size because it takes
into account the alignment of the type.
I needed to read a 56 packed structure and then write
it back.
Python insists that it needs 62 bytes for the fmt
'chl6s20schdhdh'
Using Python-2.1a2 source code I can get the correct
answer by
commenting out in Modules/structmodule.c
if (e == NULL)
return -1;
itemsize = e->size;
--> //size = align(size, c, e);
x = num * itemsize;
size += x;
Basically packing in a string doesn't require any
alignment. The alignment is neccesary once unpacking
takes place. However if the unpacking is done to a
local c variable then the alignment should not be a
problem e.g.
long Unpack_long(char * offset)
{
long value;
memcpy(&value,offset,sizeof(long);
return value;
}
Regards Rene de Zwart
----------------------------------------------------------------------
Comment By: Tim Peters
Date: 2001-02-28 11:51
Message:
Logged In: YES
user_id=31435
If he doesn't want native alignment, there are 4 format
codes documented that use "standard" (i.e., no) alignment
instead.
----------------------------------------------------------------------
Comment By: Fred L. Drake, Jr.
Date: 2001-02-28 11:41
Message:
Logged In: YES
user_id=3066
This is documented behavior of the struct module, not a bug. The purpose of the module is to allow packing and unpacking of C structures; I don't know of any that exhibits the alignment behavior that you describe -- C "short" values are aligned to sizeof(short) boundaries.
If your C compiler is exhibiting different behavior, look to see if you're using compiler options that affect the packing of structures -- this is not "normal" behavior.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=404328&group_id=5470