docs/library/index: Update built-in extension docs.

- Make the docs match the new behavior which only allows certain modules
  to be extended.
- List the modules that currently have the u-prefix.
- Add a note about the sys.path method for forcing a built-in import.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
pull/11456/head
Jim Mussared 2023-05-31 15:59:07 +10:00 zatwierdzone przez Damien George
rodzic 952a78f88a
commit 5159304ca1
1 zmienionych plików z 48 dodań i 18 usunięć

Wyświetl plik

@ -8,15 +8,17 @@ MicroPython libraries
Important summary of this section Important summary of this section
* MicroPython provides built-in modules that mirror the functionality of the * MicroPython provides built-in modules that mirror the functionality of the
Python standard library (e.g. :mod:`os`, :mod:`time`), as well as :ref:`Python standard library <micropython_lib_python>` (e.g. :mod:`os`,
MicroPython-specific modules (e.g. :mod:`bluetooth`, :mod:`machine`). :mod:`time`), as well as :ref:`MicroPython-specific modules <micropython_lib_micropython>`
* Most standard library modules implement a subset of the functionality of (e.g. :mod:`bluetooth`, :mod:`machine`).
the equivalent Python module, and in a few cases provide some * Most Python standard library modules implement a subset of the
MicroPython-specific extensions (e.g. :mod:`array`, :mod:`os`) functionality of the equivalent Python module, and in a few cases provide
some MicroPython-specific extensions (e.g. :mod:`array`, :mod:`os`)
* Due to resource constraints or other limitations, some ports or firmware * Due to resource constraints or other limitations, some ports or firmware
versions may not include all the functionality documented here. versions may not include all the functionality documented here.
* To allow for extensibility, the built-in modules can be extended from * To allow for extensibility, some built-in modules can be
Python code loaded onto the device. :ref:`extended from Python code <micropython_lib_extending>` loaded onto
the device filesystem.
This chapter describes modules (function and class libraries) which are built This chapter describes modules (function and class libraries) which are built
into MicroPython. This documentation in general aspires to describe all modules into MicroPython. This documentation in general aspires to describe all modules
@ -41,6 +43,8 @@ Beyond the built-in libraries described in this documentation, many more
modules from the Python standard library, as well as further MicroPython modules from the Python standard library, as well as further MicroPython
extensions to it, can be found in :term:`micropython-lib`. extensions to it, can be found in :term:`micropython-lib`.
.. _micropython_lib_python:
Python standard libraries and micro-libraries Python standard libraries and micro-libraries
--------------------------------------------- ---------------------------------------------
@ -77,6 +81,7 @@ library.
zlib.rst zlib.rst
_thread.rst _thread.rst
.. _micropython_lib_micropython:
MicroPython-specific libraries MicroPython-specific libraries
------------------------------ ------------------------------
@ -181,23 +186,48 @@ The following libraries are specific to the Zephyr port.
zephyr.rst zephyr.rst
.. _micropython_lib_extending:
Extending built-in libraries from Python Extending built-in libraries from Python
---------------------------------------- ----------------------------------------
In most cases, the above modules are actually named ``umodule`` rather than Many built-in modules are actually named ``umodule`` rather than ``module``, but
``module``, but MicroPython will alias any module prefixed with a ``u`` to the MicroPython will alias any module prefixed with a ``u`` to the non-``u``
non-``u`` version. However a file (or :term:`frozen module`) named version. This means that, for example, ``import time`` will first attempt to
``module.py`` will take precedence over this alias. resolve from the filesystem, and then failing that will fall back to the
built-in ``utime``. On the other hand, ``import utime`` will always go directly
to the built-in.
This allows the user to provide an extended implementation of a built-in library This allows the user to provide an extended implementation of a built-in library
(perhaps to provide additional CPython compatibility). The user-provided module (perhaps to provide additional CPython compatibility or missing functionality).
(in ``module.py``) can still use the built-in functionality by importing The user-provided module (in ``module.py``) can still use the built-in
``umodule`` directly. This is used extensively in :term:`micropython-lib`. See functionality by importing ``umodule`` directly (e.g. typically an extension
:ref:`packages` for more information. module ``time.py`` will do ``from utime import *``). This is used extensively
in :term:`micropython-lib`. See :ref:`packages` for more information.
This applies to both the Python standard libraries (e.g. ``os``, ``time``, etc), This extensibility applies to the following Python standard library modules
but also the MicroPython libraries too (e.g. ``machine``, ``bluetooth``, etc). which are built-in to the firmware: ``array``, ``binascii``, ``collections``,
The main exception is the port-specific libraries (``pyb``, ``esp``, etc). ``errno``, ``hashlib``, ``heapq``, ``io``, ``json``, ``os``, ``platform``,
``random``, ``re``, ``select``, ``socket``, ``ssl``, ``struct``, ``sys``,
``time``, ``zlib``, as well as the MicroPython-specific libraries: ``bluepy``,
``bluetooth``, ``machine``, ``timeq``, ``websocket``. All other built-in
modules cannot be extended from the filesystem.
*Other than when you specifically want to force the use of the built-in module, *Other than when you specifically want to force the use of the built-in module,
we recommend always using* ``import module`` *rather than* ``import umodule``. we recommend always using* ``import module`` *rather than* ``import umodule``.
**Note:** In MicroPython v1.21.0 and higher, it is now possible to force an
import of the built-in module by clearing ``sys.path`` during the import. For
example, in ``time.py``, you can write::
_path = sys.path
sys.path = ()
try:
from time import *
finally:
sys.path = _path
del _path
This is now the preferred way (instead of ``from utime import *``), as the
``u``-prefix will be removed from the names of built-in modules in a future
version of MicroPython.