diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-05-08 09:05:15 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-08 09:05:15 -0400 |
| commit | 03fdbf35ca3e402b2f0d428eb4c4d831781df10c (patch) | |
| tree | 762a9d9c199686694e50ca49d1473df891912909 | |
| parent | 4fd7ec3f3b848044fd8997947b94577d6a04817c (diff) | |
| parent | cbf1a7c0afbf6bc580436341c4f6bfcdf9b0e902 (diff) | |
| download | cmd2-git-03fdbf35ca3e402b2f0d428eb4c4d831781df10c.tar.gz | |
Merge pull request #396 from python-cmd2/readline_warning_py2
Readline warning py2
| -rwxr-xr-x | cmd2.py | 47 |
1 files changed, 30 insertions, 17 deletions
@@ -155,15 +155,21 @@ if 'pyreadline' in sys.modules: orig_pyreadline_display = readline.rl.mode._display_completions elif 'gnureadline' in sys.modules or 'readline' in sys.modules: - rl_type = RlType.GNU + # We don't support libedit + if 'libedit' not in readline.__doc__: + rl_type = RlType.GNU - # We need wcswidth to calculate display width of tab completions - from wcwidth import wcswidth + # We need wcswidth to calculate display width of tab completions + from wcwidth import wcswidth - # Load the readline lib so we can make changes to it - import ctypes - readline_lib = ctypes.CDLL(readline.__file__) + # Load the readline lib so we can make changes to it + import ctypes + readline_lib = ctypes.CDLL(readline.__file__) +if rl_type == RlType.NONE: + rl_err_msg = "Tab completion has been disabled since no supported version of readline was found\n" + rl_err_msg += "To resolve this, install pyreadline on Windows or gnureadline on Mac\n" + sys.stderr.write(rl_err_msg) # BrokenPipeError and FileNotFoundError exist only in Python 3. Use IOError for Python 2. if six.PY3: @@ -690,6 +696,9 @@ def strip_ansi(text): def _pop_readline_history(clear_history=True): """Returns a copy of readline's history and optionally clears it (default)""" # noinspection PyArgumentList + if rl_type == RlType.NONE: + return [] + history = [ readline.get_history_item(i) for i in range(1, 1 + readline.get_current_history_length()) @@ -701,10 +710,11 @@ def _pop_readline_history(clear_history=True): def _push_readline_history(history, clear_history=True): """Restores readline's history and optionally clears it first (default)""" - if clear_history: - readline.clear_history() - for line in history: - readline.add_history(line) + if rl_type != RlType.NONE: + if clear_history: + readline.clear_history() + for line in history: + readline.add_history(line) def _complete_from_cmd(cmd_obj, text, line, begidx, endidx): @@ -838,7 +848,7 @@ class AddSubmenu(object): original_attributes = self._get_original_attributes() history = _pop_readline_history() - if self.persistent_history_file: + if self.persistent_history_file and rl_type != RlType.NONE: try: readline.read_history_file(self.persistent_history_file) except FILE_NOT_FOUND_ERROR: @@ -871,7 +881,7 @@ class AddSubmenu(object): self._copy_out_shared_attrs(parent_cmd, original_attributes) # write submenu history - if self.persistent_history_file: + if self.persistent_history_file and rl_type != RlType.NONE: readline.write_history_file(self.persistent_history_file) # reset main app history before exit _push_readline_history(history) @@ -1030,7 +1040,7 @@ class Cmd(cmd.Cmd): pass # If persistent readline history is enabled, then read history from file and register to write to file at exit - if persistent_history_file: + if persistent_history_file and rl_type != RlType.NONE: persistent_history_file = os.path.expanduser(persistent_history_file) try: readline.read_history_file(persistent_history_file) @@ -1972,7 +1982,7 @@ class Cmd(cmd.Cmd): :param text: str - the current word that user is typing :param state: int - non-negative integer """ - if state == 0: + if state == 0 and rl_type != RlType.NONE: unclosed_quote = '' self.set_completion_defaults() @@ -3061,9 +3071,12 @@ Usage: Usage: unalias [-a] name [name ...] self.poutput(' %2d. %s\n' % (idx + 1, text)) while True: response = sm.input(prompt) - hlen = readline.get_current_history_length() - if hlen >= 1 and response != '': - readline.remove_history_item(hlen - 1) + + if rl_type != RlType.NONE: + hlen = readline.get_current_history_length() + if hlen >= 1 and response != '': + readline.remove_history_item(hlen - 1) + try: response = int(response) result = fulloptions[response - 1][0] |
