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)
Markus Gerstel
2 years ago
0 | 0 |
=======
|
1 | 1 |
History
|
2 | 2 |
=======
|
|
3 |
|
|
4 |
2.3.2 (2022-01-28)
|
|
5 |
------------------
|
|
6 |
* The run() function now understands stdin=subprocess.DEVNULL to close the subprocess stdin,
|
|
7 |
rather than to connect through the existing stdin, which is the current default
|
3 | 8 |
|
4 | 9 |
2.3.1 (2021-10-25)
|
5 | 10 |
------------------
|
454 | 454 |
:param array command: Command line to be run, specified as array.
|
455 | 455 |
:param timeout: Terminate program execution after this many seconds.
|
456 | 456 |
:param boolean debug: Enable further debug messages. (deprecated)
|
457 | |
:param stdin: Optional bytestring that is passed to command stdin.
|
|
457 |
:param stdin: Optional bytestring that is passed to command stdin,
|
|
458 |
or subprocess.DEVNULL to disable stdin.
|
458 | 459 |
:param boolean print_stdout: Pass stdout through to sys.stdout.
|
459 | 460 |
:param boolean print_stderr: Pass stderr through to sys.stderr.
|
460 | 461 |
:param callback_stdout: Optional function which is called for each
|
|
484 | 485 |
|
485 | 486 |
if stdin is None:
|
486 | 487 |
stdin_pipe = None
|
|
488 |
elif isinstance(stdin, int):
|
|
489 |
assert (
|
|
490 |
stdin == subprocess.DEVNULL
|
|
491 |
), "stdin argument only allows subprocess.DEVNULL as numeric argument"
|
|
492 |
stdin_pipe = subprocess.DEVNULL
|
|
493 |
stdin = None
|
487 | 494 |
else:
|
488 | 495 |
assert sys.platform != "win32", "stdin argument not supported on Windows"
|
489 | 496 |
stdin_pipe = subprocess.PIPE
|
14 | 14 |
command = ["echo", "hello"]
|
15 | 15 |
|
16 | 16 |
result = procrunner.run(command)
|
|
17 |
|
|
18 |
assert result.returncode == 0
|
|
19 |
assert result.stdout == b"hello" + os.linesep.encode("utf-8")
|
|
20 |
assert result.stderr == b""
|
|
21 |
|
|
22 |
|
|
23 |
def test_simple_command_invocation_with_closed_stdin():
|
|
24 |
if os.name == "nt":
|
|
25 |
command = ["cmd.exe", "/c", "echo", "hello"]
|
|
26 |
else:
|
|
27 |
command = ["echo", "hello"]
|
|
28 |
|
|
29 |
result = procrunner.run(command, stdin=subprocess.DEVNULL)
|
17 | 30 |
|
18 | 31 |
assert result.returncode == 0
|
19 | 32 |
assert result.stdout == b"hello" + os.linesep.encode("utf-8")
|