diff options
| author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-05-24 02:21:22 -0400 |
|---|---|---|
| committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-05-24 02:21:22 -0400 |
| commit | 03701ae897f7ee5deac2c9dfb2c52553488aee98 (patch) | |
| tree | 349f2cb9d6a6af0a44123b213c22722c9c7cb8e0 | |
| parent | 55350028a39703eed9e543809ee6a2ecdbe9da0c (diff) | |
| download | cmd2-git-03701ae897f7ee5deac2c9dfb2c52553488aee98.tar.gz | |
Made Python console tab complete from the correct namespace.
Reduced the amount of code within a try block.
| -rw-r--r-- | cmd2/cmd2.py | 117 |
1 files changed, 58 insertions, 59 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index a534f2a1..6ae53079 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -432,6 +432,7 @@ class Cmd(cmd.Cmd): self.initial_stdout = sys.stdout self.history = History() self.pystate = {} + self.py_history = [] self.pyscript_name = 'app' self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')] self.statement_parser = StatementParser( @@ -2573,73 +2574,71 @@ Usage: Usage: unalias [-a] name [name ...] self.pystate['quit'] = quit self.pystate['exit'] = quit - keepstate = None - try: - if rl_type != RlType.NONE: - # Save cmd2 history - saved_cmd2_history = [] - for i in range(1, readline.get_current_history_length() + 1): - saved_cmd2_history.append(readline.get_history_item(i)) - - readline.clear_history() - - # Restore py's history - # noinspection PyAttributeOutsideInit - self.py_history = getattr(self, 'py_history', []) - for item in self.py_history: - readline.add_history(item) - - if self.use_rawinput and self.completekey: - # Set up tab completion for the Python console - # rlcompleter relies on the default settings of the Python readline module - if rl_type == RlType.GNU: - old_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value - rl_basic_quote_characters.value = orig_rl_basic_quotes - - if 'gnureadline' in sys.modules: - # rlcompleter imports readline by name, so it won't use gnureadline - # Force rlcompleter to use gnureadline instead so it has our settings and history - saved_readline = None - if 'readline' in sys.modules: - saved_readline = sys.modules['readline'] + # Set up readline for Python console + if rl_type != RlType.NONE: + # Save cmd2 history + saved_cmd2_history = [] + for i in range(1, readline.get_current_history_length() + 1): + saved_cmd2_history.append(readline.get_history_item(i)) + + readline.clear_history() + + # Restore py's history + for item in self.py_history: + readline.add_history(item) + + if self.use_rawinput and self.completekey: + # Set up tab completion for the Python console + # rlcompleter relies on the default settings of the Python readline module + if rl_type == RlType.GNU: + old_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value + rl_basic_quote_characters.value = orig_rl_basic_quotes + + if 'gnureadline' in sys.modules: + # rlcompleter imports readline by name, so it won't use gnureadline + # Force rlcompleter to use gnureadline instead so it has our settings and history + saved_readline = None + if 'readline' in sys.modules: + saved_readline = sys.modules['readline'] + + sys.modules['readline'] = sys.modules['gnureadline'] + + old_delims = readline.get_completer_delims() + readline.set_completer_delims(orig_rl_delims) + + # rlcompleter will not need cmd2's custom display function + # This will be restored by cmd2 the next time complete() is called + if rl_type == RlType.GNU: + readline.set_completion_display_matches_hook(None) + elif rl_type == RlType.PYREADLINE: + readline.rl.mode._display_completions = self._display_matches_pyreadline + + # Save off the current completer and set a new one in the Python console + # Make sure it tab completes from its locals() dictionary + old_completer = readline.get_completer() + interp.runcode("from rlcompleter import Completer") + interp.runcode("import readline") + interp.runcode("readline.set_completer(Completer(locals()).complete)") + + cprt = 'Type "help", "copyright", "credits" or "license" for more information.' + keepstate = Statekeeper(sys, ('stdin', 'stdout')) + sys.stdout = self.stdout + sys.stdin = self.stdin + docstr = self.do_py.__doc__.replace('pyscript_name', self.pyscript_name) - sys.modules['readline'] = sys.modules['gnureadline'] - - old_delims = readline.get_completer_delims() - readline.set_completer_delims(orig_rl_delims) - - # rlcompleter will not need cmd2's custom display function - # This will be restored by cmd2 the next time complete() is called - if rl_type == RlType.GNU: - readline.set_completion_display_matches_hook(None) - elif rl_type == RlType.PYREADLINE: - readline.rl.mode._display_completions = self._display_matches_pyreadline - - # Load rlcompleter so it sets its completer function - old_completer = readline.get_completer() - import rlcompleter - import importlib - importlib.reload(rlcompleter) - - cprt = 'Type "help", "copyright", "credits" or "license" for more information.' - keepstate = Statekeeper(sys, ('stdin', 'stdout')) - sys.stdout = self.stdout - sys.stdin = self.stdin - docstr = self.do_py.__doc__.replace('pyscript_name', self.pyscript_name) - interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" % - (sys.version, sys.platform, cprt, self.__class__.__name__, - docstr)) + try: + interp.interact(banner="Python {} on {}\n{}\n({})\n{}". + format(sys.version, sys.platform, cprt, self.__class__.__name__, docstr)) except EmbeddedConsoleExit: pass finally: - if keepstate is not None: - keepstate.restore() + keepstate.restore() + # Set up readline for cmd2 if rl_type != RlType.NONE: # Save py's history - # noinspection PyAttributeOutsideInit - self.py_history = [] + self.py_history.clear() for i in range(1, readline.get_current_history_length() + 1): self.py_history.append(readline.get_history_item(i)) |
