Patchwork D5805: zeroconf: port to Python 3

login
register
mail settings
Submitter phabricator
Date Feb. 2, 2019, 8:08 p.m.
Message ID <differential-rev-PHID-DREV-jdx7vbx3wfdokblh47rv-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/38332/
State New
Headers show

Comments

phabricator - Feb. 2, 2019, 8:08 p.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Since we're using the source transformer on Python 3, calls into
  Zeroconf and return values from it are generally bytes.
  
  But various socket functions require str on Python 3.
  
  This commit contains enough changes to coerce test-paths.t into
  passing on Python 3. I suspect there are still a handful of bugs
  on Python 3. But the tests do pass.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5805

AFFECTED FILES
  hgext/zeroconf/__init__.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel
Yuya Nishihara - Feb. 3, 2019, 2:20 a.m.
>      # advertise to browsers
>      svc = Zeroconf.ServiceInfo('_http._tcp.local.',
> -                               name + '._http._tcp.local.',
> +                               pycompat.bytestr(name + r'._http._tcp.local.'),
>                                 server = host,
>                                 port = port,
> -                               properties = {'description': desc,
> -                                             'path': "/" + path},
> +                               properties = {
> +                                   'description': pycompat.bytestr(desc),
> +                                   'path': pycompat.bytestr(r"/" + path)},
>                                 address = localip, weight = 0, priority = 0)
>      server.registerService(svc)
>  
>      # advertise to Mercurial clients
>      svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
> -                               name + '._hg._tcp.local.',
> +                               pycompat.bytestr(name + r'._hg._tcp.local.'),
>                                 server = host,
>                                 port = port,
> -                               properties = {'description': desc,
> -                                             'path': "/" + path},
> +                               properties = {
> +                                   'description': pycompat.bytestr(desc),
> +                                   'path': pycompat.bytestr(r"/" + path)},
>                                 address = localip, weight = 0, priority = 0)

I think `path` and `desc` are bytes since they come from `ui.config()`.
phabricator - Feb. 3, 2019, 2:36 a.m.
yuja added a comment.


  >   1. advertise to browsers svc = Zeroconf.ServiceInfo('_http._tcp.local.',
  > - name + '._http._tcp.local.', +                               pycompat.bytestr(name + r'._http._tcp.local.'), server = host, port = port,
  > - properties = {'description': desc,
  > - 'path': "/" + path}, +                               properties = { +                                   'description': pycompat.bytestr(desc), +                                   'path': pycompat.bytestr(r"/" + path)}, address = localip, weight = 0, priority = 0) server.registerService(svc)
  >   1. advertise to Mercurial clients svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
  > - name + '._hg._tcp.local.', +                               pycompat.bytestr(name + r'._hg._tcp.local.'), server = host, port = port,
  > - properties = {'description': desc,
  > - 'path': "/" + path}, +                               properties = { +                                   'description': pycompat.bytestr(desc), +                                   'path': pycompat.bytestr(r"/" + path)}, address = localip, weight = 0, priority = 0)
  
  I think `path` and `desc` are bytes since they come from `ui.config()`.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5805

To: indygreg, #hg-reviewers
Cc: yuja, mercurial-devel

Patch

diff --git a/hgext/zeroconf/__init__.py b/hgext/zeroconf/__init__.py
--- a/hgext/zeroconf/__init__.py
+++ b/hgext/zeroconf/__init__.py
@@ -34,6 +34,7 @@ 
     encoding,
     extensions,
     hg,
+    pycompat,
     ui as uimod,
 )
 from mercurial.hgweb import (
@@ -55,26 +56,26 @@ 
     # finds external-facing interface without sending any packets (Linux)
     try:
         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-        s.connect(('1.0.0.1', 0))
+        s.connect((r'1.0.0.1', 0))
         ip = s.getsockname()[0]
         return ip
     except socket.error:
         pass
 
     # Generic method, sometimes gives useless results
     try:
         dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
-        if ':' in dumbip:
-            dumbip = '127.0.0.1'
-        if not dumbip.startswith('127.'):
+        if r':' in dumbip:
+            dumbip = r'127.0.0.1'
+        if not dumbip.startswith(r'127.'):
             return dumbip
     except (socket.gaierror, socket.herror):
-        dumbip = '127.0.0.1'
+        dumbip = r'127.0.0.1'
 
     # works elsewhere, but actually sends a packet
     try:
         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-        s.connect(('1.0.0.1', 1))
+        s.connect((r'1.0.0.1', 1))
         ip = s.getsockname()[0]
         return ip
     except socket.error:
@@ -86,33 +87,35 @@ 
     global server, localip
     if not server:
         ip = getip()
-        if ip.startswith('127.'):
+        if ip.startswith(r'127.'):
             # if we have no internet connection, this can happen.
             return
         localip = socket.inet_aton(ip)
         server = Zeroconf.Zeroconf(ip)
 
-    hostname = socket.gethostname().split('.')[0]
-    host = hostname + ".local"
-    name = "%s-%s" % (hostname, name)
+    hostname = socket.gethostname().split(r'.')[0]
+    host = hostname + r".local"
+    name = r"%s-%s" % (hostname, name)
 
     # advertise to browsers
     svc = Zeroconf.ServiceInfo('_http._tcp.local.',
-                               name + '._http._tcp.local.',
+                               pycompat.bytestr(name + r'._http._tcp.local.'),
                                server = host,
                                port = port,
-                               properties = {'description': desc,
-                                             'path': "/" + path},
+                               properties = {
+                                   'description': pycompat.bytestr(desc),
+                                   'path': pycompat.bytestr(r"/" + path)},
                                address = localip, weight = 0, priority = 0)
     server.registerService(svc)
 
     # advertise to Mercurial clients
     svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
-                               name + '._hg._tcp.local.',
+                               pycompat.bytestr(name + r'._hg._tcp.local.'),
                                server = host,
                                port = port,
-                               properties = {'description': desc,
-                                             'path': "/" + path},
+                               properties = {
+                                   'description': pycompat.bytestr(desc),
+                                   'path': pycompat.bytestr(r"/" + path)},
                                address = localip, weight = 0, priority = 0)
     server.registerService(svc)
 
@@ -158,18 +161,18 @@ 
 
 def getzcpaths():
     ip = getip()
-    if ip.startswith('127.'):
+    if ip.startswith(r'127.'):
         return
     server = Zeroconf.Zeroconf(ip)
     l = listener()
     Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
     time.sleep(1)
     server.close()
     for value in l.found.values():
-        name = value.name[:value.name.index('.')]
-        url = "http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port,
-                                  value.properties.get("path", "/"))
-        yield "zc-" + name, url
+        name = value.name[:value.name.index(b'.')]
+        url = r"http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port,
+                                  value.properties.get(r"path", r"/"))
+        yield b"zc-" + name, pycompat.bytestr(url)
 
 def config(orig, self, section, key, *args, **kwargs):
     if section == "paths" and key.startswith("zc-"):