Skip to content

Commit b5fd9ad

Browse files
authored
bpo-32030: Rewrite _PyMainInterpreterConfig (#4854)
_PyMainInterpreterConfig now contains Python objects, whereas _PyCoreConfig contains wchar_t* strings. Core config: * Rename _PyMainInterpreterConfig_ReadEnv() to _PyCoreConfig_ReadEnv() * Move 3 strings from _PyMainInterpreterConfig to _PyCoreConfig: module_search_path_env, home, program_name. * Add _PyCoreConfig_Clear() * _PyPathConfig_Calculate() now takes core config rather than main config * _PyMainInterpreterConfig_Read() now requires also a core config Main config: * Add _PyMainInterpreterConfig.module_search_path: sys.path list * Add _PyMainInterpreterConfig.argv: sys.argv list * _PyMainInterpreterConfig_Read() now computes module_search_path
1 parent 176baa3 commit b5fd9ad

File tree

8 files changed

+281
-193
lines changed

8 files changed

+281
-193
lines changed

Include/internal/pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ PyAPI_DATA(_PyPathConfig) _Py_path_config;
6161

6262
PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
6363
_PyPathConfig *config,
64-
const _PyMainInterpreterConfig *main_config);
64+
const _PyCoreConfig *core_config);
6565
PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
6666

6767

Include/pylifecycle.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ 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) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *);
58-
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *);
57+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadEnv(_PyCoreConfig *);
58+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *);
59+
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
60+
61+
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *, _PyCoreConfig *);
5962
PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *);
6063

6164
PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *);
@@ -103,8 +106,7 @@ PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
103106
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
104107
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
105108
#ifdef Py_BUILD_CORE
106-
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(
107-
const _PyMainInterpreterConfig *main_config);
109+
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
108110
PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
109111
#endif
110112
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);

Include/pystate.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ typedef struct {
3939
int dump_refs; /* PYTHONDUMPREFS */
4040
int malloc_stats; /* PYTHONMALLOCSTATS */
4141
int utf8_mode; /* -X utf8 or PYTHONUTF8 environment variable */
42+
43+
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
44+
wchar_t *home; /* PYTHONHOME environment variable,
45+
see also Py_SetPythonHome(). */
46+
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
4247
} _PyCoreConfig;
4348

