Skip to content

Commit

Permalink
Trac #21887: py3 making __nonzero__ an alias for __bool__ in rings fo…
Browse files Browse the repository at this point in the history
…lder

as a step to py3

part of #16076

here only in the rings folder

URL: https://trac.sagemath.org/21887
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Jeroen Demeyer
  • Loading branch information
Release Manager authored and vbraun committed Nov 19, 2016
2 parents 37f1110 + b279834 commit c1c596c
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 49 deletions.
3 changes: 2 additions & 1 deletion src/sage/rings/asymptotic/asymptotic_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def __hash__(self):
return hash(str(self))


def __nonzero__(self):
def __bool__(self):
r"""
Return whether this asymptotic expansion is not identically zero.
Expand All @@ -803,6 +803,7 @@ def __nonzero__(self):
"""
return bool(self._summands_)

__nonzero__ = __bool__

def __eq__(self, other):
r"""
Expand Down
12 changes: 5 additions & 7 deletions src/sage/rings/complex_interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1395,20 +1395,18 @@ cdef class ComplexIntervalFieldElement(sage.structure.element.FieldElement):
EXAMPLES::
sage: CIF(RIF(0, 0), RIF(0, 0)).__nonzero__()
False
sage: bool(CIF(RIF(0, 0), RIF(0, 0)))
False
sage: CIF(RIF(1), RIF(0)).__nonzero__()
sage: bool(CIF(RIF(1), RIF(0)))
True
sage: CIF(RIF(0), RIF(1)).__nonzero__()
sage: bool(CIF(RIF(0), RIF(1)))
True
sage: CIF(RIF(1, 2), RIF(0)).__nonzero__()
sage: bool(CIF(RIF(1, 2), RIF(0)))
True
sage: CIF(RIF(-1, 1), RIF(-1, 1)).__nonzero__()
sage: bool(CIF(RIF(-1, 1), RIF(-1, 1)))
True
"""
return self.real().__nonzero__() or self.imag().__nonzero__()
return bool(self.real()) or bool(self.imag())

cpdef _richcmp_(left, right, int op):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/continued_fraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def ceil(self):
return self.quotient(0)
return self.quotient(0)+1

def __nonzero__(self):
def __bool__(self):
"""
Return False if self is zero.
Expand All @@ -1003,6 +1003,8 @@ def __nonzero__(self):
"""
return bool(self.quotient(0)) or self.quotient(1) is not Infinity

__nonzero__ = __bool__

def is_zero(self):
r"""
Test whether ``self`` is zero.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/fraction_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,12 @@ cdef class FractionFieldElement(FieldElement):
sage: F = ZZ['x,y'].fraction_field()
sage: x,y = F.gens()
sage: t = F(0)/x
sage: t.__nonzero__()
sage: bool(t)
False
::
sage: (1/x).__nonzero__()
sage: bool(1/x)
True
"""
return not self.__numerator.is_zero()
Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/ideal.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _contains_(self, x):
"""
raise NotImplementedError

def __nonzero__(self):
def __bool__(self):
r"""
Return ``True`` if this ideal is not `(0)`.
Expand Down Expand Up @@ -419,6 +419,8 @@ def __nonzero__(self):
return True
return False

__nonzero__ = __bool__

def base_ring(self):
r"""
Returns the base ring of this ideal.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/multi_power_series_ring_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class MPowerSeries(PowerSeries):
#
# _mul_prec : works just fine
#
# __nonzero__ : works just fine
# __bool__ : works just fine
#
"""
Multivariate power series; these are the elements of Multivariate Power
Expand Down
8 changes: 3 additions & 5 deletions src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2272,17 +2272,15 @@ cdef class NumberFieldElement(FieldElement):
EXAMPLES::
sage: m.<b> = CyclotomicField(17)
sage: m(0).__nonzero__()
sage: bool(m(0))
False
sage: b.__nonzero__()
sage: bool(b)
True
Nonzero is used by the bool command::
``__bool__`` is used by the bool command::
sage: bool(b + 1)
True
sage: bool(m(0))
False
"""
return not IsZero_ZZX(self.__numerator)

Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/polynomial/multi_polynomial_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ def __ne__(self,right):
return CommutativeRingElement.__ne__(self, right)
return self._MPolynomial_element__element != right._MPolynomial_element__element

def __nonzero__(self):
def __bool__(self):
"""
Returns True if self != 0
Expand All @@ -1474,6 +1474,8 @@ def __nonzero__(self):
"""
return self._MPolynomial_element__element.dict()!={}

__nonzero__ = __bool__

def _floordiv_(self, right):
r"""
Quotient of division of self by other. This is denoted //.
Expand Down
3 changes: 2 additions & 1 deletion src/sage/rings/polynomial/polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8986,8 +8986,9 @@ cdef class Polynomial_generic_dense(Polynomial):
Check that exceptions are propagated correctly (:trac:`18274`)::
sage: class BrokenRational(Rational):
....: def __nonzero__(self):
....: def __bool__(self):
....: raise NotImplementedError("cannot check whether number is non-zero")
....: __nonzero__ = __bool__
sage: z = BrokenRational()
sage: R.<x> = QQ[]
sage: from sage.rings.polynomial.polynomial_element import Polynomial_generic_dense
Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,11 @@ cdef class Polynomial_integer_dense_flint(Polynomial):
EXAMPLES::
sage: R.<x> = ZZ[]
sage: R(0).__nonzero__()
sage: bool(R(0))
False
sage: R(1).__nonzero__()
sage: bool(R(1))
True
sage: x.__nonzero__()
sage: bool(x)
True
"""
return not (fmpz_poly_degree(self.__poly) == -1)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/polynomial/skew_polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1685,10 +1685,10 @@ cdef class SkewPolynomial(AlgebraElement):
sage: sigma = R.hom([t+1])
sage: S.<x> = R['x',sigma]
sage: a = x + 1
sage: a.__nonzero__()
sage: bool(a)
True
sage: b = S.zero()
sage: b.__nonzero__()
sage: bool(b)
False
"""
return not self.is_zero()
Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/power_series_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ cdef class PowerSeries_poly(PowerSeries):
EXAMPLES::
sage: R.<t> = GF(11)[[]]
sage: (1 + t + O(t^18)).__nonzero__()
sage: bool(1 + t + O(t^18))
True
sage: R(0).__nonzero__()
sage: bool(R(0))
False
sage: O(t^18).__nonzero__()
sage: bool(O(t^18))
False
"""
return not not self.__f
Expand Down
10 changes: 6 additions & 4 deletions src/sage/rings/qqbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3805,17 +3805,17 @@ def __ne__(self, other):
"""
return not self == other

