diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-21 19:46:33 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-21 19:46:33 (GMT) |
commit | ce4a9da70535b4bb9048147b141f01004af2133d (patch) | |
tree | 853fa7484683a9c858f29bfab1320fb4baee5d98 /Lib | |
parent | 0a3229de6b80cfa9e432ef5a9c72548569503075 (diff) | |
download | cpython-ce4a9da70535b4bb9048147b141f01004af2133d.zip cpython-ce4a9da70535b4bb9048147b141f01004af2133d.tar.gz cpython-ce4a9da70535b4bb9048147b141f01004af2133d.tar.bz2 |
Issue #13411: memoryview objects are now hashable when the underlying object is hashable.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_memoryview.py | 27 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 4 |
2 files changed, 29 insertions, 2 deletions
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py index 0bfddd9..a5a0ca1 100644 --- a/Lib/test/test_memoryview.py +++ b/Lib/test/test_memoryview.py @@ -283,6 +283,33 @@ class AbstractMemoryTests: i = io.BytesIO(b'ZZZZ') self.assertRaises(TypeError, i.readinto, m) + def test_hash(self): + # Memoryviews of readonly (hashable) types are hashable, and they + # hash as the corresponding object. + tp = self.ro_type + if tp is None: + self.skipTest("no read-only type to test") + b = tp(self._source) + m = self._view(b) + self.assertEqual(hash(m), hash(b"abcdef")) + # Releasing the memoryview keeps the stored hash value (as with weakrefs) + m.release() + self.assertEqual(hash(m), hash(b"abcdef")) + # Hashing a memoryview for the first time after it is released + # results in an error (as with weakrefs). + m = self._view(b) + m.release() + self.assertRaises(ValueError, hash, m) + + def test_hash_writable(self): + # Memoryviews of writable types are unhashable + tp = self.rw_type + if tp is None: + self.skipTest("no writable type to test") + b = tp(self._source) + m = self._view(b) + self.assertRaises(ValueError, hash, m) + # Variations on source objects for the buffer: bytes-like objects, then arrays # with itemsize > 1. # NOTE: support for multi-dimensional objects is unimplemented. diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index c99f4d7..ba0b592 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -770,8 +770,8 @@ class SizeofTest(unittest.TestCase): check(int(PyLong_BASE), size(vh) + 2*self.longdigit) check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit) check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit) - # memory - check(memoryview(b''), size(h + 'PP2P2i7P')) + # memory (Py_buffer + hash value) + check(memoryview(b''), size(h + 'PP2P2i7P' + 'P')) # module check(unittest, size(h + '3P')) # None |