Skip to content
Merged
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: Run the fork test as an external script.
  • Loading branch information
Anselm Kruis committed Apr 10, 2017
commit 1a0364d4bf0427df69f0e560664ab80e7389ac86
24 changes: 14 additions & 10 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import socket
import tempfile
import errno
import textwrap
from test import support
from test.support import script_helper

TESTFN = support.TESTFN

Expand Down Expand Up @@ -164,16 +166,18 @@ def test_temp_dir__existing_dir__quiet_true(self):
@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:
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)
# Run the test as an external script, because it uses fork.
script_helper.assert_python_ok("-c", textwrap.dedent("""
import os
from test import support
with support.temp_cwd() as temp_path:
pid = os.fork()
if pid != 0:
# parent process
os.waitpid(pid, 0) # wait for the child to terminate
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 test that the child didn't fail.

# make sure that temp_path is still present
assert os.path.isdir(temp_path), "Child removed 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.

I would avoid assert keyword which might be removed depending on the Python option, but use an explicit if + raise Exception(...). But maybe it's ok, I don't know :-)

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.

Since this is a subprocess, explicit os.exit() can be more appropriate.

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.

I'm currently on a business trip and can't access my Linux box. I'll update the pull request on Thursday. But could you please agree on either raising AssertionError or a print() and explicit os.exit(). In the end it makes no difference.

"""))
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 comment explaining that the child exits without removing the temporary 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.

Isn't the comment above enough?


# Tests for change_cwd()

Expand Down