diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 7d80a52c158a9e..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): @@ -750,13 +763,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 +1052,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) @@ -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.