Skip to content

Commit a140ad1

Browse files
authored
Merge pull request #178 from python-cmd2/remove_filehack
Remove ugly file hack code which is no longer necessary
2 parents 22c43d7 + af248f9 commit a140ad1

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

cmd2.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@
5959
# itertools.zip() for Python 2 or zip() for Python 3 - produces an iterator in both cases
6060
from six.moves import zip
6161

62-
# Python 3 compatibility hack due to no built-in file keyword in Python 3
63-
# Due to one occurrence of isinstance(<foo>, file) checking to see if something is of file type
64-
try:
65-
# noinspection PyUnboundLocalVariable,PyUnresolvedReferences
66-
file
67-
except NameError:
68-
import io
69-
file = io.TextIOWrapper
70-
7162
# Detect whether IPython is installed to determine if the built-in "ipy" command should be included
7263
ipython_available = True
7364
try:
@@ -927,13 +918,18 @@ def _cmdloop(self):
927918
stop = None
928919
try:
929920
while not stop:
930-
# NOTE: cmdqueue appears completely unused, but it is defined in cmd.Cmd, so we are leaving it here
931921
if self.cmdqueue:
922+
# Run command out of cmdqueue if nonempty (populated by load command or commands at invocation)
932923
line = self.cmdqueue.pop(0)
933924
else:
925+
# Otherwise, read a command from stdin
934926
line = self.pseudo_raw_input(self.prompt)
935-
if self.echo and isinstance(self.stdin, file):
927+
928+
# If echo is on and in the middle of running a script, then echo the line to the output
929+
if self.echo and self._current_script_dir is not None:
936930
self.stdout.write(line + '\n')
931+
932+
# Run the command along with all associated pre and post hooks
937933
stop = self.onecmd_plus_hooks(line)
938934
finally:
939935
if self.use_rawinput and self.completekey:
@@ -942,6 +938,11 @@ def _cmdloop(self):
942938
readline.set_completer_delims(self.old_delims)
943939
except NameError:
944940
pass
941+
942+
# Need to set empty list this way because Python 2 doesn't support the clear() method on lists
943+
self.cmdqueue = []
944+
self._script_dir = []
945+
945946
return stop
946947

947948
# noinspection PyUnusedLocal

tests/test_cmd2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,23 @@ def test_eos(base_app):
12621262

12631263
# And make sure it reduced the length of the script dir list
12641264
assert len(base_app._script_dir) == 0
1265+
1266+
1267+
def test_echo(capsys):
1268+
app = cmd2.Cmd()
1269+
# Turn echo on and pre-stage some commands in the queue, simulating like we are in the middle of a script
1270+
app.echo = True
1271+
app.cmdqueue = ['help history', 'quit', 'eos']
1272+
app._script_dir.append('some_dir')
1273+
1274+
assert app._current_script_dir is not None
1275+
1276+
# Run the inner _cmdloop
1277+
app._cmdloop()
1278+
1279+
out, err = capsys.readouterr()
1280+
1281+
# Check the output
1282+
assert app.cmdqueue == []
1283+
assert app._current_script_dir is None
1284+
assert out.startswith('help history\n' + 'history [arg]: lists past commands issued')

0 commit comments

Comments
 (0)