FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: accept str addresses in ServiceInfo.addresses under cython (#1836) · python-zeroconf/python-zeroconf@c0b2d50 · GitHub

Commit c0b2d50

Browse files
authored
fix: accept str addresses in ServiceInfo.addresses under cython (#1836)
1 parent 9dc8bf8 commit c0b2d50

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎src/zeroconf/_services/info.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def __init__(
219219
host_ttl: int = _DNS_HOST_TTL,
220220
other_ttl: int = _DNS_OTHER_TTL,
221221
*,
222-
addresses: list[bytes] | None = None,
222+
addresses: Sequence[bytes | str] | None = None,
223223
parsed_addresses: list[str] | None = None,
224224
interface_index: int | None = None,
225225
) -> None:
@@ -292,7 +292,7 @@ def addresses(self) -> list[bytes]:
292292
return self.addresses_by_version(IPVersion.V4Only)
293293

294294
@addresses.setter
295-
def addresses(self, value: list[bytes]) -> None:
295+
def addresses(self, value: Sequence[bytes | str]) -> None:
296296
"""Replace the addresses list.
297297
298298
This replaces all currently stored addresses, both IPv4 and IPv6.

‎src/zeroconf/_utils/ipaddress.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def get_ip_address_object_from_record(
137137

138138

139139
def ip_bytes_and_scope_to_address(
140-
address: bytes_, scope: int_
140+
address: bytes_ | str, scope: int_
141141
) -> ZeroconfIPv4Address | ZeroconfIPv6Address | None:
142142
"""Convert the bytes and scope to an IP address object."""
143143
base_address = cached_ip_addresses_wrapper(address)

‎tests/services/test_info.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,3 +3071,30 @@ def test_denied_flag_is_ignored_when_address_is_held(zc_loopback: r.Zeroconf) ->
30713071
assert info.addresses == [socket.inet_aton("127.0.0.1")]
30723072
# the held A record keeps the request querying instead of fast failing
30733073
assert info._is_denied is False
3074+
3075+
3076+
@pytest.mark.parametrize(
3077+
"addresses",
3078+
[
3079+
["10.0.1.2", "2001:db8::1"],
3080+
[socket.inet_aton("10.0.1.2"), socket.inet_pton(socket.AF_INET6, "2001:db8::1")],
3081+
["10.0.1.2", socket.inet_pton(socket.AF_INET6, "2001:db8::1")],
3082+
],
3083+
)
3084+
def test_addresses_setter_accepts_str_and_bytes(addresses):
3085+
"""The addresses setter accepts str and bytes addresses."""
3086+
type_ = "_http._tcp.local."
3087+
info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.")
3088+
info.addresses = addresses
3089+
assert info.parsed_addresses() == ["10.0.1.2", "2001:db8::1"]
3090+
3091+
info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.", addresses=addresses)
3092+
assert info.parsed_addresses() == ["10.0.1.2", "2001:db8::1"]
3093+
3094+
3095+
def test_addresses_setter_rejects_invalid_address():
3096+
"""The addresses setter raises TypeError for invalid addresses."""
3097+
type_ = "_http._tcp.local."
3098+
info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.")
3099+
with pytest.raises(TypeError, match="Addresses must either be"):
3100+
info.addresses = ["not an address"]

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL