diff --git a/Lib/_threading_local.py b/Lib/_threading_local.py index 2af3885458b54f..9b16979c7cb418 100644 --- a/Lib/_threading_local.py +++ b/Lib/_threading_local.py @@ -36,11 +36,13 @@ def __init__(self): def get_dict(self): """Return the dict for the current thread. Raises KeyError if none defined.""" + from threading import current_thread thread = current_thread() return self.dicts[id(thread)][1] def create_dict(self): """Create a new dict for the current thread, and return it.""" + from threading import current_thread localdict = {} key = self.key thread = current_thread() @@ -85,6 +87,7 @@ class local: def __new__(cls, /, *args, **kw): if (args or kw) and (cls.__init__ is object.__init__): raise TypeError("Initialization arguments are not supported") + from threading import RLock self = object.__new__(cls) impl = _localimpl() impl.localargs = (args, kw) @@ -115,6 +118,3 @@ def __delattr__(self, name): % self.__class__.__name__) with _patch(self): return object.__delattr__(self, name) - - -from threading import current_thread, RLock diff --git a/Lib/threading.py b/Lib/threading.py index abac31e25886fa..5e54ee2087e73d 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -61,13 +61,11 @@ TIMEOUT_MAX = _thread.TIMEOUT_MAX del _thread -# get thread-local implementation, either from the thread -# module, or from the python fallback - -try: - from _thread import _local as local -except ImportError: - from _threading_local import local +# get thread-local implementation from the thread module +# (fallback to _threading_local is obsolete since Python 3.7 - thread support +# is always available, and _thread._local always exists; keeping the fallback +# would reintroduce the circular import with _threading_local) +from _thread import _local as local # Support for profile and trace hooks