summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py23
-rw-r--r--tests/test_cmd2.py20
2 files changed, 32 insertions, 11 deletions
diff --git a/cmd2.py b/cmd2.py
index c83a9bf0..dba8689c 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -59,15 +59,6 @@ import six.moves as sm
# itertools.zip() for Python 2 or zip() for Python 3 - produces an iterator in both cases
from six.moves import zip
-# Python 3 compatibility hack due to no built-in file keyword in Python 3
-# Due to one occurrence of isinstance(<foo>, file) checking to see if something is of file type
-try:
- # noinspection PyUnboundLocalVariable,PyUnresolvedReferences
- file
-except NameError:
- import io
- file = io.TextIOWrapper
-
# Detect whether IPython is installed to determine if the built-in "ipy" command should be included
ipython_available = True
try:
@@ -927,13 +918,18 @@ class Cmd(cmd.Cmd):
stop = None
try:
while not stop:
- # NOTE: cmdqueue appears completely unused, but it is defined in cmd.Cmd, so we are leaving it here
if self.cmdqueue:
+ # Run command out of cmdqueue if nonempty (populated by load command or commands at invocation)
line = self.cmdqueue.pop(0)
else:
+ # Otherwise, read a command from stdin
line = self.pseudo_raw_input(self.prompt)
- if self.echo and isinstance(self.stdin, file):
+
+ # If echo is on and in the middle of running a script, then echo the line to the output
+ if self.echo and self._current_script_dir is not None:
self.stdout.write(line + '\n')
+
+ # Run the command along with all associated pre and post hooks
stop = self.onecmd_plus_hooks(line)
finally:
if self.use_rawinput and self.completekey:
@@ -942,6 +938,11 @@ class Cmd(cmd.Cmd):
readline.set_completer_delims(self.old_delims)
except NameError:
pass
+
+ # Need to set empty list this way because Python 2 doesn't support the clear() method on lists
+ self.cmdqueue = []
+ self._script_dir = []
+
return stop
# noinspection PyUnusedLocal
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 8bb1f52a..7e393b7b 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1262,3 +1262,23 @@ def test_eos(base_app):
# And make sure it reduced the length of the script dir list
assert len(base_app._script_dir) == 0
+
+
+def test_echo(capsys):
+ app = cmd2.Cmd()
+ # Turn echo on and pre-stage some commands in the queue, simulating like we are in the middle of a script
+ app.echo = True
+ app.cmdqueue = ['help history', 'quit', 'eos']
+ app._script_dir.append('some_dir')
+
+ assert app._current_script_dir is not None
+
+ # Run the inner _cmdloop
+ app._cmdloop()
+
+ out, err = capsys.readouterr()
+
+ # Check the output
+ assert app.cmdqueue == []
+ assert app._current_script_dir is None
+ assert out.startswith('help history\n' + 'history [arg]: lists past commands issued')