| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b852d8c commit 95ec069
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -753,10 +753,14 @@ def temp_dir(path=None, quiet=False): | |||
| 753 | 753 | raise | |
| 754 | 754 | warnings.warn('tests may fail, unable to create temp dir: ' + path, | |
| 755 | 755 | RuntimeWarning, stacklevel=3) | |
| 756 | + if dir_created: | ||
| 757 | + pid = os.getpid() | ||
| 756 | 758 | try: | |
| 757 | 759 | yield path | |
| 758 | 760 | finally: | |
| 759 | - if dir_created: | ||
| 761 | + # In case the process forks, let only the parent remove the | ||
| 762 | + # directory. The child has a diffent process id. (bpo-30028) | ||
| 763 | + if dir_created and pid == os.getpid(): | ||
| 760 | 764 | rmtree(path) | |
| 761 | 765 | ||
| 762 | 766 | @contextlib.contextmanager | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,8 +6,10 @@ | |||
| 6 | 6 | import unittest | |
| 7 | 7 | import socket | |
| 8 | 8 | import tempfile | |
| 9 | + import textwrap | ||
| 9 | 10 | import errno | |
| 10 | 11 | from test import support | |
| 12 | + from test.support import script_helper | ||
| 11 | 13 | ||
| 12 | 14 | TESTFN = support.TESTFN | |
| 13 | 15 | ||
@@ -176,6 +178,34 @@ def test_temp_dir__existing_dir__quiet_true(self): | |||
| 176 | 178 | expected = ['tests may fail, unable to create temp dir: ' + path] | |
| 177 | 179 | self.assertEqual(warnings, expected) | |
| 178 | 180 | ||
| 181 | + @unittest.skipUnless(hasattr(os, "fork"), "test requires os.fork") | ||
| 182 | + def test_temp_dir__forked_child(self): | ||
| 183 | + """Test that a forked child process does not remove the directory.""" | ||
| 184 | + # See bpo-30028 for details. | ||
| 185 | + # Run the test as an external script, because it uses fork. | ||
| 186 | + script_helper.assert_python_ok("-c", textwrap.dedent(""" | ||
| 187 | + import os | ||
| 188 | + from test import support | ||
| 189 | + with support.temp_cwd() as temp_path: | ||
| 190 | + pid = os.fork() | ||
| 191 | + if pid != 0: | ||
| 192 | + # parent process (child has pid == 0) | ||
| 193 | + | ||
| 194 | + # wait for the child to terminate | ||
| 195 | + (pid, status) = os.waitpid(pid, 0) | ||
| 196 | + if status != 0: | ||
| 197 | + raise AssertionError("Child process failed with exit " | ||
| 198 | + "status indication " | ||
| 199 | + "0x{:x}.".format(status)) | ||
| 200 | + | ||
| 201 | + # Make sure that temp_path is still present. When the child | ||
| 202 | + # process leaves the 'temp_cwd'-context, the __exit__()- | ||
| 203 | + # method of the context must not remove the temporary | ||
| 204 | + # directory. | ||
| 205 | + if not os.path.isdir(temp_path): | ||
| 206 | + raise AssertionError("Child removed temp_path.") | ||
| 207 | + """)) | ||
| 208 | + | ||
| 179 | 209 | # Tests for change_cwd() | |
| 180 | 210 | ||
| 181 | 211 | def test_change_cwd(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -774,6 +774,7 @@ Pedro Kroger | |||
| 774 | 774 | Hannu Krosing | |
| 775 | 775 | Andrej Krpic | |
| 776 | 776 | Ivan Krstić | |
| 777 | + Anselm Kruis | ||
| 777 | 778 | Steven Kryskalla | |
| 778 | 779 | Andrew Kuchling | |
| 779 | 780 | Dave Kuhlman | |
| Back | FazBrowse Home | New Git URL |
0 commit comments