views:

76

answers:

1

I was writing a module to compact bits to be passed to C program, but keep getting errors. After some tests, I found out that the field a of class Blah is stuck at 0 no matter what. Does anyone know if this is a bug or if I'm doing something wrong here?

Sorry, I forgot to mention I'm using python 3.1.2 from http://www.python.org/download/releases/3.1.2/

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint64, 64),
...                 ("b", ctypes.c_uint16, 16),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0x0
>>> print (hex(x.b ))
0xbeef
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0x0
>>> print (hex(g.b ))
0xbeef
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

swapping first two fields' position gives same result

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint16, 16),
...                 ("b", ctypes.c_uint64, 64),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0xdead
>>> print (hex(x.b ))
0x0
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0x2bad
>>> print (hex(g.b ))
0x0
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

varying field's size and I observe some weird cutoff of the input

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint64, 40),
...                 ("b", ctypes.c_uint64, 40),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0xad
>>> print (hex(x.b ))
0xef
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0xad
>>> print (hex(g.b ))
0xef
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

Does anyone know why is this happening?

+1  A: 

You could omit the 3rd field as a workaround.

>>> import ctypes
>>> class Blah(ctypes.Structure):
...   _fields_ = [("a", ctypes.c_uint64), ('b', ctypes.c_uint16), ('c', ctypes.c_uint8), ('d', ctypes.c_uint8)]
... 
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> hex(x.a)
'0xdead'
>>> hex(x.b)
'0xbeef'

I guess the rest is a bug.

KennyTM
Sorry, I forgot to mention I'm using python 3.1.2 from http://www.python.org/download/releases/3.1.2/which version of Python are you using?
SUCM
@SUCM: 3.1.2, but on a 64-bit platform.
KennyTM
for anyone who is still struggling: if you put anything before a 64 bit field in your _fields_, all of those will stay at zero even if you omit the size for the 64 bit field, but if you omit size for all fields, then you can use them normally. this is the case for python 3.1.2 official build on Windows x86
SUCM