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