Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 22e032a

Browse filesBrowse files
author
Anselm Kruis
committed
bpo-30028: make test.support.temp_cwd() fork-safe
Improve the context manager test.support.temp_cwd() to not remove the temporary directory, if a forked child terminates.
1 parent 45d22c2 commit 22e032a
Copy full SHA for 22e032a

File tree

2 files changed

+18
-1
lines changed
Filter options

2 files changed

+18
-1
lines changed

‎Lib/test/support/__init__.py

Copy file name to clipboardExpand all lines: Lib/test/support/__init__.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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

‎Lib/test/test_regrtest.py

Copy file name to clipboardExpand all lines: Lib/test/test_regrtest.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
824839
if __name__ == '__main__':
825840
unittest.main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.