Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 111 additions & 95 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
cmaloney marked this conversation as resolved.
Resolved
}
else {
Expand All @@ -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;
}
}
}
Expand All @@ -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
Expand All @@ -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);
}
Expand All @@ -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;
}
}

Expand All @@ -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
Expand Down
Loading