8000 bpo-34170: _PyCoreConfig_Read() leaves Py_IsolatedFlag unchanged (GH-… · python/cpython@f2626ce · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit f2626ce

Browse files
authored
bpo-34170: _PyCoreConfig_Read() leaves Py_IsolatedFlag unchanged (GH-8361)
* _PyCoreConfig_Read() no longer directly modifies Py_IsolatedFlag and Py_NoSiteFlag global configuration flags. The function now requires two pointers to integer, so these flags can be set later, to avoid side effets in _PyCoreConfig_Read(). * pathconfig_global_init() now leaves Py_IsolatedFlag and Py_NoSiteFlag unchanged. * Fix pathconfig_global_init(): avoid computing the path configuration twice, use _PyCoreConfig_SetPathConfig().
1 parent c884616 commit f2626ce

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

Include/internal/pystate.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ typedef struct _PyPathConfig {
5252
wchar_t *program_name;
5353
/* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
5454
wchar_t *home;
55+
/* isolated and no_site_import are used to set Py_IsolatedFlag and
56+
Py_NoSiteFlag flags on Windows in read_pth_file(). These fields
57+
are ignored when their value are equal to -1 (unset). */
58+
int isolated;
59+
int no_site_import;
5560
} _PyPathConfig;
5661

57-
#define _PyPathConfig_INIT {.module_search_path = NULL}
62+
#define _PyPathConfig_INIT \
63+
{.module_search_path = NULL, \
64+
.isolated = -1, \
65+
.no_site_import = -1}
5866
/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
5967

6068
PyAPI_DATA(_PyPathConfig) _Py_path_config;

Include/pylifecycle.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
5454
PyAPI_FUNC(_PyInitError) _Py_InitializeCore(const _PyCoreConfig *);
5555
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
5656

57-
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *);
57+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(
58+
_PyCoreConfig *config,
59+
int *isolated,
60+
int *no_site_import);
5861
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
5962
PyAPI_FUNC(int) _PyCoreConfig_Copy(
6063
_PyCoreConfig *config,
6164
const _PyCoreConfig *config2);
62-
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config);
65+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(
66+
_PyCoreConfig *config,
67+
int *isolated,
68+
int *no_site_import);
6369
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
6470
const _PyCoreConfig *config);
6571

Modules/main.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,19 +1953,9 @@ pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
19531953
return -1;
19541954
}
19551955

1956-
/* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
1957-
Py_NoSiteFlag variables if a "._pth" file is found. */
1958-
int init_isolated = Py_IsolatedFlag;
1959-
int init_no_site = Py_NoSiteFlag;
1960-
Py_IsolatedFlag = cmdline->isolated;
1961-
Py_NoSiteFlag = cmdline->no_site_import;
1962-
1963-
err = _PyCoreConfig_Read(config);
1964-
1965-
cmdline->isolated = Py_IsolatedFlag;
1966-
cmdline->no_site_import = Py_NoSiteFlag;
1967-
Py_IsolatedFlag = init_isolated;
1968-
Py_NoSiteFlag = init_no_site;
1956+
err = _PyCoreConfig_Read(config,
1957+
&cmdline->isolated,
1958+
&cmdline->no_site_import);
19691959

