Commit ef66debd authored by Raymond Hettinger's avatar Raymond Hettinger

Use threading.local() instead of threading.currentThread().

parent 99148e7e
...@@ -374,18 +374,28 @@ _condition_map = {ConversionSyntax:InvalidOperation, ...@@ -374,18 +374,28 @@ _condition_map = {ConversionSyntax:InvalidOperation,
##### Context Functions ####################################### ##### Context Functions #######################################
#To fix reloading, force it to create a new context # The getcontext() and setcontext() function manage access to a thread-local
#Old contexts have different exceptions in their dicts, making problems. # current context. Py2.4 offers direct support for thread locals. If that
if hasattr(threading.currentThread(), '__decimal_context__'): # is not available, use threading.currentThread() which is slower but will
# work for older Pythons.
try:
threading.local
except AttributeError:
#To fix reloading, force it to create a new context
#Old contexts have different exceptions in their dicts, making problems.
if hasattr(threading.currentThread(), '__decimal_context__'):
del threading.currentThread().__decimal_context__ del threading.currentThread().__decimal_context__
def setcontext(context): def setcontext(context):
"""Set this thread's context to context.""" """Set this thread's context to context."""
if context in (DefaultContext, BasicContext, ExtendedContext): if context in (DefaultContext, BasicContext, ExtendedContext):
context = context.copy() context = context.copy()
threading.currentThread().__decimal_context__ = context threading.currentThread().__decimal_context__ = context
def getcontext(): def getcontext():
"""Returns this thread's context. """Returns this thread's context.
If this thread does not yet have a context, returns If this thread does not yet have a context, returns
...@@ -399,6 +409,32 @@ def getcontext(): ...@@ -399,6 +409,32 @@ def getcontext():
threading.currentThread().__decimal_context__ = context threading.currentThread().__decimal_context__ = context
return context return context
else:
local = threading.local()
def getcontext(_local=local):
"""Returns this thread's context.
If this thread does not yet have a context, returns
a new context and sets this thread's context.
New contexts are copies of DefaultContext.
"""
try:
return _local.__decimal_context__
except AttributeError:
context = Context()
_local.__decimal_context__ = context
return context
def setcontext(context, _local=local):
"""Set this thread's context to context."""
if context in (DefaultContext, BasicContext, ExtendedContext):
context = context.copy()
_local.__decimal_context__ = context
del threading, local # Don't contaminate the namespace
##### Decimal class ########################################### ##### Decimal class ###########################################
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment