From 88a71450afec8adb1701bb010752a99b6b498f7f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 14 Nov 2026 06:54:50 +0000 Subject: [PATCH 1/2] gh-161934: Split TextIOWrapper.write into text and byte helpers --- Modules/_io/textio.c | 205 +++++++++++++++++++++++-------------------- 1 file changed, 110 insertions(+), 95 deletions(-) diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 4929e41ba9ccb41..7fce604f6ca1fce 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1752,95 +1752,13 @@ textiowrapper_check_write(textio *self) return Py_None; } -/*[clinic input] -@critical_section -_io.TextIOWrapper.write - text: unicode - / -[clinic start generated code]*/ - -static PyObject * -_io_TextIOWrapper_write_impl(textio *self, PyObject *text) -/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/ +/* Consume b, which is either bytes or an ASCII string. */ +static int +textiowrapper_write_bytes(textio *self, PyObject *b, + int needflush, int text_needflush) { - PyObject *ret; - PyObject *b; - Py_ssize_t textlen; - int haslf = 0; - int needflush = 0, text_needflush = 0; - - if (textiowrapper_check_write(self) == NULL) { - return NULL; - } - - Py_INCREF(text); - - textlen = PyUnicode_GET_LENGTH(text); - - if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) - if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) - haslf = 1; - - if (haslf && self->writetranslate && self->writenl != NULL) { - PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), - "ss", "\n", self->writenl); - Py_DECREF(text); - if (newtext == NULL) - return NULL; - if (!PyUnicode_Check(newtext)) { - PyErr_Format(PyExc_TypeError, - "replace() should return a str, not %T", newtext); - Py_DECREF(newtext); - return NULL; - } - /* replace() may have closed, detached or reinitialized the stream. */ - if (textiowrapper_check_write(self) == NULL) { - Py_DECREF(newtext); - return NULL; - } - text = newtext; - } - - if (self->write_through) - text_needflush = 1; - if (self->line_buffering && - (haslf || - PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) - needflush = 1; - - /* XXX What if we were just reading? */ - if (self->encodefunc != NULL) { - if (PyUnicode_IS_ASCII(text) && - // See bpo-43260 - PyUnicode_GET_LENGTH(text) <= self->chunk_size && - is_asciicompat_encoding(self->encodefunc)) { - b = Py_NewRef(text); - } - else { - b = (*self->encodefunc)((PyObject *) self, text); - } - self->encoding_start_of_stream = 0; - } - else { - /* A codec callback may reconfigure the stream. */ - PyObject *encoder = Py_NewRef(self->encoder); - b = PyObject_CallMethodOneArg(encoder, &_Py_ID(encode), text); - Py_DECREF(encoder); - } - - Py_DECREF(text); - if (b == NULL) - return NULL; - if (b != text && !PyBytes_Check(b)) { - PyErr_Format(PyExc_TypeError, - "encoder should return a bytes object, not '%.200s'", - Py_TYPE(b)->tp_name); - Py_DECREF(b); - return NULL; - } - Py_ssize_t bytes_len; - if (b == text) { + if (PyUnicode_Check(b)) { bytes_len = PyUnicode_GET_LENGTH(b); } else { @@ -1860,7 +1778,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) while (self->pending_bytes != NULL) { if (_textiowrapper_writeflush(self) < 0) { Py_DECREF(b); - return NULL; + return -1; } } } @@ -1874,7 +1792,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) PyObject *list = PyList_New(2); if (list == NULL) { Py_DECREF(b); - return NULL; + return -1; } // Since Python 3.12, allocating GC object won't trigger GC and release // GIL. See https://github.com/python/cpython/issues/97922 @@ -1886,7 +1804,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) else { if (PyList_Append(self->pending_bytes, b) < 0) { Py_DECREF(b); - return NULL; + return -1; } Py_DECREF(b); } @@ -1900,18 +1818,18 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) if (self->pending_bytes_count >= self->chunk_size || needflush || text_needflush) { if (_textiowrapper_writeflush(self) < 0) - return NULL; + return -1; } if (needflush) { PyObject *buffer = buffer_access_safe(self); if (buffer == NULL) { - return NULL; + return -1; } int res = _PyFile_Flush(buffer); Py_DECREF(buffer); if (res < 0) { - return NULL; + return -1; } } @@ -1921,15 +1839,112 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) } if (self->decoder) { - ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); + PyObject *ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); if (ret == NULL) - return NULL; + return -1; Py_DECREF(ret); } + return 0; +} + +static PyObject * +textiowrapper_write_text(textio *self, PyObject *text) +{ + PyObject *b; + Py_ssize_t textlen; + int haslf = 0; + int needflush = 0, text_needflush = 0; + + Py_INCREF(text); + + textlen = PyUnicode_GET_LENGTH(text); + + if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) + if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) + haslf = 1; + + if (haslf && self->writetranslate && self->writenl != NULL) { + PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), + "ss", "\n", self->writenl); + Py_DECREF(text); + if (newtext == NULL) + return NULL; + if (!PyUnicode_Check(newtext)) { + PyErr_Format(PyExc_TypeError, + "replace() should return a str, not %T", newtext); + Py_DECREF(newtext); + return NULL; + } + /* replace() may have closed, detached or reinitialized the stream. */ + if (textiowrapper_check_write(self) == NULL) { + Py_DECREF(newtext); + return NULL; + } + text = newtext; + } + + if (self->write_through) + text_needflush = 1; + if (self->line_buffering && + (haslf || + PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) + needflush = 1; + + /* XXX What if we were just reading? */ + if (self->encodefunc != NULL) { + if (PyUnicode_IS_ASCII(text) && + // See bpo-43260 + PyUnicode_GET_LENGTH(text) <= self->chunk_size && + is_asciicompat_encoding(self->encodefunc)) { + b = Py_NewRef(text); + } + else { + b = (*self->encodefunc)((PyObject *) self, text); + } + self->encoding_start_of_stream = 0; + } + else { + /* A codec callback may reconfigure the stream. */ + PyObject *encoder = Py_NewRef(self->encoder); + b = PyObject_CallMethodOneArg(encoder, &_Py_ID(encode), text); + Py_DECREF(encoder); + } + + Py_DECREF(text); + if (b == NULL) + return NULL; + if (b != text && !PyBytes_Check(b)) { + PyErr_Format(PyExc_TypeError, + "encoder should return a bytes object, not '%.200s'", + Py_TYPE(b)->tp_name); + Py_DECREF(b); + return NULL; + } + + if (textiowrapper_write_bytes(self, b, needflush, text_needflush) < 0) { + return NULL; + } return PyLong_FromSsize_t(textlen); } +/*[clinic input] +@critical_section +_io.TextIOWrapper.write + text: unicode + / +[clinic start generated code]*/ + +static PyObject * +_io_TextIOWrapper_write_impl(textio *self, PyObject *text) +/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/ +{ + if (textiowrapper_check_write(self) == NULL) { + return NULL; + } + return textiowrapper_write_text(self, text); +} + /* Steal a reference to chars and store it in the decoded_char buffer; */ static void From 3af8ce4e10d7d4f4fc359c98e2fc69d3cb09f30f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 15 Nov 2026 08:33:31 +0000 Subject: [PATCH 2/2] gh-161934: Assert text write buffers contain ASCII --- Modules/_io/textio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 7fce604f6ca1fce..719f9ef36f49570 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1759,6 +1759,7 @@ textiowrapper_write_bytes(textio *self, PyObject *b, { Py_ssize_t bytes_len; if (PyUnicode_Check(b)) { + assert(PyUnicode_IS_ASCII(b)); bytes_len = PyUnicode_GET_LENGTH(b); } else {