changeset 134:e84f33a60a5c

Moved process killing logic into platform-specific modules
author Oleg Oshmyan <chortos@inbox.lv>
date Fri, 20 May 2011 14:47:42 +0100
parents a9d2aa6810c7
children 523ba6907f3a
files testcases.py unix.py win32.py
diffstat 3 files changed, 23 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- 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:
--- 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
--- 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