Skip to content

Commit

Permalink
Be more thorough about cleaning up state between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed Oct 17, 2022
1 parent 3f3c06a commit 3c5553f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
25 changes: 19 additions & 6 deletions colorama/initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
from .ansitowin32 import AnsiToWin32


orig_stdout = None
orig_stderr = None
def _wipe_internal_state_for_tests():
global orig_stdout, orig_stderr
orig_stdout = None
orig_stderr = None

global wrapped_stdout, wrapped_stderr
wrapped_stdout = None
wrapped_stderr = None

global atexit_done
atexit_done = False

wrapped_stdout = None
wrapped_stderr = None
global fixed_windows_console
fixed_windows_console = False

atexit_done = False
# no-op if it wasn't registered
atexit.unregister(reset_all)

fixed_windows_console = False

def reset_all():
if AnsiToWin32 is not None: # Issue #74: objects might become None at exit
Expand Down Expand Up @@ -102,3 +111,7 @@ def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrapper.should_wrap():
stream = wrapper.stream
return stream


# Use this for initial setup as well, to reduce code duplication
_wipe_internal_state_for_tests()
3 changes: 2 additions & 1 deletion colorama/tests/initialise_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mock import patch

from ..ansitowin32 import StreamWrapper
from ..initialise import init
from ..initialise import init, just_fix_windows_console, _wipe_internal_state_for_tests
from .utils import osname, replace_by

orig_stdout = sys.stdout
Expand All @@ -23,6 +23,7 @@ def setUp(self):
self.assertNotWrapped()

def tearDown(self):
_wipe_internal_state_for_tests()
sys.stdout = orig_stdout
sys.stderr = orig_stderr

Expand Down

0 comments on commit 3c5553f

Please sign in to comment.