From d570d9429ab85d59982d70f9e0a407e63856cdbe Mon Sep 17 00:00:00 2001 From: Olivier Philippon Date: Fri, 27 May 2022 12:00:01 +0100 Subject: [PATCH] [live] Restore `sys.stderr` when an exception happens during a refresh Without this the user sees no error displayed in their terminal - and even capturing the exception and displayed their own error reporting is ineffective. --- rich/live.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/rich/live.py b/rich/live.py index 6db5b605f9..2b2d0a8559 100644 --- a/rich/live.py +++ b/rich/live.py @@ -118,7 +118,15 @@ def start(self, refresh: bool = False) -> None: self._enable_redirect_io() self.console.push_render_hook(self) if refresh: - self.refresh() + try: + self.refresh() + except Exception: + # If refresh fails, we want to stop the redirection of sys.stderr, + # so the error stacktrace is properly displayed in the terminal. + # (or, if the code that calls Rich captures the exception and wants to display something, + # let this be displayed in the terminal). + self.stop() + raise if self.auto_refresh: self._refresh_thread = _RefreshThread(self, self.refresh_per_second) self._refresh_thread.start() @@ -210,7 +218,10 @@ def update(self, renderable: RenderableType, *, refresh: bool = False) -> None: def refresh(self) -> None: """Update the display of the Live Render.""" with self._lock: - self._live_render.set_renderable(self.renderable) + try: + self._live_render.set_renderable(self.renderable) + except Exception as e: + t = 2 if self.console.is_jupyter: # pragma: no cover try: from IPython.display import display