gh-155389: Return bytes from _pyio.BytesIO.peek() - #155390
Merged
serhiy-storchaka merged 2 commits intoAug 16, 2026
Merged
Conversation
peek() returned a slice of the internal bytearray, where read() converts with take_bytes(). It also did not coerce its size through __index__ and did not hold the lock while slicing, both of which read() and the C implementation do.
Contributor
cmaloney
reviewed
Aug 8, 2026
| size = size_index() | ||
|
|
||
| with self._lock: | ||
| if size < 1: |
Contributor
There was a problem hiding this comment.
I don't think the size piece here needs to be in the lock
Contributor
Author
There was a problem hiding this comment.
Done, thanks. Only the slicing is under the lock now.
Member
|
It's better to add a test to check if |
The size normalisation touches no shared state, so it does not need the lock.
aisk
approved these changes
Aug 9, 2026
cmaloney
approved these changes
Aug 15, 2026
Contributor
|
for news, falls under the existing news add in: 72cad14. This is just refining behavior. |
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.
_pyio.BytesIO.peek()returned a slice of the internalbytearray, so it gave abytearraywhere the C version and the docs saybytes. It also did not accept an__index__-able size, and did not take the lock while slicing.It now does what
read()does: coerce the size, take the lock, convert withtake_bytes().test_peekonly usedassertEqual, andbytearray(b'1') == b'1', so it passed either way. It now checks the type and an__index__size, and fails without the change.BytesIO.peek()is new in 3.16, so there is no NEWS entry._pyio.BytesIO.peek()returns bytearray instead of bytes #155389