Skip to content

Commit 6c1585d

Browse files
committed
tests: Compatiblity shim for threading.Thread.is_alive()
On Python >= 3.8 thread.isAlive() is deprecated (removed in Python 3.9. On Python <= 2.5 thread.is_alive() isn't present (added in Python 2.6). (cherry picked from commit 4b39013)
1 parent 01dc412 commit 6c1585d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

tests/testlib.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def Popen__terminate(proc):
103103
Popen__terminate = subprocess.Popen.terminate
104104

105105

106+
def threading__thread_is_alive(thread):
107+
"""Return whether the thread is alive (Python version compatibility shim).
108+
109+
On Python >= 3.8 thread.isAlive() is deprecated (removed in Python 3.9).
110+
On Python <= 2.5 thread.is_alive() isn't present (added in Python 2.6).
111+
"""
112+
try:
113+
return thread.is_alive()
114+
except AttributeError:
115+
return thread.isAlive()
116+
117+
106118
def wait_for_port(
107119
host,
108120
port,
@@ -334,7 +346,9 @@ def _teardown_check_threads(self):
334346
for thread in threading.enumerate():
335347
name = thread.getName()
336348
# Python 2.4: enumerate() may return stopped threads.
337-
assert (not thread.isAlive()) or name in self.ALLOWED_THREADS, \
349+
assert \
350+
not threading__thread_is_alive(thread) \
351+
or name in self.ALLOWED_THREADS, \
338352
'Found thread %r still running after tests.' % (name,)
339353
counts[name] = counts.get(name, 0) + 1
340354

tests/utils_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class RunWithRouterTest(testlib.TestCase):
3131
def test_run_with_broker(self):
3232
router = mitogen.utils.run_with_router(func0)
3333
self.assertIsInstance(router, mitogen.master.Router)
34-
self.assertFalse(router.broker._thread.isAlive())
34+
self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread))
3535

3636

3737
class WithRouterTest(testlib.TestCase):
3838
def test_with_broker(self):
3939
router = func()
4040
self.assertIsInstance(router, mitogen.master.Router)
41-
self.assertFalse(router.broker._thread.isAlive())
41+
self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread))
4242

4343

4444
class Dict(dict): pass

0 commit comments

Comments
 (0)