mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
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.
This commit is contained in:
parent
91759d9801
commit
1075d1684a
9 changed files with 179 additions and 134 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue