From a329901dc6a2984c97980cb4028e63fe8f8e3aad Mon Sep 17 00:00:00 2001 From: sanjay <7111850+s-sanjay@users.noreply.github.com> Date: Tue, 1 Oct 2019 16:03:32 +0530 Subject: [PATCH 1/2] simplify the overlap function --- Lib/ipaddress.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 7d80a52c158a9e..8512ff1367be3f 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -750,13 +750,6 @@ def __contains__(self, other): # address return other._ip & self.netmask._ip == self.network_address._ip - def overlaps(self, other): - """Tell if self is partly contained in other.""" - return self.network_address in other or ( - self.broadcast_address in other or ( - other.network_address in self or ( - other.broadcast_address in self))) - @functools.cached_property def broadcast_address(self): return self._address_class(int(self.network_address) | @@ -1046,6 +1039,13 @@ def _is_subnet_of(a, b): raise TypeError(f"Unable to test subnet containment " f"between {a} and {b}") + def overlaps(self, other): + """Tell if self is partly contained in other.""" + if not isinstance(other, _BaseNetwork) or ( + self.version != other.version): + return False + return self.subnet_of(other) or self.supernet_of(other) + def subnet_of(self, other): """Return True if this network is a subnet of other.""" return self._is_subnet_of(self, other) From 7e256131a7b14541ac100d8f273653c20cf3a131 Mon Sep 17 00:00:00 2001 From: sanjay <7111850+s-sanjay@users.noreply.github.com> Date: Thu, 10 Oct 2019 15:10:36 +0530 Subject: [PATCH 2/2] move common code to base class, remove hardcoded values, simplify is_global for network --- Lib/ipaddress.py | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 8512ff1367be3f..eed2d9c4b5ba56 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -669,6 +669,19 @@ def __format__(self, fmt): return format(int(self), f'{alternate}0{padlen}{grouping}{fmt_base}') + @property + @functools.lru_cache() + def is_private(self): + """Test if this address is allocated for private networks. + + Returns: + A boolean, True if the address is reserved per + iana-ipv6-special-registry for ipv6 + and iana-ipv6-special-registry for ipv4 + + """ + return any(self in net for net in self._constants._private_networks) + @functools.total_ordering class _BaseNetwork(_IPAddressBase): @@ -1318,18 +1331,6 @@ def is_reserved(self): """ return self in self._constants._reserved_network - @property - @functools.lru_cache() - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per - iana-ipv4-special-registry. - - """ - return any(self in net for net in self._constants._private_networks) - @property @functools.lru_cache() def is_global(self): @@ -1511,7 +1512,6 @@ def __init__(self, address, strict=True): self.hosts = self.__iter__ @property - @functools.lru_cache() def is_global(self): """Test if this address is allocated for public networks. @@ -1520,10 +1520,7 @@ def is_global(self): iana-ipv4-special-registry. """ - return (not (self.network_address in IPv4Network('100.64.0.0/10') and - self.broadcast_address in IPv4Network('100.64.0.0/10')) and - not self.is_private) - + return self.network_address.is_global class _IPv4Constants: _linklocal_network = IPv4Network('169.254.0.0/16') @@ -1938,18 +1935,6 @@ def is_site_local(self): """ return self in self._constants._sitelocal_network - @property - @functools.lru_cache() - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per - iana-ipv6-special-registry. - - """ - return any(self in net for net in self._constants._private_networks) - @property def is_global(self): """Test if this address is allocated for public networks.