Commit 9e6b4d71 authored by Victor Stinner's avatar Victor Stinner

Issue #18408: _PyUnicodeWriter_Finish() now clears its buffer attribute in all

cases, so _PyUnicodeWriter_Dealloc() can be called after finish.
parent 79799266
...@@ -13159,18 +13159,21 @@ _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) ...@@ -13159,18 +13159,21 @@ _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer)
{ {
PyObject *str; PyObject *str;
if (writer->pos == 0) { if (writer->pos == 0) {
Py_XDECREF(writer->buffer); Py_CLEAR(writer->buffer);
_Py_RETURN_UNICODE_EMPTY(); _Py_RETURN_UNICODE_EMPTY();
} }
if (writer->readonly) { if (writer->readonly) {
assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); str = writer->buffer;
return writer->buffer; writer->buffer = NULL;
assert(PyUnicode_GET_LENGTH(str) == writer->pos);
return str;
} }
if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) {
PyObject *newbuffer; PyObject *newbuffer;
newbuffer = resize_compact(writer->buffer, writer->pos); newbuffer = resize_compact(writer->buffer, writer->pos);
if (newbuffer == NULL) { if (newbuffer == NULL) {
Py_DECREF(writer->buffer); Py_DECREF(writer->buffer);
writer->buffer = NULL;
return NULL; return NULL;
} }
writer->buffer = newbuffer; writer->buffer = newbuffer;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment