Skip to content

bpo-38335 simplify the overlap function for IpNetwork #16519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 21 additions & 36 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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) |
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -1511,7 +1512,6 @@ def __init__(self, address, strict=True):
self.hosts = self.__iter__

@property
@functools.lru_cache()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you removed this decorator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because self.network_address.is_global which in turn calls is_private already has a cache. There are pros and cons for example if someone removes cache there then suddenly this would loose caching behavior and also this having its own cache frees up the other cache. Should I add it back ?

def is_global(self):
"""Test if this address is allocated for public networks.

Expand All @@ -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')
Expand Down Expand Up @@ -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.
Expand Down