cpython: 8e3d412f8e89 (original) (raw)
Mercurial > cpython
changeset 106279:8e3d412f8e89 2.7
Issue #29335: Fix subprocess.Popen.wait() when the child process has exited to a stopped instead of terminated state (ex: when under ptrace). [#29335]
Gregory P. Smith greg@krypto.org | |
---|---|
date | Sun, 22 Jan 2017 22:38:28 -0800 |
parents | 9b22d52a6d4b |
children | fb2885f9b4dd |
files | Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS |
diffstat | 3 files changed, 53 insertions(+), 1 deletions(-)[+] [-] Lib/subprocess.py 5 Lib/test/test_subprocess.py 46 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1026,13 +1026,16 @@ class Popen(object): def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED, _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
_WEXITSTATUS=os.WEXITSTATUS):[](#l1.7)
_WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,[](#l1.8)
_WSTOPSIG=os.WSTOPSIG):[](#l1.9) # This method is called (indirectly) by __del__, so it cannot[](#l1.10) # refer to anything outside of its local scope.[](#l1.11) if _WIFSIGNALED(sts):[](#l1.12) self.returncode = -_WTERMSIG(sts)[](#l1.13) elif _WIFEXITED(sts):[](#l1.14) self.returncode = _WEXITSTATUS(sts)[](#l1.15)
elif _WIFSTOPPED(sts):[](#l1.16)
self.returncode = -_WSTOPSIG(sts)[](#l1.17) else:[](#l1.18) # Should never happen[](#l1.19) raise RuntimeError("Unknown child exit status!")[](#l1.20)
--- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -2,6 +2,7 @@ import unittest from test import test_support import subprocess import sys +import platform import signal import os import errno @@ -11,6 +12,11 @@ import re import sysconfig try:
+ +try: import resource except ImportError: resource = None @@ -1216,6 +1222,46 @@ class POSIXProcessTestCase(BaseTestCase) self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
- _libc_file_extensions = {
'Linux': 'so.6',[](#l2.28)
'Darwin': 'dylib',[](#l2.29)
- }
- @unittest.skipIf(not ctypes, 'ctypes module required.')
- @unittest.skipIf(platform.uname()[0] not in _libc_file_extensions,
'Test requires a libc this code can load with ctypes.')[](#l2.33)
- @unittest.skipIf(not sys.executable, 'Test requires sys.executable.')
- def test_child_terminated_in_stopped_state(self):
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""[](#l2.36)
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).[](#l2.37)
libc_name = 'libc.' + self._libc_file_extensions[platform.uname()[0]][](#l2.38)
libc = ctypes.CDLL(libc_name)[](#l2.39)
if not hasattr(libc, 'ptrace'):[](#l2.40)
raise unittest.SkipTest('ptrace() required.')[](#l2.41)
test_ptrace = subprocess.Popen([](#l2.42)
[sys.executable, '-c', """if True:[](#l2.43)
import ctypes[](#l2.44)
libc = ctypes.CDLL({libc_name!r})[](#l2.45)
libc.ptrace({PTRACE_TRACEME}, 0, 0)[](#l2.46)
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)[](#l2.47)
])[](#l2.48)
if test_ptrace.wait() != 0:[](#l2.49)
raise unittest.SkipTest('ptrace() failed - unable to test.')[](#l2.50)
child = subprocess.Popen([](#l2.51)
[sys.executable, '-c', """if True:[](#l2.52)
import ctypes[](#l2.53)
libc = ctypes.CDLL({libc_name!r})[](#l2.54)
libc.ptrace({PTRACE_TRACEME}, 0, 0)[](#l2.55)
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.[](#l2.56)
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)[](#l2.57)
])[](#l2.58)
try:[](#l2.59)
returncode = child.wait()[](#l2.60)
except Exception as e:[](#l2.61)
child.kill() # Clean up the hung stopped process.[](#l2.62)
raise e[](#l2.63)
self.assertNotEqual(0, returncode)[](#l2.64)
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.[](#l2.65)
+ @unittest.skipUnless(mswindows, "Windows specific tests") class Win32ProcessTestCase(BaseTestCase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -23,6 +23,9 @@ Extension Modules Library ------- +- Issue #29335: Fix subprocess.Popen.wait() when the child process has