4449
#define _PyCoreConfig_INIT (_PyCoreConfig){.use_hash_seed = -1}
@@ -52,12 +57,8 @@ typedef struct {
5257
*/
5358
typedef struct {
5459
int install_signal_handlers;
55-
/* PYTHONPATH environment variable */
56-
wchar_t *module_search_path_env;
57-
/* PYTHONHOME environment variable, see also Py_SetPythonHome(). */
58-
wchar_t *home;
59-
/* Program name, see also Py_GetProgramName() */
60-
wchar_t *program_name;
60+
PyObject *argv; /* sys.argv list, can be NULL */
61+
PyObject *module_search_path; /* sys.path list */
6162
} _PyMainInterpreterConfig;
6263

6364
#define _PyMainInterpreterConfig_INIT \

Modules/getpath.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@ find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
356356
bytes long.
357357
*/
358358
static int
359-
search_for_prefix(const _PyMainInterpreterConfig *main_config,
359+
search_for_prefix(const _PyCoreConfig *core_config,
360360
PyCalculatePath *calculate, wchar_t *prefix)
361361
{
362362
size_t n;
363363
wchar_t *vpath;
364364

365365
/* If PYTHONHOME is set, we believe it unconditionally */
366-
if (main_config->home) {
367-
wcsncpy(prefix, main_config->home, MAXPATHLEN);
366+
if (core_config->home) {
367+
wcsncpy(prefix, core_config->home, MAXPATHLEN);
368368
prefix[MAXPATHLEN] = L'\0';
369369
wchar_t *delim = wcschr(prefix, DELIM);
370370
if (delim) {
@@ -423,10 +423,10 @@ search_for_prefix(const _PyMainInterpreterConfig *main_config,
423423

424424

425425
static void
426-
calculate_prefix(const _PyMainInterpreterConfig *main_config,
426+
calculate_prefix(const _PyCoreConfig *core_config,
427427
PyCalculatePath *calculate, wchar_t *prefix)
428428
{
429-
calculate->prefix_found = search_for_prefix(main_config, calculate, prefix);
429+
calculate->prefix_found = search_for_prefix(core_config, calculate, prefix);
430430
if (!calculate->prefix_found) {
431431
if (!Py_FrozenFlag) {
432432
fprintf(stderr,
@@ -468,19 +468,19 @@ calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix)
468468
MAXPATHLEN bytes long.
469469
*/
470470
static int
471-
search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
471+
search_for_exec_prefix(const _PyCoreConfig *core_config,
472472
PyCalculatePath *calculate, wchar_t *exec_prefix)
473473
{
474474
size_t n;
475475

476476
/* If PYTHONHOME is set, we believe it unconditionally */
477-
if (main_config->home) {
478-
wchar_t *delim = wcschr(main_config->home, DELIM);
477+
if (core_config->home) {
478+
wchar_t *delim = wcschr(core_config->home, DELIM);
479479
if (delim) {
480480
wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
481481
}
482482
else {
483-
wcsncpy(exec_prefix, main_config->home, MAXPATHLEN);
483+
wcsncpy(exec_prefix, core_config->home, MAXPATHLEN);
484484
}
485485
exec_prefix[MAXPATHLEN] = L'\0';
486486
joinpath(exec_prefix, calculate->lib_python);
@@ -551,10 +551,10 @@ search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
551551

552552

553553
static void
554-
calculate_exec_prefix(const _PyMainInterpreterConfig *main_config,
554+
calculate_exec_prefix(const _PyCoreConfig *core_config,
555555
PyCalculatePath *calculate, wchar_t *exec_prefix)
556556
{
557-
calculate->exec_prefix_found = search_for_exec_prefix(main_config,
557+
calculate->exec_prefix_found = search_for_exec_prefix(core_config,
558558
calculate,
559559
exec_prefix);
560560
if (!calculate->exec_prefix_found) {
@@ -587,7 +587,7 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
587587

588588

589589
static _PyInitError
590-
calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
590+
calculate_program_full_path(const _PyCoreConfig *core_config,
591591
PyCalculatePath *calculate, _PyPathConfig *config)
592592
{
593593
wchar_t program_full_path[MAXPATHLEN+1];
@@ -607,8 +607,8 @@ calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
607607
* other way to find a directory to start the search from. If
608608
* $PATH isn't exported, you lose.
609609
*/
610-
if (wcschr(main_config->program_name, SEP)) {
611-
wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
610+
if (wcschr(core_config->program_name, SEP)) {
611+
wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
612612
}
613613
#ifdef __APPLE__
614614
/* On Mac OS X, if a script uses an interpreter of the form
@@ -650,7 +650,7 @@ calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
650650
wcsncpy(program_full_path, path, MAXPATHLEN);
651651
}
652652

653-
joinpath(program_full_path, main_config->program_name);
653+
joinpath(program_full_path, core_config->program_name);
654654
if (isxfile(program_full_path)) {
655655
break;
656656
}
@@ -815,15 +815,15 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
815815

816816

817817
static _PyInitError
818-
calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
818+
calculate_module_search_path(const _PyCoreConfig *core_config,
819819
PyCalculatePath *calculate,
820820
const wchar_t *prefix, const wchar_t *exec_prefix,
821821
_PyPathConfig *config)
822822
{
823823
/* Calculate size of return buffer */
824824
size_t bufsz = 0;
825-
if (main_config->module_search_path_env != NULL) {
826-
bufsz += wcslen(main_config->module_search_path_env) + 1;
825+
if (core_config->module_search_path_env != NULL) {
826+
bufsz += wcslen(core_config->module_search_path_env) + 1;
827827
}
828828

829829
wchar_t *defpath = calculate->pythonpath;
@@ -857,8 +857,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
857857
buf[0] = '\0';
858858

859859
/* Run-time value of $PYTHONPATH goes first */
860-
if (main_config->module_search_path_env) {
861-
wcscpy(buf, main_config->module_search_path_env);
860+
if (core_config->module_search_path_env) {
861+
wcscpy(buf, core_config->module_search_path_env);
862862
wcscat(buf, delimiter);
863863
}
864864

@@ -907,7 +907,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
907907

908908
static _PyInitError
909909
calculate_init(PyCalculatePath *calculate,
910-
const _PyMainInterpreterConfig *main_config)
910+
const _PyCoreConfig *core_config)
911911
{
912912
size_t len;
913913
const char *path = getenv("PATH");
@@ -950,12 +950,12 @@ calculate_free(PyCalculatePath *calculate)
950950

951951

952952
static _PyInitError
953-
calculate_path_impl(const _PyMainInterpreterConfig *main_config,
953+
calculate_path_impl(const _PyCoreConfig *core_config,
954954
PyCalculatePath *calculate, _PyPathConfig *config)
955955
{
956956
_PyInitError err;
957957

958-
err = calculate_program_full_path(main_config, calculate, config);
958+
err = calculate_program_full_path(core_config, calculate, config);
959959
if (_Py_INIT_FAILED(err)) {
960960
return err;
961961
}
@@ -969,13 +969,13 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
969969

970970
wchar_t prefix[MAXPATHLEN+1];
971971
memset(prefix, 0, sizeof(prefix));
972-
calculate_prefix(main_config, calculate, prefix);
972+
calculate_prefix(core_config, calculate, prefix);
973973

974974
calculate_zip_path(calculate, prefix);
975975

976976
wchar_t exec_prefix[MAXPATHLEN+1];
977977
memset(exec_prefix, 0, sizeof(exec_prefix));
978-
calculate_exec_prefix(main_config, calculate, exec_prefix);
978+
calculate_exec_prefix(core_config, calculate, exec_prefix);
979979

980980
if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
981981
!Py_FrozenFlag)
@@ -984,7 +984,7 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
984984
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
985985
}
986986

987-
err = calculate_module_search_path(main_config, calculate,
987+
err = calculate_module_search_path(core_config, calculate,
988988
prefix, exec_prefix, config);
989989
if (_Py_INIT_FAILED(err)) {
990990
return err;
@@ -1009,18 +1009,17 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
10091009

10101010

10111011
_PyInitError
1012-
_PyPathConfig_Calculate(_PyPathConfig *config,
1013-
const _PyMainInterpreterConfig *main_config)
1012+
_PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config)
10141013
{
10151014
PyCalculatePath calculate;
10161015
memset(&calculate, 0, sizeof(calculate));
10171016

1018-
_PyInitError err = calculate_init(&calculate, main_config);
1017+
_PyInitError err = calculate_init(&calculate, core_config);
10191018
if (_Py_INIT_FAILED(err)) {
10201019
goto done;
10211020
}
10221021

1023-
err = calculate_path_impl(main_config, &calculate, config);
1022+
err = calculate_path_impl(core_config, &calculate, config);
10241023
if (_Py_INIT_FAILED(err)) {
10251024
goto done;
10261025
}

0 commit comments

Comments
 (0)