BZ2File now uses the compresslevel argument given by the caller,

instead of ignoring it and always using a compression level of 9.
This commit is contained in:
Nadeem Vawda 2011-09-11 22:38:11 +02:00
parent c92f6226d7
commit 249ab5e8d1
2 changed files with 9 additions and 2 deletions

View File

@ -75,11 +75,11 @@ class BZ2File(io.BufferedIOBase):
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))

View File

@ -224,6 +224,13 @@ class BZ2FileTest(BaseTest):
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)