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

Check for 'closed' attribute on 'wrapped' in AnsiToWin32 #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):

# should we strip ANSI sequences from our output?
if strip is None:
strip = conversion_supported or (not wrapped.closed and not is_a_tty(wrapped))
strip = conversion_supported or (
hasattr(wrapped, 'closed') and not wrapped.closed and not
is_a_tty(wrapped))
self.strip = strip

# should we should convert ANSI sequences into win32 calls?
if convert is None:
convert = conversion_supported and not wrapped.closed and is_a_tty(wrapped)
convert = (conversion_supported and
Copy link
Author

Choose a reason for hiding this comment

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

Not sure if and here is right, compared to the or above?!

Copy link
Collaborator

Choose a reason for hiding this comment

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

The and and or are ok.

  • We want to strip the ANSI codes in two separate cases: either we're using a Windows cmd-like terminal (conversion_supported, ANSI codes are not supported) OR we're not using a tty at all (e.g. output redirection)
  • To convert the ANSI codes to Windows API we require two conditions: use a cmd-like terminal AND use a tty (no redirection).

hasattr(wrapped, 'closed') and not
Copy link
Owner

Choose a reason for hiding this comment

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

Personally, I'd put the 'not' on the next line - it does apply to the 'wrapped.closed'. Ditto the 'not' at the end of line 68.

wrapped.closed and is_a_tty(wrapped))
self.convert = convert

# dict of ansi codes to win32 functions and parameters
Expand Down
5 changes: 4 additions & 1 deletion colorama/tests/ansitowin32_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def test_reset_all_shouldnt_raise_on_closed_orig_stdout(self):
def test_wrap_shouldnt_raise_on_closed_orig_stdout(self):
stream = StringIO()
stream.close()
converter = AnsiToWin32(stream)
AnsiToWin32(stream)

def test_wrap_shouldnt_raise_on_missing_closed_attrib(self):
AnsiToWin32(object())

def testExtractParams(self):
stream = AnsiToWin32(Mock())
Expand Down