Skip to content
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

feat: cache is_unspecified for zeroconf ip address objects #1331

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/zeroconf/_utils/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@

class ZeroconfIPv4Address(IPv4Address):

__slots__ = ("_str", "_is_link_local")
__slots__ = ("_str", "_is_link_local", "_is_unspecified")

def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Initialize a new IPv4 address."""
super().__init__(*args, **kwargs)
self._str = super().__str__()
self._is_link_local = super().is_link_local
self._is_unspecified = super().is_unspecified

def __str__(self) -> str:
"""Return the string representation of the IPv4 address."""
Expand All @@ -51,16 +52,22 @@ def is_link_local(self) -> bool:
"""Return True if this is a link-local address."""
return self._is_link_local

@property
def is_unspecified(self) -> bool:
"""Return True if this is an unspecified address."""
return self._is_unspecified


class ZeroconfIPv6Address(IPv6Address):

__slots__ = ("_str", "_is_link_local")
__slots__ = ("_str", "_is_link_local", "_is_unspecified")

def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Initialize a new IPv6 address."""
super().__init__(*args, **kwargs)
self._str = super().__str__()
self._is_link_local = super().is_link_local
self._is_unspecified = super().is_unspecified

def __str__(self) -> str:
"""Return the string representation of the IPv6 address."""
Expand All @@ -71,6 +78,11 @@ def is_link_local(self) -> bool:
"""Return True if this is a link-local address."""
return self._is_link_local

@property
def is_unspecified(self) -> bool:
"""Return True if this is an unspecified address."""
return self._is_unspecified


@lru_cache(maxsize=512)
def _cached_ip_addresses(address: Union[str, bytes, int]) -> Optional[Union[IPv4Address, IPv6Address]]:
Expand Down
12 changes: 12 additions & 0 deletions tests/utils/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ def test_cached_ip_addresses_wrapper():
ipv4 = ipaddress.cached_ip_addresses('169.254.0.0')
assert ipv4 is not None
assert ipv4.is_link_local is True
assert ipv4.is_unspecified is False

ipv4 = ipaddress.cached_ip_addresses('0.0.0.0')
assert ipv4 is not None
assert ipv4.is_link_local is False
assert ipv4.is_unspecified is True

ipv6 = ipaddress.cached_ip_addresses('fe80::1')
assert ipv6 is not None
assert ipv6.is_link_local is True
assert ipv6.is_unspecified is False

ipv6 = ipaddress.cached_ip_addresses('0:0:0:0:0:0:0:0')
assert ipv6 is not None
assert ipv6.is_link_local is False
assert ipv6.is_unspecified is True