diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-11-13 18:59:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 18:59:26 (GMT) |
commit | 00b137c72f90fbc39a6cd7e48b37c58d19977180 (patch) | |
tree | 445883d4e7861725c81f7d067b1d0aea17dd0698 /Modules | |
parent | f966e5397ed8f5c42c185223fc9b4d750a678d02 (diff) | |
download | cpython-00b137c72f90fbc39a6cd7e48b37c58d19977180.zip cpython-00b137c72f90fbc39a6cd7e48b37c58d19977180.tar.gz cpython-00b137c72f90fbc39a6cd7e48b37c58d19977180.tar.bz2 |
bpo-35233: Fix _PyMainInterpreterConfig_Copy() (GH-10519)
* Fix _PyMainInterpreterConfig_Copy():
copy 'install_signal_handlers' attribute
* Add _PyMainInterpreterConfig_AsDict()
* Add unit tests on the main interpreter configuration
to test_embed.InitConfigTests
* test.pythoninfo: log also main_config
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 10 | ||||
-rw-r--r-- | Modules/main.c | 85 |
2 files changed, 81 insertions, 14 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index bc1f630..205b668 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4702,6 +4702,15 @@ get_coreconfig(PyObject *self, PyObject *Py_UNUSED(args)) } +static PyObject * +get_mainconfig(PyObject *self, PyObject *Py_UNUSED(args)) +{ + PyInterpreterState *interp = _PyInterpreterState_Get(); + const _PyMainInterpreterConfig *config = &interp->config; + return _PyMainInterpreterConfig_AsDict(config); +} + + #ifdef Py_REF_DEBUG static PyObject * negative_refcount(PyObject *self, PyObject *Py_UNUSED(args)) @@ -4948,6 +4957,7 @@ static PyMethodDef TestMethods[] = { {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS}, {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS}, {"get_coreconfig", get_coreconfig, METH_NOARGS}, + {"get_mainconfig", get_mainconfig, METH_NOARGS}, #ifdef Py_REF_DEBUG {"negative_refcount", negative_refcount, METH_NOARGS}, #endif diff --git a/Modules/main.c b/Modules/main.c index 281707a..3e51ef8 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -1441,31 +1441,88 @@ _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config, { _PyMainInterpreterConfig_Clear(config); -#define COPY_ATTR(ATTR) \ +#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR +#define COPY_OBJ_ATTR(OBJ_ATTR) \ do { \ - if (config2->ATTR != NULL) { \ - config->ATTR = config_copy_attr(config2->ATTR); \ - if (config->ATTR == NULL) { \ + if (config2->OBJ_ATTR != NULL) { \ + config->OBJ_ATTR = config_copy_attr(config2->OBJ_ATTR); \ + if (config->OBJ_ATTR == NULL) { \ return -1; \ } \ } \ } while (0) - COPY_ATTR(argv); - COPY_ATTR(executable); - COPY_ATTR(prefix); - COPY_ATTR(base_prefix); - COPY_ATTR(exec_prefix); - COPY_ATTR(base_exec_prefix); - COPY_ATTR(warnoptions); - COPY_ATTR(xoptions); - COPY_ATTR(module_search_path); - COPY_ATTR(pycache_prefix); + COPY_ATTR(install_signal_handlers); + COPY_OBJ_ATTR(argv); + COPY_OBJ_ATTR(executable); + COPY_OBJ_ATTR(prefix); + COPY_OBJ_ATTR(base_prefix); + COPY_OBJ_ATTR(exec_prefix); + COPY_OBJ_ATTR(base_exec_prefix); + COPY_OBJ_ATTR(warnoptions); + COPY_OBJ_ATTR(xoptions); + COPY_OBJ_ATTR(module_search_path); + COPY_OBJ_ATTR(pycache_prefix); #undef COPY_ATTR +#undef COPY_OBJ_ATTR return 0; } +PyObject* +_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config) +{ + PyObject *dict, *obj; + int res; + + dict = PyDict_New(); + if (dict == NULL) { + return NULL; + } + +#define SET_ITEM(KEY, ATTR) \ + do { \ + obj = config->ATTR; \ + if (obj == NULL) { \ + obj = Py_None; \ + } \ + res = PyDict_SetItemString(dict, (KEY), obj); \ + if (res < 0) { \ + goto fail; \ + } \ + } while (0) + + obj = PyLong_FromLong(config->install_signal_handlers); + if (obj == NULL) { + goto fail; + } + res = PyDict_SetItemString(dict, "install_signal_handlers", obj); + Py_DECREF(obj); + if (res < 0) { + goto fail; + } + + SET_ITEM("argv", argv); + SET_ITEM("executable", executable); + SET_ITEM("prefix", prefix); + SET_ITEM("base_prefix", base_prefix); + SET_ITEM("exec_prefix", exec_prefix); + SET_ITEM("base_exec_prefix", base_exec_prefix); + SET_ITEM("warnoptions", warnoptions); + SET_ITEM("xoptions", xoptions); + SET_ITEM("module_search_path", module_search_path); + SET_ITEM("pycache_prefix", pycache_prefix); + + return dict; + +fail: + Py_DECREF(dict); + return NULL; + +#undef SET_ITEM +} + + _PyInitError _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config, const _PyCoreConfig *config) |