Skip to content

Commit

Permalink
safer terminal I/O detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mogproject committed Nov 7, 2015
1 parent 2b7c7ba commit 4c20cd0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/mog_commons/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.20'
__version__ = '0.1.21'
6 changes: 3 additions & 3 deletions src/mog_commons/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, term_type=None, encoding=None,

@staticmethod
def _can_getch_enable(stdin):
if stdin.isatty():
if hasattr(stdin, 'isatty') and stdin.isatty():
return os.name == 'nt' or hasattr(stdin, 'fileno')
return False

Expand Down Expand Up @@ -125,7 +125,7 @@ def clear(self):
"""
Clear the terminal screen.
"""
if self.stdout.isatty() or self.term_type == 'mintty':
if hasattr(self.stdout, 'isatty') and self.stdout.isatty() or self.term_type == 'mintty':
cmd, shell = {
'posix': ('clear', False),
'nt': ('cls', True),
Expand All @@ -138,7 +138,7 @@ def clear_input_buffer(self):
"""
Clear the input buffer.
"""
if self.stdin.isatty():
if hasattr(self.stdin, 'isatty') and self.stdin.isatty():
if os.name == 'nt':
while msvcrt.kbhit():
msvcrt.getch()
Expand Down
5 changes: 5 additions & 0 deletions tests/mog_commons/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@


class TestTerminal(TestCase):
def test_clear(self):
with self.withAssertOutput('', '') as (out, err):
# assume this should not raise an error
TerminalHandler(stdout=out, stderr=err).clear()

def test_getch_from_file(self):
with open(os.path.join('tests', 'resources', 'test_terminal_input.txt')) as f:
t = TerminalHandler(stdin=f)
Expand Down

0 comments on commit 4c20cd0

Please sign in to comment.