Skip to content

Commit 9ac3d88

Browse files
authored
bpo-32030: Fix Py_GetPath(): init program_name (#4665)
* _PyMainInterpreterConfig_ReadEnv() now sets program_name from environment variables and pymain_parse_envvars() implements the falls back on argv[0]. * Remove _PyMain.program_name: use the program_name from _PyMainInterpreterConfig * Move the Py_SetProgramName() call back to pymain_init_python(), just before _Py_InitializeCore(). * pathconfig_global_init() now also calls _PyMainInterpreterConfig_Read() to set program_name if it isn't set yet * Cleanup PyCalculatePath: pass main_config to subfunctions to get directly fields from main_config (home, module_search_path_env and program_name)
1 parent b64de46 commit 9ac3d88

File tree

3 files changed

+124
-102
lines changed

3 files changed

+124
-102
lines changed

Modules/getpath.c

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ extern "C" {
117117

118118
typedef struct {
119119
wchar_t *path_env; /* PATH environment variable */
120-
wchar_t *home; /* PYTHONHOME environment variable */
121-
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
122120

123-
wchar_t *program_name; /* Program name */
124121
wchar_t *pythonpath; /* PYTHONPATH define */
125122
wchar_t *prefix; /* PREFIX define */
126123
wchar_t *exec_prefix; /* EXEC_PREFIX define */
@@ -360,14 +357,15 @@ find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
360357
bytes long.
361358
*/
362359
static int
363-
search_for_prefix(PyCalculatePath *calculate, wchar_t *prefix)
360+
search_for_prefix(const _PyMainInterpreterConfig *main_config,
361+
PyCalculatePath *calculate, wchar_t *prefix)
364362
{
365363
size_t n;
366364
wchar_t *vpath;
367365

368366
/* If PYTHONHOME is set, we believe it unconditionally */
369-
if (calculate->home) {
370-
wcsncpy(prefix, calculate->home, MAXPATHLEN);
367+
if (main_config->home) {
368+
wcsncpy(prefix, main_config->home, MAXPATHLEN);
371369
prefix[MAXPATHLEN] = L'\0';
372370
wchar_t *delim = wcschr(prefix, DELIM);
373371
if (delim) {
@@ -426,9 +424,10 @@ search_for_prefix(PyCalculatePath *calculate, wchar_t *prefix)
426424

427425

428426
static void
429-
calculate_prefix(PyCalculatePath *calculate, wchar_t *prefix)
427+
calculate_prefix(const _PyMainInterpreterConfig *main_config,
428+
PyCalculatePath *calculate, wchar_t *prefix)
430429
{
431-
calculate->prefix_found = search_for_prefix(calculate, prefix);
430+
calculate->prefix_found = search_for_prefix(main_config, calculate, prefix);
432431
if (!calculate->prefix_found) {
433432
if (!Py_FrozenFlag) {
434433
fprintf(stderr,
@@ -470,18 +469,19 @@ calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix)
470469
MAXPATHLEN bytes long.
471470
*/
472471
static int
473-
search_for_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
472+
search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
473+
PyCalculatePath *calculate, wchar_t *exec_prefix)
474474
{
475475
size_t n;
476476

477477
/* If PYTHONHOME is set, we believe it unconditionally */
478-
if (calculate->home) {
479-
wchar_t *delim = wcschr(calculate->home, DELIM);
478+
if (main_config->home) {
479+
wchar_t *delim = wcschr(main_config->home, DELIM);
480480
if (delim) {
481481
wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
482482
}
483483
else {
484-
wcsncpy(exec_prefix, calculate->home, MAXPATHLEN);
484+
wcsncpy(exec_prefix, main_config->home, MAXPATHLEN);
485485
}
486486
exec_prefix[MAXPATHLEN] = L'\0';
487487
joinpath(exec_prefix, calculate->lib_python);
@@ -552,9 +552,12 @@ search_for_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
552552

553553

554554
static void
555-
calculate_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
555+
calculate_exec_prefix(const _PyMainInterpreterConfig *main_config,
556+
PyCalculatePath *calculate, wchar_t *exec_prefix)
556557
{
557-
calculate->exec_prefix_found = search_for_exec_prefix(calculate, exec_prefix);
558+
calculate->exec_prefix_found = search_for_exec_prefix(main_config,
559+
calculate,
560+
exec_prefix);
558561
if (!calculate->exec_prefix_found) {
559562
if (!Py_FrozenFlag) {
560563
fprintf(stderr,
@@ -585,7 +588,8 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
585588

586589

587590
static _PyInitError
588-
calculate_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config)
591+
calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
592+
PyCalculatePath *calculate, _PyPathConfig *config)
589593
{
590594
wchar_t program_full_path[MAXPATHLEN+1];
591595
memset(program_full_path, 0, sizeof(program_full_path));
@@ -604,8 +608,8 @@ calculate_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config)
604608
* other way to find a directory to start the search from. If
605609
* $PATH isn't exported, you lose.
606610
*/
607-
if (wcschr(calculate->program_name, SEP)) {
608-
wcsncpy(program_full_path, calculate->program_name, MAXPATHLEN);
611+
if (wcschr(main_config->program_name, SEP)) {
612+
wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
609613
}
610614
#ifdef __APPLE__
611615
/* On Mac OS X, if a script uses an interpreter of the form
@@ -645,7 +649,7 @@ calculate_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config)
645649
wcsncpy(program_full_path, path, MAXPATHLEN);
646650
}
647651

648-
joinpath(program_full_path, calculate->program_name);
652+
joinpath(program_full_path, main_config->program_name);
649653
if (isxfile(program_full_path)) {
650654
break;
651655
}
@@ -810,14 +814,15 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
810814

811815

812816
static _PyInitError
813-
calculate_module_search_path(PyCalculatePath *calculate,
817+
calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
818+
PyCalculatePath *calculate,
814819
const wchar_t *prefix, const wchar_t *exec_prefix,
815820
_PyPathConfig *config)
816821
{
817822
/* Calculate size of return buffer */
818823
size_t bufsz = 0;
819-
if (calculate->module_search_path_env != NULL) {
820-
bufsz += wcslen(calculate->module_search_path_env) + 1;
824+
if (main_config->module_search_path_env != NULL) {
825+
bufsz += wcslen(main_config->module_search_path_env) + 1;
821826
}
822827

823828
wchar_t *defpath = calculate->pythonpath;
@@ -851,8 +856,8 @@ calculate_module_search_path(PyCalculatePath *calculate,
851856
buf[0] = '\0';
852857

853858
/* Run-time value of $PYTHONPATH goes first */
854-
if (calculate->module_search_path_env) {
855-
wcscpy(buf, calculate->module_search_path_env);
859+
if (main_config->module_search_path_env) {
860+
wcscpy(buf, main_config->module_search_path_env);
856861
wcscat(buf, delimiter);
857862
}
858863

@@ -903,10 +908,6 @@ static _PyInitError
903908
calculate_init(PyCalculatePath *calculate,
904909
const _PyMainInterpreterConfig *main_config)
905910
{
906-
calculate->home = main_config->home;
907-
calculate->module_search_path_env = main_config->module_search_path_env;
908-
calculate->program_name = main_config->program_name;
909-
910911
size_t len;
911912
char *path = getenv("PATH");
912913
if (path) {
@@ -948,9 +949,12 @@ calculate_free(PyCalculatePath *calculate)
948949

949950

950951
static _PyInitError
951-
calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
952+
calculate_path_impl(const _PyMainInterpreterConfig *main_config,
953+
PyCalculatePath *calculate, _PyPathConfig *config)
952954
{
953-
_PyInitError err = calculate_program_full_path(calculate, config);
955+
_PyInitError err;
956+
957+
err = calculate_program_full_path(main_config, calculate, config);
954958
if (_Py_INIT_FAILED(err)) {
955959
return err;
956960
}
@@ -964,13 +968,13 @@ calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
964968

965969
wchar_t prefix[MAXPATHLEN+1];
966970
memset(prefix, 0, sizeof(prefix));
967-
calculate_prefix(calculate, prefix);
971+
calculate_prefix(main_config, calculate, prefix);
968972

969973
calculate_zip_path(calculate, prefix);
970974

971975
wchar_t exec_prefix[MAXPATHLEN+1];
972976
memset(exec_prefix, 0, sizeof(exec_prefix));
973-
calculate_exec_prefix(calculate, exec_prefix);
977+
calculate_exec_prefix(main_config, calculate, exec_prefix);
974978

975979
if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
976980
!Py_FrozenFlag)
@@ -979,8 +983,8 @@ calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
979983
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
980984
}
981985

982-
err = calculate_module_search_path(calculate, prefix, exec_prefix,
983-
config);
986+
err = calculate_module_search_path(main_config, calculate,
987+
prefix, exec_prefix, config);
984988
if (_Py_INIT_FAILED(err)) {
985989
return err;
986990
}
@@ -1041,7 +1045,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
10411045
_PyPathConfig new_path_config;
10421046
memset(&new_path_config, 0, sizeof(new_path_config));
10431047

1044-
err = calculate_path_impl(&calculate, &new_path_config);
1048+
err = calculate_path_impl(main_config, &calculate, &new_path_config);
10451049
if (_Py_INIT_FAILED(err)) {
10461050
pathconfig_clear(&new_path_config);
10471051
goto done;
@@ -1068,14 +1072,26 @@ pathconfig_global_init(void)
10681072
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
10691073

10701074
err = _PyMainInterpreterConfig_ReadEnv(&config);
1071-
if (!_Py_INIT_FAILED(err)) {
1072-
err = _PyPathConfig_Init(&config);
1075+
if (_Py_INIT_FAILED(err)) {
1076+
goto error;
1077+
}
1078+
1079+
err = _PyMainInterpreterConfig_Read(&config);
1080+
if (_Py_INIT_FAILED(err)) {
1081+
goto error;
10731082
}
1074-
_PyMainInterpreterConfig_Clear(&config);
10751083

1084+
err = _PyPathConfig_Init(&config);
10761085
if (_Py_INIT_FAILED(err)) {
1077-
_Py_FatalInitError(err);
1086+
goto error;
10781087
}
1088+
1089+
_PyMainInterpreterConfig_Clear(&config);
1090+
return;
1091+
1092+
error:
1093+
_PyMainInterpreterConfig_Clear(&config);
1094+
_Py_FatalInitError(err);
10791095
}
10801096

10811097

Modules/main.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ typedef struct {
412412
/* non-zero if filename, command (-c) or module (-m) is set
413413
on the command line */
414414
int run_code;
415-
wchar_t *program_name;
416415
/* Error message if a function failed */
417416
_PyInitError err;
418417
/* PYTHONWARNINGS env var */
@@ -429,7 +428,6 @@ typedef struct {
429428
.config = _PyMainInterpreterConfig_INIT, \
430429
.main_importer_path = NULL, \
431430
.run_code = -1, \
432-
.program_name = NULL, \
433431
.err = _Py_INIT_OK(), \
434432
.env_warning_options = {0, NULL}}
435433

@@ -455,7 +453,6 @@ pymain_free_impl(_PyMain *pymain)
455453

456454
pymain_optlist_clear(&pymain->env_warning_options);
457455
Py_CLEAR(pymain->main_importer_path);
458-
PyMem_RawFree(pymain->program_name);
459456

460457
_PyMainInterpreterConfig_Clear(&pymain->config);
461458

@@ -874,14 +871,11 @@ pymain_init_stdio(_PyMain *pymain)
874871

875872

876873
/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
877-
environment variables on macOS if available, use argv[0] by default.
878-
879-
Return 0 on success.
880-
Set pymain->err and return -1 on error. */
881-
static int
882-
pymain_get_program_name(_PyMain *pymain)
874+
environment variables on macOS if available. */
875+
static _PyInitError
876+
config_get_program_name(_PyMainInterpreterConfig *config)
883877
{
884-
assert(pymain->program_name == NULL);
878+
assert(config->program_name == NULL);
885879
#ifdef __APPLE__
886880
char *p;
887881
/* On MacOS X, when the Python interpreter is embedded in an
@@ -899,12 +893,11 @@ pymain_get_program_name(_PyMain *pymain)
899893

900894
buffer = PyMem_RawMalloc(len * sizeof(wchar_t));
901895
if (buffer == NULL) {
902-
pymain->err = _Py_INIT_NO_MEMORY();
903-
return -1;
896+
return _Py_INIT_NO_MEMORY();
904897
}
905898

906899
mbstowcs(buffer, p, len);
907-
pymain->program_name = buffer;
900+
pymain->config.program_name = buffer;
908901
}
909902
#ifdef WITH_NEXT_FRAMEWORK
910903
else {
@@ -916,19 +909,26 @@ pymain_get_program_name(_PyMain *pymain)
916909
size_t len;
917910
wchar_t* wbuf = Py_DecodeLocale(pyvenv_launcher, &len);
918911
if (wbuf == NULL) {
919-
SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len);
920-
return -1;
912+
return SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len);
921913
}
922-
pymain->program_name = wbuf;
914+
pymain->config.program_name = wbuf;
923915
}
924916
}
925917
#endif /* WITH_NEXT_FRAMEWORK */
926918
#endif /* __APPLE__ */
919+
return _Py_INIT_OK();
920+
}
927921

928-
if (pymain->program_name == NULL) {
922+
923+
/* If config_get_program_name() found no program name: use argv[0] by default.
924+
Return 0 on success. Set pymain->err and return -1 on error. */
925+
static int
926+
pymain_get_program_name(_PyMain *pymain)
927+
{
928+
if (pymain->config.program_name == NULL) {
929929
/* Use argv[0] by default */
930-
pymain->program_name = pymain_wstrdup(pymain, pymain->argv[0]);
931-
if (pymain->program_name == NULL) {
930+
pymain->config.program_name = pymain_wstrdup(pymain, pymain->argv[0]);
931+
if (pymain->config.program_name == NULL) {
932932
return -1;
933933
}
934934
}
@@ -1451,11 +1451,9 @@ _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config)
14511451
return err;
14521452
}
14531453

1454-
/* FIXME: _PyMainInterpreterConfig_Read() has the same code. Remove it
1455-
here? See also pymain_get_program_name() and pymain_parse_envvars(). */
1456-
config->program_name = _PyMem_RawWcsdup(Py_GetProgramName());
1457-
if (config->program_name == NULL) {
1458-
return _Py_INIT_NO_MEMORY();
1454+
err = config_get_program_name(config);
1455+
if (_Py_INIT_FAILED(err)) {
1456+
return err;
14591457
}
14601458

14611459
return _Py_INIT_OK();
@@ -1481,25 +1479,17 @@ pymain_parse_envvars(_PyMain *pymain)
14811479
if (pymain_warnings_envvar(pymain) < 0) {
14821480
return -1;
14831481
}
1484-
if (pymain_get_program_name(pymain) < 0) {
1485-
return -1;
1486-
}
1487-
core_config->allocator = Py_GETENV("PYTHONMALLOC");
1488-
1489-
/* FIXME: move pymain_get_program_name() code into
1490-
_PyMainInterpreterConfig_ReadEnv().
1491-
Problem: _PyMainInterpreterConfig_ReadEnv() doesn't have access
1492-
to argv[0]. */
1493-
Py_SetProgramName(pymain->program_name);
1494-
/* Don't free program_name here: the argument to Py_SetProgramName
1495-
must remain valid until Py_FinalizeEx is called. The string is freed
1496-
by pymain_free(). */
14971482

14981483
_PyInitError err = _PyMainInterpreterConfig_ReadEnv(&pymain->config);
14991484
if (_Py_INIT_FAILED(pymain->err)) {
15001485
pymain->err = err;
15011486
return -1;
15021487
}
1488+
if (pymain_get_program_name(pymain) < 0) {
1489+
return -1;
1490+
}
1491+
1492+
core_config->allocator = Py_GETENV("PYTHONMALLOC");
15031493

15041494
/* -X options */
15051495
if (pymain_get_xoption(pymain, L"showrefcount")) {
@@ -1578,6 +1568,11 @@ pymain_init_python(_PyMain *pymain)
15781568
{
15791569
pymain_init_stdio(pymain);
15801570

1571+
Py_SetProgramName(pymain->config.program_name);
1572+
/* Don't free program_name here: the argument to Py_SetProgramName
1573+
must remain valid until Py_FinalizeEx is called. The string is freed
1574+
by pymain_free(). */
1575+
15811576
pymain->err = _Py_InitializeCore(&pymain->core_config);
15821577
if (_Py_INIT_FAILED(pymain->err)) {
15831578
return -1;

0 commit comments

Comments
 (0)