Issue #13560: os.strerror() now uses the current locale encoding instead of UTF-8

This commit is contained in:
Victor Stinner 2011-12-17 04:45:09 +01:00
parent f2ea71fcc8
commit 1f33f2b0c3
5 changed files with 35 additions and 20 deletions

View File

@ -419,6 +419,9 @@ Core and Builtins
Library Library
------- -------
- Issue #13560: os.strerror() now uses the current locale encoding instead of
UTF-8.
- Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize() - Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize()
and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to
the current locale encoding. the current locale encoding.

View File

@ -7891,7 +7891,7 @@ posix_strerror(PyObject *self, PyObject *args)
"strerror() argument out of range"); "strerror() argument out of range");
return NULL; return NULL;
} }
return PyUnicode_FromString(message); return PyUnicode_DecodeLocale(message, 1);
} }

View File

@ -4032,9 +4032,8 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
if (h->h_addrtype != af) { if (h->h_addrtype != af) {
/* Let's get real error message to return */ /* Let's get real error message to return */
PyErr_SetString(PyExc_OSError, errno = EAFNOSUPPORT;
(char *)strerror(EAFNOSUPPORT)); PyErr_SetFromErrno(PyExc_OSError);
return NULL; return NULL;
} }

View File

@ -3132,6 +3132,7 @@ PyUnicode_EncodeLocale(PyObject *unicode, int surrogateescape)
wchar_t *wstr; wchar_t *wstr;
PyObject *bytes = NULL; PyObject *bytes = NULL;
char *errmsg; char *errmsg;
PyObject *reason;
PyObject *exc; PyObject *exc;
size_t error_pos; size_t error_pos;
@ -3193,17 +3194,28 @@ PyUnicode_EncodeLocale(PyObject *unicode, int surrogateescape)
encode_error: encode_error:
errmsg = strerror(errno); errmsg = strerror(errno);
assert(errmsg != NULL); assert(errmsg != NULL);
if (errmsg == NULL)
errmsg = "wcstombs() encountered an unencodable wide character";
PyMem_Free(wstr); PyMem_Free(wstr);
Py_XDECREF(bytes); Py_XDECREF(bytes);
exc = NULL; if (errmsg != NULL)
raise_encode_exception(&exc, reason = PyUnicode_DecodeLocale(errmsg, 1);
"locale", unicode, else
error_pos, error_pos+1, reason = PyUnicode_FromString(
errmsg); "wcstombs() encountered an unencodable "
Py_XDECREF(exc); "wide character");
if (reason == NULL)
return NULL;
exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO",
"locale", unicode,
(Py_ssize_t)error_pos,
(Py_ssize_t)(error_pos+1),
reason);
Py_DECREF(reason);
if (exc != NULL) {
PyCodec_StrictErrors(exc);
Py_XDECREF(exc);
}
return NULL; return NULL;
} }

View File

@ -343,9 +343,7 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
PyObject *message; PyObject *message;
PyObject *v, *args; PyObject *v, *args;
int i = errno; int i = errno;
#ifndef MS_WINDOWS #ifdef MS_WINDOWS
char *s;
#else
WCHAR *s_buf = NULL; WCHAR *s_buf = NULL;
#endif /* Unix/Windows */ #endif /* Unix/Windows */
@ -355,11 +353,14 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
#endif #endif
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
if (i == 0) if (i != 0) {
s = "Error"; /* Sometimes errno didn't get set */ char *s = strerror(i);
else message = PyUnicode_DecodeLocale(s, 1);
s = strerror(i); }
message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); else {
/* Sometimes errno didn't get set */
message = PyUnicode_FromString("Error");
}
#else #else
if (i == 0) if (i == 0)
message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */