]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_threading_local.py
2 from doctest
import DocTestSuite
3 from test
import test_support
8 _thread
= test_support
.import_module('thread')
9 threading
= test_support
.import_module('threading')
10 import _threading_local
16 def target(local
, weaklist
):
19 weaklist
.append(weakref
.ref(weak
))
23 def test_local_refs(self
):
28 def _local_refs(self
, n
):
32 t
= threading
.Thread(target
=target
, args
=(local
, weaklist
))
38 self
.assertEqual(len(weaklist
), n
)
40 # XXX _threading_local keeps the local of the last stopped thread alive.
41 deadlist
= [weak
for weak
in weaklist
if weak() is None]
42 self
.assertIn(len(deadlist
), (n
-1, n
))
44 # Assignment to the same thread local frees it sometimes (!)
45 local
.someothervar
= None
47 deadlist
= [weak
for weak
in weaklist
if weak() is None]
48 self
.assertIn(len(deadlist
), (n
-1, n
), (n
, len(deadlist
)))
50 def test_derived(self
):
51 # Issue 3088: if there is a threads switch inside the __init__
52 # of a threading.local derived class, the per-thread dictionary
53 # is created but not correctly set on the object.
54 # The first member set may be bogus.
56 class Local(self
._local
):
63 # Simply check that the variable is correctly set
64 self
.assertEqual(local
.x
, i
)
68 t
= threading
.Thread(target
=f
, args
=(i
,))
75 def test_derived_cycle_dealloc(self
):
76 # http://bugs.python.org/issue6990
77 class Local(self
._local
):
81 e1
= threading
.Event()
82 e2
= threading
.Event()
85 # 1) Involve Local in a cycle
90 # 2) GC the cycle (triggers threadmodule.c::local_clear
91 # before local_dealloc)
97 # 4) New Locals should be empty
98 passed
[0] = all(not hasattr(local
, 'foo') for local
in locals)
100 t
= threading
.Thread(target
=f
)
104 # 3) New Locals should recycle the original's address. Creating
105 # them in the thread overwrites the thread state and avoids the
107 locals = [Local() for i
in range(10)]
111 self
.assertTrue(passed
[0])
113 def test_arguments(self
):
115 from thread
import _local
as local
116 from _threading_local
import local
as py_local
118 for cls
in (local
, py_local
):
120 def __init__(self
, *args
, **kwargs
):
125 self
.assertRaises(TypeError, cls
, a
=1)
126 self
.assertRaises(TypeError, cls
, 1)
128 def _test_one_class(self
, c
):
129 self
._failed
= "No error message set or cleared."
131 e1
= threading
.Event()
132 e2
= threading
.Event()
144 except AttributeError:
145 # This is expected -- we haven't set obj.x in this thread yet!
146 self
._failed
= "" # passed
148 self
._failed
= ('Incorrectly got value %r from class %r\n' %
150 sys
.stderr
.write(self
._failed
)
152 t1
= threading
.Thread(target
=f1
)
155 t2
= threading
.Thread(target
=f2
)
158 # The test is done; just let t1 know it can exit, and wait for it.
162 self
.assertFalse(self
._failed
, self
._failed
)
164 def test_threading_local(self
):
165 self
._test
_one
_class
(self
._local
)
167 def test_threading_local_subclass(self
):
168 class LocalSubclass(self
._local
):
169 """To test that subclasses behave properly."""
170 self
._test
_one
_class
(LocalSubclass
)
172 def _test_dict_attribute(self
, cls
):
175 self
.assertEqual(obj
.__dict
__, {'x': 5})
176 with self
.assertRaises(AttributeError):
178 with self
.assertRaises(AttributeError):
181 def test_dict_attribute(self
):
182 self
._test
_dict
_attribute
(self
._local
)
184 def test_dict_attribute_subclass(self
):
185 class LocalSubclass(self
._local
):
186 """To test that subclasses behave properly."""
187 self
._test
_dict
_attribute
(LocalSubclass
)
190 class ThreadLocalTest(unittest
.TestCase
, BaseLocalTest
):
191 _local
= _thread
._local
193 # Fails for the pure Python implementation
194 def test_cycle_collection(self
):
199 x
.local
= self
._local
()
204 self
.assertIs(wr(), None)
206 class PyThreadingLocalTest(unittest
.TestCase
, BaseLocalTest
):
207 _local
= _threading_local
.local
211 suite
= unittest
.TestSuite()
212 suite
.addTest(DocTestSuite('_threading_local'))
213 suite
.addTest(unittest
.makeSuite(ThreadLocalTest
))
214 suite
.addTest(unittest
.makeSuite(PyThreadingLocalTest
))
217 from thread
import _local
221 import _threading_local
222 local_orig
= _threading_local
.local
224 _threading_local
.local
= _local
226 _threading_local
.local
= local_orig
227 suite
.addTest(DocTestSuite('_threading_local',
228 setUp
=setUp
, tearDown
=tearDown
)
231 test_support
.run_unittest(suite
)
233 if __name__
== '__main__':