Set up interactive Python shell environment. :return: Class containing saved up cmd2 environment.
(self, interp: InteractiveConsole)
| 4820 | sys.excepthook = sys.__excepthook__ |
| 4821 | |
| 4822 | def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env: |
| 4823 | """Set up interactive Python shell environment. |
| 4824 | |
| 4825 | :return: Class containing saved up cmd2 environment. |
| 4826 | """ |
| 4827 | cmd2_env = _SavedCmd2Env() |
| 4828 | |
| 4829 | # Set up sys module for the Python console |
| 4830 | self._reset_py_display() |
| 4831 | |
| 4832 | # Enable completion if readline is available |
| 4833 | if not sys.platform.startswith("win"): |
| 4834 | import readline |
| 4835 | import rlcompleter |
| 4836 | |
| 4837 | # Save the current completer |
| 4838 | cmd2_env.completer = readline.get_completer() |
| 4839 | |
| 4840 | # Set the completer to use the interpreter's locals |
| 4841 | readline.set_completer(rlcompleter.Completer(interp.locals).complete) # type: ignore[arg-type] |
| 4842 | |
| 4843 | # Use the correct binding based on whether LibEdit or Readline is being used |
| 4844 | if "libedit" in (readline.__doc__ or ""): |
| 4845 | readline.parse_and_bind("bind ^I rl_complete") |
| 4846 | else: |
| 4847 | readline.parse_and_bind("tab: complete") |
| 4848 | |
| 4849 | return cmd2_env |
| 4850 | |
| 4851 | def _restore_cmd2_env(self, cmd2_env: _SavedCmd2Env) -> None: |
| 4852 | """Restore cmd2 environment after exiting an interactive Python shell. |