This repository was archived by the owner on Feb 13, 2025. It is now read-only.
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):
960
960
warnings .warn (f'tests may fail, unable to create '
961
961
f'temporary directory { path !r} : { exc } ' ,
962
962
RuntimeWarning , stacklevel = 3 )
963
+ if dir_created :
964
+ pid = os .getpid ()
963
965
try :
964
966
yield path
965
967
finally :
966
- if dir_created :
968
+ if dir_created and pid == os . getpid () :
967
969
rmtree (path )
968
970
969
971
@contextlib .contextmanager
Original file line number Diff line number Diff line change @@ -821,5 +821,20 @@ def test_crashed(self):
821
821
randomize = True )
822
822
823
823
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
+
824
839
if __name__ == '__main__' :
825
840
unittest .main ()
You can’t perform that action at this time.
0 commit comments