Skip to content

Commit 5ecc39e

Browse files
gpsheadcmaloney
andauthored
[3.13] gh-80050: Update BufferedReader.read docs around non-blocking (GH-130653) (#134445)
(cherry picked from commit e1f8914) Co-authored-by: Cody Maloney <[email protected]>
1 parent 2d11f3e commit 5ecc39e

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

Doc/library/io.rst

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,13 @@ I/O Base Classes
522522
It inherits from :class:`IOBase`.
523523

524524
The main difference with :class:`RawIOBase` is that methods :meth:`read`,
525-
:meth:`readinto` and :meth:`write` will try (respectively) to read as much
526-
input as requested or to consume all given output, at the expense of
527-
making perhaps more than one system call.
525+
:meth:`readinto` and :meth:`write` will try (respectively) to read
526+
as much input as requested or to emit all provided data.
528527

529-
In addition, those methods can raise :exc:`BlockingIOError` if the
530-
underlying raw stream is in non-blocking mode and cannot take or give
531-
enough data; unlike their :class:`RawIOBase` counterparts, they will
532-
never return ``None``.
528+
In addition, if the underlying raw stream is in non-blocking mode, when the
529+
system returns would block :meth:`write` will raise :exc:`BlockingIOError`
530+
with :attr:`BlockingIOError.characters_written` and :meth:`read` will return
531+
data read so far or ``None`` if no data is available.
533532

534533
Besides, the :meth:`read` method does not have a default
535534
implementation that defers to :meth:`readinto`.
@@ -562,29 +561,40 @@ I/O Base Classes
562561

563562
.. method:: read(size=-1, /)
564563

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

569-
If the argument is positive, and the underlying raw stream is not
570-
interactive, multiple raw reads may be issued to satisfy the byte count
571-
(unless EOF is reached first). But for interactive raw streams, at most
572-
one raw read will be issued, and a short result does not imply that EOF is
573-
imminent.
567+
Fewer bytes may be returned than requested. An empty :class:`bytes` object
568+
is returned if the stream is already at EOF. More than one read may be
569+
made and calls may be retried if specific errors are encountered, see
570+
:meth:`os.read` and :pep:`475` for more details. Less than size bytes
571+
being returned does not imply that EOF is imminent.
574572

575-
A :exc:`BlockingIOError` is raised if the underlying raw stream is in
576-
non blocking-mode, and has no data available at the moment.
573+
When reading as much as possible the default implementation will use
574+
``raw.readall`` if available (which should implement
575+
:meth:`RawIOBase.readall`), otherwise will read in a loop until read
576+
returns ``None``, an empty :class:`bytes`, or a non-retryable error. For
577+
most streams this is to EOF, but for non-blocking streams more data may
578+
become available.
579+
580+
.. note::
581+
582+
When the underlying raw stream is non-blocking, implementations may
583+
either raise :exc:`BlockingIOError` or return ``None`` if no data is
584+
available. :mod:`io` implementations return ``None``.
577585

578586
.. method:: read1(size=-1, /)
579587

580-
Read and return up to *size* bytes, with at most one call to the
581-
underlying raw stream's :meth:`~RawIOBase.read` (or
582-
:meth:`~RawIOBase.readinto`) method. This can be useful if you are
583-
implementing your own buffering on top of a :class:`BufferedIOBase`
584-
object.
588+
Read and return up to *size* bytes, calling :meth:`~RawIOBase.readinto`
589+
which may retry if :py:const:`~errno.EINTR` is encountered per
590+
:pep:`475`. If *size* is ``-1`` or not provided, the implementation will
591+
choose an arbitrary value for *size*.
592+
593+
.. note::
585594

586-
If *size* is ``-1`` (the default), an arbitrary number of bytes are
587-
returned (more than zero unless EOF is reached).
595+
When the underlying raw stream is non-blocking, implementations may
596+
either raise :exc:`BlockingIOError` or return ``None`` if no data is
597+
available. :mod:`io` implementations return ``None``.
588598

589599
.. method:: readinto(b, /)
590600

@@ -761,20 +771,17 @@ than raw I/O does.
761771

762772
.. method:: peek(size=0, /)
763773

764-
Return bytes from the stream without advancing the position. At most one
765-
single read on the raw stream is done to satisfy the call. The number of
766-
bytes returned may be less or more than requested.
774+
Return bytes from the stream without advancing the position. The number of
775+
bytes returned may be less or more than requested. If the underlying raw
776+
stream is non-blocking and the operation would block, returns empty bytes.
767777

768778
.. method:: read(size=-1, /)
769779

770-
Read and return *size* bytes, or if *size* is not given or negative, until
771-
EOF or if the read call would block in non-blocking mode.
780+
In :class:`BufferedReader` this is the same as :meth:`io.BufferedIOBase.read`
772781

773782
.. method:: read1(size=-1, /)
774783

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

779786
.. versionchanged:: 3.7
780787
The *size* argument is now optional.
@@ -811,8 +818,8 @@ than raw I/O does.
811818

812819
Write the :term:`bytes-like object`, *b*, and return the
813820
number of bytes written. When in non-blocking mode, a
814-
:exc:`BlockingIOError` is raised if the buffer needs to be written out but
815-
the raw stream blocks.
821+
:exc:`BlockingIOError` with :attr:`BlockingIOError.characters_written` set
822+
is raised if the buffer needs to be written out but the raw stream blocks.
816823

817824

818825
.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)

0 commit comments

Comments
 (0)