This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: a new subprocess.call which raises an error on non-zero rc
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: astrand Nosy List: astrand, ncw
Priority: normal Keywords: patch

Created on 2004-11-23 14:45 by ncw, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
xcall.patch ncw, 2004-11-23 14:45 patch for subprocess to implement raising call
xcall.patch astrand, 2004-12-13 02:55 Adapted patch against trunk
Messages (5)
msg47326 - (view) Author: Nick Craig-Wood (ncw) * Date: 2004-11-23 14:45
The attached patch introduces a 3rd utility function -
xcall() to the
subprocess module.  This function acts just like call
but raises
errors if the command had a non-zero return code.

This saves writing

if call(...):
    raise OSError(...)

It is most useful for shell script replacement
programming.  Checking
the return codes of commands called is often forgotten
in shell
programming.

When you've moved up to python because shell is too
limiting (usually
about 10 lines of shell in my case ;-) you want to make
sure that all
your commands work so you write robust code.

I consider raising an exception to be much more
pythonic than checking
a return code (ie "Errors should never pass silently"
from Zen of
Python)

Eg

# An easy to miss error

>>> subprocess.call(["mkdir", "a/b"])
mkdir: cannot create directory `a/b': No such file or
directory
1
>>> # user forgot to check non-zero return code

# becomes an impossible to miss exception

>>> subprocess.xcall(["mkdir", "a/b"])
mkdir: cannot create directory `a/b': No such file or
directory
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "subprocess.py", line 462, in xcall
    raise CalledProcessError(rc, "Command %s returned
non zero exit status" % args[0])
subprocess.CalledProcessError: [Errno 1] Command
['mkdir', 'a/b'] returned non zero exit status
>>> 

See attached patch for more!  Its been tested under
python 2.3 on
windows and linux.
msg47327 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2004-12-01 14:04
Logged In: YES 
user_id=344921

Since this is a new feature, the patch will go into trunk,
but not the 2.4 maint branch. 
msg47328 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2004-12-05 21:20
Logged In: YES 
user_id=344921

My suggested name is "check_call". 
msg47329 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2004-12-13 02:57
Logged In: YES 
user_id=344921

If there are no objections, I will commit the "Adapted patch
against trunk". 
msg47330 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2005-01-01 09:37
Logged In: YES 
user_id=344921

Patch applied. New revisions:

subprocess.py 1.12
test_subprocess.py 1.17
libsubprocess.tex 1.5
History
Date User Action Args
2022-04-11 14:56:08adminsetgithub: 41218
2004-11-23 14:45:22ncwcreate