19701960
if (_Py_INIT_FAILED(err)) {
19711961
pymain->err = err;
@@ -2116,7 +2106,7 @@ config_init_locale(_PyCoreConfig *config)
21162106
*/
21172107

21182108
_PyInitError
2119-
_PyCoreConfig_Read(_PyCoreConfig *config)
2109+
_PyCoreConfig_Read(_PyCoreConfig *config, int *isolated, int *no_site_import)
21202110
{
21212111
_PyInitError err;
21222112

@@ -2161,7 +2151,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
21612151
}
21622152

21632153
if (!config->_disable_importlib) {
2164-
err = _PyCoreConfig_InitPathConfig(config);
2154+
err = _PyCoreConfig_InitPathConfig(config, isolated, no_site_import);
21652155
if (_Py_INIT_FAILED(err)) {
21662156
return err;
21672157
}

PC/getpathp.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ get_program_full_path(const _PyCoreConfig *core_config,
553553

554554

555555
static int
556-
read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
557-
int *isolated, int *nosite)
556+
read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
558557
{
559558
FILE *sp_file = _Py_wfopen(path, L"r");
560559
if (sp_file == NULL) {
@@ -563,8 +562,8 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
563562

564563
wcscpy_s(prefix, MAXPATHLEN+1, path);
565564
reduce(prefix);
566-
*isolated = 1;
567-
*nosite = 1;
565+
config->isolated = 1;
566+
config->no_site_import = 1;
568567

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

591590
if (strcmp(line, "import site") == 0) {
592-
*nosite = 0;
591+
config->no_site_import = 0;
593592
continue;
594-
} else if (strncmp(line, "import ", 7) == 0) {
593+
}
594+
else if (strncmp(line, "import ", 7) == 0) {
595595
Py_FatalError("only 'import site' is supported in ._pth file");
596596
}
597597

@@ -680,11 +680,7 @@ calculate_pth_file(_PyPathConfig *config, wchar_t *prefix)
680680
return 0;
681681
}
682682

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

690686

Python/pathconfig.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ core_config_init_module_search_paths(_PyCoreConfig *config,
283283

284284

285285
_PyInitError
286-
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
286+
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config,
287+
int *isolated, int *no_site_import)
287288
{
288289
_PyPathConfig path_config = _PyPathConfig_INIT;
289290
_PyInitError err;
@@ -344,6 +345,13 @@ _PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
344345
}
345346
}
346347

348+
if (path_config.isolated != -1 && isolated != NULL) {
349+
*isolated = path_config.isolated;
350+
}
351+
if (path_config.no_site_import != -1 && no_site_import != NULL) {
352+
*no_site_import = path_config.no_site_import;
353+
}
354+
347355
_PyPathConfig_Clear(&path_config);
348356
return _Py_INIT_OK();
349357

@@ -365,30 +373,25 @@ pathconfig_global_init(void)
365373
}
366374

367375
_PyInitError err;
368-
_PyPathConfig path_config = _PyPathConfig_INIT;
369376
_PyCoreConfig config = _PyCoreConfig_INIT;
370377

371-
err = _PyCoreConfig_Read(&config);
378+
/* Py_IsolatedFlag and Py_NoSiteFlag are left unchanged: pass NULL.
379+
_PyCoreConfig_InitPathConfig() will be called later and will set
380+
these flags. */
381+
err = _PyCoreConfig_Read(&config, NULL, NULL);
372382
if (_Py_INIT_FAILED(err)) {
373383
goto error;
374384
}
375385

376-
err = _PyPathConfig_Calculate(&path_config, &config);
386+
err = _PyCoreConfig_SetPathConfig(&config);
377387
if (_Py_INIT_FAILED(err)) {
378388
goto error;
379389
}
380390

381-
err = _PyPathConfig_SetGlobal(&path_config);
382-
if (_Py_INIT_FAILED(err)) {
383-
goto error;
384-
}
385-
386-
_PyPathConfig_Clear(&path_config);
387391
_PyCoreConfig_Clear(&config);
388392
return;
389393

390394
error:
391-
_PyPathConfig_Clear(&path_config);
392395
_PyCoreConfig_Clear(&config);
393396
_Py_FatalInitError(err);
394397
}

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
928928
config._disable_importlib = !install_importlib;
929929
config.install_signal_handlers = install_sigs;
930930

931-
err = _PyCoreConfig_Read(&config);
931+
err = _PyCoreConfig_Read(&config, &Py_IsolatedFlag, &Py_NoSiteFlag);
932932
if (_Py_INIT_FAILED(err)) {
933933
goto done;
934934
}

0 commit comments

Comments
 (0)
0