diff hgext/zeroconf/Zeroconf.py @ 52029:997c9b2069d1

zeroconf: fix an invalid argument error on Windows The idea that pyoxidizer was triggering the problem when standing up the previous incarnation of CI for Windows was misleading- it was a Windows problem in general. See the inline bug link. Unfortunately, there's no commit referenced there, and it looks like OP closed the report himself with the suggested workaround. IOW, very modern python may not work, but it's extremely unlikely that there are any users of this extension, especially on Windows.
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 07 Oct 2024 23:20:09 -0400
parents f4733654f144
children c76c1c948804
line wrap: on
line diff
--- a/hgext/zeroconf/Zeroconf.py	Sat Oct 12 16:55:30 2024 -0400
+++ b/hgext/zeroconf/Zeroconf.py	Mon Oct 07 23:20:09 2024 -0400
@@ -232,6 +232,16 @@
 
 # implementation classes
 
+_SOL_IP = socket.SOL_IP
+
+if pycompat.iswindows:
+    # XXX: Not sure if there are newer versions of python where this would fail,
+    # but apparently socket.SOL_IP used to be 0, and socket.IPPROTO_IP is 0, so
+    # this would work with older versions of python.
+    #
+    # https://github.com/python/cpython/issues/101960
+    _SOL_IP = socket.IPPROTO_IP
+
 
 class DNSEntry:
     """A DNS entry"""
@@ -1419,8 +1429,8 @@
             # work as expected.
             #
             pass
-        self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, b"\xff")
-        self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, b"\x01")
+        self.socket.setsockopt(_SOL_IP, socket.IP_MULTICAST_TTL, b"\xff")
+        self.socket.setsockopt(_SOL_IP, socket.IP_MULTICAST_LOOP, b"\x01")
         try:
             self.socket.bind(self.group)
         except Exception:
@@ -1428,7 +1438,7 @@
             # SO_REUSEADDR and SO_REUSEPORT have been set, so ignore it
             pass
         self.socket.setsockopt(
-            socket.SOL_IP,
+            _SOL_IP,
             socket.IP_ADD_MEMBERSHIP,
             socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'),
         )
@@ -1844,7 +1854,7 @@
             self.engine.notify()
             self.unregisterAllServices()
             self.socket.setsockopt(
-                socket.SOL_IP,
+                _SOL_IP,
                 socket.IP_DROP_MEMBERSHIP,
                 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'),
             )