diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-08-11 00:36:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-11 00:36:30 (GMT) |
commit | bc69d00288a0b1f5ef49dcfd60a91c5e9b5b81ae (patch) | |
tree | 5c4a6f15ca98b77895fd4307b289aa1ef767772f /Lib/test/test_subprocess.py | |
parent | 270c3c62edd81c14fcdb6a24b4b518a777ec6534 (diff) | |
download | cpython-bc69d00288a0b1f5ef49dcfd60a91c5e9b5b81ae.zip cpython-bc69d00288a0b1f5ef49dcfd60a91c5e9b5b81ae.tar.gz cpython-bc69d00288a0b1f5ef49dcfd60a91c5e9b5b81ae.tar.bz2 |
bpo-31173: Rewrite WSTOPSIG test of test_subprocess (#3055) (#3070)
The current test_child_terminated_in_stopped_state() function test
creates a child process which calls ptrace(PTRACE_TRACEME, 0, 0) and
then crash (SIGSEGV). The problem is that calling os.waitpid() in the
parent process is not enough to close the process: the child process
remains alive and so the unit test leaks a child process in a
strange state. Closing the child process requires non-trivial code,
maybe platform specific.
Remove the functional test and replaces it with an unit test which
mocks os.waitpid() using a new _testcapi.W_STOPCODE() function to
test the WIFSTOPPED() path.
(cherry picked from commit 7b7c6dcfff6a35333988a3c74c895ed19dff2e09)
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r-- | Lib/test/test_subprocess.py | 57 |
1 files changed, 22 insertions, 35 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 4072591..4faf32e 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -29,6 +29,11 @@ try: except ImportError: threading = None +try: + import _testcapi +except ImportError: + _testcapi = None + if support.PGO: raise unittest.SkipTest("test is not helpful for PGO") @@ -2567,42 +2572,24 @@ class POSIXProcessTestCase(BaseTestCase): proc.communicate(timeout=999) mock_proc_stdin.close.assert_called_once_with() - @unittest.skipIf(not ctypes, 'ctypes module required') - @unittest.skipIf(not sys.executable, 'Test requires sys.executable') - def test_child_terminated_in_stopped_state(self): + @unittest.skipUnless(_testcapi is not None + and hasattr(_testcapi, 'W_STOPCODE'), + 'need _testcapi.W_STOPCODE') + def test_stopped(self): """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335.""" - PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME). - libc_name = ctypes.util.find_library('c') - libc = ctypes.CDLL(libc_name) - if not hasattr(libc, 'ptrace'): - raise unittest.SkipTest('ptrace() required') - - code = textwrap.dedent(f""" - import ctypes - import faulthandler - from test.support import SuppressCrashReport - - libc = ctypes.CDLL({libc_name!r}) - libc.ptrace({PTRACE_TRACEME}, 0, 0) - """) - - child = subprocess.Popen([sys.executable, '-c', code]) - if child.wait() != 0: - raise unittest.SkipTest('ptrace() failed - unable to test') - - code += textwrap.dedent(f""" - with SuppressCrashReport(): - # Crash the process - faulthandler._sigsegv() - """) - child = subprocess.Popen([sys.executable, '-c', code]) - try: - returncode = child.wait() - except: - child.kill() # Clean up the hung stopped process. - raise - self.assertNotEqual(0, returncode) - self.assertLess(returncode, 0) # signal death, likely SIGSEGV. + args = [sys.executable, '-c', 'pass'] + proc = subprocess.Popen(args) + + # Wait until the real process completes to avoid zombie process + pid = proc.pid + pid, status = os.waitpid(pid, 0) + self.assertEqual(status, 0) + + status = _testcapi.W_STOPCODE(3) + with mock.patch('subprocess.os.waitpid', return_value=(pid, status)): + returncode = proc.wait() + + self.assertEqual(returncode, -3) @unittest.skipUnless(mswindows, "Windows specific tests") |