From: Serhiy Storchaka Date: Sun, 22 Oct 2017 11:54:05 +0000 (+0300) Subject: [2.7] bpo-28286: Add tests for the mode argument of GzipFile. (GH-4074). (#4077) X-Git-Tag: v2.7.15rc1~160 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f7d19b0464cb89ec696affbad9fd7e7c94456eaf;p=python [2.7] bpo-28286: Add tests for the mode argument of GzipFile. (GH-4074). (#4077) (cherry picked from commit bcbdd2f8db396c3f0ec9186162b39b5a34effa0e) --- diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 5025b91d34..902d93fe04 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -331,6 +331,26 @@ class TestGzip(unittest.TestCase): with gzip.GzipFile(fileobj=f, mode="w") as g: self.assertEqual(g.name, "") + def test_fileobj_mode(self): + gzip.GzipFile(self.filename, "wb").close() + with open(self.filename, "r+b") as f: + with gzip.GzipFile(fileobj=f, mode='r') as g: + self.assertEqual(g.mode, gzip.READ) + with gzip.GzipFile(fileobj=f, mode='w') as g: + self.assertEqual(g.mode, gzip.WRITE) + with gzip.GzipFile(fileobj=f, mode='a') as g: + self.assertEqual(g.mode, gzip.WRITE) + with self.assertRaises(IOError): + gzip.GzipFile(fileobj=f, mode='z') + for mode in "rb", "r+b": + with open(self.filename, mode) as f: + with gzip.GzipFile(fileobj=f) as g: + self.assertEqual(g.mode, gzip.READ) + for mode in "wb", "ab": + with open(self.filename, mode) as f: + with gzip.GzipFile(fileobj=f) as g: + self.assertEqual(g.mode, gzip.WRITE) + def test_read_with_extra(self): # Gzip data with an extra field gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'