Skip to content

Commit 0918ad4

Browse files
miss-islingtoncmaloneyserhiy-storchakavstinner
authored
[3.13] gh-129011: Update docs for Raw I/O read, readinto, and write (GH-135328) (#150959)
gh-129011: Update docs for Raw I/O read, readinto, and write (GH-135328) Update `RawIOBase` and `FileIO` documentation to match implementation behavior around `.read`, `.readinto`, `.readall` and `.write`. In particular: - They may make more than one system call (PEP-475) - Add warnings if `.write()` requires a wrapping retry loop (see: gh-126606) - "Raw I/O" `.write`` may not write all bytes - `buffering=0` example results in a "Raw I/O" (cherry picked from commit e4db68b) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 6825032 commit 0918ad4

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

Doc/library/io.rst

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ will raise a :exc:`TypeError`. So will giving a :class:`bytes` object to the
4646
Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since
4747
:exc:`IOError` is now an alias of :exc:`OSError`.
4848

49+
.. _text-io:
4950

5051
Text I/O
5152
^^^^^^^^
@@ -67,6 +68,7 @@ In-memory text streams are also available as :class:`StringIO` objects::
6768
The text stream API is described in detail in the documentation of
6869
:class:`TextIOBase`.
6970

71+
.. _binary-io:
7072

7173
Binary I/O
7274
^^^^^^^^^^
@@ -105,6 +107,13 @@ stream by opening a file in binary mode with buffering disabled::
105107

106108
The raw stream API is described in detail in the docs of :class:`RawIOBase`.
107109

110+
.. warning::
111+
Raw I/O is a low-level interface and methods generally must have their return
112+
values checked and be explicitly retried to ensure an operation completes.
113+
For instance :meth:`~RawIOBase.write` returns the number of bytes written
114+
which may be less than the number of bytes provided (a partial write).
115+
High-level I/O objects like :ref:`binary-io` and :ref:`text-io` implement
116+
retry behavior.
108117

109118
.. _io-text-encoding:
110119

@@ -480,8 +489,11 @@ I/O Base Classes
480489

481490
Read up to *size* bytes from the object and return them. As a convenience,
482491
if *size* is unspecified or -1, all bytes until EOF are returned.
483-
Otherwise, only one system call is ever made. Fewer than *size* bytes may
484-
be returned if the operating system call returns fewer than *size* bytes.
492+
493+
Attempts to make only one system call but will retry if interrupted and
494+
the signal handler does not raise an exception (see :pep:`475` for the
495+
rationale). This means fewer than *size* bytes may be returned if the
496+
operating system call returns fewer than *size* bytes.
485497

486498
If 0 bytes are returned, and *size* was not 0, this indicates end of file.
487499
If the object is in non-blocking mode and no bytes are available,
@@ -495,13 +507,19 @@ I/O Base Classes
495507
Read and return all the bytes from the stream until EOF, using multiple
496508
calls to the stream if necessary.
497509

510+
If ``0`` bytes are returned this indicates end of file. If the object is in
511+
non-blocking mode and the underlying :meth:`read` returns ``None``
512+
indicating no bytes are available, ``None`` is returned.
513+
498514
.. method:: readinto(b, /)
499515

500516
Read bytes into a pre-allocated, writable
501517
:term:`bytes-like object` *b*, and return the
502518
number of bytes read. For example, *b* might be a :class:`bytearray`.
503-
If the object is in non-blocking mode and no bytes
504-
are available, ``None`` is returned.
519+
520+
If ``0`` is returned and ``len(b)`` is not ``0``, this indicates end of file. If
521+
the object is in non-blocking mode and no bytes are available, ``None`` is
522+
returned.
505523

506524
.. method:: write(b, /)
507525

@@ -515,6 +533,13 @@ I/O Base Classes
515533
this method returns, so the implementation should only access *b*
516534
during the method call.
517535

536+
.. warning::
537+
538+
This function does not ensure all bytes are written or an exception is
539+
thrown. Callers may implement that behavior by checking the return
540+
value and, if it is less than the length of *b*, looping with additional
541+
write calls until all unwritten bytes are written. High-level I/O
542+
objects like :ref:`binary-io` and :ref:`text-io` implement retry behavior.
518543

519544
.. class:: BufferedIOBase
520545

@@ -643,7 +668,11 @@ Raw File I/O
643668
.. class:: FileIO(name, mode='r', closefd=True, opener=None)
644669

645670
A raw binary stream representing an OS-level file containing bytes data. It
646-
inherits from :class:`RawIOBase`.
671+
inherits from :class:`RawIOBase` and implements its low-level access design.
672+
This means :meth:`~RawIOBase.write` does not guarantee all bytes are written
673+
and :meth:`~RawIOBase.read` may read less bytes than requested even when more
674+
bytes may be present in the underlying file. To get "write all" and
675+
"read at least" behavior, use :ref:`binary-io`.
647676

648677
The *name* can be one of two things:
649678

@@ -663,10 +692,6 @@ Raw File I/O
663692
implies writing, so this mode behaves in a similar way to ``'w'``. Add a
664693
``'+'`` to the mode to allow simultaneous reading and writing.
665694

666-
The :meth:`~RawIOBase.read` (when called with a positive argument),
667-
:meth:`~RawIOBase.readinto` and :meth:`~RawIOBase.write` methods on this
668-
class will only make one system call.
669-
670695
A custom opener can be used by passing a callable as *opener*. The underlying
671696
file descriptor for the file object is then obtained by calling *opener* with
672697
(*name*, *flags*). *opener* must return an open file descriptor (passing
@@ -678,6 +703,13 @@ Raw File I/O
678703
See the :func:`open` built-in function for examples on using the *opener*
679704
parameter.
680705

706+
.. warning::
707+
:class:`FileIO` is a low-level I/O object and members, such as
708+
:meth:`~RawIOBase.read` and :meth:`~RawIOBase.write`, need to have their
709+
return values checked explicitly in a retry loop to implement "write all"
710+
and "read at least" behavior. High-level I/O objects :ref:`binary-io` and
711+
:ref:`text-io` implement retry behavior.
712+
681713
.. versionchanged:: 3.3
682714
The *opener* parameter was added.
683715
The ``'x'`` mode was added.

0 commit comments

Comments
 (0)