diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-05-06 13:04:01 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-05-06 13:04:01 (GMT) |
commit | 7e126205e615518bc921132918c0ed7f48b31c85 (patch) | |
tree | 02d4ca50befae0f0f08494be89c519c12dc51a35 /Lib/test | |
parent | 54c74ece28d707176fa375ab65f5be229fc4db0a (diff) | |
download | cpython-7e126205e615518bc921132918c0ed7f48b31c85.zip cpython-7e126205e615518bc921132918c0ed7f48b31c85.tar.gz cpython-7e126205e615518bc921132918c0ed7f48b31c85.tar.bz2 |
Closes #13989: Add support for text modes to gzip.open().
Also, add tests for gzip.open().
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_gzip.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index d2b4871..db1312d 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -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) |