]> granicus.if.org Git - python/commitdiff
Issue #3488: Provide convenient shorthand functions `gzip.compress`
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Aug 2010 21:10:05 +0000 (21:10 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Aug 2010 21:10:05 +0000 (21:10 +0000)
and `gzip.decompress`.  Original patch by Anand B. Pillai.

Doc/library/gzip.rst
Lib/gzip.py
Lib/test/test_gzip.py
Misc/ACKS
Misc/NEWS

index b99ac740fd535ede89f1f2a0e6e6011ead95ea55..0401cd8211608dde8d754531a219f27190d0c033 100644 (file)
@@ -82,6 +82,17 @@ The module defines the following items:
    The *filename* argument is required; *mode* defaults to ``'rb'`` and
    *compresslevel* defaults to ``9``.
 
+.. function:: compress(data, compresslevel=9)
+
+   Compress the *data*, returning a :class:`bytes` object containing
+   the compressed data.  *compresslevel* has the same meaning as in
+   the :class:`GzipFile` constructor above.
+
+.. function:: decompress(data)
+
+   Decompress the *data*, returning a :class:`bytes` object containing the
+   uncompressed data.
+
 
 .. _gzip-usage-examples:
 
@@ -112,6 +123,11 @@ Example of how to GZIP compress an existing file::
    f_out.close()
    f_in.close()
 
+Example of how to GZIP compress a binary string::
+
+   import gzip
+   s_in = b"Lots of content here"
+   s_out = gzip.compress(s_in)
 
 .. seealso::
 
index fab55a3015d0d793c28d5e016e0f9edf7dd865c0..83311cc0deb800a76f44a82dd07c83a91fcba11b 100644 (file)
@@ -10,7 +10,7 @@ import zlib
 import builtins
 import io
 
-__all__ = ["GzipFile","open"]
+__all__ = ["GzipFile", "open", "compress", "decompress"]
 
 FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
 
@@ -476,6 +476,23 @@ class GzipFile(io.BufferedIOBase):
         return b''.join(bufs) # Return resulting line
 
 
+def compress(data, compresslevel=9):
+    """Compress data in one shot and return the compressed string.
+    Optional argument is the compression level, in range of 1-9.
+    """
+    buf = io.BytesIO()
+    with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f:
+        f.write(data)
+    return buf.getvalue()
+
+def decompress(data):
+    """Decompress a gzip compressed string in one shot.
+    Return the decompressed string.
+    """
+    with GzipFile(fileobj=io.BytesIO(data)) as f:
+        return f.read()
+
+
 def _test():
     # Act like gzip; with -d, act like gunzip.
     # The input file is not deleted, however, nor are any other gzip
index 7eade6f60a68cd4959648d00f86b3e368b8c723e..a95af058a39cf7531da58daa7b7c751f8ded7835 100644 (file)
@@ -265,6 +265,26 @@ class TestGzip(unittest.TestCase):
             d = f.read()
             self.assertEqual(d, data1 * 50, "Incorrect data in file")
 
+    # Testing compress/decompress shortcut functions
+
+    def test_compress(self):
+        for data in [data1, data2]:
+            for args in [(), (1,), (6,), (9,)]:
+                datac = gzip.compress(data, *args)
+                self.assertEqual(type(datac), bytes)
+                with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
+                    self.assertEqual(f.read(), data)
+
+    def test_decompress(self):
+        for data in (data1, data2):
+            buf = io.BytesIO()
+            with gzip.GzipFile(fileobj=buf, mode="wb") as f:
+                f.write(data)
+            self.assertEqual(gzip.decompress(buf.getvalue()), data)
+            # Roundtrip with compress
+            datac = gzip.compress(data)
+            self.assertEqual(gzip.decompress(datac), data)
+
 def test_main(verbose=None):
     support.run_unittest(TestGzip)
 
index 26feba34e9dcf0e89ef5ab91320e79036b48b924..45cd1b70cdbb2fa49cfccda04c45233b0890b4bd 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -634,6 +634,7 @@ Neale Pickett
 Jim St. Pierre
 Dan Pierson
 Martijn Pieters
+Anand B. Pillai
 François Pinard
 Zach Pincus
 Michael Piotrowski
index 42a04f7c59f203c846f2077d40a7369a52ae581b..4e11922867849c77a92a9db41c7295ff07d195da 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -95,6 +95,9 @@ Extensions
 Library
 -------
 
+- Issue #3488: Provide convenient shorthand functions ``gzip.compress``
+  and ``gzip.decompress``.  Original patch by Anand B. Pillai.
+
 - Issue #8807: poplib.POP3_SSL class now accepts a context parameter, which is a
   ssl.SSLContext object allowing bundling SSL configuration options,
   certificates and private keys into a single (potentially long-lived)