Skip to content

Commit 42d0ca9

Browse files
miss-islingtonjgosmannhauntsaninja
authored
[3.11] gh-100226: Clarify StreamReader.read behavior (GH-101807) (#102001)
gh-100226: Clarify StreamReader.read behavior (GH-101807) (cherry picked from commit 77d95c8) Co-authored-by: Jan Gosmann <[email protected]> Co-authored-by: Shantanu <[email protected]>
1 parent bf0a836 commit 42d0ca9

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

0 commit comments

Comments
 (0)