summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorAnselm Kruis <a.kruis@science-computing.de>2018-02-23 01:37:38 (GMT)
committerGregory P. Smith <greg@krypto.org>2018-02-23 01:37:38 (GMT)
commit33dddac00ba8d9b72cf21b8698504077eb3c23ad (patch)
tree40893955f445131d96c340493afd027cfcaa66f5 /Lib/test/support
parent520b7ae27e39d1c77ea74ccd1b184d7cb43f9dcb (diff)
downloadcpython-33dddac00ba8d9b72cf21b8698504077eb3c23ad.zip
cpython-33dddac00ba8d9b72cf21b8698504077eb3c23ad.tar.gz
cpython-33dddac00ba8d9b72cf21b8698504077eb3c23ad.tar.bz2
bpo-30028: make test.support.temp_cwd() fork-safe (GH-1066)
Make test.support.temp_cwd() fork-safe. The context manager test.support.temp_cwd() no longer removes the temporary directory when executing in a process other than the parent it entered from. If a forked child exits the context manager it won't do the cleanup.
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/__init__.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 6c9e31a..b4269f4 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -948,10 +948,14 @@ def temp_dir(path=None, quiet=False):
warnings.warn(f'tests may fail, unable to create '
f'temporary directory {path!r}: {exc}',
RuntimeWarning, stacklevel=3)
+ if dir_created:
+ pid = os.getpid()
try:
yield path
finally:
- if dir_created:
+ # In case the process forks, let only the parent remove the
+ # directory. The child has a diffent process id. (bpo-30028)
+ if dir_created and pid == os.getpid():
rmtree(path)
@contextlib.contextmanager