bpo-32030: _PyPathConfig_Init() sets home and program_name (#4673)

_PyPathConfig_Init() now also initialize home and program_name:

* Rename existing _PyPathConfig_Init() to _PyPathConfig_Calculate().
  Add a new _PyPathConfig_Init() function in pathconfig.c which
  handles the _Py_path_config variable and call
  _PyPathConfig_Calculate().
* Add home and program_name fields to _PyPathConfig.home
* _PyPathConfig_Init() now initialize home and program_name
  from main_config
* Py_SetProgramName(), Py_SetPythonHome() and Py_GetPythonHome() now
  calls Py_FatalError() on failure, instead of silently ignoring
  failures.
* config_init_home() now gets directly _Py_path_config.home to only
  get the value set by Py_SetPythonHome(), or NULL if
  Py_SetPythonHome() was not called.
* config_get_program_name() now gets directly
  _Py_path_config.program_name to only get the value set by
  Py_SetProgramName(), or NULL if Py_SetProgramName() was not called.
* pymain_init_python() doesn't call Py_SetProgramName() anymore,
  _PyPathConfig_Init() now always sets the program name
* Call _PyMainInterpreterConfig_Read() in
  pymain_parse_cmdline_envvars_impl() to control the memory allocator
* C API documentation: it's no more safe to call Py_GetProgramName()
  before Py_Initialize().
This commit is contained in:
Victor Stinner 2017-12-02 10:11:32 +01:00 committed by GitHub
parent e23c06e2b0
commit af5a895073
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 184 additions and 101 deletions

View file

@ -1008,16 +1008,10 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
}
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
and Py_GetProgramFullPath() */
_PyInitError
_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
_PyPathConfig_Calculate(_PyPathConfig *config,
const _PyMainInterpreterConfig *main_config)
{
if (_Py_path_config.module_search_path) {
/* Already initialized */
return _Py_INIT_OK();
}
PyCalculatePath calculate;
memset(&calculate, 0, sizeof(calculate));
@ -1026,16 +1020,11 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
goto done;
}
_PyPathConfig new_path_config;
memset(&new_path_config, 0, sizeof(new_path_config));
err = calculate_path_impl(main_config, &calculate, &new_path_config);
err = calculate_path_impl(main_config, &calculate, config);
if (_Py_INIT_FAILED(err)) {
_PyPathConfig_Clear(&new_path_config);
goto done;
}
_Py_path_config = new_path_config;
err = _Py_INIT_OK();
done:

View file

@ -876,6 +876,16 @@ static _PyInitError
config_get_program_name(_PyMainInterpreterConfig *config)
{
assert(config->program_name == NULL);
/* If Py_SetProgramName() was called, use its value */
wchar_t *program_name = _Py_path_config.program_name;
if (program_name != NULL) {
config->program_name = _PyMem_RawWcsdup(program_name);
if (config->program_name == NULL) {
return _Py_INIT_NO_MEMORY();
}
}
#ifdef __APPLE__
char *p;
/* On MacOS X, when the Python interpreter is embedded in an
@ -914,6 +924,7 @@ config_get_program_name(_PyMainInterpreterConfig *config)
}
#endif /* WITH_NEXT_FRAMEWORK */
#endif /* __APPLE__ */
return _Py_INIT_OK();
}
@ -948,13 +959,6 @@ pymain_init_main_interpreter(_PyMain *pymain)
{
_PyInitError err;
/* TODO: Print any exceptions raised by these operations */
err = _PyMainInterpreterConfig_Read(&pymain->config);
if (_Py_INIT_FAILED(err)) {
pymain->err = err;
return -1;
}
err = _Py_InitializeMainInterpreter(&pymain->config);
if (_Py_INIT_FAILED(err)) {
pymain->err = err;
@ -1412,14 +1416,13 @@ config_init_pythonpath(_PyMainInterpreterConfig *config)
static _PyInitError
config_init_pythonhome(_PyMainInterpreterConfig *config)
config_init_home(_PyMainInterpreterConfig *config)
{
wchar_t *home;
home = Py_GetPythonHome();
/* If Py_SetPythonHome() was called, use its value */
home = _Py_path_config.home;
if (home) {
/* Py_SetPythonHome() has been called before Py_Main(),
use its value */
config->home = _PyMem_RawWcsdup(home);
if (config->home == NULL) {
return _Py_INIT_NO_MEMORY();
@ -1439,7 +1442,7 @@ config_init_pythonhome(_PyMainInterpreterConfig *config)
_PyInitError
_PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config)
{
_PyInitError err = config_init_pythonhome(config);
_PyInitError err = config_init_home(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
@ -1543,6 +1546,12 @@ pymain_parse_cmdline_envvars_impl(_PyMain *pymain)
return -1;
}
_PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config);
if (_Py_INIT_FAILED(err)) {
pymain->err = err;
return -1;
}
return 0;
}
@ -1566,11 +1575,6 @@ pymain_init_python(_PyMain *pymain)
{
pymain_init_stdio(pymain);
Py_SetProgramName(pymain->config.program_name);
/* Don't free program_name here: the argument to Py_SetProgramName
must remain valid until Py_FinalizeEx is called. The string is freed
by pymain_free(). */
pymain->err = _Py_InitializeCore(&pymain->core_config);
if (_Py_INIT_FAILED(pymain->err)) {
return -1;