Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@ def __enter__(self):
#
# This assumes that this context manager is used in tests
# that might trigger the next manager.
import subprocess
cmd = ['/usr/bin/defaults', 'read',
'com.apple.CrashReporter', 'DialogType']
proc = subprocess.Popen(cmd,
Expand Down
47 changes: 26 additions & 21 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
import time
import re
import sysconfig
import textwrap

try:
import ctypes
except ImportError:
ctypes = None
else:
import ctypes.util

try:
import resource
Expand Down Expand Up @@ -1265,33 +1262,41 @@ def test_pipe_cloexec(self):

self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))

@unittest.skipIf(not ctypes, 'ctypes module required')
@unittest.skipIf(not sys.executable, 'Test requires sys.executable')
_libc_file_extensions = {
'Linux': 'so.6',
'Darwin': 'dylib',
}
@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.')
@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."""
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
libc_name = ctypes.util.find_library('c')
libc_name = 'libc.' + self._libc_file_extensions[platform.uname()[0]]
libc = ctypes.CDLL(libc_name)
if not hasattr(libc, 'ptrace'):
raise unittest.SkipTest('ptrace() required')

code = textwrap.dedent("""
raise unittest.SkipTest('ptrace() required.')
test_ptrace = subprocess.Popen(
[sys.executable, '-c', """if True:
import ctypes
from test.support import _crash_python

libc = ctypes.CDLL({libc_name!r})
libc.ptrace({PTRACE_TRACEME}, 0, 0)
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME))

child = subprocess.Popen([sys.executable, '-c', code])
if child.wait() != 0:
raise unittest.SkipTest('ptrace() failed - unable to test')
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
])
if test_ptrace.wait() != 0:
raise unittest.SkipTest('ptrace() failed - unable to test.')

code += textwrap.dedent("""
# Crash the process
_crash_python()
""")
child = subprocess.Popen([sys.executable, '-c', code])
child = subprocess.Popen(
[sys.executable, '-c', """if True:
import ctypes
from test.support import SuppressCrashReport
libc = ctypes.CDLL({libc_name!r})
libc.ptrace({PTRACE_TRACEME}, 0, 0)
with SuppressCrashReport():
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
])
try:
returncode = child.wait()
except:
Expand Down