@ -44,10 +44,6 @@ VOID = object()
# Error code when a process was killed because it timed out.
TIMED_OUT = - 2001
# Globals.
# Set to True if you somehow need to disable this hack.
SUBPROCESS_CLEANUP_HACKED = False
class CalledProcessError ( subprocess . CalledProcessError ) :
""" Augment the standard exception with more data. """
@ -78,51 +74,11 @@ def kill_pid(pid):
# Unable to import 'module'
# pylint: disable=no-member,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=import-error
import win32process
# Access to a protected member _handle of a client class
# pylint: disable=protected-access
return win32process . TerminateProcess ( process . _handle , - 1 )
return os . kill ( pid , signal . SIGTERM )
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.
See http : / / bugs . python . org / issue1731717 for more information .
"""
global SUBPROCESS_CLEANUP_HACKED
if not SUBPROCESS_CLEANUP_HACKED and threading . activeCount ( ) != 1 :
# Only hack if there is ever multiple threads.
# There is no point to leak with only one thread.
subprocess . _cleanup = lambda : None
SUBPROCESS_CLEANUP_HACKED = True
def get_english_env ( env ) :
""" Forces LANG and/or LANGUAGE to be English.
@ -208,10 +164,6 @@ class Popen(subprocess.Popen):
popen_lock = threading . Lock ( )
def __init__ ( self , args , * * kwargs ) :
# Make sure we hack subprocess if necessary.
hack_subprocess ( )
add_kill ( )
env = get_english_env ( kwargs . get ( ' env ' ) )
if env :
kwargs [ ' env ' ] = env