[Python-checkins] cpython: BZ2File now uses the compresslevel argument given by the caller,

nadeem.vawda python-checkins at python.org
Sun Sep 11 22:38:35 CEST 2011


http://hg.python.org/cpython/rev/d3ff5109f5fd
changeset:   72340:d3ff5109f5fd
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sun Sep 11 22:38:11 2011 +0200
summary:
  BZ2File now uses the compresslevel argument given by the caller,
instead of ignoring it and always using a compression level of 9.

files:
  Lib/bz2.py           |  4 ++--
  Lib/test/test_bz2.py |  7 +++++++
  2 files changed, 9 insertions(+), 2 deletions(-)


diff --git a/Lib/bz2.py b/Lib/bz2.py
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -75,11 +75,11 @@
         elif mode in ("w", "wb"):
             mode = "wb"
             mode_code = _MODE_WRITE
-            self._compressor = BZ2Compressor()
+            self._compressor = BZ2Compressor(compresslevel)
         elif mode in ("a", "ab"):
             mode = "ab"
             mode_code = _MODE_WRITE
-            self._compressor = BZ2Compressor()
+            self._compressor = BZ2Compressor(compresslevel)
         else:
             raise ValueError("Invalid mode: {!r}".format(mode))
 
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -224,6 +224,13 @@
         with open(self.filename, 'rb') as f:
             self.assertEqual(self.decompress(f.read()), self.TEXT)
 
+    def testWriteNonDefaultCompressLevel(self):
+        expected = bz2.compress(self.TEXT, compresslevel=5)
+        with BZ2File(self.filename, "w", compresslevel=5) as bz2f:
+            bz2f.write(self.TEXT)
+        with open(self.filename, "rb") as f:
+            self.assertEqual(f.read(), expected)
+
     def testWriteLines(self):
         with BZ2File(self.filename, "w") as bz2f:
             self.assertRaises(TypeError, bz2f.writelines)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list