33import os
44import time
55
6+ def _lazy_colorama_init ():
7+ """lazily init colorama if necessary, not to screw up stdout is debug not enabled.
8+
9+ This version of the function does nothing.
10+ """
11+ pass
12+
13+ _inited = False
14+
615try :
716 if os .name == 'nt' :
817 # does not work on Windows, as pyreadline and colorama interfere
1120 # Use colorama for nicer console output.
1221 from colorama import Fore , init
1322 from colorama import initialise
14- # pytest resets the stream at the end - causes troubles. Since after
15- # every output the stream is reset automatically we don't need this.
16- initialise .atexit_done = True
17- init ()
23+ def _lazy_colorama_init ():
24+ """lazily init colorama if necessary, not to screw up stdout is debug not enabled.
25+
26+ This version of the function does init colorama.
27+ """
28+ global _inited
29+ if not _inited :
30+ # pytest resets the stream at the end - causes troubles. Since after
31+ # every output the stream is reset automatically we don't need this.
32+ initialise .atexit_done = True
33+ init ()
34+ _inited = True
35+
1836except ImportError :
1937 class Fore (object ):
2038 RED = ''
@@ -63,6 +81,7 @@ def dbg(message, *args):
6381 mod = inspect .getmodule (frm [0 ])
6482 if not (mod .__name__ in ignored_modules ):
6583 i = ' ' * _debug_indent
84+ _lazy_colorama_init ()
6685 debug_function (NOTICE , i + 'dbg: ' + message % tuple (u (repr (a )) for a in args ))
6786
6887
@@ -81,6 +100,7 @@ def speed(name):
81100
82101def print_to_stdout (level , str_out ):
83102 """ The default debug function """
103+ _lazy_colorama_init ()
84104 if level == NOTICE :
85105 col = Fore .GREEN
86106 elif level == WARNING :
0 commit comments