diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-25 22:19:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-25 22:19:57 (GMT) |
commit | 1075d1684ab84dc7c28d93cfb46e95e70d3b6d3b (patch) | |
tree | 7a0543a13a28f88318d16f8900111ea10198a4a3 /Python | |
parent | 91759d98015e1d6d5e1367cff60592ab548e7806 (diff) | |
download | cpython-1075d1684ab84dc7c28d93cfb46e95e70d3b6d3b.zip cpython-1075d1684ab84dc7c28d93cfb46e95e70d3b6d3b.tar.gz cpython-1075d1684ab84dc7c28d93cfb46e95e70d3b6d3b.tar.bz2 |
bpo-36301: Add _Py_GetConfigsAsDict() function (GH-12540)
* Add _Py_GetConfigsAsDict() function to get all configurations as a
dict.
* dump_config() of _testembed.c now dumps preconfig as a separated
key: call _Py_GetConfigsAsDict().
* Make _PyMainInterpreterConfig_AsDict() private.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/coreconfig.c | 73 | ||||
-rw-r--r-- | Python/preconfig.c | 16 |
2 files changed, 78 insertions, 11 deletions
diff --git a/Python/coreconfig.c b/Python/coreconfig.c index ba5abb6..a434b25 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -131,7 +131,7 @@ int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ #endif -PyObject * +static PyObject * _Py_GetGlobalVariablesAsDict(void) { PyObject *dict, *obj; @@ -1563,7 +1563,7 @@ _PyCoreConfig_Write(const _PyCoreConfig *config) } -PyObject * +static PyObject * _PyCoreConfig_AsDict(const _PyCoreConfig *config) { PyObject *dict; @@ -1573,11 +1573,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) return NULL; } - if (_PyPreConfig_AsDict(&config->preconfig, dict) < 0) { - Py_DECREF(dict); - return NULL; - } - #define SET_ITEM(KEY, EXPR) \ do { \ PyObject *obj = (EXPR); \ @@ -2158,3 +2153,67 @@ done: cmdline_clear(&cmdline); return err; } + + +PyObject* +_Py_GetConfigsAsDict(void) +{ + PyObject *config = NULL; + PyObject *dict = NULL; + + config = PyDict_New(); + if (config == NULL) { + goto error; + } + + /* global config */ + dict = _Py_GetGlobalVariablesAsDict(); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "global_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + /* pre config */ + PyInterpreterState *interp = _PyInterpreterState_Get(); + const _PyCoreConfig *core_config = _PyInterpreterState_GetCoreConfig(interp); + const _PyPreConfig *pre_config = &core_config->preconfig; + dict = _PyPreConfig_AsDict(pre_config); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "pre_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + /* core config */ + dict = _PyCoreConfig_AsDict(core_config); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "core_config", dict) < 0) { + goto error; + } + 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: + Py_XDECREF(config); + Py_XDECREF(dict); + return NULL; +} diff --git a/Python/preconfig.c b/Python/preconfig.c index ac87a7a..c65ee28 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -574,9 +574,16 @@ _PyPreCmdline_SetPreConfig(const _PyPreCmdline *cmdline, _PyPreConfig *config) } -int -_PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) +PyObject* +_PyPreConfig_AsDict(const _PyPreConfig *config) { + PyObject *dict; + + dict = PyDict_New(); + if (dict == NULL) { + return NULL; + } + #define SET_ITEM(KEY, EXPR) \ do { \ PyObject *obj = (EXPR); \ @@ -608,10 +615,11 @@ _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) #endif SET_ITEM_INT(dev_mode); SET_ITEM_STR(allocator); - return 0; + return dict; fail: - return -1; + Py_DECREF(dict); + return NULL; #undef FROM_STRING #undef SET_ITEM |