Skip to content

Commit

Permalink
[live] Restore sys.stderr when an exception happens during a refresh
Browse files Browse the repository at this point in the history
Without this the user sees no error displayed in their terminal - and even capturing the exception and displayed their own error reporting is ineffective.
  • Loading branch information
olivierphi committed May 27, 2022
1 parent aa7926c commit d570d94
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions rich/live.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d570d94

Please sign in to comment.