[Python-checkins] r83678 - python/branches/py3k/Lib/test/test_threading_local.py

antoine.pitrou python-checkins at python.org
Tue Aug 3 20:32:26 CEST 2010


Author: antoine.pitrou
Date: Tue Aug  3 20:32:26 2010
New Revision: 83678

Log:
In test_threading_local, test both the default _thread._local implementation
and the pure Python implementation in Lib/_threading_local.py



Modified:
   python/branches/py3k/Lib/test/test_threading_local.py

Modified: python/branches/py3k/Lib/test/test_threading_local.py
==============================================================================
--- python/branches/py3k/Lib/test/test_threading_local.py	(original)
+++ python/branches/py3k/Lib/test/test_threading_local.py	Tue Aug  3 20:32:26 2010
@@ -1,10 +1,15 @@
 import unittest
 from doctest import DocTestSuite
 from test import support
-threading = support.import_module('threading')
 import weakref
 import gc
 
+# Modules under test
+_thread = support.import_module('_thread')
+threading = support.import_module('threading')
+import _threading_local
+
+
 class Weak(object):
     pass
 
@@ -13,7 +18,8 @@
     local.weak = weak
     weaklist.append(weakref.ref(weak))
 
-class ThreadingLocalTest(unittest.TestCase):
+
+class BaseLocalTest:
 
     def test_local_refs(self):
         self._local_refs(20)
@@ -21,7 +27,7 @@
         self._local_refs(100)
 
     def _local_refs(self, n):
-        local = threading.local()
+        local = self._local()
         weaklist = []
         for i in range(n):
             t = threading.Thread(target=target, args=(local, weaklist))
@@ -48,7 +54,7 @@
         # is created but not correctly set on the object.
         # The first member set may be bogus.
         import time
-        class Local(threading.local):
+        class Local(self._local):
             def __init__(self):
                 time.sleep(0.01)
         local = Local()
@@ -69,7 +75,7 @@
 
     def test_derived_cycle_dealloc(self):
         # http://bugs.python.org/issue6990
-        class Local(threading.local):
+        class Local(self._local):
             pass
         locals = None
         passed = False
@@ -108,24 +114,28 @@
 
     def test_arguments(self):
         # Issue 1522237
-        from _thread import _local as local
-        from _threading_local import local as py_local
+        class MyLocal(self._local):
+            def __init__(self, *args, **kwargs):
+                pass
+
+        MyLocal(a=1)
+        MyLocal(1)
+        self.assertRaises(TypeError, self._local, a=1)
+        self.assertRaises(TypeError, self._local, 1)
+
+
+class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
+    _local = _thread._local
 
-        for cls in (local, py_local):
-            class MyLocal(cls):
-                def __init__(self, *args, **kwargs):
-                    pass
-
-            MyLocal(a=1)
-            MyLocal(1)
-            self.assertRaises(TypeError, cls, a=1)
-            self.assertRaises(TypeError, cls, 1)
+class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):
+    _local = _threading_local.local
 
 
 def test_main():
     suite = unittest.TestSuite()
     suite.addTest(DocTestSuite('_threading_local'))
-    suite.addTest(unittest.makeSuite(ThreadingLocalTest))
+    suite.addTest(unittest.makeSuite(ThreadLocalTest))
+    suite.addTest(unittest.makeSuite(PyThreadingLocalTest))
 
     try:
         from thread import _local


More information about the Python-checkins mailing list