summaryrefslogtreecommitdiff
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-01-26 10:11:16 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2015-01-26 10:11:16 +0200
commit82d7d1e931d1e5351e55ca6e5e25c7d0eb5124ab (patch)
treeada09e0019f96f0e3b7f9d68b43fdee7c1d4bca7 /Lib/ipaddress.py
parent907bec882c53cf9009896a6f2d4febd761e35689 (diff)
downloadcpython-82d7d1e931d1e5351e55ca6e5e25c7d0eb5124ab.tar.gz
Issue #23268: Fixed bugs in the comparison of ipaddress classes.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py47
1 files changed, 7 insertions, 40 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index ebc04bb145..ac03c36ce0 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -388,40 +388,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."""
@@ -554,6 +521,7 @@ class _IPAddressBase(_TotalOrderingMixin):
self._report_invalid_netmask(ip_str)
+@functools.total_ordering
class _BaseAddress(_IPAddressBase):
"""A generic IP object.
@@ -578,12 +546,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
@@ -613,6 +580,7 @@ class _BaseAddress(_IPAddressBase):
return (self._version, self)
+@functools.total_ordering
class _BaseNetwork(_IPAddressBase):
"""A generic IP network object.
@@ -662,12 +630,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: