mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
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.
This commit is contained in:
parent
06f8c3328d
commit
710e826307
6 changed files with 112 additions and 110 deletions
|
@ -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 */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue