# HG changeset patch # User Oleg Oshmyan # Date 1305899262 -3600 # Node ID e84f33a60a5cd38ccf9bd30c480adf3c6606c884 # Parent a9d2aa6810c7b46fa7c884f6b4b557486b85d924 Moved process killing logic into platform-specific modules diff -r a9d2aa6810c7 -r e84f33a60a5c testcases.py --- a/testcases.py Thu May 19 16:50:00 2011 +0100 +++ b/testcases.py Fri May 20 14:47:42 2011 +0100 @@ -185,38 +185,14 @@ case.cleanup() def cleanup(case): - #if getattr(case, 'infile', None): - # case.infile.close() - #if getattr(case, 'outfile', None): - # case.outfile.close() if getattr(case, 'process', None) and case.process.returncode is None: - # Try KILLing after three unsuccessful TERM attempts in a row - for i in range(3): - try: - terminate(case.process) - except Exception: - time.sleep(0) - else: - wait(case.process) - break - else: - # If killing the process is unsuccessful three times in a row, - # just silently stop trying - for i in range(3): - try: - kill(case.process) - except Exception: - time.sleep(0) - else: - wait(case.process) - break - if case.files_to_delete: - for name in case.files_to_delete: - try: - os.remove(name) - except Exception: - # It can't be helped - pass + kill(case.process) + for name in case.files_to_delete: + try: + os.remove(name) + except OSError: + # It can't be helped + pass def open_infile(case): try: diff -r a9d2aa6810c7 -r e84f33a60a5c unix.py --- a/unix.py Thu May 19 16:50:00 2011 +0100 +++ b/unix.py Fri May 20 14:47:42 2011 +0100 @@ -22,7 +22,7 @@ SIGTERM = 15 SIGKILL = 9 -__all__ = 'call', 'kill', 'terminate', 'wait', 'pause', 'clock' +__all__ = 'call', 'kill', 'pause', 'clock' if not sys.stdin.isatty(): @@ -291,13 +291,7 @@ process.kill() except AttributeError: os.kill(process.pid, SIGKILL) - - -def terminate(process): - try: - process.terminate() - except AttributeError: - os.kill(process.pid, SIGTERM) + wait(process) # subprocess in Python 2.6- is not guarded against EINTR diff -r a9d2aa6810c7 -r e84f33a60a5c win32.py --- a/win32.py Thu May 19 16:50:00 2011 +0100 +++ b/win32.py Fri May 20 14:47:42 2011 +0100 @@ -47,7 +47,7 @@ else: ProcessTimes = namedtuple('ProcessTimes', 'kernel user') -__all__ = 'call', 'kill', 'terminate', 'wait', 'pause', 'clock' +__all__ = 'call', 'kill', 'pause', 'clock' from functools import wraps @@ -87,8 +87,8 @@ raise -# Automatically convert _subprocess handle objects into low-level HANDLEs -# and replicate their functionality for our own use +# Automatically convert _subprocess handle objects into low-level +# HANDLEs and replicate their functionality for our own use try: _subprocess_handle = type(GetCurrentProcess()) except NameError: @@ -541,9 +541,14 @@ def kill(process): - try: - process.terminate() - except AttributeError: - TerminateProcess(process._handle, 1) -terminate = kill -wait = subprocess.Popen.wait \ No newline at end of file + # Give up after three attempts + for i in range(3): + try: + try: + process.terminate() + except AttributeError: + TerminateProcess(process._handle, 1) + except WindowsError: + time.sleep(0) + else: + break