Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import sys
import threading
import time
import warnings
from collections import OrderedDict
from typing import Dict, List, Optional, Sequence, Union, cast
from typing import Any, Callable, Set, Tuple # noqa # used in type hints
Expand Down Expand Up @@ -1594,7 +1593,6 @@ class ServiceInfo(RecordUpdateListener):

* type_: fully qualified service type name
* name: fully qualified service name
* address: IP address as unsigned short, network byte order (deprecated, use addresses)
* port: port that the service runs on
* weight: weight of the service
* priority: priority of the service
Expand All @@ -1618,7 +1616,6 @@ def __init__(
self,
type_: str,
name: str,
address: Optional[Union[bytes, List[bytes]]] = None,
port: Optional[int] = None,
weight: int = 0,
priority: int = 0,
Expand All @@ -1629,22 +1626,12 @@ def __init__(
*,
addresses: Optional[List[bytes]] = None
) -> None:
# Accept both none, or one, but not both.
if address is not None and addresses is not None:
raise TypeError("address and addresses cannot be provided together")

if not type_.endswith(service_type_name(name, allow_underscores=True)):
raise BadTypeInNameException
self.type = type_
self.name = name
if addresses is not None:
self._addresses = addresses
elif address is not None:
warnings.warn("address is deprecated, use addresses instead", DeprecationWarning)
if isinstance(address, list):
self._addresses = address
else:
self._addresses = [address]
else:
self._addresses = []
# This results in an ugly error when registering, better check now
Expand All @@ -1666,23 +1653,6 @@ def __init__(
self.other_ttl = other_ttl
# fmt: on

@property
def address(self) -> Optional[bytes]:
warnings.warn("ServiceInfo.address is deprecated, use addresses instead", DeprecationWarning)
try:
# Return the first V4 address for compatibility
return self.addresses[0]
except IndexError:
return None

@address.setter
def address(self, value: bytes) -> None:
warnings.warn("ServiceInfo.address is deprecated, use addresses instead", DeprecationWarning)
if value is None:
self._addresses = []
else:
self._addresses = [value]

@property
def addresses(self) -> List[bytes]:
"""IPv4 addresses of this service.
Expand Down
111 changes: 68 additions & 43 deletions zeroconf/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_service_info_dunder(self):
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
info = ServiceInfo(
type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, b'', "ash-2.local."
type_, registration_name, 80, 0, 0, b'', "ash-2.local.", addresses=[socket.inet_aton("10.0.1.2")],
)

assert not info != info
Expand All @@ -121,7 +121,7 @@ def test_service_info_text_properties_not_given(self):
info = ServiceInfo(
type_=type_,
name=registration_name,
address=socket.inet_aton("10.0.1.2"),
addresses=[socket.inet_aton("10.0.1.2")],
port=80,
server="ash-2.local.",
)
Expand Down Expand Up @@ -456,7 +456,14 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
def verify_name_change(self, zc, type_, name, number_hosts):
desc = {'path': '/~paulsm/'}
info_service = ServiceInfo(
type_, '%s.%s' % (name, type_), socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
type_,
'%s.%s' % (name, type_),
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)

# verify name conflict
Expand Down Expand Up @@ -736,7 +743,14 @@ def test_ttl(self):

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)

# we are going to monkey patch the zeroconf send to check packet sizes
Expand Down Expand Up @@ -849,7 +863,14 @@ def test_integration_with_listener(self):
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

Expand All @@ -874,7 +895,14 @@ def test_integration_with_listener_v6_records(self):
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name, socket.inet_pton(socket.AF_INET6, addr), 80, 0, 0, desc, "ash-2.local."
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_pton(socket.AF_INET6, addr)],
)
zeroconf_registrar.register_service(info)

Expand All @@ -898,7 +926,14 @@ def test_integration_with_listener_ipv6(self):
zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

Expand All @@ -922,7 +957,14 @@ def test_integration_with_subtype_and_listener(self):
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
discovery_type, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
discovery_type,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

Expand Down Expand Up @@ -1028,7 +1070,14 @@ def update_service(self, zeroconf, type, name):
properties['prop_blank'] = b'an updated string'
desc.update(properties)
info_service = ServiceInfo(
subtype, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local."
subtype,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.update_service(info_service)
service_updated.wait(1)
Expand Down Expand Up @@ -1385,7 +1434,9 @@ def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.")
info = ServiceInfo(
type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[socket.inet_aton("10.0.1.2")]
)
zeroconf_registrar.register_service(info)

try:
Expand Down Expand Up @@ -1424,45 +1475,17 @@ def test_multiple_addresses():
address_parsed = "10.0.1.2"
address = socket.inet_aton(address_parsed)

# Old way
info = ServiceInfo(type_, registration_name, address, 80, 0, 0, desc, "ash-2.local.")

assert info.address == address
assert info.addresses == [address]

# Updating works
address2 = socket.inet_aton("10.0.1.3")
info.address = address2

assert info.address == address2
assert info.addresses == [address2]

info.address = None

assert info.address is None
assert info.addresses == []

info.addresses = [address2]

assert info.address == address2
assert info.addresses == [address2]

# Compatibility way
info = ServiceInfo(type_, registration_name, [address, address], 80, 0, 0, desc, "ash-2.local.")

assert info.addresses == [address, address]

# New kwarg way
info = ServiceInfo(
type_, registration_name, None, 80, 0, 0, desc, "ash-2.local.", addresses=[address, address]
)
info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address, address])

assert info.addresses == [address, address]

if socket.has_ipv6 and not os.environ.get('SKIP_IPV6'):
address_v6_parsed = "2001:db8::1"
address_v6 = socket.inet_pton(socket.AF_INET6, address_v6_parsed)
info = ServiceInfo(type_, registration_name, [address, address_v6], 80, 0, 0, desc, "ash-2.local.")
info = ServiceInfo(
type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address, address_v6],
)
assert info.addresses == [address]
assert info.addresses_by_version(r.IPVersion.All) == [address, address_v6]
assert info.addresses_by_version(r.IPVersion.V4Only) == [address]
Expand All @@ -1483,7 +1506,9 @@ def test_ptr_optimization():
registration_name = "%s.%s" % (name, type_)

desc = {'path': '/~paulsm/'}
info = ServiceInfo(type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.")
info = ServiceInfo(
type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[socket.inet_aton("10.0.1.2")]
)

# we are going to monkey patch the zeroconf send to check packet sizes
old_send = zc.send
Expand Down