Close #22175: Improve test_faulthandler readability with dedent.

Patch written by Xavier de Gaye.
This commit is contained in:
Victor Stinner 2014-08-10 19:50:08 +02:00
parent ac191ce1d3
commit 6d201685e4
1 changed files with 165 additions and 163 deletions

View File

@ -10,6 +10,7 @@ from test import support, script_helper
from test.script_helper import assert_python_ok
import tempfile
import unittest
from textwrap import dedent
try:
import threading
@ -47,6 +48,7 @@ class FaultHandlerTests(unittest.TestCase):
build, and replace "Current thread 0x00007f8d8fbd9700" by "Current
thread XXX".
"""
code = dedent(code).strip()
with support.SuppressCrashReport():
process = script_helper.spawn_python('-c', code)
stdout, stderr = process.communicate()
@ -80,11 +82,11 @@ class FaultHandlerTests(unittest.TestCase):
{header}:
File "<string>", line {lineno} in <module>
""".strip()
regex = regex.format(
"""
regex = dedent(regex.format(
lineno=line_number,
name=name_regex,
header=re.escape(header))
header=re.escape(header))).strip()
if other_regex:
regex += '|' + other_regex
output, exitcode = self.get_output(code, filename)
@ -99,7 +101,7 @@ class FaultHandlerTests(unittest.TestCase):
import faulthandler
faulthandler.enable()
faulthandler._read_null()
""".strip(),
""",
3,
# Issue #12700: Read NULL raises SIGILL on Mac OS X Lion
'(?:Segmentation fault|Bus error|Illegal instruction)')
@ -109,7 +111,7 @@ faulthandler._read_null()
import faulthandler
faulthandler.enable()
faulthandler._sigsegv()
""".strip(),
""",
3,
'Segmentation fault')
@ -118,7 +120,7 @@ faulthandler._sigsegv()
import faulthandler
faulthandler.enable()
faulthandler._sigabrt()
""".strip(),
""",
3,
'Aborted')
@ -129,7 +131,7 @@ faulthandler._sigabrt()
import faulthandler
faulthandler.enable()
faulthandler._sigfpe()
""".strip(),
""",
3,
'Floating point exception')
@ -140,7 +142,7 @@ faulthandler._sigfpe()
import faulthandler
faulthandler.enable()
faulthandler._sigbus()
""".strip(),
""",
3,
'Bus error')
@ -151,7 +153,7 @@ faulthandler._sigbus()
import faulthandler
faulthandler.enable()
faulthandler._sigill()
""".strip(),
""",
3,
'Illegal instruction')
@ -159,7 +161,7 @@ faulthandler._sigill()
self.check_fatal_error("""
import faulthandler
faulthandler._fatal_error(b'xyz')
""".strip(),
""",
2,
'xyz')
@ -173,7 +175,7 @@ faulthandler._fatal_error(b'xyz')
import faulthandler
faulthandler.enable()
faulthandler._stack_overflow()
""".strip(),
""",
3,
'(?:Segmentation fault|Bus error)',
other_regex='unable to raise a stack overflow')
@ -183,7 +185,7 @@ faulthandler._stack_overflow()
import faulthandler
faulthandler.enable()
faulthandler._read_null(True)
""".strip(),
""",
3,
'(?:Segmentation fault|Bus error|Illegal instruction)')
@ -194,7 +196,7 @@ import faulthandler
output = open({filename}, 'wb')
faulthandler.enable(output)
faulthandler._sigsegv()
""".strip().format(filename=repr(filename)),
""".format(filename=repr(filename)),
4,
'Segmentation fault',
filename=filename)
@ -204,7 +206,7 @@ faulthandler._sigsegv()
import faulthandler
faulthandler.enable(all_threads=False)
faulthandler._sigsegv()
""".strip(),
""",
3,
'Segmentation fault',
all_threads=False)
@ -215,7 +217,7 @@ import faulthandler
faulthandler.enable()
faulthandler.disable()
faulthandler._sigsegv()
""".strip()
"""
not_expected = 'Fatal Python error'
stderr, exitcode = self.get_output(code)
stder = '\n'.join(stderr)
@ -296,7 +298,7 @@ def funcA():
funcB()
funcA()
""".strip()
"""
code = code.format(
filename=repr(filename),
has_filename=bool(filename),
@ -333,7 +335,7 @@ def {func_name}():
faulthandler.dump_traceback(all_threads=False)
{func_name}()
""".strip()
"""
code = code.format(
func_name=func_name,
)
@ -383,7 +385,7 @@ waiter.running.wait()
dump()
waiter.stop.set()
waiter.join()
""".strip()
"""
code = code.format(filename=repr(filename))
output, exitcode = self.get_output(code, filename)
output = '\n'.join(output)
@ -401,8 +403,8 @@ waiter.join()
Current thread XXX \(most recent call first\):
File "<string>", line {lineno} in dump
File "<string>", line 28 in <module>$
""".strip()
regex = regex.format(lineno=lineno)
"""
regex = dedent(regex.format(lineno=lineno)).strip()
self.assertRegex(output, regex)
self.assertEqual(exitcode, 0)
@ -445,7 +447,7 @@ else:
func(timeout, repeat, cancel, file, loops)
if file is not None:
file.close()
""".strip()
"""
code = code.format(
timeout=TIMEOUT,
repeat=repeat,
@ -550,7 +552,7 @@ if chain and not handler.called:
if file is not None:
file.close()
sys.exit(exitcode)
""".strip()
"""
code = code.format(
filename=repr(filename),
has_filename=bool(filename),