File tree Expand file tree Collapse file tree 3 files changed +16
-14
lines changed Expand file tree Collapse file tree 3 files changed +16
-14
lines changed Original file line number Diff line number Diff line change @@ -965,6 +965,8 @@ def temp_dir(path=None, quiet=False):
965965 try :
966966 yield path
967967 finally :
968+ # In case the process forks, let only the parent remove the
969+ # directory. The child has a diffent process id. (bpo-30028)
968970 if dir_created and pid == os .getpid ():
969971 rmtree (path )
970972
Original file line number Diff line number Diff line change @@ -821,19 +821,5 @@ 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- with support .temp_cwd () as cwd :
828- pid = os .fork ()
829- if pid != 0 :
830- # parent
831- os .waitpid (pid , 0 )
832- self .assertTrue (os .path .isdir (cwd ), "directory was removed " + cwd )
833- if pid == 0 :
834- # terminate the child in order to not confuse the test runner
835- os ._exit (0 )
836-
837-
838824if __name__ == '__main__' :
839825 unittest .main ()
Original file line number Diff line number Diff line change @@ -161,6 +161,20 @@ def test_temp_dir__existing_dir__quiet_true(self):
161161 f'temporary directory { path !r} : ' ),
162162 warn )
163163
164+ @unittest .skipUnless (hasattr (os , "fork" ), "test requires os.fork" )
165+ def test_temp_dir__forked_child (self ):
166+ """Test that a forked child process does not remove the directory."""
167+ with support .temp_cwd () as temp_path :
168+ pid = os .fork ()
169+ if pid != 0 :
170+ # parent process
171+ os .waitpid (pid , 0 ) # wait for the child to terminate
172+ # make sure that temp_path is still present
173+ self .assertTrue (os .path .isdir (temp_path ))
174+ if pid == 0 :
175+ # terminate the child in order to not confuse the test runner
176+ os ._exit (0 )
177+
164178 # Tests for change_cwd()
165179
166180 def test_change_cwd (self ):
You can’t perform that action at this time.
0 commit comments