Kaydet (Commit) 96aeafbf authored tarafından Walter Dörwald's avatar Walter Dörwald

Backport checkin:

Reset internal buffers when seek() is called. This fixes SF bug #1156259.
üst 616ac231
...@@ -356,7 +356,17 @@ class StreamReader(Codec): ...@@ -356,7 +356,17 @@ class StreamReader(Codec):
from decoding errors. 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): def next(self):
......
...@@ -31,6 +31,13 @@ class StreamWriter(codecs.StreamWriter): ...@@ -31,6 +31,13 @@ class StreamWriter(codecs.StreamWriter):
class StreamReader(codecs.StreamReader): class StreamReader(codecs.StreamReader):
def reset(self):
codecs.StreamReader.reset(self)
try:
del self.decode
except AttributeError:
pass
def decode(self, input, errors='strict'): def decode(self, input, errors='strict'):
(object, consumed, byteorder) = \ (object, consumed, byteorder) = \
codecs.utf_16_ex_decode(input, errors, 0, False) codecs.utf_16_ex_decode(input, errors, 0, False)
......
...@@ -24,6 +24,17 @@ class Queue(object): ...@@ -24,6 +24,17 @@ class Queue(object):
return s return s
class ReadTest(unittest.TestCase): 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): def check_partial(self, input, partialresults):
# get a StreamReader for the encoding and feed the bytestring version # get a StreamReader for the encoding and feed the bytestring version
# of input to the reader byte by byte. Read every available from # of input to the reader byte by byte. Read every available from
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment