Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef66deb

Browse files
committed
Use threading.local() instead of threading.currentThread().
1 parent 99148e7 commit ef66deb

1 file changed

Lines changed: 57 additions & 21 deletions

File tree

Lib/decimal.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -374,30 +374,66 @@ class Underflow(Inexact, Rounded, Subnormal):
374374

375375
##### Context Functions #######################################
376376

377-
#To fix reloading, force it to create a new context
378-
#Old contexts have different exceptions in their dicts, making problems.
379-
if hasattr(threading.currentThread(), '__decimal_context__'):
380-
del threading.currentThread().__decimal_context__
381-
382-
def setcontext(context):
383-
"""Set this thread's context to context."""
384-
if context in (DefaultContext, BasicContext, ExtendedContext):
385-
context = context.copy()
386-
threading.currentThread().__decimal_context__ = context
377+
# The getcontext() and setcontext() function manage access to a thread-local
378+
# current context. Py2.4 offers direct support for thread locals. If that
379+
# is not available, use threading.currentThread() which is slower but will
380+
# work for older Pythons.
387381

388-
def getcontext():
389-
"""Returns this thread's context.
382+
try:
383+
threading.local
390384

391-
If this thread does not yet have a context, returns
392-
a new context and sets this thread's context.
393-
New contexts are copies of DefaultContext.
394-
"""
395-
try:
396-
return threading.currentThread().__decimal_context__
397-
except AttributeError:
398-
context = Context()
385+
except AttributeError:
386+
387+
#To fix reloading, force it to create a new context
388+
#Old contexts have different exceptions in their dicts, making problems.
389+
if hasattr(threading.currentThread(), '__decimal_context__'):
390+
del threading.currentThread().__decimal_context__
391+
392+
def setcontext(context):
393+
"""Set this thread's context to context."""
394+
if context in (DefaultContext, BasicContext, ExtendedContext):
395+
context = context.copy()
399396
threading.currentThread().__decimal_context__ = context
400-
return context
397+
398+
def getcontext():
399+
"""Returns this thread's context.
400+
401+
If this thread does not yet have a context, returns
402+
a new context and sets this thread's context.
403+
New contexts are copies of DefaultContext.
404+
"""
405+
try:
406+
return threading.currentThread().__decimal_context__
407+
except AttributeError:
408+
context = Context()
409+
threading.currentThread().__decimal_context__ = context
410+
return context
411+
412+
else:
413+
414+
local = threading.local()
415+
416+
def getcontext(_local=local):
417+
"""Returns this thread's context.
418+
419+
If this thread does not yet have a context, returns
420+
a new context and sets this thread's context.
421+
New contexts are copies of DefaultContext.
422+
"""
423+
try:
424+
return _local.__decimal_context__
425+
except AttributeError:
426+
context = Context()
427+
_local.__decimal_context__ = context
428+
return context
429+
430+
def setcontext(context, _local=local):
431+
"""Set this thread's context to context."""
432+
if context in (DefaultContext, BasicContext, ExtendedContext):
433+
context = context.copy()
434+
_local.__decimal_context__ = context
435+
436+
del threading, local # Don't contaminate the namespace
401437

402438

403439
##### Decimal class ###########################################

0 commit comments

Comments
 (0)