Skip to content

Commit 8d704b4

Browse files
author
Todd Leonhardt
committed
Try to import gnureadline and then if that fails import readline
1 parent f273901 commit 8d704b4

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

cmd2.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
# noinspection PyUnresolvedReferences
6363
from six.moves.urllib.request import urlopen
6464

65+
# Prefer statically linked gnureadline if available (for Mac OS X compatibility due to issues with libedit)
66+
try:
67+
import gnureadline
68+
except ImportError:
69+
import readline
70+
6571
# Python 3 compatibility hack due to no built-in file keyword in Python 3
6672
# Due to one occurrence of isinstance(<foo>, file) checking to see if something is of file type
6773
try:
@@ -1111,9 +1117,8 @@ def pseudo_raw_input(self, prompt):
11111117
if not len(line):
11121118
line = 'EOF'
11131119
else:
1114-
if line[-1] == '\n': # this was always true in Cmd
1115-
line = line[:-1]
1116-
return line
1120+
line = line.rstrip('\r\n')
1121+
return line.strip()
11171122

11181123
def _cmdloop(self):
11191124
"""Repeatedly issue a prompt, accept input, parse an initial prefix
@@ -1127,14 +1132,9 @@ def _cmdloop(self):
11271132
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
11281133
# has been split out so that it can be called separately
11291134
if self.use_rawinput and self.completekey:
1130-
try:
1131-
# noinspection PyUnresolvedReferences
1132-
import readline
1133-
self.old_completer = readline.get_completer()
1134-
readline.set_completer(self.complete)
1135-
readline.parse_and_bind(self.completekey + ": complete")
1136-
except ImportError:
1137-
pass
1135+
self.old_completer = readline.get_completer()
1136+
readline.set_completer(self.complete)
1137+
readline.parse_and_bind(self.completekey + ": complete")
11381138
stop = None
11391139
try:
11401140
while not stop:
@@ -1148,12 +1148,7 @@ def _cmdloop(self):
11481148
stop = self.onecmd_plus_hooks(line)
11491149
finally:
11501150
if self.use_rawinput and self.completekey:
1151-
try:
1152-
# noinspection PyUnresolvedReferences
1153-
import readline
1154-
readline.set_completer(self.old_completer)
1155-
except ImportError:
1156-
pass
1151+
readline.set_completer(self.old_completer)
11571152
return stop
11581153

11591154
# noinspection PyUnusedLocal

0 commit comments

Comments
 (0)