Fix broken assignment in deinit.
Add an extra test to prove that it's needed.
diff --git a/colorama/initialise.py b/colorama/initialise.py
index 7abeb1c..eb114b9 100644
--- a/colorama/initialise.py
+++ b/colorama/initialise.py
@@ -22,19 +22,16 @@
def init(autoreset=False, convert=None, strip=None, wrap=True):
-
if not wrap and any([autoreset, convert, strip]):
raise ValueError('wrap=False conflicts with any other arg=True')
global wrapped_stdout, wrapped_stderr
global orig_stdout, orig_stderr
- # Prevent multiple calls from losing the original stdout
- if (
- orig_stdout is UNSET and
- orig_stderr is UNSET
- ):
+ # Prevent multiple calls from losing the original stdout/err
+ if orig_stdout is UNSET:
orig_stdout = sys.stdout
+ if orig_stderr is UNSET:
orig_stderr = sys.stderr
if sys.stdout is None:
@@ -58,10 +55,10 @@
global orig_stdout, orig_stderr
if orig_stdout is not UNSET:
sys.stdout = orig_stdout
- orig_stdout == UNSET
+ orig_stdout = UNSET
if orig_stderr is not UNSET:
sys.stderr = orig_stderr
- orig_stderr == UNSET
+ orig_stderr = UNSET
@contextlib.contextmanager
diff --git a/colorama/tests/initialise_test.py b/colorama/tests/initialise_test.py
index 24ad744..0aac36e 100644
--- a/colorama/tests/initialise_test.py
+++ b/colorama/tests/initialise_test.py
@@ -6,12 +6,14 @@
from mock import patch
from ..ansitowin32 import StreamWrapper
+from .. import initialise
from ..initialise import deinit, init
from .utils import osname, redirected_output, replace_by
orig_stdout = sys.stdout
orig_stderr = sys.stderr
+
class InitTest(TestCase):
def setUp(self):
@@ -21,6 +23,8 @@
def tearDown(self):
sys.stdout = orig_stdout
sys.stderr = orig_stderr
+ initialise.orig_stdout = initialise.UNSET
+ initialise.orig_stderr = initialise.UNSET
def assertWrapped(self):
self.assertIsNot(sys.stdout, orig_stdout, 'stdout should be wrapped')
@@ -75,7 +79,7 @@
self.assertRaises(ValueError, lambda: init(autoreset=True, wrap=False))
@patch('colorama.ansitowin32.winapi_test', lambda *_: True)
- def testInitTwiceCanUndoneWithDeinitOnce(self):
+ def testInitTwiceCanBeUndoneWithDeinitOnce(self):
with osname('nt'):
self.assertNotWrapped()
init()
@@ -85,6 +89,21 @@
deinit()
self.assertNotWrapped()
+ @patch('colorama.ansitowin32.winapi_test', lambda *_: True)
+ def testInitDeinitInitWorks(self):
+ with osname('nt'):
+ self.assertNotWrapped()
+ init()
+ self.assertWrapped()
+ deinit()
+ self.assertNotWrapped()
+ init()
+ self.assertWrapped()
+ deinit()
+ self.assertNotWrapped()
+ init()
+ self.assertWrapped()
+
@patch('colorama.win32.SetConsoleTextAttribute')
@patch('colorama.initialise.AnsiToWin32')
def testAutoResetPassedOn(self, mockATW32, _):