[Python-bugs-list] [ python-Bugs-404327 ] struct.calcsize returns wrong size
nobody
nobody@sourceforge.net
Mon, 26 Feb 2001 08:24:24 -0800
Artifact #404327, was updated on 2001-02-26 08:20
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=404327&group_id=5470
Category: Python Library
Group: None
Status: Open
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: Nobody/Anonymous
Date: 2001-02-26 08:24
Message:
Logged In: NO
Oops forgot a test program
#!/usr/bin/python
import struct
print " 1 c ",struct.calcsize('c')
print " 3 ch ",struct.calcsize('ch')
print " 7 chl ",struct.calcsize('chl')
print "13 chl6s ",struct.calcsize('chl6s')
print "33 chl6s20s ",struct.calcsize('chl6s20s')
print "34 chl6s20sc ",struct.calcsize('chl6s20sc')
print "36 chl6s20sch ",struct.calcsize('chl6s20sch')
print "44 chl6s20schd ",struct.calcsize('chl6s20schd')
print "46 chl6s20schdh ",struct.calcsize('chl6s20schdh')
print "54 chl6s20schdhd ",struct.calcsize('chl6s20schdhd')
print "56 chl6s20schdhdh ",struct.calcsize('chl6s20schdhdh')
Journaal = "D"
Periode = 12
Datum = 991231
Kode = "123456"
Titel = "12345678901234567890"
Klasse = "H"
HoofdRek = 1300
HoofdBedrag = 100.97
TegenRek = 1400
TegenBedrag =-100.97
SubRek = 2200
data = struct.pack('chl6s20schdhdh',Journaal,Periode,Datum,\
Kode,Titel,Klasse,HoofdRek,HoofdBedrag,TegenRek,TegenBedrag,SubRek)
print
len(data),Journaal,Periode,Datum,Kode,Titel,Klasse,HoofdRek,\
HoofdBedrag,TegenRek,TegenBedrag,SubRek
Journaal, = struct.unpack('c' ,data[ 0: 1])
Periode, = struct.unpack('h' ,data[ 1: 3])
Datum, = struct.unpack('l' ,data[ 3: 7])
Kode, = struct.unpack('6s' ,data[ 7:13])
Titel, = struct.unpack('20s',data[13:33])
Klasse, = struct.unpack('c' ,data[33:34])
HoofdRek, = struct.unpack('h' ,data[34:36])
HoofdBedrag,= struct.unpack('d' ,data[36:44])
TegenRek, = struct.unpack('h' ,data[44:46])
TegenBedrag,= struct.unpack('d' ,data[46:54])
SubRek, = struct.unpack('h' ,data[54:56])
Journaal,Periode,Datum,Kode,Titel,Klasse,HoofdRek,HoofdBedrag,TegenRek,TegenBedrag,SubRek,
= struct.unpack('chl6s20schdhdh',data)
print
len(data),Journaal,Periode,Datum,Kode,Titel,Klasse,HoofdRek,HoofdBedrag,TegenRek,TegenBedrag,SubRek
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=404327&group_id=5470