1616from test .support .script_helper import spawn_python , kill_python
1717import ast
1818import builtins
19+ import contextlib
1920import glob
2021import io
2122import os
@@ -126,12 +127,9 @@ def test_addpackage(self):
126127 pth_file = PthFile ()
127128 pth_file .cleanup (prep = True ) # to make sure that nothing is
128129 # pre-existing that shouldn't be
129- try :
130- pth_file .create ()
130+ with pth_file .create ():
131131 site .addpackage (pth_file .base_dir , pth_file .filename , set ())
132132 self .pth_file_tests (pth_file )
133- finally :
134- pth_file .cleanup ()
135133
136134 def make_pth (self , contents , pth_dir = '.' , pth_name = TESTFN ):
137135 # Create a .pth file and return its (abspath, basename).
@@ -183,12 +181,9 @@ def test_addsitedir(self):
183181 # (known_paths=None), flushes paths and import lines immediately.
184182 pth_file = PthFile ()
185183 pth_file .cleanup (prep = True )
186- try :
187- pth_file .create ()
184+ with pth_file .create ():
188185 site .addsitedir (pth_file .base_dir )
189186 self .pth_file_tests (pth_file )
190- finally :
191- pth_file .cleanup ()
192187
193188 def test_addsitedir_explicit_flush (self ):
194189 # addsitedir() reads .pth files and, with
@@ -198,55 +193,43 @@ def test_addsitedir_explicit_flush(self):
198193 pth_file = PthFile ()
199194 # Ensure we have a clean slate.
200195 pth_file .cleanup (prep = True )
201- try :
202- pth_file .create ()
196+ with pth_file .create ():
203197 # Pass defer_processing_start_files=True to prevent flushing.
204198 site .addsitedir (pth_file .base_dir , set (),
205199 defer_processing_start_files = True )
206200 self .assertNotIn (pth_file .imported , sys .modules )
207201 site .process_startup_files ()
208202 self .pth_file_tests (pth_file )
209- finally :
210- pth_file .cleanup ()
211203
212204 def test_addsitedir_dotfile (self ):
213205 pth_file = PthFile ('.dotfile' )
214206 pth_file .cleanup (prep = True )
215- try :
216- pth_file .create ()
207+ with pth_file .create ():
217208 site .addsitedir (pth_file .base_dir )
218209 self .assertNotIn (site .makepath (pth_file .good_dir_path )[0 ], sys .path )
219210 self .assertIn (pth_file .base_dir , sys .path )
220- finally :
221- pth_file .cleanup ()
222211
223212 @unittest .skipUnless (hasattr (os , 'chflags' ), 'test needs os.chflags()' )
224213 def test_addsitedir_hidden_flags (self ):
225214 pth_file = PthFile ()
226215 pth_file .cleanup (prep = True )
227- try :
228- pth_file .create ()
216+ with pth_file .create ():
229217 st = os .stat (pth_file .file_path )
230218 os .chflags (pth_file .file_path , st .st_flags | stat .UF_HIDDEN )
231219 site .addsitedir (pth_file .base_dir )
232220 self .assertNotIn (site .makepath (pth_file .good_dir_path )[0 ], sys .path )
233221 self .assertIn (pth_file .base_dir , sys .path )
234- finally :
235- pth_file .cleanup ()
236222
237223 @unittest .skipUnless (sys .platform == 'win32' , 'test needs Windows' )
238224 @support .requires_subprocess ()
239225 def test_addsitedir_hidden_file_attribute (self ):
240226 pth_file = PthFile ()
241227 pth_file .cleanup (prep = True )
242- try :
243- pth_file .create ()
228+ with pth_file .create ():
244229 subprocess .check_call (['attrib' , '+H' , pth_file .file_path ])
245230 site .addsitedir (pth_file .base_dir )
246231 self .assertNotIn (site .makepath (pth_file .good_dir_path )[0 ], sys .path )
247232 self .assertIn (pth_file .base_dir , sys .path )
248- finally :
249- pth_file .cleanup ()
250233
251234 # This tests _getuserbase, hence the double underline
252235 # to distinguish from a test for getuserbase
@@ -425,6 +408,7 @@ def __init__(self, filename_base=TESTFN, imported="time",
425408 self .good_dir_path = os .path .join (self .base_dir , self .good_dirname )
426409 self .bad_dir_path = os .path .join (self .base_dir , self .bad_dirname )
427410
411+ @contextlib .contextmanager
428412 def create (self ):
429413 """Create a .pth file with a comment, blank lines, an ``import
430414 <self.imported>``, a line with self.good_dirname, and a line with
@@ -433,8 +417,7 @@ def create(self):
433417 Creation of the directory for self.good_dir_path (based off of
434418 self.good_dirname) is also performed.
435419
436- Make sure to call self.cleanup() to undo anything done by this method.
437-
420+ Used as a context manager: self.cleanup() is called on exit.
438421 """
439422 FILE = open (self .file_path , 'w' )
440423 try :
@@ -446,6 +429,10 @@ def create(self):
446429 finally :
447430 FILE .close ()
448431 os .mkdir (self .good_dir_path )
432+ try :
433+ yield self
434+ finally :
435+ self .cleanup ()
449436
450437 def cleanup (self , prep = False ):
451438 """Make sure that the .pth file is deleted, self.imported is not in
0 commit comments