1- import contextlib
21import io
32import itertools
43import pathlib
@@ -83,12 +82,8 @@ def build_alpharep_fixture():
8382
8483
8584class TestPath (unittest .TestCase ):
86- def setUp (self ):
87- self .fixtures = contextlib .ExitStack ()
88- self .addCleanup (self .fixtures .close )
89-
9085 def zipfile_ondisk (self , alpharep ):
91- tmpdir = pathlib .Path (self .fixtures . enter_context (temp_dir ()))
86+ tmpdir = pathlib .Path (self .enterContext (temp_dir ()))
9287 buffer = alpharep .fp
9388 alpharep .close ()
9489 path = tmpdir / alpharep .filename
@@ -145,7 +140,7 @@ def test_open(self, alpharep):
145140
146141 def test_open_encoding_utf16 (self ):
147142 in_memory_file = io .BytesIO ()
148- zf = zipfile .ZipFile (in_memory_file , "w" )
143+ zf = self . enterContext ( zipfile .ZipFile (in_memory_file , "w" ) )
149144 zf .writestr ("path/16.txt" , "This was utf-16" .encode ("utf-16" ))
150145 zf .filename = "test_open_utf16.zip"
151146 root = zipfile .Path (zf )
@@ -160,7 +155,7 @@ def test_open_encoding_utf16(self):
160155
161156 def test_open_encoding_errors (self ):
162157 in_memory_file = io .BytesIO ()
163- zf = zipfile .ZipFile (in_memory_file , "w" )
158+ zf = self . enterContext ( zipfile .ZipFile (in_memory_file , "w" ) )
164159 zf .writestr ("path/bad-utf8.bin" , b"invalid utf-8: \xff \xff ." )
165160 zf .filename = "test_read_text_encoding_errors.zip"
166161 root = zipfile .Path (zf )
@@ -204,7 +199,8 @@ def test_open_write(self):
204199 If the zipfile is open for write, it should be possible to
205200 write bytes or text to it.
206201 """
207- zf = zipfile .Path (zipfile .ZipFile (io .BytesIO (), mode = 'w' ))
202+ zip_file = self .enterContext (zipfile .ZipFile (io .BytesIO (), mode = 'w' ))
203+ zf = zipfile .Path (zip_file )
208204 with zf .joinpath ('file.bin' ).open ('wb' ) as strm :
209205 strm .write (b'binary contents' )
210206 with zf .joinpath ('file.txt' ).open ('w' , encoding = "utf-8" ) as strm :
@@ -319,7 +315,7 @@ def test_mutability(self, alpharep):
319315 def huge_zipfile (self ):
320316 """Create a read-only zipfile with a huge number of entries."""
321317 strm = io .BytesIO ()
322- zf = zipfile .ZipFile (strm , "w" )
318+ zf = self . enterContext ( zipfile .ZipFile (strm , "w" ) )
323319 for entry in map (str , range (self .HUGE_ZIPFILE_NUM_ENTRIES )):
324320 zf .writestr (entry , entry )
325321 zf .mode = 'r'
@@ -530,7 +526,8 @@ def test_glob_chars(self, alpharep):
530526 ]
531527
532528 def test_glob_empty (self ):
533- root = zipfile .Path (zipfile .ZipFile (io .BytesIO (), 'w' ))
529+ zip_file = self .enterContext (zipfile .ZipFile (io .BytesIO (), 'w' ))
530+ root = zipfile .Path (zip_file )
534531 with self .assertRaises (ValueError ):
535532 root .glob ('' )
536533
@@ -614,7 +611,7 @@ def test_malformed_paths(self):
614611 Paths with dots are treated like regular files.
615612 """
616613 data = io .BytesIO ()
617- zf = zipfile .ZipFile (data , "w" )
614+ zf = self . enterContext ( zipfile .ZipFile (data , "w" ) )
618615 zf .writestr ("/one-slash.txt" , b"content" )
619616 zf .writestr ("//two-slash.txt" , b"content" )
620617 zf .writestr ("../parent.txt" , b"content" )
@@ -632,7 +629,7 @@ def test_unsupported_names(self):
632629 in the zip file.
633630 """
634631 data = io .BytesIO ()
635- zf = zipfile .ZipFile (data , "w" )
632+ zf = self . enterContext ( zipfile .ZipFile (data , "w" ) )
636633 zf .writestr ("path?" , b"content" )
637634 zf .writestr ("V: NMS.flac" , b"fLaC..." )
638635 zf .filename = ''
@@ -647,7 +644,7 @@ def test_backslash_not_separator(self):
647644 In a zip file, backslashes are not separators.
648645 """
649646 data = io .BytesIO ()
650- zf = zipfile .ZipFile (data , "w" )
647+ zf = self . enterContext ( zipfile .ZipFile (data , "w" ) )
651648 zf .writestr (DirtyZipInfo ("foo\\ bar" )._for_archive (zf ), b"content" )
652649 zf .filename = ''
653650 root = zipfile .Path (zf )
0 commit comments