bpo-33361: Fix bug with seeking in StreamRecoders (GH-8278)

(cherry picked from commit a6ec1ce1ac)

Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
This commit is contained in:
Miss Islington (bot) 2019-05-31 13:03:22 -07:00 committed by Berker Peksag
parent 49fc57abf5
commit a6dc5d4e1c
3 changed files with 33 additions and 0 deletions

View File

@ -847,6 +847,12 @@ class StreamRecoder:
self.reader.reset()
self.writer.reset()
def seek(self, offset, whence=0):
# Seeks must be propagated to both the readers and writers
# as they might need to reset their internal buffers.
self.reader.seek(offset, whence)
self.writer.seek(offset, whence)
def __getattr__(self, name,
getattr=getattr):

View File

@ -3318,6 +3318,31 @@ class StreamRecoderTest(unittest.TestCase):
sr.write(text.encode('latin1'))
self.assertEqual(bio.getvalue(), text.encode('utf-8'))
def test_seeking_read(self):
bio = io.BytesIO('line1\nline2\nline3\n'.encode('utf-16-le'))
sr = codecs.EncodedFile(bio, 'utf-8', 'utf-16-le')
self.assertEqual(sr.readline(), b'line1\n')
sr.seek(0)
self.assertEqual(sr.readline(), b'line1\n')
self.assertEqual(sr.readline(), b'line2\n')
self.assertEqual(sr.readline(), b'line3\n')
self.assertEqual(sr.readline(), b'')
def test_seeking_write(self):
bio = io.BytesIO('123456789\n'.encode('utf-16-le'))
sr = codecs.EncodedFile(bio, 'utf-8', 'utf-16-le')
# Test that seek() only resets its internal buffer when offset
# and whence are zero.
sr.seek(2)
sr.write(b'\nabc\n')
self.assertEqual(sr.readline(), b'789\n')
sr.seek(0)
self.assertEqual(sr.readline(), b'1\n')
self.assertEqual(sr.readline(), b'abc\n')
self.assertEqual(sr.readline(), b'789\n')
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,2 @@
Fix a bug in :class:`codecs.StreamRecoder` where seeking might leave old data in a
buffer and break subsequent read calls. Patch by Ammar Askar.