mirror of
https://github.com/python/cpython.git
synced 2025-08-28 20:56:54 +00:00
* Context.copy() now makes a deepcopy.
* Facilitate reloads of local thread.
This commit is contained in:
parent
161c963276
commit
9fce44bc8c
1 changed files with 21 additions and 12 deletions
|
@ -392,7 +392,7 @@ except AttributeError:
|
||||||
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 = copy.deepcopy(context)
|
context = context.copy()
|
||||||
context.clear_flags()
|
context.clear_flags()
|
||||||
threading.currentThread().__decimal_context__ = context
|
threading.currentThread().__decimal_context__ = context
|
||||||
|
|
||||||
|
@ -413,6 +413,8 @@ except AttributeError:
|
||||||
else:
|
else:
|
||||||
|
|
||||||
local = threading.local()
|
local = threading.local()
|
||||||
|
if hasattr(local, '__decimal_context__'):
|
||||||
|
del local.__decimal_context__
|
||||||
|
|
||||||
def getcontext(_local=local):
|
def getcontext(_local=local):
|
||||||
"""Returns this thread's context.
|
"""Returns this thread's context.
|
||||||
|
@ -431,7 +433,7 @@ else:
|
||||||
def setcontext(context, _local=local):
|
def setcontext(context, _local=local):
|
||||||
"""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 = copy.deepcopy(context)
|
context = context.copy()
|
||||||
context.clear_flags()
|
context.clear_flags()
|
||||||
_local.__decimal_context__ = context
|
_local.__decimal_context__ = context
|
||||||
|
|
||||||
|
@ -642,7 +644,7 @@ class Decimal(object):
|
||||||
elif self.adjusted < other.adjusted() and other._int[0] != 0:
|
elif self.adjusted < other.adjusted() and other._int[0] != 0:
|
||||||
return -((-1)**self._sign)
|
return -((-1)**self._sign)
|
||||||
|
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
rounding = context._set_rounding(ROUND_UP) #round away from 0
|
rounding = context._set_rounding(ROUND_UP) #round away from 0
|
||||||
|
|
||||||
flags = context._ignore_all_flags()
|
flags = context._ignore_all_flags()
|
||||||
|
@ -861,7 +863,7 @@ class Decimal(object):
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
if not round:
|
if not round:
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
context._set_rounding_decision(NEVER_ROUND)
|
context._set_rounding_decision(NEVER_ROUND)
|
||||||
|
|
||||||
if self._sign:
|
if self._sign:
|
||||||
|
@ -1358,7 +1360,7 @@ class Decimal(object):
|
||||||
|
|
||||||
# If DivisionImpossible causes an error, do not leave Rounded/Inexact
|
# If DivisionImpossible causes an error, do not leave Rounded/Inexact
|
||||||
# ignored in the calling function.
|
# ignored in the calling function.
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
flags = context._ignore_flags(Rounded, Inexact)
|
flags = context._ignore_flags(Rounded, Inexact)
|
||||||
#keep DivisionImpossible flags
|
#keep DivisionImpossible flags
|
||||||
(side, r) = self.__divmod__(other, context=context)
|
(side, r) = self.__divmod__(other, context=context)
|
||||||
|
@ -1367,7 +1369,7 @@ class Decimal(object):
|
||||||
context._regard_flags(*flags)
|
context._regard_flags(*flags)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
rounding = context._set_rounding_decision(NEVER_ROUND)
|
rounding = context._set_rounding_decision(NEVER_ROUND)
|
||||||
|
|
||||||
if other._sign:
|
if other._sign:
|
||||||
|
@ -1600,7 +1602,7 @@ class Decimal(object):
|
||||||
#Now we've got the rounding function
|
#Now we've got the rounding function
|
||||||
|
|
||||||
if prec != context.prec:
|
if prec != context.prec:
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
context.prec = prec
|
context.prec = prec
|
||||||
ans = this_function(prec, expdiff, context)
|
ans = this_function(prec, expdiff, context)
|
||||||
context._raise_error(Rounded)
|
context._raise_error(Rounded)
|
||||||
|
@ -1738,7 +1740,7 @@ class Decimal(object):
|
||||||
|
|
||||||
mul = Decimal(self)
|
mul = Decimal(self)
|
||||||
val = Decimal(1)
|
val = Decimal(1)
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
context.prec = firstprec + elength + 1
|
context.prec = firstprec + elength + 1
|
||||||
rounding = context.rounding
|
rounding = context.rounding
|
||||||
if n < 0:
|
if n < 0:
|
||||||
|
@ -1938,7 +1940,7 @@ class Decimal(object):
|
||||||
else:
|
else:
|
||||||
tmp._exp = 0
|
tmp._exp = 0
|
||||||
|
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
flags = context._ignore_all_flags()
|
flags = context._ignore_all_flags()
|
||||||
firstprec = context.prec
|
firstprec = context.prec
|
||||||
context.prec = 3
|
context.prec = 3
|
||||||
|
@ -2166,12 +2168,19 @@ class Context(object):
|
||||||
for flag in self.flags:
|
for flag in self.flags:
|
||||||
self.flags[flag] = 0
|
self.flags[flag] = 0
|
||||||
|
|
||||||
def copy(self):
|
def _shallow_copy(self):
|
||||||
"""Returns a copy from self."""
|
"""Returns a shallow copy from self."""
|
||||||
nc = Context(self.prec, self.rounding, self.traps, self.flags,
|
nc = Context(self.prec, self.rounding, self.traps, self.flags,
|
||||||
self._rounding_decision, self.Emin, self.Emax,
|
self._rounding_decision, self.Emin, self.Emax,
|
||||||
self.capitals, self._clamp, self._ignored_flags)
|
self.capitals, self._clamp, self._ignored_flags)
|
||||||
return nc
|
return nc
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
"""Returns a deep copy from self."""
|
||||||
|
nc = Context(self.prec, self.rounding, self.traps.copy(), self.flags.copy(),
|
||||||
|
self._rounding_decision, self.Emin, self.Emax,
|
||||||
|
self.capitals, self._clamp, self._ignored_flags)
|
||||||
|
return nc
|
||||||
__copy__ = copy
|
__copy__ = copy
|
||||||
|
|
||||||
def _raise_error(self, condition, explanation = None, *args):
|
def _raise_error(self, condition, explanation = None, *args):
|
||||||
|
@ -2233,7 +2242,7 @@ class Context(object):
|
||||||
Sets the rounding decision, and returns the current (previous)
|
Sets the rounding decision, and returns the current (previous)
|
||||||
rounding decision. Often used like:
|
rounding decision. Often used like:
|
||||||
|
|
||||||
context = context.copy()
|
context = context._shallow_copy()
|
||||||
# That so you don't change the calling context
|
# That so you don't change the calling context
|
||||||
# if an error occurs in the middle (say DivisionImpossible is raised).
|
# if an error occurs in the middle (say DivisionImpossible is raised).
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue