Skip to content

Commit

Permalink
Keep devnull behavior compatible with subprocess.run (#33)
Browse files Browse the repository at this point in the history
Fixes: #18
  • Loading branch information
ssbarnea authored Apr 11, 2021
1 parent 938b78f commit 5c49c54
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/subprocess_tee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:
if platform.system() == "Windows":
platform_settings["env"] = os.environ

# this part keeps behavior backwards compatible with subprocess.run
stdout = kwargs.get("stdout", sys.stdout)
if stdout == subprocess.DEVNULL:
stdout = open(os.devnull, "w")
stderr = kwargs.get("stderr", sys.stderr)
if stderr == subprocess.DEVNULL:
stderr = open(os.devnull, "w")

# We need to tell subprocess which shell to use when running shell-like
# commands.
# * SHELL is not always defined
Expand Down Expand Up @@ -68,13 +76,13 @@ def tee(line: bytes, sink: List[str], pipe: Optional[Any]) -> None:
if process.stdout:
tasks.append(
loop.create_task(
_read_stream(process.stdout, lambda l: tee(l, out, sys.stdout))
_read_stream(process.stdout, lambda l: tee(l, out, stdout))
)
)
if process.stderr:
tasks.append(
loop.create_task(
_read_stream(process.stderr, lambda l: tee(l, err, sys.stderr))
_read_stream(process.stderr, lambda l: tee(l, err, stderr))
)
)

Expand Down

0 comments on commit 5c49c54

Please sign in to comment.