[Patches] [ python-Patches-1071764 ] a new subprocess.call which raises an error on non-zero rc

SourceForge.net noreply at sourceforge.net
Mon Dec 13 03:57:58 CET 2004


Patches item #1071764, was opened at 2004-11-23 15:45
Message generated for change (Comment added) made by astrand
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1071764&group_id=5470

Category: Library (Lib)
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Nick Craig-Wood (ncw)
Assigned to: Peter Åstrand (astrand)
Summary: a new subprocess.call which raises an error on non-zero rc

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Peter Åstrand (astrand)
Date: 2004-12-13 03:57

Message:
Logged In: YES 
user_id=344921

If there are no objections, I will commit the "Adapted patch
against trunk". 

----------------------------------------------------------------------

Comment By: Peter Åstrand (astrand)
Date: 2004-12-05 22:20

Message:
Logged In: YES 
user_id=344921

My suggested name is "check_call". 

----------------------------------------------------------------------

Comment By: Peter Åstrand (astrand)
Date: 2004-12-01 15:04

Message:
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. 

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1071764&group_id=5470


More information about the Patches mailing list