Merge 3.4 (ctypes)

This commit is contained in:
Victor Stinner 2015-07-29 14:35:12 +02:00
commit 5ef6fde92c
3 changed files with 32 additions and 0 deletions

View File

@ -259,5 +259,33 @@ class BitFieldTest(unittest.TestCase):
x.a = 0xFEDCBA9876543211 x.a = 0xFEDCBA9876543211
self.assertEqual(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__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -13,6 +13,9 @@ Core and Builtins
Library Library
------- -------
- Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch
written by Matthieu Gautier.
- Issue #23254: Document how to close the TCPServer listening socket. - Issue #23254: Document how to close the TCPServer listening socket.
Patch from Martin Panter. Patch from Martin Panter.

View File

@ -765,6 +765,7 @@ I_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
if (get_ulong(value, &val) < 0) if (get_ulong(value, &val) < 0)
return NULL; return NULL;
memcpy(&field, ptr, sizeof(field)); memcpy(&field, ptr, sizeof(field));
field = SWAP_INT(field);
field = SET(unsigned int, field, (unsigned int)val, size); field = SET(unsigned int, field, (unsigned int)val, size);
field = SWAP_INT(field); field = SWAP_INT(field);
memcpy(ptr, &field, sizeof(field)); memcpy(ptr, &field, sizeof(field));