mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
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.
This commit is contained in:
parent
100964e031
commit
048a35659a
9 changed files with 189 additions and 16 deletions
|
@ -242,8 +242,9 @@ PyStatus PyStatus_Ok(void)
|
|||
|
||||
PyStatus PyStatus_Error(const char *err_msg)
|
||||
{
|
||||
assert(err_msg != NULL);
|
||||
return (PyStatus){._type = _PyStatus_TYPE_ERROR,
|
||||
.err_msg = err_msg};
|
||||
.err_msg = err_msg};
|
||||
}
|
||||
|
||||
PyStatus PyStatus_NoMemory(void)
|
||||
|
@ -262,6 +263,23 @@ int PyStatus_IsExit(PyStatus status)
|
|||
int PyStatus_Exception(PyStatus status)
|
||||
{ return _PyStatus_EXCEPTION(status); }
|
||||
|
||||
PyObject*
|
||||
_PyErr_SetFromPyStatus(PyStatus status)
|
||||
{
|
||||
if (!_PyStatus_IS_ERROR(status)) {
|
||||
PyErr_Format(PyExc_SystemError,
|
||||
"%s() expects an error PyStatus",
|
||||
_PyStatus_GET_FUNC());
|
||||
}
|
||||
else if (status.func) {
|
||||
PyErr_Format(PyExc_ValueError, "%s: %s", status.func, status.err_msg);
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_ValueError, "%s", status.err_msg);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* --- PyWideStringList ------------------------------------------------ */
|
||||
|
||||
|
|
|
@ -428,6 +428,67 @@ _Py_SetLocaleFromEnv(int category)
|
|||
}
|
||||
|
||||
|
||||
static int
|
||||
interpreter_set_config(const PyConfig *config)
|
||||
{
|
||||
PyThreadState *tstate = PyThreadState_Get();
|
||||
|
||||
PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = _PyConfig_Copy(&tstate->interp->config, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
return -1;
|
||||
}
|
||||
config = &tstate->interp->config;
|
||||
|
||||
if (config->_install_importlib && _Py_IsMainInterpreter(tstate)) {
|
||||
status = _PyConfig_WritePathConfig(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the sys module for the new configuration
|
||||
if (_PySys_UpdateConfig(tstate) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
_PyInterpreterState_SetConfig(const PyConfig *src_config)
|
||||
{
|
||||
int res = -1;
|
||||
|
||||
PyConfig config;
|
||||
PyConfig_InitPythonConfig(&config);
|
||||
PyStatus status = _PyConfig_Copy(&config, src_config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
goto done;
|
||||
}
|
||||
|
||||
status = PyConfig_Read(&config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
goto done;
|
||||
}
|
||||
|
||||
res = interpreter_set_config(&config);
|
||||
|
||||
done:
|
||||
PyConfig_Clear(&config);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* Global initializations. Can be undone by Py_Finalize(). Don't
|
||||
call this twice without an intervening Py_Finalize() call.
|
||||
|
||||
|
@ -462,7 +523,7 @@ pyinit_core_reconfigure(_PyRuntimeState *runtime,
|
|||
return status;
|
||||
}
|
||||
|
||||
status = _PyInterpreterState_SetConfig(interp, config);
|
||||
status = _PyConfig_Copy(&interp->config, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
@ -550,7 +611,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
|
|||
return _PyStatus_ERR("can't make main interpreter");
|
||||
}
|
||||
|
||||
PyStatus status = _PyInterpreterState_SetConfig(interp, config);
|
||||
PyStatus status = _PyConfig_Copy(&interp->config, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
@ -917,7 +978,7 @@ pyinit_core(_PyRuntimeState *runtime,
|
|||
}
|
||||
|
||||
PyConfig config;
|
||||
_PyConfig_InitCompatConfig(&config);
|
||||
PyConfig_InitPythonConfig(&config);
|
||||
|
||||
status = _PyConfig_Copy(&config, src_config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
|
@ -1835,7 +1896,8 @@ new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
|
|||
config = _PyInterpreterState_GetConfig(main_interp);
|
||||
}
|
||||
|
||||
status = _PyInterpreterState_SetConfig(interp, config);
|
||||
|
||||
status = _PyConfig_Copy(&interp->config, config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto error;
|
||||
}
|
||||
|
|
|
@ -778,7 +778,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
|
|||
return PyList_SetItem(interp->modules_by_index, index, Py_None);
|
||||
}
|
||||
|
||||
/* Used by PyImport_Cleanup() */
|
||||
// Used by finalize_modules()
|
||||
void
|
||||
_PyInterpreterState_ClearModules(PyInterpreterState *interp)
|
||||
{
|
||||
|
@ -1920,11 +1920,17 @@ _PyInterpreterState_GetConfig(PyInterpreterState *interp)
|
|||
}
|
||||
|
||||
|
||||
PyStatus
|
||||
_PyInterpreterState_SetConfig(PyInterpreterState *interp,
|
||||
const PyConfig *config)
|
||||
int
|
||||
_PyInterpreterState_GetConfigCopy(PyConfig *config)
|
||||
{
|
||||
return _PyConfig_Copy(&interp->config, config);
|
||||
PyInterpreterState *interp = PyInterpreterState_Get();
|
||||
|
||||
PyStatus status = _PyConfig_Copy(config, &interp->config);
|
||||
if (PyStatus_Exception(status)) {
|
||||
_PyErr_SetFromPyStatus(status);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue