summaryrefslogtreecommitdiffstats
path: root/Lib/decimal.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2005-03-27 10:47:39 (GMT)
committerRaymond Hettinger <python@rcn.com>2005-03-27 10:47:39 (GMT)
commit267b868f23a85c6a63a06452c85487355cf9ab8a (patch)
tree42ac2b7e1ff7c4aa6f2148756f178a5891820ab3 /Lib/decimal.py
parente6c470f255b958d65db75e2b9c2416585638b6cf (diff)
downloadcpython-267b868f23a85c6a63a06452c85487355cf9ab8a.zip
cpython-267b868f23a85c6a63a06452c85487355cf9ab8a.tar.gz
cpython-267b868f23a85c6a63a06452c85487355cf9ab8a.tar.bz2
* Fix decimal's handling of foreign types. Now returns NotImplemented
instead of raising a TypeError. Allows other types to successfully implement __radd__() style methods. * Remove future division import from test suite. * Remove test suite's shadowing of __builtin__.dir().
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r--Lib/decimal.py43
1 files changed, 39 insertions, 4 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index fb11e8f..e3e7fd5 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -645,6 +645,8 @@ class Decimal(object):
def __cmp__(self, other, context=None):
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
@@ -696,12 +698,12 @@ class Decimal(object):
def __eq__(self, other):
if not isinstance(other, (Decimal, int, long)):
- return False
+ return NotImplemented
return self.__cmp__(other) == 0
def __ne__(self, other):
if not isinstance(other, (Decimal, int, long)):
- return True
+ return NotImplemented
return self.__cmp__(other) != 0
def compare(self, other, context=None):
@@ -714,6 +716,8 @@ class Decimal(object):
Like __cmp__, but returns Decimal instances.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
#compare(NaN, NaN) = NaN
if (self._is_special or other and other._is_special):
@@ -919,6 +923,8 @@ class Decimal(object):
-INF + INF (or the reverse) cause InvalidOperation errors.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if context is None:
context = getcontext()
@@ -1006,6 +1012,8 @@ class Decimal(object):
def __sub__(self, other, context=None):
"""Return self + (-other)"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
ans = self._check_nans(other, context=context)
@@ -1023,6 +1031,8 @@ class Decimal(object):
def __rsub__(self, other, context=None):
"""Return other + (-self)"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
tmp = Decimal(self)
tmp._sign = 1 - tmp._sign
@@ -1068,6 +1078,8 @@ class Decimal(object):
(+-) INF * 0 (or its reverse) raise InvalidOperation.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if context is None:
context = getcontext()
@@ -1140,6 +1152,10 @@ class Decimal(object):
computing the other value are not raised.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ if divmod in (0, 1):
+ return NotImplemented
+ return (NotImplemented, NotImplemented)
if context is None:
context = getcontext()
@@ -1292,6 +1308,8 @@ class Decimal(object):
def __rdiv__(self, other, context=None):
"""Swaps self/other and returns __div__."""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
return other.__div__(self, context=context)
__rtruediv__ = __rdiv__
@@ -1304,6 +1322,8 @@ class Decimal(object):
def __rdivmod__(self, other, context=None):
"""Swaps self/other and returns __divmod__."""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
return other.__divmod__(self, context=context)
def __mod__(self, other, context=None):
@@ -1311,6 +1331,8 @@ class Decimal(object):
self % other
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
@@ -1325,6 +1347,8 @@ class Decimal(object):
def __rmod__(self, other, context=None):
"""Swaps self/other and returns __mod__."""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
return other.__mod__(self, context=context)
def remainder_near(self, other, context=None):
@@ -1332,6 +1356,8 @@ class Decimal(object):
Remainder nearest to 0- abs(remainder-near) <= other/2
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
@@ -1411,6 +1437,8 @@ class Decimal(object):
def __rfloordiv__(self, other, context=None):
"""Swaps self/other and returns __floordiv__."""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
return other.__floordiv__(self, context=context)
def __float__(self):
@@ -1661,6 +1689,8 @@ class Decimal(object):
If modulo is None (default), don't take it mod modulo.
"""
n = _convert_other(n)
+ if n is NotImplemented:
+ return n
if context is None:
context = getcontext()
@@ -1747,6 +1777,8 @@ class Decimal(object):
def __rpow__(self, other, context=None):
"""Swaps self/other and returns __pow__."""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
return other.__pow__(self, context=context)
def normalize(self, context=None):
@@ -2001,6 +2033,8 @@ class Decimal(object):
NaN (and signals if one is sNaN). Also rounds.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
# if one operand is a quiet NaN and the other is number, then the
@@ -2048,6 +2082,8 @@ class Decimal(object):
NaN (and signals if one is sNaN). Also rounds.
"""
other = _convert_other(other)
+ if other is NotImplemented:
+ return other
if self._is_special or other._is_special:
# if one operand is a quiet NaN and the other is number, then the
@@ -2874,8 +2910,7 @@ def _convert_other(other):
return other
if isinstance(other, (int, long)):
return Decimal(other)
-
- raise TypeError, "You can interact Decimal only with int, long or Decimal data types."
+ return NotImplemented
_infinity_map = {
'inf' : 1,