diff options
author | Stefan Krah <skrah@bytereef.org> | 2015-08-08 11:38:10 (GMT) |
---|---|---|
committer | Stefan Krah <skrah@bytereef.org> | 2015-08-08 11:38:10 (GMT) |
commit | 0c51595a784787d8e198af910cb6d79172ee52bb (patch) | |
tree | 56d21e209ab26a91c65cad305097fdf6bd8f1b45 /Lib | |
parent | 917c2c36542131dd81f2b5b4d185c50d3b15546f (diff) | |
download | cpython-0c51595a784787d8e198af910cb6d79172ee52bb.zip cpython-0c51595a784787d8e198af910cb6d79172ee52bb.tar.gz cpython-0c51595a784787d8e198af910cb6d79172ee52bb.tar.bz2 |
Issue #15944: memoryview: Allow arbitrary formats when casting to bytes.
Original patch by Martin Panter.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_buffer.py | 29 | ||||
-rw-r--r-- | Lib/test/test_memoryview.py | 21 |
2 files changed, 48 insertions, 2 deletions
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index 6803156..a653390 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -2559,8 +2559,7 @@ class TestBufferProtocol(unittest.TestCase): ex = ndarray(sitems, shape=[1], format=sfmt) msrc = memoryview(ex) for dfmt, _, _ in iter_format(1): - if (not is_memoryview_format(sfmt) or - not is_memoryview_format(dfmt)): + if not is_memoryview_format(dfmt): self.assertRaises(ValueError, msrc.cast, dfmt, [32//dsize]) else: @@ -2773,6 +2772,32 @@ class TestBufferProtocol(unittest.TestCase): ndim=ndim, shape=shape, strides=strides, lst=lst, cast=True) + if ctypes: + # format: "T{>l:x:>d:y:}" + class BEPoint(ctypes.BigEndianStructure): + _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_double)] + point = BEPoint(100, 200.1) + m1 = memoryview(point) + m2 = m1.cast('B') + self.assertEqual(m2.obj, point) + self.assertEqual(m2.itemsize, 1) + self.assertEqual(m2.readonly, 0) + self.assertEqual(m2.ndim, 1) + self.assertEqual(m2.shape, (m2.nbytes,)) + self.assertEqual(m2.strides, (1,)) + self.assertEqual(m2.suboffsets, ()) + + x = ctypes.c_double(1.2) + m1 = memoryview(x) + m2 = m1.cast('c') + self.assertEqual(m2.obj, x) + self.assertEqual(m2.itemsize, 1) + self.assertEqual(m2.readonly, 0) + self.assertEqual(m2.ndim, 1) + self.assertEqual(m2.shape, (m2.nbytes,)) + self.assertEqual(m2.strides, (1,)) + self.assertEqual(m2.suboffsets, ()) + def test_memoryview_tolist(self): # Most tolist() tests are in self.verify() etc. diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py index 6fe61a4..da01a84 100644 --- a/Lib/test/test_memoryview.py +++ b/Lib/test/test_memoryview.py @@ -492,5 +492,26 @@ class ArrayMemorySliceSliceTest(unittest.TestCase, pass +class OtherTest(unittest.TestCase): + def test_ctypes_cast(self): + # Issue 15944: Allow all source formats when casting to bytes. + ctypes = test.support.import_module("ctypes") + p6 = bytes(ctypes.c_double(0.6)) + + d = ctypes.c_double() + m = memoryview(d).cast("B") + m[:2] = p6[:2] + m[2:] = p6[2:] + self.assertEqual(d.value, 0.6) + + for format in "Bbc": + with self.subTest(format): + d = ctypes.c_double() + m = memoryview(d).cast(format) + m[:2] = memoryview(p6).cast(format)[:2] + m[2:] = memoryview(p6).cast(format)[2:] + self.assertEqual(d.value, 0.6) + + if __name__ == "__main__": unittest.main() |