Skip to content

io.BytesIO does not have peek() #90533

Description

@marcelm
mannequin
BPO 46375
Nosy @benjaminp, @marcelm, @kumaraditya303
PRs
  • gh-90533: Implement BytesIO.peek() #30808
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2022-01-14.10:39:24.835>
    labels = ['type-feature', 'library', '3.11']
    title = 'io.BytesIO does not have peek()'
    updated_at = <Date 2022-01-22.22:08:29.533>
    user = 'https://github.com/marcelm'

    bugs.python.org fields:

    activity = <Date 2022-01-22.22:08:29.533>
    actor = 'marcelm'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-01-14.10:39:24.835>
    creator = 'marcelm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46375
    keywords = ['patch']
    message_count = 2.0
    messages = ['410552', '411294']
    nosy_count = 4.0
    nosy_names = ['benjamin.peterson', 'stutzbach', 'marcelm', 'kumaraditya']
    pr_nums = ['30808']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46375'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. marcelm commented on Jan 14, 2022

      marcelmmannequin
      MannequinAuthor

      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().

    2. added
      3.11only security fixes
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Jan 14, 2022
    3. marcelm commented on Jan 22, 2022

      marcelmmannequin
      MannequinAuthor

      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.

    4. transferred this issue fromon Apr 10, 2022
    5. edward-b-1 commented on Dec 1, 2024

      @edward-b-1

      Correct me if I am wrong, but I think you want a io.BufferedRandom object? I am assuming you reached first for io.BytesIO, as I did, and found it was missing the peek function.

      You are correct in that BufferedReader supports peek - 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

    6. added a commit that references this issue on Jun 26, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-IOtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions