Note that there are some explanatory texts on larger screens.

plurals
  1. POPython structure always stuck at 0 no matter what value you assign to it?
    primarykey
    data
    text
    <p>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?</p> <p>Sorry, I forgot to mention I'm using python 3.1.2 from <a href="http://www.python.org/download/releases/3.1.2/" rel="nofollow">http://www.python.org/download/releases/3.1.2/</a></p> <pre><code>&gt;&gt;&gt; import ctypes &gt;&gt;&gt; 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)] ... &gt;&gt;&gt; x = Blah(0xDEAD,0xBEEF,0x44,0x12) &gt;&gt;&gt; print (hex(x.a) ) 0x0 &gt;&gt;&gt; print (hex(x.b )) 0xbeef &gt;&gt;&gt; print (hex(x.c )) 0x44 &gt;&gt;&gt; print (hex(x.d )) 0x12 &gt;&gt;&gt; &gt;&gt;&gt; g = Blah(0x2BAD,0xBEEF,0x55,0x12) &gt;&gt;&gt; print (hex(g.a )) 0x0 &gt;&gt;&gt; print (hex(g.b )) 0xbeef &gt;&gt;&gt; print (hex(g.c )) 0x55 &gt;&gt;&gt; print (hex(g.d )) 0x12 &gt;&gt;&gt; </code></pre> <p>swapping first two fields' position gives same result</p> <pre><code>&gt;&gt;&gt; import ctypes &gt;&gt;&gt; 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)] ... &gt;&gt;&gt; x = Blah(0xDEAD,0xBEEF,0x44,0x12) &gt;&gt;&gt; print (hex(x.a) ) 0xdead &gt;&gt;&gt; print (hex(x.b )) 0x0 &gt;&gt;&gt; print (hex(x.c )) 0x44 &gt;&gt;&gt; print (hex(x.d )) 0x12 &gt;&gt;&gt; &gt;&gt;&gt; g = Blah(0x2BAD,0xBEEF,0x55,0x12) &gt;&gt;&gt; print (hex(g.a )) 0x2bad &gt;&gt;&gt; print (hex(g.b )) 0x0 &gt;&gt;&gt; print (hex(g.c )) 0x55 &gt;&gt;&gt; print (hex(g.d )) 0x12 &gt;&gt;&gt; </code></pre> <p>varying field's size and I observe some weird cutoff of the input</p> <pre><code>&gt;&gt;&gt; import ctypes &gt;&gt;&gt; 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)] ... &gt;&gt;&gt; x = Blah(0xDEAD,0xBEEF,0x44,0x12) &gt;&gt;&gt; print (hex(x.a) ) 0xad &gt;&gt;&gt; print (hex(x.b )) 0xef &gt;&gt;&gt; print (hex(x.c )) 0x44 &gt;&gt;&gt; print (hex(x.d )) 0x12 &gt;&gt;&gt; &gt;&gt;&gt; g = Blah(0x2BAD,0xBEEF,0x55,0x12) &gt;&gt;&gt; print (hex(g.a )) 0xad &gt;&gt;&gt; print (hex(g.b )) 0xef &gt;&gt;&gt; print (hex(g.c )) 0x55 &gt;&gt;&gt; print (hex(g.d )) 0x12 &gt;&gt;&gt; </code></pre> <p>Does anyone know why is this happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload