Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.11] gh-105102: Fix nested unions in structures when the system byteorder is the opposite (GH-105106) #114205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Lib/ctypes/_endian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions Lib/ctypes/test/test_byteswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,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.