mirror of
https://github.com/python/cpython.git
synced 2025-07-19 17:25:54 +00:00
bpo-38304: Add PyConfig.struct_size (GH-16451)
Add a new struct_size field to PyPreConfig and PyConfig structures to allow to modify these structures in the future without breaking the backward compatibility. * Replace private _config_version field with public struct_size field in PyPreConfig and PyConfig. * Public PyPreConfig_InitIsolatedConfig() and PyPreConfig_InitPythonConfig() return type becomes PyStatus, instead of void. * Internal _PyConfig_InitCompatConfig(), _PyPreConfig_InitCompatConfig(), _PyPreConfig_InitFromConfig(), _PyPreConfig_InitFromPreConfig() return type becomes PyStatus, instead of void. * Remove _Py_CONFIG_VERSION * Update the Initialization Configuration documentation.
This commit is contained in:
parent
52d1b86bde
commit
441b10cf28
13 changed files with 350 additions and 60 deletions
|
@ -40,6 +40,7 @@ Py_FrozenMain(int argc, char **argv)
|
|||
}
|
||||
|
||||
PyConfig config;
|
||||
config.struct_size = sizeof(PyConfig);
|
||||
status = PyConfig_InitPythonConfig(&config);
|
||||
if (PyStatus_Exception(status)) {
|
||||
PyConfig_Clear(&config);
|
||||
|
|
|
@ -528,6 +528,18 @@ Py_GetArgcArgv(int *argc, wchar_t ***argv)
|
|||
? _PyStatus_ERR("cannot decode " NAME) \
|
||||
: _PyStatus_NO_MEMORY())
|
||||
|
||||
|
||||
static PyStatus
|
||||
config_check_struct_size(const PyConfig *config)
|
||||
{
|
||||
if (config->struct_size != sizeof(PyConfig)) {
|
||||
return _PyStatus_ERR("unsupported PyConfig structure size "
|
||||
"(Python version mismatch?)");
|
||||
}
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
/* Free memory allocated in config, but don't clear all attributes */
|
||||
void
|
||||
PyConfig_Clear(PyConfig *config)
|
||||
|
@ -568,12 +580,19 @@ PyConfig_Clear(PyConfig *config)
|
|||
}
|
||||
|
||||
|
||||
void
|
||||
PyStatus
|
||||
_PyConfig_InitCompatConfig(PyConfig *config)
|
||||
{
|
||||
size_t struct_size = config->struct_size;
|
||||
memset(config, 0, sizeof(*config));
|
||||
config->struct_size = struct_size;
|
||||
|
||||
PyStatus status = config_check_struct_size(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_version = _Py_CONFIG_VERSION;
|
||||
config->_config_init = (int)_PyConfig_INIT_COMPAT;
|
||||
config->isolated = -1;
|
||||
config->use_environment = -1;
|
||||
|
@ -603,13 +622,17 @@ _PyConfig_InitCompatConfig(PyConfig *config)
|
|||
#ifdef MS_WINDOWS
|
||||
config->legacy_windows_stdio = -1;
|
||||
#endif
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
static PyStatus
|
||||
config_init_defaults(PyConfig *config)
|
||||
{
|
||||
_PyConfig_InitCompatConfig(config);
|
||||
PyStatus status = _PyConfig_InitCompatConfig(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
config->isolated = 0;
|
||||
config->use_environment = 1;
|
||||
|
@ -628,13 +651,18 @@ config_init_defaults(PyConfig *config)
|
|||
#ifdef MS_WINDOWS
|
||||
config->legacy_windows_stdio = 0;
|
||||
#endif
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
PyStatus
|
||||
PyConfig_InitPythonConfig(PyConfig *config)
|
||||
{
|
||||
config_init_defaults(config);
|
||||
PyStatus status = config_init_defaults(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_init = (int)_PyConfig_INIT_PYTHON;
|
||||
config->configure_c_stdio = 1;
|
||||
|
@ -647,7 +675,11 @@ PyConfig_InitPythonConfig(PyConfig *config)
|
|||
PyStatus
|
||||
PyConfig_InitIsolatedConfig(PyConfig *config)
|
||||
{
|
||||
config_init_defaults(config);
|
||||
PyStatus status = config_init_defaults(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_init = (int)_PyConfig_INIT_ISOLATED;
|
||||
config->isolated = 1;
|
||||
|
@ -742,6 +774,19 @@ PyStatus
|
|||
_PyConfig_Copy(PyConfig *config, const PyConfig *config2)
|
||||
{
|
||||
PyStatus status;
|
||||
|
||||
status = config_check_struct_size(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
status = config_check_struct_size(config2);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
PyConfig_Clear(config);
|
||||
|
||||
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
|
||||
|
@ -2204,7 +2249,12 @@ core_read_precmdline(PyConfig *config, _PyPreCmdline *precmdline)
|
|||
}
|
||||
|
||||
PyPreConfig preconfig;
|
||||
_PyPreConfig_InitFromPreConfig(&preconfig, &_PyRuntime.preconfig);
|
||||
preconfig.struct_size = sizeof(PyPreConfig);
|
||||
|
||||
status = _PyPreConfig_InitFromPreConfig(&preconfig, &_PyRuntime.preconfig);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
_PyPreConfig_GetConfig(&preconfig, config);
|
||||
|
||||
|
@ -2385,6 +2435,12 @@ PyConfig_Read(PyConfig *config)
|
|||
PyStatus status;
|
||||
PyWideStringList orig_argv = PyWideStringList_INIT;
|
||||
|
||||
status = config_check_struct_size(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _Py_PreInitializeFromConfig(config, NULL);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
|
|
|
@ -434,7 +434,12 @@ pathconfig_global_read(_PyPathConfig *pathconfig)
|
|||
{
|
||||
PyStatus status;
|
||||
PyConfig config;
|
||||
_PyConfig_InitCompatConfig(&config);
|
||||
config.struct_size = sizeof(PyConfig);
|
||||
|
||||
status = _PyConfig_InitCompatConfig(&config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Call _PyConfig_InitPathConfig() */
|
||||
status = PyConfig_Read(&config);
|
||||
|
|
|
@ -269,12 +269,30 @@ _PyPreCmdline_Read(_PyPreCmdline *cmdline, const PyPreConfig *preconfig)
|
|||
/* --- PyPreConfig ----------------------------------------------- */
|
||||
|
||||
|
||||
void
|
||||
static PyStatus
|
||||
preconfig_check_struct_size(PyPreConfig *config)
|
||||
{
|
||||
if (config->struct_size != sizeof(PyPreConfig)) {
|
||||
return _PyStatus_ERR("unsupported PyPreConfig structure size "
|
||||
"(Python version mismatch?)");
|
||||
}
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
PyStatus
|
||||
_PyPreConfig_InitCompatConfig(PyPreConfig *config)
|
||||
{
|
||||
size_t struct_size = config->struct_size;
|
||||
memset(config, 0, sizeof(*config));
|
||||
config->struct_size = struct_size;
|
||||
|
||||
PyStatus status = preconfig_check_struct_size(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_version = _Py_CONFIG_VERSION;
|
||||
config->_config_init = (int)_PyConfig_INIT_COMPAT;
|
||||
config->parse_argv = 0;
|
||||
config->isolated = -1;
|
||||
|
@ -295,13 +313,18 @@ _PyPreConfig_InitCompatConfig(PyPreConfig *config)
|
|||
#ifdef MS_WINDOWS
|
||||
config->legacy_windows_fs_encoding = -1;
|
||||
#endif
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PyStatus
|
||||
PyPreConfig_InitPythonConfig(PyPreConfig *config)
|
||||
{
|
||||
_PyPreConfig_InitCompatConfig(config);
|
||||
PyStatus status = _PyPreConfig_InitCompatConfig(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_init = (int)_PyConfig_INIT_PYTHON;
|
||||
config->isolated = 0;
|
||||
|
@ -316,13 +339,18 @@ PyPreConfig_InitPythonConfig(PyPreConfig *config)
|
|||
#ifdef MS_WINDOWS
|
||||
config->legacy_windows_fs_encoding = 0;
|
||||
#endif
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PyStatus
|
||||
PyPreConfig_InitIsolatedConfig(PyPreConfig *config)
|
||||
{
|
||||
_PyPreConfig_InitCompatConfig(config);
|
||||
PyStatus status = _PyPreConfig_InitCompatConfig(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
config->_config_init = (int)_PyConfig_INIT_ISOLATED;
|
||||
config->configure_locale = 0;
|
||||
|
@ -333,41 +361,55 @@ PyPreConfig_InitIsolatedConfig(PyPreConfig *config)
|
|||
#ifdef MS_WINDOWS
|
||||
config->legacy_windows_fs_encoding = 0;
|
||||
#endif
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PyStatus
|
||||
_PyPreConfig_InitFromPreConfig(PyPreConfig *config,
|
||||
const PyPreConfig *config2)
|
||||
{
|
||||
PyPreConfig_InitPythonConfig(config);
|
||||
PyStatus status = PyPreConfig_InitPythonConfig(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
preconfig_copy(config, config2);
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PyStatus
|
||||
_PyPreConfig_InitFromConfig(PyPreConfig *preconfig, const PyConfig *config)
|
||||
{
|
||||
PyStatus status;
|
||||
_PyConfigInitEnum config_init = (_PyConfigInitEnum)config->_config_init;
|
||||
switch (config_init) {
|
||||
case _PyConfig_INIT_PYTHON:
|
||||
PyPreConfig_InitPythonConfig(preconfig);
|
||||
status = PyPreConfig_InitPythonConfig(preconfig);
|
||||
break;
|
||||
case _PyConfig_INIT_ISOLATED:
|
||||
PyPreConfig_InitIsolatedConfig(preconfig);
|
||||
status = PyPreConfig_InitIsolatedConfig(preconfig);
|
||||
break;
|
||||
case _PyConfig_INIT_COMPAT:
|
||||
default:
|
||||
_PyPreConfig_InitCompatConfig(preconfig);
|
||||
status = _PyPreConfig_InitCompatConfig(preconfig);
|
||||
}
|
||||
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
_PyPreConfig_GetConfig(preconfig, config);
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
preconfig_copy(PyPreConfig *config, const PyPreConfig *config2)
|
||||
{
|
||||
assert(config2->_config_version == _Py_CONFIG_VERSION);
|
||||
assert(config->struct_size == sizeof(PyPreConfig));
|
||||
|
||||
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
|
||||
|
||||
COPY_ATTR(_config_init);
|
||||
|
@ -787,6 +829,12 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
|
|||
return status;
|
||||
}
|
||||
|
||||
status = preconfig_check_struct_size(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyStatus_UPDATE_FUNC(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
preconfig_get_global_vars(config);
|
||||
|
||||
/* Copy LC_CTYPE locale, since it's modified later */
|
||||
|
@ -801,7 +849,12 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
|
|||
|
||||
/* Save the config to be able to restore it if encodings change */
|
||||
PyPreConfig save_config;
|
||||
_PyPreConfig_InitFromPreConfig(&save_config, config);
|
||||
save_config.struct_size = sizeof(PyPreConfig);
|
||||
|
||||
status = _PyPreConfig_InitFromPreConfig(&save_config, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Set LC_CTYPE to the user preferred locale */
|
||||
if (config->configure_locale) {
|
||||
|
@ -923,7 +976,12 @@ PyStatus
|
|||
_PyPreConfig_Write(const PyPreConfig *src_config)
|
||||
{
|
||||
PyPreConfig config;
|
||||
_PyPreConfig_InitFromPreConfig(&config, src_config);
|
||||
config.struct_size = sizeof(PyPreConfig);
|
||||
|
||||
PyStatus status = _PyPreConfig_InitFromPreConfig(&config, src_config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (_PyRuntime.core_initialized) {
|
||||
/* bpo-34008: Calling this functions after Py_Initialize() ignores
|
||||
|
|
|
@ -735,7 +735,12 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
|
|||
runtime->preinitializing = 1;
|
||||
|
||||
PyPreConfig config;
|
||||
_PyPreConfig_InitFromPreConfig(&config, src_config);
|
||||
config.struct_size = sizeof(PyPreConfig);
|
||||
|
||||
status = _PyPreConfig_InitFromPreConfig(&config, src_config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyPreConfig_Read(&config, args);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
|
@ -794,7 +799,12 @@ _Py_PreInitializeFromConfig(const PyConfig *config,
|
|||
}
|
||||
|
||||
PyPreConfig preconfig;
|
||||
_PyPreConfig_InitFromConfig(&preconfig, config);
|
||||
preconfig.struct_size = sizeof(PyPreConfig);
|
||||
|
||||
status = _PyPreConfig_InitFromConfig(&preconfig, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (!config->parse_argv) {
|
||||
return Py_PreInitialize(&preconfig);
|
||||
|
@ -842,7 +852,12 @@ pyinit_core(_PyRuntimeState *runtime,
|
|||
}
|
||||
|
||||
PyConfig config;
|
||||
_PyConfig_InitCompatConfig(&config);
|
||||
config.struct_size = sizeof(PyConfig);
|
||||
|
||||
status = _PyConfig_InitCompatConfig(&config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
status = _PyConfig_Copy(&config, src_config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
|
@ -1064,7 +1079,13 @@ Py_InitializeEx(int install_sigs)
|
|||
}
|
||||
|
||||
PyConfig config;
|
||||
_PyConfig_InitCompatConfig(&config);
|
||||
config.struct_size = sizeof(PyConfig);
|
||||
|
||||
status = _PyConfig_InitCompatConfig(&config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
Py_ExitStatusException(status);
|
||||
}
|
||||
|
||||
config.install_signal_handlers = install_sigs;
|
||||
|
||||
status = Py_InitializeFromConfig(&config);
|
||||
|
|
|
@ -60,7 +60,12 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
|
|||
|
||||
_PyGC_Initialize(&runtime->gc);
|
||||
_PyEval_Initialize(&runtime->ceval);
|
||||
PyPreConfig_InitPythonConfig(&runtime->preconfig);
|
||||
|
||||
runtime->preconfig.struct_size = sizeof(PyPreConfig);
|
||||
PyStatus status = PyPreConfig_InitPythonConfig(&runtime->preconfig);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
runtime->gilstate.check_enabled = 1;
|
||||
|
||||
|
@ -204,6 +209,7 @@ PyInterpreterState_New(void)
|
|||
memset(interp, 0, sizeof(*interp));
|
||||
interp->id_refcount = -1;
|
||||
|
||||
interp->config.struct_size = sizeof(PyConfig);
|
||||
PyStatus status = PyConfig_InitPythonConfig(&interp->config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
/* Don't report status to caller: PyConfig_InitPythonConfig()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue