@@ -2258,7 +2258,7 @@ class Decompressor:
22582258 def decompress (self , data ):
22592259 return data .swapcase ()
22602260
2261- def setUp (self ):
2261+ def test_roundtrip_monkeypatched_decompressor (self ):
22622262 orig_check_compression = zipfile ._check_compression
22632263 orig_get_compressor = zipfile ._get_compressor
22642264 orig_get_decompressor = zipfile ._get_decompressor
@@ -2277,29 +2277,26 @@ def get_decompressor(compress_type):
22772277 return self .Decompressor ()
22782278 return orig_get_decompressor (compress_type )
22792279
2280- self .enterContext (mock .patch .object (
2281- zipfile , '_check_compression' , check_compression ))
2282- self .enterContext (mock .patch .object (
2283- zipfile , '_get_compressor' , get_compressor ))
2284- self .enterContext (mock .patch .object (
2285- zipfile , '_get_decompressor' , get_decompressor ))
2286-
2287- def test_roundtrip_monkeypatched_decompressor (self ):
2288- data = bytes (range (256 )) * 8
2289- buf = io .BytesIO ()
2290- with zipfile .ZipFile (buf , "w" , compression = self .COMPRESSION ) as zf :
2291- zf .writestr ("member" , data )
2292- self .assertIn (data .swapcase (), buf .getvalue ())
2293- with zipfile .ZipFile (io .BytesIO (buf .getvalue ())) as zf :
2294- self .assertEqual (zf .read ("member" ), data )
2295- with zf .open ("member" ) as f :
2296- self .assertEqual (f .read (100 ), data [:100 ])
2297- self .assertEqual (f .read1 (100 ), data [100 :200 ])
2298- f .seek (- 100 , os .SEEK_END )
2299- self .assertEqual (f .read (), data [- 100 :])
2300- # Rewinding past the read buffer re-creates the decompressor.
2301- f .seek (0 )
2302- self .assertEqual (f .read (), data )
2280+ with (
2281+ mock .patch .object (zipfile , '_check_compression' , check_compression ),
2282+ mock .patch .object (zipfile , '_get_compressor' , get_compressor ),
2283+ mock .patch .object (zipfile , '_get_decompressor' , get_decompressor ),
2284+ ):
2285+ data = bytes (range (256 )) * 8
2286+ buf = io .BytesIO ()
2287+ with zipfile .ZipFile (buf , "w" , compression = self .COMPRESSION ) as zf :
2288+ zf .writestr ("member" , data )
2289+ self .assertIn (data .swapcase (), buf .getvalue ())
2290+ with zipfile .ZipFile (io .BytesIO (buf .getvalue ())) as zf :
2291+ self .assertEqual (zf .read ("member" ), data )
2292+ with zf .open ("member" ) as f :
2293+ self .assertEqual (f .read (100 ), data [:100 ])
2294+ self .assertEqual (f .read1 (100 ), data [100 :200 ])
2295+ f .seek (- 100 , os .SEEK_END )
2296+ self .assertEqual (f .read (), data [- 100 :])
2297+ # Rewinding past the read buffer re-creates the decompressor.
2298+ f .seek (0 )
2299+ self .assertEqual (f .read (), data )
23032300
23042301
23052302class AbstractBadCrcTests :
0 commit comments