Skip to content

Commit 027ae81

Browse files
committed
Switch to using Pytest's terminal reporter
Signed-off-by: Pedro Algarvio <[email protected]>
1 parent bc09f80 commit 027ae81

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

pytest_timeout.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99
import inspect
1010
import os
11-
import shutil
1211
import signal
1312
import sys
1413
import threading
@@ -445,11 +444,12 @@ def timeout_sigalrm(item, settings):
445444
return
446445
__tracebackhide__ = True
447446
nthreads = len(threading.enumerate())
447+
terminal = item.config.get_terminal_writer()
448448
if nthreads > 1:
449-
write_title("Timeout", sep="+")
450-
dump_stacks()
449+
terminal.sep("+", title="Timeout")
450+
dump_stacks(terminal)
451451
if nthreads > 1:
452-
write_title("Timeout", sep="+")
452+
terminal.sep("+", title="Timeout")
453453
pytest.fail("Timeout >%ss" % settings.timeout)
454454

455455

@@ -461,28 +461,29 @@ def timeout_timer(item, settings):
461461
"""
462462
if not settings.disable_debugger_detection and is_debugging():
463463
return
464+
terminal = item.config.get_terminal_writer()
464465
try:
465466
capman = item.config.pluginmanager.getplugin("capturemanager")
466467
if capman:
467468
capman.suspend_global_capture(item)
468469
stdout, stderr = capman.read_global_capture()
469470
else:
470471
stdout, stderr = None, None
471-
write_title("Timeout", sep="+")
472+
terminal.sep("+", title="Timeout")
472473
caplog = item.config.pluginmanager.getplugin("_capturelog")
473474
if caplog and hasattr(item, "capturelog_handler"):
474475
log = item.capturelog_handler.stream.getvalue()
475476
if log:
476-
write_title("Captured log")
477-
write(log)
477+
terminal.sep("~", title="Captured log")
478+
terminal.write(log)
478479
if stdout:
479-
write_title("Captured stdout")
480-
write(stdout)
480+
terminal.sep("~", title="Captured stdout")
481+
terminal.write(stdout)
481482
if stderr:
482-
write_title("Captured stderr")
483-
write(stderr)
484-
dump_stacks()
485-
write_title("Timeout", sep="+")
483+
terminal.sep("~", title="Captured stderr")
484+
terminal.write(stderr)
485+
dump_stacks(terminal)
486+
terminal.sep("+", title="Timeout")
486487
except Exception:
487488
traceback.print_exc()
488489
finally:
@@ -491,7 +492,7 @@ def timeout_timer(item, settings):
491492
os._exit(1)
492493

493494

494-
def dump_stacks():
495+
def dump_stacks(terminal):
495496
"""Dump the stacks of all threads except the current thread."""
496497
current_ident = threading.current_thread().ident
497498
for thread_ident, frame in sys._current_frames().items():
@@ -503,31 +504,5 @@ def dump_stacks():
503504
break
504505
else:
505506
thread_name = "<unknown>"
506-
write_title("Stack of %s (%s)" % (thread_name, thread_ident))
507-
write("".join(traceback.format_stack(frame)))
508-
509-
510-
def write_title(title, stream=None, sep="~"):
511-
"""Write a section title.
512-
513-
If *stream* is None sys.stderr will be used, *sep* is used to
514-
draw the line.
515-
"""
516-
if stream is None:
517-
stream = sys.stderr
518-
width, height = shutil.get_terminal_size()
519-
fill = int((width - len(title) - 2) / 2)
520-
line = " ".join([sep * fill, title, sep * fill])
521-
if len(line) < width:
522-
line += sep * (width - len(line))
523-
stream.write("\n" + line + "\n")
524-
525-
526-
def write(text, stream=None):
527-
"""Write text to stream.
528-
529-
Pretty stupid really, only here for symmetry with .write_title().
530-
"""
531-
if stream is None:
532-
stream = sys.stderr
533-
stream.write(text)
507+
terminal.sep("~", title="Stack of %s (%s)" % (thread_name, thread_ident))
508+
terminal.write("".join(traceback.format_stack(frame)))

0 commit comments

Comments
 (0)