]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 29 Jul 2015 12:37:17 +0000 (14:37 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 29 Jul 2015 12:37:17 +0000 (14:37 +0200)
written by Matthieu Gautier.

Lib/ctypes/test/test_bitfields.py
Misc/NEWS
Modules/_ctypes/cfield.c

index 991dbe862d1b3620eb73c2fecae8cbfcf8fe945b..a854d2b7d5ac20bcc2a87555b5f5b381967aec73 100644 (file)
@@ -259,5 +259,33 @@ class BitFieldTest(unittest.TestCase):
         x.a = 0xFEDCBA9876543211
         self.assertEqual(x.a, 0xFEDCBA9876543211)
 
+    @need_symbol('c_uint32')
+    def test_uint32_swap_little_endian(self):
+        # Issue #23319
+        class Little(LittleEndianStructure):
+            _fields_ = [("a", c_uint32, 24),
+                        ("b", c_uint32, 4),
+                        ("c", c_uint32, 4)]
+        b = bytearray(4)
+        x = Little.from_buffer(b)
+        x.a = 0xabcdef
+        x.b = 1
+        x.c = 2
+        self.assertEqual(b, b'\xef\xcd\xab\x21')
+
+    @need_symbol('c_uint32')
+    def test_uint32_swap_big_endian(self):
+        # Issue #23319
+        class Big(BigEndianStructure):
+            _fields_ = [("a", c_uint32, 24),
+                        ("b", c_uint32, 4),
+                        ("c", c_uint32, 4)]
+        b = bytearray(4)
+        x = Big.from_buffer(b)
+        x.a = 0xabcdef
+        x.b = 1
+        x.c = 2
+        self.assertEqual(b, b'\xab\xcd\xef\x12')
+
 if __name__ == "__main__":
     unittest.main()
index e86117581cb088a37600c494f64568bbab45dd36..65bb83796172391d31755f0fbdef19d08f0d62e3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch
+  written by Matthieu Gautier.
+
 - Issue #23254: Document how to close the TCPServer listening socket.
   Patch from Martin Panter.
 
index 76c72f877a7cdf35ffa3879bf6a586c734a29366..85b5ad28903fb10f8e4e668c26a16d0168c3a5db 100644 (file)
@@ -769,6 +769,7 @@ I_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
     if (get_ulong(value, &val) < 0)
         return  NULL;
     memcpy(&field, ptr, sizeof(field));
+    field = SWAP_INT(field);
     field = SET(unsigned int, field, (unsigned int)val, size);
     field = SWAP_INT(field);
     memcpy(ptr, &field, sizeof(field));