bpo-36444: Remove _PyMainInterpreterConfig (GH-12571)

This commit is contained in:
Victor Stinner 2019-03-27 01:36:16 +01:00 committed by GitHub
parent 6a258c8890
commit 8b9dbc017a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 103 additions and 417 deletions

View file

@ -2208,17 +2208,6 @@ _Py_GetConfigsAsDict(void)
}
Py_CLEAR(dict);
/* main config */
const _PyMainInterpreterConfig *main_config = _PyInterpreterState_GetMainConfig(interp);
dict = _PyMainInterpreterConfig_AsDict(main_config);
if (dict == NULL) {
goto error;
}
if (PyDict_SetItemString(config, "main_config", dict) < 0) {
goto error;
}
Py_CLEAR(dict);
return config;
error:

View file

@ -853,14 +853,19 @@ _Py_InitializeCore(const _PyCoreConfig *src_config,
configuration. Example of bpo-34008: Py_Main() called after
Py_Initialize(). */
static _PyInitError
_Py_ReconfigureMainInterpreter(PyInterpreterState *interp,
const _PyMainInterpreterConfig *config)
_Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
{
if (config->argv != NULL) {
int res = PyDict_SetItemString(interp->sysdict, "argv", config->argv);
if (res < 0) {
return _Py_INIT_ERR("fail to set sys.argv");
}
_PyCoreConfig *core_config = &interp->core_config;
PyObject *argv = _PyWstrList_AsList(&core_config->argv);
if (argv == NULL) {
return _Py_INIT_NO_MEMORY(); \
}
int res = PyDict_SetItemString(interp->sysdict, "argv", argv);
Py_DECREF(argv);
if (res < 0) {
return _Py_INIT_ERR("fail to set sys.argv");
}
return _Py_INIT_OK();
}
@ -877,22 +882,17 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp,
* non-zero return code.
*/
_PyInitError
_Py_InitializeMainInterpreter(PyInterpreterState *interp,
const _PyMainInterpreterConfig *config)
_Py_InitializeMainInterpreter(PyInterpreterState *interp)
{
if (!_PyRuntime.core_initialized) {
return _Py_INIT_ERR("runtime core not initialized");
}
/* Configure the main interpreter */
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
return _Py_INIT_ERR("failed to copy main interpreter config");
}
config = &interp->config;
_PyCoreConfig *core_config = &interp->core_config;
if (_PyRuntime.initialized) {
return _Py_ReconfigureMainInterpreter(interp, config);
return _Py_ReconfigureMainInterpreter(interp);
}
if (!core_config->_install_importlib) {
@ -929,7 +929,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
return err;
}
if (interp->config.install_signal_handlers) {
if (core_config->install_signal_handlers) {
err = initsigs(); /* Signal handling stuff, including initintr() */
if (_Py_INIT_FAILED(err)) {
return err;
@ -991,12 +991,7 @@ _Py_InitializeFromConfig(const _PyCoreConfig *config)
}
config = &interp->core_config;
_PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
err = _PyMainInterpreterConfig_Read(&main_config, config);
if (!_Py_INIT_FAILED(err)) {
err = _Py_InitializeMainInterpreter(interp, &main_config);
}
_PyMainInterpreterConfig_Clear(&main_config);
err = _Py_InitializeMainInterpreter(interp);
if (_Py_INIT_FAILED(err)) {
return err;
}
@ -1364,24 +1359,18 @@ new_interpreter(PyThreadState **tstate_p)
/* Copy the current interpreter config into the new interpreter */
_PyCoreConfig *core_config;
_PyMainInterpreterConfig *config;
if (save_tstate != NULL) {
core_config = &save_tstate->interp->core_config;
config = &save_tstate->interp->config;
} else {
/* No current thread state, copy from the main interpreter */
PyInterpreterState *main_interp = PyInterpreterState_Main();
core_config = &main_interp->core_config;
config = &main_interp->config;
}
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
return _Py_INIT_ERR("failed to copy core config");
}
core_config = &interp->core_config;
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
return _Py_INIT_ERR("failed to copy main interpreter config");
}
err = _PyExc_Init();
if (_Py_INIT_FAILED(err)) {

View file

@ -174,7 +174,6 @@ PyInterpreterState_New(void)
interp->id_refcount = -1;
interp->check_interval = 100;
interp->core_config = _PyCoreConfig_INIT;
interp->config = _PyMainInterpreterConfig_INIT;
interp->eval_frame = _PyEval_EvalFrameDefault;
#ifdef HAVE_DLOPEN
#if HAVE_DECL_RTLD_NOW
@ -221,7 +220,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
PyThreadState_Clear(p);
HEAD_UNLOCK();
_PyCoreConfig_Clear(&interp->core_config);
_PyMainInterpreterConfig_Clear(&interp->config);
Py_CLEAR(interp->codec_search_path);
Py_CLEAR(interp->codec_search_cache);
Py_CLEAR(interp->codec_error_registry);
@ -455,12 +453,6 @@ _PyInterpreterState_GetCoreConfig(PyInterpreterState *interp)
return &interp->core_config;
}
_PyMainInterpreterConfig *
_PyInterpreterState_GetMainConfig(PyInterpreterState *interp)
{
return &interp->config;
}
PyObject *
_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
{

View file

@ -17,6 +17,7 @@ Data members:
#include "Python.h"
#include "code.h"
#include "frameobject.h"
#include "pycore_coreconfig.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pathconfig.h"
@ -2530,26 +2531,71 @@ err_occurred:
} \
} while (0)
static int
sys_add_xoption(PyObject *opts, const wchar_t *s)
{
PyObject *name, *value;
const wchar_t *name_end = wcschr(s, L'=');
if (!name_end) {
name = PyUnicode_FromWideChar(s, -1);
value = Py_True;
Py_INCREF(value);
}
else {
name = PyUnicode_FromWideChar(s, name_end - s);
value = PyUnicode_FromWideChar(name_end + 1, -1);
}
if (name == NULL || value == NULL) {
goto error;
}
if (PyDict_SetItem(opts, name, value) < 0) {
goto error;
}
Py_DECREF(name);
Py_DECREF(value);
return 0;
error:
Py_XDECREF(name);
Py_XDECREF(value);
return -1;
}
static PyObject*
sys_create_xoptions_dict(const _PyCoreConfig *config)
{
Py_ssize_t nxoption = config->xoptions.length;
wchar_t * const * xoptions = config->xoptions.items;
PyObject *dict = PyDict_New();
if (dict == NULL) {
return NULL;
}
for (Py_ssize_t i=0; i < nxoption; i++) {
const wchar_t *option = xoptions[i];
if (sys_add_xoption(dict, option) < 0) {
Py_DECREF(dict);
return NULL;
}
}
return dict;
}
int
_PySys_InitMain(PyInterpreterState *interp)
{
PyObject *sysdict = interp->sysdict;
const _PyCoreConfig *core_config = &interp->core_config;
const _PyMainInterpreterConfig *config = &interp->config;
const _PyCoreConfig *config = &interp->core_config;
int res;
/* _PyMainInterpreterConfig_Read() must set all these variables */
assert(config->module_search_path != NULL);
assert(config->executable != NULL);
assert(config->prefix != NULL);
assert(config->base_prefix != NULL);
assert(config->exec_prefix != NULL);
assert(config->base_exec_prefix != NULL);
#define COPY_LIST(KEY, ATTR) \
#define COPY_LIST(KEY, VALUE) \
do { \
assert(PyList_Check(ATTR)); \
PyObject *list = PyList_GetSlice(ATTR, 0, PyList_GET_SIZE(ATTR)); \
PyObject *list = _PyWstrList_AsList(&(VALUE)); \
if (list == NULL) { \
return -1; \
} \
@ -2557,36 +2603,41 @@ _PySys_InitMain(PyInterpreterState *interp)
Py_DECREF(list); \
} while (0)
COPY_LIST("path", config->module_search_path);
#define SET_SYS_FROM_WSTR(KEY, VALUE) \
do { \
PyObject *str = PyUnicode_FromWideChar(VALUE, -1); \
if (str == NULL) { \
return -1; \
} \
SET_SYS_FROM_STRING_BORROW(KEY, str); \
Py_DECREF(str); \
} while (0)
SET_SYS_FROM_STRING_BORROW("executable", config->executable);
SET_SYS_FROM_STRING_BORROW("prefix", config->prefix);
SET_SYS_FROM_STRING_BORROW("base_prefix", config->base_prefix);
SET_SYS_FROM_STRING_BORROW("exec_prefix", config->exec_prefix);
SET_SYS_FROM_STRING_BORROW("base_exec_prefix", config->base_exec_prefix);
COPY_LIST("path", config->module_search_paths);
SET_SYS_FROM_WSTR("executable", config->executable);
SET_SYS_FROM_WSTR("prefix", config->prefix);
SET_SYS_FROM_WSTR("base_prefix", config->base_prefix);
SET_SYS_FROM_WSTR("exec_prefix", config->exec_prefix);
SET_SYS_FROM_WSTR("base_exec_prefix", config->base_exec_prefix);
if (config->pycache_prefix != NULL) {
SET_SYS_FROM_STRING_BORROW("pycache_prefix", config->pycache_prefix);
SET_SYS_FROM_WSTR("pycache_prefix", config->pycache_prefix);
} else {
PyDict_SetItemString(sysdict, "pycache_prefix", Py_None);
}
if (config->argv != NULL) {
SET_SYS_FROM_STRING_BORROW("argv", config->argv);
}
if (config->warnoptions != NULL) {
COPY_LIST("warnoptions", config->warnoptions);
}
if (config->xoptions != NULL) {
PyObject *dict = PyDict_Copy(config->xoptions);
if (dict == NULL) {
return -1;
}
SET_SYS_FROM_STRING_BORROW("_xoptions", dict);
Py_DECREF(dict);
COPY_LIST("argv", config->argv);
COPY_LIST("warnoptions", config->warnoptions);
PyObject *xoptions = sys_create_xoptions_dict(config);
if (xoptions == NULL) {
return -1;
}
SET_SYS_FROM_STRING_BORROW("_xoptions", xoptions);
#undef COPY_LIST
#undef SET_SYS_FROM_WSTR
/* Set flags to their final values */
SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags());
@ -2602,7 +2653,7 @@ _PySys_InitMain(PyInterpreterState *interp)
}
SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode",
PyBool_FromLong(!core_config->write_bytecode));
PyBool_FromLong(!config->write_bytecode));
if (get_warnoptions() == NULL)
return -1;