[Python-Dev] Hashable memoryviews
solipsis at pitrou.net
Sun Nov 13 11:49:11 CET 2011
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', )
> >>> b = b'\x00\x00\x00\x00\x00\x00\x00\x00'
> >>> m_array = memoryview(a)
> >>> m_bytes = memoryview(b)
> >>> m_cast = m_array.cast('B')
> >>> m_bytes == m_cast
> >>> hash(b) == hash(a)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: unhashable type: 'array.array'
In this case, the memoryview wouldn't be hashable either.
More information about the Python-Dev