]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-100226: Clarify StreamReader.read behavior (GH-101807) (#102001)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 23 Feb 2023 13:35:59 +0000 (05:35 -0800)
committerGitHub <noreply@github.com>
Thu, 23 Feb 2023 13:35:59 +0000 (19:05 +0530)
gh-100226: Clarify StreamReader.read behavior (GH-101807)
(cherry picked from commit 77d95c83733722ada35eb1ef89ae5b84a51ddd32)

Co-authored-by: Jan Gosmann <jan@hyper-world.de>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Doc/library/asyncio-stream.rst
Lib/asyncio/streams.py

index 74891306a63731a611be71f25c85394949a1d1da..e9d466d95e547e061d8156afde36bbb1fc17ad4b 100644 (file)
@@ -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`.
       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
index d21a31d1860d91ece36391126042f6b7bbfe80b5..560ad8a8510e55c04d351a6059494b36759c3994 100644 (file)
@@ -648,16 +648,17 @@ class StreamReader:
     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.