From 4ba6000f9204f9d2039fbb9e42289cd7333b5882 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 17 May 2016 16:46:45 -0700 Subject: [PATCH 1/2] Initialize colorama lazily Will prevent colorama from wrapping stderr/out if not in debug mode, which leads to some errors. --- jedi/debug.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jedi/debug.py b/jedi/debug.py index 5c59a7b6..331c3744 100644 --- a/jedi/debug.py +++ b/jedi/debug.py @@ -3,6 +3,11 @@ import inspect import os import time +def _lazy_colorama_init(): + pass + +_inited=False + try: if os.name == 'nt': # does not work on Windows, as pyreadline and colorama interfere @@ -13,8 +18,13 @@ try: from colorama import initialise # pytest resets the stream at the end - causes troubles. Since after # every output the stream is reset automatically we don't need this. - initialise.atexit_done = True - init() + def _lazy_colorama_init(): + global _inited + if not _inited: + initialise.atexit_done = True + init() + _inited = True + except ImportError: class Fore(object): RED = '' @@ -63,6 +73,7 @@ def dbg(message, *args): mod = inspect.getmodule(frm[0]) if not (mod.__name__ in ignored_modules): i = ' ' * _debug_indent + _lazy_colorama_init() debug_function(NOTICE, i + 'dbg: ' + message % tuple(u(repr(a)) for a in args)) @@ -81,6 +92,7 @@ def speed(name): def print_to_stdout(level, str_out): """ The default debug function """ + _lazy_colorama_init() if level == NOTICE: col = Fore.GREEN elif level == WARNING: From e96cccb81c5085c23f7293498aca049be8f22b87 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 18 May 2016 16:48:01 -0700 Subject: [PATCH 2/2] Document a bit more --- jedi/debug.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jedi/debug.py b/jedi/debug.py index 331c3744..d73a72ae 100644 --- a/jedi/debug.py +++ b/jedi/debug.py @@ -4,6 +4,10 @@ import os import time def _lazy_colorama_init(): + """lazily init colorama if necessary, not to screw up stdout is debug not enabled. + + This version of the function does nothing. + """ pass _inited=False @@ -16,11 +20,15 @@ try: # Use colorama for nicer console output. from colorama import Fore, init from colorama import initialise - # pytest resets the stream at the end - causes troubles. Since after - # every output the stream is reset automatically we don't need this. def _lazy_colorama_init(): + """lazily init colorama if necessary, not to screw up stdout is debug not enabled. + + This version of the function does init colorama. + """ global _inited if not _inited: + # pytest resets the stream at the end - causes troubles. Since after + # every output the stream is reset automatically we don't need this. initialise.atexit_done = True init() _inited = True