diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-06 00:44:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-06 00:44:31 (GMT) |
commit | 4fffd380a4070aff39b7fd443d90e60746c1b623 (patch) | |
tree | 2128ef8300deb023c117151905b1a822157761d9 /Python/preconfig.c | |
parent | c656e25667c9acc0d13e5bb16d3df2938d0f614b (diff) | |
download | cpython-4fffd380a4070aff39b7fd443d90e60746c1b623.zip cpython-4fffd380a4070aff39b7fd443d90e60746c1b623.tar.gz cpython-4fffd380a4070aff39b7fd443d90e60746c1b623.tar.bz2 |
bpo-36142: _PyPreConfig_Read() sets LC_CTYPE (GH-12188)
* _PyPreConfig_Read() now sets temporarily LC_CTYPE to the user
preferred locale, as _PyPreConfig_Write() will do permanentely.
* Fix _PyCoreConfig_Clear(): clear run_xxx attributes
* _Py_SetArgcArgv() doesn't have to be exported
* _PyCoreConfig_SetGlobalConfig() no longer applies preconfig
Diffstat (limited to 'Python/preconfig.c')
-rw-r--r-- | Python/preconfig.c | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/Python/preconfig.c b/Python/preconfig.c index ee9dca4..45093d2 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -472,10 +472,50 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline) } +static _PyInitError +get_ctype_locale(char **locale_p) +{ + const char *loc = setlocale(LC_CTYPE, NULL); + if (loc == NULL) { + return _Py_INIT_ERR("failed to LC_CTYPE locale"); + } + + char *copy = _PyMem_RawStrdup(loc); + if (copy == NULL) { + return _Py_INIT_NO_MEMORY(); + } + + *locale_p = copy; + return _Py_INIT_OK(); +} + + +/* Read the configuration from: + + - environment variables + - Py_xxx global configuration variables + - the LC_CTYPE locale + + See _PyPreConfig_ReadFromArgv() to parse also command line arguments. */ _PyInitError _PyPreConfig_Read(_PyPreConfig *config) { - return preconfig_read(config, NULL); + _PyInitError err; + char *old_loc; + + err = get_ctype_locale(&old_loc); + if (_Py_INIT_FAILED(err)) { + return err; + } + + /* Set LC_CTYPE to the user preferred locale */ + _Py_SetLocaleFromEnv(LC_CTYPE); + + err = preconfig_read(config, NULL); + + setlocale(LC_CTYPE, old_loc); + + return err; } @@ -604,7 +644,14 @@ done: } -/* Read the preconfiguration. */ +/* Read the configuration from: + + - command line arguments + - environment variables + - Py_xxx global configuration variables + - the LC_CTYPE locale + + See _PyPreConfig_ReadFromArgv() to parse also command line arguments. */ _PyInitError _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args) { @@ -624,15 +671,8 @@ _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args) int locale_coerced = 0; int loops = 0; - /* copy LC_CTYPE locale */ - const char *loc = setlocale(LC_CTYPE, NULL); - if (loc == NULL) { - err = _Py_INIT_ERR("failed to LC_CTYPE locale"); - goto done; - } - init_ctype_locale = _PyMem_RawStrdup(loc); - if (init_ctype_locale == NULL) { - err = _Py_INIT_NO_MEMORY(); + err = get_ctype_locale(&init_ctype_locale); + if (_Py_INIT_FAILED(err)) { goto done; } @@ -767,15 +807,23 @@ _PyPreConfig_SetAllocator(_PyPreConfig *config) } -/* Write the pre-configuration. +/* Write the pre-configuration: + + - set the memory allocators + - set Py_xxx global configuration variables + - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode + (PEP 540) If the memory allocator is changed, config is re-allocated with new - allocator. So calling _PyPreConfig_Clear(config) is safe after this call. */ + allocator. So calling _PyPreConfig_Clear(config) is safe after this call. + + Do nothing if called after Py_Initialize(): ignore the new + pre-configuration. */ _PyInitError _PyPreConfig_Write(_PyPreConfig *config) { if (_PyRuntime.core_initialized) { - /* bpo-34008: Calling Py_Main() after Py_Initialize() ignores + /* bpo-34008: Calling this functions after Py_Initialize() ignores the new configuration. */ return _Py_INIT_OK(); } |