Issue #28408: Fixed a leak and remove redundant code in _PyUnicodeWriter_Finish().

Patch by Xiang Zhang.
This commit is contained in:
Serhiy Storchaka 2016-10-25 13:25:04 +03:00
commit a4f8823063
1 changed files with 12 additions and 18 deletions

View File

@ -13551,34 +13551,28 @@ PyObject *
_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer)
{ {
PyObject *str; PyObject *str;
if (writer->pos == 0) { if (writer->pos == 0) {
Py_CLEAR(writer->buffer); Py_CLEAR(writer->buffer);
_Py_RETURN_UNICODE_EMPTY(); _Py_RETURN_UNICODE_EMPTY();
} }
str = writer->buffer;
writer->buffer = NULL;
if (writer->readonly) { if (writer->readonly) {
str = writer->buffer;
writer->buffer = NULL;
assert(PyUnicode_GET_LENGTH(str) == writer->pos); assert(PyUnicode_GET_LENGTH(str) == writer->pos);
return str; return str;
} }
if (writer->pos == 0) {
Py_CLEAR(writer->buffer);
/* Get the empty Unicode string singleton ('') */ if (PyUnicode_GET_LENGTH(str) != writer->pos) {
_Py_INCREF_UNICODE_EMPTY(); PyObject *str2;
str = unicode_empty; str2 = resize_compact(str, writer->pos);
} if (str2 == NULL) {
else { Py_DECREF(str);
str = writer->buffer; return NULL;
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;
} }
str = str2;
} }
assert(_PyUnicode_CheckConsistency(str, 1)); assert(_PyUnicode_CheckConsistency(str, 1));