[Python-Dev] Hashable memoryviews (original) (raw)
Stefan Krah stefan at bytereef.org
Sun Nov 13 11:39:46 CET 2011
- Previous message: [Python-Dev] Hashable memoryviews
- Next message: [Python-Dev] Hashable memoryviews
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Antoine Pitrou <solipsis at pitrou.net> wrote:
Only if the original object is itself mutable, otherwise the memoryview is read-only.
I would propose the following algorithm: 1) try to calculate the original object's hash; if it fails, consider the memoryview unhashable (the buffer is probably mutable)
With slices or the new casts (See: http://bugs.python.org/issue5231, implemented in http://hg.python.org/features/pep-3118#memoryview ), it is possible to have different hashes for equal objects:
b1 = bytes([1,2,3,4]) b2 = bytes([4,3,2,1]) m1 = memoryview(b1) m2 = memoryview(b2)[::-1] m1 == m2 True hash(b1) 4154562130492273536 hash(b2) -1828484551660457336
Or:
a = array.array('L', [0]) b = b'\x00\x00\x00\x00\x00\x00\x00\x00' marray = memoryview(a) mbytes = memoryview(b) mcast = marray.cast('B') mbytes == mcast True hash(b) == hash(a) Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'array.array'
Stefan Krah
- Previous message: [Python-Dev] Hashable memoryviews
- Next message: [Python-Dev] Hashable memoryviews
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]