Created
February 16, 2013 22:18
-
-
Save merickson/4968979 to your computer and use it in GitHub Desktop.
check_output hack for python 2.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hack to get subprocess.check_output() on pythons < 2.7. See: | |
# http://stackoverflow.com/questions/4814970/subprocess-check-output-doesnt-seem-to-exist-python-2-6-5 | |
if "check_output" not in dir(subprocess): # duck punch it in! | |
def f(*popenargs, **kwargs): | |
if 'stdout' in kwargs: | |
raise ValueError('stdout argument not allowed, it will be overridden.') | |
process = subprocess.Popen(stdout=subprocess.PIPE, stderr=subprocess.PIPE, *popenargs, **kwargs) | |
output, errstr = process.communicate() | |
print "%s, %s" % (output, errstr,) | |
retcode = process.poll() | |
if retcode: | |
cmd = kwargs.get("args") | |
if cmd is None: | |
cmd = popenargs[0] | |
exc = subprocess.CalledProcessError(retcode, cmd) | |
exc.output = output | |
exc.errstr = errstr | |
raise exc | |
return output | |
subprocess.check_output = f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment