Skip to content

Commit 5c49c54

Browse files
authored
Keep devnull behavior compatible with subprocess.run (#33)
Fixes: #18
1 parent 938b78f commit 5c49c54

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/subprocess_tee/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:
3232
if platform.system() == "Windows":
3333
platform_settings["env"] = os.environ
3434

35+
# this part keeps behavior backwards compatible with subprocess.run
36+
stdout = kwargs.get("stdout", sys.stdout)
37+
if stdout == subprocess.DEVNULL:
38+
stdout = open(os.devnull, "w")
39+
stderr = kwargs.get("stderr", sys.stderr)
40+
if stderr == subprocess.DEVNULL:
41+
stderr = open(os.devnull, "w")
42+
3543
# We need to tell subprocess which shell to use when running shell-like
3644
# commands.
3745
# * SHELL is not always defined
@@ -68,13 +76,13 @@ def tee(line: bytes, sink: List[str], pipe: Optional[Any]) -> None:
6876
if process.stdout:
6977
tasks.append(
7078
loop.create_task(
71-
_read_stream(process.stdout, lambda l: tee(l, out, sys.stdout))
79+
_read_stream(process.stdout, lambda l: tee(l, out, stdout))
7280
)
7381
)
7482
if process.stderr:
7583
tasks.append(
7684
loop.create_task(
77-
_read_stream(process.stderr, lambda l: tee(l, err, sys.stderr))
85+
_read_stream(process.stderr, lambda l: tee(l, err, stderr))
7886
)
7987
)
8088

0 commit comments

Comments
 (0)