8
8
"""
9
9
import inspect
10
10
import os
11
- import shutil
12
11
import signal
13
12
import sys
14
13
import threading
@@ -445,11 +444,12 @@ def timeout_sigalrm(item, settings):
445
444
return
446
445
__tracebackhide__ = True
447
446
nthreads = len (threading .enumerate ())
447
+ terminal = item .config .get_terminal_writer ()
448
448
if nthreads > 1 :
449
- write_title ( "Timeout " , sep = "+ " )
450
- dump_stacks ()
449
+ terminal . sep ( "+ " , title = "Timeout " )
450
+ dump_stacks (terminal )
451
451
if nthreads > 1 :
452
- write_title ( "Timeout " , sep = "+ " )
452
+ terminal . sep ( "+ " , title = "Timeout " )
453
453
pytest .fail ("Timeout >%ss" % settings .timeout )
454
454
455
455
@@ -461,28 +461,29 @@ def timeout_timer(item, settings):
461
461
"""
462
462
if not settings .disable_debugger_detection and is_debugging ():
463
463
return
464
+ terminal = item .config .get_terminal_writer ()
464
465
try :
465
466
capman = item .config .pluginmanager .getplugin ("capturemanager" )
466
467
if capman :
467
468
capman .suspend_global_capture (item )
468
469
stdout , stderr = capman .read_global_capture ()
469
470
else :
470
471
stdout , stderr = None , None
471
- write_title ( "Timeout " , sep = "+ " )
472
+ terminal . sep ( "+ " , title = "Timeout " )
472
473
caplog = item .config .pluginmanager .getplugin ("_capturelog" )
473
474
if caplog and hasattr (item , "capturelog_handler" ):
474
475
log = item .capturelog_handler .stream .getvalue ()
475
476
if log :
476
- write_title ( "Captured log" )
477
- write (log )
477
+ terminal . sep ( "~" , title = "Captured log" )
478
+ terminal . write (log )
478
479
if stdout :
479
- write_title ( "Captured stdout" )
480
- write (stdout )
480
+ terminal . sep ( "~" , title = "Captured stdout" )
481
+ terminal . write (stdout )
481
482
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 " )
486
487
except Exception :
487
488
traceback .print_exc ()
488
489
finally :
@@ -491,7 +492,7 @@ def timeout_timer(item, settings):
491
492
os ._exit (1 )
492
493
493
494
494
- def dump_stacks ():
495
+ def dump_stacks (terminal ):
495
496
"""Dump the stacks of all threads except the current thread."""
496
497
current_ident = threading .current_thread ().ident
497
498
for thread_ident , frame in sys ._current_frames ().items ():
@@ -503,31 +504,5 @@ def dump_stacks():
503
504
break
504
505
else :
505
506
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