def __nonzero__(self):
def __bool__(self):
"""
Check whether self is equal is nonzero. This is fast if
interval arithmetic proves that self is nonzero, but may be
slow if the number actually is very close to zero.
EXAMPLES::
sage: (QQbar.zeta(2) + 1).__nonzero__()
sage: bool(QQbar.zeta(2) + 1)
False
sage: (QQbar.zeta(7) / (2^500)).__nonzero__()
sage: bool(QQbar.zeta(7) / (2^500))
True
"""
val = self._value
Expand All @@ -3832,7 +3832,9 @@ def __nonzero__(self):

# Sigh...
self.exactify()
return self.__nonzero__()
return self.__bool__()

__nonzero__ = __bool__

def __pow__(self, e):
r""" ``self**p`` returns the `p`'th power of self (where `p` can
Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/quotient_ring_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def lift(self):
"""
return self.__rep

def __nonzero__(self):
def __bool__(self):
"""
Return True if quotient ring element is non-zero in the
quotient ring `R/I`, by determining whether the element
Expand All @@ -143,13 +143,13 @@ def __nonzero__(self):
TESTS::
sage: S(0).__nonzero__()
False
sage: (a-a).__nonzero__()
sage: bool(a - a)
False
"""
return self.__rep not in self.parent().defining_ideal()

__nonzero__ = __bool__

def is_unit(self):
"""
Return True if self is a unit in the quotient ring.
Expand Down
4 changes: 1 addition & 3 deletions src/sage/rings/rational.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2559,9 +2559,7 @@ cdef class Rational(sage.structure.element.FieldElement):

EXAMPLES::

sage: (-4/17).__nonzero__()
True
sage: (0/5).__nonzero__()
sage: bool(0/5)
False
sage: bool(-4/17)
True
Expand Down
10 changes: 5 additions & 5 deletions src/sage/rings/real_mpfi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3737,15 +3737,15 @@ cdef class RealIntervalFieldElement(RingElement):

EXAMPLES::

sage: RIF(0).__nonzero__()
sage: bool(RIF(0))
False
sage: RIF(1).__nonzero__()
sage: bool(RIF(1))
True
sage: RIF(1, 2).__nonzero__()
sage: bool(RIF(1, 2))
True
sage: RIF(0, 1).__nonzero__()
sage: bool(RIF(0, 1))
True
sage: RIF(-1, 1).__nonzero__()
sage: bool(RIF(-1, 1))
True
"""
return not (mpfr_zero_p(&self.value.left) and mpfr_zero_p(&self.value.right))
Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/real_mpfr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3790,18 +3790,18 @@ cdef class RealNumber(sage.structure.element.RingElement):

EXAMPLES::

sage: RR(1).__nonzero__()
sage: bool(RR(1))
True
sage: RR(0).__nonzero__()
sage: bool(RR(0))
False
sage: RR('inf').__nonzero__()
sage: bool(RR('inf'))
True

TESTS:

Check that :trac:`20502` is fixed::

sage: RR('nan').__nonzero__()
sage: bool(RR('nan'))
True
sage: RR('nan').is_zero()
False
Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/universal_cyclotomic_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def __init__(self, parent, obj):
self._obj = obj
FieldElement.__init__(self, parent)

def __nonzero__(self):
def __bool__(self):
r"""
TESTS::

Expand All @@ -285,6 +285,8 @@ def __nonzero__(self):
"""
return bool(self._obj)

__nonzero__ = __bool__

def __reduce__(self):
r"""
TESTS::
Expand Down

0 comments on commit c1c596c

Please sign in to comment.