uio – Input/output streams
This module implements a subset of the corresponding CPython module, as described below. For more information, please refer to the original CPython documentation: io
This module contains additional types of stream (file-like) objects and helper functions.
Conceptual hierarchy
Differences from CPython
As described in this section, the conceptual hierarchy of stream base classes is simplified in MicroPython.
(Summary) The base stream classes serve as the foundation for the behavior of all concrete classes, and in CPython they follow a number of dichotomies (paired classifications). In MicroPython, they are slightly simplified and implicit to achieve higher efficiency and save resources.
An important dichotomy in CPython is unbuffered vs buffered streams. In MicroPython, all streams are currently unbuffered. This is because all modern operating systems, and even many RTOSes and filesystem drivers, already perform buffering on their side. Adding another layer of buffering would be counterproductive (a problem known as “bufferbloat”) and would take up precious memory. Note that there are still cases where buffering may be useful, so we may introduce optional buffering support later.
However, in CPython, another important dichotomy relates to “buffering” - whether a stream may cause short reads/writes. A short read means the user requests, for example, 10 bytes from a stream, and similarly for writes. In CPython, unbuffered streams are automatically susceptible to short operations, while buffering is a guarantee against them. The absence of short reads/writes is an important feature, because it allows developing more concise and efficient programs - something MicroPython greatly needs. Therefore, although MicroPython does not support buffered streams, it still provides non-short-operation streams. Whether there will be short operations depends on the needs of each particular class, but developers are strongly encouraged to support non-short-operation behavior, for the reasons described above. For example, MicroPython sockets are guaranteed to avoid short reads/writes. In fact, at this point, there is no example of a short-operation stream class in the core, and one is a port-specific class where this need is determined by hardware characteristics.
In the case of non-blocking streams, non-short-operation behavior becomes tricky; blocking vs non-blocking behavior is another CPython dichotomy, fully supported by MicroPython. A non-blocking stream never waits for data to arrive or to be written - it may read/write, or indicate a lack of data (or the ability to write data). Obviously, this conflicts with the “non-short-operation” policy; in fact, in CPython the situation with non-blocking buffered (and thus non-short-operation) streams is baffling - in some places this combination is forbidden, in some cases it is undefined or simply undocumented, and in some cases it raises verbose exceptions. This problem is much simpler in MicroPython: non-blocking streams are important for efficient asynchronous operations, so this property takes precedence over “no-short-ops”. So,
The last dichotomy is binary vs text streams. MicroPython of course supports these, but in CPython text streams are themselves buffered, which they are not in MicroPython. (In fact, this is one of the cases where we may introduce buffering support.)
Note that, for efficiency, MicroPython does not provide abstract base classes corresponding to the hierarchy above, and it is not possible to implement or subclass stream classes in pure Python.
Functions
- uio.open(name, mode='r', **kwargs)
Open a file. The built-in
open()function is an alias for this function.
Classes
- class uio.FileIO(...)
This is the type of file opened in binary mode, e.g. using
open(name, "rb"). You should not instantiate this class directly.
- class uio.TextIOWrapper(...)
This is the type of file opened in text mode, e.g. using
open(name, "rt"). You should not instantiate this class directly.
- class uio.StringIO([string])
- class uio.BytesIO([string])
In-memory file-like objects for input/output.
StringIOis used for text-mode I/O (similar to a normal file opened with the “t” modifier).BytesIOis used for binary-mode I/O (similar to a normal file opened with the “b” modifier). The initial contents of the file-like object can be specified with a string argument (which should be a normal string for StringIO or a bytes object for BytesIO). All the common file methods such asread(),write(),seek(),flush(),close()are available on these objects, with the following additional methods:- getvalue()
Get the current contents of the underlying buffer that holds the data.
- class uio.StringIO(alloc_size)
- class uio.BytesIO(alloc_size)
Create an empty StringIO/BytesIO object, preallocated to hold alloc_size bytes. This means that writing that number of bytes will not cause the buffer to be reallocated, so there will be no out-of-memory situations or memory fragmentation. These constructors are MicroPython extensions and are recommended only for special cases and system-level libraries, not for end-user applications.
Difference to CPython
These constructors are MicroPython extensions.