[pypy-svn] r46870 - pypy/dist/pypy/module/zlib

arigo at codespeak.net arigo at codespeak.net
Tue Sep 25 19:57:17 CEST 2007


Author: arigo
Date: Tue Sep 25 19:57:16 2007
New Revision: 46870

Modified:
   pypy/dist/pypy/module/zlib/__init__.py
   pypy/dist/pypy/module/zlib/app_zlib.py
   pypy/dist/pypy/module/zlib/interp_zlib.py
Log:
Move the compress() and decompress() function at interp-level.


Modified: pypy/dist/pypy/module/zlib/__init__.py
==============================================================================
--- pypy/dist/pypy/module/zlib/__init__.py	(original)
+++ pypy/dist/pypy/module/zlib/__init__.py	Tue Sep 25 19:57:16 2007
@@ -12,10 +12,10 @@
         'adler32': 'interp_zlib.adler32',
         'compressobj': 'interp_zlib.Compress',
         'decompressobj': 'interp_zlib.Decompress',
+        'compress': 'interp_zlib.compress',
+        'decompress': 'interp_zlib.decompress',
         }
 
     appleveldefs = {
         'error': 'app_zlib.error',
-        'compress': 'app_zlib.compress',
-        'decompress': 'app_zlib.decompress',
         }

Modified: pypy/dist/pypy/module/zlib/app_zlib.py
==============================================================================
--- pypy/dist/pypy/module/zlib/app_zlib.py	(original)
+++ pypy/dist/pypy/module/zlib/app_zlib.py	Tue Sep 25 19:57:16 2007
@@ -9,29 +9,3 @@
     """
     Raised by zlib operations.
     """
-
-
-# XXX the following should be moved to interp-level to avoid various overheads
-
-def compress(string, *args):
-    """
-    compress(string[, level]) -- Returned compressed string.
-
-    Optional arg level is the compression level, in 1-9.
-    """
-    import zlib
-    compressor = zlib.compressobj(*args)
-    return compressor.compress(string) + compressor.flush()
-
-
-def decompress(string, *args):
-    """
-    decompress(string[, wbits[, bufsize]]) -- Return decompressed string.
-
-    Optional arg wbits is the window buffer size.  Optional arg bufsize is
-    the initial output buffer size.
-    """
-    # XXX bufsize is not accepted by this version but it's basically useless.
-    import zlib
-    decompressor = zlib.decompressobj(*args)
-    return decompressor.decompress(string) + decompressor.flush()

Modified: pypy/dist/pypy/module/zlib/interp_zlib.py
==============================================================================
--- pypy/dist/pypy/module/zlib/interp_zlib.py	(original)
+++ pypy/dist/pypy/module/zlib/interp_zlib.py	Tue Sep 25 19:57:16 2007
@@ -44,12 +44,49 @@
 adler32.unwrap_spec = [ObjSpace, str, int]
 
 
-def error_from_zlib(space, status):
-    if status == Z_STREAM_ERROR:
-        return OperationError(
-            space.w_ValueError,
-            space.wrap("Invalid initialization option"))
-    assert False, "unhandled status %s" % (status,)
+def compress(space, string, level=rzlib.Z_DEFAULT_COMPRESSION):
+    """
+    compress(string[, level]) -- Returned compressed string.
+
+    Optional arg level is the compression level, in 1-9.
+    """
+    try:
+        try:
+            stream = rzlib.deflateInit(level)
+        except ValueError:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("Invalid initialization option"))
+        try:
+            result = rzlib.compress(stream, string, rzlib.Z_FINISH)
+        finally:
+            rzlib.deflateEnd(stream)
+    except rzlib.RZlibError, e:
+        raise zlib_error(self.space, e.msg)
+    return space.wrap(result)
+compress.unwrap_spec = [ObjSpace, str, int]
+
+
+def decompress(space, string, wbits=rzlib.MAX_WBITS, bufsize=0):
+    """
+    decompress(string[, wbits[, bufsize]]) -- Return decompressed string.
+
+    Optional arg wbits is the window buffer size.  Optional arg bufsize is
+    the initial output buffer size.
+    """
+    try:
+        try:
+            stream = rzlib.inflateInit(wbits)
+        except ValueError:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("Invalid initialization option"))
+        try:
+            result = rzlib.decompress(stream, string, rzlib.Z_FINISH)
+        finally:
+            rzlib.inflateEnd(stream)
+    except rzlib.RZlibError, e:
+        raise zlib_error(self.space, e.msg)
+    return space.wrap(result)
+decompress.unwrap_spec = [ObjSpace, str, int, int]
 
 
 class Compress(Wrappable):
@@ -155,6 +192,9 @@
             self.stream = rzlib.inflateInit(wbits)
         except rzlib.RZlibError, e:
             raise zlib_error(self.space, e.msg)
+        except ValueError:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("Invalid initialization option"))
 
     def __del__(self):
         """Automatically free the resources used by the stream."""



More information about the Pypy-commit mailing list