Skip to content

Commit b423ae6

Browse files
authored
gh-107954, PEP 741: Adjust Python initialization config (#123663)
Setting dev_mode to 1 in an isolated configuration now enables also faulthandler. Moreover, setting "module_search_paths" option with PyInitConfig_SetStrList() now sets "module_search_paths_set" to 1.
1 parent 7bd964d commit b423ae6

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

Lib/test/test_embed.py

-1
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,6 @@ def test_initconfig_api(self):
17661766
'use_hash_seed': True,
17671767
}
17681768
config_dev_mode(preconfig, config)
1769-
config['faulthandler'] = 0
17701769
self.check_all_configs("test_initconfig_api", config, preconfig,
17711770
api=API_ISOLATED)
17721771

Programs/_testembed.c

+34-12
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,16 @@ static int test_init_set_config(void)
18061806
}
18071807

18081808

1809+
static int initconfig_getint(PyInitConfig *config, const char *name)
1810+
{
1811+
int64_t value;
1812+
int res = PyInitConfig_GetInt(config, name, &value);
1813+
assert(res == 0);
1814+
assert(INT_MIN <= value && value <= INT_MAX);
1815+
return (int)value;
1816+
}
1817+
1818+
18091819
static int test_initconfig_api(void)
18101820
{
18111821
PyInitConfig *config = PyInitConfig_Create();
@@ -1844,7 +1854,6 @@ static int test_initconfig_api(void)
18441854
goto error;
18451855
}
18461856

1847-
18481857
if (Py_InitializeFromInitConfig(config) < 0) {
18491858
goto error;
18501859
}
@@ -1876,38 +1885,51 @@ static int test_initconfig_get_api(void)
18761885
assert(PyInitConfig_HasOption(config, "non-existent") == 0);
18771886

18781887
// test PyInitConfig_GetInt()
1879-
int64_t value;
1880-
assert(PyInitConfig_GetInt(config, "dev_mode", &value) == 0);
1881-
assert(value == 0);
1888+
assert(initconfig_getint(config, "dev_mode") == 0);
18821889
assert(PyInitConfig_SetInt(config, "dev_mode", 1) == 0);
1883-
assert(PyInitConfig_GetInt(config, "dev_mode", &value) == 0);
1884-
assert(value == 1);
1890+
assert(initconfig_getint(config, "dev_mode") == 1);
18851891

18861892
// test PyInitConfig_GetInt() on a PyPreConfig option
1887-
assert(PyInitConfig_GetInt(config, "utf8_mode", &value) == 0);
1888-
assert(value == 0);
1893+
assert(initconfig_getint(config, "utf8_mode") == 0);
18891894
assert(PyInitConfig_SetInt(config, "utf8_mode", 1) == 0);
1890-
assert(PyInitConfig_GetInt(config, "utf8_mode", &value) == 0);
1891-
assert(value == 1);
1895+
assert(initconfig_getint(config, "utf8_mode") == 1);
18921896

18931897
// test PyInitConfig_GetStr()
18941898
char *str;
1899+
assert(PyInitConfig_GetStr(config, "program_name", &str) == 0);
1900+
assert(str == NULL);
18951901
assert(PyInitConfig_SetStr(config, "program_name", PROGRAM_NAME_UTF8) == 0);
18961902
assert(PyInitConfig_GetStr(config, "program_name", &str) == 0);
18971903
assert(strcmp(str, PROGRAM_NAME_UTF8) == 0);
18981904
free(str);
18991905

19001906
// test PyInitConfig_GetStrList() and PyInitConfig_FreeStrList()
1907+
size_t length;
1908+
char **items;
1909+
assert(PyInitConfig_GetStrList(config, "xoptions", &length, &items) == 0);
1910+
assert(length == 0);
1911+
19011912
char* xoptions[] = {"faulthandler"};
19021913
assert(PyInitConfig_SetStrList(config, "xoptions",
19031914
Py_ARRAY_LENGTH(xoptions), xoptions) == 0);
1904-
size_t length;
1905-
char **items;
1915+
19061916
assert(PyInitConfig_GetStrList(config, "xoptions", &length, &items) == 0);
19071917
assert(length == 1);
19081918
assert(strcmp(items[0], "faulthandler") == 0);
19091919
PyInitConfig_FreeStrList(length, items);
19101920

1921+
// Setting hash_seed sets use_hash_seed
1922+
assert(initconfig_getint(config, "use_hash_seed") == 0);
1923+
assert(PyInitConfig_SetInt(config, "hash_seed", 123) == 0);
1924+
assert(initconfig_getint(config, "use_hash_seed") == 1);
1925+
1926+
// Setting module_search_paths sets module_search_paths_set
1927+
assert(initconfig_getint(config, "module_search_paths_set") == 0);
1928+
char* paths[] = {"search", "path"};
1929+
assert(PyInitConfig_SetStrList(config, "module_search_paths",
1930+
Py_ARRAY_LENGTH(paths), paths) == 0);
1931+
assert(initconfig_getint(config, "module_search_paths_set") == 1);
1932+
19111933
return 0;
19121934
}
19131935

Python/initconfig.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,6 @@ PyConfig_InitIsolatedConfig(PyConfig *config)
10311031
config->dev_mode = 0;
10321032
config->install_signal_handlers = 0;
10331033
config->use_hash_seed = 0;
1034-
config->faulthandler = 0;
10351034
config->tracemalloc = 0;
10361035
config->perf_profiling = 0;
10371036
config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
@@ -3753,7 +3752,7 @@ PyInitConfig_SetInt(PyInitConfig *config, const char *name, int64_t value)
37533752
return -1;
37543753
}
37553754

3756-
if (strcmp(name, "hash_seed")) {
3755+
if (strcmp(name, "hash_seed") == 0) {
37573756
config->config.use_hash_seed = 1;
37583757
}
37593758

@@ -3863,7 +3862,14 @@ PyInitConfig_SetStrList(PyInitConfig *config, const char *name,
38633862
return -1;
38643863
}
38653864
PyWideStringList *list = raw_member;
3866-
return _PyWideStringList_FromUTF8(config, list, length, items);
3865+
if (_PyWideStringList_FromUTF8(config, list, length, items) < 0) {
3866+
return -1;
3867+
}
3868+
3869+
if (strcmp(name, "module_search_paths") == 0) {
3870+
config->config.module_search_paths_set = 1;
3871+
}
3872+
return 0;
38673873
}
38683874

38693875

0 commit comments

Comments
 (0)