Skip to content

Commit e32e79f

Browse files
authored
bpo-32030: Move PYTHONPATH to _PyMainInterpreterConfig (#4511)
Move _PyCoreConfig.module_search_path_env to _PyMainInterpreterConfig structure.
1 parent 0784a2e commit e32e79f

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

‎Include/pylifecycle.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
9494
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
9595
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
9696
#ifdef Py_BUILD_CORE
97-
PyAPI_FUNC(wchar_t *) _Py_GetPathWithConfig(_PyCoreConfig *config);
97+
PyAPI_FUNC(wchar_t *) _Py_GetPathWithConfig(_PyMainInterpreterConfig *config);
9898
#endif
9999
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
100100
#ifdef MS_WINDOWS

‎Include/pystate.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ typedef struct {
3030
unsigned long hash_seed;
3131
int _disable_importlib; /* Needed by freeze_importlib */
3232
const char *allocator; /* Memory allocator: _PyMem_SetupAllocators() */
33-
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
3433
int dev_mode; /* -X dev */
3534
int faulthandler; /* -X faulthandler */
3635
int tracemalloc; /* -X tracemalloc=N */
@@ -46,7 +45,6 @@ typedef struct {
4645
.hash_seed = 0, \
4746
._disable_importlib = 0, \
4847
.allocator = NULL, \
49-
.module_search_path_env = NULL, \
5048
.dev_mode = 0, \
5149
.faulthandler = 0, \
5250
.tracemalloc = 0, \
@@ -62,11 +60,13 @@ typedef struct {
6260
*/
6361
typedef struct {
6462
int install_signal_handlers;
63+
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
6564
} _PyMainInterpreterConfig;
6665

6766
#define _PyMainInterpreterConfig_INIT \
6867
(_PyMainInterpreterConfig){\
69-
.install_signal_handlers = -1}
68+
.install_signal_handlers = -1, \
69+
.module_search_path_env = NULL}
7070

7171
typedef struct _is {
7272

‎Modules/getpath.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home,
456456
}
457457

458458
static void
459-
calculate_path(_PyCoreConfig *core_config)
459+
calculate_path(_PyMainInterpreterConfig *config)
460460
{
461461
extern wchar_t *Py_GetProgramName(void);
462462

@@ -706,9 +706,9 @@ calculate_path(_PyCoreConfig *core_config)
706706
bufsz = 0;
707707

708708
wchar_t *env_path = NULL;
709-
if (core_config) {
710-
if (core_config->module_search_path_env) {
711-
bufsz += wcslen(core_config->module_search_path_env) + 1;
709+
if (config) {
710+
if (config->module_search_path_env) {
711+
bufsz += wcslen(config->module_search_path_env) + 1;
712712
}
713713
}
714714
else {
@@ -752,9 +752,9 @@ calculate_path(_PyCoreConfig *core_config)
752752

753753
/* Run-time value of $PYTHONPATH goes first */
754754
buf[0] = '\0';
755-
if (core_config) {
756-
if (core_config->module_search_path_env) {
757-
wcscpy(buf, core_config->module_search_path_env);
755+
if (config) {
756+
if (config->module_search_path_env) {
757+
wcscpy(buf, config->module_search_path_env);
758758
wcscat(buf, delimiter);
759759
}
760760
}
@@ -858,10 +858,10 @@ Py_SetPath(const wchar_t *path)
858858
}
859859

860860
wchar_t *
861-
_Py_GetPathWithConfig(_PyCoreConfig *core_config)
861+
_Py_GetPathWithConfig(_PyMainInterpreterConfig *config)
862862
{
863863
if (!module_search_path) {
864-
calculate_path(core_config);
864+
calculate_path(config);
865865
}
866866
return module_search_path;
867867
}

‎Modules/main.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ typedef struct {
389389
/* non-zero is stdin is a TTY or if -i option is used */
390390
int stdin_is_interactive;
391391
_PyCoreConfig core_config;
392+
_PyMainInterpreterConfig config;
392393
_Py_CommandLineDetails cmdline;
393394
PyObject *main_importer_path;
394395
/* non-zero if filename, command (-c) or module (-m) is set
@@ -409,6 +410,7 @@ typedef struct {
409410
{.status = 0, \
410411
.cf = {.cf_flags = 0}, \
411412
.core_config = _PyCoreConfig_INIT, \
413+
.config = _PyMainInterpreterConfig_INIT, \
412414
.main_importer_path = NULL, \
413415
.run_code = -1, \
414416
.program_name = NULL, \
@@ -442,7 +444,7 @@ pymain_free_impl(_PyMain *pymain)
442444
Py_CLEAR(pymain->main_importer_path);
443445
PyMem_RawFree(pymain->program_name);
444446

445-
PyMem_RawFree(pymain->core_config.module_search_path_env);
447+
PyMem_RawFree(pymain->config.module_search_path_env);
446448

447449
#ifdef __INSURE__
448450
/* Insure++ is a memory analysis tool that aids in discovering
@@ -957,20 +959,16 @@ pymain_get_program_name(_PyMain *pymain)
957959
static int
958960
pymain_init_main_interpreter(_PyMain *pymain)
959961
{
960-
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
961962
_PyInitError err;
962963

963-
/* TODO: Moar config options! */
964-
config.install_signal_handlers = 1;
965-
966964
/* TODO: Print any exceptions raised by these operations */
967-
err = _Py_ReadMainInterpreterConfig(&config);
965+
err = _Py_ReadMainInterpreterConfig(&pymain->config);
968966
if (_Py_INIT_FAILED(err)) {
969967
pymain->err = err;
970968
return -1;
971969
}
972970

973-
err = _Py_InitializeMainInterpreter(&config);
971+
err = _Py_InitializeMainInterpreter(&pymain->config);
974972
if (_Py_INIT_FAILED(err)) {
975973
pymain->err = err;
976974
return -1;
@@ -1387,7 +1385,7 @@ pymain_init_pythonpath(_PyMain *pymain)
13871385
return -1;
13881386
}
13891387

1390-
pymain->core_config.module_search_path_env = path2;
1388+
pymain->config.module_search_path_env = path2;
13911389
#else
13921390
char *path = pymain_get_env_var("PYTHONPATH");
13931391
if (!path) {
@@ -1405,7 +1403,7 @@ pymain_init_pythonpath(_PyMain *pymain)
14051403
}
14061404
return -1;
14071405
}
1408-
pymain->core_config.module_search_path_env = wpath;
1406+
pymain->config.module_search_path_env = wpath;
14091407
#endif
14101408
return 0;
14111409
}
@@ -1574,6 +1572,8 @@ pymain_init(_PyMain *pymain)
15741572
}
15751573

15761574
pymain->core_config._disable_importlib = 0;
1575+
/* TODO: Moar config options! */
1576+
pymain->config.install_signal_handlers = 1;
15771577

15781578
orig_argc = pymain->argc; /* For Py_GetArgcArgv() */
15791579
orig_argv = pymain->argv;

‎PC/getpathp.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ read_pth_file(const wchar_t *path, wchar_t *prefix, int *isolated, int *nosite)
624624

625625

626626
static void
627-
calculate_path(_PyCoreConfig *core_config)
627+
calculate_path(_PyMainInterpreterConfig *config)
628628
{
629629
wchar_t argv0_path[MAXPATHLEN+1];
630630
wchar_t *buf;
@@ -637,8 +637,8 @@ calculate_path(_PyCoreConfig *core_config)
637637
wchar_t *userpath = NULL;
638638
wchar_t zip_path[MAXPATHLEN+1];
639639

640-
if (core_config) {
641-
envpath = core_config->module_search_path_env;
640+
if (config) {
641+
envpath = config->module_search_path_env;
642642
}
643643
else if (!Py_IgnoreEnvironmentFlag) {
644644
envpath = _wgetenv(L"PYTHONPATH");
@@ -899,10 +899,10 @@ Py_SetPath(const wchar_t *path)
899899
}
900900

901901
wchar_t *
902-
_Py_GetPathWithConfig(_PyCoreConfig *core_config)
902+
_Py_GetPathWithConfig(_PyMainInterpreterConfig *config)
903903
{
904904
if (!module_search_path) {
905-
calculate_path(core_config);
905+
calculate_path(config);
906906
}
907907
return module_search_path;
908908
}

‎Python/pylifecycle.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
843843
/* GetPath may initialize state that _PySys_EndInit locks
844844
in, and so has to be called first. */
845845
/* TODO: Call Py_GetPath() in Py_ReadConfig, rather than here */
846-
wchar_t *sys_path = _Py_GetPathWithConfig(&interp->core_config);
846+
wchar_t *sys_path = _Py_GetPathWithConfig(&interp->config);
847847

848848
if (interp->core_config._disable_importlib) {
849849
/* Special mode for freeze_importlib: run with no import system
@@ -1301,7 +1301,7 @@ new_interpreter(PyThreadState **tstate_p)
13011301

13021302
/* XXX The following is lax in error checking */
13031303

1304-
wchar_t *sys_path = _Py_GetPathWithConfig(&interp->core_config);
1304+
wchar_t *sys_path = _Py_GetPathWithConfig(&interp->config);
13051305

13061306
PyObject *modules = PyDict_New();
13071307
if (modules == NULL) {

0 commit comments

Comments
 (0)