Skip to content

Commit fe327be

Browse files
ImHoppyBastian-Krause
authored andcommitted
util/helper: add stderr param to ProcessWrapper.check_output()
Allow separation of stderr from the output. For example, stderr can be ignored by redirecting it to subprocess.DEVNULL or prevent capturing by passing None, similar to how subprocess handles it. Signed-off-by: Hoppy <dev.hoppy@gmail.com> [bst: use default similar to subprocess: subprocess.STDOUT means merging stdout and stderr, None means no capturing; raise on unsupported stderr values] Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent 063d1bd commit fe327be

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

labgrid/util/helper.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ class ProcessWrapper:
4141
loglevel = logging.INFO
4242

4343
@step(args=['command'], result=True, tag='process')
44-
def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=None, timeout=None): # pylint: disable=redefined-builtin
44+
def check_output(
45+
self, command, *, print_on_silent_log=False, input=None, stdin=None, stderr=subprocess.STDOUT, timeout=None
46+
): # pylint: disable=redefined-builtin
4547
"""Run a command and supply the output to callback functions"""
48+
49+
if stderr not in (None, subprocess.STDOUT, subprocess.DEVNULL):
50+
raise NotImplementedError(f"{stderr=} not supported")
51+
4652
logger = logging.getLogger("Process")
4753
res = []
4854
mfd, sfd = pty.openpty()
@@ -59,8 +65,7 @@ def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=
5965
elif stdin is not None:
6066
kwargs['stdin'] = stdin
6167

62-
process = subprocess.Popen(command, stderr=sfd,
63-
stdout=sfd, bufsize=0, **kwargs)
68+
process = subprocess.Popen(command, stderr=stderr, stdout=sfd, bufsize=0, **kwargs)
6469

6570
logger.log(ProcessWrapper.loglevel, "[%d] command: %s", process.pid, " ".join(command))
6671

0 commit comments

Comments
 (0)