summaryrefslogtreecommitdiffstats
path: root/Python/initconfig.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-09-14 23:31:45 (GMT)
committerGitHub <noreply@github.com>2021-09-14 23:31:45 (GMT)
commita65c86889e208dddb26a7ebe7840c24edbcca775 (patch)
treeec55222c3ac183806fc06f0d60514ab21e6dc560 /Python/initconfig.c
parent1aaa85949717e4ab2ed700e58762f0a3ce049a37 (diff)
downloadcpython-a65c86889e208dddb26a7ebe7840c24edbcca775.zip
cpython-a65c86889e208dddb26a7ebe7840c24edbcca775.tar.gz
cpython-a65c86889e208dddb26a7ebe7840c24edbcca775.tar.bz2
bpo-45020: Add -X frozen_modules=[on|off] to explicitly control use of frozen modules. (gh-28320)
Currently we freeze several modules into the runtime. For each of these modules it is essential to bootstrapping the runtime that they be frozen. Any other stdlib module that we later freeze into the runtime is not essential. We can just as well import from the .py file. This PR lets users explicitly choose which should be used, with the new "-X frozen_modules=[on|off]" CLI flag. The default is "off" for now. https://bugs.python.org/issue45020
Diffstat (limited to 'Python/initconfig.c')
-rw-r--r--Python/initconfig.c60
1 files changed, 59 insertions, 1 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c
index 61cd0e6..8740cc1 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -100,6 +100,8 @@ static const char usage_3[] = "\
instruction in code objects. This is useful when smaller code objects and pyc \n\
files are desired as well as supressing the extra visual location indicators \n\
when the interpreter displays tracebacks.\n\
+ -X frozen_modules=[on|off]: whether or not frozen modules should be used.\n\
+ The default is \"on\" (or \"off\" if you are running a local build).\n\
\n\
--check-hash-based-pycs always|default|never:\n\
control how Python invalidates hash-based .pyc files\n\
@@ -949,6 +951,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(pathconfig_warnings);
COPY_ATTR(_init_main);
COPY_ATTR(_isolated_interpreter);
+ COPY_ATTR(use_frozen_modules);
COPY_WSTRLIST(orig_argv);
#undef COPY_ATTR
@@ -1052,6 +1055,7 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_INT(_init_main);
SET_ITEM_INT(_isolated_interpreter);
SET_ITEM_WSTRLIST(orig_argv);
+ SET_ITEM_INT(use_frozen_modules);
return dict;
@@ -1334,6 +1338,7 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
GET_UINT(_install_importlib);
GET_UINT(_init_main);
GET_UINT(_isolated_interpreter);
+ GET_UINT(use_frozen_modules);
#undef CHECK_VALUE
#undef GET_UINT
@@ -1590,6 +1595,17 @@ config_get_xoption(const PyConfig *config, wchar_t *name)
return _Py_get_xoption(&config->xoptions, name);
}
+static const wchar_t*
+config_get_xoption_value(const PyConfig *config, wchar_t *name)
+{
+ const wchar_t *xoption = config_get_xoption(config, name);
+ if (xoption == NULL) {
+ return NULL;
+ }
+ const wchar_t *sep = wcschr(xoption, L'=');
+ return sep ? sep + 1 : L"";
+}
+
static PyStatus
config_init_home(PyConfig *config)
@@ -2066,6 +2082,48 @@ config_init_fs_encoding(PyConfig *config, const PyPreConfig *preconfig)
static PyStatus
+config_init_import(PyConfig *config, int compute_path_config)
+{
+ PyStatus status;
+
+ status = _PyConfig_InitPathConfig(config, compute_path_config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ /* -X frozen_modules=[on|off] */
+ const wchar_t *value = config_get_xoption_value(config, L"frozen_modules");
+ if (value == NULL) {
+ // For now we always default to "off".
+ // In the near future we will be factoring in PGO and in-development.
+ config->use_frozen_modules = 0;
+ }
+ else if (wcscmp(value, L"on") == 0) {
+ config->use_frozen_modules = 1;
+ }
+ else if (wcscmp(value, L"off") == 0) {
+ config->use_frozen_modules = 0;
+ }
+ else if (wcslen(value) == 0) {
+ // "-X frozen_modules" and "-X frozen_modules=" both imply "on".
+ config->use_frozen_modules = 1;
+ }
+ else {
+ return PyStatus_Error("bad value for option -X frozen_modules "
+ "(expected \"on\" or \"off\")");
+ }
+
+ return _PyStatus_OK();
+}
+
+PyStatus
+_PyConfig_InitImportConfig(PyConfig *config)
+{
+ return config_init_import(config, 1);
+}
+
+
+static PyStatus
config_read(PyConfig *config, int compute_path_config)
{
PyStatus status;
@@ -2111,7 +2169,7 @@ config_read(PyConfig *config, int compute_path_config)
}
if (config->_install_importlib) {
- status = _PyConfig_InitPathConfig(config, compute_path_config);
+ status = config_init_import(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}