Visitar URL original
bpo-30028: make test.support.temp_cwd() fork-safe by akruis · Pull Request #1066 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
Anselm Kruis
Anselm Kruis committed Apr 10, 2017
commit 9ca966554cab0decb4b555701148fef5be2b3875
2 changes: 2 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ def temp_dir(path=None, quiet=False):
try:
yield path
finally:
# 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():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is non obvious, can you please add a short comment explaining why you test the pid?

I guess that it's test to avoid that two processes try to remove the directory if the parent uses fork()?

Add bpo-30028 in the comment.

rmtree(path)

Expand Down
14 changes: 0 additions & 14 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,19 +821,5 @@ def test_crashed(self):
randomize=True)


class TempCwdTestCase(unittest.TestCase):
@unittest.skipUnless(hasattr(os, "fork"), "test requires os.fork")
def test_forked_child(self):
with support.temp_cwd() as cwd:
pid = os.fork()
if pid != 0:
# parent
os.waitpid(pid, 0)
self.assertTrue(os.path.isdir(cwd), "directory was removed "+cwd)
if pid == 0:
# terminate the child in order to not confuse the test runner
os._exit(0)


if __name__ == '__main__':
unittest.main()
14 changes: 14 additions & 0 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ def test_temp_dir__existing_dir__quiet_true(self):
f'temporary directory {path!r}: '),
warn)

@unittest.skipUnless(hasattr(os, "fork"), "test requires os.fork")
def test_temp_dir__forked_child(self):
"""Test that a forked child process does not remove the directory."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a reference to the issue: bpo-30028

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No other doc string in this file contains a reference to bpo. Therefore I added the reference as a comment.

with support.temp_cwd() as temp_path:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, please don't use fork in a test process. Write a short script run with test.support.script_helper. Otherwise, I expect too many issues like bad handling of KeyboardInterrupt. Use textwrap.dedent() to include the script directly here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (1a0364d). Is the patch now OK?

pid = os.fork()
if pid != 0:
# parent process
os.waitpid(pid, 0) # wait for the child to terminate
# make sure that temp_path is still present
self.assertTrue(os.path.isdir(temp_path))
if pid == 0:
# terminate the child in order to not confuse the test runner
os._exit(0)

# Tests for change_cwd()

def test_change_cwd(self):
Expand Down