Issue #23268: Fixed bugs in the comparison of ipaddress classes.

This commit is contained in:
Serhiy Storchaka 2015-01-26 10:11:39 +02:00
commit ffd48c9e3d
3 changed files with 51 additions and 45 deletions

View file

@ -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: