Index: Lib/codecs.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/codecs.py,v retrieving revision 1.35 diff -u -r1.35 codecs.py --- Lib/codecs.py 17 Oct 2004 23:51:21 -0000 1.35 +++ Lib/codecs.py 15 Dec 2004 21:23:44 -0000 @@ -256,41 +256,39 @@ definition of the encoding and the given size, e.g. if optional encoding endings or state markers are available on the stream, these should be read too. - """ # read until we get the required number of characters (if available) - done = False while True: # can the request can be satisfied from the character buffer? if chars < 0: if self.charbuffer: - done = True + break else: if len(self.charbuffer) >= chars: - done = True - if done: - if chars < 0: - result = self.charbuffer - self.charbuffer = u"" - break - else: - result = self.charbuffer[:chars] - self.charbuffer = self.charbuffer[chars:] break # we need more data if size < 0: newdata = self.stream.read() else: newdata = self.stream.read(size) + # decode bytes (those remaining from the last call included) data = self.bytebuffer + newdata - object, decodedbytes = self.decode(data, self.errors) + newchars, decodedbytes = self.decode(data, self.errors) # keep undecoded bytes until the next call self.bytebuffer = data[decodedbytes:] # put new characters in the character buffer - self.charbuffer += object + self.charbuffer += newchars # there was no data available if not newdata: - done = True + break + if chars < 0: + # Return everything we've got + result = self.charbuffer + self.charbuffer = u"" + else: + # Return the first chars characters + result = self.charbuffer[:chars] + self.charbuffer = self.charbuffer[chars:] return result def readline(self, size=None, keepends=True): @@ -302,11 +300,10 @@ read() method. """ - if size is None: - size = 10 + readsize = size or 10 line = u"" while True: - data = self.read(size) + data = self.read(readsize) line += data pos = line.find("\n") if pos>=0: @@ -316,10 +313,10 @@ else: line = line[:pos] return line - elif not data: + elif not data or size is not None: return line - if size<8000: - size *= 2 + if readsize<8000: + readsize *= 2 def readlines(self, sizehint=None, keepends=True):