From 6415e2ee4955b1a995c1e75544e2506b03780c3d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 21 Jan 2022 02:12:18 +0100 Subject: bpo-46417: _testembed.c avoids Py_SetProgramName() (GH-30732) * _testembed_Py_Initialize() now uses the PyConfig API, rather than deprecated Py_SetProgramName(). * Reduce INIT_LOOPS from 16 to 4: test_embed now takes 8.7 seconds rather than 14.7 seconds. --- Lib/test/test_embed.py | 2 +- Programs/_testembed.c | 62 ++++++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index 204b194..19c53c3 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -32,7 +32,7 @@ API_PYTHON = 2 # _PyCoreConfig_InitIsolatedConfig() API_ISOLATED = 3 -INIT_LOOPS = 16 +INIT_LOOPS = 4 MAX_HASH_SEED = 4294967295 diff --git a/Programs/_testembed.c b/Programs/_testembed.c index 5bc0a12..08e27d9 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -30,7 +30,7 @@ char **main_argv; /* Use path starting with "./" avoids a search along the PATH */ #define PROGRAM_NAME L"./_testembed" -#define INIT_LOOPS 16 +#define INIT_LOOPS 4 // Ignore Py_DEPRECATED() compiler warnings: deprecated functions are // tested on purpose here. @@ -45,10 +45,39 @@ static void error(const char *msg) } +static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str) +{ + PyStatus status = PyConfig_SetString(config, config_str, str); + if (PyStatus_Exception(status)) { + PyConfig_Clear(config); + Py_ExitStatusException(status); + } +} + + +static void config_set_program_name(PyConfig *config) +{ + const wchar_t *program_name = PROGRAM_NAME; + config_set_string(config, &config->program_name, program_name); +} + + +static void init_from_config_clear(PyConfig *config) +{ + PyStatus status = Py_InitializeFromConfig(config); + PyConfig_Clear(config); + if (PyStatus_Exception(status)) { + Py_ExitStatusException(status); + } +} + + static void _testembed_Py_Initialize(void) { - Py_SetProgramName(PROGRAM_NAME); - Py_Initialize(); + PyConfig config; + _PyConfig_InitCompatConfig(&config); + config_set_program_name(&config); + init_from_config_clear(&config); } @@ -391,16 +420,6 @@ static int test_init_initialize_config(void) } -static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str) -{ - PyStatus status = PyConfig_SetString(config, config_str, str); - if (PyStatus_Exception(status)) { - PyConfig_Clear(config); - Py_ExitStatusException(status); - } -} - - static void config_set_argv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv) { PyStatus status = PyConfig_SetArgv(config, argc, argv); @@ -423,23 +442,6 @@ config_set_wide_string_list(PyConfig *config, PyWideStringList *list, } -static void config_set_program_name(PyConfig *config) -{ - const wchar_t *program_name = PROGRAM_NAME; - config_set_string(config, &config->program_name, program_name); -} - - -static void init_from_config_clear(PyConfig *config) -{ - PyStatus status = Py_InitializeFromConfig(config); - PyConfig_Clear(config); - if (PyStatus_Exception(status)) { - Py_ExitStatusException(status); - } -} - - static int check_init_compat_config(int preinit) { PyStatus status; -- cgit v0.12