Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1 | from contextlib import contextmanager |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 2 | import datetime |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 3 | import faulthandler |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 4 | import os |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 5 | import re |
| 6 | import signal |
| 7 | import subprocess |
| 8 | import sys |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 9 | from test import support |
| 10 | from test.support import script_helper |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 11 | import tempfile |
| 12 | import unittest |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 13 | from textwrap import dedent |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 14 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 15 | try: |
| 16 | import threading |
| 17 | HAVE_THREADS = True |
| 18 | except ImportError: |
| 19 | HAVE_THREADS = False |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 20 | try: |
| 21 | import _testcapi |
| 22 | except ImportError: |
| 23 | _testcapi = None |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 24 | |
Victor Stinner | 44378d4 | 2011-04-01 15:37:12 +0200 | [diff] [blame] | 25 | TIMEOUT = 0.5 |
| 26 | |
Victor Stinner | 301f3f0 | 2011-06-01 13:49:12 +0200 | [diff] [blame] | 27 | def expected_traceback(lineno1, lineno2, header, min_count=1): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 28 | regex = header |
Victor Stinner | 7ad24e9 | 2011-03-31 22:35:49 +0200 | [diff] [blame] | 29 | regex += ' File "<string>", line %s in func\n' % lineno1 |
| 30 | regex += ' File "<string>", line %s in <module>' % lineno2 |
Victor Stinner | 301f3f0 | 2011-06-01 13:49:12 +0200 | [diff] [blame] | 31 | if 1 < min_count: |
| 32 | return '^' + (regex + '\n') * (min_count - 1) + regex |
| 33 | else: |
| 34 | return '^' + regex + '$' |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 35 | |
| 36 | @contextmanager |
| 37 | def temporary_filename(): |
| 38 | filename = tempfile.mktemp() |
| 39 | try: |
| 40 | yield filename |
| 41 | finally: |
| 42 | support.unlink(filename) |
| 43 | |
| 44 | class FaultHandlerTests(unittest.TestCase): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 45 | def get_output(self, code, filename=None, fd=None): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 46 | """ |
| 47 | Run the specified code in Python (in a new child process) and read the |
| 48 | output from the standard error or from a file (if filename is set). |
| 49 | Return the output lines as a list. |
| 50 | |
| 51 | Strip the reference count from the standard error for Python debug |
| 52 | build, and replace "Current thread 0x00007f8d8fbd9700" by "Current |
| 53 | thread XXX". |
| 54 | """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 55 | code = dedent(code).strip() |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 56 | pass_fds = [] |
| 57 | if fd is not None: |
| 58 | pass_fds.append(fd) |
Antoine Pitrou | 77e904e | 2013-10-08 23:04:32 +0200 | [diff] [blame] | 59 | with support.SuppressCrashReport(): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 60 | process = script_helper.spawn_python('-c', code, pass_fds=pass_fds) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 61 | stdout, stderr = process.communicate() |
| 62 | exitcode = process.wait() |
Victor Stinner | 1940233 | 2011-03-31 18:15:52 +0200 | [diff] [blame] | 63 | output = support.strip_python_stderr(stdout) |
| 64 | output = output.decode('ascii', 'backslashreplace') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 65 | if filename: |
Victor Stinner | 1940233 | 2011-03-31 18:15:52 +0200 | [diff] [blame] | 66 | self.assertEqual(output, '') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 67 | with open(filename, "rb") as fp: |
| 68 | output = fp.read() |
Victor Stinner | 1940233 | 2011-03-31 18:15:52 +0200 | [diff] [blame] | 69 | output = output.decode('ascii', 'backslashreplace') |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 70 | elif fd is not None: |
| 71 | self.assertEqual(output, '') |
| 72 | os.lseek(fd, os.SEEK_SET, 0) |
| 73 | with open(fd, "rb", closefd=False) as fp: |
| 74 | output = fp.read() |
| 75 | output = output.decode('ascii', 'backslashreplace') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 76 | output = re.sub('Current thread 0x[0-9a-f]+', |
| 77 | 'Current thread XXX', |
| 78 | output) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 79 | return output.splitlines(), exitcode |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 80 | |
| 81 | def check_fatal_error(self, code, line_number, name_regex, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 82 | filename=None, all_threads=True, other_regex=None, |
| 83 | fd=None): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 84 | """ |
| 85 | Check that the fault handler for fatal errors is enabled and check the |
| 86 | traceback from the child process output. |
| 87 | |
| 88 | Raise an error if the output doesn't match the expected format. |
| 89 | """ |
| 90 | if all_threads: |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 91 | header = 'Current thread XXX (most recent call first)' |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 92 | else: |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 93 | header = 'Stack (most recent call first)' |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 94 | regex = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 95 | ^Fatal Python error: {name} |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 96 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 97 | {header}: |
| 98 | File "<string>", line {lineno} in <module> |
| 99 | """ |
| 100 | regex = dedent(regex.format( |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 101 | lineno=line_number, |
| 102 | name=name_regex, |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 103 | header=re.escape(header))).strip() |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 104 | if other_regex: |
| 105 | regex += '|' + other_regex |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 106 | output, exitcode = self.get_output(code, filename=filename, fd=fd) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 107 | output = '\n'.join(output) |
| 108 | self.assertRegex(output, regex) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 109 | self.assertNotEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 110 | |
Victor Stinner | 330426c | 2013-07-03 22:29:42 +0200 | [diff] [blame] | 111 | @unittest.skipIf(sys.platform.startswith('aix'), |
| 112 | "the first page of memory is a mapped read-only on AIX") |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 113 | def test_read_null(self): |
| 114 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 115 | import faulthandler |
| 116 | faulthandler.enable() |
| 117 | faulthandler._read_null() |
| 118 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 119 | 3, |
Victor Stinner | a8db378 | 2011-08-08 22:43:45 +0200 | [diff] [blame] | 120 | # Issue #12700: Read NULL raises SIGILL on Mac OS X Lion |
| 121 | '(?:Segmentation fault|Bus error|Illegal instruction)') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 122 | |
| 123 | def test_sigsegv(self): |
| 124 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 125 | import faulthandler |
| 126 | faulthandler.enable() |
| 127 | faulthandler._sigsegv() |
| 128 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 129 | 3, |
| 130 | 'Segmentation fault') |
| 131 | |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 132 | def test_sigabrt(self): |
| 133 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 134 | import faulthandler |
| 135 | faulthandler.enable() |
| 136 | faulthandler._sigabrt() |
| 137 | """, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 138 | 3, |
| 139 | 'Aborted') |
| 140 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 141 | @unittest.skipIf(sys.platform == 'win32', |
| 142 | "SIGFPE cannot be caught on Windows") |
| 143 | def test_sigfpe(self): |
| 144 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 145 | import faulthandler |
| 146 | faulthandler.enable() |
| 147 | faulthandler._sigfpe() |
| 148 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 149 | 3, |
| 150 | 'Floating point exception') |
| 151 | |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 152 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 153 | @unittest.skipUnless(hasattr(signal, 'SIGBUS'), 'need signal.SIGBUS') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 154 | def test_sigbus(self): |
| 155 | self.check_fatal_error(""" |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 156 | import _testcapi |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 157 | import faulthandler |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 158 | import signal |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 159 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 160 | faulthandler.enable() |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 161 | _testcapi.raise_signal(signal.SIGBUS) |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 162 | """, |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 163 | 6, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 164 | 'Bus error') |
| 165 | |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 166 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 167 | @unittest.skipUnless(hasattr(signal, 'SIGILL'), 'need signal.SIGILL') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 168 | def test_sigill(self): |
| 169 | self.check_fatal_error(""" |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 170 | import _testcapi |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 171 | import faulthandler |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 172 | import signal |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 173 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 174 | faulthandler.enable() |
Victor Stinner | 68e0808 | 2014-08-10 19:51:05 +0200 | [diff] [blame] | 175 | _testcapi.raise_signal(signal.SIGILL) |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 176 | """, |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 177 | 6, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 178 | 'Illegal instruction') |
| 179 | |
| 180 | def test_fatal_error(self): |
| 181 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 182 | import faulthandler |
| 183 | faulthandler._fatal_error(b'xyz') |
| 184 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 185 | 2, |
| 186 | 'xyz') |
| 187 | |
Victor Stinner | 57003f8 | 2016-03-15 17:23:35 +0100 | [diff] [blame] | 188 | def test_fatal_error_without_gil(self): |
| 189 | self.check_fatal_error(""" |
| 190 | import faulthandler |
| 191 | faulthandler._fatal_error(b'xyz', True) |
| 192 | """, |
| 193 | 2, |
| 194 | 'xyz') |
| 195 | |
Charles-François Natali | 3391e64 | 2011-09-01 23:08:21 +0200 | [diff] [blame] | 196 | @unittest.skipIf(sys.platform.startswith('openbsd') and HAVE_THREADS, |
| 197 | "Issue #12868: sigaltstack() doesn't work on " |
| 198 | "OpenBSD if Python is compiled with pthread") |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 199 | @unittest.skipIf(not hasattr(faulthandler, '_stack_overflow'), |
| 200 | 'need faulthandler._stack_overflow()') |
| 201 | def test_stack_overflow(self): |
| 202 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 203 | import faulthandler |
| 204 | faulthandler.enable() |
| 205 | faulthandler._stack_overflow() |
| 206 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 207 | 3, |
Victor Stinner | f048075 | 2011-03-31 11:34:08 +0200 | [diff] [blame] | 208 | '(?:Segmentation fault|Bus error)', |
| 209 | other_regex='unable to raise a stack overflow') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 210 | |
| 211 | def test_gil_released(self): |
| 212 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 213 | import faulthandler |
| 214 | faulthandler.enable() |
Victor Stinner | 5083828 | 2014-09-30 13:54:14 +0200 | [diff] [blame] | 215 | faulthandler._sigsegv(True) |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 216 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 217 | 3, |
Victor Stinner | 5083828 | 2014-09-30 13:54:14 +0200 | [diff] [blame] | 218 | 'Segmentation fault') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 219 | |
| 220 | def test_enable_file(self): |
| 221 | with temporary_filename() as filename: |
| 222 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 223 | import faulthandler |
| 224 | output = open({filename}, 'wb') |
| 225 | faulthandler.enable(output) |
| 226 | faulthandler._sigsegv() |
| 227 | """.format(filename=repr(filename)), |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 228 | 4, |
Victor Stinner | 5678539 | 2013-06-17 23:37:59 +0200 | [diff] [blame] | 229 | 'Segmentation fault', |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 230 | filename=filename) |
| 231 | |
Victor Stinner | ff2a661 | 2015-03-13 11:01:30 +0100 | [diff] [blame] | 232 | @unittest.skipIf(sys.platform == "win32", |
| 233 | "subprocess doesn't support pass_fds on Windows") |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 234 | def test_enable_fd(self): |
| 235 | with tempfile.TemporaryFile('wb+') as fp: |
| 236 | fd = fp.fileno() |
| 237 | self.check_fatal_error(""" |
| 238 | import faulthandler |
| 239 | import sys |
| 240 | faulthandler.enable(%s) |
| 241 | faulthandler._sigsegv() |
| 242 | """ % fd, |
| 243 | 4, |
| 244 | 'Segmentation fault', |
| 245 | fd=fd) |
| 246 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 247 | def test_enable_single_thread(self): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 248 | self.check_fatal_error(""" |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 249 | import faulthandler |
| 250 | faulthandler.enable(all_threads=False) |
| 251 | faulthandler._sigsegv() |
| 252 | """, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 253 | 3, |
Victor Stinner | 5678539 | 2013-06-17 23:37:59 +0200 | [diff] [blame] | 254 | 'Segmentation fault', |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 255 | all_threads=False) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 256 | |
| 257 | def test_disable(self): |
| 258 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 259 | import faulthandler |
| 260 | faulthandler.enable() |
| 261 | faulthandler.disable() |
| 262 | faulthandler._sigsegv() |
| 263 | """ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 264 | not_expected = 'Fatal Python error' |
Antoine Pitrou | 77e904e | 2013-10-08 23:04:32 +0200 | [diff] [blame] | 265 | stderr, exitcode = self.get_output(code) |
Victor Stinner | 29001c8 | 2014-09-25 00:38:48 +0200 | [diff] [blame] | 266 | stderr = '\n'.join(stderr) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 267 | self.assertTrue(not_expected not in stderr, |
| 268 | "%r is present in %r" % (not_expected, stderr)) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 269 | self.assertNotEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 270 | |
| 271 | def test_is_enabled(self): |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 272 | orig_stderr = sys.stderr |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 273 | try: |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 274 | # regrtest may replace sys.stderr by io.StringIO object, but |
| 275 | # faulthandler.enable() requires that sys.stderr has a fileno() |
| 276 | # method |
Victor Stinner | 7248850 | 2011-06-29 23:24:31 +0200 | [diff] [blame] | 277 | sys.stderr = sys.__stderr__ |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 278 | |
| 279 | was_enabled = faulthandler.is_enabled() |
| 280 | try: |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 281 | faulthandler.enable() |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 282 | self.assertTrue(faulthandler.is_enabled()) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 283 | faulthandler.disable() |
Victor Stinner | 7e32f3a | 2011-06-29 13:44:05 +0200 | [diff] [blame] | 284 | self.assertFalse(faulthandler.is_enabled()) |
| 285 | finally: |
| 286 | if was_enabled: |
| 287 | faulthandler.enable() |
| 288 | else: |
| 289 | faulthandler.disable() |
| 290 | finally: |
| 291 | sys.stderr = orig_stderr |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 292 | |
Victor Stinner | d5698cb | 2012-07-31 02:55:49 +0200 | [diff] [blame] | 293 | def test_disabled_by_default(self): |
| 294 | # By default, the module should be disabled |
| 295 | code = "import faulthandler; print(faulthandler.is_enabled())" |
Gregory P. Smith | fe7c5d6 | 2015-01-22 17:33:28 -0800 | [diff] [blame] | 296 | args = filter(None, (sys.executable, |
| 297 | "-E" if sys.flags.ignore_environment else "", |
| 298 | "-c", code)) |
| 299 | env = os.environ.copy() |
| 300 | env.pop("PYTHONFAULTHANDLER", None) |
| 301 | # don't use assert_python_ok() because it always enables faulthandler |
| 302 | output = subprocess.check_output(args, env=env) |
Victor Stinner | 8898350 | 2013-09-08 11:36:23 +0200 | [diff] [blame] | 303 | self.assertEqual(output.rstrip(), b"False") |
Victor Stinner | d5698cb | 2012-07-31 02:55:49 +0200 | [diff] [blame] | 304 | |
| 305 | def test_sys_xoptions(self): |
| 306 | # Test python -X faulthandler |
| 307 | code = "import faulthandler; print(faulthandler.is_enabled())" |
Gregory P. Smith | fe7c5d6 | 2015-01-22 17:33:28 -0800 | [diff] [blame] | 308 | args = filter(None, (sys.executable, |
| 309 | "-E" if sys.flags.ignore_environment else "", |
| 310 | "-X", "faulthandler", "-c", code)) |
| 311 | env = os.environ.copy() |
| 312 | env.pop("PYTHONFAULTHANDLER", None) |
| 313 | # don't use assert_python_ok() because it always enables faulthandler |
| 314 | output = subprocess.check_output(args, env=env) |
Victor Stinner | 8898350 | 2013-09-08 11:36:23 +0200 | [diff] [blame] | 315 | self.assertEqual(output.rstrip(), b"True") |
| 316 | |
| 317 | def test_env_var(self): |
| 318 | # empty env var |
| 319 | code = "import faulthandler; print(faulthandler.is_enabled())" |
| 320 | args = (sys.executable, "-c", code) |
| 321 | env = os.environ.copy() |
| 322 | env['PYTHONFAULTHANDLER'] = '' |
Gregory P. Smith | fe7c5d6 | 2015-01-22 17:33:28 -0800 | [diff] [blame] | 323 | # don't use assert_python_ok() because it always enables faulthandler |
Victor Stinner | 8898350 | 2013-09-08 11:36:23 +0200 | [diff] [blame] | 324 | output = subprocess.check_output(args, env=env) |
| 325 | self.assertEqual(output.rstrip(), b"False") |
| 326 | |
| 327 | # non-empty env var |
| 328 | env = os.environ.copy() |
| 329 | env['PYTHONFAULTHANDLER'] = '1' |
| 330 | output = subprocess.check_output(args, env=env) |
| 331 | self.assertEqual(output.rstrip(), b"True") |
Victor Stinner | d5698cb | 2012-07-31 02:55:49 +0200 | [diff] [blame] | 332 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 333 | def check_dump_traceback(self, *, filename=None, fd=None): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 334 | """ |
| 335 | Explicitly call dump_traceback() function and check its output. |
| 336 | Raise an error if the output doesn't match the expected format. |
| 337 | """ |
| 338 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 339 | import faulthandler |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 340 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 341 | filename = {filename!r} |
| 342 | fd = {fd} |
| 343 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 344 | def funcB(): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 345 | if filename: |
| 346 | with open(filename, "wb") as fp: |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 347 | faulthandler.dump_traceback(fp, all_threads=False) |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 348 | elif fd is not None: |
| 349 | faulthandler.dump_traceback(fd, |
| 350 | all_threads=False) |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 351 | else: |
| 352 | faulthandler.dump_traceback(all_threads=False) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 353 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 354 | def funcA(): |
| 355 | funcB() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 356 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 357 | funcA() |
| 358 | """ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 359 | code = code.format( |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 360 | filename=filename, |
| 361 | fd=fd, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 362 | ) |
| 363 | if filename: |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 364 | lineno = 9 |
| 365 | elif fd is not None: |
| 366 | lineno = 12 |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 367 | else: |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 368 | lineno = 14 |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 369 | expected = [ |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 370 | 'Stack (most recent call first):', |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 371 | ' File "<string>", line %s in funcB' % lineno, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 372 | ' File "<string>", line 17 in funcA', |
| 373 | ' File "<string>", line 19 in <module>' |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 374 | ] |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 375 | trace, exitcode = self.get_output(code, filename, fd) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 376 | self.assertEqual(trace, expected) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 377 | self.assertEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 378 | |
| 379 | def test_dump_traceback(self): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 380 | self.check_dump_traceback() |
Victor Stinner | 1940233 | 2011-03-31 18:15:52 +0200 | [diff] [blame] | 381 | |
| 382 | def test_dump_traceback_file(self): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 383 | with temporary_filename() as filename: |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 384 | self.check_dump_traceback(filename=filename) |
| 385 | |
Victor Stinner | ff2a661 | 2015-03-13 11:01:30 +0100 | [diff] [blame] | 386 | @unittest.skipIf(sys.platform == "win32", |
| 387 | "subprocess doesn't support pass_fds on Windows") |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 388 | def test_dump_traceback_fd(self): |
| 389 | with tempfile.TemporaryFile('wb+') as fp: |
| 390 | self.check_dump_traceback(fd=fp.fileno()) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 391 | |
Victor Stinner | 53386d8 | 2012-08-01 19:45:34 +0200 | [diff] [blame] | 392 | def test_truncate(self): |
| 393 | maxlen = 500 |
| 394 | func_name = 'x' * (maxlen + 50) |
| 395 | truncated = 'x' * maxlen + '...' |
| 396 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 397 | import faulthandler |
Victor Stinner | 53386d8 | 2012-08-01 19:45:34 +0200 | [diff] [blame] | 398 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 399 | def {func_name}(): |
| 400 | faulthandler.dump_traceback(all_threads=False) |
Victor Stinner | 53386d8 | 2012-08-01 19:45:34 +0200 | [diff] [blame] | 401 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 402 | {func_name}() |
| 403 | """ |
Victor Stinner | 53386d8 | 2012-08-01 19:45:34 +0200 | [diff] [blame] | 404 | code = code.format( |
| 405 | func_name=func_name, |
| 406 | ) |
| 407 | expected = [ |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 408 | 'Stack (most recent call first):', |
Victor Stinner | 53386d8 | 2012-08-01 19:45:34 +0200 | [diff] [blame] | 409 | ' File "<string>", line 4 in %s' % truncated, |
| 410 | ' File "<string>", line 6 in <module>' |
| 411 | ] |
| 412 | trace, exitcode = self.get_output(code) |
| 413 | self.assertEqual(trace, expected) |
| 414 | self.assertEqual(exitcode, 0) |
| 415 | |
Victor Stinner | ff4cd88 | 2011-04-07 11:50:25 +0200 | [diff] [blame] | 416 | @unittest.skipIf(not HAVE_THREADS, 'need threads') |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 417 | def check_dump_traceback_threads(self, filename): |
| 418 | """ |
| 419 | Call explicitly dump_traceback(all_threads=True) and check the output. |
| 420 | Raise an error if the output doesn't match the expected format. |
| 421 | """ |
| 422 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 423 | import faulthandler |
| 424 | from threading import Thread, Event |
| 425 | import time |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 426 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 427 | def dump(): |
| 428 | if {filename}: |
| 429 | with open({filename}, "wb") as fp: |
| 430 | faulthandler.dump_traceback(fp, all_threads=True) |
| 431 | else: |
| 432 | faulthandler.dump_traceback(all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 433 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 434 | class Waiter(Thread): |
| 435 | # avoid blocking if the main thread raises an exception. |
| 436 | daemon = True |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 437 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 438 | def __init__(self): |
| 439 | Thread.__init__(self) |
| 440 | self.running = Event() |
| 441 | self.stop = Event() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 442 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 443 | def run(self): |
| 444 | self.running.set() |
| 445 | self.stop.wait() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 446 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 447 | waiter = Waiter() |
| 448 | waiter.start() |
| 449 | waiter.running.wait() |
| 450 | dump() |
| 451 | waiter.stop.set() |
| 452 | waiter.join() |
| 453 | """ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 454 | code = code.format(filename=repr(filename)) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 455 | output, exitcode = self.get_output(code, filename) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 456 | output = '\n'.join(output) |
| 457 | if filename: |
| 458 | lineno = 8 |
| 459 | else: |
| 460 | lineno = 10 |
| 461 | regex = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 462 | ^Thread 0x[0-9a-f]+ \(most recent call first\): |
| 463 | (?: File ".*threading.py", line [0-9]+ in [_a-z]+ |
| 464 | ){{1,3}} File "<string>", line 23 in run |
| 465 | File ".*threading.py", line [0-9]+ in _bootstrap_inner |
| 466 | File ".*threading.py", line [0-9]+ in _bootstrap |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 467 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 468 | Current thread XXX \(most recent call first\): |
| 469 | File "<string>", line {lineno} in dump |
| 470 | File "<string>", line 28 in <module>$ |
| 471 | """ |
| 472 | regex = dedent(regex.format(lineno=lineno)).strip() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 473 | self.assertRegex(output, regex) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 474 | self.assertEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 475 | |
| 476 | def test_dump_traceback_threads(self): |
| 477 | self.check_dump_traceback_threads(None) |
Victor Stinner | 1940233 | 2011-03-31 18:15:52 +0200 | [diff] [blame] | 478 | |
| 479 | def test_dump_traceback_threads_file(self): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 480 | with temporary_filename() as filename: |
| 481 | self.check_dump_traceback_threads(filename) |
| 482 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 483 | @unittest.skipIf(not hasattr(faulthandler, 'dump_traceback_later'), |
| 484 | 'need faulthandler.dump_traceback_later()') |
| 485 | def check_dump_traceback_later(self, repeat=False, cancel=False, loops=1, |
| 486 | *, filename=None, fd=None): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 487 | """ |
| 488 | Check how many times the traceback is written in timeout x 2.5 seconds, |
| 489 | or timeout x 3.5 seconds if cancel is True: 1, 2 or 3 times depending |
| 490 | on repeat and cancel options. |
| 491 | |
| 492 | Raise an error if the output doesn't match the expect format. |
| 493 | """ |
Victor Stinner | c790a53 | 2011-04-08 13:39:59 +0200 | [diff] [blame] | 494 | timeout_str = str(datetime.timedelta(seconds=TIMEOUT)) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 495 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 496 | import faulthandler |
| 497 | import time |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 498 | import sys |
| 499 | |
| 500 | timeout = {timeout} |
| 501 | repeat = {repeat} |
| 502 | cancel = {cancel} |
| 503 | loops = {loops} |
| 504 | filename = {filename!r} |
| 505 | fd = {fd} |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 506 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 507 | def func(timeout, repeat, cancel, file, loops): |
| 508 | for loop in range(loops): |
| 509 | faulthandler.dump_traceback_later(timeout, repeat=repeat, file=file) |
| 510 | if cancel: |
| 511 | faulthandler.cancel_dump_traceback_later() |
| 512 | time.sleep(timeout * 5) |
| 513 | faulthandler.cancel_dump_traceback_later() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 514 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 515 | if filename: |
| 516 | file = open(filename, "wb") |
| 517 | elif fd is not None: |
| 518 | file = sys.stderr.fileno() |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 519 | else: |
| 520 | file = None |
| 521 | func(timeout, repeat, cancel, file, loops) |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 522 | if filename: |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 523 | file.close() |
| 524 | """ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 525 | code = code.format( |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 526 | timeout=TIMEOUT, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 527 | repeat=repeat, |
| 528 | cancel=cancel, |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 529 | loops=loops, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 530 | filename=filename, |
| 531 | fd=fd, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 532 | ) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 533 | trace, exitcode = self.get_output(code, filename) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 534 | trace = '\n'.join(trace) |
| 535 | |
Victor Stinner | f77ccc6 | 2011-04-03 18:45:42 +0200 | [diff] [blame] | 536 | if not cancel: |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 537 | count = loops |
Victor Stinner | f77ccc6 | 2011-04-03 18:45:42 +0200 | [diff] [blame] | 538 | if repeat: |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 539 | count *= 2 |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 540 | header = r'Timeout \(%s\)!\nThread 0x[0-9a-f]+ \(most recent call first\):\n' % timeout_str |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 541 | regex = expected_traceback(17, 26, header, min_count=count) |
Victor Stinner | f77ccc6 | 2011-04-03 18:45:42 +0200 | [diff] [blame] | 542 | self.assertRegex(trace, regex) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 543 | else: |
Victor Stinner | f77ccc6 | 2011-04-03 18:45:42 +0200 | [diff] [blame] | 544 | self.assertEqual(trace, '') |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 545 | self.assertEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 546 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 547 | def test_dump_traceback_later(self): |
| 548 | self.check_dump_traceback_later() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 549 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 550 | def test_dump_traceback_later_repeat(self): |
| 551 | self.check_dump_traceback_later(repeat=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 552 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 553 | def test_dump_traceback_later_cancel(self): |
| 554 | self.check_dump_traceback_later(cancel=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 555 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 556 | def test_dump_traceback_later_file(self): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 557 | with temporary_filename() as filename: |
| 558 | self.check_dump_traceback_later(filename=filename) |
| 559 | |
Victor Stinner | ff2a661 | 2015-03-13 11:01:30 +0100 | [diff] [blame] | 560 | @unittest.skipIf(sys.platform == "win32", |
| 561 | "subprocess doesn't support pass_fds on Windows") |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 562 | def test_dump_traceback_later_fd(self): |
| 563 | with tempfile.TemporaryFile('wb+') as fp: |
| 564 | self.check_dump_traceback_later(fd=fp.fileno()) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 565 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 566 | def test_dump_traceback_later_twice(self): |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 567 | self.check_dump_traceback_later(loops=2) |
Victor Stinner | de10f40 | 2011-04-08 12:57:06 +0200 | [diff] [blame] | 568 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 569 | @unittest.skipIf(not hasattr(faulthandler, "register"), |
| 570 | "need faulthandler.register") |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 571 | def check_register(self, filename=False, all_threads=False, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 572 | unregister=False, chain=False, fd=None): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 573 | """ |
| 574 | Register a handler displaying the traceback on a user signal. Raise the |
| 575 | signal and check the written traceback. |
| 576 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 577 | If chain is True, check that the previous signal handler is called. |
| 578 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 579 | Raise an error if the output doesn't match the expected format. |
| 580 | """ |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 581 | signum = signal.SIGUSR1 |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 582 | code = """ |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 583 | import faulthandler |
| 584 | import os |
| 585 | import signal |
| 586 | import sys |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 587 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 588 | all_threads = {all_threads} |
| 589 | signum = {signum} |
| 590 | unregister = {unregister} |
| 591 | chain = {chain} |
| 592 | filename = {filename!r} |
| 593 | fd = {fd} |
| 594 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 595 | def func(signum): |
| 596 | os.kill(os.getpid(), signum) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 597 | |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 598 | def handler(signum, frame): |
| 599 | handler.called = True |
| 600 | handler.called = False |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 601 | |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 602 | if filename: |
| 603 | file = open(filename, "wb") |
| 604 | elif fd is not None: |
| 605 | file = sys.stderr.fileno() |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 606 | else: |
| 607 | file = None |
| 608 | if chain: |
| 609 | signal.signal(signum, handler) |
| 610 | faulthandler.register(signum, file=file, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 611 | all_threads=all_threads, chain={chain}) |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 612 | if unregister: |
| 613 | faulthandler.unregister(signum) |
| 614 | func(signum) |
| 615 | if chain and not handler.called: |
| 616 | if file is not None: |
| 617 | output = file |
| 618 | else: |
| 619 | output = sys.stderr |
| 620 | print("Error: signal handler not called!", file=output) |
| 621 | exitcode = 1 |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 622 | else: |
| 623 | exitcode = 0 |
| 624 | if filename: |
Victor Stinner | 6d20168 | 2014-08-10 19:50:08 +0200 | [diff] [blame] | 625 | file.close() |
| 626 | sys.exit(exitcode) |
| 627 | """ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 628 | code = code.format( |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 629 | all_threads=all_threads, |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 630 | signum=signum, |
| 631 | unregister=unregister, |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 632 | chain=chain, |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 633 | filename=filename, |
| 634 | fd=fd, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 635 | ) |
Victor Stinner | 05585cb | 2011-03-31 13:29:56 +0200 | [diff] [blame] | 636 | trace, exitcode = self.get_output(code, filename) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 637 | trace = '\n'.join(trace) |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 638 | if not unregister: |
| 639 | if all_threads: |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 640 | regex = 'Current thread XXX \(most recent call first\):\n' |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 641 | else: |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 642 | regex = 'Stack \(most recent call first\):\n' |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 643 | regex = expected_traceback(14, 32, regex) |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 644 | self.assertRegex(trace, regex) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 645 | else: |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 646 | self.assertEqual(trace, '') |
| 647 | if unregister: |
| 648 | self.assertNotEqual(exitcode, 0) |
| 649 | else: |
| 650 | self.assertEqual(exitcode, 0) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 651 | |
| 652 | def test_register(self): |
| 653 | self.check_register() |
| 654 | |
Victor Stinner | a01ca12 | 2011-04-01 12:56:17 +0200 | [diff] [blame] | 655 | def test_unregister(self): |
| 656 | self.check_register(unregister=True) |
| 657 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 658 | def test_register_file(self): |
| 659 | with temporary_filename() as filename: |
| 660 | self.check_register(filename=filename) |
| 661 | |
Victor Stinner | ff2a661 | 2015-03-13 11:01:30 +0100 | [diff] [blame] | 662 | @unittest.skipIf(sys.platform == "win32", |
| 663 | "subprocess doesn't support pass_fds on Windows") |
Victor Stinner | 95bb714 | 2015-03-12 15:32:03 +0100 | [diff] [blame] | 664 | def test_register_fd(self): |
| 665 | with tempfile.TemporaryFile('wb+') as fp: |
| 666 | self.check_register(fd=fp.fileno()) |
| 667 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 668 | def test_register_threads(self): |
| 669 | self.check_register(all_threads=True) |
| 670 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 671 | def test_register_chain(self): |
| 672 | self.check_register(chain=True) |
| 673 | |
Victor Stinner | e2d6690 | 2014-05-14 17:15:50 +0200 | [diff] [blame] | 674 | @contextmanager |
| 675 | def check_stderr_none(self): |
| 676 | stderr = sys.stderr |
| 677 | try: |
| 678 | sys.stderr = None |
| 679 | with self.assertRaises(RuntimeError) as cm: |
| 680 | yield |
| 681 | self.assertEqual(str(cm.exception), "sys.stderr is None") |
| 682 | finally: |
| 683 | sys.stderr = stderr |
| 684 | |
| 685 | def test_stderr_None(self): |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 686 | # Issue #21497: provide a helpful error if sys.stderr is None, |
Victor Stinner | e2d6690 | 2014-05-14 17:15:50 +0200 | [diff] [blame] | 687 | # instead of just an attribute error: "None has no attribute fileno". |
| 688 | with self.check_stderr_none(): |
| 689 | faulthandler.enable() |
| 690 | with self.check_stderr_none(): |
| 691 | faulthandler.dump_traceback() |
| 692 | if hasattr(faulthandler, 'dump_traceback_later'): |
| 693 | with self.check_stderr_none(): |
| 694 | faulthandler.dump_traceback_later(1e-3) |
| 695 | if hasattr(faulthandler, "register"): |
| 696 | with self.check_stderr_none(): |
| 697 | faulthandler.register(signal.SIGUSR1) |
| 698 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 699 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 700 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 701 | unittest.main() |