[Python-checkins] cpython (3.2): #17249: convert a test in test_capi to use unittest and reap threads.

ezio.melotti python-checkins at python.org
Sat Feb 23 04:59:54 CET 2013


http://hg.python.org/cpython/rev/329732a1572f
changeset:   82326:329732a1572f
branch:      3.2
parent:      82322:7d95a0aa6b5a
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Sat Feb 23 05:52:46 2013 +0200
summary:
  #17249: convert a test in test_capi to use unittest and reap threads.

files:
  Lib/test/test_capi.py |  56 +++++++++++++++---------------
  Misc/NEWS             |   2 +
  2 files changed, 30 insertions(+), 28 deletions(-)


diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -8,6 +8,7 @@
 import subprocess
 import sys
 import time
+import _thread
 import unittest
 from test import support
 try:
@@ -222,8 +223,34 @@
             os.chdir(oldcwd)
 
 
+ at unittest.skipUnless(threading, 'Threading required for this test.')
+class TestThreadState(unittest.TestCase):
+
+    @support.reap_threads
+    def test_thread_state(self):
+        # some extra thread-state tests driven via _testcapi
+        def target():
+            idents = []
+
+            def callback():
+                idents.append(_thread.get_ident())
+
+            _testcapi._test_thread_state(callback)
+            a = b = callback
+            time.sleep(1)
+            # Check our main thread is in the list exactly 3 times.
+            self.assertEqual(idents.count(_thread.get_ident()), 3,
+                             "Couldn't find main thread correctly in the list")
+
+        target()
+        t = threading.Thread(target=target)
+        t.start()
+        t.join()
+
+
 def test_main():
-    support.run_unittest(CAPITest, TestPendingCalls, Test6012, EmbeddingTest)
+    support.run_unittest(CAPITest, TestPendingCalls, Test6012,
+                         EmbeddingTest, TestThreadState)
 
     for name in dir(_testcapi):
         if name.startswith('test_'):
@@ -232,32 +259,5 @@
                 print("internal", name)
             test()
 
-    # some extra thread-state tests driven via _testcapi
-    def TestThreadState():
-        if support.verbose:
-            print("auto-thread-state")
-
-        idents = []
-
-        def callback():
-            idents.append(_thread.get_ident())
-
-        _testcapi._test_thread_state(callback)
-        a = b = callback
-        time.sleep(1)
-        # Check our main thread is in the list exactly 3 times.
-        if idents.count(_thread.get_ident()) != 3:
-            raise support.TestFailed(
-                        "Couldn't find main thread correctly in the list")
-
-    if threading:
-        import _thread
-        import time
-        TestThreadState()
-        t = threading.Thread(target=TestThreadState)
-        t.start()
-        t.join()
-
-
 if __name__ == "__main__":
     test_main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -936,6 +936,8 @@
 Tests
 -----
 
+- Issue #17249: convert a test in test_capi to use unittest and reap threads.
+
 - Issue #17041: Fix testing when Python is configured with the
   --without-doc-strings.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list