@@ -312,26 +312,26 @@ def test_low_compression(self):
312
312
self .assertEqual (openobj .read (1 ), b'2' )
313
313
314
314
def test_writestr_compression (self ):
315
- zipfp = zipfile .ZipFile (TESTFN2 , "w" )
316
- zipfp .writestr ("b.txt" , "hello world" , compress_type = self .compression )
317
- info = zipfp .getinfo ('b.txt' )
318
- self .assertEqual (info .compress_type , self .compression )
315
+ with zipfile .ZipFile (TESTFN2 , "w" ) as zipfp :
316
+ zipfp .writestr ("b.txt" , "hello world" , compress_type = self .compression )
317
+ info = zipfp .getinfo ('b.txt' )
318
+ self .assertEqual (info .compress_type , self .compression )
319
319
320
320
def test_writestr_compresslevel (self ):
321
- zipfp = zipfile .ZipFile (TESTFN2 , "w" , compresslevel = 1 )
322
- zipfp .writestr ("a.txt" , "hello world" , compress_type = self .compression )
323
- zipfp .writestr ("b.txt" , "hello world" , compress_type = self .compression ,
324
- compresslevel = 2 )
321
+ with zipfile .ZipFile (TESTFN2 , "w" , compresslevel = 1 ) as zipfp :
322
+ zipfp .writestr ("a.txt" , "hello world" , compress_type = self .compression )
323
+ zipfp .writestr ("b.txt" , "hello world" , compress_type = self .compression ,
324
+ compresslevel = 2 )
325
325
326
- # Compression level follows the constructor.
327
- a_info = zipfp .getinfo ('a.txt' )
328
- self .assertEqual (a_info .compress_type , self .compression )
329
- self .assertEqual (a_info .compress_level , 1 )
326
+ # Compression level follows the constructor.
327
+ a_info = zipfp .getinfo ('a.txt' )
328
+ self .assertEqual (a_info .compress_type , self .compression )
329
+ self .assertEqual (a_info .compress_level , 1 )
330
330
331
- # Compression level is overridden.
332
- b_info = zipfp .getinfo ('b.txt' )
333
- self .assertEqual (b_info .compress_type , self .compression )
334
- self .assertEqual (b_info ._compresslevel , 2 )
331
+ # Compression level is overridden.
332
+ b_info = zipfp .getinfo ('b.txt' )
333
+ self .assertEqual (b_info .compress_type , self .compression )
334
+ self .assertEqual (b_info ._compresslevel , 2 )
335
335
336
336
def test_read_return_size (self ):
337
337
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
@@ -2330,13 +2330,15 @@ def test_empty_zipfile(self):
2330
2330
zipf = zipfile .ZipFile (TESTFN , mode = "r" )
2331
2331
except zipfile .BadZipFile :
2332
2332
self .fail ("Unable to create empty ZIP file in 'w' mode" )
2333
+ zipf .close ()
2333
2334
2334
2335
zipf = zipfile .ZipFile (TESTFN , mode = "a" )
2335
2336
zipf .close ()
2336
2337
try :
2337
2338
zipf = zipfile .ZipFile (TESTFN , mode = "r" )
2338
2339
except :
2339
2340
self .fail ("Unable to create empty ZIP file in 'a' mode" )
2341
+ zipf .close ()
2340
2342
2341
2343
def test_open_empty_file (self ):
2342
2344
# Issue 1710703: Check that opening a file with less than 22 bytes
0 commit comments