You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
io.BytesIO.getvalue() can return bytes with a stale hash #160181
BytesIO.getvalue() (and read()/peek() covering the whole buffer) returns the internal bytes object without copying. Whether that object may later be modified in place is decided from its reference count alone, so if the caller hashed the result and dropped it, a same-size write(), a write through getbuffer(), or truncate() followed by a refilling write overwrites it in place and keeps the old cached hash.
The same happens when the hashed object was passed to BytesIO(data) and data was dropped. Set and dict lookups with the result silently miss. This dates back to the copy-on-write buffer added in 3.5 (gh-66202). gh-158219 was the same problem in bytearray.
Activity
pythongh-160181: Don't modify a hashed bytes buffer in place in io.By…
gvanrossum commentedon Oct 22, 2026
Isn't someone (Serhiy?) planning to remove the hash?
vstinner commentedon Oct 22, 2026
PyBytesObject.ob_shashis deprecated since Python 3.11. I asked in #158074 if it should be removed or if the deprecation should be removed.gh-160181: Don't modify a hashed bytes buffer in place in io.BytesIO (G…