Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(31626)

Delta Between Two Patch Sets: Lib/test/test_memoryview.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years ago
Right Patch Set: Created 5 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_logging.py ('k') | Lib/test/test_minidom.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
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
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 30 matching lines...) Expand all
345 L = [] 345 L = []
346 def callback(wr, b=b): 346 def callback(wr, b=b):
347 L.append(b) 347 L.append(b)
348 wr = weakref.ref(m, callback) 348 wr = weakref.ref(m, callback)
349 self.assertIs(wr(), m) 349 self.assertIs(wr(), m)
350 del m 350 del m
351 test.support.gc_collect() 351 test.support.gc_collect()
352 self.assertIsNone(wr()) 352 self.assertIsNone(wr())
353 self.assertIs(L[0], b) 353 self.assertIs(L[0], b)
354 354
355 def test_reversed(self):
356 for tp in self._types:
357 b = tp(self._source)
358 m = self._view(b)
359 aslist = list(reversed(m.tolist()))
360 self.assertEqual(list(reversed(m)), aslist)
361 self.assertEqual(list(reversed(m)), list(m[::-1]))
362
363
355 # Variations on source objects for the buffer: bytes-like objects, then arrays 364 # Variations on source objects for the buffer: bytes-like objects, then arrays
356 # with itemsize > 1. 365 # with itemsize > 1.
357 # NOTE: support for multi-dimensional objects is unimplemented. 366 # NOTE: support for multi-dimensional objects is unimplemented.
358 367
359 class BaseBytesMemoryTests(AbstractMemoryTests): 368 class BaseBytesMemoryTests(AbstractMemoryTests):
360 ro_type = bytes 369 ro_type = bytes
361 rw_type = bytearray 370 rw_type = bytearray
362 getitem_type = bytes 371 getitem_type = bytes
363 itemsize = 1 372 itemsize = 1
364 format = 'B' 373 format = 'B'
365 374
366 class BaseArrayMemoryTests(AbstractMemoryTests): 375 class BaseArrayMemoryTests(AbstractMemoryTests):
367 ro_type = None 376 ro_type = None
368 rw_type = lambda self, b: array.array('i', list(b)) 377 rw_type = lambda self, b: array.array('i', list(b))
369 getitem_type = lambda self, b: array.array('i', list(b)).tobytes() 378 getitem_type = lambda self, b: array.array('i', list(b)).tobytes()
370 itemsize = array.array('i').itemsize 379 itemsize = array.array('i').itemsize
371 format = 'i' 380 format = 'i'
372 381
382 @unittest.skip('XXX test should be adapted for non-byte buffers')
373 def test_getbuffer(self): 383 def test_getbuffer(self):
374 # XXX Test should be adapted for non-byte buffers
375 pass 384 pass
376 385
386 @unittest.skip('XXX NotImplementedError: tolist() only supports byte views')
377 def test_tolist(self): 387 def test_tolist(self):
378 # XXX NotImplementedError: tolist() only supports byte views
379 pass 388 pass
380 389
381 390
382 # Variations on indirection levels: memoryview, slice of memoryview, 391 # Variations on indirection levels: memoryview, slice of memoryview,
383 # slice of slice of memoryview. 392 # slice of slice of memoryview.
384 # This is important to test allocation subtleties. 393 # This is important to test allocation subtleties.
385 394
386 class BaseMemoryviewTests: 395 class BaseMemoryviewTests:
387 def _view(self, obj): 396 def _view(self, obj):
388 return memoryview(obj) 397 return memoryview(obj)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 class ArrayMemorySliceSliceTest(unittest.TestCase, 469 class ArrayMemorySliceSliceTest(unittest.TestCase,
461 BaseMemorySliceSliceTests, BaseArrayMemoryTests): 470 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
462 pass 471 pass
463 472
464 473
465 def test_main(): 474 def test_main():
466 test.support.run_unittest(__name__) 475 test.support.run_unittest(__name__)
467 476
468 if __name__ == "__main__": 477 if __name__ == "__main__":
469 test_main() 478 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+