Skip to content

Commit

Permalink
Merge pull request #704 from prospector-dev/bugfiz/703-isatty-django
Browse files Browse the repository at this point in the history
Implement isatty() to make the CaptureOutput compatible with Django requirements
  • Loading branch information
carlio authored Nov 19, 2024
2 parents 132a13a + 16af5ba commit 27c00b0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions prospector/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@


class CaptureStream(TextIOWrapper):
def __init__(self) -> None:
def __init__(self, tty: bool) -> None:
self.contents = ""
self._tty = tty

def write(self, text: str, /) -> int:
self.contents += text
Expand All @@ -17,6 +18,9 @@ def close(self) -> None:
def flush(self) -> None:
pass

def isatty(self) -> bool:
return self._tty


class CaptureOutput:
_prev_streams = None
Expand All @@ -28,14 +32,16 @@ def __init__(self, hide: bool) -> None:

def __enter__(self) -> "CaptureOutput":
if self.hide:
is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()

self._prev_streams = (
sys.stdout,
sys.stderr,
sys.__stdout__,
sys.__stderr__,
)
self.stdout = CaptureStream()
self.stderr = CaptureStream()
self.stdout = CaptureStream(is_a_tty)
self.stderr = CaptureStream(is_a_tty)
sys.stdout, sys.__stdout__ = self.stdout, self.stdout # type: ignore[misc]
sys.stderr, sys.__stderr__ = self.stderr, self.stderr # type: ignore[misc]
return self
Expand Down

0 comments on commit 27c00b0

Please sign in to comment.