[Python-checkins] cpython: Writer APIs: use empty string singletons

victor.stinner python-checkins at python.org
Mon Oct 12 07:40:48 EDT 2015


https://hg.python.org/cpython/rev/f33433d9c163
changeset:   98712:f33433d9c163
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Oct 12 13:29:43 2015 +0200
summary:
  Writer APIs: use empty string singletons

Modify _PyBytesWriter_Finish() and _PyUnicodeWriter_Finish() to return the
empty bytes/Unicode string if the string is empty.

files:
  Objects/bytesobject.c   |  23 +++++++++++++--------
  Objects/unicodeobject.c |  31 ++++++++++++++++++----------
  2 files changed, 34 insertions(+), 20 deletions(-)


diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -4019,19 +4019,24 @@
     _PyBytesWriter_CheckConsistency(writer, str);
 
     pos = _PyBytesWriter_GetPos(writer, str);
-    if (!writer->use_small_buffer) {
+    if (pos == 0) {
+        Py_CLEAR(writer->buffer);
+        /* Get the empty byte string singleton */
+        result = PyBytes_FromStringAndSize(NULL, 0);
+    }
+    else if (writer->use_small_buffer) {
+        result = PyBytes_FromStringAndSize(writer->small_buffer, pos);
+    }
+    else {
+        result = writer->buffer;
+        writer->buffer = NULL;
+
         if (pos != writer->allocated) {
-            if (_PyBytes_Resize(&writer->buffer, pos)) {
-                assert(writer->buffer == NULL);
+            if (_PyBytes_Resize(&result, pos)) {
+                assert(result == NULL);
                 return NULL;
             }
         }
-
-        result = writer->buffer;
-        writer->buffer = NULL;
-    }
-    else {
-        result = PyBytes_FromStringAndSize(writer->small_buffer, pos);
     }
     return result;
 }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -13715,17 +13715,26 @@
         assert(PyUnicode_GET_LENGTH(str) == writer->pos);
         return str;
     }
-    if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) {
-        PyObject *newbuffer;
-        newbuffer = resize_compact(writer->buffer, writer->pos);
-        if (newbuffer == NULL) {
-            Py_CLEAR(writer->buffer);
-            return NULL;
-        }
-        writer->buffer = newbuffer;
-    }
-    str = writer->buffer;
-    writer->buffer = NULL;
+    if (writer->pos == 0) {
+        Py_CLEAR(writer->buffer);
+
+        /* Get the empty Unicode string singleton ('') */
+        _Py_INCREF_UNICODE_EMPTY();
+        str  = unicode_empty;
+    }
+    else {
+        str = writer->buffer;
+        writer->buffer = NULL;
+
+        if (PyUnicode_GET_LENGTH(str) != writer->pos) {
+            PyObject *str2;
+            str2 = resize_compact(str, writer->pos);
+            if (str2 == NULL)
+                return NULL;
+            str = str2;
+        }
+    }
+
     assert(_PyUnicode_CheckConsistency(str, 1));
     return unicode_result_ready(str);
 }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list