From 6c1585de77dab5a1a6a72e1a551d76f6cc15f889 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Sun, 10 Jan 2021 21:00:06 +0000 Subject: [PATCH] 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 4b39013ef43bd22791bbab870d731e26e7c9b6aa) --- tests/testlib.py | 16 +++++++++++++++- tests/utils_test.py | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/testlib.py b/tests/testlib.py index ace8f0a2..ee76a26d 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -103,6 +103,18 @@ if hasattr(subprocess.Popen, 'terminate'): Popen__terminate = subprocess.Popen.terminate +def threading__thread_is_alive(thread): + """Return whether the thread is alive (Python version compatibility shim). + + 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). + """ + try: + return thread.is_alive() + except AttributeError: + return thread.isAlive() + + def wait_for_port( host, port, @@ -334,7 +346,9 @@ class TestCase(unittest2.TestCase): for thread in threading.enumerate(): name = thread.getName() # Python 2.4: enumerate() may return stopped threads. - assert (not thread.isAlive()) or name in self.ALLOWED_THREADS, \ + assert \ + not threading__thread_is_alive(thread) \ + or name in self.ALLOWED_THREADS, \ 'Found thread %r still running after tests.' % (name,) counts[name] = counts.get(name, 0) + 1 diff --git a/tests/utils_test.py b/tests/utils_test.py index a70b23dc..b5204a3c 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -31,14 +31,14 @@ class RunWithRouterTest(testlib.TestCase): def test_run_with_broker(self): router = mitogen.utils.run_with_router(func0) self.assertIsInstance(router, mitogen.master.Router) - self.assertFalse(router.broker._thread.isAlive()) + self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread)) class WithRouterTest(testlib.TestCase): def test_with_broker(self): router = func() self.assertIsInstance(router, mitogen.master.Router) - self.assertFalse(router.broker._thread.isAlive()) + self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread)) class Dict(dict): pass