Commit 0464de0f authored by Zackery Spytz's avatar Zackery Spytz Committed by Serhiy Storchaka

[2.7] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918). (GH-8013)

(cherry picked from commit 23db935b)
Co-authored-by: default avatarZackery Spytz <zspytz@gmail.com>
parent 11ba0509
......@@ -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()
......
......@@ -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):
......
Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
Patch by Zackery Spytz.
......@@ -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;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment