WIP: use assign expr on if by vstinner · Pull Request #8116 · python/cpython (original) (raw)
@@ -886,35 +886,31 @@ def __lt__(self, other, context=None):
self, other = _convert_for_comparison(self, other)
if other is NotImplemented:
return other
ans = self._compare_check_nans(other, context)
if ans:
if self._compare_check_nans(other, context):
return False
return self._cmp(other) < 0
def __le__(self, other, context=None):
self, other = _convert_for_comparison(self, other)
if other is NotImplemented:
return other
ans = self._compare_check_nans(other, context)
if ans:
if self._compare_check_nans(other, context):
return False
return self._cmp(other) <= 0
def __gt__(self, other, context=None):
self, other = _convert_for_comparison(self, other)
if other is NotImplemented:
return other
ans = self._compare_check_nans(other, context)
if ans:
if self._compare_check_nans(other, context):
return False
return self._cmp(other) > 0
def __ge__(self, other, context=None):
self, other = _convert_for_comparison(self, other)
if other is NotImplemented:
return other
ans = self._compare_check_nans(other, context)
if ans:
if self._compare_check_nans(other, context):
return False
return self._cmp(other) >= 0
@@ -930,8 +926,7 @@ def compare(self, other, context=None):
# Compare(NaN, NaN) = NaN
if (self._is_special or other and other._is_special):
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
return Decimal(self._cmp(other))
@@ -1090,10 +1085,8 @@ def __neg__(self, context=None):
Rounds, if it has reason.
"""
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
if self._is_special and (ans := self._check_nans(context=context)):
return ans
if context is None:
context = getcontext()
@@ -1112,10 +1105,8 @@ def __pos__(self, context=None):
Rounds the number (if more than precision digits)
"""
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
if self._is_special and (ans := self._check_nans(context=context)):
return ans
if context is None:
context = getcontext()
@@ -1138,10 +1129,8 @@ def __abs__(self, round=True, context=None):
if not round:
return self.copy_abs()
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
if self._is_special and (ans := self._check_nans(context=context)):
return ans
if self._sign:
ans = self.__neg__(context=context)
@@ -1163,8 +1152,7 @@ def __add__(self, other, context=None):
context = getcontext()
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if self._isinfinity():
@@ -1244,10 +1232,9 @@ def __sub__(self, other, context=None):
if other is NotImplemented:
return other
if self._is_special or other._is_special:
ans = self._check_nans(other, context=context)
if ans:
return ans
if (self._is_special or other._is_special
and (ans := self._check_nans(other, context=context))):
return ans
# self - other is computed as self + other.copy_negate()
return self.__add__(other.copy_negate(), context=context)
@@ -1275,8 +1262,7 @@ def __mul__(self, other, context=None):
resultsign = self._sign ^ other._sign
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if self._isinfinity():
@@ -1329,8 +1315,7 @@ def __truediv__(self, other, context=None):
sign = self._sign ^ other._sign
if self._is_special or other._is_special:
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if self._isinfinity() and other._isinfinity():
@@ -1427,8 +1412,7 @@ def __divmod__(self, other, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return (ans, ans)
sign = self._sign ^ other._sign
@@ -1470,8 +1454,7 @@ def __mod__(self, other, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if self._isinfinity():
@@ -1502,8 +1485,7 @@ def remainder_near(self, other, context=None):
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
# self == +/-infinity -> InvalidOperation
@@ -1577,8 +1559,7 @@ def __floordiv__(self, other, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if self._isinfinity():
@@ -2316,8 +2297,7 @@ def __pow__(self, other, modulo=None, context=None):
context = getcontext()
# either argument is a NaN => result is NaN
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
# 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity)
@@ -2511,8 +2491,7 @@ def normalize(self, context=None):
context = getcontext()
if self._is_special:
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
dup = self._fix(context)
@@ -2542,8 +2521,7 @@ def quantize(self, exp, rounding=None, context=None):
rounding = context.rounding
if self._is_special or exp._is_special:
ans = self._check_nans(exp, context)
if ans:
if (ans := self._check_nans(exp, context)):
return ans
if exp._isinfinity() or self._isinfinity():
@@ -2673,8 +2651,7 @@ def to_integral_exact(self, rounding=None, context=None):
this method except that it doesn't raise Inexact or Rounded.
"""
if self._is_special:
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
return Decimal(self)
if self._exp >= 0:
@@ -2698,8 +2675,7 @@ def to_integral_value(self, rounding=None, context=None):
if rounding is None:
rounding = context.rounding
if self._is_special:
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
return Decimal(self)
if self._exp >= 0:
@@ -2716,8 +2692,7 @@ def sqrt(self, context=None):
context = getcontext()
if self._is_special:
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
if self._isinfinity() and self._sign == 0:
@@ -2923,8 +2898,7 @@ def compare_signal(self, other, context=None):
NaNs taking precedence over quiet NaNs.
"""
other = _convert_other(other, raiseit = True)
ans = self._compare_check_nans(other, context)
if ans:
if (ans := self._compare_check_nans(other, context)):
return ans
return self.compare(other, context=context)
@@ -3036,8 +3010,7 @@ def exp(self, context=None):
context = getcontext()
# exp(NaN) = NaN
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
# exp(-Infinity) = 0
@@ -3192,8 +3165,7 @@ def ln(self, context=None):
context = getcontext()
# ln(NaN) = NaN
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
# ln(0.0) == -Infinity
@@ -3272,8 +3244,7 @@ def log10(self, context=None):
context = getcontext()
# log10(NaN) = NaN
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
# log10(0.0) == -Infinity
@@ -3325,8 +3296,7 @@ def logb(self, context=None):
without limiting the resulting exponent).
"""
# logb(NaN) = NaN
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
if context is None:
@@ -3496,8 +3466,7 @@ def next_minus(self, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
if self._isinfinity() == -1:
@@ -3519,8 +3488,7 @@ def next_plus(self, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(context=context)
if ans:
if (ans := self._check_nans(context=context)):
return ans
if self._isinfinity() == 1:
@@ -3551,8 +3519,7 @@ def next_toward(self, other, context=None):
if context is None:
context = getcontext()
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
comparison = self._cmp(other)
@@ -3636,8 +3603,7 @@ def rotate(self, other, context=None):
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if other._exp != 0:
@@ -3669,8 +3635,7 @@ def scaleb(self, other, context=None):
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if other._exp != 0:
@@ -3694,8 +3659,7 @@ def shift(self, other, context=None):
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
if (ans := self._check_nans(other, context)):
return ans
if other._exp != 0: