Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r44617:1dc4d44ed272 Date: 2011-06-01 11:11 +0200 http://bitbucket.org/pypy/pypy/changeset/1dc4d44ed272/
Log: (gontran) Patch 734: bz2 seek rewinds unnecessarily. Change to make sure that the variable 'read' in a long long. diff --git a/pypy/module/bz2/interp_bz2.py b/pypy/module/bz2/interp_bz2.py --- a/pypy/module/bz2/interp_bz2.py +++ b/pypy/module/bz2/interp_bz2.py @@ -363,42 +363,46 @@ def seek(self, offset, whence): READMAX = 2**18 # 256KB - if whence == 1: - if offset >= 0: - read = r_longlong(0) - while read < offset: - count = offset - read - if count < READMAX: - count = intmask(count) - else: - count = READMAX - read += len(self.read(count)) - else: - pos = self.readlength + offset - self.seek(pos, 0) + + # Make offset relative to the start of the file + if whence == 2: + # Read everything to arrive at the end + while len(self.read(READMAX)) > 0: + pass + offset += self.readlength + elif whence == 1: + offset += self.readlength elif whence == 0: + pass + else: + raise operationerrfmt(space.w_ValueError, + "Invalid value for whence: %d", whence) + + # Make offset relative to the current pos + # Rewind iff necessary + if offset < self.readlength: self.stream.seek(0, 0) self.decompressor = W_BZ2Decompressor(self.space) self.readlength = r_longlong(0) self.buffer = "" self.finished = False - read = 0 - while read < offset: - count = offset - read - if count < READMAX: - count = intmask(count) - else: - count = READMAX - length = len(self.read(count)) - read += length - if not length: - break else: - # first measure the length by reading everything left - while len(self.read(READMAX)) > 0: - pass - pos = self.readlength + offset - self.seek(pos, 0) + offset -= self.readlength + + # Seek + read = r_longlong(0) + while read < offset: + count = offset - read + if count < READMAX: + count = intmask(count) + else: + count = READMAX + length = len(self.read(count)) + if not length: + break + read += length + + return self.readlength def readall(self): w_result = self.decompressor.decompress(self.stream.readall()) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit