Skip to content

Commit af5a895

Browse files
authored
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().
1 parent e23c06e commit af5a895

File tree

8 files changed

+180
-97
lines changed

8 files changed

+180
-97
lines changed

Doc/c-api/init.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ The following functions can be safely called before Python is initialized:
4040
* :c:func:`Py_GetCompiler`
4141
* :c:func:`Py_GetCopyright`
4242
* :c:func:`Py_GetPlatform`
43-
* :c:func:`Py_GetProgramName`
4443
* :c:func:`Py_GetVersion`
4544

4645
* Utilities:
@@ -59,8 +58,8 @@ The following functions can be safely called before Python is initialized:
5958
The following functions **should not be called** before
6059
:c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
6160
:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
62-
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
63-
:c:func:`PyEval_InitThreads`.
61+
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`,
62+
:c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`.
6463

6564

6665
.. _global-conf-vars:

Include/internal/pystate.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,35 @@ typedef struct {
4848
#endif
4949
/* Set by Py_SetPath(), or computed by _PyPathConfig_Init() */
5050
wchar_t *module_search_path;
51+
/* Python program name */
52+
wchar_t *program_name;
53+
/* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
54+
wchar_t *home;
5155
} _PyPathConfig;
5256

53-
#define _PyPathConfig_INIT {.module_search_path = NULL}
57+
#ifdef MS_WINDOWS
58+
#define _PyPathConfig_INIT \
59+
{.program_full_path = NULL, \
60+
.prefix = NULL, \
61+
.dll_path = NULL, \
62+
.module_search_path = NULL, \
63+
.program_name = NULL, \
64+
.home = NULL}
65+
#else
66+
#define _PyPathConfig_INIT \
67+
{.program_full_path = NULL, \
68+
.prefix = NULL, \
69+
.exec_prefix = NULL, \
70+
.module_search_path = NULL, \
71+
.program_name = NULL, \
72+
.home = NULL}
73+
#endif
5474

5575
PyAPI_DATA(_PyPathConfig) _Py_path_config;
5676

77+
PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
78+
_PyPathConfig *config,
79+
const _PyMainInterpreterConfig *main_config);
5780
PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
5881

5982

Include/pystate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ typedef struct {
7272
(_PyMainInterpreterConfig){\
7373
.install_signal_handlers = -1, \
7474
.module_search_path_env = NULL, \
75-
.home = NULL}
75+
.home = NULL, \
76+
.program_name = NULL}
7677

7778
typedef struct _is {
7879

Modules/getpath.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,10 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
10081008
}
10091009

10101010

1011-
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
1012-
and Py_GetProgramFullPath() */
10131011
_PyInitError
1014-
_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1012+
_PyPathConfig_Calculate(_PyPathConfig *config,
1013+
const _PyMainInterpreterConfig *main_config)
10151014
{
1016-
if (_Py_path_config.module_search_path) {
1017-
/* Already initialized */
1018-
return _Py_INIT_OK();
1019-
}
1020-
10211015
PyCalculatePath calculate;
10221016
memset(&calculate, 0, sizeof(calculate));
10231017

@@ -1026,16 +1020,11 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
10261020
goto done;
10271021
}
10281022

1029-
_PyPathConfig new_path_config;
1030-
memset(&new_path_config, 0, sizeof(new_path_config));
1031-
1032-
err = calculate_path_impl(main_config, &calculate, &new_path_config);
1023+
err = calculate_path_impl(main_config, &calculate, config);
10331024
if (_Py_INIT_FAILED(err)) {
1034-
_PyPathConfig_Clear(&new_path_config);
10351025
goto done;
10361026
}
10371027

1038-
_Py_path_config = new_path_config;
10391028
err = _Py_INIT_OK();
10401029

10411030
done:

Modules/main.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,16 @@ static _PyInitError
876876
config_get_program_name(_PyMainInterpreterConfig *config)
877877
{
878878
assert(config->program_name == NULL);
879+
880+
/* If Py_SetProgramName() was called, use its value */
881+
wchar_t *program_name = _Py_path_config.program_name;
882+
if (program_name != NULL) {
883+
config->program_name = _PyMem_RawWcsdup(program_name);
884+
if (config->program_name == NULL) {
885+
return _Py_INIT_NO_MEMORY();
886+
}
887+
}
888+
879889
#ifdef __APPLE__
880890
char *p;
881891
/* On MacOS X, when the Python interpreter is embedded in an
@@ -914,6 +924,7 @@ config_get_program_name(_PyMainInterpreterConfig *config)
914924
}
915925
#endif /* WITH_NEXT_FRAMEWORK */
916926
#endif /* __APPLE__ */
927+
917928
return _Py_INIT_OK();
918929
}
919930

@@ -948,13 +959,6 @@ pymain_init_main_interpreter(_PyMain *pymain)
948959
{
949960
_PyInitError err;
950961

951-
/* TODO: Print any exceptions raised by these operations */
952-
err = _PyMainInterpreterConfig_Read(&pymain->config);
953-
if (_Py_INIT_FAILED(err)) {
954-
pymain->err = err;
955-
return -1;
956-
}
957-
958962
err = _Py_InitializeMainInterpreter(&pymain->config);
959963
if (_Py_INIT_FAILED(err)) {
960964
pymain->err = err;
@@ -1412,14 +1416,13 @@ config_init_pythonpath(_PyMainInterpreterConfig *config)
14121416

14131417

14141418
static _PyInitError
1415-
config_init_pythonhome(_PyMainInterpreterConfig *config)
1419+
config_init_home(_PyMainInterpreterConfig *config)
14161420
{
14171421
wchar_t *home;
14181422

1419-
home = Py_GetPythonHome();
1423+
/* If Py_SetPythonHome() was called, use its value */
1424+
home = _Py_path_config.home;
14201425
if (home) {
1421-
/* Py_SetPythonHome() has been called before Py_Main(),
1422-
use its value */
14231426
config->home = _PyMem_RawWcsdup(home);
14241427
if (config->home == NULL) {
14251428
return _Py_INIT_NO_MEMORY();
@@ -1439,7 +1442,7 @@ config_init_pythonhome(_PyMainInterpreterConfig *config)
14391442
_PyInitError
14401443
_PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config)
14411444
{
1442-
_PyInitError err = config_init_pythonhome(config);
1445+
_PyInitError err = config_init_home(config);
14431446
if (_Py_INIT_FAILED(err)) {
14441447
return err;
14451448
}
@@ -1543,6 +1546,12 @@ pymain_parse_cmdline_envvars_impl(_PyMain *pymain)
15431546
return -1;
15441547
}
15451548

1549+
_PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config);
1550+
if (_Py_INIT_FAILED(err)) {
1551+
pymain->err = err;
1552+
return -1;
1553+
}
1554+
15461555
return 0;
15471556
}
15481557

@@ -1566,11 +1575,6 @@ pymain_init_python(_PyMain *pymain)
15661575
{
15671576
pymain_init_stdio(pymain);
15681577

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

PC/getpathp.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,38 +1058,23 @@ calculate_free(PyCalculatePath *calculate)
10581058
}
10591059

10601060

1061-
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
1062-
and Py_GetProgramFullPath() */
10631061
_PyInitError
1064-
_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1062+
_PyPathConfig_Calculate(_PyPathConfig *config,
1063+
const _PyMainInterpreterConfig *main_config)
10651064
{
1066-
if (_Py_path_config.module_search_path) {
1067-
/* Already initialized */
1068-
return _Py_INIT_OK();
1069-
}
1070-
1071-
_PyInitError err;
1072-
10731065
PyCalculatePath calculate;
10741066
memset(&calculate, 0, sizeof(calculate));
10751067

10761068
calculate_init(&calculate, main_config);
10771069

1078-
_PyPathConfig new_path_config;
1079-
memset(&new_path_config, 0, sizeof(new_path_config));
1080-
1081-
err = calculate_path_impl(main_config, &calculate, &new_path_config);
1070+
_PyInitError err = calculate_path_impl(main_config, &calculate, config);
10821071
if (_Py_INIT_FAILED(err)) {
10831072
goto done;
10841073
}
10851074

1086-
_Py_path_config = new_path_config;
10871075
err = _Py_INIT_OK();
10881076

10891077
done:
1090-
if (_Py_INIT_FAILED(err)) {
1091-
_PyPathConfig_Clear(&new_path_config);
1092-
}
10931078
calculate_free(&calculate);
10941079
return err;
10951080
}

0 commit comments

Comments
 (0)