]> granicus.if.org Git - python/commitdiff
bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 29 Jun 2018 10:34:34 +0000 (03:34 -0700)
committerGitHub <noreply@github.com>
Fri, 29 Jun 2018 10:34:34 +0000 (03:34 -0700)
(cherry picked from commit 23db935bcf258657682e66464bf8512def8af830)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/_pyio.py
Lib/test/test_io.py
Misc/NEWS.d/next/Core and Builtins/2017-10-07-10-13-15.bpo-25862.FPYBA5.rst [new file with mode: 0644]
Modules/_io/textio.c

index c91a647a2f67b0da07e0760c3ef556e35465d255..f0d4f4ed27a2434d4ee097b32c9265010336e67d 100644 (file)
@@ -2149,6 +2149,7 @@ class TextIOWrapper(TextIOBase):
         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()
index 286ae760e17fcbeafb9a1e2fe6733c6f79256995..a03a7f78109c234fb36b2294b85d7cf15c77b183 100644 (file)
@@ -3549,6 +3549,17 @@ class TextIOWrapperTest(unittest.TestCase):
         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 (file)
index 0000000..7871636
--- /dev/null
@@ -0,0 +1,2 @@
+Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
+Patch by Zackery Spytz.
index 717b56ab319b618a460d720d3150c2a0922e588d..fa162e2c6facae22a243fd559923fc7a8734990a 100644 (file)
@@ -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;
     }