summaryrefslogtreecommitdiffstats
path: root/Lib/_pydecimal.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-01-27 18:46:46 (GMT)
committerGitHub <noreply@github.com>2018-01-27 18:46:46 (GMT)
commitf13f12d8daa587b5fcc66fe3ed1090a5dadab289 (patch)
tree50f8217d1fcbcdf447e2a21b9e903cb6824ac8af /Lib/_pydecimal.py
parentbc4123b0b380edda774b8bff2fa1bcc96453b440 (diff)
downloadcpython-f13f12d8daa587b5fcc66fe3ed1090a5dadab289.zip
cpython-f13f12d8daa587b5fcc66fe3ed1090a5dadab289.tar.gz
cpython-f13f12d8daa587b5fcc66fe3ed1090a5dadab289.tar.bz2
bpo-32630: Use contextvars in decimal (GH-5278)
Diffstat (limited to 'Lib/_pydecimal.py')
-rw-r--r--Lib/_pydecimal.py20
1 files changed, 9 insertions, 11 deletions
diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py
index a1662bb..3596900 100644
--- a/Lib/_pydecimal.py
+++ b/Lib/_pydecimal.py
@@ -433,13 +433,11 @@ _rounding_modes = (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_CEILING,
# The getcontext() and setcontext() function manage access to a thread-local
# current context.
-import threading
+import contextvars
-local = threading.local()
-if hasattr(local, '__decimal_context__'):
- del local.__decimal_context__
+_current_context_var = contextvars.ContextVar('decimal_context')
-def getcontext(_local=local):
+def getcontext():
"""Returns this thread's context.
If this thread does not yet have a context, returns
@@ -447,20 +445,20 @@ def getcontext(_local=local):
New contexts are copies of DefaultContext.
"""
try:
- return _local.__decimal_context__
- except AttributeError:
+ return _current_context_var.get()
+ except LookupError:
context = Context()
- _local.__decimal_context__ = context
+ _current_context_var.set(context)
return context
-def setcontext(context, _local=local):
+def setcontext(context):
"""Set this thread's context to context."""
if context in (DefaultContext, BasicContext, ExtendedContext):
context = context.copy()
context.clear_flags()
- _local.__decimal_context__ = context
+ _current_context_var.set(context)
-del threading, local # Don't contaminate the namespace
+del contextvars # Don't contaminate the namespace
def localcontext(ctx=None):
"""Return a context manager for a copy of the supplied context