Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/ctypes/_endian.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _other_endian(typ):
if isinstance(typ, _array_type):
return _other_endian(typ._type_) * typ._length_
# if typ is structure
if issubclass(typ, Structure):
if issubclass(typ, Structure | Union):
return typ
raise TypeError("This type does not support other endian: %s" % typ)

Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_ctypes/test_byteswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ class TestUnion(parent):
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()
Original file line number Diff line number Diff line change
@@ -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.