Skip to content

Commit ce179bf

Browse files
committed
Add _PyBytesWriter_WriteBytes() to factorize the code
1 parent ad77158 commit ce179bf

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

Include/bytesobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ PyAPI_FUNC(char*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
174174
PyAPI_FUNC(char*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
175175
char *str,
176176
Py_ssize_t size);
177+
178+
/* Write bytes.
179+
Raise an exception and return NULL on error. */
180+
PyAPI_FUNC(char*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
181+
char *str,
182+
char *bytes,
183+
Py_ssize_t size);
177184
#endif /* Py_LIMITED_API */
178185

179186
#ifdef __cplusplus

Objects/bytesobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,3 +3995,17 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
39953995

39963996
return result;
39973997
}
3998+
3999+
char*
4000+
_PyBytesWriter_WriteBytes(_PyBytesWriter *writer, char *str,
4001+
char *bytes, Py_ssize_t size)
4002+
{
4003+
str = _PyBytesWriter_Prepare(writer, str, size);
4004+
if (str == NULL)
4005+
return NULL;
4006+
4007+
Py_MEMCPY(str, bytes, size);
4008+
str += size;
4009+
4010+
return str;
4011+
}

Objects/stringlib/codecs.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,24 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
388388
/* substract preallocated bytes */
389389
writer.min_size -= max_char_size;
390390

391-
if (PyBytes_Check(rep))
392-
repsize = PyBytes_GET_SIZE(rep);
393-
else
394-
repsize = PyUnicode_GET_LENGTH(rep);
395-
396-
p = _PyBytesWriter_Prepare(&writer, p, repsize);
397-
if (p == NULL)
398-
goto error;
399-
400391
if (PyBytes_Check(rep)) {
401-
memcpy(p, PyBytes_AS_STRING(rep), repsize);
402-
p += repsize;
392+
p = _PyBytesWriter_WriteBytes(&writer, p,
393+
PyBytes_AS_STRING(rep),
394+
PyBytes_GET_SIZE(rep));
395+
if (p == NULL)
396+
goto error;
403397
}
404398
else {
405399
/* rep is unicode */
406400
if (PyUnicode_READY(rep) < 0)
407401
goto error;
408402

403+
repsize = PyUnicode_GET_LENGTH(rep);
404+
405+
p = _PyBytesWriter_Prepare(&writer, p, repsize);
406+
if (p == NULL)
407+
goto error;
408+
409409
if (!PyUnicode_IS_ASCII(rep)) {
410410
raise_encode_exception(&exc, "utf-8",
411411
unicode,

Objects/unicodeobject.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6706,14 +6706,12 @@ unicode_encode_ucs1(PyObject *unicode,
67066706

67076707
if (PyBytes_Check(repunicode)) {
67086708
/* Directly copy bytes result to output. */
6709-
repsize = PyBytes_Size(repunicode);
6710-
6711-
str = _PyBytesWriter_Prepare(&writer, str, repsize);
6709+
str = _PyBytesWriter_WriteBytes(&writer, str,
6710+
PyBytes_AS_STRING(repunicode),
6711+
PyBytes_GET_SIZE(repunicode));
67126712
if (str == NULL)
67136713
goto onError;
67146714

6715-
memcpy(str, PyBytes_AsString(repunicode), repsize);
6716-
str += repsize;
67176715
pos = newpos;
67186716
Py_DECREF(repunicode);
67196717
break;

0 commit comments

Comments
 (0)