4

Python 3.5 introduced the run() function in the subprocess module as the new recommended high-level means of subprocess invocation.

Amongst the three older (available since Python 2.5 / 2.7) high-level API functions is check_call(). The Python 3.5 documentation claims that check_call()

[...] is equivalent to:

run(..., check=True)

The documentation also warns against passing subprocess.PIPE as stdout or stderr to check_call():

Note

Do not use stdout=PIPE or stderr=PIPE with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

As it's "equivalent", does this warning also apply to run(..., check=True), i.e., should

subprocess.run(..., stdout=subprocess.PIPE, check=True)

and

subprocess.run(..., stderr=subprocess.PIPE, check=True)

be avoided, too? (The documentation of run() doesn't mention this caveat.)

1 Answer 1

10

Is it safe to combine check=True with stdout=PIPE?

Yes.

The reason one should not use stdout=PIPE as an argument to subprocess.check_call (or subprocess.call) is that those utility functions do not handle any input and output the processes may recieve/produce. If you wish to handle output, you needed (before subprocess.run was implemented) to use subprocess.check_output, which does specifically handle output, which, in its own documentation, states equivalence to run(..., check=True, stdout=PIPE).stdout. That clearly shows that subprocess.run(...,check=True,stdout=PIPE) is valid.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.