bpo-36763: Add _PyCoreConfig_InitPythonConfig() (GH-13388)

Add new functions to get the Python interpreter behavior:

* _PyPreConfig_InitPythonConfig()
* _PyCoreConfig_InitPythonConfig()

Add new functions to get an isolated configuration:

* _PyPreConfig_InitIsolatedConfig()
* _PyCoreConfig_InitIsolatedConfig()

Replace _PyPreConfig_INIT and _PyCoreConfig_INIT with new functions
_PyPreConfig_Init() and _PyCoreConfig_Init().

_PyCoreConfig: set configure_c_stdio and parse_argv to 0 by default
to behave as Python 3.6 in the default configuration.

_PyCoreConfig_Read() no longer sets coerce_c_locale_warn to 1 if it's
equal to 0. coerce_c_locale_warn must now be set to -1 (ex: using
_PyCoreConfig_InitPythonConfig()) to enable C locale coercion
warning.

Add unit tests for _PyCoreConfig_InitPythonConfig()
and _PyCoreConfig_InitIsolatedConfig().

Changes:

* Rename _PyCoreConfig_GetCoreConfig() to _PyPreConfig_GetCoreConfig()
* Fix core_read_precmdline(): handle parse_argv=0
* Fix _Py_PreInitializeFromCoreConfig(): pass coreconfig.argv
  to _Py_PreInitializeFromPyArgv(), except if parse_argv=0
This commit is contained in:
Victor Stinner 2019-05-17 19:01:14 +02:00 committed by GitHub
parent b16b4e4592
commit cab5d0741e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 362 additions and 91 deletions

View file

@ -109,7 +109,7 @@ static const char usage_6[] =
/* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
stdin and stdout error handler to "surrogateescape". It is equal to
-1 by default: unknown, will be set by Py_Main() */
int Py_UTF8Mode = -1;
int Py_UTF8Mode = 0;
int Py_DebugFlag = 0; /* Needed by parser.c */
int Py_VerboseFlag = 0; /* Needed by import.c */
int Py_QuietFlag = 0; /* Needed by sysmodule.c */
@ -520,6 +520,61 @@ _PyCoreConfig_Clear(_PyCoreConfig *config)
}
void
_PyCoreConfig_Init(_PyCoreConfig *config)
{
*config = _PyCoreConfig_INIT;
}
_PyInitError
_PyCoreConfig_InitPythonConfig(_PyCoreConfig *config)
{
_PyCoreConfig_Init(config);
config->configure_c_stdio = 1;
config->parse_argv = 1;
return _Py_INIT_OK();
}
_PyInitError
_PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config)
{
_PyCoreConfig_Init(config);
/* set to 1 */
config->isolated = 1;
config->site_import = 1;
config->write_bytecode = 1;
config->buffered_stdio = 1;
/* set to 0 */
config->use_environment = 0;
config->dev_mode = 0;
config->install_signal_handlers = 0;
config->use_hash_seed = 0;
config->faulthandler = 0;
config->tracemalloc = 0;
config->bytes_warning = 0;
config->inspect = 0;
config->interactive = 0;
config->optimization_level = 0;
config->parser_debug = 0;
config->verbose = 0;
config->quiet = 0;
config->user_site_directory = 0;
config->configure_c_stdio = 0;
config->pathconfig_warnings = 0;
#ifdef MS_WINDOWS
config->legacy_windows_stdio = 0;
#endif
return _Py_INIT_OK();
}
/* Copy str into *config_str (duplicate the string) */
_PyInitError
_PyCoreConfig_SetString(wchar_t **config_str, const wchar_t *str)
@ -2014,17 +2069,20 @@ core_read_precmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
{
_PyInitError err;
if (_PyWstrList_Copy(&precmdline->argv, &config->argv) < 0) {
return _Py_INIT_NO_MEMORY();
if (config->parse_argv) {
if (_PyWstrList_Copy(&precmdline->argv, &config->argv) < 0) {
return _Py_INIT_NO_MEMORY();
}
}
_PyPreConfig preconfig = _PyPreConfig_INIT;
_PyPreConfig preconfig;
_PyPreConfig_Init(&preconfig);
if (_PyPreConfig_Copy(&preconfig, &_PyRuntime.preconfig) < 0) {
err = _Py_INIT_NO_MEMORY();
return err;
}
_PyCoreConfig_GetCoreConfig(&preconfig, config);
_PyPreConfig_GetCoreConfig(&preconfig, config);
err = _PyPreCmdline_Read(precmdline, &preconfig);
if (_Py_INIT_FAILED(err)) {
@ -2155,6 +2213,7 @@ _PyInitError
_PyCoreConfig_Read(_PyCoreConfig *config)
{
_PyInitError err;
_PyWstrList orig_argv = _PyWstrList_INIT;
err = _Py_PreInitializeFromCoreConfig(config, NULL);
if (_Py_INIT_FAILED(err)) {
@ -2163,6 +2222,10 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
_PyCoreConfig_GetGlobalConfig(config);
if (_PyWstrList_Copy(&orig_argv, &config->argv) < 0) {
return _Py_INIT_NO_MEMORY();
}
_PyPreCmdline precmdline = _PyPreCmdline_INIT;
err = core_read_precmdline(config, &precmdline);
if (_Py_INIT_FAILED(err)) {
@ -2185,10 +2248,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
goto done;
}
/* precmdline.argv is a copy of config.argv which is modified
by config_read_cmdline() */
const _PyWstrList *argv = &precmdline.argv;
if (_Py_SetArgcArgv(argv->length, argv->items) < 0) {
if (_Py_SetArgcArgv(orig_argv.length, orig_argv.items) < 0) {
err = _Py_INIT_NO_MEMORY();
goto done;
}
@ -2249,6 +2309,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
err = _Py_INIT_OK();
done:
_PyWstrList_Clear(&orig_argv);
_PyPreCmdline_Clear(&precmdline);
return err;
}