diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_buffer.py | 39 | ||||
-rw-r--r-- | Lib/test/test_memoryview.py | 11 |
2 files changed, 34 insertions, 16 deletions
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index f302da4..a3f3ef0 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -924,23 +924,30 @@ class TestBufferProtocol(unittest.TestCase): except BufferError: # re-exporter does not provide full information return ex = result.obj if isinstance(result, memoryview) else result - self.assertIs(m.obj, ex) - self.assertEqual(m.nbytes, expected_len) - self.assertEqual(m.itemsize, itemsize) - self.assertEqual(m.format, fmt) - self.assertEqual(m.readonly, readonly) - self.assertEqual(m.ndim, ndim) - self.assertEqual(m.shape, tuple(shape)) - if not (sliced and suboffsets): - self.assertEqual(m.strides, tuple(strides)) - self.assertEqual(m.suboffsets, tuple(suboffsets)) - - n = 1 if ndim == 0 else len(lst) - self.assertEqual(len(m), n) - rep = result.tolist() if fmt else result.tobytes() - self.assertEqual(rep, lst) - self.assertEqual(m, result) + def check_memoryview(m, expected_readonly=readonly): + self.assertIs(m.obj, ex) + self.assertEqual(m.nbytes, expected_len) + self.assertEqual(m.itemsize, itemsize) + self.assertEqual(m.format, fmt) + self.assertEqual(m.readonly, expected_readonly) + self.assertEqual(m.ndim, ndim) + self.assertEqual(m.shape, tuple(shape)) + if not (sliced and suboffsets): + self.assertEqual(m.strides, tuple(strides)) + self.assertEqual(m.suboffsets, tuple(suboffsets)) + + n = 1 if ndim == 0 else len(lst) + self.assertEqual(len(m), n) + + rep = result.tolist() if fmt else result.tobytes() + self.assertEqual(rep, lst) + self.assertEqual(m, result) + + check_memoryview(m) + with m.toreadonly() as mm: + check_memoryview(mm, expected_readonly=True) + m.tobytes() # Releasing mm didn't release m def verify_getbuf(self, orig_ex, ex, req, sliced=False): def simple_fmt(ex): diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py index ddd5b9a..ca307d8 100644 --- a/Lib/test/test_memoryview.py +++ b/Lib/test/test_memoryview.py @@ -362,6 +362,17 @@ class AbstractMemoryTests: self.assertEqual(list(reversed(m)), aslist) self.assertEqual(list(reversed(m)), list(m[::-1])) + def test_toreadonly(self): + for tp in self._types: + b = tp(self._source) + m = self._view(b) + mm = m.toreadonly() + self.assertTrue(mm.readonly) + self.assertTrue(memoryview(mm).readonly) + self.assertEqual(mm.tolist(), m.tolist()) + mm.release() + m.tolist() + def test_issue22668(self): a = array.array('H', [256, 256, 256, 256]) x = memoryview(a) |