summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2005-03-14 19:20:19 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2005-03-14 19:20:19 (GMT)
commit96aeafbfbcd1fe6a6f5703d030560a133f7baf1f (patch)
treea0573238559b858828d58bc21523c0ac59b0f352 /Lib
parent616ac231786bc60b0273a86712fb9250ca635c59 (diff)
downloadcpython-96aeafbfbcd1fe6a6f5703d030560a133f7baf1f.zip
cpython-96aeafbfbcd1fe6a6f5703d030560a133f7baf1f.tar.gz
cpython-96aeafbfbcd1fe6a6f5703d030560a133f7baf1f.tar.bz2
Backport checkin:
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/codecs.py12
-rw-r--r--Lib/encodings/utf_16.py7
-rw-r--r--Lib/test/test_codecs.py11
3 files changed, 29 insertions, 1 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py
index b4103fb..092da0c 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 a33581c..95abb05 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 da6891f..c8d99fe 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -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