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):
965
965
try :
966
966
yield path
967
967
finally :
968
+ # In case the process forks, let only the parent remove the
969
+ # directory. The child has a diffent process id. (bpo-30028)
968
970
if dir_created and pid == os .getpid ():
969
971
rmtree (path )
970
972
Original file line number Diff line number Diff line change @@ -821,19 +821,5 @@ 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
- 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
-
838
824
if __name__ == '__main__' :
839
825
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):
161
161
f'temporary directory { path !r} : ' ),
162
162
warn )
163
163
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
+
164
178
# Tests for change_cwd()
165
179
166
180
def test_change_cwd (self ):
You can’t perform that action at this time.
0 commit comments