Skip to content

Commit 3c1b495

Browse files
gh-100226: Clarify StreamReader.read behavior (GH-101807)
(cherry picked from commit 77d95c8) Co-authored-by: Jan Gosmann <[email protected]>
1 parent b615678 commit 3c1b495

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Doc/library/asyncio-stream.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,20 @@ StreamReader
192192

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

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

197+
If *n* is not provided or set to ``-1``,
198+
read until EOF, then return all read :class:`bytes`.
198199
If EOF was received and the internal buffer is empty,
199200
return an empty ``bytes`` object.
200201

202+
If *n* is ``0``, return an empty ``bytes`` object immediately.
203+
204+
If *n* is positive, return at most *n* available ``bytes``
205+
as soon as at least 1 byte is available in the internal buffer.
206+
If EOF is received before any byte is read, return an empty
207+
``bytes`` object.
208+
201209
.. coroutinemethod:: readline()
202210

203211
Read one line, where "line" is a sequence of bytes

Lib/asyncio/streams.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,17 @@ async def readuntil(self, separator=b'\n'):
627627
async def read(self, n=-1):
628628
"""Read up to `n` bytes from the stream.
629629
630-
If n is not provided, or set to -1, read until EOF and return all read
631-
bytes. If the EOF was received and the internal buffer is empty, return
632-
an empty bytes object.
630+
If `n` is not provided or set to -1,
631+
read until EOF, then return all read bytes.
632+
If EOF was received and the internal buffer is empty,
633+
return an empty bytes object.
633634
634-
If n is zero, return empty bytes object immediately.
635+
If `n` is 0, return an empty bytes object immediately.
635636
636-
If n is positive, this function try to read `n` bytes, and may return
637-
less or equal bytes than requested, but at least one byte. If EOF was
638-
received before any byte is read, this function returns empty byte
639-
object.
637+
If `n` is positive, return at most `n` available bytes
638+
as soon as at least 1 byte is available in the internal buffer.
639+
If EOF is received before any byte is read, return an empty
640+
bytes object.
640641
641642
Returned value is not limited with limit, configured at stream
642643
creation.

0 commit comments

Comments
 (0)