Finally figured out why this module did its imports at the
bottom of the file. Restored that, and added a comment explaining why this is necessary. Hint: on my box, and yours, it's not :-( Also added an __all__ list.
This commit is contained in:
parent
0969e8ad4e
commit
e247e89846
|
@ -1,9 +1,9 @@
|
||||||
"""Thread-local objects
|
"""Thread-local objects.
|
||||||
|
|
||||||
(Note that this module provides a Python version of thread
|
(Note that this module provides a Python version of the threading.local
|
||||||
threading.local class. Depending on the version of Python you're
|
class. Depending on the version of Python you're using, there may be a
|
||||||
using, there may be a faster one available. You should always import
|
faster one available. You should always import the `local` class from
|
||||||
the local class from threading.)
|
`threading`.)
|
||||||
|
|
||||||
Thread-local objects support the management of thread-local data.
|
Thread-local objects support the management of thread-local data.
|
||||||
If you have data that you want to be local to a thread, simply create
|
If you have data that you want to be local to a thread, simply create
|
||||||
|
@ -133,7 +133,17 @@ affects what we see:
|
||||||
>>> del mydata
|
>>> del mydata
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from threading import currentThread, RLock, enumerate
|
__all__ = ["local"]
|
||||||
|
|
||||||
|
# We need to use objects from the threading module, but the threading
|
||||||
|
# module may also want to use our `local` class, if support for locals
|
||||||
|
# isn't compiled in to the `thread` module. This creates potential problems
|
||||||
|
# with circular imports. For that reason, we don't import `threading`
|
||||||
|
# until the bottom of this file (a hack sufficient to worm around the
|
||||||
|
# potential problems). Note that almost all platforms do have support for
|
||||||
|
# locals in the `thread` module, and there is no circular import problem
|
||||||
|
# then, so problems introduced by fiddling the order of imports here won't
|
||||||
|
# manifest on most boxes.
|
||||||
|
|
||||||
class _localbase(object):
|
class _localbase(object):
|
||||||
__slots__ = '_local__key', '_local__args', '_local__lock'
|
__slots__ = '_local__key', '_local__args', '_local__lock'
|
||||||
|
@ -202,16 +212,13 @@ class local(_localbase):
|
||||||
finally:
|
finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
# The default argument is a hack, to give __del__ a local name for
|
import threading
|
||||||
# threading.enumerate (sidestepping problems with Python None'ing-out
|
|
||||||
# module globals at shutdown time).
|
|
||||||
def __del__(self, _threading_enumerate=enumerate):
|
|
||||||
|
|
||||||
key = object.__getattribute__(self, '_local__key')
|
key = object.__getattribute__(self, '_local__key')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
threads = list(_threading_enumerate())
|
threads = list(threading.enumerate())
|
||||||
except:
|
except:
|
||||||
# If enumerate fails, as it seems to do during
|
# If enumerate fails, as it seems to do during
|
||||||
# shutdown, we'll skip cleanup under the assumption
|
# shutdown, we'll skip cleanup under the assumption
|
||||||
|
@ -230,3 +237,5 @@ class local(_localbase):
|
||||||
del __dict__[key]
|
del __dict__[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # didn't have anything in this thread
|
pass # didn't have anything in this thread
|
||||||
|
|
||||||
|
from threading import currentThread, RLock
|
||||||
|
|
Loading…
Reference in New Issue