diff --git a/mitogen/core.py b/mitogen/core.py index f8c0b2db..d64f48fb 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -386,6 +386,20 @@ def _partition(s, sep, find): return left, sep, s[len(left)+len(sep):] +def threading__current_thread(): + try: + return threading.current_thread() # Added in Python 2.6+ + except AttributeError: + return threading.currentThread() # Deprecated in Python 3.10+ + + +def threading__thread_name(thread): + try: + return thread.name # Added in Python 2.6+ + except AttributeError: + return thread.getName() # Deprecated in Python 3.10+ + + if hasattr(UnicodeType, 'rpartition'): str_partition = UnicodeType.partition str_rpartition = UnicodeType.rpartition @@ -2758,7 +2772,7 @@ class Latch(object): return 'Latch(%#x, size=%d, t=%r)' % ( id(self), len(self._queue), - threading.currentThread().getName(), + threading__thread_name(threading__current_thread()), ) diff --git a/mitogen/master.py b/mitogen/master.py index 31dd4e3d..3a163a8b 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -1236,7 +1236,7 @@ class Broker(mitogen.core.Broker): def __init__(self, install_watcher=True): if install_watcher: self._watcher = ThreadWatcher.watch( - target=threading.currentThread(), + target=mitogen.core.threading__current_thread(), on_join=self.shutdown, ) super(Broker, self).__init__() diff --git a/mitogen/os_fork.py b/mitogen/os_fork.py index da832c65..9c649d07 100644 --- a/mitogen/os_fork.py +++ b/mitogen/os_fork.py @@ -35,7 +35,6 @@ Support for operating in a mixed threading/forking environment. import os import socket import sys -import threading import weakref import mitogen.core @@ -158,7 +157,7 @@ class Corker(object): held. This will not return until each thread acknowledges it has ceased execution. """ - current = threading.currentThread() + current = mitogen.core.threading__current_thread() s = mitogen.core.b('CORK') * ((128 // 4) * 1024) self._rsocks = [] diff --git a/mitogen/service.py b/mitogen/service.py index 249a8781..d4f5f730 100644 --- a/mitogen/service.py +++ b/mitogen/service.py @@ -109,7 +109,8 @@ def get_or_create_pool(size=None, router=None, context=None): def get_thread_name(): - return threading.currentThread().getName() + thread = mitogen.core.threading__current_thread() + return mitogen.core.threading__thread_name(thread) def call(service_name, method_name, call_context=None, **kwargs): diff --git a/tests/broker_test.py b/tests/broker_test.py index c8b12bff..d58d9480 100644 --- a/tests/broker_test.py +++ b/tests/broker_test.py @@ -1,7 +1,3 @@ - -import time -import threading - import mock import testlib @@ -52,7 +48,7 @@ class DeferSyncTest(testlib.TestCase): def test_okay(self): broker = self.klass() try: - th = broker.defer_sync(lambda: threading.currentThread()) + th = broker.defer_sync(lambda: mitogen.core.threading__current_thread()) self.assertEqual(th, broker._thread) finally: broker.shutdown() diff --git a/tests/testlib.py b/tests/testlib.py index 784a643a..6cebe9c5 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -116,6 +116,13 @@ def threading__thread_is_alive(thread): return thread.isAlive() +def threading_thread_name(thread): + try: + return thread.name # Available in Python 2.6+ + except AttributeError: + return thread.getName() # Deprecated in Python 3.10+ + + def wait_for_port( host, port, @@ -349,7 +356,7 @@ class TestCase(unittest.TestCase): def _teardown_check_threads(self): counts = {} for thread in threading.enumerate(): - name = thread.getName() + name = threading_thread_name(thread) # Python 2.4: enumerate() may return stopped threads. assert \ not threading__thread_is_alive(thread) \