]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-105102: Fix nested unions in structures when the system byteorder is the...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 17 Jan 2024 19:58:28 +0000 (21:58 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Jan 2024 19:58:28 +0000 (19:58 +0000)
(cherry picked from commit 0b541f64c472976b2fee1ec9919bc7b02a798242)

Co-authored-by: Sheidan <37596668+Sh3idan@users.noreply.github.com>
Lib/ctypes/_endian.py
Lib/ctypes/test/test_byteswap.py
Misc/NEWS.d/next/Library/2023-05-30-18-30-11.gh-issue-105102.SnpK04.rst [new file with mode: 0644]

index 34dee64b1a65a693b34417c274d1de9ccdc05123..6f674b21371c4047e7649814bb71020e523b9986 100644 (file)
@@ -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)
 
index caefb774cc53754255ce64c540043f9a37c3e29e..44d32ff27ba9236b4b3b8a96e012b396c2c628ae 100644 (file)
@@ -352,5 +352,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()
diff --git a/Misc/NEWS.d/next/Library/2023-05-30-18-30-11.gh-issue-105102.SnpK04.rst b/Misc/NEWS.d/next/Library/2023-05-30-18-30-11.gh-issue-105102.SnpK04.rst
new file mode 100644 (file)
index 0000000..7ca21af
--- /dev/null
@@ -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.