diff --git a/developer-workflow/frozen-modules.rst b/developer-workflow/frozen-modules.rst new file mode 100644 index 000000000..b2b9d313d --- /dev/null +++ b/developer-workflow/frozen-modules.rst @@ -0,0 +1,116 @@ +.. _frozen-modules: + +============== +Frozen modules +============== + +A *frozen* module is a Python module whose compiled code is built into the +interpreter. Its marshalled bytecode is stored as an array of bytes in a C +header file and compiled into the executable, so importing it does not need to +find and read a ``.py`` or ``.pyc`` file. Python 3.11 froze the modules that +are essential for startup to make interpreter startup faster +(see :ref:`What's New in 3.11 `). + +Frozen modules are imported by :class:`importlib.machinery.FrozenImporter`. +Their module spec has the origin ``"frozen"``, and frames from their code +show up in tracebacks as ```` instead of a file name. +You can ask :mod:`!_imp` whether a name is frozen with +``_imp.is_frozen("os")``. + +Kinds of frozen modules +======================= + +:cpy-file:`Python/frozen.c` defines three groups of frozen modules. + +* **Bootstrap modules** are needed to set up the import system itself. + They are :mod:`!importlib._bootstrap` (frozen as ``_frozen_importlib``), + :mod:`!importlib._bootstrap_external` (``_frozen_importlib_external``), + :mod:`zipimport` and ``_pybuiltins`` (from :cpy-file:`Lib/_pybuiltins.py`, + frozen with the ID ``builtins`` so that tracebacks show + ````). + These are always used, even when frozen modules are turned off. +* **Stdlib modules** are the standard library modules that are imported + during startup. + The list is grouped by why they are needed: + startup without :mod:`site` (``python -S``), startup with :mod:`site`, + the interactive interpreter, and running a module with ``-m``. + At the time of writing it contains for example :mod:`os`, :mod:`site`, + :mod:`io`, :mod:`codecs`, :mod:`abc`, :mod:`stat` and :mod:`runpy`. +* **Test modules** exist only to test the frozen module machinery. + Like the stdlib modules, they are only used when frozen modules are on. + They are ``__hello__``, the package ``__phello__`` with its submodules, + a few aliases of them, and ``__hello_only__``, which has no source module + in the stdlib. + +The module ``getpath`` (:cpy-file:`Modules/getpath.py`) is also frozen, +but it has its own rule in :cpy-file:`Makefile.pre.in` +and is not part of the lists above. + +Finding and changing the frozen stdlib modules +============================================== + +The source of truth is the ``FROZEN`` list at the top of +:cpy-file:`Tools/build/freeze_modules.py`. +Each entry has the form ``frozenid``, ``frozenid : modname`` or +``frozenid : modname = pyfile``, and is placed in a section. +The docstring of ``_parse_spec()`` in that file describes the syntax, +including how to freeze packages and their submodules. +:cpy-file:`Python/frozen.c` is mostly generated from this list. + +To add or remove a frozen module, edit the list and run:: + + make regen-frozen + +This runs ``Tools/build/freeze_modules.py``. +According to the notes at the top of :cpy-file:`Python/frozen.c`, +the script freezes the target modules, updates the includes and the module +arrays in ``frozen.c``, updates the ``FROZEN_FILES`` variable and the +per-module targets in :cpy-file:`Makefile.pre.in`, and updates the module +lists in :cpy-file:`PCbuild/_freeze_module.vcxproj` and +:cpy-file:`PCbuild/_freeze_module.vcxproj.filters`. +Commit the changes to all of these files. +The Makefile may have changed, so run ``make`` again afterwards. + +The frozen data itself, in ``Python/frozen_modules/*.h``, is always +generated by the build and is not kept in the repository. +Each header has a rule in :cpy-file:`Makefile.pre.in` that depends on the +module's ``.py`` file, so a normal ``make`` refreshes the header after you +edit the source of a frozen module. +The data depends on the bytecode and marshal formats, so changes to the +compiler, the bytecode or the marshal format need ``make regen-frozen`` +(the comment at the top of :cpy-file:`Python/frozen.c` says so). + +Using frozen modules while developing +===================================== + +Whether frozen modules are used is a runtime setting. +Set it with the :option:`-X frozen_modules <-X>` option +(``on`` or ``off``) or with the :envvar:`PYTHON_FROZEN_MODULES` +environment variable. +In :cpy-file:`Python/initconfig.c` the default is off for a debug build +(``Py_DEBUG``) and on otherwise. +The bootstrap modules are used either way. + +When frozen modules are off, the import system uses the ``.py`` and +``.pyc`` files, which is what you want when you edit a module that would +otherwise be frozen. + +Frozen modules in tests +======================= + +Tests that need the frozen or the non-frozen version of a module can use +the helpers in :cpy-file:`Lib/test/support/import_helper.py`: + +* ``frozen_modules(enabled=True)`` is a context manager that forces frozen + modules on (or off with ``enabled=False``). + It only applies to modules that have not been imported yet, + and the essential modules are always imported frozen. +* ``import_fresh_module()`` and ``CleanImport`` take a ``usefrozen`` + argument that defaults to ``False``, which disables the frozen importer + for the import (except for essential modules such as + :mod:`!importlib._bootstrap`). + +:cpy-file:`Lib/test/test_frozen.py` is a small example of a test that +imports ``__hello__`` with frozen modules enabled. +The tests for the frozen importer itself are in +:cpy-file:`Lib/test/test_importlib/frozen/`. diff --git a/developer-workflow/index.rst b/developer-workflow/index.rst index e04fc8a8b..fba43f8fa 100644 --- a/developer-workflow/index.rst +++ b/developer-workflow/index.rst @@ -12,6 +12,7 @@ Development workflow development-cycle stdlib extension-modules + frozen-modules c-api grammar porting