Skip to content

Commit

Permalink
Prevent user from wrapping already wrapped streams
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchy committed Nov 6, 2018
1 parent c11da6f commit 9bbe4e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions colorama/initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):
global wrapped_stdout, wrapped_stderr
global orig_stdout, orig_stderr

if (orig_stdout is not None) or (orig_stderr is not None):
deinit()

orig_stdout = sys.stdout
orig_stderr = sys.stderr

Expand All @@ -49,10 +52,14 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):


def deinit():
global orig_stdout, orig_stderr

if orig_stdout is not None:
sys.stdout = orig_stdout
orig_stdout = None
if orig_stderr is not None:
sys.stderr = orig_stderr
orig_stderr = None


@contextlib.contextmanager
Expand Down
3 changes: 2 additions & 1 deletion colorama/tests/initialise_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mock import patch

from ..ansitowin32 import StreamWrapper
from ..initialise import init
from ..initialise import init, deinit
from .utils import osname, redirected_output, replace_by

orig_stdout = sys.stdout
Expand All @@ -22,6 +22,7 @@ def setUp(self):
def tearDown(self):
sys.stdout = orig_stdout
sys.stderr = orig_stderr
deinit()

def assertWrapped(self):
self.assertIsNot(sys.stdout, orig_stdout, 'stdout should be wrapped')
Expand Down

0 comments on commit 9bbe4e8

Please sign in to comment.