From 61992efc4bb413ae7a19752215eca5af09be6b6d Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 6 Aug 2004 23:42:16 +0000 Subject: [PATCH] SF bug #1002530: test_decimal fails if repeated * Protect the pre-defined contexts by using a deepcopy() instead of copy(). * Micro-optimization: prefer x&1 over x%2 --- Lib/decimal.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/decimal.py b/Lib/decimal.py index 717c6b7f3ba..5216ac4cfe1 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -392,7 +392,8 @@ except AttributeError: def setcontext(context): """Set this thread's context to context.""" if context in (DefaultContext, BasicContext, ExtendedContext): - context = context.copy() + context = copy.deepcopy(context) + context.clear_flags() threading.currentThread().__decimal_context__ = context def getcontext(): @@ -430,7 +431,8 @@ else: def setcontext(context, _local=local): """Set this thread's context to context.""" if context in (DefaultContext, BasicContext, ExtendedContext): - context = context.copy() + context = copy.deepcopy(context) + context.clear_flags() _local.__decimal_context__ = context del threading, local # Don't contaminate the namespace @@ -1634,7 +1636,7 @@ class Decimal(object): half = 0 break if half: - if self._int[prec-1] %2 == 0: + if self._int[prec-1] & 1 == 0: return tmp return self._round_half_up(prec, expdiff, context, tmp) @@ -1930,7 +1932,7 @@ class Decimal(object): tmp = Decimal(self) expadd = tmp._exp / 2 - if tmp._exp % 2 == 1: + if tmp._exp & 1: tmp._int += (0,) tmp._exp = 0 else: @@ -1940,7 +1942,7 @@ class Decimal(object): flags = context._ignore_all_flags() firstprec = context.prec context.prec = 3 - if tmp.adjusted() % 2 == 0: + if tmp.adjusted() & 1 == 0: ans = Decimal( (0, (8,1,9), tmp.adjusted() - 2) ) ans = ans.__add__(tmp.__mul__(Decimal((0, (2,5,9), -2)), context=context), context=context) @@ -2075,7 +2077,7 @@ class Decimal(object): """Returns 1 if self is even. Assumes self is an integer.""" if self._exp > 0: return 1 - return self._int[-1+self._exp] % 2 == 0 + return self._int[-1+self._exp] & 1 == 0 def adjusted(self): """Return the adjusted exponent of self"""