Skip to content

Commit 9ca9665

Browse files
author
Anselm Kruis
committed
bpo-30028: Add comments, move the test to test_support.py
Added an explanatory comment and moved the test to test_support.py. I also modified the test to be in line with the other tests of temp_cwd.
1 parent 7b96cf7 commit 9ca9665

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

Lib/test/support/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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

Lib/test/test_regrtest.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff 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-
838824
if __name__ == '__main__':
839825
unittest.main()

Lib/test/test_support.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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):

0 commit comments

Comments
 (0)