Skip to content

Commit

Permalink
fix: Partial fix for file-descriptor-level capture of stdout and stde…
Browse files Browse the repository at this point in the history
…rr on the annoying operating system

Issue-duty#23: pawamoy/duty#23
  • Loading branch information
pawamoy committed Oct 22, 2024
1 parent b1f27df commit 9916580
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/failprint/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ def __init__(self, capture: Capture = Capture.BOTH, stdin: str | None = None) ->
self._saved_stdout_fd: int = -1
self._saved_stderr_fd: int = -1
self._output: str | None = None
self._old_env: dict[str, str] = {}

def __enter__(self) -> CaptureManager: # noqa: PYI034 (false-positive)
# Annoying operating system being annoying again.
# Bash ports and WSL2 work fine, this environment variable
# just helps a bit with cmd and PowerShell.
self._old_env["PYTHONLEGACYWINDOWSSTDIO"] = os.getenv("PYTHONLEGACYWINDOWSSTDIO")
os.environ["PYTHONLEGACYWINDOWSSTDIO"] = "1"

if self._capture is Capture.NONE:
return self

Expand Down Expand Up @@ -185,6 +192,13 @@ def __exit__(
self._output = self._temp_file.read()
self._temp_file.close()

# Restore environment variables.
for key, value in self._old_env.items():
if value is None:
del os.environ[key]
else:
os.environ[key] = value

def __str__(self) -> str:
return self.output

Expand Down

0 comments on commit 9916580

Please sign in to comment.