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

Add initialization counter #262

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 27 additions & 3 deletions colorama/initialise.py
Expand Up @@ -5,6 +5,7 @@

from .ansitowin32 import AnsiToWin32

_init_counter = 0

orig_stdout = None
orig_stderr = None
Expand All @@ -20,7 +21,7 @@ def reset_all():
AnsiToWin32(orig_stdout).reset_all()


def init(autoreset=False, convert=None, strip=None, wrap=True):
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')
Expand Down Expand Up @@ -48,13 +49,29 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):
atexit_done = True


def deinit():
def init(autoreset=False, convert=None, strip=None, wrap=True):
global _init_counter
if _init_counter == 0:
_init(autoreset, convert, strip, wrap)
_init_counter += 1


def _deinit():
if orig_stdout is not None:
sys.stdout = orig_stdout
if orig_stderr is not None:
sys.stderr = orig_stderr


def deinit():
global _init_counter
if _init_counter == 1:
_deinit()
_init_counter -= 1
if _init_counter < 0:
_init_counter = 0


@contextlib.contextmanager
def colorama_text(*args, **kwargs):
init(*args, **kwargs)
Expand All @@ -64,13 +81,20 @@ def colorama_text(*args, **kwargs):
deinit()


def reinit():
def _reinit():
if wrapped_stdout is not None:
sys.stdout = wrapped_stdout
if wrapped_stderr is not None:
sys.stderr = wrapped_stderr


def reinit():
global _init_counter
if _init_counter == 0:
_reinit()
_init_counter += 1


def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrap:
wrapper = AnsiToWin32(stream,
Expand Down