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.
This commit is contained in:
Victor Stinner 2024-09-04 12:58:32 +02:00 committed by GitHub
parent 7bd964dbbe
commit b423ae6b08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 16 deletions

View File

@ -1766,7 +1766,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'use_hash_seed': True,
}
config_dev_mode(preconfig, config)
config['faulthandler'] = 0
self.check_all_configs("test_initconfig_api", config, preconfig,
api=API_ISOLATED)

View File

@ -1806,6 +1806,16 @@ static int test_init_set_config(void)
}
static int initconfig_getint(PyInitConfig *config, const char *name)
{
int64_t value;
int res = PyInitConfig_GetInt(config, name, &value);
assert(res == 0);
assert(INT_MIN <= value && value <= INT_MAX);
return (int)value;
}
static int test_initconfig_api(void)
{
PyInitConfig *config = PyInitConfig_Create();
@ -1844,7 +1854,6 @@ static int test_initconfig_api(void)
goto error;
}
if (Py_InitializeFromInitConfig(config) < 0) {
goto error;
}
@ -1876,38 +1885,51 @@ static int test_initconfig_get_api(void)
assert(PyInitConfig_HasOption(config, "non-existent") == 0);
// test PyInitConfig_GetInt()
int64_t value;
assert(PyInitConfig_GetInt(config, "dev_mode", &value) == 0);
assert(value == 0);
assert(initconfig_getint(config, "dev_mode") == 0);
assert(PyInitConfig_SetInt(config, "dev_mode", 1) == 0);
assert(PyInitConfig_GetInt(config, "dev_mode", &value) == 0);
assert(value == 1);
assert(initconfig_getint(config, "dev_mode") == 1);
// test PyInitConfig_GetInt() on a PyPreConfig option
assert(PyInitConfig_GetInt(config, "utf8_mode", &value) == 0);
assert(value == 0);
assert(initconfig_getint(config, "utf8_mode") == 0);
assert(PyInitConfig_SetInt(config, "utf8_mode", 1) == 0);
assert(PyInitConfig_GetInt(config, "utf8_mode", &value) == 0);
assert(value == 1);
assert(initconfig_getint(config, "utf8_mode") == 1);
// test PyInitConfig_GetStr()
char *str;
assert(PyInitConfig_GetStr(config, "program_name", &str) == 0);
assert(str == NULL);
assert(PyInitConfig_SetStr(config, "program_name", PROGRAM_NAME_UTF8) == 0);
assert(PyInitConfig_GetStr(config, "program_name", &str) == 0);
assert(strcmp(str, PROGRAM_NAME_UTF8) == 0);
free(str);
// test PyInitConfig_GetStrList() and PyInitConfig_FreeStrList()
size_t length;
char **items;
assert(PyInitConfig_GetStrList(config, "xoptions", &length, &items) == 0);
assert(length == 0);
char* xoptions[] = {"faulthandler"};
assert(PyInitConfig_SetStrList(config, "xoptions",
Py_ARRAY_LENGTH(xoptions), xoptions) == 0);
size_t length;
char **items;
assert(PyInitConfig_GetStrList(config, "xoptions", &length, &items) == 0);
assert(length == 1);
assert(strcmp(items[0], "faulthandler") == 0);
PyInitConfig_FreeStrList(length, items);
// Setting hash_seed sets use_hash_seed
assert(initconfig_getint(config, "use_hash_seed") == 0);
assert(PyInitConfig_SetInt(config, "hash_seed", 123) == 0);
assert(initconfig_getint(config, "use_hash_seed") == 1);
// Setting module_search_paths sets module_search_paths_set
assert(initconfig_getint(config, "module_search_paths_set") == 0);
char* paths[] = {"search", "path"};
assert(PyInitConfig_SetStrList(config, "module_search_paths",
Py_ARRAY_LENGTH(paths), paths) == 0);
assert(initconfig_getint(config, "module_search_paths_set") == 1);
return 0;
}

View File

@ -1031,7 +1031,6 @@ PyConfig_InitIsolatedConfig(PyConfig *config)
config->dev_mode = 0;
config->install_signal_handlers = 0;
config->use_hash_seed = 0;
config->faulthandler = 0;
config->tracemalloc = 0;
config->perf_profiling = 0;
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)
return -1;
}
if (strcmp(name, "hash_seed")) {
if (strcmp(name, "hash_seed") == 0) {
config->config.use_hash_seed = 1;
}
@ -3863,7 +3862,14 @@ PyInitConfig_SetStrList(PyInitConfig *config, const char *name,
return -1;
}
PyWideStringList *list = raw_member;
return _PyWideStringList_FromUTF8(config, list, length, items);
if (_PyWideStringList_FromUTF8(config, list, length, items) < 0) {
return -1;
}
if (strcmp(name, "module_search_paths") == 0) {
config->config.module_search_paths_set = 1;
}
return 0;
}