Skip to content

Instantly share code, notes, and snippets.

@arouene
Created April 10, 2018 11:49
Show Gist options
  • Save arouene/578c7da285103e321c9ee44d3d0d27ee to your computer and use it in GitHub Desktop.
Save arouene/578c7da285103e321c9ee44d3d0d27ee to your computer and use it in GitHub Desktop.
Python backports
# Backport subprocess.run for Python 3.3 and 3.4
import subprocess
if not 'run' in subprocess.__all__:
class CompletedProcess(object):
def __init__(self, stdout, stderr, returncode):
self._stdout = stdout
self._stderr = stderr
self._returncode = returncode
@property
def stdout(self):
return self._stdout
@property
def stderr(self):
return self._stderr
@property
def returncode(self):
return self._returncode
def run(*args, **kwargs):
timeout = None
if 'timeout' in kwargs:
timeout = kwargs['timeout']
kwargs.pop('timeout', None)
process = subprocess.Popen(*args, **kwargs)
try:
stdout, stderr = process.communicate(timeout=timeout)
except:
process.kill()
process.wait()
raise
return CompletedProcess(stdout, stderr, process.poll())
setattr(subprocess, 'run', run)
@arouene
Copy link
Author

arouene commented Apr 10, 2018

Backport subprocess.run for Python 3.3 and 3.4

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