Rob Gaddi added the comment:

I was just working on similar things, and found the same problem.  I can 
confirm failure on both Python 2.7.4 and Python 3.3.1 running on 64-bit Linux, 
and that the Windows builds do not have this problem.

My code:

from __future__ import print_function
from ctypes import *
from itertools import product

bases = (BigEndianStructure, LittleEndianStructure)
packs = (True, False)
basetypes = ( (c_uint,16), (c_ushort,16), (c_uint,32) )

print("Base                     Basetype  pack  high  low   size  bytes")
for basetype, base, pack in product(basetypes, bases, packs):
    fields = [
        ('high', basetype[0], basetype[1]),
        ('low', basetype[0], basetype[1]),
    ]
    cls = type('', (base,), {'_pack_' : pack, '_fields_' : fields})
    
    x = cls(high = 0x1234, low = 0x5678)
    
    bacls = c_uint8 * sizeof(x)
    ba = bacls.from_buffer(x)
    s = ''.join('{0:02X}'.format(b) for b in ba)
    
    k = '*' if (x.high != 0x1234 or x.low != 0x5678) else ''
    
    report = "{name:25s}{basetype:10s}{pack:4d}  {high:04X}  {low:04X}  
{size:4d}  {s}{k}".format(
        name = base.__name__,
        high = x.high,
        low = x.low,
        size = sizeof(x),
        pack = pack,
        basetype = basetype[0].__name__,
        s = s,
        k = k
    )
    print(report)
        
My results:
Base                     Basetype  pack  high  low   size  bytes
BigEndianStructure       c_uint       1  0000  5678     4  00005678*
BigEndianStructure       c_uint       0  0000  5678     4  00005678*
Structure                c_uint       1  1234  5678     4  34127856
Structure                c_uint       0  1234  5678     4  34127856
BigEndianStructure       c_ushort     1  1234  5678     4  12345678
BigEndianStructure       c_ushort     0  1234  5678     4  12345678
Structure                c_ushort     1  1234  5678     4  34127856
Structure                c_ushort     0  1234  5678     4  34127856
BigEndianStructure       c_uint       1  1234  5678     8  0000123400005678
BigEndianStructure       c_uint       0  1234  5678     8  0000123400005678
Structure                c_uint       1  1234  5678     8  3412000078560000
Structure                c_uint       0  1234  5678     8  3412000078560000

On python3, the BigEndianStructure seemingly at random will set the high or low 
fields from one execution to the next, but always misses one or the other.  I 
have always seen high = 0, low = 0x5678 on python2.

----------
nosy: +rgaddi
versions: +Python 3.3

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue20629>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to