]> granicus.if.org Git - python/commitdiff
a useful decorator for cleaning up threads
authorBenjamin Peterson <benjamin@python.org>
Thu, 14 May 2009 22:40:34 +0000 (22:40 +0000)
committerBenjamin Peterson <benjamin@python.org>
Thu, 14 May 2009 22:40:34 +0000 (22:40 +0000)
Lib/test/test_hashlib.py
Lib/test/test_support.py

index b736755836a80d2b4249bf81c0e2618ebd7937f6..e77b2460750fb9bfefb37b55387baa80b0b1c1f0 100644 (file)
@@ -254,14 +254,9 @@ class HashLibTestCase(unittest.TestCase):
 
         self.assertEqual(expected_hash, hasher.hexdigest())
 
-
+@test_support.reap_threads
 def test_main():
-    key = test_support.threading_setup()
-    try:
-        test_support.run_unittest(HashLibTestCase)
-    finally:
-        test_support.threading_cleanup(*key)
-
+    test_support.run_unittest(HashLibTestCase)
 
 if __name__ == "__main__":
     test_main()
index 7d0ec8d391fc51a822568584808839cf6eaf6c3d..52dd16e90f16c74fbc1bac56d9b2a2b8b55e2573 100644 (file)
@@ -5,6 +5,7 @@ if __name__ != 'test.test_support':
 
 import contextlib
 import errno
+import functools
 import socket
 import sys
 import os
@@ -934,6 +935,16 @@ def threading_cleanup(num_active, num_limbo):
         count += 1
         time.sleep(0.1)
 
+def reap_threads(func):
+    @functools.wraps(func)
+    def decorator(*args):
+        key = threading_setup()
+        try:
+            return func(*args)
+        finally:
+            threading_cleanup(*key)
+    return decorator
+
 def reap_children():
     """Use this function at the end of test_main() whenever sub-processes
     are started.  This will help ensure that no extra children (zombies)