mirror of
https://github.com/python/cpython.git
synced 2025-08-19 16:20:59 +00:00
Backport of decimal module context management updates from rev 51694 to 2.5 release branch
This commit is contained in:
parent
f07b590d7e
commit
c48daf5bc4
5 changed files with 107 additions and 73 deletions
|
@ -131,7 +131,7 @@ __all__ = [
|
|||
'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN',
|
||||
|
||||
# Functions for manipulating contexts
|
||||
'setcontext', 'getcontext'
|
||||
'setcontext', 'getcontext', 'localcontext'
|
||||
]
|
||||
|
||||
import copy as _copy
|
||||
|
@ -458,6 +458,49 @@ else:
|
|||
|
||||
del threading, local # Don't contaminate the namespace
|
||||
|
||||
def localcontext(ctx=None):
|
||||
"""Return a context manager for a copy of the supplied context
|
||||
|
||||
Uses a copy of the current context if no context is specified
|
||||
The returned context manager creates a local decimal context
|
||||
in a with statement:
|
||||
def sin(x):
|
||||
with localcontext() as ctx:
|
||||
ctx.prec += 2
|
||||
# Rest of sin calculation algorithm
|
||||
# uses a precision 2 greater than normal
|
||||
return +s # Convert result to normal precision
|
||||
|
||||
def sin(x):
|
||||
with localcontext(ExtendedContext):
|
||||
# Rest of sin calculation algorithm
|
||||
# uses the Extended Context from the
|
||||
# General Decimal Arithmetic Specification
|
||||
return +s # Convert result to normal context
|
||||
|
||||
"""
|
||||
# The below can't be included in the docstring until Python 2.6
|
||||
# as the doctest module doesn't understand __future__ statements
|
||||
"""
|
||||
>>> from __future__ import with_statement
|
||||
>>> print getcontext().prec
|
||||
28
|
||||
>>> with localcontext():
|
||||
... ctx = getcontext()
|
||||
... ctx.prec() += 2
|
||||
... print ctx.prec
|
||||
...
|
||||
30
|
||||
>>> with localcontext(ExtendedContext):
|
||||
... print getcontext().prec
|
||||
...
|
||||
9
|
||||
>>> print getcontext().prec
|
||||
28
|
||||
"""
|
||||
if ctx is None: ctx = getcontext()
|
||||
return _ContextManager(ctx)
|
||||
|
||||
|
||||
##### Decimal class ###########################################
|
||||
|
||||
|
@ -2173,23 +2216,14 @@ for name in rounding_functions:
|
|||
|
||||
del name, val, globalname, rounding_functions
|
||||
|
||||
class ContextManager(object):
|
||||
"""Helper class to simplify Context management.
|
||||
|
||||
Sample usage:
|
||||
|
||||
with decimal.ExtendedContext:
|
||||
s = ...
|
||||
return +s # Convert result to normal precision
|
||||
|
||||
with decimal.getcontext() as ctx:
|
||||
ctx.prec += 2
|
||||
s = ...
|
||||
return +s
|
||||
class _ContextManager(object):
|
||||
"""Context manager class to support localcontext().
|
||||
|
||||
Sets a copy of the supplied context in __enter__() and restores
|
||||
the previous decimal context in __exit__()
|
||||
"""
|
||||
def __init__(self, new_context):
|
||||
self.new_context = new_context
|
||||
self.new_context = new_context.copy()
|
||||
def __enter__(self):
|
||||
self.saved_context = getcontext()
|
||||
setcontext(self.new_context)
|
||||
|
@ -2248,9 +2282,6 @@ class Context(object):
|
|||
s.append('traps=[' + ', '.join([t.__name__ for t, v in self.traps.items() if v]) + ']')
|
||||
return ', '.join(s) + ')'
|
||||
|
||||
def get_manager(self):
|
||||
return ContextManager(self.copy())
|
||||
|
||||
def clear_flags(self):
|
||||
"""Reset all flags to zero"""
|
||||
for flag in self.flags:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue