Python structure always stuck at 0 no matter what value you assign to it? -
i writing module compact bits passed c program, keep getting errors. after tests, found out field of class blah stuck @ 0 no matter what. know if bug or if i'm doing wrong here?
sorry, forgot mention i'm using python 3.1.2 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 2 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 , observe weird cutoff of 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 know why happening?
you omit 3rd field 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 rest bug.
Comments
Post a Comment