◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
10 changes: 9 additions & 1 deletion Include/internal/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ typedef struct _PyPathConfig {
wchar_t *program_name;
/* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
wchar_t *home;
} _PyPathConfig;

#define _PyPathConfig_INIT {.module_search_path = NULL}
/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */

PyAPI_DATA(_PyPathConfig) _Py_path_config;
Expand Down
20 changes: 5 additions & 15 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1953,19 +1953,9 @@ pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
return -1;
}

/* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
Py_NoSiteFlag variables if a "._pth" file is found. */
int init_isolated = Py_IsolatedFlag;
int init_no_site = Py_NoSiteFlag;
Py_IsolatedFlag = cmdline->isolated;
Py_NoSiteFlag = cmdline->no_site_import;

err = _PyCoreConfig_Read(config);

cmdline->isolated = Py_IsolatedFlag;
cmdline->no_site_import = Py_NoSiteFlag;
Py_IsolatedFlag = init_isolated;
Py_NoSiteFlag = init_no_site;

if (_Py_INIT_FAILED(err)) {
pymain->err = err;
Expand Down Expand Up @@ -2116,7 +2106,7 @@ config_init_locale(_PyCoreConfig *config)
*/

_PyInitError
_PyCoreConfig_Read(_PyCoreConfig *config)
{
_PyInitError err;

Expand Down Expand Up @@ -2161,7 +2151,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
}

if (!config->_disable_importlib) {
err = _PyCoreConfig_InitPathConfig(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
Expand Down
18 changes: 7 additions & 11 deletions PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ get_program_full_path(const _PyCoreConfig *core_config,


static int
read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
int *isolated, int *nosite)
{
FILE *sp_file = _Py_wfopen(path, L"r");
if (sp_file == NULL) {
Expand All @@ -563,8 +562,8 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,

wcscpy_s(prefix, MAXPATHLEN+1, path);
reduce(prefix);
*isolated = 1;
*nosite = 1;

size_t bufsiz = MAXPATHLEN;
size_t prefixlen = wcslen(prefix);
Expand All @@ -589,9 +588,10 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
}

if (strcmp(line, "import site") == 0) {
*nosite = 0;
continue;
} else if (strncmp(line, "import ", 7) == 0) {
Py_FatalError("only 'import site' is supported in ._pth file");
}

Expand Down Expand Up @@ -680,11 +680,7 @@ calculate_pth_file(_PyPathConfig *config, wchar_t *prefix)
return 0;
}

/* FIXME, bpo-32030: Global configuration variables should not be modified
here, _PyPathConfig_Init() is called early in Python initialization:
see pymain_cmdline(). */
return read_pth_file(config, prefix, spbuffer,
&Py_IsolatedFlag, &Py_NoSiteFlag);
}


Expand Down
25 changes: 14 additions & 11 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ core_config_init_module_search_paths(_PyCoreConfig *config,


_PyInitError
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
{
_PyPathConfig path_config = _PyPathConfig_INIT;
_PyInitError err;
Expand Down Expand Up @@ -344,6 +345,13 @@ _PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
}
}

_PyPathConfig_Clear(&path_config);
return _Py_INIT_OK();

Expand All @@ -365,30 +373,25 @@ pathconfig_global_init(void)
}

_PyInitError err;
_PyPathConfig path_config = _PyPathConfig_INIT;
_PyCoreConfig config = _PyCoreConfig_INIT;

err = _PyCoreConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyPathConfig_Calculate(&path_config, &config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

err = _PyPathConfig_SetGlobal(&path_config);
if (_Py_INIT_FAILED(err)) {
goto error;
}

_PyPathConfig_Clear(&path_config);
_PyCoreConfig_Clear(&config);
return;

error:
_PyPathConfig_Clear(&path_config);
_PyCoreConfig_Clear(&config);
_Py_FatalInitError(err);
}
Expand Down
Toggle all file notes Toggle all file annotations