mirror of
https://github.com/python/cpython.git
synced 2025-08-28 04:35:02 +00:00
Writer APIs: use empty string singletons
Modify _PyBytesWriter_Finish() and _PyUnicodeWriter_Finish() to return the empty bytes/Unicode string if the string is empty.
This commit is contained in:
parent
c29e29bed1
commit
6c2cdae9e6
2 changed files with 33 additions and 19 deletions
|
@ -13715,17 +13715,26 @@ _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer)
|
|||
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;
|
||||
if (writer->pos == 0) {
|
||||
Py_CLEAR(writer->buffer);
|
||||
|
||||
/* Get the empty Unicode string singleton ('') */
|
||||
_Py_INCREF_UNICODE_EMPTY();
|
||||
str = unicode_empty;
|
||||
}
|
||||
str = writer->buffer;
|
||||
writer->buffer = NULL;
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue