mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
bpo-32030: Add _PyMainInterpreterConfig.pythonhome (#4513)
* Py_Main() now reads the PYTHONHOME environment variable * Add _Py_GetPythonHomeWithConfig() private function * Add _PyWarnings_InitWithConfig() * init_filters() doesn't get the current core configuration from the current interpreter or Python thread anymore. Pass explicitly the configuration to _PyWarnings_InitWithConfig(). * _Py_InitializeCore() now fails on _PyWarnings_InitWithConfig() failure. * Pass configuration as constant
This commit is contained in:
parent
e32e79f7d8
commit
1f15111a6e
8 changed files with 136 additions and 43 deletions
|
|
@ -400,7 +400,6 @@ typedef struct {
|
|||
_PyInitError err;
|
||||
/* PYTHONWARNINGS env var */
|
||||
_Py_OptList env_warning_options;
|
||||
/* PYTHONPATH env var */
|
||||
int argc;
|
||||
wchar_t **argv;
|
||||
} _PyMain;
|
||||
|
|
@ -1368,43 +1367,94 @@ pymain_set_flags_from_env(_PyMain *pymain)
|
|||
|
||||
|
||||
static int
|
||||
pymain_init_pythonpath(_PyMain *pymain)
|
||||
pymain_get_env_var_dup(_PyMain *pymain, wchar_t **dest,
|
||||
wchar_t *wname, char *name)
|
||||
{
|
||||
if (Py_IgnoreEnvironmentFlag) {
|
||||
*dest = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
wchar_t *path = _wgetenv(L"PYTHONPATH");
|
||||
if (!path || path[0] == '\0') {
|
||||
wchar_t *var = _wgetenv(wname);
|
||||
if (!var || var[0] == '\0') {
|
||||
*dest = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t *path2 = pymain_wstrdup(pymain, path);
|
||||
if (path2 == NULL) {
|
||||
wchar_t *copy = pymain_wstrdup(pymain, var);
|
||||
if (copy == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pymain->config.module_search_path_env = path2;
|
||||
*dest = copy;
|
||||
#else
|
||||
char *path = pymain_get_env_var("PYTHONPATH");
|
||||
if (!path) {
|
||||
char *var = getenv(name);
|
||||
if (!var || var[0] == '\0') {
|
||||
*dest = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t len;
|
||||
wchar_t *wpath = Py_DecodeLocale(path, &len);
|
||||
if (!wpath) {
|
||||
wchar_t *wvar = Py_DecodeLocale(var, &len);
|
||||
if (!wvar) {
|
||||
if (len == (size_t)-2) {
|
||||
pymain->err = _Py_INIT_ERR("failed to decode PYTHONHOME");
|
||||
/* don't set pymain->err */
|
||||
return -2;
|
||||
}
|
||||
else {
|
||||
pymain->err = INIT_NO_MEMORY();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
*dest = wvar;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
pymain_init_pythonpath(_PyMain *pymain)
|
||||
{
|
||||
wchar_t *path;
|
||||
int res = pymain_get_env_var_dup(pymain, &path,
|
||||
L"PYTHONPATH", "PYTHONPATH");
|
||||
if (res < 0) {
|
||||
if (res == -2) {
|
||||
pymain->err = _Py_INIT_ERR("failed to decode PYTHONPATH");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
pymain->config.module_search_path_env = wpath;
|
||||
#endif
|
||||
pymain->config.module_search_path_env = path;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
pymain_init_pythonhome(_PyMain *pymain)
|
||||
{
|
||||
wchar_t *home;
|
||||
|
||||
home = Py_GetPythonHome();
|
||||
if (home) {
|
||||
/* Py_SetPythonHome() has been called before Py_Main(),
|
||||
use its value */
|
||||
pymain->config.pythonhome = pymain_wstrdup(pymain, home);
|
||||
if (pymain->config.pythonhome == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int res = pymain_get_env_var_dup(pymain, &home,
|
||||
L"PYTHONHOME", "PYTHONHOME");
|
||||
if (res < 0) {
|
||||
if (res == -2) {
|
||||
pymain->err = _Py_INIT_ERR("failed to decode PYTHONHOME");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
pymain->config.pythonhome = home;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -1433,6 +1483,9 @@ pymain_parse_envvars(_PyMain *pymain)
|
|||
if (pymain_init_pythonpath(pymain) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (pymain_init_pythonhome(pymain) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -X options */
|
||||
if (pymain_get_xoption(pymain, L"showrefcount")) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue