MCPcopy
hub / github.com/prompt-toolkit/ptpython / PythonRepl

Class PythonRepl

src/ptpython/repl.py:84–436  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82
83
84class PythonRepl(PythonInput):
85 def __init__(self, *a, **kw) -> None:
86 self._startup_paths: Sequence[str | Path] | None = kw.pop("startup_paths", None)
87 super().__init__(*a, **kw)
88 self._load_start_paths()
89
90 def _load_start_paths(self) -> None:
91 "Start the Read-Eval-Print Loop."
92 if self._startup_paths:
93 for path in self._startup_paths:
94 if os.path.exists(path):
95 with open(path, "rb") as f:
96 code = compile(f.read(), path, "exec")
97 exec(code, self.get_globals(), self.get_locals())
98 else:
99 output = self.app.output
100 output.write(f"WARNING | File not found: {path}\n\n")
101
102 def run_and_show_expression(self, expression: str) -> None:
103 try:
104 # Eval.
105 try:
106 result = self.eval(expression)
107 except KeyboardInterrupt:
108 # KeyboardInterrupt doesn't inherit from Exception.
109 raise
110 except SystemExit:
111 raise
112 except ReplExit:
113 raise
114 except BaseException as e:
115 self._handle_exception(e)
116 else:
117 if isinstance(result, exit):
118 # When `exit` is evaluated without parentheses.
119 # Automatically trigger the `ReplExit` exception.
120 raise ReplExit
121
122 # Print.
123 if result is not None:
124 self._show_result(result)
125 if self.insert_blank_line_after_output:
126 self.app.output.write("\n")
127
128 # Loop.
129 self.current_statement_index += 1
130 self.signatures = []
131
132 except KeyboardInterrupt as e:
133 # Handle all possible `KeyboardInterrupt` errors. This can
134 # happen during the `eval`, but also during the
135 # `show_result` if something takes too long.
136 # (Try/catch is around the whole block, because we want to
137 # prevent that a Control-C keypress terminates the REPL in
138 # any case.)
139 self._handle_keyboard_interrupt(e)
140
141 def _get_output_printer(self) -> OutputPrinter:

Callers 2

__init__Method · 0.90
embedFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected