[Python-checkins] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918)
Serhiy Storchaka
webhook-mailer at python.org
Fri Jun 29 06:15:01 EDT 2018
https://github.com/python/cpython/commit/23db935bcf258657682e66464bf8512def8af830
commit: 23db935bcf258657682e66464bf8512def8af830
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2018-06-29T13:14:58+03:00
summary:
bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918)
files:
A Misc/NEWS.d/next/Core and Builtins/2017-10-07-10-13-15.bpo-25862.FPYBA5.rst
M Lib/_pyio.py
M Lib/test/test_io.py
M Modules/_io/textio.c
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index c91a647a2f67..f0d4f4ed27a2 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -2149,6 +2149,7 @@ def write(self, s):
self.buffer.write(b)
if self._line_buffering and (haslf or "\r" in s):
self.flush()
+ self._set_decoded_chars('')
self._snapshot = None
if self._decoder:
self._decoder.reset()
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 286ae760e17f..a03a7f78109c 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -3549,6 +3549,17 @@ def test_reconfigure_newline(self):
expected = 'linesep' + os.linesep + 'LF\nLF\nCR\rCRLF\r\n'
self.assertEqual(txt.detach().getvalue().decode('ascii'), expected)
+ def test_issue25862(self):
+ # Assertion failures occurred in tell() after read() and write().
+ t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
+ t.read(1)
+ t.read()
+ t.tell()
+ t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
+ t.read(1)
+ t.write('x')
+ t.tell()
+
class MemviewBytesIO(io.BytesIO):
'''A BytesIO object whose read method returns memoryviews
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-07-10-13-15.bpo-25862.FPYBA5.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-07-10-13-15.bpo-25862.FPYBA5.rst
new file mode 100644
index 000000000000..787163643ad8
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-07-10-13-15.bpo-25862.FPYBA5.rst
@@ -0,0 +1,2 @@
+Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
+Patch by Zackery Spytz.
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 209aa13cbd17..6e706cdeb60a 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -694,6 +694,9 @@ typedef struct
PyObject *dict;
} textio;
+static void
+textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
+
/* A couple of specialized cases in order to bypass the slow incremental
encoding methods for the most popular encodings. */
@@ -1606,6 +1609,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
Py_DECREF(ret);
}
+ textiowrapper_set_decoded_chars(self, NULL);
Py_CLEAR(self->snapshot);
if (self->decoder) {
@@ -1835,6 +1839,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
if (result == NULL)
goto fail;
+ textiowrapper_set_decoded_chars(self, NULL);
Py_CLEAR(self->snapshot);
return result;
}
More information about the Python-checkins
mailing list