summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-08-06 23:42:16 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-08-06 23:42:16 (GMT)
commit61992efc4bb413ae7a19752215eca5af09be6b6d (patch)
tree1f2d74d26d74767bd1211cea11bfdb46179162c1
parent19397e5ec549e5281b33aa5795b517006c00f64d (diff)
downloadcpython-61992efc4bb413ae7a19752215eca5af09be6b6d.zip
cpython-61992efc4bb413ae7a19752215eca5af09be6b6d.tar.gz
cpython-61992efc4bb413ae7a19752215eca5af09be6b6d.tar.bz2
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
-rw-r--r--Lib/decimal.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 717c6b7..5216ac4 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"""