MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _run_python

Method _run_python

cmd2/cmd2.py:4862–5007  ·  view source on GitHub ↗

Run an interactive Python shell or execute a pyscript file. Called by do_py() and do_run_pyscript(). If pyscript is None, then this function runs an interactive Python shell. Otherwise, it runs the pyscript file. :param pyscript: optional path to a pyscript file to

(self, *, pyscript: str | None = None)

Source from the content-addressed store, hash-verified

4860 readline.set_completer(cmd2_env.completer)
4861
4862 def _run_python(self, *, pyscript: str | None = None) -> bool | None:
4863 """Run an interactive Python shell or execute a pyscript file.
4864
4865 Called by do_py() and do_run_pyscript().
4866
4867 If pyscript is None, then this function runs an interactive Python shell.
4868 Otherwise, it runs the pyscript file.
4869
4870 :param pyscript: optional path to a pyscript file to run. This is intended only to be used by do_run_pyscript()
4871 after it sets up sys.argv for the script. (Defaults to None)
4872 :return: True if running of commands should stop
4873 """
4874 self.last_result = False
4875
4876 # Replace print() in the embedded Python environment. Standard print() writes to
4877 # sys.stdout, which bypasses cmd2 redirection (e.g., run_pyscript script.py > out.txt).
4878 # Using self.print_to(self.stdout) ensures output is capturable and respects 'allow_style'
4879 # without requiring the user to have access to 'self'.
4880 def py_print(
4881 *objects: Any,
4882 sep: str = " ",
4883 end: str = "\n",
4884 file: IO[str] | None = None,
4885 flush: bool = False, # noqa: ARG001
4886 ) -> None:
4887 """Print objects to a stream, defaulting to self.stdout.
4888
4889 This is used as the print() function within interactive Python shells and pyscripts.
4890 It wraps cmd2's print_to() method to honor output redirection and style settings.
4891
4892 :param objects: objects to print (including Rich objects)
4893 :param sep: string to write between printed text. Defaults to " ".
4894 :param end: string to write at end of printed text. Defaults to a newline.
4895 :param file: file stream being written to. Defaults to self.stdout.
4896 :param flush: ignored as Rich-based output is flushed automatically. Defaults to False.
4897 """
4898 if file is None:
4899 file = self.stdout
4900
4901 self.print_to(file, *objects, sep=sep, end=end)
4902
4903 # Replace quit/exit in the embedded Python environment. Standard sys.exit()
4904 # would kill the entire application process; raising EmbeddedConsoleExit
4905 # allows the interpreter to return gracefully to the cmd2 prompt.
4906 def py_quit() -> None:
4907 """Exit an interactive Python shell or pyscript."""
4908 raise EmbeddedConsoleExit
4909
4910 from .py_bridge import PyBridge
4911
4912 add_to_history = self.scripts_add_to_history if pyscript else True
4913 py_bridge = PyBridge(self, add_to_history=add_to_history)
4914 saved_sys_path = None
4915
4916 if self.in_pyscript():
4917 self.perror("Recursively entering interactive Python shells is not allowed")
4918 return None
4919

Callers 2

do_pyMethod · 0.95
do_run_pyscriptMethod · 0.95

Calls 7

in_pyscriptMethod · 0.95
perrorMethod · 0.95
_set_up_py_shell_envMethod · 0.95
_restore_cmd2_envMethod · 0.95
poutputMethod · 0.95
PyBridgeClass · 0.85
readMethod · 0.80

Tested by

no test coverage detected