Skip to content
Merged
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
14 changes: 14 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,3 +1945,17 @@ def __exit__(self, *ignore_exc):
resource.setrlimit(resource.RLIMIT_CORE, self.old_value)
except (ValueError, OSError):
pass


def _crash_python():
"""Deliberate crash of Python.

Python can be killed by a segmentation fault (SIGSEGV), a bus error
(SIGBUS), or a different error depending on the platform.

Use SuppressCrashReport() to prevent a crash report from popping up.
"""

import ctypes
with SuppressCrashReport():
ctypes.string_at(0)
2 changes: 1 addition & 1 deletion Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def test_list_tests(self):

def test_crashed(self):
# Any code which causes a crash
code = 'import ctypes; ctypes.string_at(0)'
code = 'import test.support; test.support._crash_python()'
crash_test = self.create_test(name="crash", code=code)
ok_test = self.create_test(name="ok")

Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ def test_child_terminated_in_stopped_state(self):

code = textwrap.dedent("""
import ctypes
from test.support import SuppressCrashReport
from test.support import _crash_python

libc = ctypes.CDLL({libc_name!r})
libc.ptrace({PTRACE_TRACEME}, 0, 0)
Expand All @@ -1288,9 +1288,8 @@ def test_child_terminated_in_stopped_state(self):
raise unittest.SkipTest('ptrace() failed - unable to test')

code += textwrap.dedent("""
with SuppressCrashReport():
# Crash the process
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
# Crash the process
_crash_python()
""")
child = subprocess.Popen([sys.executable, '-c', code])
try:
Expand Down