[Python-checkins] CVS: python/dist/src/Lib gzip.py,1.24,1.25

Martin v. L?wis loewis@users.sourceforge.net
Thu, 09 Aug 2001 00:21:58 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv912/Lib

Modified Files:
	gzip.py 
Log Message:
Patch #448474: Add support for tell() and seek() to gzip.GzipFile.


Index: gzip.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/gzip.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** gzip.py	2001/03/20 15:51:14	1.24
--- gzip.py	2001/08/09 07:21:56	1.25
***************
*** 65,68 ****
--- 65,69 ----
  
          self.fileobj = fileobj
+         self.offset = 0
  
          if self.mode == WRITE:
***************
*** 139,142 ****
--- 140,144 ----
              self.crc = zlib.crc32(data, self.crc)
              self.fileobj.write( self.compress.compress(data) )
+             self.offset += len(data)
  
      def writelines(self,lines):
***************
*** 168,171 ****
--- 170,174 ----
          self.extrasize = self.extrasize - size
  
+         self.offset += size
          return chunk
  
***************
*** 173,176 ****
--- 176,180 ----
          self.extrabuf = buf + self.extrabuf
          self.extrasize = len(buf) + self.extrasize
+         self.offset -= len(buf)
  
      def _read(self, size=1024):
***************
*** 186,190 ****
              self.fileobj.seek(0, 2)     # Seek to end of file
              if pos == self.fileobj.tell():
-                 self.fileobj = None
                  raise EOFError, "Reached EOF"
              else:
--- 190,193 ----
***************
*** 205,209 ****
              uncompress = self.decompress.flush()
              self._read_eof()
-             self.fileobj = None
              self._add_read_data( uncompress )
              raise EOFError, 'Reached EOF'
--- 208,211 ----
***************
*** 270,273 ****
--- 272,305 ----
      def isatty(self):
          return 0
+ 
+     def tell(self):
+         return self.offset
+ 
+     def rewind(self):
+         '''Return the uncompressed stream file position indicator to the
+         beginning of the file''' 
+         if self.mode != READ:
+             raise IOError("Can't rewind in write mode")
+         self.fileobj.seek(0)
+         self._new_member = 1
+         self.extrabuf = ""
+         self.extrasize = 0
+         self.offset = 0
+ 
+     def seek(self, offset):
+         if self.mode == WRITE:
+             if offset < self.offset:
+                 raise IOError('Negative seek in write mode')
+             count = offset - self.offset
+             for i in range(count/1024): 
+                 f.write(1024*'\0')
+             self.write((count%1024)*'\0')
+         elif self.mode == READ:
+             if offset < self.offset:
+                 # for negative seek, rewind and do positive seek
+                 self.rewind()
+             count = offset - self.offset
+             for i in range(count/1024): self.read(1024)
+             self.read(count % 1024)
  
      def readline(self, size=-1):