bpo-32030: Fix Py_GetPath(): init program_name (#4665) · python/cpython@9ac3d88 (original) (raw)
`@@ -117,10 +117,7 @@ extern "C" {
`
117
117
``
118
118
`typedef struct {
`
119
119
`wchar_t path_env; / PATH environment variable */
`
120
``
`-
wchar_t home; / PYTHONHOME environment variable */
`
121
``
`-
wchar_t module_search_path_env; / PYTHONPATH environment variable */
`
122
120
``
123
``
`-
wchar_t program_name; / Program name */
`
124
121
`wchar_t pythonpath; / PYTHONPATH define */
`
125
122
`wchar_t prefix; / PREFIX define */
`
126
123
`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)
`
360
357
` bytes long.
`
361
358
`*/
`
362
359
`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)
`
364
362
`{
`
365
363
`size_t n;
`
366
364
`wchar_t *vpath;
`
367
365
``
368
366
`/* 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);
`
371
369
`prefix[MAXPATHLEN] = L'\0';
`
372
370
`wchar_t *delim = wcschr(prefix, DELIM);
`
373
371
`if (delim) {
`
`@@ -426,9 +424,10 @@ search_for_prefix(PyCalculatePath *calculate, wchar_t *prefix)
`
426
424
``
427
425
``
428
426
`static void
`
429
``
`-
calculate_prefix(PyCalculatePath *calculate, wchar_t *prefix)
`
``
427
`+
calculate_prefix(const _PyMainInterpreterConfig *main_config,
`
``
428
`+
PyCalculatePath *calculate, wchar_t *prefix)
`
430
429
`{
`
431
``
`-
calculate->prefix_found = search_for_prefix(calculate, prefix);
`
``
430
`+
calculate->prefix_found = search_for_prefix(main_config, calculate, prefix);
`
432
431
`if (!calculate->prefix_found) {
`
433
432
`if (!Py_FrozenFlag) {
`
434
433
`fprintf(stderr,
`
`@@ -470,18 +469,19 @@ calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix)
`
470
469
` MAXPATHLEN bytes long.
`
471
470
`*/
`
472
471
`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)
`
474
474
`{
`
475
475
`size_t n;
`
476
476
``
477
477
`/* 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);
`
480
480
`if (delim) {
`
481
481
`wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
`
482
482
` }
`
483
483
`else {
`
484
``
`-
wcsncpy(exec_prefix, calculate->home, MAXPATHLEN);
`
``
484
`+
wcsncpy(exec_prefix, main_config->home, MAXPATHLEN);
`
485
485
` }
`
486
486
`exec_prefix[MAXPATHLEN] = L'\0';
`
487
487
`joinpath(exec_prefix, calculate->lib_python);
`
`@@ -552,9 +552,12 @@ search_for_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
`
552
552
``
553
553
``
554
554
`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)
`
556
557
`{
`
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);
`
558
561
`if (!calculate->exec_prefix_found) {
`
559
562
`if (!Py_FrozenFlag) {
`
560
563
`fprintf(stderr,
`
`@@ -585,7 +588,8 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
`
585
588
``
586
589
``
587
590
`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)
`
589
593
`{
`
590
594
`wchar_t program_full_path[MAXPATHLEN+1];
`
591
595
`memset(program_full_path, 0, sizeof(program_full_path));
`
`@@ -604,8 +608,8 @@ calculate_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config)
`
604
608
` * other way to find a directory to start the search from. If
`
605
609
` * $PATH isn't exported, you lose.
`
606
610
` */
`
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);
`
609
613
` }
`
610
614
`#ifdef APPLE
`
611
615
`/* 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)
`
645
649
`wcsncpy(program_full_path, path, MAXPATHLEN);
`
646
650
` }
`
647
651
``
648
``
`-
joinpath(program_full_path, calculate->program_name);
`
``
652
`+
joinpath(program_full_path, main_config->program_name);
`
649
653
`if (isxfile(program_full_path)) {
`
650
654
`break;
`
651
655
` }
`
`@@ -810,14 +814,15 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
`
810
814
``
811
815
``
812
816
`static _PyInitError
`
813
``
`-
calculate_module_search_path(PyCalculatePath *calculate,
`
``
817
`+
calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
`
``
818
`+
PyCalculatePath *calculate,
`
814
819
`const wchar_t *prefix, const wchar_t *exec_prefix,
`
815
820
`_PyPathConfig *config)
`
816
821
`{
`
817
822
`/* Calculate size of return buffer */
`
818
823
`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;
`
821
826
` }
`
822
827
``
823
828
`wchar_t *defpath = calculate->pythonpath;
`
`@@ -851,8 +856,8 @@ calculate_module_search_path(PyCalculatePath *calculate,
`
851
856
`buf[0] = '\0';
`
852
857
``
853
858
`/* 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);
`
856
861
`wcscat(buf, delimiter);
`
857
862
` }
`
858
863
``
`@@ -903,10 +908,6 @@ static _PyInitError
`
903
908
`calculate_init(PyCalculatePath *calculate,
`
904
909
`const _PyMainInterpreterConfig *main_config)
`
905
910
`{
`
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
``
-
910
911
`size_t len;
`
911
912
`char *path = getenv("PATH");
`
912
913
`if (path) {
`
`@@ -948,9 +949,12 @@ calculate_free(PyCalculatePath *calculate)
`
948
949
``
949
950
``
950
951
`static _PyInitError
`
951
``
`-
calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
`
``
952
`+
calculate_path_impl(const _PyMainInterpreterConfig *main_config,
`
``
953
`+
PyCalculatePath *calculate, _PyPathConfig *config)
`
952
954
`{
`
953
``
`-
_PyInitError err = calculate_program_full_path(calculate, config);
`
``
955
`+
_PyInitError err;
`
``
956
+
``
957
`+
err = calculate_program_full_path(main_config, calculate, config);
`
954
958
`if (_Py_INIT_FAILED(err)) {
`
955
959
`return err;
`
956
960
` }
`
`@@ -964,13 +968,13 @@ calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
`
964
968
``
965
969
`wchar_t prefix[MAXPATHLEN+1];
`
966
970
`memset(prefix, 0, sizeof(prefix));
`
967
``
`-
calculate_prefix(calculate, prefix);
`
``
971
`+
calculate_prefix(main_config, calculate, prefix);
`
968
972
``
969
973
`calculate_zip_path(calculate, prefix);
`
970
974
``
971
975
`wchar_t exec_prefix[MAXPATHLEN+1];
`
972
976
`memset(exec_prefix, 0, sizeof(exec_prefix));
`
973
``
`-
calculate_exec_prefix(calculate, exec_prefix);
`
``
977
`+
calculate_exec_prefix(main_config, calculate, exec_prefix);
`
974
978
``
975
979
`if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
`
976
980
` !Py_FrozenFlag)
`
`@@ -979,8 +983,8 @@ calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config)
`
979
983
`"Consider setting $PYTHONHOME to [:]\n");
`
980
984
` }
`
981
985
``
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);
`
984
988
`if (_Py_INIT_FAILED(err)) {
`
985
989
`return err;
`
986
990
` }
`
`@@ -1041,7 +1045,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
`
1041
1045
`_PyPathConfig new_path_config;
`
1042
1046
`memset(&new_path_config, 0, sizeof(new_path_config));
`
1043
1047
``
1044
``
`-
err = calculate_path_impl(&calculate, &new_path_config);
`
``
1048
`+
err = calculate_path_impl(main_config, &calculate, &new_path_config);
`
1045
1049
`if (_Py_INIT_FAILED(err)) {
`
1046
1050
`pathconfig_clear(&new_path_config);
`
1047
1051
` goto done;
`
`@@ -1068,14 +1072,26 @@ pathconfig_global_init(void)
`
1068
1072
`_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
`
1069
1073
``
1070
1074
`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;
`
1073
1082
` }
`
1074
``
`-
_PyMainInterpreterConfig_Clear(&config);
`
1075
1083
``
``
1084
`+
err = _PyPathConfig_Init(&config);
`
1076
1085
`if (_Py_INIT_FAILED(err)) {
`
1077
``
`-
_Py_FatalInitError(err);
`
``
1086
`+
goto error;
`
1078
1087
` }
`
``
1088
+
``
1089
`+
_PyMainInterpreterConfig_Clear(&config);
`
``
1090
`+
return;
`
``
1091
+
``
1092
`+
error:
`
``
1093
`+
_PyMainInterpreterConfig_Clear(&config);
`
``
1094
`+
_Py_FatalInitError(err);
`
1079
1095
`}
`
1080
1096
``
1081
1097
``