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 cf99a8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305

## [12.4.4] - 2022-05-24

### Changed
Expand Down
10 changes: 9 additions & 1 deletion 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

0 comments on commit cf99a8d

Please sign in to comment.