Created
April 10, 2018 11:49
-
-
Save arouene/578c7da285103e321c9ee44d3d0d27ee to your computer and use it in GitHub Desktop.
Python backports
This file contains hidden or 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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Backport subprocess.run for Python 3.3 and 3.4