Skip to content

Commit 03fdbf3

Browse files
authored
Merge pull request #396 from python-cmd2/readline_warning_py2
Readline warning py2
2 parents 4fd7ec3 + cbf1a7c commit 03fdbf3

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

cmd2.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,21 @@ class RlType(Enum):
155155
orig_pyreadline_display = readline.rl.mode._display_completions
156156

157157
elif 'gnureadline' in sys.modules or 'readline' in sys.modules:
158-
rl_type = RlType.GNU
158+
# We don't support libedit
159+
if 'libedit' not in readline.__doc__:
160+
rl_type = RlType.GNU
159161

160-
# We need wcswidth to calculate display width of tab completions
161-
from wcwidth import wcswidth
162+
# We need wcswidth to calculate display width of tab completions
163+
from wcwidth import wcswidth
162164

163-
# Load the readline lib so we can make changes to it
164-
import ctypes
165-
readline_lib = ctypes.CDLL(readline.__file__)
165+
# Load the readline lib so we can make changes to it
166+
import ctypes
167+
readline_lib = ctypes.CDLL(readline.__file__)
166168

169+
if rl_type == RlType.NONE:
170+
rl_err_msg = "Tab completion has been disabled since no supported version of readline was found\n"
171+
rl_err_msg += "To resolve this, install pyreadline on Windows or gnureadline on Mac\n"
172+
sys.stderr.write(rl_err_msg)
167173

168174
# BrokenPipeError and FileNotFoundError exist only in Python 3. Use IOError for Python 2.
169175
if six.PY3:
@@ -690,6 +696,9 @@ def strip_ansi(text):
690696
def _pop_readline_history(clear_history=True):
691697
"""Returns a copy of readline's history and optionally clears it (default)"""
692698
# noinspection PyArgumentList
699+
if rl_type == RlType.NONE:
700+
return []
701+
693702
history = [
694703
readline.get_history_item(i)
695704
for i in range(1, 1 + readline.get_current_history_length())
@@ -701,10 +710,11 @@ def _pop_readline_history(clear_history=True):
701710

702711
def _push_readline_history(history, clear_history=True):
703712
"""Restores readline's history and optionally clears it first (default)"""
704-
if clear_history:
705-
readline.clear_history()
706-
for line in history:
707-
readline.add_history(line)
713+
if rl_type != RlType.NONE:
714+
if clear_history:
715+
readline.clear_history()
716+
for line in history:
717+
readline.add_history(line)
708718

709719

710720
def _complete_from_cmd(cmd_obj, text, line, begidx, endidx):
@@ -838,7 +848,7 @@ def enter_submenu(parent_cmd, line):
838848
original_attributes = self._get_original_attributes()
839849
history = _pop_readline_history()
840850

841-
if self.persistent_history_file:
851+
if self.persistent_history_file and rl_type != RlType.NONE:
842852
try:
843853
readline.read_history_file(self.persistent_history_file)
844854
except FILE_NOT_FOUND_ERROR:
@@ -871,7 +881,7 @@ def enter_submenu(parent_cmd, line):
871881
self._copy_out_shared_attrs(parent_cmd, original_attributes)
872882

873883
# write submenu history
874-
if self.persistent_history_file:
884+
if self.persistent_history_file and rl_type != RlType.NONE:
875885
readline.write_history_file(self.persistent_history_file)
876886
# reset main app history before exit
877887
_push_readline_history(history)
@@ -1030,7 +1040,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
10301040
pass
10311041

10321042
# If persistent readline history is enabled, then read history from file and register to write to file at exit
1033-
if persistent_history_file:
1043+
if persistent_history_file and rl_type != RlType.NONE:
10341044
persistent_history_file = os.path.expanduser(persistent_history_file)
10351045
try:
10361046
readline.read_history_file(persistent_history_file)
@@ -1972,7 +1982,7 @@ def complete(self, text, state):
19721982
:param text: str - the current word that user is typing
19731983
:param state: int - non-negative integer
19741984
"""
1975-
if state == 0:
1985+
if state == 0 and rl_type != RlType.NONE:
19761986
unclosed_quote = ''
19771987
self.set_completion_defaults()
19781988

@@ -3061,9 +3071,12 @@ def select(self, opts, prompt='Your choice? '):
30613071
self.poutput(' %2d. %s\n' % (idx + 1, text))
30623072
while True:
30633073
response = sm.input(prompt)
3064-
hlen = readline.get_current_history_length()
3065-
if hlen >= 1 and response != '':
3066-
readline.remove_history_item(hlen - 1)
3074+
3075+
if rl_type != RlType.NONE:
3076+
hlen = readline.get_current_history_length()
3077+
if hlen >= 1 and response != '':
3078+
readline.remove_history_item(hlen - 1)
3079+
30673080
try:
30683081
response = int(response)
30693082
result = fulloptions[response - 1][0]

0 commit comments

Comments
 (0)