Skip to content

Commit

Permalink
[#703] Django needs to know if the output stream is a TTY in some cas…
Browse files Browse the repository at this point in the history
…es, so the capture output stream needs to minic this behaviour for cases where prospector is being used with Django
  • Loading branch information
carlio committed Nov 19, 2024
1 parent 132a13a commit 16af5ba
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 16af5ba

Please sign in to comment.