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
This commit is contained in:
Victor Stinner 2010-11-08 22:43:46 +00:00
parent 0cfba09b09
commit 2f02a51135
4 changed files with 37 additions and 11 deletions

View file

@ -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);