LEFT | RIGHT |
1 """Unit tests for the memoryview | 1 """Unit tests for the memoryview |
2 | 2 |
3 Some tests are in test_bytes. Many tests that require _testbuffer.ndarray | 3 Some tests are in test_bytes. Many tests that require _testbuffer.ndarray |
4 are in test_buffer. | 4 are in test_buffer. |
5 """ | 5 """ |
6 | 6 |
7 import unittest | 7 import unittest |
8 import test.support | 8 import test.support |
9 import sys | 9 import sys |
10 import gc | 10 import gc |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 self.check_getitem_with_type(tp) | 50 self.check_getitem_with_type(tp) |
51 | 51 |
52 def test_iter(self): | 52 def test_iter(self): |
53 for tp in self._types: | 53 for tp in self._types: |
54 b = tp(self._source) | 54 b = tp(self._source) |
55 m = self._view(b) | 55 m = self._view(b) |
56 self.assertEqual(list(m), [m[i] for i in range(len(m))]) | 56 self.assertEqual(list(m), [m[i] for i in range(len(m))]) |
57 | 57 |
58 def test_setitem_readonly(self): | 58 def test_setitem_readonly(self): |
59 if not self.ro_type: | 59 if not self.ro_type: |
60 return | 60 self.skipTest("no read-only type to test") |
61 b = self.ro_type(self._source) | 61 b = self.ro_type(self._source) |
62 oldrefcount = sys.getrefcount(b) | 62 oldrefcount = sys.getrefcount(b) |
63 m = self._view(b) | 63 m = self._view(b) |
64 def setitem(value): | 64 def setitem(value): |
65 m[0] = value | 65 m[0] = value |
66 self.assertRaises(TypeError, setitem, b"a") | 66 self.assertRaises(TypeError, setitem, b"a") |
67 self.assertRaises(TypeError, setitem, 65) | 67 self.assertRaises(TypeError, setitem, 65) |
68 self.assertRaises(TypeError, setitem, memoryview(b"a")) | 68 self.assertRaises(TypeError, setitem, memoryview(b"a")) |
69 m = None | 69 m = None |
70 self.assertEqual(sys.getrefcount(b), oldrefcount) | 70 self.assertEqual(sys.getrefcount(b), oldrefcount) |
71 | 71 |
72 def test_setitem_writable(self): | 72 def test_setitem_writable(self): |
73 if not self.rw_type: | 73 if not self.rw_type: |
74 return | 74 self.skipTest("no writable type to test") |
75 tp = self.rw_type | 75 tp = self.rw_type |
76 b = self.rw_type(self._source) | 76 b = self.rw_type(self._source) |
77 oldrefcount = sys.getrefcount(b) | 77 oldrefcount = sys.getrefcount(b) |
78 m = self._view(b) | 78 m = self._view(b) |
79 m[0] = ord(b'1') | 79 m[0] = ord(b'1') |
80 self._check_contents(tp, b, b"1bcdef") | 80 self._check_contents(tp, b, b"1bcdef") |
81 m[0:1] = tp(b"0") | 81 m[0:1] = tp(b"0") |
82 self._check_contents(tp, b, b"0bcdef") | 82 self._check_contents(tp, b, b"0bcdef") |
83 m[1:3] = tp(b"12") | 83 m[1:3] = tp(b"12") |
84 self._check_contents(tp, b, b"012def") | 84 self._check_contents(tp, b, b"012def") |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 self.assertEqual(m.itemsize, self.itemsize) | 182 self.assertEqual(m.itemsize, self.itemsize) |
183 self.assertEqual(m.ndim, 1) | 183 self.assertEqual(m.ndim, 1) |
184 self.assertEqual(m.shape, (6,)) | 184 self.assertEqual(m.shape, (6,)) |
185 self.assertEqual(len(m), 6) | 185 self.assertEqual(len(m), 6) |
186 self.assertEqual(m.strides, (self.itemsize,)) | 186 self.assertEqual(m.strides, (self.itemsize,)) |
187 self.assertEqual(m.suboffsets, ()) | 187 self.assertEqual(m.suboffsets, ()) |
188 return m | 188 return m |
189 | 189 |
190 def test_attributes_readonly(self): | 190 def test_attributes_readonly(self): |
191 if not self.ro_type: | 191 if not self.ro_type: |
192 return | 192 self.skipTest("no read-only type to test") |
193 m = self.check_attributes_with_type(self.ro_type) | 193 m = self.check_attributes_with_type(self.ro_type) |
194 self.assertEqual(m.readonly, True) | 194 self.assertEqual(m.readonly, True) |
195 | 195 |
196 def test_attributes_writable(self): | 196 def test_attributes_writable(self): |
197 if not self.rw_type: | 197 if not self.rw_type: |
198 return | 198 self.skipTest("no writable type to test") |
199 m = self.check_attributes_with_type(self.rw_type) | 199 m = self.check_attributes_with_type(self.rw_type) |
200 self.assertEqual(m.readonly, False) | 200 self.assertEqual(m.readonly, False) |
201 | 201 |
202 def test_getbuffer(self): | 202 def test_getbuffer(self): |
203 # Test PyObject_GetBuffer() on a memoryview object. | 203 # Test PyObject_GetBuffer() on a memoryview object. |
204 for tp in self._types: | 204 for tp in self._types: |
205 b = tp(self._source) | 205 b = tp(self._source) |
206 oldrefcount = sys.getrefcount(b) | 206 oldrefcount = sys.getrefcount(b) |
207 m = self._view(b) | 207 m = self._view(b) |
208 oldviewrefcount = sys.getrefcount(m) | 208 oldviewrefcount = sys.getrefcount(m) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 self._check_released(m, tp) | 294 self._check_released(m, tp) |
295 # Can be called a second time (it's a no-op) | 295 # Can be called a second time (it's a no-op) |
296 m.release() | 296 m.release() |
297 self._check_released(m, tp) | 297 self._check_released(m, tp) |
298 | 298 |
299 def test_writable_readonly(self): | 299 def test_writable_readonly(self): |
300 # Issue #10451: memoryview incorrectly exposes a readonly | 300 # Issue #10451: memoryview incorrectly exposes a readonly |
301 # buffer as writable causing a segfault if using mmap | 301 # buffer as writable causing a segfault if using mmap |
302 tp = self.ro_type | 302 tp = self.ro_type |
303 if tp is None: | 303 if tp is None: |
304 return | 304 self.skipTest("no read-only type to test") |
305 b = tp(self._source) | 305 b = tp(self._source) |
306 m = self._view(b) | 306 m = self._view(b) |
307 i = io.BytesIO(b'ZZZZ') | 307 i = io.BytesIO(b'ZZZZ') |
308 self.assertRaises(TypeError, i.readinto, m) | 308 self.assertRaises(TypeError, i.readinto, m) |
309 | 309 |
310 def test_getbuf_fail(self): | 310 def test_getbuf_fail(self): |
311 self.assertRaises(TypeError, self._view, {}) | 311 self.assertRaises(TypeError, self._view, {}) |
312 | 312 |
313 def test_hash(self): | 313 def test_hash(self): |
314 # Memoryviews of readonly (hashable) types are hashable, and they | 314 # Memoryviews of readonly (hashable) types are hashable, and they |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 itemsize = 1 | 372 itemsize = 1 |
373 format = 'B' | 373 format = 'B' |
374 | 374 |
375 class BaseArrayMemoryTests(AbstractMemoryTests): | 375 class BaseArrayMemoryTests(AbstractMemoryTests): |
376 ro_type = None | 376 ro_type = None |
377 rw_type = lambda self, b: array.array('i', list(b)) | 377 rw_type = lambda self, b: array.array('i', list(b)) |
378 getitem_type = lambda self, b: array.array('i', list(b)).tobytes() | 378 getitem_type = lambda self, b: array.array('i', list(b)).tobytes() |
379 itemsize = array.array('i').itemsize | 379 itemsize = array.array('i').itemsize |
380 format = 'i' | 380 format = 'i' |
381 | 381 |
| 382 @unittest.skip('XXX test should be adapted for non-byte buffers') |
382 def test_getbuffer(self): | 383 def test_getbuffer(self): |
383 # XXX Test should be adapted for non-byte buffers | |
384 pass | 384 pass |
385 | 385 |
| 386 @unittest.skip('XXX NotImplementedError: tolist() only supports byte views') |
386 def test_tolist(self): | 387 def test_tolist(self): |
387 # XXX NotImplementedError: tolist() only supports byte views | |
388 pass | 388 pass |
389 | 389 |
390 | 390 |
391 # Variations on indirection levels: memoryview, slice of memoryview, | 391 # Variations on indirection levels: memoryview, slice of memoryview, |
392 # slice of slice of memoryview. | 392 # slice of slice of memoryview. |
393 # This is important to test allocation subtleties. | 393 # This is important to test allocation subtleties. |
394 | 394 |
395 class BaseMemoryviewTests: | 395 class BaseMemoryviewTests: |
396 def _view(self, obj): | 396 def _view(self, obj): |
397 return memoryview(obj) | 397 return memoryview(obj) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 class ArrayMemorySliceSliceTest(unittest.TestCase, | 469 class ArrayMemorySliceSliceTest(unittest.TestCase, |
470 BaseMemorySliceSliceTests, BaseArrayMemoryTests): | 470 BaseMemorySliceSliceTests, BaseArrayMemoryTests): |
471 pass | 471 pass |
472 | 472 |
473 | 473 |
474 def test_main(): | 474 def test_main(): |
475 test.support.run_unittest(__name__) | 475 test.support.run_unittest(__name__) |
476 | 476 |
477 if __name__ == "__main__": | 477 if __name__ == "__main__": |
478 test_main() | 478 test_main() |
LEFT | RIGHT |