Skip to content

gh-158219: Clear the cached hash when bytearray adopts a bytes buffer - #158222

Closed
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:gh-158219-bytearray-stale-hash
Closed

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:gh-158219-bytearray-stale-hash

Conversation

@cmaloney

@cmaloney cmaloney commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Change so mutable bytes adopted by bytearray have their hash cleared. Ensure that bytes which are considerable mutable have a clear hash so it is computed only after they are immutable.

…buffer

Change so mortal bytes adopted by `bytearray` have their hash
cleared. Ensure that bytes which are considerable mutable have a clear
hash so it is computed only after they are immutable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cmaloney
cmaloney requested a review from vstinner September 26, 2026 06:47
@cmaloney cmaloney added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 26, 2026
@vstinner

Copy link
Copy Markdown
Member

Use a bytes object if its hash value was already computed sounds risky to me. Clearing its hash value might just hide bugs.

I propose PR gh-158329 to not use a bytes object if its hash value is already computed.

Another approach would be to remove the bytearray optimization to reuse a bytes object if its refcount is 1: always create a copy.

@cmaloney cmaloney closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants