]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_threading_local.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_threading_local.py
CommitLineData
4710c53d 1import unittest\r
2from doctest import DocTestSuite\r
3from test import test_support\r
4import weakref\r
5import gc\r
6\r
7# Modules under test\r
8_thread = test_support.import_module('thread')\r
9threading = test_support.import_module('threading')\r
10import _threading_local\r
11\r
12\r
13class Weak(object):\r
14 pass\r
15\r
16def target(local, weaklist):\r
17 weak = Weak()\r
18 local.weak = weak\r
19 weaklist.append(weakref.ref(weak))\r
20\r
21class BaseLocalTest:\r
22\r
23 def test_local_refs(self):\r
24 self._local_refs(20)\r
25 self._local_refs(50)\r
26 self._local_refs(100)\r
27\r
28 def _local_refs(self, n):\r
29 local = self._local()\r
30 weaklist = []\r
31 for i in range(n):\r
32 t = threading.Thread(target=target, args=(local, weaklist))\r
33 t.start()\r
34 t.join()\r
35 del t\r
36\r
37 gc.collect()\r
38 self.assertEqual(len(weaklist), n)\r
39\r
40 # XXX _threading_local keeps the local of the last stopped thread alive.\r
41 deadlist = [weak for weak in weaklist if weak() is None]\r
42 self.assertIn(len(deadlist), (n-1, n))\r
43\r
44 # Assignment to the same thread local frees it sometimes (!)\r
45 local.someothervar = None\r
46 gc.collect()\r
47 deadlist = [weak for weak in weaklist if weak() is None]\r
48 self.assertIn(len(deadlist), (n-1, n), (n, len(deadlist)))\r
49\r
50 def test_derived(self):\r
51 # Issue 3088: if there is a threads switch inside the __init__\r
52 # of a threading.local derived class, the per-thread dictionary\r
53 # is created but not correctly set on the object.\r
54 # The first member set may be bogus.\r
55 import time\r
56 class Local(self._local):\r
57 def __init__(self):\r
58 time.sleep(0.01)\r
59 local = Local()\r
60\r
61 def f(i):\r
62 local.x = i\r
63 # Simply check that the variable is correctly set\r
64 self.assertEqual(local.x, i)\r
65\r
66 threads= []\r
67 for i in range(10):\r
68 t = threading.Thread(target=f, args=(i,))\r
69 t.start()\r
70 threads.append(t)\r
71\r
72 for t in threads:\r
73 t.join()\r
74\r
75 def test_derived_cycle_dealloc(self):\r
76 # http://bugs.python.org/issue6990\r
77 class Local(self._local):\r
78 pass\r
79 locals = None\r
80 passed = [False]\r
81 e1 = threading.Event()\r
82 e2 = threading.Event()\r
83\r
84 def f():\r
85 # 1) Involve Local in a cycle\r
86 cycle = [Local()]\r
87 cycle.append(cycle)\r
88 cycle[0].foo = 'bar'\r
89\r
90 # 2) GC the cycle (triggers threadmodule.c::local_clear\r
91 # before local_dealloc)\r
92 del cycle\r
93 gc.collect()\r
94 e1.set()\r
95 e2.wait()\r
96\r
97 # 4) New Locals should be empty\r
98 passed[0] = all(not hasattr(local, 'foo') for local in locals)\r
99\r
100 t = threading.Thread(target=f)\r
101 t.start()\r
102 e1.wait()\r
103\r
104 # 3) New Locals should recycle the original's address. Creating\r
105 # them in the thread overwrites the thread state and avoids the\r
106 # bug\r
107 locals = [Local() for i in range(10)]\r
108 e2.set()\r
109 t.join()\r
110\r
111 self.assertTrue(passed[0])\r
112\r
113 def test_arguments(self):\r
114 # Issue 1522237\r
115 from thread import _local as local\r
116 from _threading_local import local as py_local\r
117\r
118 for cls in (local, py_local):\r
119 class MyLocal(cls):\r
120 def __init__(self, *args, **kwargs):\r
121 pass\r
122\r
123 MyLocal(a=1)\r
124 MyLocal(1)\r
125 self.assertRaises(TypeError, cls, a=1)\r
126 self.assertRaises(TypeError, cls, 1)\r
127\r
128 def _test_one_class(self, c):\r
129 self._failed = "No error message set or cleared."\r
130 obj = c()\r
131 e1 = threading.Event()\r
132 e2 = threading.Event()\r
133\r
134 def f1():\r
135 obj.x = 'foo'\r
136 obj.y = 'bar'\r
137 del obj.y\r
138 e1.set()\r
139 e2.wait()\r
140\r
141 def f2():\r
142 try:\r
143 foo = obj.x\r
144 except AttributeError:\r
145 # This is expected -- we haven't set obj.x in this thread yet!\r
146 self._failed = "" # passed\r
147 else:\r
148 self._failed = ('Incorrectly got value %r from class %r\n' %\r
149 (foo, c))\r
150 sys.stderr.write(self._failed)\r
151\r
152 t1 = threading.Thread(target=f1)\r
153 t1.start()\r
154 e1.wait()\r
155 t2 = threading.Thread(target=f2)\r
156 t2.start()\r
157 t2.join()\r
158 # The test is done; just let t1 know it can exit, and wait for it.\r
159 e2.set()\r
160 t1.join()\r
161\r
162 self.assertFalse(self._failed, self._failed)\r
163\r
164 def test_threading_local(self):\r
165 self._test_one_class(self._local)\r
166\r
167 def test_threading_local_subclass(self):\r
168 class LocalSubclass(self._local):\r
169 """To test that subclasses behave properly."""\r
170 self._test_one_class(LocalSubclass)\r
171\r
172 def _test_dict_attribute(self, cls):\r
173 obj = cls()\r
174 obj.x = 5\r
175 self.assertEqual(obj.__dict__, {'x': 5})\r
176 with self.assertRaises(AttributeError):\r
177 obj.__dict__ = {}\r
178 with self.assertRaises(AttributeError):\r
179 del obj.__dict__\r
180\r
181 def test_dict_attribute(self):\r
182 self._test_dict_attribute(self._local)\r
183\r
184 def test_dict_attribute_subclass(self):\r
185 class LocalSubclass(self._local):\r
186 """To test that subclasses behave properly."""\r
187 self._test_dict_attribute(LocalSubclass)\r
188\r
189\r
190class ThreadLocalTest(unittest.TestCase, BaseLocalTest):\r
191 _local = _thread._local\r
192\r
193 # Fails for the pure Python implementation\r
194 def test_cycle_collection(self):\r
195 class X:\r
196 pass\r
197\r
198 x = X()\r
199 x.local = self._local()\r
200 x.local.x = x\r
201 wr = weakref.ref(x)\r
202 del x\r
203 gc.collect()\r
204 self.assertIs(wr(), None)\r
205\r
206class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):\r
207 _local = _threading_local.local\r
208\r
209\r
210def test_main():\r
211 suite = unittest.TestSuite()\r
212 suite.addTest(DocTestSuite('_threading_local'))\r
213 suite.addTest(unittest.makeSuite(ThreadLocalTest))\r
214 suite.addTest(unittest.makeSuite(PyThreadingLocalTest))\r
215\r
216 try:\r
217 from thread import _local\r
218 except ImportError:\r
219 pass\r
220 else:\r
221 import _threading_local\r
222 local_orig = _threading_local.local\r
223 def setUp(test):\r
224 _threading_local.local = _local\r
225 def tearDown(test):\r
226 _threading_local.local = local_orig\r
227 suite.addTest(DocTestSuite('_threading_local',\r
228 setUp=setUp, tearDown=tearDown)\r
229 )\r
230\r
231 test_support.run_unittest(suite)\r
232\r
233if __name__ == '__main__':\r
234 test_main()\r