]> granicus.if.org Git - python/commitdiff
Add _PyBytesWriter_WriteBytes() to factorize the code
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 9 Oct 2015 10:57:22 +0000 (12:57 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 9 Oct 2015 10:57:22 +0000 (12:57 +0200)
Include/bytesobject.h
Objects/bytesobject.c
Objects/stringlib/codecs.h
Objects/unicodeobject.c

index 6cd5a340a5e5d60a3d92ea049c2bc8b102f2ffd7..2c4c4c4fd47fe223105fd41e49d0a809cddc1f78 100644 (file)
@@ -174,6 +174,13 @@ PyAPI_FUNC(char*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
 PyAPI_FUNC(char*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
     char *str,
     Py_ssize_t size);
+
+/* Write bytes.
+   Raise an exception and return NULL on error. */
+PyAPI_FUNC(char*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
+    char *str,
+    char *bytes,
+    Py_ssize_t size);
 #endif   /* Py_LIMITED_API */
 
 #ifdef __cplusplus
index 075edf8c67c114aa0ac231917a790427679fa4be..3aa905c6fcb9ca889090da0529dd90bd94757962 100644 (file)
@@ -3995,3 +3995,17 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
 
     return result;
 }
+
+char*
+_PyBytesWriter_WriteBytes(_PyBytesWriter *writer, char *str,
+                          char *bytes, Py_ssize_t size)
+{
+    str = _PyBytesWriter_Prepare(writer, str, size);
+    if (str == NULL)
+        return NULL;
+
+    Py_MEMCPY(str, bytes, size);
+    str += size;
+
+    return str;
+}
index 6842f67432d58e593b5db559b07cb3e9f91585fe..7e8d928e20bcc1fb853331db32491250e25a534c 100644 (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,
index 0bcacd834fb783eb75c144c725bd9ffc20ceff64..23b8cc764d44de4e7cf70c60606cf1f7caa8d248 100644 (file)
@@ -6706,14 +6706,12 @@ unicode_encode_ucs1(PyObject *unicode,
 
                 if (PyBytes_Check(repunicode)) {
                     /* Directly copy bytes result to output. */
-                    repsize = PyBytes_Size(repunicode);
-
-                    str = _PyBytesWriter_Prepare(&writer, str, repsize);
+                    str = _PyBytesWriter_WriteBytes(&writer, str,
+                                                    PyBytes_AS_STRING(repunicode),
+                                                    PyBytes_GET_SIZE(repunicode));
                     if (str == NULL)
                         goto onError;
 
-                    memcpy(str, PyBytes_AsString(repunicode), repsize);
-                    str += repsize;
                     pos = newpos;
                     Py_DECREF(repunicode);
                     break;