Patchwork [06,of,10] py3: convert url to str before passing in urllib.request.Request

login
register
mail settings
Submitter Pulkit Goyal
Date May 6, 2017, 3:01 a.m.
Message ID <6065574d18279e75caa5.1494039678@pulkit-goyal>
Download mbox | patch
Permalink /patch/20474/
State Accepted
Headers show

Comments

Pulkit Goyal - May 6, 2017, 3:01 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1494029803 -19800
#      Sat May 06 05:46:43 2017 +0530
# Node ID 6065574d18279e75caa5b1172e4149fc498eccd9
# Parent  1a38d712f305b2dd5f2b2833cd78815ec3b383fd
py3: convert url to str before passing in urllib.request.Request

If we try to instantiate urllib.request.Request with a bytes url, it returns
ValueError from urllib.request.Request._parse().

Related link: https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l321
Yuya Nishihara - May 9, 2017, 2:38 p.m.
On Sat, 06 May 2017 08:31:18 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1494029803 -19800
> #      Sat May 06 05:46:43 2017 +0530
> # Node ID 6065574d18279e75caa5b1172e4149fc498eccd9
> # Parent  1a38d712f305b2dd5f2b2833cd78815ec3b383fd
> py3: convert url to str before passing in urllib.request.Request
> 
> If we try to instantiate urllib.request.Request with a bytes url, it returns
> ValueError from urllib.request.Request._parse().
> 
> Related link: https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l321
> 
> diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
> --- a/mercurial/httppeer.py
> +++ b/mercurial/httppeer.py
> @@ -175,7 +175,7 @@
>              else:
>                  q += sorted(args.items())
>          qs = '?%s' % urlreq.urlencode(q)
> -        cu = "%s%s" % (self._url, qs)
> +        cu = pycompat.sysstr("%s%s" % (self._url, qs))

Perhaps here self._url must be an ASCII string and that's important. How
about adding a function that converts bytes url back to str?

Patch

diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
--- a/mercurial/httppeer.py
+++ b/mercurial/httppeer.py
@@ -175,7 +175,7 @@ 
             else:
                 q += sorted(args.items())
         qs = '?%s' % urlreq.urlencode(q)
-        cu = "%s%s" % (self._url, qs)
+        cu = pycompat.sysstr("%s%s" % (self._url, qs))
         size = 0
         if util.safehasattr(data, 'length'):
             size = data.length