Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(770)

Unified Diff: subprocess2.py

Issue 6783022: Add subprocess.Popen().kill() to python 2.5. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Rebase against HEAD Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/fake_repos.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: subprocess2.py
diff --git a/subprocess2.py b/subprocess2.py
index ed34d3e98eebfa0c5bf7185db6d3a83d04505916..c6ff24f6b2505483a449e9dbbb65515a68db1f3f 100644
--- a/subprocess2.py
+++ b/subprocess2.py
@@ -41,6 +41,47 @@ class CalledProcessError(subprocess.CalledProcessError):
return '\n'.join(filter(None, (out, self.stdout, self.stderr)))
+## Utility functions
+
+
+def kill_pid(pid):
+ """Kills a process by its process id."""
+ try:
+ # Unable to import 'module'
+ # pylint: disable=F0401
+ import signal
+ return os.kill(pid, signal.SIGKILL)
+ except ImportError:
+ pass
+
+
+def kill_win(process):
+ """Kills a process with its windows handle.
+
+ Has no effect on other platforms.
+ """
+ try:
+ # Unable to import 'module'
+ # pylint: disable=F0401
+ import win32process
+ # Access to a protected member _handle of a client class
+ # pylint: disable=W0212
+ return win32process.TerminateProcess(process._handle, -1)
+ except ImportError:
+ pass
+
+
+def add_kill():
+ """Adds kill() method to subprocess.Popen for python <2.6"""
+ if hasattr(subprocess.Popen, 'kill'):
+ return
+
+ if sys.platform == 'win32':
+ subprocess.Popen.kill = kill_win
+ else:
+ subprocess.Popen.kill = lambda process: kill_pid(process.pid)
+
+
def hack_subprocess():
"""subprocess functions may throw exceptions when used in multiple threads.
@@ -92,6 +133,7 @@ def Popen(args, **kwargs):
"""
# Make sure we hack subprocess if necessary.
hack_subprocess()
+ add_kill()
env = get_english_env(kwargs.get('env'))
if env:
« no previous file with comments | « no previous file | tests/fake_repos.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698