Kaydet (Commit) 249ab5e8 authored tarafından Nadeem Vawda's avatar Nadeem Vawda

BZ2File now uses the compresslevel argument given by the caller,

instead of ignoring it and always using a compression level of 9.
üst c92f6226
...@@ -75,11 +75,11 @@ class BZ2File(io.BufferedIOBase): ...@@ -75,11 +75,11 @@ class BZ2File(io.BufferedIOBase):
elif mode in ("w", "wb"): elif mode in ("w", "wb"):
mode = "wb" mode = "wb"
mode_code = _MODE_WRITE mode_code = _MODE_WRITE
self._compressor = BZ2Compressor() self._compressor = BZ2Compressor(compresslevel)
elif mode in ("a", "ab"): elif mode in ("a", "ab"):
mode = "ab" mode = "ab"
mode_code = _MODE_WRITE mode_code = _MODE_WRITE
self._compressor = BZ2Compressor() self._compressor = BZ2Compressor(compresslevel)
else: else:
raise ValueError("Invalid mode: {!r}".format(mode)) raise ValueError("Invalid mode: {!r}".format(mode))
......
...@@ -224,6 +224,13 @@ class BZ2FileTest(BaseTest): ...@@ -224,6 +224,13 @@ class BZ2FileTest(BaseTest):
with open(self.filename, 'rb') as f: with open(self.filename, 'rb') as f:
self.assertEqual(self.decompress(f.read()), self.TEXT) 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): def testWriteLines(self):
with BZ2File(self.filename, "w") as bz2f: with BZ2File(self.filename, "w") as bz2f:
self.assertRaises(TypeError, bz2f.writelines) self.assertRaises(TypeError, bz2f.writelines)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment