Skip to content

Commit 751f936

Browse files
authored
Merge pull request #200 from python-cmd2/ruggedization
Made a few code ruggedizations
2 parents be2c87e + c28a027 commit 751f936

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

cmd2.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,14 @@ def poutput(self, msg, end='\n'):
566566
Also handles BrokenPipeError exceptions for when a commands's output has been piped to another process and
567567
that process terminates before the cmd2 command is finished executing.
568568
569-
:param msg: str - message to print to current stdout
569+
:param msg: str - message to print to current stdout - anyting convertible to a str with '{}'.format() is OK
570570
:param end: str - string appended after the end of the message if not already present, default a newline
571571
"""
572572
if msg:
573573
try:
574-
self.stdout.write(msg)
575-
if not msg.endswith(end):
574+
msg_str = '{}'.format(msg)
575+
self.stdout.write(msg_str)
576+
if not msg_str.endswith(end):
576577
self.stdout.write(end)
577578
except BrokenPipeError:
578579
# This occurs if a command's output is being piped to another process and that process closes before the
@@ -698,6 +699,10 @@ def postparsing_postcmd(self, stop):
698699
:param stop: bool - True implies the entire application should exit.
699700
:return: bool - True implies the entire application should exit.
700701
"""
702+
if not sys.platform.startswith('win'):
703+
# Fix those annoying problems that occur with terminal programs like "less" when you pipe to them
704+
proc = subprocess.Popen(shlex.split('stty sane'))
705+
proc.communicate()
701706
return stop
702707

703708
def parseline(self, line):
@@ -844,21 +849,21 @@ def _restore_output(self, statement):
844849
self.stdout.seek(0)
845850
write_to_paste_buffer(self.stdout.read())
846851

847-
# Close the file or pipe that stdout was redirected to
848852
try:
853+
# Close the file or pipe that stdout was redirected to
849854
self.stdout.close()
850855
except BrokenPipeError:
851856
pass
857+
finally:
858+
# Restore self.stdout
859+
self.kept_state.restore()
860+
self.kept_state = None
852861

853862
# If we were piping output to a shell command, then close the subprocess the shell command was running in
854863
if self.pipe_proc is not None:
855864
self.pipe_proc.communicate()
856865
self.pipe_proc = None
857866

858-
# Restore self.stdout
859-
self.kept_state.restore()
860-
self.kept_state = None
861-
862867
# Restore sys.stdout if need be
863868
if self.kept_sys is not None:
864869
self.kept_sys.restore()
@@ -895,15 +900,15 @@ def onecmd(self, line):
895900
statement = self.parser_manager.parsed(line)
896901
funcname = self._func_named(statement.parsed.command)
897902
if not funcname:
898-
return self._default(statement)
903+
return self.default(statement)
899904
try:
900905
func = getattr(self, funcname)
901906
except AttributeError:
902-
return self._default(statement)
907+
return self.default(statement)
903908
stop = func(statement)
904909
return stop
905910

906-
def _default(self, statement):
911+
def default(self, statement):
907912
"""Executed when the command given isn't a recognized command implemented by a do_* method.
908913
909914
:param statement: ParsedString - subclass of string including the pyparsing ParseResults
@@ -914,12 +919,10 @@ def _default(self, statement):
914919
result = os.system(arg)
915920
# If os.system() succeeded, then don't print warning about unknown command
916921
if not result:
917-
return False
922+
return
918923

919924
# Print out a message stating this is an unknown command
920-
self.default(arg)
921-
922-
return False
925+
self.poutput('*** Unknown syntax: {}\n'.format(arg))
923926

924927
@staticmethod
925928
def _surround_ansi_escapes(prompt, start="\x01", end="\x02"):

tests/test_cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def test_default_to_shell_good(capsys):
856856
app.default_to_shell = True
857857
line = 'ls'
858858
statement = app.parser_manager.parsed(line)
859-
retval = app._default(statement)
859+
retval = app.default(statement)
860860
assert not retval
861861
out, err = capsys.readouterr()
862862
assert out == ''
@@ -866,7 +866,7 @@ def test_default_to_shell_failure(capsys):
866866
app.default_to_shell = True
867867
line = 'ls does_not_exist.xyz'
868868
statement = app.parser_manager.parsed(line)
869-
retval = app._default(statement)
869+
retval = app.default(statement)
870870
assert not retval
871871
out, err = capsys.readouterr()
872872
assert out == "*** Unknown syntax: {}\n".format(line)

0 commit comments

Comments
 (0)