cpython: 88f2c4f12b56 (original) (raw)
Mercurial > cpython
changeset 97122:88f2c4f12b56 2.7
Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch written by Matthieu Gautier. [#23319]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Wed, 29 Jul 2015 14:37:17 +0200 |
parents | 8afd995802a6 |
children | 12b353f76447 |
files | Lib/ctypes/test/test_bitfields.py Misc/NEWS Modules/_ctypes/cfield.c |
diffstat | 3 files changed, 32 insertions(+), 0 deletions(-)[+] [-] Lib/ctypes/test/test_bitfields.py 28 Misc/NEWS 3 Modules/_ctypes/cfield.c 1 |
line wrap: on
line diff
--- a/Lib/ctypes/test/test_bitfields.py +++ b/Lib/ctypes/test/test_bitfields.py @@ -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[](#l1.9)
class Little(LittleEndianStructure):[](#l1.10)
_fields_ = [("a", c_uint32, 24),[](#l1.11)
("b", c_uint32, 4),[](#l1.12)
("c", c_uint32, 4)][](#l1.13)
b = bytearray(4)[](#l1.14)
x = Little.from_buffer(b)[](#l1.15)
x.a = 0xabcdef[](#l1.16)
x.b = 1[](#l1.17)
x.c = 2[](#l1.18)
self.assertEqual(b, b'\xef\xcd\xab\x21')[](#l1.19)
- @need_symbol('c_uint32')
- def test_uint32_swap_big_endian(self):
# Issue #23319[](#l1.23)
class Big(BigEndianStructure):[](#l1.24)
_fields_ = [("a", c_uint32, 24),[](#l1.25)
("b", c_uint32, 4),[](#l1.26)
("c", c_uint32, 4)][](#l1.27)
b = bytearray(4)[](#l1.28)
x = Big.from_buffer(b)[](#l1.29)
x.a = 0xabcdef[](#l1.30)
x.b = 1[](#l1.31)
x.c = 2[](#l1.32)
self.assertEqual(b, b'\xab\xcd\xef\x12')[](#l1.33)
+ if name == "main": unittest.main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,9 @@ Core and Builtins Library ------- +- Issue #23319: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch
--- a/Modules/_ctypes/cfield.c +++ b/Modules/ctypes/cfield.c @@ -769,6 +769,7 @@ I_set_sw(void *ptr, PyObject *value, Py if (get_ulong(value, &val) < 0) return NULL; memcpy(&field, ptr, sizeof(field));