support encoding error handlers that return bytes (closes #16585)

This commit is contained in:
Benjamin Peterson 2012-12-02 11:20:28 -05:00
parent aff472394c
commit 47a00f3d1a
3 changed files with 13 additions and 2 deletions

View file

@ -316,7 +316,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
goto errorexit;
if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
!PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) ||
(!PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) && !PyBytes_Check(tobj)) ||
!PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) {
PyErr_SetString(PyExc_TypeError,
"encoding error handler must return "
@ -324,7 +324,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
goto errorexit;
}
{
if (PyUnicode_Check(tobj)) {
const Py_UNICODE *uraw = PyUnicode_AS_UNICODE(tobj);
retstr = multibytecodec_encode(codec, state, &uraw,
@ -333,6 +333,10 @@ multibytecodec_encerror(MultibyteCodec *codec,
if (retstr == NULL)
goto errorexit;
}
else {
Py_INCREF(tobj);
retstr = tobj;
}
assert(PyBytes_Check(retstr));
retstrsize = PyBytes_GET_SIZE(retstr);