--- decimal.py.orig 2008-04-01 23:12:12.000000000 -0300 +++ decimal.py 2008-04-01 23:42:28.000000000 -0300 @@ -808,24 +808,21 @@ def __eq__(self, other): other = _convert_other(other) - if other is NotImplemented: - return other + if self.is_nan() or other.is_nan(): return False return self._cmp(other) == 0 def __ne__(self, other): other = _convert_other(other) - if other is NotImplemented: - return other + if self.is_nan() or other.is_nan(): return True return self._cmp(other) != 0 def __lt__(self, other, context=None): other = _convert_other(other) - if other is NotImplemented: - return other + ans = self._compare_check_nans(other, context) if ans: return False @@ -833,8 +830,7 @@ def __le__(self, other, context=None): other = _convert_other(other) - if other is NotImplemented: - return other + ans = self._compare_check_nans(other, context) if ans: return False @@ -842,8 +838,7 @@ def __gt__(self, other, context=None): other = _convert_other(other) - if other is NotImplemented: - return other + ans = self._compare_check_nans(other, context) if ans: return False @@ -851,8 +846,7 @@ def __ge__(self, other, context=None): other = _convert_other(other) - if other is NotImplemented: - return other + ans = self._compare_check_nans(other, context) if ans: return False @@ -867,7 +861,7 @@ NaN => one is NaN Like __cmp__, but returns Decimal instances. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) # Compare(NaN, NaN) = NaN if (self._is_special or other and other._is_special): @@ -1049,8 +1043,7 @@ -INF + INF (or the reverse) cause InvalidOperation errors. """ other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -1134,8 +1127,7 @@ 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) @@ -1148,8 +1140,7 @@ def __rsub__(self, other, context=None): """Return other - self""" other = _convert_other(other) - if other is NotImplemented: - return other + return other.__sub__(self, context=context) @@ -1159,8 +1150,7 @@ (+-) INF * 0 (or its reverse) raise InvalidOperation. """ other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -1213,8 +1203,7 @@ def __div__(self, other, context=None): """Return self / other.""" other = _convert_other(other) - if other is NotImplemented: - return NotImplemented + if context is None: context = getcontext() @@ -1307,8 +1296,7 @@ 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__ @@ -1317,8 +1305,7 @@ Return (self // other, self % other) """ other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -1351,8 +1338,7 @@ 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): @@ -1360,8 +1346,7 @@ self % other """ other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -1385,8 +1370,7 @@ 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): @@ -1396,7 +1380,7 @@ if context is None: context = getcontext() - other = _convert_other(other, raiseit=True) + other = _convert_other(other) ans = self._check_nans(other, context) if ans: @@ -1467,8 +1451,7 @@ def __floordiv__(self, other, context=None): """self // other""" other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -1495,8 +1478,7 @@ 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): @@ -1717,7 +1699,7 @@ and a single final rounding is performed. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) # compute product; raise InvalidOperation if either operand is # a signaling NaN or if the product is zero times infinity. @@ -1756,7 +1738,7 @@ # if can't convert other and modulo to Decimal, raise # TypeError; there's no point returning NotImplemented (no # equivalent of __rpow__ for three argument pow) - other = _convert_other(other, raiseit=True) + other = _convert_other(other) modulo = _convert_other(modulo, raiseit=True) if context is None: @@ -2075,8 +2057,7 @@ return self._power_modulo(other, modulo, context) other = _convert_other(other) - if other is NotImplemented: - return other + if context is None: context = getcontext() @@ -2232,8 +2213,7 @@ 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): @@ -2340,7 +2320,7 @@ * return True if both operands are NaNs * otherwise, return False. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if self._is_special or other._is_special: return (self.is_nan() and other.is_nan() or self.is_infinite() and other.is_infinite()) @@ -2556,7 +2536,7 @@ Like max(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if context is None: context = getcontext() @@ -2598,7 +2578,7 @@ Like min(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if context is None: context = getcontext() @@ -3156,7 +3136,7 @@ def max_mag(self, other, context=None): """Compares the values numerically with their sign ignored.""" - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if context is None: context = getcontext() @@ -3186,7 +3166,7 @@ def min_mag(self, other, context=None): """Compares the values numerically with their sign ignored.""" - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if context is None: context = getcontext() @@ -3269,7 +3249,7 @@ numerically equal, then the result is a copy of self with the sign set to be the same as the sign of other. """ - other = _convert_other(other, raiseit=True) + other = _convert_other(other) if context is None: context = getcontext() @@ -5278,7 +5258,7 @@ ##### Helper Functions #################################################### -def _convert_other(other, raiseit=False): +def _convert_other(other, raiseit=True): """Convert other to Decimal. Verifies that it's ok to use in an implicit construction.