From 360e00722ffd5d15e52e0fa1229cb32037d303d0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 27 Jun 2021 20:04:53 +0530 Subject: [PATCH] Add a build test for monkeypatching zeroconf --- src/calibre/devices/smart_device_app/driver.py | 18 +++++++++++++----- src/calibre/test_build.py | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/calibre/devices/smart_device_app/driver.py b/src/calibre/devices/smart_device_app/driver.py index e34b142125..bc779cfb7d 100644 --- a/src/calibre/devices/smart_device_app/driver.py +++ b/src/calibre/devices/smart_device_app/driver.py @@ -394,10 +394,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): self.debug_start_time = time.time() self.debug_time = time.time() self.is_connected = False - # Hack to work around the newly-enforced 15 character service name limit. - # "monkeypatch" zeroconf with a function without the check - import zeroconf - zeroconf.service_type_name = service_type_name + monkeypatch_zeroconf() # Don't call this method from the GUI unless you are sure that there is no # network traffic in progress. Otherwise the gui might hang waiting for the @@ -2059,7 +2056,7 @@ from zeroconf import ( ) -def service_type_name(type_: str, *, strict: bool = True) -> str: +def monkeypatched_service_type_name(type_: str, *, strict: bool = True) -> str: """ Validate a fully qualified service name, instance or subtype. [rfc6763] @@ -2178,3 +2175,14 @@ def service_type_name(type_: str, *, strict: bool = True) -> str: ) return service_name + trailer + + +def monkeypatch_zeroconf(): + # Hack to work around the newly-enforced 15 character service name limit. + # "monkeypatch" zeroconf with a function without the check + try: + from zeroconf._utils.name import service_type_name + service_type_name.__kwdefaults__['strict'] = False + except ImportError: + import zeroconf + zeroconf.service_type_name = monkeypatched_service_type_name diff --git a/src/calibre/test_build.py b/src/calibre/test_build.py index c286a90222..424adbfd80 100644 --- a/src/calibre/test_build.py +++ b/src/calibre/test_build.py @@ -106,6 +106,8 @@ class BuildTest(unittest.TestCase): def test_zeroconf(self): import zeroconf as z, ifaddr + from calibre.devices.smart_device_app.driver import monkeypatch_zeroconf + monkeypatch_zeroconf() del z del ifaddr