Skip to content

gh-100226: Clarify StreamReader.read behavior #101807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Doc/library/asyncio-stream.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,20 @@ StreamReader

.. coroutinemethod:: read(n=-1)

Read up to *n* bytes. If *n* is not provided, or set to ``-1``,
read until EOF and return all read bytes.
Read up to *n* bytes from the stream.

If *n* is not provided or set to ``-1``,
read until EOF, then return all read :class:`bytes`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a beginning reader, before having read @jgosmann 's detailed explaination and @gvanrossum 's summary, I was evidently confused here about what "until EOF" specifically meant in this context. It therefore might warrant some more clarity here, but maybe its just my lack of subject matter expertise compared with the target audience.

If EOF was received and the internal buffer is empty,
return an empty ``bytes`` object.

If *n* is ``0``, return an empty ``bytes`` object immediately.

If *n* is positive, return at most *n* available ``bytes``
as soon as at least 1 byte is available in the internal buffer.
If EOF is received before any byte is read, return an empty
``bytes`` object.

.. coroutinemethod:: readline()

Read one line, where "line" is a sequence of bytes
Expand Down
17 changes: 9 additions & 8 deletions Lib/asyncio/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,16 +649,17 @@ async def readuntil(self, separator=b'\n'):
async def read(self, n=-1):
"""Read up to `n` bytes from the stream.

If n is not provided, or set to -1, read until EOF and return all read
bytes. If the EOF was received and the internal buffer is empty, return
an empty bytes object.
If `n` is not provided or set to -1,
read until EOF, then return all read bytes.
If EOF was received and the internal buffer is empty,
return an empty bytes object.

If n is zero, return empty bytes object immediately.
If `n` is 0, return an empty bytes object immediately.

If n is positive, this function try to read `n` bytes, and may return
less or equal bytes than requested, but at least one byte. If EOF was
received before any byte is read, this function returns empty byte
object.
If `n` is positive, return at most `n` available bytes
as soon as at least 1 byte is available in the internal buffer.
If EOF is received before any byte is read, return an empty
bytes object.

Returned value is not limited with limit, configured at stream
creation.
Expand Down