Create a gist now

Instantly share code, notes, and snippets.

Embed
Backport of subprocess.check_output() from Python 2.7 to Python 2.6
import subprocess
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
@rcoup

This comment has been minimized.

Show comment
Hide comment
@rcoup

rcoup Jun 20, 2011

raise subprocess.CalledProcessError(retcode, cmd, output=output)

CalledProcessError doesn't take kwargs in earlier versions of Python...

rcoup commented Jun 20, 2011

raise subprocess.CalledProcessError(retcode, cmd, output=output)

CalledProcessError doesn't take kwargs in earlier versions of Python...

@edufelipe

This comment has been minimized.

Show comment
Hide comment
@edufelipe

edufelipe Jun 20, 2011

@rcoup Thanks for the report. Updated the gist so the output is set the right way.

Owner

edufelipe commented Jun 20, 2011

@rcoup Thanks for the report. Updated the gist so the output is set the right way.

@achikin

This comment has been minimized.

Show comment
Hide comment
@achikin

achikin Jul 7, 2014

Good job, thanks!

achikin commented Jul 7, 2014

Good job, thanks!

@amp79

This comment has been minimized.

Show comment
Hide comment
@amp79

amp79 Sep 19, 2014

thank you for this.

amp79 commented Sep 19, 2014

thank you for this.

@sachinkanekar

This comment has been minimized.

Show comment
Hide comment
@sachinkanekar

sachinkanekar May 14, 2015

thanks! That was helpful

thanks! That was helpful

@busbey

This comment has been minimized.

Show comment
Hide comment
@busbey

busbey Apr 16, 2016

What are the license terms for your backport work? Could it be "under the same terms as Python 2.7" or "PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2"? (ref: said license)

busbey commented Apr 16, 2016

What are the license terms for your backport work? Could it be "under the same terms as Python 2.7" or "PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2"? (ref: said license)

@busbey

This comment has been minimized.

Show comment
Hide comment
@busbey

busbey Apr 24, 2016

Ping @edufelipe. We'd like to use this in the YCSB project, but without a license we'll reimplemented our own.

busbey commented Apr 24, 2016

Ping @edufelipe. We'd like to use this in the YCSB project, but without a license we'll reimplemented our own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment