8000 gh-111374: Add a new PYTHON_FROZEN_MODULES env var, equivalent of `-X… · python/cpython@45a36d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45a36d5

Browse files
yileihugovk
andauthored
gh-111374: Add a new PYTHON_FROZEN_MODULES env var, equivalent of -X frozen_modules. (#111411)
Adds a new PYTHON_FROZEN_MODULES env var to correspond with -X frozen_modules. Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
1 parent 7e135a4 commit 45a36d5

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

Doc/using/cmdline.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,13 @@ Miscellaneous options
535535
location indicators when the interpreter displays tracebacks. See also
536536
:envvar:`PYTHONNODEBUGRANGES`.
537537
* ``-X frozen_modules`` determines whether or not frozen modules are
538-
ignored by the import machinery. A value of "on" means they get
539-
imported and "off" means they are ignored. The default is "on"
538+
ignored by the import machinery. A value of ``on`` means they get
539+
imported and ``off`` means they are ignored. The default is ``on``
540540
if this is an installed Python (the normal case). If it's under
541-
development (running from the source tree) then the default is "off".
542-
Note that the "importlib_bootstrap" and "importlib_bootstrap_external"
543-
frozen modules are always used, even if this flag is set to "off".
541+
development (running from the source tree) then the default is ``off``.
542+
Note that the :mod:`!importlib_bootstrap` and
543+
:mod:`!importlib_bootstrap_external` frozen modules are always used, even
544+
if this flag is set to ``off``. See also :envvar:`PYTHON_FROZEN_MODULES`.
544545
* ``-X perf`` enables support for the Linux ``perf`` profiler.
545546
When this option is provided, the ``perf`` profiler will be able to
546547
report Python calls. This option is only available on some platforms and
@@ -1095,6 +1096,20 @@ conflict.
10951096

10961097
.. versionadded:: 3.13
10971098

1099+
.. envvar:: PYTHON_FROZEN_MODULES
1100+
1101+
If this variable is set to ``on`` or ``off``, it determines whether or not
1102+
frozen modules are ignored by the import machinery. A value of ``on`` means
1103+
they get imported and ``off`` means they are ignored. The default is ``on``
1104+
for non-debug builds (the normal case) and ``off`` for debug builds.
1105+
Note that the :mod:`!importlib_bootstrap` and
1106+
:mod:`!importlib_bootstrap_external` frozen modules are always used, even
1107+
if this flag is set to ``off``.
1108+
1109+
See also the :option:`-X frozen_modules <-X>` command-line option.
1110+
1111+
.. versionadded:: 3.13
1112+
10981113

10991114
Debug-mode variables
11001115
~~~~~~~~~~~~~~~~~~~~

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ Other Language Changes
120120
is rejected when the global is used in the :keyword:`else` block.
121121
(Contributed by Irit Katriel in :gh:`111123`.)
122122

123+
* Added a new environment variable :envvar:`PYTHON_FROZEN_MODULES`. It
124+
determines whether or not frozen modules are ignored by the import machinery,
125+
equivalent of the :option:`-X frozen_modules <-X>` command-line option.
126+
(Contributed by Yilei Yang in :gh:`111374`.)
127+
123128
New Modules
124129
===========
125130

Lib/test/test_cmd_line.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ def test_xoption_frozen_modules(self):
153153
res = assert_python_ok(*cmd)
154154
self.assertRegex(res.out.decode('utf-8'), expected)
155155

156+
def test_env_var_frozen_modules(self):
157+
tests = {
158+
('on', 'FrozenImporter'),
159+
('off', 'SourceFileLoader'),
160+
}
161+
for raw, expected in tests:
162+
cmd = ['-c', 'import os; print(os.__spec__.loader, end="")']
163+
with self.subTest(raw):
164+
res = assert_python_ok(*cmd, PYTHON_FROZEN_MODULES=raw)
165+
self.assertRegex(res.out.decode('utf-8'), expected)
166+
156167
def test_run_module(self):
157168
# Test expected operation of the '-m' switch
158169
# Switch needs an argument
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added a new environment variable :envvar:`PYTHON_FROZEN_MODULES`. It
2+
determines whether or not frozen modules are ignored by the import machinery,
3+
equivalent of the :option:`-X frozen_modules <-X>` command-line option.

Python/initconfig.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,14 @@ static const char usage_envvars[] =
285285
"PYTHONDEVMODE: enable the development mode.\n"
286286
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"
287287
"PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'.\n"
288-
"PYTHONNODEBUGRANGES: If this variable is set, it disables the inclusion of the \n"
288+
"PYTHONNODEBUGRANGES: if this variable is set, it disables the inclusion of the \n"
289289
" tables mapping extra location information (end line, start column offset \n"
290290
" and end column offset) to every instruction in code objects. This is useful \n"
291291
" when smaller code objects and pyc files are desired as well as suppressing the \n"
292292
" extra visual location indicators when the interpreter displays tracebacks.\n"
293+
"PYTHON_FROZEN_MODULES : if this variable is set, it determines whether or not \n"
294+
" frozen modules should be used. The default is \"on\" (or \"off\" if you are \n"
295+
" running a local build).\n"
293296
"These variables have equivalent command-line parameters (see --help for details):\n"
294297
"PYTHONDEBUG : enable parser debug mode (-d)\n"
295298
"PYTHONDONTWRITEBYTECODE : don't write .pyc files (-B)\n"
@@ -2132,6 +2135,19 @@ config_init_import(PyConfig *config, int compute_path_config)
21322135
return status;
21332136
}
21342137

2138+
const char *env = config_get_env(config, "PYTHON_FROZEN_MODULES");
2139+
if (env == NULL) {
2140+
}
2141+
else if (strcmp(env, "on") == 0) {
2142+
config->use_frozen_modules = 1;
2143+
}
2144+
else if (strcmp(env, "off") == 0) {
2145+
config->use_frozen_modules = 0;
2146+
} else {
2147+
return PyStatus_Error("bad value for PYTHON_FROZEN_MODULES "
2148+
"(expected \"on\" or \"off\")");
2149+
}
2150+
21352151
/* -X frozen_modules=[on|off] */
21362152
const wchar_t *value = config_get_xoption_value(config, L"frozen_modules");
21372153
if (value == NULL) {

0 commit comments

Comments
 (0)
0