From ad2a7508e04c15a1487f727bbb03c14c691ee800 Mon Sep 17 00:00:00 2001 From: Tomasz Chyrowicz Date: Tue, 23 Oct 2018 12:32:35 +0200 Subject: [PATCH] Prevent user from wrapping already wrapped streams --- colorama/initialise.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/colorama/initialise.py b/colorama/initialise.py index 430d066..62ee510 100644 --- a/colorama/initialise.py +++ b/colorama/initialise.py @@ -28,19 +28,23 @@ def init(autoreset=False, convert=None, strip=None, wrap=True): global wrapped_stdout, wrapped_stderr global orig_stdout, orig_stderr - orig_stdout = sys.stdout - orig_stderr = sys.stderr - - if sys.stdout is None: - wrapped_stdout = None - else: - sys.stdout = wrapped_stdout = \ - wrap_stream(orig_stdout, convert, strip, autoreset, wrap) - if sys.stderr is None: - wrapped_stderr = None - else: - sys.stderr = wrapped_stderr = \ - wrap_stream(orig_stderr, convert, strip, autoreset, wrap) + if orig_stdout is None: + orig_stdout = sys.stdout + + if sys.stdout is None: + wrapped_stdout = None + else: + sys.stdout = wrapped_stdout = \ + wrap_stream(orig_stdout, convert, strip, autoreset, wrap) + + if orig_stderr is None: + orig_stderr = sys.stderr + + if sys.stderr is None: + wrapped_stderr = None + else: + sys.stderr = wrapped_stderr = \ + wrap_stream(orig_stderr, convert, strip, autoreset, wrap) global atexit_done if not atexit_done: