[Python-Dev] Hashable memoryviews (original) (raw)
Antoine Pitrou solipsis at pitrou.net
Sun Nov 13 11:49:11 CET 2011
- Previous message: [Python-Dev] Hashable memoryviews
- Next message: [Python-Dev] Hashable memoryviews
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, 13 Nov 2011 11:39:46 +0100 Stefan Krah <stefan at bytereef.org> wrote:
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]
I don't understand this feature. How do you represent a reversed buffer using the buffer API, and how do you ensure that consumers (especially those written in C) see the buffer reversed?
Regardless, it's simply a matter of getting the hash algorithm right (i.e. iterate in logical order rather than memory order).
>>> 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'
In this case, the memoryview wouldn't be hashable either.
Regards
Antoine.
- Previous message: [Python-Dev] Hashable memoryviews
- Next message: [Python-Dev] Hashable memoryviews
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]