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

Support possibility to wrap either stdout or stderr #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions colorama/initialise.py
Expand Up @@ -22,23 +22,26 @@ def reset_all():

def init(autoreset=False, convert=None, strip=None, wrap=True):

if wrap not in [True, False, "stdout", "stderr"]:
raise ValueError('wrap must be one of: True, False, stdout, stderr')

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

orig_stdout = sys.stdout
orig_stderr = sys.stderr

if sys.stdout is None:
wrapped_stdout = None
else:
elif wrap != "stderr":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgive my very overdue drive-by review, but does this do the wrong thing if wrap==False ?

orig_stdout = sys.stdout
sys.stdout = wrapped_stdout = \
wrap_stream(orig_stdout, convert, strip, autoreset, wrap)

if sys.stderr is None:
wrapped_stderr = None
else:
elif wrap != "stdout":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

orig_stderr = sys.stderr
sys.stderr = wrapped_stderr = \
wrap_stream(orig_stderr, convert, strip, autoreset, wrap)

Expand All @@ -65,9 +68,9 @@ def colorama_text(*args, **kwargs):


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


Expand Down