From 048a35659aa8074afe7d7d054e7cea1f8ee6d366 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Nov 2020 00:45:56 +0100 Subject: bpo-42260: Add _PyInterpreterState_SetConfig() (GH-23158) * Inline _PyInterpreterState_SetConfig(): replace it with _PyConfig_Copy(). * Add _PyErr_SetFromPyStatus() * Add _PyInterpreterState_GetConfigCopy() * Add a new _PyInterpreterState_SetConfig() function. * Add an unit which gets, modifies, and sets the config. --- Programs/_testembed.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'Programs/_testembed.c') diff --git a/Programs/_testembed.c b/Programs/_testembed.c index 5aad16a6f7..cb3a23a101 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -1526,6 +1526,55 @@ static int test_init_warnoptions(void) } +static int tune_config(void) +{ + PyConfig config; + PyConfig_InitPythonConfig(&config); + if (_PyInterpreterState_GetConfigCopy(&config) < 0) { + PyConfig_Clear(&config); + PyErr_Print(); + return -1; + } + + config.bytes_warning = 2; + + if (_PyInterpreterState_SetConfig(&config) < 0) { + PyConfig_Clear(&config); + return -1; + } + PyConfig_Clear(&config); + return 0; +} + + +static int test_set_config(void) +{ + // Initialize core + PyConfig config; + PyConfig_InitIsolatedConfig(&config); + config_set_string(&config, &config.program_name, PROGRAM_NAME); + config._init_main = 0; + config.bytes_warning = 0; + init_from_config_clear(&config); + + // Tune the configuration using _PyInterpreterState_SetConfig() + if (tune_config() < 0) { + PyErr_Print(); + return 1; + } + + // Finish initialization: main part + PyStatus status = _Py_InitializeMain(); + if (PyStatus_Exception(status)) { + Py_ExitStatusException(status); + } + + dump_config(); + Py_Finalize(); + return 0; +} + + static void configure_init_main(PyConfig *config) { wchar_t* argv[] = { @@ -1693,6 +1742,7 @@ static struct TestCase TestCases[] = { {"test_init_setpath_config", test_init_setpath_config}, {"test_init_setpythonhome", test_init_setpythonhome}, {"test_init_warnoptions", test_init_warnoptions}, + {"test_init_set_config", test_set_config}, {"test_run_main", test_run_main}, {"test_get_argc_argv", test_get_argc_argv}, -- cgit v1.2.1