@@ -862,15 +862,15 @@ def _cmp(self, other):
862862 # that specified by IEEE 754.
863863
864864 def __eq__ (self , other , context = None ):
865- other = _convert_other ( other , allow_float = True )
865+ self , other = _convert_for_comparison ( self , other , equality_op = True )
866866 if other is NotImplemented :
867867 return other
868868 if self ._check_nans (other , context ):
869869 return False
870870 return self ._cmp (other ) == 0
871871
872872 def __ne__ (self , other , context = None ):
873- other = _convert_other ( other , allow_float = True )
873+ self , other = _convert_for_comparison ( self , other , equality_op = True )
874874 if other is NotImplemented :
875875 return other
876876 if self ._check_nans (other , context ):
@@ -879,7 +879,7 @@ def __ne__(self, other, context=None):
879879
880880
881881 def __lt__ (self , other , context = None ):
882- other = _convert_other ( other , allow_float = True )
882+ self , other = _convert_for_comparison ( self , other )
883883 if other is NotImplemented :
884884 return other
885885 ans = self ._compare_check_nans (other , context )
@@ -888,7 +888,7 @@ def __lt__(self, other, context=None):
888888 return self ._cmp (other ) < 0
889889
890890 def __le__ (self , other , context = None ):
891- other = _convert_other ( other , allow_float = True )
891+ self , other = _convert_for_comparison ( self , other )
892892 if other is NotImplemented :
893893 return other
894894 ans = self ._compare_check_nans (other , context )
@@ -897,7 +897,7 @@ def __le__(self, other, context=None):
897897 return self ._cmp (other ) <= 0
898898
899899 def __gt__ (self , other , context = None ):
900- other = _convert_other ( other , allow_float = True )
900+ self , other = _convert_for_comparison ( self , other )
901901 if other is NotImplemented :
902902 return other
903903 ans = self ._compare_check_nans (other , context )
@@ -906,7 +906,7 @@ def __gt__(self, other, context=None):
906906 return self ._cmp (other ) > 0
907907
908908 def __ge__ (self , other , context = None ):
909- other = _convert_other ( other , allow_float = True )
909+ self , other = _convert_for_comparison ( self , other )
910910 if other is NotImplemented :
911911 return other
912912 ans = self ._compare_check_nans (other , context )
@@ -5860,6 +5860,37 @@ def _convert_other(other, raiseit=False, allow_float=False):
58605860 raise TypeError ("Unable to convert %s to Decimal" % other )
58615861 return NotImplemented
58625862
5863+ def _convert_for_comparison (self , other , equality_op = False ):
5864+ """Given a Decimal instance self and a Python object other, return
5865+ an pair (s, o) of Decimal instances such that "s op o" is
5866+ equivalent to "self op other" for any of the 6 comparison
5867+ operators "op".
5868+
5869+ """
5870+ if isinstance (other , Decimal ):
5871+ return self , other
5872+
5873+ # Comparison with a Rational instance (also includes integers):
5874+ # self op n/d <=> self*d op n (for n and d integers, d positive).
5875+ # A NaN or infinity can be left unchanged without affecting the
5876+ # comparison result.
5877+ if isinstance (other , _numbers .Rational ):
5878+ if not self ._is_special :
5879+ self = _dec_from_triple (self ._sign ,
5880+ str (int (self ._int ) * other .denominator ),
5881+ self ._exp )
5882+ return self , Decimal (other .numerator )
5883+
5884+ # Comparisons with float and complex types. == and != comparisons
5885+ # with complex numbers should succeed, returning either True or False
5886+ # as appropriate. Other comparisons return NotImplemented.
5887+ if equality_op and isinstance (other , _numbers .Complex ) and other .imag == 0 :
5888+ other = other .real
5889+ if isinstance (other , float ):
5890+ return self , Decimal .from_float (other )
5891+ return NotImplemented , NotImplemented
5892+
5893+
58635894##### Setup Specific Contexts ############################################
58645895
58655896# The default context prototype used by Context()
0 commit comments