diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-11-08 22:43:46 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-11-08 22:43:46 (GMT) |
commit | 2f02a511353c761bef2ab867ca4295bbb0217b96 (patch) | |
tree | 955a54d70d6f09279e7fd593e15f003d139b7b2b /Objects | |
parent | 0cfba09b097d2aa4d35c7b1411cb32565f17a891 (diff) | |
download | cpython-2f02a511353c761bef2ab867ca4295bbb0217b96.zip cpython-2f02a511353c761bef2ab867ca4295bbb0217b96.tar.gz cpython-2f02a511353c761bef2ab867ca4295bbb0217b96.tar.bz2 |
PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
* Add error_pos optional argument to _Py_wchar2char()
* PyUnicode_EncodeFS() raises a UnicodeEncodeError or MemoryError if
_Py_wchar2char() fails
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index cff756a..2250f45 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1606,14 +1606,31 @@ PyUnicode_EncodeFSDefault(PyObject *unicode) wchar_t *wchar; char *bytes; PyObject *bytes_obj; + size_t error_pos; wchar = PyUnicode_AsWideCharString(unicode, NULL); if (wchar == NULL) return NULL; - bytes = _Py_wchar2char(wchar); - PyMem_Free(wchar); - if (bytes == NULL) + bytes = _Py_wchar2char(wchar, &error_pos); + if (bytes == NULL) { + if (error_pos != (size_t)-1) { + char *errmsg = strerror(errno); + PyObject *exc = NULL; + if (errmsg == NULL) + errmsg = "Py_wchar2char() failed"; + raise_encode_exception(&exc, + "filesystemencoding", + PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), + error_pos, error_pos+1, + errmsg); + Py_XDECREF(exc); + } + else + PyErr_NoMemory(); + PyMem_Free(wchar); return NULL; + } + PyMem_Free(wchar); bytes_obj = PyBytes_FromString(bytes); PyMem_Free(bytes); |