File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -960,10 +960,12 @@ def temp_dir(path=None, quiet=False):
960960 warnings .warn (f'tests may fail, unable to create '
961961 f'temporary directory { path !r} : { exc } ' ,
962962 RuntimeWarning , stacklevel = 3 )
963+ if dir_created :
964+ pid = os .getpid ()
963965 try :
964966 yield path
965967 finally :
966- if dir_created :
968+ if dir_created and pid == os . getpid () :
967969 rmtree (path )
968970
969971@contextlib .contextmanager
Original file line number Diff line number Diff line change @@ -821,5 +821,20 @@ def test_crashed(self):
821821 randomize = True )
822822
823823
824+ class TempCwdTestCase (unittest .TestCase ):
825+ @unittest .skipUnless (hasattr (os , "fork" ), "test requires os.fork" )
826+ def test_forked_child (self ):
827+ import sys
828+ with support .temp_cwd () as cwd :
829+ pid = os .fork ()
830+ if pid != 0 :
831+ # parent
832+ os .waitpid (pid , 0 )
833+ self .assertTrue (os .path .isdir (cwd ), "directory was removed " + cwd )
834+ if pid == 0 :
835+ # terminate the child in order to not confuse the test runner
836+ os ._exit (0 )
837+
838+
824839if __name__ == '__main__' :
825840 unittest .main ()
You can’t perform that action at this time.
0 commit comments