changeset: 97119:935ae7f001e3 branch: 3.4 parent: 97116:33dbcde76b3f user: Victor Stinner date: Wed Jul 29 14:33:52 2015 +0200 files: Lib/ctypes/test/test_bitfields.py Misc/NEWS Modules/_ctypes/cfield.c description: Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch written by Matthieu Gautier. diff -r 33dbcde76b3f -r 935ae7f001e3 Lib/ctypes/test/test_bitfields.py --- a/Lib/ctypes/test/test_bitfields.py Tue Jul 28 23:22:23 2015 -0700 +++ b/Lib/ctypes/test/test_bitfields.py Wed Jul 29 14:33:52 2015 +0200 @@ -259,5 +259,33 @@ 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() diff -r 33dbcde76b3f -r 935ae7f001e3 Misc/NEWS --- a/Misc/NEWS Tue Jul 28 23:22:23 2015 -0700 +++ b/Misc/NEWS Wed Jul 29 14:33:52 2015 +0200 @@ -66,6 +66,9 @@ 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. diff -r 33dbcde76b3f -r 935ae7f001e3 Modules/_ctypes/cfield.c --- a/Modules/_ctypes/cfield.c Tue Jul 28 23:22:23 2015 -0700 +++ b/Modules/_ctypes/cfield.c Wed Jul 29 14:33:52 2015 +0200 @@ -765,6 +765,7 @@ 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));