Skip to content

Commit 77d95c8

Browse files
authored
gh-100226: Clarify StreamReader.read behavior (#101807)
1 parent a1723ca commit 77d95c8

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
@@ -206,12 +206,20 @@ StreamReader
206206

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

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

211+
If *n* is not provided or set to ``-1``,
212+
read until EOF, then return all read :class:`bytes`.
212213
If EOF was received and the internal buffer is empty,
213214
return an empty ``bytes`` object.
214215

216+
If *n* is ``0``, return an empty ``bytes`` object immediately.
217+
218+
If *n* is positive, return at most *n* available ``bytes``
219+
as soon as at least 1 byte is available in the internal buffer.
220+
If EOF is received before any byte is read, return an empty
221+
``bytes`` object.
222+
215223
.. coroutinemethod:: readline()
216224

217225
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
@@ -649,16 +649,17 @@ async def readuntil(self, separator=b'\n'):
649649
async def read(self, n=-1):
650650
"""Read up to `n` bytes from the stream.
651651
652-
If n is not provided, or set to -1, read until EOF and return all read
653-
bytes. If the EOF was received and the internal buffer is empty, return
654-
an empty bytes object.
652+
If `n` is not provided or set to -1,
653+
read until EOF, then return all read bytes.
654+
If EOF was received and the internal buffer is empty,
655+
return an empty bytes object.
655656
656-
If n is zero, return empty bytes object immediately.
657+
If `n` is 0, return an empty bytes object immediately.
657658
658-
If n is positive, this function try to read `n` bytes, and may return
659-
less or equal bytes than requested, but at least one byte. If EOF was
660-
received before any byte is read, this function returns empty byte
661-
object.
659+
If `n` is positive, return at most `n` available bytes
660+
as soon as at least 1 byte is available in the internal buffer.
661+
If EOF is received before any byte is read, return an empty
662+
bytes object.
662663
663664
Returned value is not limited with limit, configured at stream
664665
creation.

0 commit comments

Comments
 (0)