[Python-checkins] CVS: python/dist/src/Lib gzip.py,1.18,1.19
A.M. Kuchling
python-dev@python.org
2000年7月29日 13:15:28 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv28353
Modified Files:
gzip.py
Log Message:
SF patch #100740: Add optional size arguments to .readline() and
.readlines() methods. Inspired by a patch from Wolfgang Grafen,
though this version of the patch was completely rewritten from his
code.
Index: gzip.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/gzip.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** gzip.py 2000年05月08日 16:59:59 1.18
--- gzip.py 2000年07月29日 20:15:26 1.19
***************
*** 6,13 ****
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
! import time
! import string
import zlib
- import struct
import __builtin__
--- 6,11 ----
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
! import string, struct, sys, time
import zlib
import __builtin__
***************
*** 176,180 ****
if self._new_member:
! # If the _new_member flag is set, we have to
#
# First, check if we're at the end of the file;
--- 174,179 ----
if self._new_member:
! # If the _new_member flag is set, we have to
! # jump to the next member, if there is one.
#
# First, check if we're at the end of the file;
***************
*** 274,298 ****
return 0
! def readline(self):
bufs = []
! readsize = 100
while 1:
c = self.read(readsize)
i = string.find(c, '\n')
if i >= 0 or c == '':
! bufs.append(c[:i+1])
! self._unread(c[i+1:])
! return string.join(bufs, '')
bufs.append(c)
! readsize = readsize * 2
! def readlines(self, ignored=None):
! buf = self.read()
! lines = string.split(buf, '\n')
! for i in range(len(lines)-1):
! lines[i] = lines[i] + '\n'
! if lines and not lines[-1]:
! del lines[-1]
! return lines
def writelines(self, L):
--- 273,316 ----
return 0
! def readline(self, size=-1):
! if size < 0: size = sys.maxint
bufs = []
! orig_size = size
! readsize = min(100, size) # Read from the file in small chunks
while 1:
+ if size == 0:
+ return string.join(bufs, '') # Return resulting line
+
c = self.read(readsize)
i = string.find(c, '\n')
+ if size is not None:
+ # We set i=size to break out of the loop under two
+ # conditions: 1) there's no newline, and the chunk is
+ # larger than size, or 2) there is a newline, but the
+ # resulting line would be longer than 'size'.
+ if i==-1 and len(c) > size: i=size-1
+ elif size <= i: i = size -1
+
if i >= 0 or c == '':
! bufs.append(c[:i+1]) # Add portion of last chunk
! self._unread(c[i+1:]) # Push back rest of chunk
! return string.join(bufs, '') # Return resulting line
!
! # Append chunk to list, decrease 'size',
bufs.append(c)
! size = size - len(c)
! readsize = min(size, readsize * 2)
!
! def readlines(self, sizehint=0):
! # Negative numbers result in reading all the lines
! if sizehint <= 0: sizehint = sys.maxint
! L = []
! while sizehint > 0:
! line = self.readline()
! if line == "": break
! L.append( line )
! sizehint = sizehint - len(line)
! return L
def writelines(self, L):