]> granicus.if.org Git - python/commitdiff
[3.6] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918). (GH...
authorZackery Spytz <zspytz@gmail.com>
Fri, 29 Jun 2018 19:30:07 +0000 (13:30 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 29 Jun 2018 19:30:07 +0000 (22:30 +0300)
(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 2ebfb0576f592bd68cffd07f69b18d3880413350..b577dede5da86053845ca901c05839909cd9e4cb 100644 (file)
@@ -2064,6 +2064,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 0b5b033b5f88a7446c3da077596e0ce28a555d98..88fd6ce4a6beae039c5f7b96f64c830926579e31 100644 (file)
@@ -3348,6 +3348,17 @@ class TextIOWrapperTest(unittest.TestCase):
         F.tell = lambda x: 0
         t = self.TextIOWrapper(F(), encoding='utf-8')
 
+    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 4638b4759701f84887909c6fa38a9af66046e3fe..f86a35f744487abd4f49b9184d6a214ab3c50064 100644 (file)
@@ -696,6 +696,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. */
 
@@ -1367,6 +1370,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) {
@@ -1602,6 +1606,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;
     }