@@ -38,6 +38,7 @@ will raise a :exc:`TypeError`. So will giving a :class:`bytes` object to the
3838 Operations that used to raise :exc: `IOError ` now raise :exc: `OSError `, since
3939 :exc: `IOError ` is now an alias of :exc: `OSError `.
4040
41+ .. _text-io :
4142
4243Text I/O
4344^^^^^^^^
@@ -65,6 +66,7 @@ In-memory text streams are also available as :class:`StringIO` objects::
6566The text stream API is described in detail in the documentation of
6667:class: `TextIOBase `.
6768
69+ .. _binary-io :
6870
6971Binary I/O
7072^^^^^^^^^^
@@ -103,6 +105,13 @@ stream by opening a file in binary mode with buffering disabled::
103105
104106The raw stream API is described in detail in the docs of :class: `RawIOBase `.
105107
108+ .. warning ::
109+ Raw I/O is a low-level interface and methods generally must have their return
110+ values checked and be explicitly retried to ensure an operation completes.
111+ For instance :meth: `~RawIOBase.write ` returns the number of bytes written
112+ which may be less than the number of bytes provided (a partial write).
113+ High-level I/O objects like :ref: `binary-io ` and :ref: `text-io ` implement
114+ retry behavior.
106115
107116.. _io-text-encoding :
108117
@@ -478,8 +487,11 @@ I/O Base Classes
478487
479488 Read up to *size * bytes from the object and return them. As a convenience,
480489 if *size * is unspecified or -1, all bytes until EOF are returned.
481- Otherwise, only one system call is ever made. Fewer than *size * bytes may
482- be returned if the operating system call returns fewer than *size * bytes.
490+
491+ Attempts to make only one system call but will retry if interrupted and
492+ the signal handler does not raise an exception (see :pep: `475 ` for the
493+ rationale). This means fewer than *size * bytes may be returned if the
494+ operating system call returns fewer than *size * bytes.
483495
484496 If 0 bytes are returned, and *size * was not 0, this indicates end of file.
485497 If the object is in non-blocking mode and no bytes are available,
@@ -493,13 +505,19 @@ I/O Base Classes
493505 Read and return all the bytes from the stream until EOF, using multiple
494506 calls to the stream if necessary.
495507
508+ If ``0 `` bytes are returned this indicates end of file. If the object is in
509+ non-blocking mode and the underlying :meth: `read ` returns ``None ``
510+ indicating no bytes are available, ``None `` is returned.
511+
496512 .. method :: readinto(b, /)
497513
498514 Read bytes into a pre-allocated, writable
499515 :term: `bytes-like object ` *b *, and return the
500516 number of bytes read. For example, *b * might be a :class: `bytearray `.
501- If the object is in non-blocking mode and no bytes
502- are available, ``None `` is returned.
517+
518+ If ``0 `` is returned and ``len(b) `` is not ``0 ``, this indicates end of file. If
519+ the object is in non-blocking mode and no bytes are available, ``None `` is
520+ returned.
503521
504522 .. method :: write(b, /)
505523
@@ -513,6 +531,13 @@ I/O Base Classes
513531 this method returns, so the implementation should only access *b *
514532 during the method call.
515533
534+ .. warning ::
535+
536+ This function does not ensure all bytes are written or an exception is
537+ thrown. Callers may implement that behavior by checking the return
538+ value and, if it is less than the length of *b *, looping with additional
539+ write calls until all unwritten bytes are written. High-level I/O
540+ objects like :ref: `binary-io ` and :ref: `text-io ` implement retry behavior.
516541
517542.. class :: BufferedIOBase
518543
@@ -641,7 +666,11 @@ Raw File I/O
641666.. class :: FileIO(name, mode='r', closefd=True, opener=None)
642667
643668 A raw binary stream representing an OS-level file containing bytes data. It
644- inherits from :class: `RawIOBase `.
669+ inherits from :class: `RawIOBase ` and implements its low-level access design.
670+ This means :meth: `~RawIOBase.write ` does not guarantee all bytes are written
671+ and :meth: `~RawIOBase.read ` may read less bytes than requested even when more
672+ bytes may be present in the underlying file. To get "write all" and
673+ "read at least" behavior, use :ref: `binary-io `.
645674
646675 The *name * can be one of two things:
647676
@@ -661,10 +690,6 @@ Raw File I/O
661690 implies writing, so this mode behaves in a similar way to ``'w' ``. Add a
662691 ``'+' `` to the mode to allow simultaneous reading and writing.
663692
664- The :meth: `~RawIOBase.read ` (when called with a positive argument),
665- :meth: `~RawIOBase.readinto ` and :meth: `~RawIOBase.write ` methods on this
666- class will only make one system call.
667-
668693 A custom opener can be used by passing a callable as *opener *. The underlying
669694 file descriptor for the file object is then obtained by calling *opener * with
670695 (*name *, *flags *). *opener * must return an open file descriptor (passing
@@ -676,6 +701,13 @@ Raw File I/O
676701 See the :func: `open ` built-in function for examples on using the *opener *
677702 parameter.
678703
704+ .. warning ::
705+ :class: `FileIO ` is a low-level I/O object and members, such as
706+ :meth: `~RawIOBase.read ` and :meth: `~RawIOBase.write `, need to have their
707+ return values checked explicitly in a retry loop to implement "write all"
708+ and "read at least" behavior. High-level I/O objects :ref: `binary-io ` and
709+ :ref: `text-io ` implement retry behavior.
710+
679711 .. versionchanged :: 3.3
680712 The *opener * parameter was added.
681713 The ``'x' `` mode was added.
0 commit comments