[pypy-svn] r49593 - pypy/branch/pypy-interp-file/module/bz2

arigo at codespeak.net arigo at codespeak.net
Mon Dec 10 13:17:45 CET 2007


Author: arigo
Date: Mon Dec 10 13:17:45 2007
New Revision: 49593

Modified:
   pypy/branch/pypy-interp-file/module/bz2/interp_bz2.py
Log:
The ReadBZ2Filter acts like a read buffer itself, so we don't need
to layer another one on top of it - at least if the peek() method
is present.


Modified: pypy/branch/pypy-interp-file/module/bz2/interp_bz2.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/bz2/interp_bz2.py	(original)
+++ pypy/branch/pypy-interp-file/module/bz2/interp_bz2.py	Mon Dec 10 13:17:45 2007
@@ -250,7 +250,9 @@
                              space.wrap("cannot append to bz2 file"))
     stream = open_path_helper(path, os_flags, False)
     if reading:
-        bz2stream = ReadBZ2Filter(space, stream, compresslevel)
+        bz2stream = ReadBZ2Filter(space, stream, buffering)
+        buffering = 0     # by construction, the ReadBZ2Filter acts like
+                          # a read buffer too - no need for another one
     else:
         assert writing
         bz2stream = WriteBZ2Filter(space, stream, compresslevel)
@@ -263,13 +265,16 @@
 
     """Standard I/O stream filter that decompresses the stream with bz2."""
 
-    def __init__(self, space, stream, compresslevel):
+    def __init__(self, space, stream, buffering):
         self.space = space
         self.stream = stream
         self.decompressor = W_BZ2Decompressor(space)
         self.readlength = 0
         self.buffer = ""
         self.finished = False
+        if buffering < 1024:
+            buffering = 1024   # minimum amount of compressed data read at once
+        self.buffering = buffering
 
     def close(self):
         self.stream.close()
@@ -320,7 +325,7 @@
         while not self.buffer:
             if self.finished:
                 return ""
-            moredata = self.stream.read(n)
+            moredata = self.stream.read(max(self.buffering, n))
             if not moredata:
                 self.finished = True
                 return ""
@@ -341,6 +346,9 @@
         self.readlength += len(result)
         return result
 
+    def peek(self):
+        return self.buffer
+
     def try_to_find_file_descriptor(self):
         return self.stream.try_to_find_file_descriptor()
 



More information about the Pypy-commit mailing list