@@ -331,6 +331,26 @@ def test_fileobj_from_fdopen(self):
331331 with gzip .GzipFile (fileobj = f , mode = "w" ) as g :
332332 self .assertEqual (g .name , "" )
333333
334+ def test_fileobj_mode (self ):
335+ gzip .GzipFile (self .filename , "wb" ).close ()
336+ with open (self .filename , "r+b" ) as f :
337+ with gzip .GzipFile (fileobj = f , mode = 'r' ) as g :
338+ self .assertEqual (g .mode , gzip .READ )
339+ with gzip .GzipFile (fileobj = f , mode = 'w' ) as g :
340+ self .assertEqual (g .mode , gzip .WRITE )
341+ with gzip .GzipFile (fileobj = f , mode = 'a' ) as g :
342+ self .assertEqual (g .mode , gzip .WRITE )
343+ with self .assertRaises (IOError ):
344+ gzip .GzipFile (fileobj = f , mode = 'z' )
345+ for mode in "rb" , "r+b" :
346+ with open (self .filename , mode ) as f :
347+ with gzip .GzipFile (fileobj = f ) as g :
348+ self .assertEqual (g .mode , gzip .READ )
349+ for mode in "wb" , "ab" :
350+ with open (self .filename , mode ) as f :
351+ with gzip .GzipFile (fileobj = f ) as g :
352+ self .assertEqual (g .mode , gzip .WRITE )
353+
334354 def test_read_with_extra (self ):
335355 # Gzip data with an extra field
336356 gzdata = (b'\x1f \x8b \x08 \x04 \xb2 \x17 cQ\x02 \xff '
0 commit comments