[Python-checkins] python/dist/src/Lib subprocess.py,1.11,1.12

astrand at users.sourceforge.net astrand at users.sourceforge.net
Sat Jan 1 10:36:37 CET 2005


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24099/d
Modified Files:
	subprocess.py 
Log Message:
New subprocess utility function: check_call. Closes #1071764.
Index: subprocess.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/subprocess.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- subprocess.py	5 Dec 2004 20:15:36 -0000	1.11
+++ subprocess.py	1 Jan 2005 09:36:34 -0000	1.12
@@ -133,6 +133,15 @@
 
 retcode = call(["ls", "-l"])
 
+check_call(*popenargs, **kwargs):
+ Run command with arguments. Wait for command to complete. If the
+ exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the errno attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
 
 Exceptions
 ----------
@@ -148,6 +157,9 @@
 
 A ValueError will be raised if Popen is called with invalid arguments.
 
+check_call() will raise CalledProcessError, which is a subclass of
+OSError, if the called process returns a non-zero return code.
+
 
 Security
 --------
@@ -363,6 +375,13 @@
 import types
 import traceback
 
+# Exception classes used by this module.
+class CalledProcessError(OSError):
+ """This exception is raised when a process run by check_call() returns
+ a non-zero exit status. The exit status will be stored in the
+ errno attribute. This exception is a subclass of
+ OSError."""
+
 if mswindows:
 import threading
 import msvcrt
@@ -393,7 +412,7 @@
 import fcntl
 import pickle
 
-__all__ = ["Popen", "PIPE", "STDOUT", "call"]
+__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "CalledProcessError"]
 
 try:
 MAXFD = os.sysconf("SC_OPEN_MAX")
@@ -428,6 +447,25 @@
 return Popen(*popenargs, **kwargs).wait()
 
 
+def check_call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete. If
+ the exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the errno attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
+ """
+ retcode = call(*popenargs, **kwargs)
+ cmd = kwargs.get("args")
+ if cmd is None:
+ cmd = popenargs[0]
+ if retcode:
+ raise CalledProcessError(retcode, "Command %s returned non-zero exit status" % cmd)
+ return retcode
+
+
 def list2cmdline(seq):
 """
 Translate a sequence of arguments into a command line


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /