MCPcopy Index your code
hub / github.com/RustPython/RustPython / _convert_for_comparison

Function _convert_for_comparison

Lib/_pydecimal.py:6010–6044  ·  view source on GitHub ↗

Given a Decimal instance self and a Python object other, return a pair (s, o) of Decimal instances such that "s op o" is equivalent to "self op other" for any of the 6 comparison operators "op".

(self, other, equality_op=False)

Source from the content-addressed store, hash-verified

6008 return NotImplemented
6009
6010def _convert_for_comparison(self, other, equality_op=False):
6011 """Given a Decimal instance self and a Python object other, return
6012 a pair (s, o) of Decimal instances such that "s op o" is
6013 equivalent to "self op other" for any of the 6 comparison
6014 operators "op".
6015
6016 """
6017 if isinstance(other, Decimal):
6018 return self, other
6019
6020 # Comparison with a Rational instance (also includes integers):
6021 # self op n/d <=> self*d op n (for n and d integers, d positive).
6022 # A NaN or infinity can be left unchanged without affecting the
6023 # comparison result.
6024 if isinstance(other, _numbers.Rational):
6025 if not self._is_special:
6026 self = _dec_from_triple(self._sign,
6027 str(int(self._int) * other.denominator),
6028 self._exp)
6029 return self, Decimal(other.numerator)
6030
6031 # Comparisons with float and complex types. == and != comparisons
6032 # with complex numbers should succeed, returning either True or False
6033 # as appropriate. Other comparisons return NotImplemented.
6034 if equality_op and isinstance(other, _numbers.Complex) and other.imag == 0:
6035 other = other.real
6036 if isinstance(other, float):
6037 context = getcontext()
6038 if equality_op:
6039 context.flags[FloatOperation] = 1
6040 else:
6041 context._raise_error(FloatOperation,
6042 "strict semantics for mixing floats and Decimals are enabled")
6043 return self, Decimal.from_float(other)
6044 return NotImplemented, NotImplemented
6045
6046
6047##### Setup Specific Contexts ############################################

Callers 5

__eq__Method · 0.85
__lt__Method · 0.85
__le__Method · 0.85
__gt__Method · 0.85
__ge__Method · 0.85

Calls 7

isinstanceFunction · 0.85
_dec_from_tripleFunction · 0.85
strFunction · 0.85
DecimalClass · 0.85
getcontextFunction · 0.85
_raise_errorMethod · 0.80
from_floatMethod · 0.45

Tested by

no test coverage detected