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

Method run_and_show_expression_async

src/ptpython/repl.py:192–243  ·  view source on GitHub ↗
(self, text: str)

Source from the content-addressed store, hash-verified

190 self._remove_from_namespace()
191
192 async def run_and_show_expression_async(self, text: str) -> Any:
193 loop = asyncio.get_running_loop()
194 system_exit: SystemExit | None = None
195
196 try:
197 try:
198 # Create `eval` task. Ensure that control-c will cancel this
199 # task.
200 async def eval() -> Any:
201 nonlocal system_exit
202 try:
203 return await self.eval_async(text)
204 except SystemExit as e:
205 # Don't propagate SystemExit in `create_task()`. That
206 # will kill the event loop. We want to handle it
207 # gracefully.
208 system_exit = e
209
210 task = asyncio.create_task(eval())
211 loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel())
212 result = await task
213
214 if system_exit is not None:
215 raise system_exit
216 except KeyboardInterrupt:
217 # KeyboardInterrupt doesn't inherit from Exception.
218 raise
219 except SystemExit:
220 raise
221 except BaseException as e:
222 self._handle_exception(e)
223 else:
224 # Print.
225 if result is not None:
226 await loop.run_in_executor(None, lambda: self._show_result(result))
227
228 # Loop.
229 self.current_statement_index += 1
230 self.signatures = []
231 # Return the result for future consumers.
232 return result
233 finally:
234 loop.remove_signal_handler(signal.SIGINT)
235
236 except KeyboardInterrupt as e:
237 # Handle all possible `KeyboardInterrupt` errors. This can
238 # happen during the `eval`, but also during the
239 # `show_result` if something takes too long.
240 # (Try/catch is around the whole block, because we want to
241 # prevent that a Control-C keypress terminates the REPL in
242 # any case.)
243 self._handle_keyboard_interrupt(e)
244
245 async def run_async(self) -> None:
246 """

Callers 1

run_asyncMethod · 0.95

Calls 3

_handle_exceptionMethod · 0.95
_show_resultMethod · 0.95

Tested by

no test coverage detected