Skip to content

Commit 0610219

Browse files
committed
pythongh-80050: Update BufferedReader.read around non-blocking
Synchronize `io.BufferedReader` and `io.BufferedIOBase` documentation with the current implementation. PArticular focus on behavior around non-blocking streams.
1 parent 043ab3a commit 0610219

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

Doc/library/io.rst

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,19 @@ I/O Base Classes
568568

569569
.. method:: read(size=-1, /)
570570

571-
Read and return up to *size* bytes. If the argument is omitted, ``None``,
572-
or negative, data is read and returned until EOF is reached. An empty
573-
:class:`bytes` object is returned if the stream is already at EOF.
571+
Read and return up to *size* bytes. If the argument is omitted, ``None``,
572+
or negative read as much as possible.
574573

575-
If the argument is positive, and the underlying raw stream is not
576-
interactive, multiple raw reads may be issued to satisfy the byte count
577-
(unless EOF is reached first). But for interactive raw streams, at most
578-
one raw read will be issued, and a short result does not imply that EOF is
579-
imminent.
574+
Reading as much as possible will use :meth:`~raw.readall` if available,
575+
otherwise will read in a loop until read returns ``None``, a size-zero
576+
``bytes``, or a non-retryable error. For most streams this is to EOF, but
577+
for non-blocking streams more data may become available.
578+
579+
Less bytes may be returned than requested. An empty :class:`bytes` object
580+
is returned if the stream is already at EOF. More than one read may be
581+
made, and calls may be retried if specific errors (ex. :py:const:`errno.EINTR`).
582+
See :meth:`os.read` and :pep:`475` for more details. Less than size bytes
583+
being returned does not imply that EOF is imminent.
580584

581585
A :exc:`BlockingIOError` is raised if the underlying raw stream is in
582586
non blocking-mode, and has no data available at the moment.
@@ -592,6 +596,11 @@ I/O Base Classes
592596
If *size* is ``-1`` (the default), an arbitrary number of bytes are
593597
returned (more than zero unless EOF is reached).
594598

599+
.. note::
600+
601+
When the underlying raw stream is non-blocking, a :exc:`BlockingIOError`
602+
may be raised if a read operation cannot be completed immediately.
603+
595604
.. method:: readinto(b, /)
596605

597606
Read bytes into a pre-allocated, writable
@@ -737,14 +746,14 @@ than raw I/O does.
737746

738747
.. method:: read1(size=-1, /)
739748

740-
In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.read`.
749+
In :class:`BytesIO`, this is the same as :meth:`io.BufferedIOBase.read`.
741750

742751
.. versionchanged:: 3.7
743752
The *size* argument is now optional.
744753

745754
.. method:: readinto1(b, /)
746755

747-
In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.readinto`.
756+
In :class:`BytesIO`, this is the same as :meth:`io.BufferedIOBase.readinto`.
748757

749758
.. versionadded:: 3.5
750759

@@ -767,34 +776,21 @@ than raw I/O does.
767776

768777
.. method:: peek(size=0, /)
769778

770-
Return bytes from the stream without advancing the position. At most one
771-
single read on the raw stream is done to satisfy the call. The number of
772-
bytes returned may be less or more than requested.
779+
Return bytes from the stream without advancing the position. The number of
780+
bytes returned may be less or more than requested. If the underlying raw
781+
stream is non-blocking and the operation would block, returns empty bytes.
773782

774783
.. method:: read(size=-1, /)
775784

776-
Read and return *size* bytes, or if *size* is not given or negative, until
777-
EOF or if the read call would block in non-blocking mode.
778-
779-
.. note::
780-
781-
When the underlying raw stream is non-blocking, a :exc:`BlockingIOError`
782-
may be raised if a read operation cannot be completed immediately.
785+
In :class:`BufferedReader` this is the same as :meth:`io.BufferedIOBase.read`
783786

784787
.. method:: read1(size=-1, /)
785788

786-
Read and return up to *size* bytes with only one call on the raw stream.
787-
If at least one byte is buffered, only buffered bytes are returned.
788-
Otherwise, one raw stream read call is made.
789+
In :class:`BufferedReader` this is the same as :meth:`io.BufferedIOBase.read1`
789790

790791
.. versionchanged:: 3.7
791792
The *size* argument is now optional.
792793

793-
.. note::
794-
795-
When the underlying raw stream is non-blocking, a :exc:`BlockingIOError`
796-
may be raised if a read operation cannot be completed immediately.
797-
798794
.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
799795

800796
A buffered binary stream providing higher-level access to a writeable, non
@@ -856,7 +852,7 @@ than raw I/O does.
856852
:data:`DEFAULT_BUFFER_SIZE`.
857853

858854
:class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
859-
except for :meth:`~BufferedIOBase.detach`, which raises
855+
except for :meth:`io.BufferedIOBase.detach`, which raises
860856
:exc:`UnsupportedOperation`.
861857

862858
.. warning::
@@ -1006,8 +1002,8 @@ Text I/O
10061002
If *line_buffering* is ``True``, :meth:`~IOBase.flush` is implied when a call to
10071003
write contains a newline character or a carriage return.
10081004

1009-
If *write_through* is ``True``, calls to :meth:`~BufferedIOBase.write` are guaranteed
1010-
not to be buffered: any data written on the :class:`TextIOWrapper`
1005+
If *write_through* is ``True``, calls to :meth:`io.BufferedIOBase.write` are
1006+
guaranteed not to be buffered: any data written on the :class:`TextIOWrapper`
10111007
object is immediately handled to its underlying binary *buffer*.
10121008

10131009
.. versionchanged:: 3.3

0 commit comments

Comments
 (0)