run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate() stable
authorThomas Arendsen Hein <thomas@intevation.de>
Thu, 30 Jun 2011 16:25:05 +0200
branchstable
changeset 14821 2017495bd552
parent 14820 7ef125fa9b35
child 14822 760d2dae4839
run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
tests/run-tests.py
--- a/tests/run-tests.py	Thu Jun 30 13:22:12 2011 -0500
+++ b/tests/run-tests.py	Thu Jun 30 16:25:05 2011 +0200
@@ -78,10 +78,7 @@
                 time.sleep(1)
             p.timeout = True
             if p.returncode is None:
-                try:
-                    p.terminate()
-                except OSError:
-                    pass
+                terminate(p)
         threading.Thread(target=t).start()
 
     return p
@@ -343,6 +340,17 @@
         else:
             print "WARNING: Did not find prerequisite tool: "+p
 
+def terminate(proc):
+    """Terminate subprocess (with fallback for Python versions < 2.6)"""
+    vlog('# Terminating process %d' % proc.pid)
+    try:
+        if hasattr(proc, 'terminate'):
+            proc.terminate()
+        else:
+            os.kill(proc.pid, signal.SIGTERM)
+    except OSError:
+        pass
+
 def killdaemons():
     # Kill off any leftover daemon processes
     try:
@@ -651,10 +659,7 @@
 
     proc = Popen4(cmd, wd, options.timeout)
     def cleanup():
-        try:
-            proc.terminate()
-        except OSError:
-            pass
+        terminate(proc)
         ret = proc.wait()
         if ret == 0:
             ret = signal.SIGTERM << 8