bpo-47000: Add locale.getencoding() (GH-32068)

This commit is contained in:
Inada Naoki 2022-04-09 09:54:54 +09:00 committed by GitHub
parent cd29bd13ef
commit 6773203487
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 88 additions and 46 deletions

View file

@ -93,6 +93,10 @@ _Py_device_encoding(int fd)
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
#else
if (_PyRuntime.preconfig.utf8_mode) {
_Py_DECLARE_STR(utf_8, "utf-8");
return Py_NewRef(&_Py_STR(utf_8));
}
return _Py_GetLocaleEncodingObject();
#endif
}
@ -873,10 +877,10 @@ _Py_EncodeLocaleEx(const wchar_t *text, char **str,
// Get the current locale encoding name:
//
// - Return "UTF-8" if _Py_FORCE_UTF8_LOCALE macro is defined (ex: on Android)
// - Return "UTF-8" if the UTF-8 Mode is enabled
// - Return "utf-8" if _Py_FORCE_UTF8_LOCALE macro is defined (ex: on Android)
// - Return "utf-8" if the UTF-8 Mode is enabled
// - On Windows, return the ANSI code page (ex: "cp1250")
// - Return "UTF-8" if nl_langinfo(CODESET) returns an empty string.
// - Return "utf-8" if nl_langinfo(CODESET) returns an empty string.
// - Otherwise, return nl_langinfo(CODESET).
//
// Return NULL on memory allocation failure.
@ -888,12 +892,8 @@ _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 _PyMem_RawWcsdup(L"UTF-8");
return _PyMem_RawWcsdup(L"utf-8");
#else
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
if (preconfig->utf8_mode) {
return _PyMem_RawWcsdup(L"UTF-8");
}
#ifdef MS_WINDOWS
wchar_t encoding[23];
@ -906,7 +906,7 @@ _Py_GetLocaleEncoding(void)
if (!encoding || encoding[0] == '\0') {
// Use UTF-8 if nl_langinfo() returns an empty string. It can happen on
// macOS if the LC_CTYPE locale is not supported.
return _PyMem_RawWcsdup(L"UTF-8");
return _PyMem_RawWcsdup(L"utf-8");
}
wchar_t *wstr;