Skip to content

BufferedIOBase.readinto raises TypeError when called on a non-blocking streams block with no data #162031

Open

Description

@cmaloney

Bug report

From PEP-3116 non-blocking should return None in this case (https://peps.python.org/pep-3116/#non-blocking-i-o). Goal for fixing this now is to normalize what happens between .read and .readinto; TypeError is definitely outside of the documented behavior.

import io, _pyio, os

class Reader(io.BufferedIOBase):
    def read(self, size=-1):
        return None  # non-blocking, no data available

r, w = os.pipe()
os.set_blocking(r, False)
print(io.open(r, 'rb', closefd=False).readinto(bytearray(1)))     # None
print(_pyio.open(r, 'rb', closefd=False).readinto(bytearray(1)))  # 0
Reader().readinto(bytearray(1))  # TypeError: read() should return bytes

This exact fix was proposed in 2015 in nonblock-none.patch on gh-57531, but it never landed as that touched many other broad questsions as well. gh-109523 fixed the same kind of bug in TextIOWrapper: a would-block read should never surface as TypeError.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

topic-IOtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions