Skip to content

Commit

Permalink
Merge pull request Textualize#1580 from Psycojoker/is-atty-broken-in-…
Browse files Browse the repository at this point in the history
…pytest

fix a edge case bug when running under pytest
  • Loading branch information
willmcgugan authored Oct 16, 2021
2 parents 0dc08ea + 8a33ce2 commit 7029cae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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).

## [10.12.1] - unreleased

### Fixed

- Fixed an edge case bug when console module try to detect if they are in a tty at the end of a pytest run

## [10.12.0] - 2021-10-06

### Updated
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following people have contributed to the development of Rich:
- [Will McGugan](https://github.com/willmcgugan)
- [Nathan Page](https://github.com/nathanrpage97)
- [Avi Perl](https://github.com/avi-perl)
- [Laurent Peuch](https://github.com/psycojoker)
- [Kylian Point](https://github.com/p0lux)
- [Kyle Pollina](https://github.com/kylepollina)
- [Clément Robert](https://github.com/neutrinoceros)
Expand Down
8 changes: 7 additions & 1 deletion rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,13 @@ def is_terminal(self) -> bool:
if self._force_terminal is not None:
return self._force_terminal
isatty: Optional[Callable[[], bool]] = getattr(self.file, "isatty", None)
return False if isatty is None else isatty()
try:
return False if isatty is None else isatty()
except ValueError:
# in some situation (at the end of a pytest run for example) isatty() can raise
# ValueError: I/O operation on closed file
# return False because we aren't in a terminal anymore
return False

@property
def is_dumb_terminal(self) -> bool:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,14 @@ def test_print_newline_start():
result = console.end_capture()

assert result == "Foo\n\nFoo\nbar\n\n"


def test_is_terminal_broken_file():
console = Console()

def _mock_isatty():
raise ValueError()

console.file.isatty = _mock_isatty

assert console.is_terminal == False

0 comments on commit 7029cae

Please sign in to comment.