diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-27 01:36:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-27 01:36:16 +0100 |
commit | 8b9dbc017a190d13f717e714630d620adb7c7ac2 (patch) | |
tree | aec94454c3d4ee9f96f19d98eb64a9a9c5df7d97 /Python/sysmodule.c | |
parent | 6a258c88906a7e8acde455ee2acb78b6f315ea0b (diff) | |
download | cpython-git-8b9dbc017a190d13f717e714630d620adb7c7ac2.tar.gz |
bpo-36444: Remove _PyMainInterpreterConfig (GH-12571)
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 119 |
1 files changed, 85 insertions, 34 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 12ec7d5918..1af11c4ab9 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -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; |