MCPcopy Index your code
hub / github.com/python/cpython / _convert_for_comparison

Function _convert_for_comparison

Lib/_pydecimal.py:6013–6047  ·  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

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

_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

Used in the wild real call sites across dependent graphs

searching dependent graphs…