Skip to content

Commit

Permalink
Fix #84: check if stream has 'closed' attribute before testing it
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnon Yaari committed Feb 11, 2016
1 parent 27a3833 commit 1244a00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 7 additions & 3 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
winterm = WinTerm()


def is_stream_closed(stream):
return not hasattr(stream, 'closed') or stream.closed


def is_a_tty(stream):
return hasattr(stream, 'isatty') and stream.isatty()

Expand Down Expand Up @@ -64,12 +68,12 @@ 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 (not is_stream_closed(wrapped) 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 not is_stream_closed(wrapped) and is_a_tty(wrapped)
self.convert = convert

# dict of ansi codes to win32 functions and parameters
Expand Down Expand Up @@ -145,7 +149,7 @@ def write(self, text):
def reset_all(self):
if self.convert:
self.call_win32('m', (0,))
elif not self.strip and not self.wrapped.closed:
elif not self.strip and not is_stream_closed(self.wrapped):
self.wrapped.write(Style.RESET_ALL)


Expand Down
9 changes: 8 additions & 1 deletion colorama/tests/ansitowin32_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def testStripIsTrueOnWindows(self):

def testStripIsFalseOffWindows(self):
with osname('posix'):
mockStdout = Mock()
mockStdout = Mock(closed=False)
stream = AnsiToWin32(mockStdout)
self.assertFalse(stream.strip)

Expand Down Expand Up @@ -167,6 +167,13 @@ def test_wrap_shouldnt_raise_on_closed_orig_stdout(self):
stream = StringIO()
stream.close()
converter = AnsiToWin32(stream)
self.assertFalse(converter.strip)
self.assertFalse(converter.convert)

def test_wrap_shouldnt_raise_on_missing_closed_attr(self):
converter = AnsiToWin32(object())
self.assertFalse(converter.strip)
self.assertFalse(converter.convert)

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

0 comments on commit 1244a00

Please sign in to comment.