Skip to content

Commit

Permalink
Trac #23886: py3: no more cmp between sets
Browse files Browse the repository at this point in the history
as a step to py3

URL: https://trac.sagemath.org/23886
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Sep 19, 2017
2 parents 027c878 + 5f849aa commit 5de3397
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/sage/sets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from sage.structure.category_object import CategoryObject
from sage.structure.element import Element
from sage.structure.parent import Parent, Set_generic
from sage.structure.richcmp import richcmp_method, richcmp, rich_to_bool

from sage.categories.sets_cat import Sets
from sage.categories.enumerated_sets import EnumeratedSets
Expand Down Expand Up @@ -225,6 +226,7 @@ def is_Set(x):
"""
return isinstance(x, Set_generic)

@richcmp_method
class Set_object(Set_generic):
r"""
A set attached to an almost arbitrary object.
Expand Down Expand Up @@ -391,13 +393,13 @@ def __contains__(self, x):
"""
return x in self.__object

def __cmp__(self, right):
def __richcmp__(self, right, op):
r"""
Compare ``self`` and ``right``.
If right is not a :class:`Set_object` compare types. If ``right`` is
also a :class:`Set_object`, returns comparison on the underlying
objects.
If ``right`` is not a :class:`Set_object`, return ``NotImplemented``.
If ``right`` is also a :class:`Set_object`, returns comparison
on the underlying objects.
.. NOTE::
Expand All @@ -422,8 +424,8 @@ def __cmp__(self, right):
True or False
"""
if not isinstance(right, Set_object):
return cmp(type(self), type(right))
return cmp(self.__object, right.__object)
return NotImplemented
return richcmp(self.__object, right.__object, op)

def union(self, X):
"""
Expand Down Expand Up @@ -921,7 +923,7 @@ def __hash__(self):
"""
return hash(self.frozenset())

def __cmp__(self, other):
def __richcmp__(self, other, op):
"""
Compare the sets ``self`` and ``other``.
Expand All @@ -935,12 +937,11 @@ def __cmp__(self, other):
sage: Set(QQ) == Set(ZZ)
False
"""
if isinstance(other, Set_object_enumerated):
if self.set() == other.set():
return 0
return -1
else:
return Set_object.__cmp__(self, other)
if not isinstance(other, Set_object_enumerated):
return NotImplemented
if self.set() == other.set():
return rich_to_bool(op, 0)
return rich_to_bool(op, -1)

def issubset(self, other):
r"""
Expand Down Expand Up @@ -1208,7 +1209,7 @@ def is_finite(self):
"""
return self._X.is_finite() and self._Y.is_finite()

def __cmp__(self, right):
def __richcmp__(self, right, op):
r"""
Try to compare ``self`` and ``right``.
Expand Down Expand Up @@ -1237,13 +1238,13 @@ def __cmp__(self, right):
False
"""
if not is_Set(right):
return -1
return rich_to_bool(op, -1)
if not isinstance(right, Set_object_union):
return -1
return rich_to_bool(op, -1)
if self._X == right._X and self._Y == right._Y or \
self._X == right._Y and self._Y == right._X:
return 0
return -1
return rich_to_bool(op, 0)
return rich_to_bool(op, -1)

def __iter__(self):
"""
Expand Down Expand Up @@ -1342,7 +1343,7 @@ def is_finite(self):
return True
raise NotImplementedError

def __cmp__(self, right):
def __richcmp__(self, right, op):
r"""
Try to compare ``self`` and ``right``.
Expand Down Expand Up @@ -1371,13 +1372,13 @@ def __cmp__(self, right):
False
"""
if not is_Set(right):
return -1
return rich_to_bool(op, -1)
if not isinstance(right, Set_object_intersection):
return -1
return rich_to_bool(op, -1)
if self._X == right._X and self._Y == right._Y or \
self._X == right._Y and self._Y == right._X:
return 0
return -1
return rich_to_bool(op, 0)
return rich_to_bool(op, -1)

def __iter__(self):
"""
Expand Down Expand Up @@ -1484,7 +1485,7 @@ def is_finite(self):
return False
raise NotImplementedError

def __cmp__(self, right):
def __richcmp__(self, right, op):
r"""
Try to compare ``self`` and ``right``.
Expand Down Expand Up @@ -1517,12 +1518,12 @@ def __cmp__(self, right):
True
"""
if not is_Set(right):
return -1
return rich_to_bool(op, -1)
if not isinstance(right, Set_object_difference):
return -1
return rich_to_bool(op, -1)
if self._X == right._X and self._Y == right._Y:
return 0
return -1
return rich_to_bool(op, 0)
return rich_to_bool(op, -1)

def __iter__(self):
"""
Expand Down Expand Up @@ -1621,7 +1622,7 @@ def is_finite(self):
return False
raise NotImplementedError

def __cmp__(self, right):
def __richcmp__(self, right, op):
r"""
Try to compare ``self`` and ``right``.
Expand All @@ -1644,13 +1645,13 @@ def __cmp__(self, right):
"""
if not is_Set(right):
return -1
return rich_to_bool(op, -1)
if not isinstance(right, Set_object_symmetric_difference):
return -1
return rich_to_bool(op, -1)
if self._X == right._X and self._Y == right._Y or \
self._X == right._Y and self._Y == right._X:
return 0
return -1
return rich_to_bool(op, 0)
return rich_to_bool(op, -1)

def __iter__(self):
"""
Expand Down

0 comments on commit 5de3397

Please sign in to comment.