bpo-28286: Add tests for the mode argument of GzipFile. (GH-4074) (#4076)

(cherry picked from commit bcbdd2f8db)
This commit is contained in:
Miss Islington (bot) 2017-10-22 04:53:43 -07:00 committed by Serhiy Storchaka
parent 34ef6da8f5
commit 251de30935
1 changed files with 24 additions and 0 deletions

View File

@ -431,6 +431,30 @@ class TestGzip(BaseTest):
with gzip.GzipFile(fileobj=f, mode="w") as g:
pass
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 gzip.GzipFile(fileobj=f, mode='x') as g:
self.assertEqual(g.mode, gzip.WRITE)
with self.assertRaises(ValueError):
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", "xb":
if "x" in mode:
support.unlink(self.filename)
with open(self.filename, mode) as f:
with gzip.GzipFile(fileobj=f) as g:
self.assertEqual(g.mode, gzip.WRITE)
def test_bytes_filename(self):
str_filename = self.filename
try: