mirror of
https://github.com/python/cpython.git
synced 2024-11-27 03:45:08 +08:00
gh-105102: Fix nested unions in structures when the system byteorder is the opposite (GH-105106)
This commit is contained in:
parent
33b47a2c28
commit
0b541f64c4
@ -15,8 +15,8 @@ def _other_endian(typ):
|
||||
# if typ is array
|
||||
if isinstance(typ, _array_type):
|
||||
return _other_endian(typ._type_) * typ._length_
|
||||
# if typ is structure
|
||||
if issubclass(typ, Structure):
|
||||
# if typ is structure or union
|
||||
if issubclass(typ, (Structure, Union)):
|
||||
return typ
|
||||
raise TypeError("This type does not support other endian: %s" % typ)
|
||||
|
||||
|
@ -363,6 +363,24 @@ class Test(unittest.TestCase):
|
||||
self.assertEqual(s.point.x, 1)
|
||||
self.assertEqual(s.point.y, 2)
|
||||
|
||||
def test_build_struct_union_opposite_system_byteorder(self):
|
||||
# gh-105102
|
||||
if sys.byteorder == "little":
|
||||
_Structure = BigEndianStructure
|
||||
_Union = BigEndianUnion
|
||||
else:
|
||||
_Structure = LittleEndianStructure
|
||||
_Union = LittleEndianUnion
|
||||
|
||||
class S1(_Structure):
|
||||
_fields_ = [("a", c_byte), ("b", c_byte)]
|
||||
|
||||
class U1(_Union):
|
||||
_fields_ = [("s1", S1), ("ab", c_short)]
|
||||
|
||||
class S2(_Structure):
|
||||
_fields_ = [("u1", U1), ("c", c_byte)]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -0,0 +1,2 @@
|
||||
Allow :class:`ctypes.Union` to be nested in :class:`ctypes.Structure` when
|
||||
the system endianness is the opposite of the classes.
|
Loading…
Reference in New Issue
Block a user