https://github.com/python/cpython/commit/7c59816cb4d3db8387c454ce33d6b56c7d93fa6e
commit: 7c59816cb4d3db8387c454ce33d6b56c7d93fa6e
branch: main
author: Inada Naoki <[email protected]>
committer: methane <[email protected]>
date: 2026-11-16T11:05:00+09:00
summary:
gh-161934: Split TextIOWrapper.write into text and byte helpers (#161991)
files:
M Modules/_io/textio.c
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 4929e41ba9ccb41..719f9ef36f49570 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1752,95 +1752,14 @@ 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)) {
+ assert(PyUnicode_IS_ASCII(b));
bytes_len = PyUnicode_GET_LENGTH(b);
}
else {
@@ -1860,7 +1779,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 +1793,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 +1805,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 +1819,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 +1840,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
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]