Stefan Krah added the comment:

Actually we have an extra safety net in memory_hash() apart from
the readonly check:  We also check if the underlying object is
hashable.

This might be applicable here, too. Unfortunately mmap objects
*are* hashable, leading to some funny results:


>>> import mmap
>>> with open("hello.txt", "wb") as f:
...     f.write(b"xxxxx\n")
...
6
>>> f = open("hello.txt", "r+b")
>>> mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
>>> x = memoryview(mm)
>>> hash(mm)
-9223363309538046107
>>> hash(x)
-3925142568057840789
>>> x.tolist()
[120, 120, 120, 120, 120, 10]
>>>
>>> with open("hello.txt", "wb") as g:
...     g.write(b"yyy\n")
...
4
>>> hash(mm)
-9223363309538046107
>>> hash(x)
-3925142568057840789
>>> x.tolist()
[121, 121, 121, 10, 0, 0]


memoryview (rightfully) assumes that hashable objects are immutable
and caches the first hash.

I'm not sure why mmap objects are hashable, it looks like a bug
to me.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22003>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to