summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-12-17 03:45:09 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-12-17 03:45:09 (GMT)
commit1f33f2b0c381337d5991c227652d65eadd168209 (patch)
tree4f2303936f90a15a9d7a132a3d5f57c238eba38d
parentf2ea71fcc8986101512265b685d8d3dfdf7b7bdb (diff)
downloadcpython-1f33f2b0c381337d5991c227652d65eadd168209.zip
cpython-1f33f2b0c381337d5991c227652d65eadd168209.tar.gz
cpython-1f33f2b0c381337d5991c227652d65eadd168209.tar.bz2
Issue #13560: os.strerror() now uses the current locale encoding instead of UTF-8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/posixmodule.c2
-rw-r--r--Modules/socketmodule.c5
-rw-r--r--Objects/unicodeobject.c28
-rw-r--r--Python/errors.c17
5 files changed, 35 insertions, 20 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 51505d4..286192c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -419,6 +419,9 @@ Core and Builtins
Library
-------
+- Issue #13560: os.strerror() now uses the current locale encoding instead of
+ UTF-8.
+
- Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize()
and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to
the current locale encoding.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 8ba2975..6b832c0 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7891,7 +7891,7 @@ posix_strerror(PyObject *self, PyObject *args)
"strerror() argument out of range");
return NULL;
}
- return PyUnicode_FromString(message);
+ return PyUnicode_DecodeLocale(message, 1);
}
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index c828d49..132d6c7 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4032,9 +4032,8 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
if (h->h_addrtype != af) {
/* Let's get real error message to return */
- PyErr_SetString(PyExc_OSError,
- (char *)strerror(EAFNOSUPPORT));
-
+ errno = EAFNOSUPPORT;
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index a2c3227..2fea30b 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3132,6 +3132,7 @@ PyUnicode_EncodeLocale(PyObject *unicode, int surrogateescape)
wchar_t *wstr;
PyObject *bytes = NULL;
char *errmsg;
+ PyObject *reason;
PyObject *exc;
size_t error_pos;
@@ -3193,17 +3194,28 @@ PyUnicode_EncodeLocale(PyObject *unicode, int surrogateescape)
encode_error:
errmsg = strerror(errno);
assert(errmsg != NULL);
- if (errmsg == NULL)
- errmsg = "wcstombs() encountered an unencodable wide character";
PyMem_Free(wstr);
Py_XDECREF(bytes);
- exc = NULL;
- raise_encode_exception(&exc,
- "locale", unicode,
- error_pos, error_pos+1,
- errmsg);
- Py_XDECREF(exc);
+ if (errmsg != NULL)
+ reason = PyUnicode_DecodeLocale(errmsg, 1);
+ else
+ reason = PyUnicode_FromString(
+ "wcstombs() encountered an unencodable "
+ "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;
}
diff --git a/Python/errors.c b/Python/errors.c
index 36b8c54..122e444 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -343,9 +343,7 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
PyObject *message;
PyObject *v, *args;
int i = errno;
-#ifndef MS_WINDOWS
- char *s;
-#else
+#ifdef MS_WINDOWS
WCHAR *s_buf = NULL;
#endif /* Unix/Windows */
@@ -355,11 +353,14 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
#endif
#ifndef MS_WINDOWS
- if (i == 0)
- s = "Error"; /* Sometimes errno didn't get set */
- else
- s = strerror(i);
- message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
+ if (i != 0) {
+ char *s = strerror(i);
+ message = PyUnicode_DecodeLocale(s, 1);
+ }
+ else {
+ /* Sometimes errno didn't get set */
+ message = PyUnicode_FromString("Error");
+ }
#else
if (i == 0)
message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */