diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 5f2e4000057..a2ee9525f5c 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1667,7 +1667,9 @@ class TextIOWrapper(TextIOBase): self._snapshot = None # Restore the decoder to its state from the safe start point. - if self._decoder or dec_flags or chars_to_skip: + if cookie == 0 and self._decoder: + self._decoder.reset() + elif self._decoder or dec_flags or chars_to_skip: self._decoder = self._decoder or self._get_decoder() self._decoder.setstate((b'', dec_flags)) self._snapshot = (dec_flags, b'') diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 58f33a2933a..1c7d1f14ff2 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1706,6 +1706,8 @@ class TextIOWrapperTest(unittest.TestCase): f.write(data) f.seek(0) self.assertEquals(f.read(), data * 2) + f.seek(0) + self.assertEquals(f.read(), data * 2) self.assertEquals(buf.getvalue(), (data * 2).encode(encoding)) def test_read_one_by_one(self):