Operations that used to raise :exc:`IOError` now raise :exc:`OSError`, since
:exc:`IOError` is now an alias of :exc:`OSError`.
+.. _text-io:
Text I/O
^^^^^^^^
The text stream API is described in detail in the documentation of
:class:`TextIOBase`.
+.. _binary-io:
Binary I/O
^^^^^^^^^^
The raw stream API is described in detail in the docs of :class:`RawIOBase`.
+.. warning::
+ Raw I/O is a low-level interface and methods generally must have their return
+ values checked and be explicitly retried to ensure an operation completes.
+ For instance :meth:`~RawIOBase.write` returns the number of bytes written
+ which may be less than the number of bytes provided (a partial write).
+ High-level I/O objects like :ref:`binary-io` and :ref:`text-io` implement
+ retry behavior.
.. _io-text-encoding:
Read up to *size* bytes from the object and return them. As a convenience,
if *size* is unspecified or -1, all bytes until EOF are returned.
- Otherwise, only one system call is ever made. Fewer than *size* bytes may
- be returned if the operating system call returns fewer than *size* bytes.
+
+ Attempts to make only one system call but will retry if interrupted and
+ the signal handler does not raise an exception (see :pep:`475` for the
+ rationale). This means fewer than *size* bytes may be returned if the
+ operating system call returns fewer than *size* bytes.
If 0 bytes are returned, and *size* was not 0, this indicates end of file.
If the object is in non-blocking mode and no bytes are available,
Read and return all the bytes from the stream until EOF, using multiple
calls to the stream if necessary.
+ If ``0`` bytes are returned this indicates end of file. If the object is in
+ non-blocking mode and the underlying :meth:`read` returns ``None``
+ indicating no bytes are available, ``None`` is returned.
+
.. method:: readinto(b, /)
Read bytes into a pre-allocated, writable
:term:`bytes-like object` *b*, and return the
number of bytes read. For example, *b* might be a :class:`bytearray`.
- If the object is in non-blocking mode and no bytes
- are available, ``None`` is returned.
+
+ If ``0`` is returned and ``len(b)`` is not ``0``, this indicates end of file. If
+ the object is in non-blocking mode and no bytes are available, ``None`` is
+ returned.
.. method:: write(b, /)
this method returns, so the implementation should only access *b*
during the method call.
+ .. warning::
+
+ This function does not ensure all bytes are written or an exception is
+ thrown. Callers may implement that behavior by checking the return
+ value and, if it is less than the length of *b*, looping with additional
+ write calls until all unwritten bytes are written. High-level I/O
+ objects like :ref:`binary-io` and :ref:`text-io` implement retry behavior.
.. class:: BufferedIOBase
.. class:: FileIO(name, mode='r', closefd=True, opener=None)
A raw binary stream representing an OS-level file containing bytes data. It
- inherits from :class:`RawIOBase`.
+ inherits from :class:`RawIOBase` and implements its low-level access design.
+ This means :meth:`~RawIOBase.write` does not guarantee all bytes are written
+ and :meth:`~RawIOBase.read` may read less bytes than requested even when more
+ bytes may be present in the underlying file. To get "write all" and
+ "read at least" behavior, use :ref:`binary-io`.
The *name* can be one of two things:
implies writing, so this mode behaves in a similar way to ``'w'``. Add a
``'+'`` to the mode to allow simultaneous reading and writing.
- The :meth:`~RawIOBase.read` (when called with a positive argument),
- :meth:`~RawIOBase.readinto` and :meth:`~RawIOBase.write` methods on this
- class will only make one system call.
-
A custom opener can be used by passing a callable as *opener*. The underlying
file descriptor for the file object is then obtained by calling *opener* with
(*name*, *flags*). *opener* must return an open file descriptor (passing
See the :func:`open` built-in function for examples on using the *opener*
parameter.
+ .. warning::
+ :class:`FileIO` is a low-level I/O object and members, such as
+ :meth:`~RawIOBase.read` and :meth:`~RawIOBase.write`, need to have their
+ return values checked explicitly in a retry loop to implement "write all"
+ and "read at least" behavior. High-level I/O objects :ref:`binary-io` and
+ :ref:`text-io` implement retry behavior.
+
.. versionchanged:: 3.3
The *opener* parameter was added.
The ``'x'`` mode was added.