From: Walter Dörwald Date: Mon, 14 Mar 2005 19:06:30 +0000 (+0000) Subject: Reset internal buffers when seek() is called. This fixes SF bug #1156259. X-Git-Tag: v2.5a0~1923 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=729c31f5c3e8dcbff571e066ab253db272e490a5;p=thirdparty%2FPython%2Fcpython.git Reset internal buffers when seek() is called. This fixes SF bug #1156259. --- diff --git a/Lib/codecs.py b/Lib/codecs.py index b4103fb6e465..092da0c7d759 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -356,7 +356,17 @@ class StreamReader(Codec): from decoding errors. """ - pass + self.bytebuffer = "" + self.charbuffer = u"" + self.atcr = False + + def seek(self, offset, whence): + """ Set the input stream's current position. + + Resets the codec buffers used for keeping state. + """ + self.reset() + self.stream.seek(offset, whence) def next(self): diff --git a/Lib/encodings/utf_16.py b/Lib/encodings/utf_16.py index a33581c58be2..95abb056248a 100644 --- a/Lib/encodings/utf_16.py +++ b/Lib/encodings/utf_16.py @@ -31,6 +31,13 @@ class StreamWriter(codecs.StreamWriter): class StreamReader(codecs.StreamReader): + def reset(self): + codecs.StreamReader.reset(self) + try: + del self.decode + except AttributeError: + pass + def decode(self, input, errors='strict'): (object, consumed, byteorder) = \ codecs.utf_16_ex_decode(input, errors, 0, False) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 01d8955f442f..e6dba3454950 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -755,6 +755,21 @@ class BasicUnicodeTest(unittest.TestCase): decodedresult += reader.read() self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) + def test_seek(self): + # all codecs should be able to encode these + s = u"%s\n%s\n" % (100*u"abc123", 100*u"def456") + for encoding in all_unicode_encodings: + if encoding == "idna": # FIXME: See SF bug #1163178 + continue + if encoding in broken_unicode_with_streams: + continue + reader = codecs.getreader(encoding)(StringIO.StringIO(s.encode(encoding))) + for t in xrange(5): + # Test that calling seek resets the internal codec state and buffers + reader.seek(0, 0) + line = reader.readline() + self.assertEqual(s[:len(line)], line) + class BasicStrTest(unittest.TestCase): def test_basics(self): s = "abc123"