summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-10-12 11:29:43 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-10-12 11:29:43 (GMT)
commit6c2cdae9e6b664a0b3e204773aeb8954ae1508e1 (patch)
tree9827e325a16cfaa1002a63f53bbacf6888d0489b /Objects/bytesobject.c
parentc29e29bed1ea1efc1a0cd3178fac96be4d763ecf (diff)
downloadcpython-6c2cdae9e6b664a0b3e204773aeb8954ae1508e1.zip
cpython-6c2cdae9e6b664a0b3e204773aeb8954ae1508e1.tar.gz
cpython-6c2cdae9e6b664a0b3e204773aeb8954ae1508e1.tar.bz2
Writer APIs: use empty string singletons
Modify _PyBytesWriter_Finish() and _PyUnicodeWriter_Finish() to return the empty bytes/Unicode string if the string is empty.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 4b31271..532051e 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -4019,19 +4019,24 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
_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;
}