[New-bugs-announce] [issue35548] memoryview needlessly (?) requires represented object to be hashable
Ilya Kulakov
report at bugs.python.org
Fri Dec 21 01:18:19 EST 2018
New submission from Ilya Kulakov <kulakov.ilya at gmail.com>:
Implementation of memoryview's hashing method [1] imposes the following constraints in order to be hashable (per documentation):
> One-dimensional memoryviews of hashable (read-only) types with formats ‘B’, ‘b’ or ‘c’ are also hashable. The hash is defined as hash(m) == hash(m.tobytes()):
However it's not clear why original type needs to be hashable given that memoryview deals with 1-dimensional read-only bytes representation of an object. Not only it requires the developer to make an extra copy of C-bytes, but also calls __hash__ of a represented object without using the result other than to detect an error.
My particular use case involves a memory view of a readonly numpy's ndarray. My view satisfies the following constraints:
>>> print(data.format, data.readonly, data.shape, data.c_contiguous)
b True (25,) True
But nevertheless the hashing fails because ndarray itself is not hashable.
Stefan Krah wrote [2]:
> Note that memory_hash() raises an error if the exporter *itself* is
not hashable, so it only hashes immutable objects by design.
But while __hash__ indeed tells that object is (supposed to be) immutable, there is no requirement for all immutable objects to have __hash__.
Both threads I have found ([3], [4]) are quite lengthy and show certain discord in opinions regarding the issue. Perhaps after 6 years since the release of the feature the view on the problem has changed?
1: https://github.com/python/cpython/blob/d1e717588728a23d576c4ead775f7dbd68149696/Objects/memoryobject.c#L2829-L2876
2: https://bugs.python.org/issue15814#msg169510
3: https://bugs.python.org/issue15573
4: https://bugs.python.org/issue15573
----------
components: Library (Lib)
messages: 332280
nosy: Kentzo, skrah
priority: normal
severity: normal
status: open
title: memoryview needlessly (?) requires represented object to be hashable
versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35548>
_______________________________________
More information about the New-bugs-announce
mailing list