blob: 6b6c4d220a3bd090631156453f09fcf81ff31c72 [file] [log] [blame]
Antoine Pitrou4c8ce842013-09-01 19:51:49 +02001"""
2Tests for the threading module.
3"""
Skip Montanaro4533f602001-08-20 20:28:48 +00004
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005import test.support
Serhiy Storchakaa7930372016-07-03 22:27:26 +03006from test.support import (verbose, import_module, cpython_only,
7 requires_type_collecting)
Berker Peksagce643912015-05-06 06:33:17 +03008from test.support.script_helper import assert_python_ok, assert_python_failure
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02009
Skip Montanaro4533f602001-08-20 20:28:48 +000010import random
Guido van Rossumcd16bf62007-06-13 18:07:49 +000011import sys
Antoine Pitrouc4d78642011-05-05 20:17:32 +020012_thread = import_module('_thread')
13threading = import_module('threading')
Skip Montanaro4533f602001-08-20 20:28:48 +000014import time
Tim Peters84d54892005-01-08 06:03:17 +000015import unittest
Christian Heimesd3eb5a152008-02-24 00:38:49 +000016import weakref
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +000017import os
Gregory P. Smith4b129d22011-01-04 00:51:50 +000018import subprocess
Skip Montanaro4533f602001-08-20 20:28:48 +000019
Antoine Pitrou557934f2009-11-06 22:41:14 +000020from test import lock_tests
Martin Panter19e69c52015-11-14 12:46:42 +000021from test import support
Antoine Pitrou557934f2009-11-06 22:41:14 +000022
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +030023
24# Between fork() and exec(), only async-safe functions are allowed (issues
25# #12316 and #11870), and fork() from a worker thread is known to trigger
26# problems with some operating systems (issue #3863): skip problematic tests
27# on platforms known to behave badly.
28platforms_to_skip = ('freebsd4', 'freebsd5', 'freebsd6', 'netbsd5',
29 'hp-ux11')
30
31
Tim Peters84d54892005-01-08 06:03:17 +000032# A trivial mutable counter.
33class Counter(object):
34 def __init__(self):
35 self.value = 0
36 def inc(self):
37 self.value += 1
38 def dec(self):
39 self.value -= 1
40 def get(self):
41 return self.value
Skip Montanaro4533f602001-08-20 20:28:48 +000042
43class TestThread(threading.Thread):
Tim Peters84d54892005-01-08 06:03:17 +000044 def __init__(self, name, testcase, sema, mutex, nrunning):
45 threading.Thread.__init__(self, name=name)
46 self.testcase = testcase
47 self.sema = sema
48 self.mutex = mutex
49 self.nrunning = nrunning
50
Skip Montanaro4533f602001-08-20 20:28:48 +000051 def run(self):
Christian Heimes4fbc72b2008-03-22 00:47:35 +000052 delay = random.random() / 10000.0
Skip Montanaro4533f602001-08-20 20:28:48 +000053 if verbose:
Jeffrey Yasskinca674122008-03-29 05:06:52 +000054 print('task %s will run for %.1f usec' %
Benjamin Petersonfdbea962008-08-18 17:33:47 +000055 (self.name, delay * 1e6))
Tim Peters84d54892005-01-08 06:03:17 +000056
Christian Heimes4fbc72b2008-03-22 00:47:35 +000057 with self.sema:
58 with self.mutex:
59 self.nrunning.inc()
60 if verbose:
61 print(self.nrunning.get(), 'tasks are running')
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +020062 self.testcase.assertLessEqual(self.nrunning.get(), 3)
Tim Peters84d54892005-01-08 06:03:17 +000063
Christian Heimes4fbc72b2008-03-22 00:47:35 +000064 time.sleep(delay)
65 if verbose:
Benjamin Petersonfdbea962008-08-18 17:33:47 +000066 print('task', self.name, 'done')
Benjamin Peterson672b8032008-06-11 19:14:14 +000067
Christian Heimes4fbc72b2008-03-22 00:47:35 +000068 with self.mutex:
69 self.nrunning.dec()
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +020070 self.testcase.assertGreaterEqual(self.nrunning.get(), 0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000071 if verbose:
72 print('%s is finished. %d tasks are running' %
Benjamin Petersonfdbea962008-08-18 17:33:47 +000073 (self.name, self.nrunning.get()))
Benjamin Peterson672b8032008-06-11 19:14:14 +000074
Skip Montanaro4533f602001-08-20 20:28:48 +000075
Antoine Pitroub0e9bd42009-10-27 20:05:26 +000076class BaseTestCase(unittest.TestCase):
77 def setUp(self):
78 self._threads = test.support.threading_setup()
79
80 def tearDown(self):
81 test.support.threading_cleanup(*self._threads)
82 test.support.reap_children()
83
84
85class ThreadTests(BaseTestCase):
Skip Montanaro4533f602001-08-20 20:28:48 +000086
Tim Peters84d54892005-01-08 06:03:17 +000087 # Create a bunch of threads, let each do some work, wait until all are
88 # done.
89 def test_various_ops(self):
90 # This takes about n/3 seconds to run (about n/3 clumps of tasks,
91 # times about 1 second per clump).
92 NUMTASKS = 10
93
94 # no more than 3 of the 10 can run at once
95 sema = threading.BoundedSemaphore(value=3)
96 mutex = threading.RLock()
97 numrunning = Counter()
98
99 threads = []
100
101 for i in range(NUMTASKS):
102 t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
103 threads.append(t)
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200104 self.assertIsNone(t.ident)
105 self.assertRegex(repr(t), r'^<TestThread\(.*, initial\)>$')
Tim Peters84d54892005-01-08 06:03:17 +0000106 t.start()
107
108 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000109 print('waiting for all tasks to complete')
Tim Peters84d54892005-01-08 06:03:17 +0000110 for t in threads:
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200111 t.join()
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200112 self.assertFalse(t.is_alive())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000113 self.assertNotEqual(t.ident, 0)
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200114 self.assertIsNotNone(t.ident)
115 self.assertRegex(repr(t), r'^<TestThread\(.*, stopped -?\d+\)>$')
Tim Peters84d54892005-01-08 06:03:17 +0000116 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000117 print('all tasks done')
Tim Peters84d54892005-01-08 06:03:17 +0000118 self.assertEqual(numrunning.get(), 0)
119
Benjamin Petersond23f8222009-04-05 19:13:16 +0000120 def test_ident_of_no_threading_threads(self):
121 # The ident still must work for the main thread and dummy threads.
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200122 self.assertIsNotNone(threading.currentThread().ident)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000123 def f():
124 ident.append(threading.currentThread().ident)
125 done.set()
126 done = threading.Event()
127 ident = []
128 _thread.start_new_thread(f, ())
129 done.wait()
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200130 self.assertIsNotNone(ident[0])
Antoine Pitrouca13a0d2009-11-08 00:30:04 +0000131 # Kill the "immortal" _DummyThread
132 del threading._active[ident[0]]
Benjamin Petersond23f8222009-04-05 19:13:16 +0000133
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134 # run with a small(ish) thread stack size (256kB)
135 def test_various_ops_small_stack(self):
136 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000137 print('with 256kB thread stack size...')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000138 try:
139 threading.stack_size(262144)
Georg Brandl2067bfd2008-05-25 13:05:15 +0000140 except _thread.error:
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +0000141 raise unittest.SkipTest(
142 'platform does not support changing thread stack size')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 self.test_various_ops()
144 threading.stack_size(0)
145
146 # run with a large thread stack size (1MB)
147 def test_various_ops_large_stack(self):
148 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000149 print('with 1MB thread stack size...')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150 try:
151 threading.stack_size(0x100000)
Georg Brandl2067bfd2008-05-25 13:05:15 +0000152 except _thread.error:
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +0000153 raise unittest.SkipTest(
154 'platform does not support changing thread stack size')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155 self.test_various_ops()
156 threading.stack_size(0)
157
Tim Peters711906e2005-01-08 07:30:42 +0000158 def test_foreign_thread(self):
159 # Check that a "foreign" thread can use the threading module.
160 def f(mutex):
Antoine Pitroub0872682009-11-09 16:08:16 +0000161 # Calling current_thread() forces an entry for the foreign
Tim Peters711906e2005-01-08 07:30:42 +0000162 # thread to get made in the threading._active map.
Antoine Pitroub0872682009-11-09 16:08:16 +0000163 threading.current_thread()
Tim Peters711906e2005-01-08 07:30:42 +0000164 mutex.release()
165
166 mutex = threading.Lock()
167 mutex.acquire()
Georg Brandl2067bfd2008-05-25 13:05:15 +0000168 tid = _thread.start_new_thread(f, (mutex,))
Tim Peters711906e2005-01-08 07:30:42 +0000169 # Wait for the thread to finish.
170 mutex.acquire()
Benjamin Peterson577473f2010-01-19 00:09:57 +0000171 self.assertIn(tid, threading._active)
Ezio Melottie9615932010-01-24 19:26:24 +0000172 self.assertIsInstance(threading._active[tid], threading._DummyThread)
Xiang Zhangf3a9fab2017-02-27 11:01:30 +0800173 #Issue 29376
174 self.assertTrue(threading._active[tid].is_alive())
175 self.assertRegex(repr(threading._active[tid]), '_DummyThread')
Tim Peters711906e2005-01-08 07:30:42 +0000176 del threading._active[tid]
Tim Peters84d54892005-01-08 06:03:17 +0000177
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000178 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
179 # exposed at the Python level. This test relies on ctypes to get at it.
180 def test_PyThreadState_SetAsyncExc(self):
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200181 ctypes = import_module("ctypes")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000182
183 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
184
185 class AsyncExc(Exception):
186 pass
187
188 exception = ctypes.py_object(AsyncExc)
189
Antoine Pitroube4d8092009-10-18 18:27:17 +0000190 # First check it works when setting the exception from the same thread.
Victor Stinner2a129742011-05-30 23:02:52 +0200191 tid = threading.get_ident()
Antoine Pitroube4d8092009-10-18 18:27:17 +0000192
193 try:
194 result = set_async_exc(ctypes.c_long(tid), exception)
195 # The exception is async, so we might have to keep the VM busy until
196 # it notices.
197 while True:
198 pass
199 except AsyncExc:
200 pass
201 else:
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000202 # This code is unreachable but it reflects the intent. If we wanted
203 # to be smarter the above loop wouldn't be infinite.
Antoine Pitroube4d8092009-10-18 18:27:17 +0000204 self.fail("AsyncExc not raised")
205 try:
206 self.assertEqual(result, 1) # one thread state modified
207 except UnboundLocalError:
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000208 # The exception was raised too quickly for us to get the result.
Antoine Pitroube4d8092009-10-18 18:27:17 +0000209 pass
210
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000211 # `worker_started` is set by the thread when it's inside a try/except
212 # block waiting to catch the asynchronously set AsyncExc exception.
213 # `worker_saw_exception` is set by the thread upon catching that
214 # exception.
215 worker_started = threading.Event()
216 worker_saw_exception = threading.Event()
217
218 class Worker(threading.Thread):
219 def run(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200220 self.id = threading.get_ident()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000221 self.finished = False
222
223 try:
224 while True:
225 worker_started.set()
226 time.sleep(0.1)
227 except AsyncExc:
228 self.finished = True
229 worker_saw_exception.set()
230
231 t = Worker()
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000232 t.daemon = True # so if this fails, we don't hang Python at shutdown
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000233 t.start()
234 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000235 print(" started worker thread")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000236
237 # Try a thread id that doesn't make sense.
238 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000239 print(" trying nonsensical thread id")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000240 result = set_async_exc(ctypes.c_long(-1), exception)
241 self.assertEqual(result, 0) # no thread states modified
242
243 # Now raise an exception in the worker thread.
244 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000245 print(" waiting for worker thread to get started")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000246 ret = worker_started.wait()
247 self.assertTrue(ret)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000248 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000249 print(" verifying worker hasn't exited")
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200250 self.assertFalse(t.finished)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000251 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000252 print(" attempting to raise asynch exception in worker")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000253 result = set_async_exc(ctypes.c_long(t.id), exception)
254 self.assertEqual(result, 1) # one thread state modified
255 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000256 print(" waiting for worker to say it caught the exception")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000257 worker_saw_exception.wait(timeout=10)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000258 self.assertTrue(t.finished)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000259 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000260 print(" all OK -- joining worker")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000261 if t.finished:
262 t.join()
263 # else the thread is still running, and we have no way to kill it
264
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000265 def test_limbo_cleanup(self):
266 # Issue 7481: Failure to start thread should cleanup the limbo map.
267 def fail_new_thread(*args):
268 raise threading.ThreadError()
269 _start_new_thread = threading._start_new_thread
270 threading._start_new_thread = fail_new_thread
271 try:
272 t = threading.Thread(target=lambda: None)
Gregory P. Smithf50f1682010-03-01 03:13:36 +0000273 self.assertRaises(threading.ThreadError, t.start)
274 self.assertFalse(
275 t in threading._limbo,
276 "Failed to cleanup _limbo map on failure of Thread.start().")
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000277 finally:
278 threading._start_new_thread = _start_new_thread
279
Christian Heimes7d2ff882007-11-30 14:35:04 +0000280 def test_finalize_runnning_thread(self):
281 # Issue 1402: the PyGILState_Ensure / _Release functions may be called
282 # very late on python exit: on deallocation of a running thread for
283 # example.
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200284 import_module("ctypes")
Christian Heimes7d2ff882007-11-30 14:35:04 +0000285
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200286 rc, out, err = assert_python_failure("-c", """if 1:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000287 import ctypes, sys, time, _thread
Christian Heimes7d2ff882007-11-30 14:35:04 +0000288
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000289 # This lock is used as a simple event variable.
Georg Brandl2067bfd2008-05-25 13:05:15 +0000290 ready = _thread.allocate_lock()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000291 ready.acquire()
292
Christian Heimes7d2ff882007-11-30 14:35:04 +0000293 # Module globals are cleared before __del__ is run
294 # So we save the functions in class dict
295 class C:
296 ensure = ctypes.pythonapi.PyGILState_Ensure
297 release = ctypes.pythonapi.PyGILState_Release
298 def __del__(self):
299 state = self.ensure()
300 self.release(state)
301
302 def waitingThread():
303 x = C()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000304 ready.release()
Christian Heimes7d2ff882007-11-30 14:35:04 +0000305 time.sleep(100)
306
Georg Brandl2067bfd2008-05-25 13:05:15 +0000307 _thread.start_new_thread(waitingThread, ())
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000308 ready.acquire() # Be sure the other thread is waiting.
Christian Heimes7d2ff882007-11-30 14:35:04 +0000309 sys.exit(42)
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200310 """)
Christian Heimes7d2ff882007-11-30 14:35:04 +0000311 self.assertEqual(rc, 42)
312
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000313 def test_finalize_with_trace(self):
314 # Issue1733757
315 # Avoid a deadlock when sys.settrace steps into threading._shutdown
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200316 assert_python_ok("-c", """if 1:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000317 import sys, threading
318
319 # A deadlock-killer, to prevent the
320 # testsuite to hang forever
321 def killer():
322 import os, time
323 time.sleep(2)
324 print('program blocked; aborting')
325 os._exit(2)
326 t = threading.Thread(target=killer)
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000327 t.daemon = True
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000328 t.start()
329
330 # This is the trace function
331 def func(frame, event, arg):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000332 threading.current_thread()
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000333 return func
334
335 sys.settrace(func)
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200336 """)
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000337
Antoine Pitrou011bd622009-10-20 21:52:47 +0000338 def test_join_nondaemon_on_shutdown(self):
339 # Issue 1722344
340 # Raising SystemExit skipped threading._shutdown
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200341 rc, out, err = assert_python_ok("-c", """if 1:
Antoine Pitrou011bd622009-10-20 21:52:47 +0000342 import threading
343 from time import sleep
344
345 def child():
346 sleep(1)
347 # As a non-daemon thread we SHOULD wake up and nothing
348 # should be torn down yet
349 print("Woke up, sleep function is:", sleep)
350
351 threading.Thread(target=child).start()
352 raise SystemExit
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200353 """)
354 self.assertEqual(out.strip(),
Antoine Pitrou899d1c62009-10-23 21:55:36 +0000355 b"Woke up, sleep function is: <built-in function sleep>")
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200356 self.assertEqual(err, b"")
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000357
Christian Heimes1af737c2008-01-23 08:24:23 +0000358 def test_enumerate_after_join(self):
359 # Try hard to trigger #1703448: a thread is still returned in
360 # threading.enumerate() after it has been join()ed.
361 enum = threading.enumerate
Antoine Pitrouc3b07572009-11-13 22:19:19 +0000362 old_interval = sys.getswitchinterval()
Christian Heimes1af737c2008-01-23 08:24:23 +0000363 try:
Jeffrey Yasskinca674122008-03-29 05:06:52 +0000364 for i in range(1, 100):
Antoine Pitrouc3b07572009-11-13 22:19:19 +0000365 sys.setswitchinterval(i * 0.0002)
Christian Heimes1af737c2008-01-23 08:24:23 +0000366 t = threading.Thread(target=lambda: None)
367 t.start()
368 t.join()
369 l = enum()
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000370 self.assertNotIn(t, l,
Christian Heimes1af737c2008-01-23 08:24:23 +0000371 "#1703448 triggered after %d trials: %s" % (i, l))
372 finally:
Antoine Pitrouc3b07572009-11-13 22:19:19 +0000373 sys.setswitchinterval(old_interval)
Christian Heimes1af737c2008-01-23 08:24:23 +0000374
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000375 def test_no_refcycle_through_target(self):
376 class RunSelfFunction(object):
377 def __init__(self, should_raise):
378 # The links in this refcycle from Thread back to self
379 # should be cleaned up when the thread completes.
380 self.should_raise = should_raise
381 self.thread = threading.Thread(target=self._run,
382 args=(self,),
383 kwargs={'yet_another':self})
384 self.thread.start()
385
386 def _run(self, other_ref, yet_another):
387 if self.should_raise:
388 raise SystemExit
389
390 cyclic_object = RunSelfFunction(should_raise=False)
391 weak_cyclic_object = weakref.ref(cyclic_object)
392 cyclic_object.thread.join()
393 del cyclic_object
Raymond Hettinger7beae8a2011-01-06 05:34:17 +0000394 self.assertIsNone(weak_cyclic_object(),
Ezio Melottib3aedd42010-11-20 19:04:17 +0000395 msg=('%d references still around' %
396 sys.getrefcount(weak_cyclic_object())))
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000397
398 raising_cyclic_object = RunSelfFunction(should_raise=True)
399 weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
400 raising_cyclic_object.thread.join()
401 del raising_cyclic_object
Raymond Hettinger7beae8a2011-01-06 05:34:17 +0000402 self.assertIsNone(weak_raising_cyclic_object(),
Ezio Melottib3aedd42010-11-20 19:04:17 +0000403 msg=('%d references still around' %
404 sys.getrefcount(weak_raising_cyclic_object())))
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000405
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000406 def test_old_threading_api(self):
407 # Just a quick sanity check to make sure the old method names are
408 # still present
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000409 t = threading.Thread()
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000410 t.isDaemon()
411 t.setDaemon(True)
412 t.getName()
413 t.setName("name")
414 t.isAlive()
415 e = threading.Event()
416 e.isSet()
417 threading.activeCount()
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000418
Brian Curtin81a4a6a2010-07-23 16:30:10 +0000419 def test_repr_daemon(self):
420 t = threading.Thread()
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200421 self.assertNotIn('daemon', repr(t))
Brian Curtin81a4a6a2010-07-23 16:30:10 +0000422 t.daemon = True
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200423 self.assertIn('daemon', repr(t))
Brett Cannon3f5f2262010-07-23 15:50:52 +0000424
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000425 def test_deamon_param(self):
426 t = threading.Thread()
427 self.assertFalse(t.daemon)
428 t = threading.Thread(daemon=False)
429 self.assertFalse(t.daemon)
430 t = threading.Thread(daemon=True)
431 self.assertTrue(t.daemon)
432
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +0200433 @unittest.skipUnless(hasattr(os, 'fork'), 'test needs fork()')
434 def test_dummy_thread_after_fork(self):
435 # Issue #14308: a dummy thread in the active list doesn't mess up
436 # the after-fork mechanism.
437 code = """if 1:
438 import _thread, threading, os, time
439
440 def background_thread(evt):
441 # Creates and registers the _DummyThread instance
442 threading.current_thread()
443 evt.set()
444 time.sleep(10)
445
446 evt = threading.Event()
447 _thread.start_new_thread(background_thread, (evt,))
448 evt.wait()
449 assert threading.active_count() == 2, threading.active_count()
450 if os.fork() == 0:
451 assert threading.active_count() == 1, threading.active_count()
452 os._exit(0)
453 else:
454 os.wait()
455 """
456 _, out, err = assert_python_ok("-c", code)
457 self.assertEqual(out, b'')
458 self.assertEqual(err, b'')
459
Charles-François Natali9939cc82013-08-30 23:32:53 +0200460 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
461 def test_is_alive_after_fork(self):
462 # Try hard to trigger #18418: is_alive() could sometimes be True on
463 # threads that vanished after a fork.
464 old_interval = sys.getswitchinterval()
465 self.addCleanup(sys.setswitchinterval, old_interval)
466
467 # Make the bug more likely to manifest.
Xavier de Gayecb9ab0f2016-12-08 12:21:00 +0100468 test.support.setswitchinterval(1e-6)
Charles-François Natali9939cc82013-08-30 23:32:53 +0200469
470 for i in range(20):
471 t = threading.Thread(target=lambda: None)
472 t.start()
473 self.addCleanup(t.join)
474 pid = os.fork()
475 if pid == 0:
476 os._exit(1 if t.is_alive() else 0)
477 else:
478 pid, status = os.waitpid(pid, 0)
479 self.assertEqual(0, status)
480
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +0300481 def test_main_thread(self):
482 main = threading.main_thread()
483 self.assertEqual(main.name, 'MainThread')
484 self.assertEqual(main.ident, threading.current_thread().ident)
485 self.assertEqual(main.ident, threading.get_ident())
486
487 def f():
488 self.assertNotEqual(threading.main_thread().ident,
489 threading.current_thread().ident)
490 th = threading.Thread(target=f)
491 th.start()
492 th.join()
493
494 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
495 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
496 def test_main_thread_after_fork(self):
497 code = """if 1:
498 import os, threading
499
500 pid = os.fork()
501 if pid == 0:
502 main = threading.main_thread()
503 print(main.name)
504 print(main.ident == threading.current_thread().ident)
505 print(main.ident == threading.get_ident())
506 else:
507 os.waitpid(pid, 0)
508 """
509 _, out, err = assert_python_ok("-c", code)
510 data = out.decode().replace('\r', '')
511 self.assertEqual(err, b"")
512 self.assertEqual(data, "MainThread\nTrue\nTrue\n")
513
514 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
515 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
516 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
517 def test_main_thread_after_fork_from_nonmain_thread(self):
518 code = """if 1:
519 import os, threading, sys
520
521 def f():
522 pid = os.fork()
523 if pid == 0:
524 main = threading.main_thread()
525 print(main.name)
526 print(main.ident == threading.current_thread().ident)
527 print(main.ident == threading.get_ident())
528 # stdout is fully buffered because not a tty,
529 # we have to flush before exit.
530 sys.stdout.flush()
531 else:
532 os.waitpid(pid, 0)
533
534 th = threading.Thread(target=f)
535 th.start()
536 th.join()
537 """
538 _, out, err = assert_python_ok("-c", code)
539 data = out.decode().replace('\r', '')
540 self.assertEqual(err, b"")
541 self.assertEqual(data, "Thread-1\nTrue\nTrue\n")
542
Antoine Pitrou7b476992013-09-07 23:38:37 +0200543 def test_tstate_lock(self):
544 # Test an implementation detail of Thread objects.
545 started = _thread.allocate_lock()
546 finish = _thread.allocate_lock()
547 started.acquire()
548 finish.acquire()
549 def f():
550 started.release()
551 finish.acquire()
552 time.sleep(0.01)
553 # The tstate lock is None until the thread is started
554 t = threading.Thread(target=f)
555 self.assertIs(t._tstate_lock, None)
556 t.start()
557 started.acquire()
558 self.assertTrue(t.is_alive())
559 # The tstate lock can't be acquired when the thread is running
560 # (or suspended).
561 tstate_lock = t._tstate_lock
562 self.assertFalse(tstate_lock.acquire(timeout=0), False)
563 finish.release()
564 # When the thread ends, the state_lock can be successfully
565 # acquired.
566 self.assertTrue(tstate_lock.acquire(timeout=5), False)
567 # But is_alive() is still True: we hold _tstate_lock now, which
568 # prevents is_alive() from knowing the thread's end-of-life C code
569 # is done.
570 self.assertTrue(t.is_alive())
571 # Let is_alive() find out the C code is done.
572 tstate_lock.release()
573 self.assertFalse(t.is_alive())
574 # And verify the thread disposed of _tstate_lock.
Serhiy Storchaka8c0f0c52016-03-14 10:28:59 +0200575 self.assertIsNone(t._tstate_lock)
Antoine Pitrou7b476992013-09-07 23:38:37 +0200576
Tim Peters72460fa2013-09-09 18:48:24 -0500577 def test_repr_stopped(self):
578 # Verify that "stopped" shows up in repr(Thread) appropriately.
579 started = _thread.allocate_lock()
580 finish = _thread.allocate_lock()
581 started.acquire()
582 finish.acquire()
583 def f():
584 started.release()
585 finish.acquire()
586 t = threading.Thread(target=f)
587 t.start()
588 started.acquire()
589 self.assertIn("started", repr(t))
590 finish.release()
591 # "stopped" should appear in the repr in a reasonable amount of time.
592 # Implementation detail: as of this writing, that's trivially true
593 # if .join() is called, and almost trivially true if .is_alive() is
594 # called. The detail we're testing here is that "stopped" shows up
595 # "all on its own".
596 LOOKING_FOR = "stopped"
597 for i in range(500):
598 if LOOKING_FOR in repr(t):
599 break
600 time.sleep(0.01)
601 self.assertIn(LOOKING_FOR, repr(t)) # we waited at least 5 seconds
Christian Heimes1af737c2008-01-23 08:24:23 +0000602
Tim Peters7634e1c2013-10-08 20:55:51 -0500603 def test_BoundedSemaphore_limit(self):
Tim Peters3d1b7a02013-10-08 21:29:27 -0500604 # BoundedSemaphore should raise ValueError if released too often.
605 for limit in range(1, 10):
606 bs = threading.BoundedSemaphore(limit)
607 threads = [threading.Thread(target=bs.acquire)
608 for _ in range(limit)]
609 for t in threads:
610 t.start()
611 for t in threads:
612 t.join()
613 threads = [threading.Thread(target=bs.release)
614 for _ in range(limit)]
615 for t in threads:
616 t.start()
617 for t in threads:
618 t.join()
619 self.assertRaises(ValueError, bs.release)
Tim Peters7634e1c2013-10-08 20:55:51 -0500620
Serhiy Storchakaf28ba362014-02-07 10:10:55 +0200621 @cpython_only
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100622 def test_frame_tstate_tracing(self):
623 # Issue #14432: Crash when a generator is created in a C thread that is
624 # destroyed while the generator is still used. The issue was that a
625 # generator contains a frame, and the frame kept a reference to the
626 # Python state of the destroyed C thread. The crash occurs when a trace
627 # function is setup.
628
629 def noop_trace(frame, event, arg):
630 # no operation
631 return noop_trace
632
633 def generator():
634 while 1:
Berker Peksag4882cac2015-04-14 09:30:01 +0300635 yield "generator"
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100636
637 def callback():
638 if callback.gen is None:
639 callback.gen = generator()
640 return next(callback.gen)
641 callback.gen = None
642
643 old_trace = sys.gettrace()
644 sys.settrace(noop_trace)
645 try:
646 # Install a trace function
647 threading.settrace(noop_trace)
648
649 # Create a generator in a C thread which exits after the call
Serhiy Storchakaf28ba362014-02-07 10:10:55 +0200650 import _testcapi
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100651 _testcapi.call_in_temporary_c_thread(callback)
652
653 # Call the generator in a different Python thread, check that the
654 # generator didn't keep a reference to the destroyed thread state
655 for test in range(3):
656 # The trace function is still called here
657 callback()
658 finally:
659 sys.settrace(old_trace)
660
Victor Stinner45956b92013-11-12 16:37:55 +0100661
Antoine Pitroub0e9bd42009-10-27 20:05:26 +0000662class ThreadJoinOnShutdown(BaseTestCase):
Jesse Nollera8513972008-07-17 16:49:17 +0000663
664 def _run_and_join(self, script):
665 script = """if 1:
666 import sys, os, time, threading
667
668 # a thread, which waits for the main program to terminate
669 def joiningfunc(mainthread):
670 mainthread.join()
671 print('end of thread')
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000672 # stdout is fully buffered because not a tty, we have to flush
673 # before exit.
674 sys.stdout.flush()
Jesse Nollera8513972008-07-17 16:49:17 +0000675 \n""" + script
676
Antoine Pitrouc4d78642011-05-05 20:17:32 +0200677 rc, out, err = assert_python_ok("-c", script)
678 data = out.decode().replace('\r', '')
Benjamin Petersonad703dc2008-07-17 17:02:57 +0000679 self.assertEqual(data, "end of main\nend of thread\n")
Jesse Nollera8513972008-07-17 16:49:17 +0000680
681 def test_1_join_on_shutdown(self):
682 # The usual case: on exit, wait for a non-daemon thread
683 script = """if 1:
684 import os
685 t = threading.Thread(target=joiningfunc,
686 args=(threading.current_thread(),))
687 t.start()
688 time.sleep(0.1)
689 print('end of main')
690 """
691 self._run_and_join(script)
692
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +0000693 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
Victor Stinner26d31862011-07-01 14:26:24 +0200694 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
Jesse Nollera8513972008-07-17 16:49:17 +0000695 def test_2_join_in_forked_process(self):
696 # Like the test above, but from a forked interpreter
Jesse Nollera8513972008-07-17 16:49:17 +0000697 script = """if 1:
698 childpid = os.fork()
699 if childpid != 0:
700 os.waitpid(childpid, 0)
701 sys.exit(0)
702
703 t = threading.Thread(target=joiningfunc,
704 args=(threading.current_thread(),))
705 t.start()
706 print('end of main')
707 """
708 self._run_and_join(script)
709
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +0000710 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
Victor Stinner26d31862011-07-01 14:26:24 +0200711 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000712 def test_3_join_in_forked_from_thread(self):
Jesse Nollera8513972008-07-17 16:49:17 +0000713 # Like the test above, but fork() was called from a worker thread
714 # In the forked process, the main Thread object must be marked as stopped.
Alexandre Vassalotti93f2cd22009-07-22 04:54:52 +0000715
Jesse Nollera8513972008-07-17 16:49:17 +0000716 script = """if 1:
717 main_thread = threading.current_thread()
718 def worker():
719 childpid = os.fork()
720 if childpid != 0:
721 os.waitpid(childpid, 0)
722 sys.exit(0)
723
724 t = threading.Thread(target=joiningfunc,
725 args=(main_thread,))
726 print('end of main')
727 t.start()
728 t.join() # Should not block: main_thread is already stopped
729
730 w = threading.Thread(target=worker)
731 w.start()
732 """
733 self._run_and_join(script)
734
Victor Stinner26d31862011-07-01 14:26:24 +0200735 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
Tim Petersc363a232013-09-08 18:44:40 -0500736 def test_4_daemon_threads(self):
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200737 # Check that a daemon thread cannot crash the interpreter on shutdown
738 # by manipulating internal structures that are being disposed of in
739 # the main thread.
740 script = """if True:
741 import os
742 import random
743 import sys
744 import time
745 import threading
746
747 thread_has_run = set()
748
749 def random_io():
750 '''Loop for a while sleeping random tiny amounts and doing some I/O.'''
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200751 while True:
Victor Stinnera6d2c762011-06-30 18:20:11 +0200752 in_f = open(os.__file__, 'rb')
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200753 stuff = in_f.read(200)
Victor Stinnera6d2c762011-06-30 18:20:11 +0200754 null_f = open(os.devnull, 'wb')
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200755 null_f.write(stuff)
756 time.sleep(random.random() / 1995)
757 null_f.close()
758 in_f.close()
759 thread_has_run.add(threading.current_thread())
760
761 def main():
762 count = 0
763 for _ in range(40):
764 new_thread = threading.Thread(target=random_io)
765 new_thread.daemon = True
766 new_thread.start()
767 count += 1
768 while len(thread_has_run) < count:
769 time.sleep(0.001)
770 # Trigger process shutdown
771 sys.exit(0)
772
773 main()
774 """
775 rc, out, err = assert_python_ok('-c', script)
776 self.assertFalse(err)
777
Charles-François Natali6d0d24e2012-02-02 20:31:42 +0100778 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
Charles-François Natalib2c9e9a2012-02-08 21:29:11 +0100779 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
Charles-François Natali6d0d24e2012-02-02 20:31:42 +0100780 def test_reinit_tls_after_fork(self):
781 # Issue #13817: fork() would deadlock in a multithreaded program with
782 # the ad-hoc TLS implementation.
783
784 def do_fork_and_wait():
785 # just fork a child process and wait it
786 pid = os.fork()
787 if pid > 0:
788 os.waitpid(pid, 0)
789 else:
790 os._exit(0)
791
792 # start a bunch of threads that will fork() child processes
793 threads = []
794 for i in range(16):
795 t = threading.Thread(target=do_fork_and_wait)
796 threads.append(t)
797 t.start()
798
799 for t in threads:
800 t.join()
801
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200802 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
803 def test_clear_threads_states_after_fork(self):
804 # Issue #17094: check that threads states are cleared after fork()
805
806 # start a bunch of threads
807 threads = []
808 for i in range(16):
809 t = threading.Thread(target=lambda : time.sleep(0.3))
810 threads.append(t)
811 t.start()
812
813 pid = os.fork()
814 if pid == 0:
815 # check that threads states have been cleared
816 if len(sys._current_frames()) == 1:
817 os._exit(0)
818 else:
819 os._exit(1)
820 else:
821 _, status = os.waitpid(pid, 0)
822 self.assertEqual(0, status)
823
824 for t in threads:
825 t.join()
826
Jesse Nollera8513972008-07-17 16:49:17 +0000827
Antoine Pitrou7eaf3f72013-08-25 19:48:18 +0200828class SubinterpThreadingTests(BaseTestCase):
829
830 def test_threads_join(self):
831 # Non-daemon threads should be joined at subinterpreter shutdown
832 # (issue #18808)
833 r, w = os.pipe()
834 self.addCleanup(os.close, r)
835 self.addCleanup(os.close, w)
836 code = r"""if 1:
837 import os
838 import threading
839 import time
840
841 def f():
842 # Sleep a bit so that the thread is still running when
843 # Py_EndInterpreter is called.
844 time.sleep(0.05)
845 os.write(%d, b"x")
846 threading.Thread(target=f).start()
847 """ % (w,)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100848 ret = test.support.run_in_subinterp(code)
Antoine Pitrou7eaf3f72013-08-25 19:48:18 +0200849 self.assertEqual(ret, 0)
850 # The thread was joined properly.
851 self.assertEqual(os.read(r, 1), b"x")
852
Antoine Pitrou7b476992013-09-07 23:38:37 +0200853 def test_threads_join_2(self):
854 # Same as above, but a delay gets introduced after the thread's
855 # Python code returned but before the thread state is deleted.
856 # To achieve this, we register a thread-local object which sleeps
857 # a bit when deallocated.
858 r, w = os.pipe()
859 self.addCleanup(os.close, r)
860 self.addCleanup(os.close, w)
861 code = r"""if 1:
862 import os
863 import threading
864 import time
865
866 class Sleeper:
867 def __del__(self):
868 time.sleep(0.05)
869
870 tls = threading.local()
871
872 def f():
873 # Sleep a bit so that the thread is still running when
874 # Py_EndInterpreter is called.
875 time.sleep(0.05)
876 tls.x = Sleeper()
877 os.write(%d, b"x")
878 threading.Thread(target=f).start()
879 """ % (w,)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100880 ret = test.support.run_in_subinterp(code)
Antoine Pitrou7b476992013-09-07 23:38:37 +0200881 self.assertEqual(ret, 0)
882 # The thread was joined properly.
883 self.assertEqual(os.read(r, 1), b"x")
884
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200885 @cpython_only
Antoine Pitrou7eaf3f72013-08-25 19:48:18 +0200886 def test_daemon_threads_fatal_error(self):
887 subinterp_code = r"""if 1:
888 import os
889 import threading
890 import time
891
892 def f():
893 # Make sure the daemon thread is still running when
894 # Py_EndInterpreter is called.
895 time.sleep(10)
896 threading.Thread(target=f, daemon=True).start()
897 """
898 script = r"""if 1:
899 import _testcapi
900
901 _testcapi.run_in_subinterp(%r)
902 """ % (subinterp_code,)
Antoine Pitrou77e904e2013-10-08 23:04:32 +0200903 with test.support.SuppressCrashReport():
904 rc, out, err = assert_python_failure("-c", script)
Antoine Pitrou7eaf3f72013-08-25 19:48:18 +0200905 self.assertIn("Fatal Python error: Py_EndInterpreter: "
906 "not the last thread", err.decode())
907
908
Antoine Pitroub0e9bd42009-10-27 20:05:26 +0000909class ThreadingExceptionTests(BaseTestCase):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000910 # A RuntimeError should be raised if Thread.start() is called
911 # multiple times.
912 def test_start_thread_again(self):
913 thread = threading.Thread()
914 thread.start()
915 self.assertRaises(RuntimeError, thread.start)
916
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000917 def test_joining_current_thread(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000918 current_thread = threading.current_thread()
919 self.assertRaises(RuntimeError, current_thread.join);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000920
921 def test_joining_inactive_thread(self):
922 thread = threading.Thread()
923 self.assertRaises(RuntimeError, thread.join)
924
925 def test_daemonize_active_thread(self):
926 thread = threading.Thread()
927 thread.start()
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000928 self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000929
Antoine Pitroufcf81fd2011-02-28 22:03:34 +0000930 def test_releasing_unacquired_lock(self):
931 lock = threading.Lock()
932 self.assertRaises(RuntimeError, lock.release)
933
Benjamin Petersond541d3f2012-10-13 11:46:44 -0400934 @unittest.skipUnless(sys.platform == 'darwin' and test.support.python_is_optimized(),
935 'test macosx problem')
Ned Deily9a7c5242011-05-28 00:19:56 -0700936 def test_recursion_limit(self):
937 # Issue 9670
938 # test that excessive recursion within a non-main thread causes
939 # an exception rather than crashing the interpreter on platforms
940 # like Mac OS X or FreeBSD which have small default stack sizes
941 # for threads
942 script = """if True:
943 import threading
944
945 def recurse():
946 return recurse()
947
948 def outer():
949 try:
950 recurse()
Yury Selivanovf488fb42015-07-03 01:04:23 -0400951 except RecursionError:
Ned Deily9a7c5242011-05-28 00:19:56 -0700952 pass
953
954 w = threading.Thread(target=outer)
955 w.start()
956 w.join()
957 print('end of main thread')
958 """
959 expected_output = "end of main thread\n"
960 p = subprocess.Popen([sys.executable, "-c", script],
Antoine Pitroub8b6a682012-06-29 19:40:35 +0200961 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Ned Deily9a7c5242011-05-28 00:19:56 -0700962 stdout, stderr = p.communicate()
963 data = stdout.decode().replace('\r', '')
Antoine Pitroub8b6a682012-06-29 19:40:35 +0200964 self.assertEqual(p.returncode, 0, "Unexpected error: " + stderr.decode())
Ned Deily9a7c5242011-05-28 00:19:56 -0700965 self.assertEqual(data, expected_output)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000966
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300967 def test_print_exception(self):
968 script = r"""if True:
969 import threading
970 import time
971
972 running = False
973 def run():
974 global running
975 running = True
976 while running:
977 time.sleep(0.01)
978 1/0
979 t = threading.Thread(target=run)
980 t.start()
981 while not running:
982 time.sleep(0.01)
983 running = False
984 t.join()
985 """
986 rc, out, err = assert_python_ok("-c", script)
987 self.assertEqual(out, b'')
988 err = err.decode()
989 self.assertIn("Exception in thread", err)
990 self.assertIn("Traceback (most recent call last):", err)
991 self.assertIn("ZeroDivisionError", err)
992 self.assertNotIn("Unhandled exception", err)
993
Serhiy Storchakaa7930372016-07-03 22:27:26 +0300994 @requires_type_collecting
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300995 def test_print_exception_stderr_is_none_1(self):
996 script = r"""if True:
997 import sys
998 import threading
999 import time
1000
1001 running = False
1002 def run():
1003 global running
1004 running = True
1005 while running:
1006 time.sleep(0.01)
1007 1/0
1008 t = threading.Thread(target=run)
1009 t.start()
1010 while not running:
1011 time.sleep(0.01)
1012 sys.stderr = None
1013 running = False
1014 t.join()
1015 """
1016 rc, out, err = assert_python_ok("-c", script)
1017 self.assertEqual(out, b'')
1018 err = err.decode()
1019 self.assertIn("Exception in thread", err)
1020 self.assertIn("Traceback (most recent call last):", err)
1021 self.assertIn("ZeroDivisionError", err)
1022 self.assertNotIn("Unhandled exception", err)
1023
1024 def test_print_exception_stderr_is_none_2(self):
1025 script = r"""if True:
1026 import sys
1027 import threading
1028 import time
1029
1030 running = False
1031 def run():
1032 global running
1033 running = True
1034 while running:
1035 time.sleep(0.01)
1036 1/0
1037 sys.stderr = None
1038 t = threading.Thread(target=run)
1039 t.start()
1040 while not running:
1041 time.sleep(0.01)
1042 running = False
1043 t.join()
1044 """
1045 rc, out, err = assert_python_ok("-c", script)
1046 self.assertEqual(out, b'')
1047 self.assertNotIn("Unhandled exception", err.decode())
1048
Victor Stinnereec93312016-08-18 18:13:10 +02001049 def test_bare_raise_in_brand_new_thread(self):
1050 def bare_raise():
1051 raise
1052
1053 class Issue27558(threading.Thread):
1054 exc = None
1055
1056 def run(self):
1057 try:
1058 bare_raise()
1059 except Exception as exc:
1060 self.exc = exc
1061
1062 thread = Issue27558()
1063 thread.start()
1064 thread.join()
1065 self.assertIsNotNone(thread.exc)
1066 self.assertIsInstance(thread.exc, RuntimeError)
Serhiy Storchaka52005c22014-09-21 22:08:13 +03001067
R David Murray19aeb432013-03-30 17:19:38 -04001068class TimerTests(BaseTestCase):
1069
1070 def setUp(self):
1071 BaseTestCase.setUp(self)
1072 self.callback_args = []
1073 self.callback_event = threading.Event()
1074
1075 def test_init_immutable_default_args(self):
1076 # Issue 17435: constructor defaults were mutable objects, they could be
1077 # mutated via the object attributes and affect other Timer objects.
1078 timer1 = threading.Timer(0.01, self._callback_spy)
1079 timer1.start()
1080 self.callback_event.wait()
1081 timer1.args.append("blah")
1082 timer1.kwargs["foo"] = "bar"
1083 self.callback_event.clear()
1084 timer2 = threading.Timer(0.01, self._callback_spy)
1085 timer2.start()
1086 self.callback_event.wait()
1087 self.assertEqual(len(self.callback_args), 2)
1088 self.assertEqual(self.callback_args, [((), {}), ((), {})])
1089
1090 def _callback_spy(self, *args, **kwargs):
1091 self.callback_args.append((args[:], kwargs.copy()))
1092 self.callback_event.set()
1093
Antoine Pitrou557934f2009-11-06 22:41:14 +00001094class LockTests(lock_tests.LockTests):
1095 locktype = staticmethod(threading.Lock)
1096
Antoine Pitrou434736a2009-11-10 18:46:01 +00001097class PyRLockTests(lock_tests.RLockTests):
1098 locktype = staticmethod(threading._PyRLock)
1099
Charles-François Natali6b671b22012-01-28 11:36:04 +01001100@unittest.skipIf(threading._CRLock is None, 'RLock not implemented in C')
Antoine Pitrou434736a2009-11-10 18:46:01 +00001101class CRLockTests(lock_tests.RLockTests):
1102 locktype = staticmethod(threading._CRLock)
Antoine Pitrou557934f2009-11-06 22:41:14 +00001103
1104class EventTests(lock_tests.EventTests):
1105 eventtype = staticmethod(threading.Event)
1106
1107class ConditionAsRLockTests(lock_tests.RLockTests):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001108 # Condition uses an RLock by default and exports its API.
Antoine Pitrou557934f2009-11-06 22:41:14 +00001109 locktype = staticmethod(threading.Condition)
1110
1111class ConditionTests(lock_tests.ConditionTests):
1112 condtype = staticmethod(threading.Condition)
1113
1114class SemaphoreTests(lock_tests.SemaphoreTests):
1115 semtype = staticmethod(threading.Semaphore)
1116
1117class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
1118 semtype = staticmethod(threading.BoundedSemaphore)
1119
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +00001120class BarrierTests(lock_tests.BarrierTests):
1121 barriertype = staticmethod(threading.Barrier)
Antoine Pitrou557934f2009-11-06 22:41:14 +00001122
Martin Panter19e69c52015-11-14 12:46:42 +00001123class MiscTestCase(unittest.TestCase):
1124 def test__all__(self):
1125 extra = {"ThreadError"}
1126 blacklist = {'currentThread', 'activeCount'}
1127 support.check__all__(self, threading, ('threading', '_thread'),
1128 extra=extra, blacklist=blacklist)
1129
Tim Peters84d54892005-01-08 06:03:17 +00001130if __name__ == "__main__":
R David Murray19aeb432013-03-30 17:19:38 -04001131 unittest.main()