diff options
Diffstat (limited to 'Lib/test/test_gzip.py')
-rw-r--r--[-rwxr-xr-x] | Lib/test/test_gzip.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 5eac9217b22..aeafc6536d7 100755..100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -131,6 +131,14 @@ class TestGzip(BaseTest): if not ztxt: break self.assertEqual(contents, b'a'*201) + def test_exclusive_write(self): + with gzip.GzipFile(self.filename, 'xb') as f: + f.write(data1 * 50) + with gzip.GzipFile(self.filename, 'rb') as f: + self.assertEqual(f.read(), data1 * 50) + with self.assertRaises(FileExistsError): + gzip.GzipFile(self.filename, 'xb') + def test_buffered_reader(self): # Issue #7471: a GzipFile can be wrapped in a BufferedReader for # performance. @@ -206,6 +214,9 @@ class TestGzip(BaseTest): self.test_write() with gzip.GzipFile(self.filename, 'r') as f: self.assertEqual(f.myfileobj.mode, 'rb') + support.unlink(self.filename) + with gzip.GzipFile(self.filename, 'x') as f: + self.assertEqual(f.myfileobj.mode, 'xb') def test_1647484(self): for mode in ('wb', 'rb'): @@ -389,6 +400,20 @@ class TestGzip(BaseTest): datac = gzip.compress(data) self.assertEqual(gzip.decompress(datac), data) + def test_read_truncated(self): + data = data1*50 + # Drop the CRC (4 bytes) and file size (4 bytes). + truncated = gzip.compress(data)[:-8] + with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: + self.assertRaises(EOFError, f.read) + with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: + self.assertEqual(f.read(len(data)), data) + self.assertRaises(EOFError, f.read, 1) + # Incomplete 10-byte header. + for i in range(2, 10): + with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f: + self.assertRaises(EOFError, f.read, 1) + def test_read_with_extra(self): # Gzip data with an extra field gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff' @@ -400,35 +425,59 @@ class TestGzip(BaseTest): class TestOpen(BaseTest): def test_binary_modes(self): 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) + with self.assertRaises(FileExistsError): + gzip.open(self.filename, "xb") + support.unlink(self.filename) + with gzip.open(self.filename, "xb") as f: + f.write(uncompressed) + with open(self.filename, "rb") as f: + file_data = gzip.decompress(f.read()) + self.assertEqual(file_data, uncompressed) + def test_implicit_binary_modes(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) + with self.assertRaises(FileExistsError): + gzip.open(self.filename, "x") + support.unlink(self.filename) + with gzip.open(self.filename, "x") as f: + f.write(uncompressed) + with open(self.filename, "rb") as f: + file_data = gzip.decompress(f.read()) + self.assertEqual(file_data, uncompressed) + def test_text_modes(self): uncompressed = data1.decode("ascii") * 50 uncompressed_raw = uncompressed.replace("\n", os.linesep) @@ -463,6 +512,8 @@ class TestOpen(BaseTest): with self.assertRaises(ValueError): gzip.open(self.filename, "wbt") with self.assertRaises(ValueError): + gzip.open(self.filename, "xbt") + with self.assertRaises(ValueError): gzip.open(self.filename, "rb", encoding="utf-8") with self.assertRaises(ValueError): gzip.open(self.filename, "rb", errors="ignore") |