uio.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. :mod:`uio` -- input/output streams
  2. ==================================
  3. .. module:: uio
  4. :synopsis: input/output streams
  5. |see_cpython_module| :mod:`python:io`.
  6. This module contains additional types of `stream` (file-like) objects
  7. and helper functions.
  8. Conceptual hierarchy
  9. --------------------
  10. .. admonition:: Difference to CPython
  11. :class: attention
  12. Conceptual hierarchy of stream base classes is simplified in MicroPython,
  13. as described in this section.
  14. (Abstract) base stream classes, which serve as a foundation for behavior
  15. of all the concrete classes, adhere to few dichotomies (pair-wise
  16. classifications) in CPython. In MicroPython, they are somewhat simplified
  17. and made implicit to achieve higher efficiencies and save resources.
  18. An important dichotomy in CPython is unbuffered vs buffered streams. In
  19. MicroPython, all streams are currently unbuffered. This is because all
  20. modern OSes, and even many RTOSes and filesystem drivers already perform
  21. buffering on their side. Adding another layer of buffering is counter-
  22. productive (an issue known as "bufferbloat") and takes precious memory.
  23. Note that there still cases where buffering may be useful, so we may
  24. introduce optional buffering support at a later time.
  25. But in CPython, another important dichotomy is tied with "bufferedness" -
  26. it's whether a stream may incur short read/writes or not. A short read
  27. is when a user asks e.g. 10 bytes from a stream, but gets less, similarly
  28. for writes. In CPython, unbuffered streams are automatically short
  29. operation susceptible, while buffered are guarantee against them. The
  30. no short read/writes is an important trait, as it allows to develop
  31. more concise and efficient programs - something which is highly desirable
  32. for MicroPython. So, while MicroPython doesn't support buffered streams,
  33. it still provides for no-short-operations streams. Whether there will
  34. be short operations or not depends on each particular class' needs, but
  35. developers are strongly advised to favor no-short-operations behavior
  36. for the reasons stated above. For example, MicroPython sockets are
  37. guaranteed to avoid short read/writes. Actually, at this time, there is
  38. no example of a short-operations stream class in the core, and one would
  39. be a port-specific class, where such a need is governed by hardware
  40. peculiarities.
  41. The no-short-operations behavior gets tricky in case of non-blocking
  42. streams, blocking vs non-blocking behavior being another CPython dichotomy,
  43. fully supported by MicroPython. Non-blocking streams never wait for
  44. data either to arrive or be written - they read/write whatever possible,
  45. or signal lack of data (or ability to write data). Clearly, this conflicts
  46. with "no-short-operations" policy, and indeed, a case of non-blocking
  47. buffered (and this no-short-ops) streams is convoluted in CPython - in
  48. some places, such combination is prohibited, in some it's undefined or
  49. just not documented, in some cases it raises verbose exceptions. The
  50. matter is much simpler in MicroPython: non-blocking stream are important
  51. for efficient asynchronous operations, so this property prevails on
  52. the "no-short-ops" one. So, while blocking streams will avoid short
  53. reads/writes whenever possible (the only case to get a short read is
  54. if end of file is reached, or in case of error (but errors don't
  55. return short data, but raise exceptions)), non-blocking streams may
  56. produce short data to avoid blocking the operation.
  57. The final dichotomy is binary vs text streams. MicroPython of course
  58. supports these, but while in CPython text streams are inherently
  59. buffered, they aren't in MicroPython. (Indeed, that's one of the cases
  60. for which we may introduce buffering support.)
  61. Note that for efficiency, MicroPython doesn't provide abstract base
  62. classes corresponding to the hierarchy above, and it's not possible
  63. to implement, or subclass, a stream class in pure Python.
  64. Functions
  65. ---------
  66. .. function:: open(name, mode='r', **kwargs)
  67. Open a file. Builtin ``open()`` function is aliased to this function.
  68. All ports (which provide access to file system) are required to support
  69. *mode* parameter, but support for other arguments vary by port.
  70. Classes
  71. -------
  72. .. class:: FileIO(...)
  73. This is type of a file open in binary mode, e.g. using ``open(name, "rb")``.
  74. You should not instantiate this class directly.
  75. .. class:: TextIOWrapper(...)
  76. This is type of a file open in text mode, e.g. using ``open(name, "rt")``.
  77. You should not instantiate this class directly.
  78. .. class:: StringIO([string])
  79. .. class:: BytesIO([string])
  80. In-memory file-like objects for input/output. `StringIO` is used for
  81. text-mode I/O (similar to a normal file opened with "t" modifier).
  82. `BytesIO` is used for binary-mode I/O (similar to a normal file
  83. opened with "b" modifier). Initial contents of file-like objects
  84. can be specified with *string* parameter (should be normal string
  85. for `StringIO` or bytes object for `BytesIO`). All the usual file
  86. methods like ``read()``, ``write()``, ``seek()``, ``flush()``,
  87. ``close()`` are available on these objects, and additionally, a
  88. following method:
  89. .. method:: getvalue()
  90. Get the current contents of the underlying buffer which holds data.