Add _PyBytesWriter_WriteBytes() to factorize the code

This commit is contained in:
Victor Stinner 2015-10-09 12:57:22 +02:00
parent ad7715891e
commit ce179bf6ba
4 changed files with 35 additions and 16 deletions

View file

@ -388,24 +388,24 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
/* substract preallocated bytes */
writer.min_size -= max_char_size;
if (PyBytes_Check(rep))
repsize = PyBytes_GET_SIZE(rep);
else
repsize = PyUnicode_GET_LENGTH(rep);
p = _PyBytesWriter_Prepare(&writer, p, repsize);
if (p == NULL)
goto error;
if (PyBytes_Check(rep)) {
memcpy(p, PyBytes_AS_STRING(rep), repsize);
p += repsize;
p = _PyBytesWriter_WriteBytes(&writer, p,
PyBytes_AS_STRING(rep),
PyBytes_GET_SIZE(rep));
if (p == NULL)
goto error;
}
else {
/* rep is unicode */
if (PyUnicode_READY(rep) < 0)
goto error;
repsize = PyUnicode_GET_LENGTH(rep);
p = _PyBytesWriter_Prepare(&writer, p, repsize);
if (p == NULL)
goto error;
if (!PyUnicode_IS_ASCII(rep)) {
raise_encode_exception(&exc, "utf-8",
unicode,