Skip to content

Commit 7cb29b9

Browse files
s0undt3chflub
authored andcommitted
Switch to using Pytest's terminal reporter
Signed-off-by: Pedro Algarvio <[email protected]>
1 parent 50bb71d commit 7cb29b9

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ Unreleased
348348

349349
- Fix debugger detection for recent VSCode, this compiles pydevd using
350350
cython which is now correctly detected. Thanks Adrian Gielniewski.
351+
- Switched to using Pytest's ``TerminalReporter`` instead of writing
352+
directly to ``sys.{stdout,stderr}``.
353+
This change also switches all output from ``sys.stderr`` to ``sys.stdout``.
354+
Thanks Pedro Algarvio.
351355

352356
2.2.0
353357
-----

pytest_timeout.py

Lines changed: 18 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,37 +461,39 @@ 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:
490+
terminal.flush()
489491
sys.stdout.flush()
490492
sys.stderr.flush()
491493
os._exit(1)
492494

493495

494-
def dump_stacks():
496+
def dump_stacks(terminal):
495497
"""Dump the stacks of all threads except the current thread."""
496498
current_ident = threading.current_thread().ident
497499
for thread_ident, frame in sys._current_frames().items():
@@ -503,31 +505,5 @@ def dump_stacks():
503505
break
504506
else:
505507
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)
508+
terminal.sep("~", title="Stack of %s (%s)" % (thread_name, thread_ident))
509+
terminal.write("".join(traceback.format_stack(frame)))

0 commit comments

Comments
 (0)