Skip to content

Commit 9bbe4e8

Browse files
committed
Prevent user from wrapping already wrapped streams
1 parent c11da6f commit 9bbe4e8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

colorama/initialise.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):
2828
global wrapped_stdout, wrapped_stderr
2929
global orig_stdout, orig_stderr
3030

31+
if (orig_stdout is not None) or (orig_stderr is not None):
32+
deinit()
33+
3134
orig_stdout = sys.stdout
3235
orig_stderr = sys.stderr
3336

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

5053

5154
def deinit():
55+
global orig_stdout, orig_stderr
56+
5257
if orig_stdout is not None:
5358
sys.stdout = orig_stdout
59+
orig_stdout = None
5460
if orig_stderr is not None:
5561
sys.stderr = orig_stderr
62+
orig_stderr = None
5663

5764

5865
@contextlib.contextmanager

colorama/tests/initialise_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mock import patch
77

88
from ..ansitowin32 import StreamWrapper
9-
from ..initialise import init
9+
from ..initialise import init, deinit
1010
from .utils import osname, redirected_output, replace_by
1111

1212
orig_stdout = sys.stdout
@@ -22,6 +22,7 @@ def setUp(self):
2222
def tearDown(self):
2323
sys.stdout = orig_stdout
2424
sys.stderr = orig_stderr
25+
deinit()
2526

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

0 commit comments

Comments
 (0)