Skip to content

Commit 76cbd98

Browse files
committed
Allow stdin=subprocess.DEVNULL
The current default is to connect through the existing stdin to the subprocess, and there is no way to explicitly close the subprocess stdin. Allow stdin=subprocess.DEVNULL for this. (Backport from v3.0)
1 parent 331ab3b commit 76cbd98

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
History
33
=======
44

5+
2.3.2 (2022-01-28)
6+
------------------
7+
* The run() function now understands stdin=subprocess.DEVNULL to close the subprocess stdin,
8+
rather than to connect through the existing stdin, which is the current default
9+
510
2.3.1 (2021-10-25)
611
------------------
712
* Add Python 3.10 support

procrunner/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ def run(
455455
:param array command: Command line to be run, specified as array.
456456
:param timeout: Terminate program execution after this many seconds.
457457
:param boolean debug: Enable further debug messages. (deprecated)
458-
:param stdin: Optional bytestring that is passed to command stdin.
458+
:param stdin: Optional bytestring that is passed to command stdin,
459+
or subprocess.DEVNULL to disable stdin.
459460
:param boolean print_stdout: Pass stdout through to sys.stdout.
460461
:param boolean print_stderr: Pass stderr through to sys.stderr.
461462
:param callback_stdout: Optional function which is called for each
@@ -485,6 +486,12 @@ def run(
485486

486487
if stdin is None:
487488
stdin_pipe = None
489+
elif isinstance(stdin, int):
490+
assert (
491+
stdin == subprocess.DEVNULL
492+
), "stdin argument only allows subprocess.DEVNULL as numeric argument"
493+
stdin_pipe = subprocess.DEVNULL
494+
stdin = None
488495
else:
489496
assert sys.platform != "win32", "stdin argument not supported on Windows"
490497
stdin_pipe = subprocess.PIPE

tests/test_procrunner_system.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ def test_simple_command_invocation():
2121
assert result.stderr == b""
2222

2323

24+
def test_simple_command_invocation_with_closed_stdin():
25+
if os.name == "nt":
26+
command = ["cmd.exe", "/c", "echo", "hello"]
27+
else:
28+
command = ["echo", "hello"]
29+
30+
result = procrunner.run(command, stdin=subprocess.DEVNULL)
31+
32+
assert result.returncode == 0
33+
assert result.stdout == b"hello" + os.linesep.encode("utf-8")
34+
assert result.stderr == b""
35+
36+
2437
def test_decode_invalid_utf8_input(capsys):
2538
test_string = b"test\xa0string\n"
2639
if os.name == "nt":

0 commit comments

Comments
 (0)