Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,26 +1426,28 @@ _PyCodec_SurrogateEscapeUnicodeEncodeError(PyObject *exc)
return NULL;
}

PyObject *res = PyBytes_FromStringAndSize(NULL, slen);
if (res == NULL) {
PyBytesWriter *writer = PyBytesWriter_Create(slen);
if (writer == NULL) {
Py_DECREF(obj);
return NULL;
}

char *outp = PyBytes_AsString(res);
char *outp = PyBytesWriter_GetData(writer);
for (Py_ssize_t i = start; i < end; i++) {
Py_UCS4 ch = PyUnicode_READ_CHAR(obj, i);
if (ch < 0xdc80 || ch > 0xdcff) {
/* Not a UTF-8b surrogate, fail with original exception. */
Py_DECREF(obj);
Py_DECREF(res);
PyBytesWriter_Discard(writer);
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
return NULL;
}
*outp++ = ch - 0xdc00;
}
Py_DECREF(obj);

PyObject *res = PyBytesWriter_Finish(writer);
// Py_BuildValue() propagates the exception if res is NULL
return Py_BuildValue("(Nn)", res, end);
}

Expand Down
Loading