From 10c294c0d3e0e05e453d28b92ca0648cb55dc074 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 30 Jun 2017 23:11:36 +0200 Subject: [PATCH] bpo-30764: Fix test_subprocess on macOS Revert partially commit 2097b9e0ef32ab7a0d745edc0f707c615780c006. --- Lib/test/support/__init__.py | 1 + Lib/test/test_subprocess.py | 47 ++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c024b0773c0686..63b9419b08ac4b 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -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, diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 77633376eb5671..ee1d25d4a15b68 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -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 @@ -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: