Patchwork [4,of,4] py3: add a bytes version of urllib.parse.urlencode() to pycompat.py

login
register
mail settings
Submitter Pulkit Goyal
Date April 7, 2017, 10:59 a.m.
Message ID <c3e934121b61d54184e9.1491562780@pulkit-goyal>
Download mbox | patch
Permalink /patch/19998/
State Accepted
Headers show

Comments

Pulkit Goyal - April 7, 2017, 10:59 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1491561044 -19800
#      Fri Apr 07 16:00:44 2017 +0530
# Node ID c3e934121b61d54184e914bda587281b4184eabf
# Parent  beedb62e62a78a2f45ef8bd0b72c51f14619ee7f
py3: add a bytes version of urllib.parse.urlencode() to pycompat.py

urllib.parse.urlencode() returns unicodes on Python 3. This commit adds a
method which will take its output and encode it to bytes so that we can use
bytes consistently.
Yuya Nishihara - April 7, 2017, 12:18 p.m.
On Fri, 07 Apr 2017 16:29:40 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1491561044 -19800
> #      Fri Apr 07 16:00:44 2017 +0530
> # Node ID c3e934121b61d54184e914bda587281b4184eabf
> # Parent  beedb62e62a78a2f45ef8bd0b72c51f14619ee7f
> py3: add a bytes version of urllib.parse.urlencode() to pycompat.py
> 
> urllib.parse.urlencode() returns unicodes on Python 3. This commit adds a
> method which will take its output and encode it to bytes so that we can use
> bytes consistently.
> 
> diff -r beedb62e62a7 -r c3e934121b61 mercurial/pycompat.py
> --- a/mercurial/pycompat.py	Fri Apr 07 15:58:04 2017 +0530
> +++ b/mercurial/pycompat.py	Fri Apr 07 16:00:44 2017 +0530
> @@ -399,4 +399,12 @@
>          s = urllib.parse.quote_from_bytes(s, safe=safe)
>          return s.encode('ascii', 'strict')
>  
> +    # urllib.parse.urlencode() returns str. We use this function to make
> +    # sure we return bytes.
> +    def urlencode(query, doseq=False, safe='', encoding=None, errors=None):

Nit: better to not provide safe, encoding and errors arguments which don't
exist in Python 3. Other than that, the patch looks good to me.

> +            s = urllib.parse.urlencode(query, doseq=doseq, safe=safe,
> +                        encoding=encoding, errors=errors)
> +            return s.encode('ascii')
Yuya Nishihara - April 8, 2017, 8:05 a.m.
On Fri, 7 Apr 2017 23:14:02 +0530, Pulkit Goyal wrote:
> One general thing, when
> you are referring to Python 3 here, are you also considering the version
> older than Python 3.5 as my current approach is to look for
> things(documentation) in 3.5 only. I don't care about previous versions. 

No, I don't care about old Python 3. Python 3.5+ are the only usable versions.

Patch

diff -r beedb62e62a7 -r c3e934121b61 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Fri Apr 07 15:58:04 2017 +0530
+++ b/mercurial/pycompat.py	Fri Apr 07 16:00:44 2017 +0530
@@ -399,4 +399,12 @@ 
         s = urllib.parse.quote_from_bytes(s, safe=safe)
         return s.encode('ascii', 'strict')
 
+    # urllib.parse.urlencode() returns str. We use this function to make
+    # sure we return bytes.
+    def urlencode(query, doseq=False, safe='', encoding=None, errors=None):
+            s = urllib.parse.urlencode(query, doseq=doseq, safe=safe,
+                        encoding=encoding, errors=errors)
+            return s.encode('ascii')
+
     urlreq.quote = quote
+    urlreq.urlencode = urlencode