summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2018-01-15 22:43:24 (GMT)
committerGitHub <noreply@github.com>2018-01-15 22:43:24 (GMT)
commitb92c159efada05b3a5ff9d0dbce3fcb2334631f6 (patch)
treee0c7a7c67133cd4b7e6b17383ff69e2b7e5e11cc /Objects
parent5f959c4f9eca404b8bc4bc6348fed27c4b907b89 (diff)
downloadcpython-b92c159efada05b3a5ff9d0dbce3fcb2334631f6.zip
cpython-b92c159efada05b3a5ff9d0dbce3fcb2334631f6.tar.gz
cpython-b92c159efada05b3a5ff9d0dbce3fcb2334631f6.tar.bz2
[3.6] bpo-32555: Fix locale encodings (#5193)
On FreeBSD and Solaris, os.strerror() now always decode the byte string from the current locale encoding, rather than using ASCII/surrogateescape in some cases. Changes: * Add _Py_DecodeLocaleEx() and _Py_EncodeLocaleEx() which has an additional current_locale parameter. * PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize() and * PyUnicode_EncodeLocale() now always use the current locale * encoding, instead of using Py_DecodeLocale()/Py_EncodeLocale(). * Document encoding in Py_DecodeLocale() and Py_EncodeLocale() documentations. * Add USE_FORCE_ASCII define to not define decode_ascii_surrogateescape() on Android.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c36
1 files changed, 26 insertions, 10 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 64905e8..86cac96 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3439,8 +3439,9 @@ locale_error_handler(const char *errors, int *surrogateescape)
}
}
-PyObject *
-PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
+static PyObject *
+unicode_encode_locale(PyObject *unicode, const char *errors,
+ int current_locale)
{
Py_ssize_t wlen, wlen2;
wchar_t *wstr;
@@ -3469,7 +3470,7 @@ PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
/* "surrogateescape" error handler */
char *str;
- str = Py_EncodeLocale(wstr, &error_pos);
+ str = _Py_EncodeLocaleEx(wstr, &error_pos, current_locale);
if (str == NULL) {
if (error_pos == (size_t)-1) {
PyErr_NoMemory();
@@ -3550,6 +3551,12 @@ encode_error:
}
PyObject *
+PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
+{
+ return unicode_encode_locale(unicode, errors, 1);
+}
+
+PyObject *
PyUnicode_EncodeFSDefault(PyObject *unicode)
{
#if defined(__APPLE__)
@@ -3571,7 +3578,8 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
Py_FileSystemDefaultEncodeErrors);
}
else {
- return PyUnicode_EncodeLocale(unicode, Py_FileSystemDefaultEncodeErrors);
+ return unicode_encode_locale(unicode,
+ Py_FileSystemDefaultEncodeErrors, 0);
}
#endif
}
@@ -3741,9 +3749,9 @@ mbstowcs_errorpos(const char *str, size_t len)
return 0;
}
-PyObject*
-PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
- const char *errors)
+static PyObject*
+unicode_decode_locale(const char *str, Py_ssize_t len,
+ const char *errors, int current_locale)
{
wchar_t smallbuf[256];
size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf);
@@ -3766,7 +3774,7 @@ PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
if (surrogateescape) {
/* "surrogateescape" error handler */
- wstr = Py_DecodeLocale(str, &wlen);
+ wstr = _Py_DecodeLocaleEx(str, &wlen, current_locale);
if (wstr == NULL) {
if (wlen == (size_t)-1)
PyErr_NoMemory();
@@ -3845,10 +3853,17 @@ decode_error:
}
PyObject*
+PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t size,
+ const char *errors)
+{
+ return unicode_decode_locale(str, size, errors, 1);
+}
+
+PyObject*
PyUnicode_DecodeLocale(const char *str, const char *errors)
{
Py_ssize_t size = (Py_ssize_t)strlen(str);
- return PyUnicode_DecodeLocaleAndSize(str, size, errors);
+ return unicode_decode_locale(str, size, errors, 1);
}
@@ -3880,7 +3895,8 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
Py_FileSystemDefaultEncodeErrors);
}
else {
- return PyUnicode_DecodeLocaleAndSize(s, size, Py_FileSystemDefaultEncodeErrors);
+ return unicode_decode_locale(s, size,
+ Py_FileSystemDefaultEncodeErrors, 0);
}
#endif
}