Skip to content

Commit 660bd38

Browse files
author
Todd Leonhardt
committed
Added some checks to deal with unit testing on Windows systems with no readline
NOTE: For real use on Windows you should have pyreadline installed.
1 parent 8d704b4 commit 660bd38

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

cmd2.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@
6464

6565
# Prefer statically linked gnureadline if available (for Mac OS X compatibility due to issues with libedit)
6666
try:
67-
import gnureadline
67+
import gnureadline as readline
6868
except ImportError:
69-
import readline
69+
try:
70+
import readline
71+
except ImportError:
72+
pass
7073

7174
# Python 3 compatibility hack due to no built-in file keyword in Python 3
7275
# Due to one occurrence of isinstance(<foo>, file) checking to see if something is of file type
@@ -1132,9 +1135,12 @@ def _cmdloop(self):
11321135
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
11331136
# has been split out so that it can be called separately
11341137
if self.use_rawinput and self.completekey:
1135-
self.old_completer = readline.get_completer()
1136-
readline.set_completer(self.complete)
1137-
readline.parse_and_bind(self.completekey + ": complete")
1138+
try:
1139+
self.old_completer = readline.get_completer()
1140+
readline.set_completer(self.complete)
1141+
readline.parse_and_bind(self.completekey + ": complete")
1142+
except NameError:
1143+
pass
11381144
stop = None
11391145
try:
11401146
while not stop:
@@ -1148,7 +1154,10 @@ def _cmdloop(self):
11481154
stop = self.onecmd_plus_hooks(line)
11491155
finally:
11501156
if self.use_rawinput and self.completekey:
1151-
readline.set_completer(self.old_completer)
1157+
try:
1158+
readline.set_completer(self.old_completer)
1159+
except NameError:
1160+
pass
11521161
return stop
11531162

11541163
# noinspection PyUnusedLocal

0 commit comments

Comments
 (0)