[issue4376] Nested ctypes 'BigEndianStructure' fails

STINNER Victor report at bugs.python.org
Wed Jul 13 18:05:38 CEST 2011


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

I don't like your test because it depends on system endian:

+            if sys.byteorder == "little":
+                struct.menu.spam = 0x000000FF
+            else:
+                struct.menu.spam = 0xFF000000

I would prefer a test creating a structure from a byte string. Something like:
---------------------------
import ctypes

class Nested(ctypes.BigEndianStructure):
    _fields_ = (
        ('x', ctypes.c_uint32),
        ('y', ctypes.c_uint32),
    )

class TestStruct(ctypes.BigEndianStructure):
    _fields_ = (
        ('point', Nested),
    )

data = b'\0\0\0\1\0\0\0\2'
assert len(data) == ctypes.sizeof(TestStruct)
obj = ctypes.cast(data, ctypes.POINTER(TestStruct))[0]
assert obj.point.x == 1
assert obj.point.y == 2
---------------------------

Use b'\1\0\0\0\2\0\0\0' for little endian.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4376>
_______________________________________


More information about the Python-bugs-list mailing list