diff options
author | Victor Stinner <vstinner@python.org> | 2020-11-01 19:59:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-01 19:59:35 (GMT) |
commit | 82458b6cdbae3b849dc11d0d7dc2ab06ef0451c4 (patch) | |
tree | f70b3003306fdc3165740eec39eadec9a2e82a9c /Python/initconfig.c | |
parent | 1f7dfb277e5b88cddc13e5024766be787a3e9127 (diff) | |
download | cpython-82458b6cdbae3b849dc11d0d7dc2ab06ef0451c4.zip cpython-82458b6cdbae3b849dc11d0d7dc2ab06ef0451c4.tar.gz cpython-82458b6cdbae3b849dc11d0d7dc2ab06ef0451c4.tar.bz2 |
bpo-42236: Enhance _locale._get_locale_encoding() (GH-23083)
* Rename _Py_GetLocaleEncoding() to _Py_GetLocaleEncodingObject()
* Add _Py_GetLocaleEncoding() which returns a wchar_t* string to
share code between _Py_GetLocaleEncodingObject()
and config_get_locale_encoding().
* _Py_GetLocaleEncodingObject() now decodes nl_langinfo(CODESET)
from the current locale encoding with surrogateescape,
rather than using UTF-8.
Diffstat (limited to 'Python/initconfig.c')
-rw-r--r-- | Python/initconfig.c | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c index e129278..56f4297 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -11,11 +11,7 @@ #include "osdefs.h" // DELIM #include <locale.h> // setlocale() -#ifdef HAVE_LANGINFO_H -# include <langinfo.h> // nl_langinfo(CODESET) -#endif #if defined(MS_WINDOWS) || defined(__CYGWIN__) -# include <windows.h> // GetACP() # ifdef HAVE_IO_H # include <io.h> # endif @@ -1497,41 +1493,24 @@ config_get_stdio_errors(const PyPreConfig *preconfig) } -// See also _Py_GetLocaleEncoding() and config_get_fs_encoding() +// See also config_get_fs_encoding() static PyStatus config_get_locale_encoding(PyConfig *config, const PyPreConfig *preconfig, wchar_t **locale_encoding) { -#ifdef _Py_FORCE_UTF8_LOCALE - return PyConfig_SetString(config, locale_encoding, L"utf-8"); -#else - if (preconfig->utf8_mode) { - return PyConfig_SetString(config, locale_encoding, L"utf-8"); - } - -#ifdef MS_WINDOWS - char encoding[20]; - PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP()); - return PyConfig_SetBytesString(config, locale_encoding, encoding); -#else - const char *encoding = nl_langinfo(CODESET); - if (!encoding || encoding[0] == '\0') { -#ifdef _Py_FORCE_UTF8_FS_ENCODING - // nl_langinfo() can return an empty string when the LC_CTYPE locale is - // not supported. Default to UTF-8 in that case, because UTF-8 is the - // default charset on macOS. - encoding = "UTF-8"; -#else - return _PyStatus_ERR("failed to get the locale encoding: " - "nl_langinfo(CODESET) returns an empty string"); -#endif + const char *errmsg; + wchar_t *encoding = _Py_GetLocaleEncoding(&errmsg); + if (encoding == NULL) { + if (errmsg != NULL) { + return _PyStatus_ERR(errmsg); + } + else { + return _PyStatus_NO_MEMORY(); + } } - /* nl_langinfo(CODESET) is decoded by Py_DecodeLocale() */ - return CONFIG_SET_BYTES_STR(config, - locale_encoding, encoding, - "nl_langinfo(CODESET)"); -#endif // !MS_WINDOWS -#endif // !_Py_FORCE_UTF8_LOCALE + PyStatus status = PyConfig_SetString(config, locale_encoding, encoding); + PyMem_RawFree(encoding); + return status; } |