Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against multiple calls to init(). #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions colorama/initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from .ansitowin32 import AnsiToWin32

UNSET = object()

orig_stdout = None
orig_stderr = None
orig_stdout = UNSET
wiggin15 marked this conversation as resolved.
Show resolved Hide resolved
orig_stderr = UNSET

wrapped_stdout = None
wrapped_stderr = None
Expand All @@ -28,8 +29,13 @@ 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
# Prevent multiple calls from losing the original stdout
if (
orig_stdout is UNSET and
orig_stderr is UNSET
):
orig_stdout = sys.stdout
orig_stderr = sys.stderr

if sys.stdout is None:
wrapped_stdout = None
Expand All @@ -49,10 +55,13 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):


def deinit():
if orig_stdout is not None:
global orig_stdout, orig_stderr
if orig_stdout is not UNSET:
sys.stdout = orig_stdout
if orig_stderr is not None:
orig_stdout == UNSET
wiggin15 marked this conversation as resolved.
Show resolved Hide resolved
if orig_stderr is not UNSET:
sys.stderr = orig_stderr
orig_stderr == UNSET


@contextlib.contextmanager
Expand All @@ -78,3 +87,4 @@ def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrapper.should_wrap():
stream = wrapper.stream
return stream

12 changes: 7 additions & 5 deletions colorama/tests/initialise_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
from mock import patch

from ..ansitowin32 import StreamWrapper
from ..initialise import init
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):
Expand Down Expand Up @@ -75,13 +74,16 @@ def testInitWrapOffDoesntWrapOnWindows(self):
def testInitWrapOffIncompatibleWithAutoresetOn(self):
self.assertRaises(ValueError, lambda: init(autoreset=True, wrap=False))

@patch('colorama.ansitowin32.winterm', None)
@patch('colorama.ansitowin32.winapi_test', lambda *_: True)
def testInitOnlyWrapsOnce(self):
with osname("nt"):
def testInitTwiceCanUndoneWithDeinitOnce(self):
with osname('nt'):
self.assertNotWrapped()
init()
self.assertWrapped()
init()
self.assertWrapped()
deinit()
self.assertNotWrapped()
wiggin15 marked this conversation as resolved.
Show resolved Hide resolved

@patch('colorama.win32.SetConsoleTextAttribute')
@patch('colorama.initialise.AnsiToWin32')
Expand Down