[Bug #925107] Make .readline() consider self.stop. This makes read() and readline() very similar, so they're refactored into _read. Patch by Johannes Gijsbers.

2.3 bugfix candidate.
This commit is contained in:
Andrew M. Kuchling 2004-07-07 14:09:21 +00:00
parent 5a8b4593d3
commit 1263bd8b6c
1 changed files with 8 additions and 13 deletions

View File

@ -43,28 +43,23 @@ class _Subfile:
self.stop = stop self.stop = stop
self.pos = self.start self.pos = self.start
def read(self, length = None):
def _read(self, length, read_function):
if self.pos >= self.stop: if self.pos >= self.stop:
return '' return ''
remaining = self.stop - self.pos remaining = self.stop - self.pos
if length is None or length < 0: if length is None or length < 0 or length > remaining:
length = remaining
elif length > remaining:
length = remaining length = remaining
self.fp.seek(self.pos) self.fp.seek(self.pos)
data = self.fp.read(length) data = read_function(length)
self.pos = self.fp.tell() self.pos = self.fp.tell()
return data return data
def read(self, length = None):
self._read(length, self.fp.read)
def readline(self, length = None): def readline(self, length = None):
if self.pos >= self.stop: self._read(length, self.fp.readline)
return ''
if length is None:
length = self.stop - self.pos
self.fp.seek(self.pos)
data = self.fp.readline(length)
self.pos = self.fp.tell()
return data
def readlines(self, sizehint = -1): def readlines(self, sizehint = -1):
lines = [] lines = []