summaryrefslogtreecommitdiffstats
path: root/Lib/decimal.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-07-14 21:04:27 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-07-14 21:04:27 (GMT)
commitef66debd7ef590304466d13dde4082632750fa7c (patch)
tree4e4f2f5214e55944a36fc3da0cbb517186e793f7 /Lib/decimal.py
parent99148e7eaad7741fbfb0822009b7c9b216ecc227 (diff)
downloadcpython-ef66debd7ef590304466d13dde4082632750fa7c.zip
cpython-ef66debd7ef590304466d13dde4082632750fa7c.tar.gz
cpython-ef66debd7ef590304466d13dde4082632750fa7c.tar.bz2
Use threading.local() instead of threading.currentThread().
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r--Lib/decimal.py78
1 files changed, 57 insertions, 21 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index ba94d7a..717c6b7 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -374,30 +374,66 @@ _condition_map = {ConversionSyntax:InvalidOperation,
##### Context Functions #######################################
-#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__
-
-def setcontext(context):
- """Set this thread's context to context."""
- if context in (DefaultContext, BasicContext, ExtendedContext):
- context = context.copy()
- threading.currentThread().__decimal_context__ = context
+# The getcontext() and setcontext() function manage access to a thread-local
+# current context. Py2.4 offers direct support for thread locals. If that
+# is not available, use threading.currentThread() which is slower but will
+# work for older Pythons.
-def getcontext():
- """Returns this thread's context.
+try:
+ threading.local
- 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 threading.currentThread().__decimal_context__
- except AttributeError:
- context = Context()
+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__
+
+ def setcontext(context):
+ """Set this thread's context to context."""
+ if context in (DefaultContext, BasicContext, ExtendedContext):
+ context = context.copy()
threading.currentThread().__decimal_context__ = context
- return context
+
+ def getcontext():
+ """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 threading.currentThread().__decimal_context__
+ except AttributeError:
+ context = Context()
+ threading.currentThread().__decimal_context__ = 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 ###########################################