]> granicus.if.org Git - python/commitdiff
test_threaded_import must clean up after itself
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 15 Jul 2011 21:09:13 +0000 (23:09 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 15 Jul 2011 21:09:13 +0000 (23:09 +0200)
Lib/test/test_threaded_import.py
Lib/test/threaded_import_hangers.py

index 6919d21e1dc387f213189a1fe157cdacbef25288..789920b7fa9e5407667934ca912ef31f4e9bae57 100644 (file)
@@ -11,8 +11,8 @@ import sys
 import time
 import shutil
 import unittest
-from test.support import verbose, import_module, run_unittest, TESTFN
-thread = import_module('_thread')
+from test.support import (
+    verbose, import_module, run_unittest, TESTFN, reap_threads)
 threading = import_module('threading')
 
 def task(N, done, done_tasks, errors):
@@ -62,7 +62,7 @@ class Finder:
     def __init__(self):
         self.numcalls = 0
         self.x = 0
-        self.lock = thread.allocate_lock()
+        self.lock = threading.Lock()
 
     def find_module(self, name, path=None):
         # Simulate some thread-unsafe behaviour. If calls to find_module()
@@ -113,7 +113,9 @@ class ThreadedImportTests(unittest.TestCase):
             done_tasks = []
             done.clear()
             for i in range(N):
-                thread.start_new_thread(task, (N, done, done_tasks, errors,))
+                t = threading.Thread(target=task,
+                                     args=(N, done, done_tasks, errors,))
+                t.start()
             done.wait(60)
             self.assertFalse(errors)
             if verbose:
@@ -203,6 +205,7 @@ class ThreadedImportTests(unittest.TestCase):
         self.assertEqual(set(results), {'a', 'b'})
 
 
+@reap_threads
 def test_main():
     run_unittest(ThreadedImportTests)
 
index adf03e31ffd663ac2df95a2b4fabc053b04b9bbe..d7cc2559985d3ce7b894c51bf98044d8c99f38a8 100644 (file)
@@ -35,8 +35,12 @@ for name, func, args in [
         ("os.path.abspath", os.path.abspath, ('.',)),
         ]:
 
-    t = Worker(func, args)
-    t.start()
-    t.join(TIMEOUT)
-    if t.is_alive():
-        errors.append("%s appeared to hang" % name)
+    try:
+        t = Worker(func, args)
+        t.start()
+        t.join(TIMEOUT)
+        if t.is_alive():
+            errors.append("%s appeared to hang" % name)
+    finally:
+        del t
+