Codebase list python-procrunner / 6ca16b7
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. Markus Gerstel 2 years ago
3 changed file(s) with 25 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1515 (previously deprecated in `#62 <https://github.com/DiamondLightSource/python-procrunner/pull/62>`_)
1616 * The run() function no longer accepts a 'debug' argument
1717 (previously deprecated in `#63 <https://github.com/DiamondLightSource/python-procrunner/pull/63>`_)
18 * The run() function now understands stdin=subprocess.DEVNULL to close the subprocess stdin,
19 rather than to connect through the existing stdin, which is the current default
1820
1921 2.3.1 (2021-10-25)
2022 ------------------
1212 import warnings
1313 from multiprocessing import Pipe
1414 from threading import Thread
15 from typing import Any, Callable, Optional
15 from typing import Any, Callable, Optional, Union
1616
1717 #
1818 # run() - A function to synchronously run an external process, supporting
303303 command,
304304 *,
305305 timeout: Optional[float] = None,
306 stdin: Optional[bytes] = None,
306 stdin: Optional[Union[bytes, int]] = None,
307307 print_stdout: bool = True,
308308 print_stderr: bool = True,
309309 callback_stdout: Optional[Callable] = None,
322322
323323 :param array command: Command line to be run, specified as array.
324324 :param timeout: Terminate program execution after this many seconds.
325 :param stdin: Optional bytestring that is passed to command stdin.
325 :param stdin: Optional bytestring that is passed to command stdin,
326 or subprocess.DEVNULL to disable stdin.
326327 :param boolean print_stdout: Pass stdout through to sys.stdout.
327328 :param boolean print_stderr: Pass stderr through to sys.stderr.
328329 :param callback_stdout: Optional function which is called for each
347348
348349 if stdin is None:
349350 stdin_pipe = None
351 elif isinstance(stdin, int):
352 assert (
353 stdin == subprocess.DEVNULL
354 ), "stdin argument only allows subprocess.DEVNULL as numeric argument"
355 stdin_pipe = subprocess.DEVNULL
356 stdin = None
350357 else:
351358 assert sys.platform != "win32", "stdin argument not supported on Windows"
352359 stdin_pipe = subprocess.PIPE
1616 command = ["echo", "hello"]
1717
1818 result = procrunner.run(command)
19
20 assert result.returncode == 0
21 assert result.stdout == b"hello" + os.linesep.encode("utf-8")
22 assert result.stderr == b""
23
24
25 def test_simple_command_invocation_with_closed_stdin():
26 if os.name == "nt":
27 command = ["cmd.exe", "/c", "echo", "hello"]
28 else:
29 command = ["echo", "hello"]
30
31 result = procrunner.run(command, stdin=subprocess.DEVNULL)
1932
2033 assert result.returncode == 0
2134 assert result.stdout == b"hello" + os.linesep.encode("utf-8")