summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-01-27 19:29:45 (GMT)
committerGuido van Rossum <guido@python.org>1998-01-27 19:29:45 (GMT)
commitb16a3b84509f6f66f60bb3c7521b2bee70ac1b1c (patch)
tree4db1fab31922818ce355b65b01cdbd80a42e1d35 /Lib/gzip.py
parent7570669a0838e5b1ae868774f35105107fa1992b (diff)
downloadcpython-b16a3b84509f6f66f60bb3c7521b2bee70ac1b1c.zip
cpython-b16a3b84509f6f66f60bb3c7521b2bee70ac1b1c.tar.gz
cpython-b16a3b84509f6f66f60bb3c7521b2bee70ac1b1c.tar.bz2
(This fix is really by Jeremy)
Here's my suggested replacement for gzip.py for 1.5.1. I've re-implemeted methods readline and readlines, added an _unread, and tweaked read and _read. I tried a more complicated buffer scheme for unread (using a list of strings and string.join), but it was more complicated and slower. This version is a lot faster than the current version and is still pretty simple.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r--Lib/gzip.py51
1 files changed, 29 insertions, 22 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 26d32aa..3d656b9 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -80,7 +80,6 @@ class GzipFile:
self._write_gzip_header()
elif self.mode == READ:
self._read_gzip_header()
-
def __repr__(self):
s = repr(self.fileobj)
@@ -161,19 +160,20 @@ class GzipFile:
def read(self,size=None):
if self.extrasize <= 0 and self.fileobj is None:
return ''
-
- if not size:
- # get the whole thing
+
+ readsize = 1024
+ if not size: # get the whole thing
try:
while 1:
- self._read()
+ self._read(readsize)
+ readsize = readsize * 2
except EOFError:
size = self.extrasize
- else:
- # just get some more of it
+ else: # just get some more of it
try:
while size > self.extrasize:
- self._read()
+ self._read(readsize)
+ readsize = readsize * 2
except EOFError:
pass
@@ -183,8 +183,15 @@ class GzipFile:
return chunk
- def _read(self):
- buf = self.fileobj.read(1024)
+ def _unread(self, buf):
+ self.extrabuf = buf + self.extrabuf
+ self.extrasize = len(buf) + self.extrasize
+
+ def _read(self, size=1024):
+ try:
+ buf = self.fileobj.read(size)
+ except AttributeError:
+ raise EOFError, "Reached EOF"
if buf == "":
uncompress = self.decompress.flush()
if uncompress == "":
@@ -237,21 +244,21 @@ class GzipFile:
return 0
def readline(self):
- # XXX This function isn't implemented in a very efficient way
- line=""
+ bufs = []
+ readsize = 100
while 1:
- c = self.read(1)
- line = line + c
- if c=='\n' or c=="": break
- return line
+ c = self.read(readsize)
+ i = string.find(c, '\n')
+ if i >= 0 or c == '':
+ bufs.append(c[:i])
+ self._unread(c[i+1:])
+ return string.join(bufs, '')
+ bufs.append(c)
+ readsize = readsize * 2
def readlines(self):
- L=[]
- line = self.readline()
- while line!="":
- L.append(line)
- line = self.readline()
- return L
+ buf = self.read()
+ return string.split(buf, '\n')
def writelines(self, L):
for line in L: