Skip to content

Commit 71687e5

Browse files
committed
gh-756: Add a page about frozen modules
Covers what frozen modules are, the three kinds, how to change which stdlib modules are frozen, the runtime setting and how tests handle them.
1 parent e97d25f commit 71687e5

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
.. _frozen-modules:
2+
3+
==============
4+
Frozen modules
5+
==============
6+
7+
A *frozen* module is a Python module whose compiled code is built into the
8+
interpreter. Its marshalled bytecode is stored as an array of bytes in a C
9+
header file and compiled into the executable, so importing it does not need to
10+
find and read a ``.py`` or ``.pyc`` file. Python 3.11 froze the modules that
11+
are essential for startup to make interpreter startup faster
12+
(see :ref:`What's New in 3.11 <whatsnew311-faster-startup>`).
13+
14+
Frozen modules are imported by :class:`importlib.machinery.FrozenImporter`.
15+
Their module spec has the origin ``"frozen"``, and frames from their code
16+
show up in tracebacks as ``<frozen os>`` instead of a file name.
17+
You can ask :mod:`!_imp` whether a name is frozen with
18+
``_imp.is_frozen("os")``.
19+
20+
Kinds of frozen modules
21+
=======================
22+
23+
:cpy-file:`Python/frozen.c` defines three groups of frozen modules.
24+
25+
* **Bootstrap modules** are needed to set up the import system itself.
26+
They are :mod:`!importlib._bootstrap` (frozen as ``_frozen_importlib``),
27+
:mod:`!importlib._bootstrap_external` (``_frozen_importlib_external``),
28+
:mod:`zipimport` and ``_pybuiltins`` (from :cpy-file:`Lib/_pybuiltins.py`,
29+
frozen with the ID ``builtins`` so that tracebacks show
30+
``<frozen builtins>``).
31+
These are always used, even when frozen modules are turned off.
32+
* **Stdlib modules** are the standard library modules that are imported
33+
during startup.
34+
The list is grouped by why they are needed:
35+
startup without :mod:`site` (``python -S``), startup with :mod:`site`,
36+
the interactive interpreter, and running a module with ``-m``.
37+
At the time of writing it contains for example :mod:`os`, :mod:`site`,
38+
:mod:`io`, :mod:`codecs`, :mod:`abc`, :mod:`stat` and :mod:`runpy`.
39+
* **Test modules** exist only to test the frozen module machinery.
40+
Like the stdlib modules, they are only used when frozen modules are on.
41+
They are ``__hello__``, the package ``__phello__`` with its submodules,
42+
a few aliases of them, and ``__hello_only__``, which has no source module
43+
in the stdlib.
44+
45+
The module ``getpath`` (:cpy-file:`Modules/getpath.py`) is also frozen,
46+
but it has its own rule in :cpy-file:`Makefile.pre.in`
47+
and is not part of the lists above.
48+
49+
Finding and changing the frozen stdlib modules
50+
==============================================
51+
52+
The source of truth is the ``FROZEN`` list at the top of
53+
:cpy-file:`Tools/build/freeze_modules.py`.
54+
Each entry has the form ``frozenid``, ``frozenid : modname`` or
55+
``frozenid : modname = pyfile``, and is placed in a section.
56+
The docstring of ``_parse_spec()`` in that file describes the syntax,
57+
including how to freeze packages and their submodules.
58+
:cpy-file:`Python/frozen.c` is mostly generated from this list.
59+
60+
To add or remove a frozen module, edit the list and run::
61+
62+
make regen-frozen
63+
64+
This runs ``Tools/build/freeze_modules.py``.
65+
According to the notes at the top of :cpy-file:`Python/frozen.c`,
66+
the script freezes the target modules, updates the includes and the module
67+
arrays in ``frozen.c``, updates the ``FROZEN_FILES`` variable and the
68+
per-module targets in :cpy-file:`Makefile.pre.in`, and updates the module
69+
lists in :cpy-file:`PCbuild/_freeze_module.vcxproj` and
70+
:cpy-file:`PCbuild/_freeze_module.vcxproj.filters`.
71+
Commit the changes to all of these files.
72+
The Makefile may have changed, so run ``make`` again afterwards.
73+
74+
The frozen data itself, in ``Python/frozen_modules/*.h``, is always
75+
generated by the build and is not kept in the repository.
76+
Each header has a rule in :cpy-file:`Makefile.pre.in` that depends on the
77+
module's ``.py`` file, so a normal ``make`` refreshes the header after you
78+
edit the source of a frozen module.
79+
The data depends on the bytecode and marshal formats, so changes to the
80+
compiler, the bytecode or the marshal format need ``make regen-frozen``
81+
(the comment at the top of :cpy-file:`Python/frozen.c` says so).
82+
83+
Not every module is frozen.
84+
Only the modules in the lists above are, and the sections show why they are
85+
there.
86+
For example, :mod:`encodings` is commented out of the list:
87+
the comment in ``freeze_modules.py`` says that freezing it only partially
88+
causes import errors for its submodules when the standard library is zipped
89+
(gh-148750).
90+
91+
Using frozen modules while developing
92+
=====================================
93+
94+
Whether frozen modules are used is a runtime setting.
95+
Set it with the :option:`-X frozen_modules <-X>` option
96+
(``on`` or ``off``) or with the :envvar:`PYTHON_FROZEN_MODULES`
97+
environment variable.
98+
In :cpy-file:`Python/initconfig.c` the default is off for a debug build
99+
(``Py_DEBUG``) and on otherwise.
100+
The bootstrap modules are used either way.
101+
102+
When frozen modules are off, the import system uses the ``.py`` and
103+
``.pyc`` files, which is what you want when you edit a module that would
104+
otherwise be frozen.
105+
106+
Frozen modules in tests
107+
=======================
108+
109+
Tests that need the frozen or the non-frozen version of a module can use
110+
the helpers in :cpy-file:`Lib/test/support/import_helper.py`:
111+
112+
* ``frozen_modules(enabled=True)`` is a context manager that forces frozen
113+
modules on (or off with ``enabled=False``).
114+
It only applies to modules that have not been imported yet,
115+
and the essential modules are always imported frozen.
116+
* ``import_fresh_module()`` and ``CleanImport`` take a ``usefrozen``
117+
argument that defaults to ``False``, which disables the frozen importer
118+
for the import (except for essential modules such as
119+
:mod:`!importlib._bootstrap`).
120+
121+
:cpy-file:`Lib/test/test_frozen.py` is a small example of a test that
122+
imports ``__hello__`` with frozen modules enabled.
123+
The tests for the frozen importer itself are in
124+
:cpy-file:`Lib/test/test_importlib/frozen/`.

‎developer-workflow/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Development workflow
1212
development-cycle
1313
stdlib
1414
extension-modules
15+
frozen-modules
1516
c-api
1617
grammar
1718
porting

0 commit comments

Comments
 (0)