]> granicus.if.org Git - python/commitdiff
[2.7] bpo-28286: Add tests for the mode argument of GzipFile. (GH-4074). (#4077)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 22 Oct 2017 11:54:05 +0000 (14:54 +0300)
committerGitHub <noreply@github.com>
Sun, 22 Oct 2017 11:54:05 +0000 (14:54 +0300)
(cherry picked from commit bcbdd2f8db396c3f0ec9186162b39b5a34effa0e)

Lib/test/test_gzip.py

index 5025b91d343aa6cf6ad8e3181b1af60edf1e0460..902d93fe043f8e42d2c393407a536a198703f7fe 100644 (file)
@@ -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'