Repository navigation
io.BytesIO does not have peek() #90533
Description
Activity
It would be great to be able to use peek() on BytesIO objects.
I have a function that gets passed a file-like object and uses peek() on it. This works for nearly all types of files relevant in my library, except BytesIO instances (which I use during testing), for which I need to add a small workaround using tell() and seek().
- added3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Jan 14, 2022 I opened a PR, but I now wonder whether the missing peek() is by design.
First, I noticed that instead of using BytesIO directly, I can wrap the instance in an io.BufferedReader, which does have peek(). (It’s just a bit inconvenient.)
The second thing is that BytesIO is currently documented to inherit from BufferedIOBase, but if peek() is implemented, one could argue that BytesIO now should inherit from BufferedReader because it then has all the methods. And that seems to great a change from my perspective.
I’ll defer to someone more knowledgeable and do not mind at all if this issue is closed without action.
Correct me if I am wrong, but I think you want a
io.BufferedRandomobject? I am assuming you reached first forio.BytesIO, as I did, and found it was missing thepeekfunction.You are correct in that
BufferedReadersupportspeek- but this leads to a slightly strange asymmetrical design. I assume you wanted to write to this buffer as well as read from it?Assuming your situation is the same as mine - the right tool to use is
io.BufferedRandom. The slightly unusual name I suppose obscures its intended use.Hope that helps you? Of course feel free to ignore my comment if your situation is not as I have described.
Best wishes
- added a commit that references this issue
on Jun 26, 2026
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs