classification
Title: Add check parameter to subprocess.Popen.communicate
Type: enhancement Stage: resolved
Components: Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: OG7, akira, martin.panter
Priority: normal Keywords:

Created on 2009-07-09 09:29 by OG7, last changed 2017-03-07 19:04 by serhiy.storchaka. This issue is now closed.

Messages (5)
msg90318 - (view) Author: OG7 (OG7) Date: 2009-07-09 09:29
communicate is often used in one-liners, but becomes a four-liner if you
want to check the process exit status. Adding a check parameter would
make it more convenient to get things right and write non-buggy code.

The CalledProcessError requires a cmd argument, which means also adding
a cmd member to Popen objects.
msg112302 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-01 09:56
@OG7 this is unlikely to move unless you provide a patch that changes the code and the unit test.
msg218675 - (view) Author: Akira Li (akira) * Date: 2014-05-16 16:55
subprocess.check_output() could be used in "communicate() + check process
exit status" one-liners. It returns child process output (stdout)
and raises an exception if the returncode is not zero. It is available
since Python 2.7 (3.1)

If you don't want to raise an error for non-zero exit status; you
could define a helper function:

  def status_output(*args, **kwargs):
      try:
          return 0, check_output(*args, **kwargs)
      except CalledProcessError as e:
          return e.returncode, e.output


> The CalledProcessError requires a cmd argument, which means also adding
a cmd member to Popen objects.

Popen.args is available since Python 3.3, see issue #21353
msg243634 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-05-19 22:52
In the mean time, a subprocess.run() API has been added which can call communicate() and raise CalledProcessError in one step (depending on arguments). See Issue 23342. This might satisfy some use cases, but maybe adding the check functionality to the low-level API would be nice as well.

Perhaps a separate check_exit_status() method would be more flexible? Or maybe a flag stored in the subprocess object that causes wait() and poll() to do the check?
msg265179 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-09 03:28
According to PEP 375, 3.1 was out on 27 June 2009, just before this report was opened (9 July). So perhaps OG7 wasn’t aware of check_output() at the time. But it sounds like they did want CalledProcessError to be raised.

I suggest to close this, unless there is demand for some new feature.
History
Date User Action Args
2017-03-07 19:04:21serhiy.storchakasetstatus: pending -> closed
stage: test needed -> resolved
2016-05-09 03:28:45martin.pantersetstatus: open -> pending
resolution: out of date
messages: + msg265179
2015-05-19 22:52:39martin.pantersetnosy: + martin.panter
messages: + msg243634
2014-05-16 16:55:34akirasetnosy: + akira
messages: + msg218675
2014-02-03 19:51:07BreamoreBoysetnosy: - BreamoreBoy
2010-08-01 09:56:38BreamoreBoysetversions: + Python 3.2
nosy: + BreamoreBoy

messages: + msg112302

type: enhancement
stage: test needed
2009-07-09 09:29:52OG7create