| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Fix unbounded memory growth in :class:`io.TextIOWrapper` when repeatedly | ||
| 2 | + writing an empty string. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1741,32 +1741,38 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) | |||
| 1741 | 1741 | } | |
| 1742 | 1742 | } | |
| 1743 | 1743 | ||
| 1744 | - if (self->pending_bytes == NULL) { | ||
| 1745 | - assert(self->pending_bytes_count == 0); | ||
| 1746 | - self->pending_bytes = b; | ||
| 1747 | - } | ||
| 1748 | - else if (!PyList_CheckExact(self->pending_bytes)) { | ||
| 1749 | - PyObject *list = PyList_New(2); | ||
| 1750 | - if (list == NULL) { | ||
| 1744 | + if (bytes_len > 0) { | ||
| 1745 | + if (self->pending_bytes == NULL) { | ||
| 1746 | + assert(self->pending_bytes_count == 0); | ||
| 1747 | + self->pending_bytes = b; | ||
| 1748 | + } | ||
| 1749 | + else if (!PyList_CheckExact(self->pending_bytes)) { | ||
| 1750 | + PyObject *list = PyList_New(2); | ||
| 1751 | + if (list == NULL) { | ||
| 1752 | + Py_DECREF(b); | ||
| 1753 | + return NULL; | ||
| 1754 | + } | ||
| 1755 | + // Since Python 3.12, allocating GC object won't trigger GC and release | ||
| 1756 | + // GIL. See https://github.com/python/cpython/issues/97922 | ||
| 1757 | + assert(!PyList_CheckExact(self->pending_bytes)); | ||
| 1758 | + PyList_SET_ITEM(list, 0, self->pending_bytes); | ||
| 1759 | + PyList_SET_ITEM(list, 1, b); | ||
| 1760 | + self->pending_bytes = list; | ||
| 1761 | + } | ||
| 1762 | + else { | ||
| 1763 | + if (PyList_Append(self->pending_bytes, b) < 0) { | ||
| 1764 | + Py_DECREF(b); | ||
| 1765 | + return NULL; | ||
| 1766 | + } | ||
| 1751 | 1767 | Py_DECREF(b); | |
| 1752 | - return NULL; | ||
| 1753 | 1768 | } | |
| 1754 | - // Since Python 3.12, allocating GC object won't trigger GC and release | ||
| 1755 | - // GIL. See https://github.com/python/cpython/issues/97922 | ||
| 1756 | - assert(!PyList_CheckExact(self->pending_bytes)); | ||
| 1757 | - PyList_SET_ITEM(list, 0, self->pending_bytes); | ||
| 1758 | - PyList_SET_ITEM(list, 1, b); | ||
| 1759 | - self->pending_bytes = list; | ||
| 1769 | + | ||
| 1770 | + self->pending_bytes_count += bytes_len; | ||
| 1760 | 1771 | } | |
| 1761 | 1772 | else { | |
| 1762 | - if (PyList_Append(self->pending_bytes, b) < 0) { | ||
| 1763 | - Py_DECREF(b); | ||
| 1764 | - return NULL; | ||
| 1765 | - } | ||
| 1766 | 1773 | Py_DECREF(b); | |
| 1767 | 1774 | } | |
| 1768 | 1775 | ||
| 1769 | - self->pending_bytes_count += bytes_len; | ||
| 1770 | 1776 | if (self->pending_bytes_count >= self->chunk_size || needflush || | |
| 1771 | 1777 | text_needflush) { | |
| 1772 | 1778 | if (_textiowrapper_writeflush(self) < 0) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments