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/test/test_buffer.py | |
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/test/test_buffer.py')
-rw-r--r-- | Lib/test/test_buffer.py | 29 |
1 files changed, 27 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. |