|
|
|
@ -374,6 +374,94 @@ class TestGzip(unittest.TestCase): |
|
|
|
datac = gzip.compress(data) |
|
|
|
self.assertEqual(gzip.decompress(datac), data) |
|
|
|
|
|
|
|
# Test the 'open' convenience function. |
|
|
|
|
|
|
|
def test_open_binary(self): |
|
|
|
# Test explicit binary modes. |
|
|
|
uncompressed = data1 * 50 |
|
|
|
with gzip.open(self.filename, "wb") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()) |
|
|
|
self.assertEqual(file_data, uncompressed) |
|
|
|
with gzip.open(self.filename, "rb") as f: |
|
|
|
self.assertEqual(f.read(), uncompressed) |
|
|
|
with gzip.open(self.filename, "ab") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()) |
|
|
|
self.assertEqual(file_data, uncompressed * 2) |
|
|
|
|
|
|
|
def test_open_default_binary(self): |
|
|
|
# Test implicit binary modes (no "b" or "t" in mode string). |
|
|
|
uncompressed = data1 * 50 |
|
|
|
with gzip.open(self.filename, "w") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()) |
|
|
|
self.assertEqual(file_data, uncompressed) |
|
|
|
with gzip.open(self.filename, "r") as f: |
|
|
|
self.assertEqual(f.read(), uncompressed) |
|
|
|
with gzip.open(self.filename, "a") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()) |
|
|
|
self.assertEqual(file_data, uncompressed * 2) |
|
|
|
|
|
|
|
def test_open_text(self): |
|
|
|
# Test text modes. |
|
|
|
uncompressed = data1.decode("ascii") * 50 |
|
|
|
with gzip.open(self.filename, "wt") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()).decode("ascii") |
|
|
|
self.assertEqual(file_data, uncompressed) |
|
|
|
with gzip.open(self.filename, "rt") as f: |
|
|
|
self.assertEqual(f.read(), uncompressed) |
|
|
|
with gzip.open(self.filename, "at") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()).decode("ascii") |
|
|
|
self.assertEqual(file_data, uncompressed * 2) |
|
|
|
|
|
|
|
def test_open_bad_params(self): |
|
|
|
# Test invalid parameter combinations. |
|
|
|
with self.assertRaises(ValueError): |
|
|
|
gzip.open(self.filename, "wbt") |
|
|
|
with self.assertRaises(ValueError): |
|
|
|
gzip.open(self.filename, "rb", encoding="utf-8") |
|
|
|
with self.assertRaises(ValueError): |
|
|
|
gzip.open(self.filename, "rb", errors="ignore") |
|
|
|
with self.assertRaises(ValueError): |
|
|
|
gzip.open(self.filename, "rb", newline="\n") |
|
|
|
|
|
|
|
def test_open_with_encoding(self): |
|
|
|
# Test non-default encoding. |
|
|
|
uncompressed = data1.decode("ascii") * 50 |
|
|
|
with gzip.open(self.filename, "wt", encoding="utf-16") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with open(self.filename, "rb") as f: |
|
|
|
file_data = gzip.decompress(f.read()).decode("utf-16") |
|
|
|
self.assertEqual(file_data, uncompressed) |
|
|
|
with gzip.open(self.filename, "rt", encoding="utf-16") as f: |
|
|
|
self.assertEqual(f.read(), uncompressed) |
|
|
|
|
|
|
|
def test_open_with_encoding_error_handler(self): |
|
|
|
# Test with non-default encoding error handler. |
|
|
|
with gzip.open(self.filename, "wb") as f: |
|
|
|
f.write(b"foo\xffbar") |
|
|
|
with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \ |
|
|
|
as f: |
|
|
|
self.assertEqual(f.read(), "foobar") |
|
|
|
|
|
|
|
def test_open_with_newline(self): |
|
|
|
# Test with explicit newline (universal newline mode disabled). |
|
|
|
uncompressed = data1.decode("ascii") * 50 |
|
|
|
with gzip.open(self.filename, "wt") as f: |
|
|
|
f.write(uncompressed) |
|
|
|
with gzip.open(self.filename, "rt", newline="\r") as f: |
|
|
|
self.assertEqual(f.readlines(), [uncompressed]) |
|
|
|
|
|
|
|
def test_main(verbose=None): |
|
|
|
support.run_unittest(TestGzip) |
|
|
|
|
|
|
|
|