=== modified file 'Lib/decimal.py' --- Lib/decimal.py 2009-03-18 08:22:51 +0000 +++ Lib/decimal.py 2009-03-21 17:59:42 +0000 @@ -859,7 +859,7 @@ # that specified by IEEE 754. def __eq__(self, other): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other if self.is_nan() or other.is_nan(): @@ -867,7 +867,7 @@ return self._cmp(other) == 0 def __ne__(self, other): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other if self.is_nan() or other.is_nan(): @@ -875,7 +875,7 @@ return self._cmp(other) != 0 def __lt__(self, other, context=None): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) @@ -884,7 +884,7 @@ return self._cmp(other) < 0 def __le__(self, other, context=None): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) @@ -893,7 +893,7 @@ return self._cmp(other) <= 0 def __gt__(self, other, context=None): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) @@ -902,7 +902,7 @@ return self._cmp(other) > 0 def __ge__(self, other, context=None): - other = _convert_other(other) + other = _relaxed_convert_other(other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) @@ -5357,6 +5357,23 @@ raise TypeError("Unable to convert %s to Decimal" % other) return NotImplemented +def _relaxed_convert_other(other, raiseit=False): + """Convert other to Decimal. Identical to _convert_other, except that + it also allows floats. This function is used by the comparison + magic methods (Decimal.__le__ and friends), to avoid getting + misleading results from float-to-Decimal comparisons. + + """ + if isinstance(other, Decimal): + return other + if isinstance(other, (int, long)): + return Decimal(other) + if isinstance(other, float): + return Decimal.from_float(other) + if raiseit: + raise TypeError("Unable to convert %s to Decimal" % other) + return NotImplemented + ##### Setup Specific Contexts ############################################ # The default context prototype used by Context() === modified file 'Lib/test/test_decimal.py' --- Lib/test/test_decimal.py 2009-03-18 08:22:51 +0000 +++ Lib/test/test_decimal.py 2009-03-21 18:08:25 +0000 @@ -1162,7 +1162,6 @@ #a Decimal and uncomparable self.assertNotEqual(da, 'ugly') - self.assertNotEqual(da, 32.7) self.assertNotEqual(da, object()) self.assertNotEqual(da, object) @@ -1177,6 +1176,23 @@ self.assertFalse(Decimal(1) < None) self.assertTrue(Decimal(1) > None) + def test_decimal_float_comparison(self): + da = Decimal('0.25') + db = Decimal('3.0') + self.assert_(da < 3.0) + self.assert_(da <= 3.0) + self.assert_(db > 0.25) + self.assert_(db >= 0.25) + self.assert_(da != 1.5) + self.assert_(da == 0.25) + self.assert_(3.0 > da) + self.assert_(3.0 >= da) + self.assert_(0.25 < db) + self.assert_(0.25 <= db) + self.assert_(0.25 != db) + self.assert_(3.0 == db) + self.assert_(0.1 != Decimal('0.1')) + def test_copy_and_deepcopy_methods(self): d = Decimal('43.24') c = copy.copy(d)