]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918). (GH...
authorZackery Spytz <zspytz@gmail.com>
Fri, 29 Jun 2018 20:07:13 +0000 (14:07 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 29 Jun 2018 20:07:13 +0000 (23:07 +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 f022a4e88b8ed7ff197b7ae82f0f8b61c20e3fc0..98c2d58d0d8605729acca9db8c362881169310ed 100644 (file)
@@ -1619,6 +1619,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 a7257115fc9089492e3f371fa1f9e5078325397d..ebaf7960401307f74751c72fc334cb6b0f65d500 100644 (file)
@@ -2741,6 +2741,17 @@ class TextIOWrapperTest(unittest.TestCase):
         with self.maybeRaises(TypeError):
             t.read(42)
 
+    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 CTextIOWrapperTest(TextIOWrapperTest):
 
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 bf37f72bd11f26a788fc4310e5afd9cbecf4f7fc..1979539cc05469b08f830764be24dc743caa6a34 100644 (file)
@@ -707,6 +707,8 @@ 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. */
@@ -1329,6 +1331,7 @@ textiowrapper_write(textio *self, PyObject *args)
         Py_DECREF(ret);
     }
 
+    textiowrapper_set_decoded_chars(self, NULL);
     Py_CLEAR(self->snapshot);
 
     if (self->decoder) {
@@ -1534,6 +1537,7 @@ textiowrapper_read(textio *self, PyObject *args)
         if (final == NULL)
             goto fail;
 
+        textiowrapper_set_decoded_chars(self, NULL);
         Py_CLEAR(self->snapshot);
         return final;
     }