]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport checkin:
authorWalter Dörwald <walter@livinglogic.de>
Mon, 14 Mar 2005 19:20:19 +0000 (19:20 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Mon, 14 Mar 2005 19:20:19 +0000 (19:20 +0000)
Reset internal buffers when seek() is called. This fixes SF bug #1156259.

Lib/codecs.py
Lib/encodings/utf_16.py
Lib/test/test_codecs.py

index b4103fb6e465ccbccd1b000a74ae1c71b23cbe89..092da0c7d7598c3704d723c0878d71482574d75b 100644 (file)
@@ -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):
 
index a33581c58be24de4f8401bb14e64853374a29ba2..95abb056248af8ba1a90cc6232d42d6af24735c7 100644 (file)
@@ -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)
index da6891f85d8ba3788eaff6cb6ac266e6adf09e01..c8d99fea79e685ca0a3067c8d49f5c3311eae540 100644 (file)
@@ -24,6 +24,17 @@ class Queue(object):
             return s
 
 class ReadTest(unittest.TestCase):
+    def test_seek(self):
+        # all codecs should be able to encode these
+        s = u"%s\n%s\n" % (100*u"abc123", 100*u"def456")
+        encoding = self.encoding
+        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)
+
     def check_partial(self, input, partialresults):
         # get a StreamReader for the encoding and feed the bytestring version
         # of input to the reader byte by byte. Read every available from