diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-05 01:01:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-05 01:01:27 (GMT) |
commit | cad1f747da47849ab5d8b0b881f7a0b94564d290 (patch) | |
tree | b66ed4ec4f2f26c08fde4989fbf2be680834973d /Python/preconfig.c | |
parent | 7e9ce4c89e9a34ff84a89831812bc8b42d37ac1f (diff) | |
download | cpython-cad1f747da47849ab5d8b0b881f7a0b94564d290.zip cpython-cad1f747da47849ab5d8b0b881f7a0b94564d290.tar.gz cpython-cad1f747da47849ab5d8b0b881f7a0b94564d290.tar.bz2 |
bpo-36142: Add _PyPreConfig structure (GH-12172)
* Add _PyPreConfig structure
* Move 'ignored' and 'use_environment' fields from _PyCoreConfig
to _PyPreConfig
* Add a new "_PyPreConfig preconfig;" field to _PyCoreConfig
Diffstat (limited to 'Python/preconfig.c')
-rw-r--r-- | Python/preconfig.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/Python/preconfig.c b/Python/preconfig.c index 8102924..bb1e830 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -90,3 +90,116 @@ _PyArgv_Decode(const _PyArgv *args, wchar_t*** argv_p) *argv_p = argv; return _Py_INIT_OK(); } + + +/* --- _PyPreConfig ----------------------------------------------- */ + +void +_PyPreConfig_Clear(_PyPreConfig *config) +{ +} + + +int +_PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) +{ + _PyPreConfig_Clear(config); + +#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR + + COPY_ATTR(isolated); + COPY_ATTR(use_environment); + +#undef COPY_ATTR + return 0; +} + + +void +_PyPreConfig_GetGlobalConfig(_PyPreConfig *config) +{ +#define COPY_FLAG(ATTR, VALUE) \ + if (config->ATTR == -1) { \ + config->ATTR = VALUE; \ + } +#define COPY_NOT_FLAG(ATTR, VALUE) \ + if (config->ATTR == -1) { \ + config->ATTR = !(VALUE); \ + } + + COPY_FLAG(isolated, Py_IsolatedFlag); + COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); + +#undef COPY_FLAG +#undef COPY_NOT_FLAG +} + + +void +_PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) +{ +#define COPY_FLAG(ATTR, VAR) \ + if (config->ATTR != -1) { \ + VAR = config->ATTR; \ + } +#define COPY_NOT_FLAG(ATTR, VAR) \ + if (config->ATTR != -1) { \ + VAR = !config->ATTR; \ + } + + COPY_FLAG(isolated, Py_IsolatedFlag); + COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); + +#undef COPY_FLAG +#undef COPY_NOT_FLAG +} + + +_PyInitError +_PyPreConfig_Read(_PyPreConfig *config) +{ + _PyPreConfig_GetGlobalConfig(config); + + if (config->isolated > 0) { + config->use_environment = 0; + } + + /* Default values */ + if (config->use_environment < 0) { + config->use_environment = 0; + } + + assert(config->use_environment >= 0); + + return _Py_INIT_OK(); +} + + +int +_PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) +{ +#define SET_ITEM(KEY, EXPR) \ + do { \ + PyObject *obj = (EXPR); \ + if (obj == NULL) { \ + goto fail; \ + } \ + int res = PyDict_SetItemString(dict, (KEY), obj); \ + Py_DECREF(obj); \ + if (res < 0) { \ + goto fail; \ + } \ + } while (0) +#define SET_ITEM_INT(ATTR) \ + SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR)) + + SET_ITEM_INT(isolated); + SET_ITEM_INT(use_environment); + return 0; + +fail: + return -1; + +#undef SET_ITEM +#undef SET_ITEM_INT +} |