From ce179bf6baed91ba84cc3ff647e96287c3b8e2f2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 9 Oct 2015 12:57:22 +0200 Subject: Add _PyBytesWriter_WriteBytes() to factorize the code --- Objects/stringlib/codecs.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'Objects/stringlib/codecs.h') diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h index 6842f67432..7e8d928e20 100644 --- a/Objects/stringlib/codecs.h +++ b/Objects/stringlib/codecs.h @@ -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, -- cgit v1.2.1