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

Prevent user from wrapping already wrapped streams #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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