mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Add _PyBytesWriter_WriteBytes() to factorize the code
This commit is contained in:
parent
ad7715891e
commit
ce179bf6ba
4 changed files with 35 additions and 16 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue