bpo-32030: Add _PyMainInterpreterConfig_ReadEnv() (#4542) · python/cpython@46972b7 (original) (raw)
`@@ -120,7 +120,6 @@ typedef struct {
`
120
120
`wchar_t path_env; / PATH environment variable */
`
121
121
`wchar_t home; / PYTHONHOME environment variable */
`
122
122
`wchar_t module_search_path_env; / PYTHONPATH environment variable */
`
123
``
`-
wchar_t *module_search_path_buffer;
`
124
123
``
125
124
`wchar_t prog; / Program name */
`
126
125
`wchar_t pythonpath; / PYTHONPATH define */
`
`@@ -886,67 +885,45 @@ calculate_module_search_path(PyCalculatePath *calculate, PyPathConfig *config)
`
886
885
`}
`
887
886
``
888
887
``
889
``
`-
#define DECODE_FAILED(NAME, LEN) \
`
``
888
`+
#define DECODE_LOCALE_ERR(NAME, LEN) \
`
890
889
` ((LEN) == (size_t)-2) \
`
891
``
`-
? _Py_INIT_ERR("failed to decode " #NAME) \
`
``
890
`+
? _Py_INIT_USER_ERR("failed to decode " #NAME) \
`
892
891
` : _Py_INIT_NO_MEMORY()
`
893
892
``
894
893
``
895
894
`static _PyInitError
`
896
895
`calculate_init(PyCalculatePath *calculate,
`
897
896
`const _PyMainInterpreterConfig *main_config)
`
898
897
`{
`
899
``
`-
_PyInitError err;
`
900
``
-
901
``
`-
err = _Py_GetPythonHomeWithConfig(main_config, &calculate->home);
`
902
``
`-
if (_Py_INIT_FAILED(err)) {
`
903
``
`-
return err;
`
904
``
`-
}
`
``
898
`+
calculate->home = main_config->home;
`
``
899
`+
calculate->module_search_path_env = main_config->module_search_path_env;
`
905
900
``
906
901
`size_t len;
`
907
902
`char *path = getenv("PATH");
`
908
903
`if (path) {
`
909
904
`calculate->path_env = Py_DecodeLocale(path, &len);
`
910
905
`if (!calculate->path_env) {
`
911
``
`-
return DECODE_FAILED("PATH environment variable", len);
`
``
906
`+
return DECODE_LOCALE_ERR("PATH environment variable", len);
`
912
907
` }
`
913
908
` }
`
914
909
``
915
910
`calculate->prog = Py_GetProgramName();
`
916
911
``
917
912
`calculate->pythonpath = Py_DecodeLocale(PYTHONPATH, &len);
`
918
913
`if (!calculate->pythonpath) {
`
919
``
`-
return DECODE_FAILED("PYTHONPATH define", len);
`
``
914
`+
return DECODE_LOCALE_ERR("PYTHONPATH define", len);
`
920
915
` }
`
921
916
`calculate->prefix = Py_DecodeLocale(PREFIX, &len);
`
922
917
`if (!calculate->prefix) {
`
923
``
`-
return DECODE_FAILED("PREFIX define", len);
`
``
918
`+
return DECODE_LOCALE_ERR("PREFIX define", len);
`
924
919
` }
`
925
920
`calculate->exec_prefix = Py_DecodeLocale(EXEC_PREFIX, &len);
`
926
921
`if (!calculate->prefix) {
`
927
``
`-
return DECODE_FAILED("EXEC_PREFIX define", len);
`
``
922
`+
return DECODE_LOCALE_ERR("EXEC_PREFIX define", len);
`
928
923
` }
`
929
924
`calculate->lib_python = Py_DecodeLocale("lib/python" VERSION, &len);
`
930
925
`if (!calculate->lib_python) {
`
931
``
`-
return DECODE_FAILED("EXEC_PREFIX define", len);
`
932
``
`-
}
`
933
``
-
934
``
`-
calculate->module_search_path_env = NULL;
`
935
``
`-
if (main_config) {
`
936
``
`-
if (main_config->module_search_path_env) {
`
937
``
`-
calculate->module_search_path_env = main_config->module_search_path_env;
`
938
``
`-
}
`
939
``
-
940
``
`-
}
`
941
``
`-
else {
`
942
``
`-
char *pythonpath = Py_GETENV("PYTHONPATH");
`
943
``
`-
if (pythonpath && pythonpath[0] != '\0') {
`
944
``
`-
calculate->module_search_path_buffer = Py_DecodeLocale(pythonpath, &len);
`
945
``
`-
if (!calculate->module_search_path_buffer) {
`
946
``
`-
return DECODE_FAILED("PYTHONPATH environment variable", len);
`
947
``
`-
}
`
948
``
`-
calculate->module_search_path_env = calculate->module_search_path_buffer;
`
949
``
`-
}
`
``
926
`+
return DECODE_LOCALE_ERR("EXEC_PREFIX define", len);
`
950
927
` }
`
951
928
`return _Py_INIT_OK();
`
952
929
`}
`
`@@ -960,7 +937,6 @@ calculate_free(PyCalculatePath *calculate)
`
960
937
`PyMem_RawFree(calculate->exec_prefix);
`
961
938
`PyMem_RawFree(calculate->lib_python);
`
962
939
`PyMem_RawFree(calculate->path_env);
`
963
``
`-
PyMem_RawFree(calculate->module_search_path_buffer);
`
964
940
`}
`
965
941
``
966
942
``
`@@ -988,13 +964,24 @@ calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config)
`
988
964
`static void
`
989
965
`calculate_path(const _PyMainInterpreterConfig *main_config)
`
990
966
`{
`
``
967
`+
_PyInitError err;
`
991
968
`PyCalculatePath calculate;
`
992
969
`memset(&calculate, 0, sizeof(calculate));
`
993
970
``
994
``
`-
_PyInitError err = calculate_init(&calculate, main_config);
`
``
971
`+
_PyMainInterpreterConfig tmp_config;
`
``
972
`+
int use_tmp = (main_config == NULL);
`
``
973
`+
if (use_tmp) {
`
``
974
`+
tmp_config = _PyMainInterpreterConfig_INIT;
`
``
975
`+
err = _PyMainInterpreterConfig_ReadEnv(&tmp_config);
`
``
976
`+
if (_Py_INIT_FAILED(err)) {
`
``
977
`+
goto fatal_error;
`
``
978
`+
}
`
``
979
`+
main_config = &tmp_config;
`
``
980
`+
}
`
``
981
+
``
982
`+
err = calculate_init(&calculate, main_config);
`
995
983
`if (_Py_INIT_FAILED(err)) {
`
996
``
`-
calculate_free(&calculate);
`
997
``
`-
_Py_FatalInitError(err);
`
``
984
`+
goto fatal_error;
`
998
985
` }
`
999
986
``
1000
987
`PyPathConfig new_path_config;
`
`@@ -1003,7 +990,18 @@ calculate_path(const _PyMainInterpreterConfig *main_config)
`
1003
990
`calculate_path_impl(&calculate, &new_path_config);
`
1004
991
`path_config = new_path_config;
`
1005
992
``
``
993
`+
if (use_tmp) {
`
``
994
`+
_PyMainInterpreterConfig_Clear(&tmp_config);
`
``
995
`+
}
`
``
996
`+
calculate_free(&calculate);
`
``
997
`+
return;
`
``
998
+
``
999
`+
fatal_error:
`
``
1000
`+
if (use_tmp) {
`
``
1001
`+
_PyMainInterpreterConfig_Clear(&tmp_config);
`
``
1002
`+
}
`
1006
1003
`calculate_free(&calculate);
`
``
1004
`+
_Py_FatalInitError(err);
`
1007
1005
`}
`
1008
1006
``
1009
1007
``