diff options
author | Victor Stinner <vstinner@python.org> | 2020-10-31 00:02:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-31 00:02:09 (GMT) |
commit | 710e82630775774dceba5e8f24b1b10e6dfaf9b7 (patch) | |
tree | b4415da3ed5f8bc2ad2a52ec9a8544b9e50d975f /Python/fileutils.c | |
parent | 06f8c3328dcd81c84d1ee2b3a57b5381dcb38482 (diff) | |
download | cpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.zip cpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.tar.gz cpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.tar.bz2 |
bpo-42208: Add _Py_GetLocaleEncoding() (GH-23050)
_io.TextIOWrapper no longer calls getpreferredencoding(False) of
_bootlocale to get the locale encoding, but calls
_Py_GetLocaleEncoding() instead.
Add config_get_fs_encoding() sub-function. Reorganize also
config_get_locale_encoding() code.
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r-- | Python/fileutils.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c index e125ba4..ba26904 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1,5 +1,6 @@ #include "Python.h" -#include "pycore_fileutils.h" +#include "pycore_fileutils.h" // fileutils definitions +#include "pycore_runtime.h" // _PyRuntime #include "osdefs.h" // SEP #include <locale.h> @@ -820,6 +821,46 @@ _Py_EncodeLocaleEx(const wchar_t *text, char **str, } +// Get the current locale encoding: locale.getpreferredencoding(False). +// See also config_get_locale_encoding() +PyObject * +_Py_GetLocaleEncoding(void) +{ +#ifdef _Py_FORCE_UTF8_LOCALE + // On Android langinfo.h and CODESET are missing, + // and UTF-8 is always used in mbstowcs() and wcstombs(). + return PyUnicode_FromString("UTF-8"); +#else + const PyPreConfig *preconfig = &_PyRuntime.preconfig; + if (preconfig->utf8_mode) { + return PyUnicode_FromString("UTF-8"); + } + +#if defined(MS_WINDOWS) + return PyUnicode_FromFormat("cp%u", GetACP()); +#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 + PyErr_SetString(PyExc_ValueError, + "failed to get the locale encoding: " + "nl_langinfo(CODESET) returns an empty string"); + return NULL; +#endif + } + // Decode from UTF-8 + return PyUnicode_FromString(encoding); +#endif // !CODESET + +#endif +} + + #ifdef MS_WINDOWS static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |