Skip to content

Commit

Permalink
Move tty / closed functions to StreamWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 11, 2018
1 parent 3579cde commit 2057f03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
29 changes: 14 additions & 15 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
winterm = WinTerm()


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


def is_a_tty(stream):
if 'PYCHARM_HOSTED' in os.environ:
if stream is not None and (stream is sys.__stdout__ or stream is sys.__stderr__):
return True
return (hasattr(stream, 'isatty') and stream.isatty())


class StreamWrapper(object):
'''
Wraps a stream (such as stdout), acting as a transparent proxy for all
Expand All @@ -43,7 +32,17 @@ def write(self, text):
self.__convertor.write(text)

def isatty(self):
return is_a_tty(self.__wrapped)
stream = self.__wrapped
if 'PYCHARM_HOSTED' in os.environ:
if stream is not None and (stream is sys.__stdout__ or stream is sys.__stderr__):
return True
return (hasattr(stream, 'isatty') and stream.isatty())

@property
def closed(self):
stream = self.__wrapped
return not hasattr(stream, 'closed') or stream.closed


class AnsiToWin32(object):
'''
Expand Down Expand Up @@ -73,12 +72,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 is_stream_closed(wrapped) and not is_a_tty(wrapped))
strip = conversion_supported or (not self.stream.closed and not self.stream.isatty())
self.strip = strip

# should we should convert ANSI sequences into win32 calls?
if convert is None:
convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped)
convert = conversion_supported and not self.stream.closed and self.stream.isatty()
self.convert = convert

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


Expand Down
13 changes: 8 additions & 5 deletions colorama/tests/isatty_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import sys
from unittest import TestCase, main

from ..ansitowin32 import is_a_tty, AnsiToWin32
from ..ansitowin32 import StreamWrapper, AnsiToWin32
from .utils import pycharm, replace_by, replace_original_by, StreamTTY, StreamNonTTY


def is_a_tty(stream):
return StreamWrapper(stream, None).isatty()

class IsattyTest(TestCase):

def test_TTY(self):
Expand Down Expand Up @@ -44,10 +47,10 @@ def test_withPycharmNoneOverride(self):

def test_withPycharmStreamWrapped(self):
with pycharm():
self.assertTrue(is_a_tty(AnsiToWin32(StreamTTY()).stream))
self.assertFalse(is_a_tty(AnsiToWin32(StreamNonTTY()).stream))
self.assertTrue(is_a_tty(AnsiToWin32(sys.stdout).stream))
self.assertTrue(is_a_tty(AnsiToWin32(sys.stderr).stream))
self.assertTrue(AnsiToWin32(StreamTTY()).stream.isatty())
self.assertFalse(AnsiToWin32(StreamNonTTY()).stream.isatty())
self.assertTrue(AnsiToWin32(sys.stdout).stream.isatty())
self.assertTrue(AnsiToWin32(sys.stderr).stream.isatty())


if __name__ == '__main__':
Expand Down

0 comments on commit 2057f03

Please sign in to comment.