Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement isatty() to make the CaptureOutput compatible with Django requirements #704

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading