Skip to content

Commit a1c84dc

Browse files
authored
feat: cache is_unspecified for zeroconf ip address objects (#1331)
1 parent ede0a2a commit a1c84dc

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/zeroconf/_utils/ipaddress.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434

3535
class ZeroconfIPv4Address(IPv4Address):
3636

37-
__slots__ = ("_str", "_is_link_local")
37+
__slots__ = ("_str", "_is_link_local", "_is_unspecified")
3838

3939
def __init__(self, *args: Any, **kwargs: Any) -> None:
4040
"""Initialize a new IPv4 address."""
4141
super().__init__(*args, **kwargs)
4242
self._str = super().__str__()
4343
self._is_link_local = super().is_link_local
44+
self._is_unspecified = super().is_unspecified
4445

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

55+
@property
56+
def is_unspecified(self) -> bool:
57+
"""Return True if this is an unspecified address."""
58+
return self._is_unspecified
59+
5460

5561
class ZeroconfIPv6Address(IPv6Address):
5662

57-
__slots__ = ("_str", "_is_link_local")
63+
__slots__ = ("_str", "_is_link_local", "_is_unspecified")
5864

5965
def __init__(self, *args: Any, **kwargs: Any) -> None:
6066
"""Initialize a new IPv6 address."""
6167
super().__init__(*args, **kwargs)
6268
self._str = super().__str__()
6369
self._is_link_local = super().is_link_local
70+
self._is_unspecified = super().is_unspecified
6471

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

81+
@property
82+
def is_unspecified(self) -> bool:
83+
"""Return True if this is an unspecified address."""
84+
return self._is_unspecified
85+
7486

7587
@lru_cache(maxsize=512)
7688
def _cached_ip_addresses(address: Union[str, bytes, int]) -> Optional[Union[IPv4Address, IPv6Address]]:

tests/utils/test_ipaddress.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ def test_cached_ip_addresses_wrapper():
1818
ipv4 = ipaddress.cached_ip_addresses('169.254.0.0')
1919
assert ipv4 is not None
2020
assert ipv4.is_link_local is True
21+
assert ipv4.is_unspecified is False
22+
23+
ipv4 = ipaddress.cached_ip_addresses('0.0.0.0')
24+
assert ipv4 is not None
25+
assert ipv4.is_link_local is False
26+
assert ipv4.is_unspecified is True
2127

2228
ipv6 = ipaddress.cached_ip_addresses('fe80::1')
2329
assert ipv6 is not None
2430
assert ipv6.is_link_local is True
31+
assert ipv6.is_unspecified is False
32+
33+
ipv6 = ipaddress.cached_ip_addresses('0:0:0:0:0:0:0:0')
34+
assert ipv6 is not None
35+
assert ipv6.is_link_local is False
36+
assert ipv6.is_unspecified is True

0 commit comments

Comments
 (0)