mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
bpo-47000: Add locale.getencoding()
(GH-32068)
This commit is contained in:
parent
cd29bd13ef
commit
6773203487
11 changed files with 88 additions and 46 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue