[tor-commits] [stem/master] Subprocess lacks a kill() method on python 2.5

atagar at torproject.org atagar at torproject.org
Mon Jun 25 01:45:02 UTC 2012


commit 9d54db62a7b918c3bc6a852275adaf70dfdfef44
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jun 22 09:14:20 2012 -0700

    Subprocess lacks a kill() method on python 2.5
    
    The Popen.kill() ad Popen.terminate() methods were first added in python 2.6...
    http://docs.python.org/library/subprocess.html#subprocess.Popen.kill
    
    We can work around this with os.kill() but that only works on *nix...
    http://stackoverflow.com/questions/552423/use-python-2-6-subprocess-module-in-python-2-5/552510#552510
---
 stem/process.py       |   18 ++++++++++++++++--
 test/integ/process.py |   18 +++++++++++++++++-
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/stem/process.py b/stem/process.py
index 36ef7b9..e9d453e 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -23,6 +23,7 @@ import signal
 import tempfile
 import subprocess
 
+import stem.prereq
 import stem.util.system
 
 NO_TORRC = "<no torrc>"
@@ -80,7 +81,15 @@ def launch_tor(tor_cmd = "tor", args = None, torrc_path = None, completion_perce
         try: os.remove(temp_file)
         except: pass
       
-      tor_process.kill()
+      # We can't kill the subprocess on python 2.5 running Windows without the
+      # win32process module...
+      # http://stackoverflow.com/questions/552423/use-python-2-6-subprocess-module-in-python-2-5/552510#552510
+      
+      if stem.prereq.is_python_26():
+        tor_process.kill()
+      elif stem.util.system.is_windows():
+        os.kill(tor_process.pid, signal.SIGTERM)
+      
       raise OSError("reached a %i second timeout without success" % timeout)
     
     signal.signal(signal.SIGALRM, timeout_handler)
@@ -95,7 +104,12 @@ def launch_tor(tor_cmd = "tor", args = None, torrc_path = None, completion_perce
     
     # this will provide empty results if the process is terminated
     if not init_line:
-      tor_process.kill() # ... but best make sure
+      # ... but best make sure
+      if stem.prereq.is_python_26():
+        tor_process.kill()
+      elif stem.util.system.is_windows():
+        os.kill(tor_process.pid, signal.SIGTERM)
+      
       raise OSError("Process terminated: %s" % last_problem)
     
     # provide the caller with the initialization message if they want it
diff --git a/test/integ/process.py b/test/integ/process.py
index e3dd277..2037d93 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -2,12 +2,16 @@
 Tests the stem.process functions with various use cases.
 """
 
+import os
 import time
+import signal
 import unittest
 
+import stem.prereq
 import stem.socket
 import stem.process
 import test.runner
+import stem.util.system
 
 class TestProcess(unittest.TestCase):
   def test_launch_tor_with_config(self):
@@ -15,6 +19,10 @@ class TestProcess(unittest.TestCase):
     Exercises launch_tor_with_config.
     """
     
+    if not stem.prereq.is_python_26() and stem.util.system.is_windows():
+      test.runner.skip("(unable to kill subprocesses)")
+      return
+    
     if test.runner.only_run_once(self, "test_launch_tor_with_config"): return
     
     # Launch tor without a torrc, but with a control port. Confirms that this
@@ -37,13 +45,21 @@ class TestProcess(unittest.TestCase):
       self.assertEquals("ControlPort=2778", str(getconf_response))
     finally:
       if control_socket: control_socket.close()
-      tor_process.kill()
+      
+      if stem.prereq.is_python_26():
+        tor_process.kill()
+      elif stem.util.system.is_windows():
+        os.kill(tor_process.pid, signal.SIGTERM)
   
   def test_launch_tor_with_timeout(self):
     """
     Runs launch_tor where it times out before completing.
     """
     
+    if not stem.prereq.is_python_26() and stem.util.system.is_windows():
+      test.runner.skip("(unable to kill subprocesses)")
+      return
+    
     if test.runner.only_run_once(self, "test_launch_tor_with_timeout"): return
     
     start_time = time.time()





More information about the tor-commits mailing list