Author: Carl Friedrich Bolz <cfb...@gmx.de> Branch: Changeset: r48740:ff9f3efc4c62 Date: 2011-11-04 10:47 +0100 http://bitbucket.org/pypy/pypy/changeset/ff9f3efc4c62/
Log: merge diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py --- a/pypy/module/_file/interp_file.py +++ b/pypy/module/_file/interp_file.py @@ -206,24 +206,28 @@ @unwrap_spec(size=int) def direct_readlines(self, size=0): stream = self.getstream() - # NB. this implementation is very inefficient for unbuffered - # streams, but ok if stream.readline() is efficient. + # this is implemented as: .read().split('\n') + # except that it keeps the \n in the resulting strings if size <= 0: - result = [] - while True: - line = stream.readline() - if not line: - break - result.append(line) - size -= len(line) + data = stream.readall() else: - result = [] - while size > 0: - line = stream.readline() - if not line: - break - result.append(line) - size -= len(line) + data = stream.read(size) + result = [] + splitfrom = 0 + for i in range(len(data)): + if data[i] == '\n': + result.append(data[splitfrom : i + 1]) + splitfrom = i + 1 + # + if splitfrom < len(data): + # there is a partial line at the end. If size > 0, it is likely + # to be because the 'read(size)' returned data up to the middle + # of a line. In that case, use 'readline()' to read until the + # end of the current line. + data = data[splitfrom:] + if size > 0: + data += stream.readline() + result.append(data) return result @unwrap_spec(offset=r_longlong, whence=int) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit