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

Side by Side Diff: Lib/test/test_memoryview.py

Issue 13411: Hashable memoryviews
Patch Set: Created 7 years, 5 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:
View unified diff | Download patch
« no previous file with comments | « Include/object.h ('k') | Lib/test/test_sys.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for the memoryview 1 """Unit tests for the memoryview
2 2
3 XXX We need more tests! Some tests are in test_bytes 3 XXX We need more tests! Some tests are in test_bytes
4 """ 4 """
5 5
6 import unittest 6 import unittest
7 import test.support 7 import test.support
8 import sys 8 import sys
9 import gc 9 import gc
10 import weakref 10 import weakref
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 def test_writable_readonly(self): 275 def test_writable_readonly(self):
276 # Issue #10451: memoryview incorrectly exposes a readonly 276 # Issue #10451: memoryview incorrectly exposes a readonly
277 # buffer as writable causing a segfault if using mmap 277 # buffer as writable causing a segfault if using mmap
278 tp = self.ro_type 278 tp = self.ro_type
279 if tp is None: 279 if tp is None:
280 return 280 return
281 b = tp(self._source) 281 b = tp(self._source)
282 m = self._view(b) 282 m = self._view(b)
283 i = io.BytesIO(b'ZZZZ') 283 i = io.BytesIO(b'ZZZZ')
284 self.assertRaises(TypeError, i.readinto, m) 284 self.assertRaises(TypeError, i.readinto, m)
285
286 def test_hash(self):
287 # Memoryviews of readonly (hashable) types are hashable, and they
288 # hash as the corresponding object.
289 tp = self.ro_type
290 if tp is None:
291 self.skipTest("no read-only type to test")
292 b = tp(self._source)
293 m = self._view(b)
294 self.assertEqual(hash(m), hash(b"abcdef"))
295 # Releasing the memoryview keeps the stored hash value (as with weakrefs )
296 m.release()
297 self.assertEqual(hash(m), hash(b"abcdef"))
298 # Hashing a memoryview for the first time after it is released
299 # results in an error (as with weakrefs).
300 m = self._view(b)
301 m.release()
302 self.assertRaises(ValueError, hash, m)
303
304 def test_hash_writable(self):
305 # Memoryviews of writable types are unhashable
306 tp = self.rw_type
307 if tp is None:
308 self.skipTest("no writable type to test")
309 b = tp(self._source)
310 m = self._view(b)
311 self.assertRaises(ValueError, hash, m)
285 312
286 # Variations on source objects for the buffer: bytes-like objects, then arrays 313 # Variations on source objects for the buffer: bytes-like objects, then arrays
287 # with itemsize > 1. 314 # with itemsize > 1.
288 # NOTE: support for multi-dimensional objects is unimplemented. 315 # NOTE: support for multi-dimensional objects is unimplemented.
289 316
290 class BaseBytesMemoryTests(AbstractMemoryTests): 317 class BaseBytesMemoryTests(AbstractMemoryTests):
291 ro_type = bytes 318 ro_type = bytes
292 rw_type = bytearray 319 rw_type = bytearray
293 getitem_type = bytes 320 getitem_type = bytes
294 itemsize = 1 321 itemsize = 1
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 class ArrayMemorySliceSliceTest(unittest.TestCase, 418 class ArrayMemorySliceSliceTest(unittest.TestCase,
392 BaseMemorySliceSliceTests, BaseArrayMemoryTests): 419 BaseMemorySliceSliceTests, BaseArrayMemoryTests):
393 pass 420 pass
394 421
395 422
396 def test_main(): 423 def test_main():
397 test.support.run_unittest(__name__) 424 test.support.run_unittest(__name__)
398 425
399 if __name__ == "__main__": 426 if __name__ == "__main__":
400 test_main() 427 test_main()
OLDNEW
« no previous file with comments | « Include/object.h ('k') | Lib/test/test_sys.py » ('j') | no next file with comments »

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