From 6dd19b9a63ba2fc0c107b4b4c0daf39ea5094ea9 Mon Sep 17 00:00:00 2001 From: Andreas Oberritter Date: Mon, 10 Feb 2020 21:54:21 +0100 Subject: [PATCH 1/6] Do not convert empty strings in TXT records to bool --- zeroconf/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf/__init__.py b/zeroconf/__init__.py index b3df8180..ddb64b5d 100644 --- a/zeroconf/__init__.py +++ b/zeroconf/__init__.py @@ -1699,7 +1699,7 @@ def _set_text(self, text: bytes) -> None: else: if value == b'true': value = True - elif value == b'false' or not value: + elif value == b'false': value = False # Only update non-existent properties From 5c258840914d96d67ce8b5e735000c2df4aa746a Mon Sep 17 00:00:00 2001 From: Andreas Oberritter Date: Thu, 20 Feb 2020 00:14:31 +0100 Subject: [PATCH 2/6] Adjust weird tests to match new behavior --- zeroconf/test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zeroconf/test.py b/zeroconf/test.py index 3b381f60..9e3343a3 100644 --- a/zeroconf/test.py +++ b/zeroconf/test.py @@ -985,9 +985,9 @@ def update_service(self, zeroconf, type, name): # get service info without answer cache info = zeroconf_browser.get_service_info(type_, registration_name) assert info is not None - assert info.properties[b'prop_none'] is False + assert info.properties[b'prop_none'] == b'' assert info.properties[b'prop_string'] == properties['prop_string'] - assert info.properties[b'prop_float'] is False + assert info.properties[b'prop_float'] == b'' assert info.properties[b'prop_blank'] == properties['prop_blank'] assert info.properties[b'prop_true'] is True assert info.properties[b'prop_false'] is False @@ -997,7 +997,7 @@ def update_service(self, zeroconf, type, name): info = zeroconf_browser.get_service_info(subtype, registration_name) assert info is not None - assert info.properties[b'prop_none'] is False + assert info.properties[b'prop_none'] == b'' # test TXT record update sublistener = MySubListener() From e716183bff70ef913db8015f96bcfc9e6b654c95 Mon Sep 17 00:00:00 2001 From: Andreas Oberritter Date: Fri, 28 Feb 2020 02:10:33 +0100 Subject: [PATCH 3/6] ServiceInfo: Do not interpret received values; use None if a property has no value --- zeroconf/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/zeroconf/__init__.py b/zeroconf/__init__.py index ddb64b5d..c5e64c95 100644 --- a/zeroconf/__init__.py +++ b/zeroconf/__init__.py @@ -1695,12 +1695,7 @@ def _set_text(self, text: bytes) -> None: except ValueError: # No equals sign at all key = s - value = False - else: - if value == b'true': - value = True - elif value == b'false': - value = False + value = None # Only update non-existent properties if key and result.get(key) is None: From d9777ec2d3b25aeafd23685b43464da4a6f6bc8c Mon Sep 17 00:00:00 2001 From: Andreas Oberritter Date: Fri, 28 Feb 2020 02:12:31 +0100 Subject: [PATCH 4/6] ServiceInfo: When encoding values, use either raw bytes or UTF-8 --- zeroconf/__init__.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/zeroconf/__init__.py b/zeroconf/__init__.py index c5e64c95..3e971acd 100644 --- a/zeroconf/__init__.py +++ b/zeroconf/__init__.py @@ -1655,20 +1655,12 @@ def _set_properties(self, properties: Union[bytes, Dict]) -> None: if isinstance(key, str): key = key.encode('utf-8') - if value is None: - suffix = b'' - elif isinstance(value, str): - suffix = value.encode('utf-8') - elif isinstance(value, bytes): - suffix = value - elif isinstance(value, int): - if value: - suffix = b'true' - else: - suffix = b'false' - else: - suffix = b'' - list_.append(b'='.join((key, suffix))) + record = key + if value is not None: + if not isinstance(value, bytes): + value = str(value).encode('utf-8') + record += b'=' + value + list_.append(record) for item in list_: result = b''.join((result, int2byte(len(item)), item)) self.text = result From 0312a2d5f2d45fdc5fb6dc4fbde38d4dd2f961a8 Mon Sep 17 00:00:00 2001 From: Andreas Oberritter Date: Fri, 28 Feb 2020 02:22:41 +0100 Subject: [PATCH 5/6] Adjust tests again to match new behavior --- zeroconf/test.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/zeroconf/test.py b/zeroconf/test.py index 9e3343a3..faa794bb 100644 --- a/zeroconf/test.py +++ b/zeroconf/test.py @@ -985,19 +985,19 @@ def update_service(self, zeroconf, type, name): # get service info without answer cache info = zeroconf_browser.get_service_info(type_, registration_name) assert info is not None - assert info.properties[b'prop_none'] == b'' + assert info.properties[b'prop_none'] is None assert info.properties[b'prop_string'] == properties['prop_string'] - assert info.properties[b'prop_float'] == b'' + assert info.properties[b'prop_float'] == b'1.0' assert info.properties[b'prop_blank'] == properties['prop_blank'] - assert info.properties[b'prop_true'] is True - assert info.properties[b'prop_false'] is False + assert info.properties[b'prop_true'] == b'1' + assert info.properties[b'prop_false'] == b'0' assert info.addresses == addresses[:1] # no V6 by default all_addresses = info.addresses_by_version(r.IPVersion.All) assert all_addresses == addresses, all_addresses info = zeroconf_browser.get_service_info(subtype, registration_name) assert info is not None - assert info.properties[b'prop_none'] == b'' + assert info.properties[b'prop_none'] is None # test TXT record update sublistener = MySubListener() From 4cde41fdd33c0025cf767cdf032b15285872636e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 8 Mar 2020 00:47:49 +0100 Subject: [PATCH 6/6] Correct a type hint --- zeroconf/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf/__init__.py b/zeroconf/__init__.py index 3e971acd..899fa0a5 100644 --- a/zeroconf/__init__.py +++ b/zeroconf/__init__.py @@ -1683,7 +1683,7 @@ def _set_text(self, text: bytes) -> None: for s in strs: parts = s.split(b'=', 1) try: - key, value = parts # type: Tuple[bytes, Union[bool, bytes]] + key, value = parts # type: Tuple[bytes, Optional[bytes]] except ValueError: # No equals sign at all key = s