Commit 52141aae authored by Mark Dickinson's avatar Mark Dickinson

Issue #1717: Remove cmp. Stage 1: remove all uses of cmp and __cmp__ from

the standard library and tests.
parent da4be9fd
...@@ -21,7 +21,7 @@ Every version number class implements the following interface: ...@@ -21,7 +21,7 @@ Every version number class implements the following interface:
an equivalent string -- ie. one that will generate an equivalent an equivalent string -- ie. one that will generate an equivalent
version number instance) version number instance)
* __repr__ generates Python code to recreate the version number instance * __repr__ generates Python code to recreate the version number instance
* __cmp__ compares the current instance with either another instance * _cmp compares the current instance with either another instance
of the same class or a string (which will be parsed to an instance of the same class or a string (which will be parsed to an instance
of the same class, thus must follow the same rules) of the same class, thus must follow the same rules)
""" """
...@@ -32,7 +32,7 @@ class Version: ...@@ -32,7 +32,7 @@ class Version:
"""Abstract base class for version numbering classes. Just provides """Abstract base class for version numbering classes. Just provides
constructor (__init__) and reproducer (__repr__), because those constructor (__init__) and reproducer (__repr__), because those
seem to be the same for all version numbering classes; and route seem to be the same for all version numbering classes; and route
rich comparisons to __cmp__. rich comparisons to _cmp.
""" """
def __init__ (self, vstring=None): def __init__ (self, vstring=None):
...@@ -43,37 +43,37 @@ class Version: ...@@ -43,37 +43,37 @@ class Version:
return "%s ('%s')" % (self.__class__.__name__, str(self)) return "%s ('%s')" % (self.__class__.__name__, str(self))
def __eq__(self, other): def __eq__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c == 0 return c == 0
def __ne__(self, other): def __ne__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c != 0 return c != 0
def __lt__(self, other): def __lt__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c < 0 return c < 0
def __le__(self, other): def __le__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c <= 0 return c <= 0
def __gt__(self, other): def __gt__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c > 0 return c > 0
def __ge__(self, other): def __ge__(self, other):
c = self.__cmp__(other) c = self._cmp(other)
if c is NotImplemented: if c is NotImplemented:
return c return c
return c >= 0 return c >= 0
...@@ -91,7 +91,7 @@ class Version: ...@@ -91,7 +91,7 @@ class Version:
# (if not identical to) the string supplied to parse # (if not identical to) the string supplied to parse
# __repr__ (self) - generate Python code to recreate # __repr__ (self) - generate Python code to recreate
# the instance # the instance
# __cmp__ (self, other) - compare two version numbers ('other' may # _cmp (self, other) - compare two version numbers ('other' may
# be an unparsed version string, or another # be an unparsed version string, or another
# instance of your version class) # instance of your version class)
...@@ -169,30 +169,39 @@ class StrictVersion (Version): ...@@ -169,30 +169,39 @@ class StrictVersion (Version):
return vstring return vstring
def __cmp__ (self, other): def _cmp (self, other):
if isinstance(other, str): if isinstance(other, str):
other = StrictVersion(other) other = StrictVersion(other)
compare = cmp(self.version, other.version) if self.version != other.version:
if (compare == 0): # have to compare prerelease # numeric versions don't match
# prerelease stuff doesn't matter
# case 1: neither has prerelease; they're equal if self.version < other.version:
# case 2: self has prerelease, other doesn't; other is greater return -1
# case 3: self doesn't have prerelease, other does: self is greater else:
# case 4: both have prerelease: must compare them! return 1
if (not self.prerelease and not other.prerelease): # have to compare prerelease
# case 1: neither has prerelease; they're equal
# case 2: self has prerelease, other doesn't; other is greater
# case 3: self doesn't have prerelease, other does: self is greater
# case 4: both have prerelease: must compare them!
if (not self.prerelease and not other.prerelease):
return 0
elif (self.prerelease and not other.prerelease):
return -1
elif (not self.prerelease and other.prerelease):
return 1
elif (self.prerelease and other.prerelease):
if self.prerelease == other.prerelease:
return 0 return 0
elif (self.prerelease and not other.prerelease): elif self.prerelease < other.prerelease:
return -1 return -1
elif (not self.prerelease and other.prerelease): else:
return 1 return 1
elif (self.prerelease and other.prerelease): else:
return cmp(self.prerelease, other.prerelease) assert False, "never get here"
else: # numeric versions don't match --
return compare # prerelease stuff doesn't matter
# end class StrictVersion # end class StrictVersion
...@@ -325,7 +334,7 @@ class LooseVersion (Version): ...@@ -325,7 +334,7 @@ class LooseVersion (Version):
return "LooseVersion ('%s')" % str(self) return "LooseVersion ('%s')" % str(self)
def __cmp__ (self, other): def _cmp (self, other):
if isinstance(other, str): if isinstance(other, str):
other = LooseVersion(other) other = LooseVersion(other)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment