Skip to content

Commit 4a1468e

Browse files
authored
bpo-36356: Fix _PyCoreConfig_Read() (GH-12454)
Don't override parameters which are already set by the user.
1 parent f29084d commit 4a1468e

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

Python/coreconfig.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,15 @@ config_read_env_vars(_PyCoreConfig *config)
961961
config->malloc_stats = 1;
962962
}
963963

964-
wchar_t *path;
965-
int res = _PyCoreConfig_GetEnvDup(config, &path,
966-
L"PYTHONPATH", "PYTHONPATH");
967-
if (res < 0) {
968-
return DECODE_LOCALE_ERR("PYTHONPATH", res);
964+
if (config->module_search_path_env == NULL) {
965+
wchar_t *path;
966+
int res = _PyCoreConfig_GetEnvDup(config, &path,
967+
L"PYTHONPATH", "PYTHONPATH");
968+
if (res < 0) {
969+
return DECODE_LOCALE_ERR("PYTHONPATH", res);
970+
}
971+
config->module_search_path_env = path;
969972
}
970-
config->module_search_path_env = path;
971973

972974
if (config->use_hash_seed < 0) {
973975
_PyInitError err = config_init_hash_seed(config);
@@ -1689,28 +1691,32 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
16891691
}
16901692

16911693
if (c == 'c') {
1692-
/* -c is the last option; following arguments
1693-
that look like options are left for the
1694-
command to interpret. */
1695-
size_t len = wcslen(_PyOS_optarg) + 1 + 1;
1696-
wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
1697-
if (command == NULL) {
1698-
return _Py_INIT_NO_MEMORY();
1694+
if (config->run_command == NULL) {
1695+
/* -c is the last option; following arguments
1696+
that look like options are left for the
1697+
command to interpret. */
1698+
size_t len = wcslen(_PyOS_optarg) + 1 + 1;
1699+
wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
1700+
if (command == NULL) {
1701+
return _Py_INIT_NO_MEMORY();
1702+
}
1703+
memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
1704+
command[len - 2] = '\n';
1705+
command[len - 1] = 0;
1706+
config->run_command = command;
16991707
}
1700-
memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
1701-
command[len - 2] = '\n';
1702-
command[len - 1] = 0;
1703-
config->run_command = command;
17041708
break;
17051709
}
17061710

17071711
if (c == 'm') {
17081712
/* -m is the last option; following arguments
17091713
that look like options are left for the
17101714
module to interpret. */
1711-
config->run_module = _PyMem_RawWcsdup(_PyOS_optarg);
17121715
if (config->run_module == NULL) {
1713-
return _Py_INIT_NO_MEMORY();
1716+
config->run_module = _PyMem_RawWcsdup(_PyOS_optarg);
1717+
if (config->run_module == NULL) {
1718+
return _Py_INIT_NO_MEMORY();
1719+
}
17141720
}
17151721
break;
17161722
}
@@ -1825,7 +1831,8 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
18251831

18261832
if (config->run_command == NULL && config->run_module == NULL
18271833
&& _PyOS_optind < cmdline->argv.length
1828-
&& wcscmp(cmdline->argv.items[_PyOS_optind], L"-") != 0)
1834+
&& wcscmp(cmdline->argv.items[_PyOS_optind], L"-") != 0
1835+
&& config->run_filename == NULL)
18291836
{
18301837
config->run_filename = _PyMem_RawWcsdup(cmdline->argv.items[_PyOS_optind]);
18311838
if (config->run_filename == NULL) {
@@ -2032,9 +2039,11 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
20322039

20332040
_PyCoreConfig_GetGlobalConfig(config);
20342041

2035-
err = config_init_program(config, cmdline);
2036-
if (_Py_INIT_FAILED(err)) {
2037-
return err;
2042+
if (config->program == NULL) {
2043+
err = config_init_program(config, cmdline);
2044+
if (_Py_INIT_FAILED(err)) {
2045+
return err;
2046+
}
20382047
}
20392048

20402049
err = config_parse_cmdline(config, cmdline, &need_usage);

0 commit comments

Comments
 (0)