Repository navigation
gh-160181: Don't modify a hashed bytes buffer in place in io.BytesIO - #160182
Merged
Merged
Conversation
gpshead
marked this pull request as ready for review
October 22, 2026 04:26
Member
Author
|
The behavior here feels a little sus. This seems better... but bytes. immutable. overwriting. scary. |
…tesIO Treat the internal buffer as shared once its hash is cached, so the next modification copies it, as pythongh-158219 did for bytearray.
gpshead
force-pushed
the
bytesio-getvalue-stale-hash
branch
from
October 22, 2026 04:44
c720598 to
6965755
Compare
Member
Member
|
cc @cmaloney |
cmaloney
reviewed
Oct 22, 2026
cmaloney
left a comment
Contributor
There was a problem hiding this comment.
It would be good to add a _PyBytes_IsMutable assertion in write_bytes_lock_held to validate we've got all the cases here. For the previous fixes to bytearray allowing mutating immutable things found an underlying _PyBytes_Resize issue by adding that shape check.
(resize_unshared_buffer_lock_held doesn't need one because it uses _PyBytes_ResizeKeepOnError which already does)
cmaloney
approved these changes
Oct 22, 2026
gpshead
enabled auto-merge (squash)
October 22, 2026 22:25
vstinner
approved these changes
Oct 22, 2026
|
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-160408 is a backport of this pull request to the 3.15 branch. |
|
GH-160409 is a backport of this pull request to the 3.14 branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Treat the internal buffer as shared once its hash is cached, so the next write,
getbuffer()export or resize copies it instead of modifying abytesobject that may already be in a set or dict.SHARED_BUF()is the copy-on-write check every modifying path uses, so this covers all of them on both the default and free-threaded builds. The fix for gh-158219 did the same forbytearray.Fixes #160181
ANT-2026-IZN6JY41