diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-01-26 08:11:39 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-01-26 08:11:39 (GMT) |
commit | ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e (patch) | |
tree | 93a1d720e8ba0e133aa422e94431a4e33a7ab744 /Lib/ipaddress.py | |
parent | 34af5023fc1e74c2d6a7537a11148885c84d9cb4 (diff) | |
parent | f186e128b6d4e6031e1e7e97b26b3eda64c1b88f (diff) | |
download | cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.zip cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.tar.gz cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.tar.bz2 |
Issue #23268: Fixed bugs in the comparison of ipaddress classes.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r-- | Lib/ipaddress.py | 47 |
1 files changed, 7 insertions, 40 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 2ba98d8..6c225f3 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -382,40 +382,7 @@ def get_mixed_type_key(obj): return NotImplemented -class _TotalOrderingMixin: - # Helper that derives the other comparison operations from - # __lt__ and __eq__ - # We avoid functools.total_ordering because it doesn't handle - # NotImplemented correctly yet (http://bugs.python.org/issue10042) - def __eq__(self, other): - raise NotImplementedError - def __ne__(self, other): - equal = self.__eq__(other) - if equal is NotImplemented: - return NotImplemented - return not equal - def __lt__(self, other): - raise NotImplementedError - def __le__(self, other): - less = self.__lt__(other) - if less is NotImplemented or not less: - return self.__eq__(other) - return less - def __gt__(self, other): - less = self.__lt__(other) - if less is NotImplemented: - return NotImplemented - equal = self.__eq__(other) - if equal is NotImplemented: - return NotImplemented - return not (less or equal) - def __ge__(self, other): - less = self.__lt__(other) - if less is NotImplemented: - return NotImplemented - return not less - -class _IPAddressBase(_TotalOrderingMixin): +class _IPAddressBase: """The mother class.""" @@ -567,6 +534,7 @@ class _IPAddressBase(_TotalOrderingMixin): return self.__class__, (str(self),) +@functools.total_ordering class _BaseAddress(_IPAddressBase): """A generic IP object. @@ -586,12 +554,11 @@ class _BaseAddress(_IPAddressBase): return NotImplemented def __lt__(self, other): + if not isinstance(other, _BaseAddress): + return NotImplemented if self._version != other._version: raise TypeError('%s and %s are not of the same version' % ( self, other)) - if not isinstance(other, _BaseAddress): - raise TypeError('%s and %s are not of the same type' % ( - self, other)) if self._ip != other._ip: return self._ip < other._ip return False @@ -624,6 +591,7 @@ class _BaseAddress(_IPAddressBase): return self.__class__, (self._ip,) +@functools.total_ordering class _BaseNetwork(_IPAddressBase): """A generic IP network object. @@ -673,12 +641,11 @@ class _BaseNetwork(_IPAddressBase): return self._address_class(broadcast + n) def __lt__(self, other): + if not isinstance(other, _BaseNetwork): + return NotImplemented if self._version != other._version: raise TypeError('%s and %s are not of the same version' % ( self, other)) - if not isinstance(other, _BaseNetwork): - raise TypeError('%s and %s are not of the same type' % ( - self, other)) if self.network_address != other.network_address: return self.network_address < other.network_address if self.netmask != other.netmask: |