Patchwork [1,of,8,py3] pycompat: alias urlreq.unquote to unquote_to_bytes

login
register
mail settings
Submitter Gregory Szorc
Date March 22, 2017, 5:56 a.m.
Message ID <285f48d5644ea070f717.1490162198@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/19550/
State Accepted
Headers show

Comments

Gregory Szorc - March 22, 2017, 5:56 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1490160011 25200
#      Tue Mar 21 22:20:11 2017 -0700
# Node ID 285f48d5644ea070f717473af077e6728df6ea82
# Parent  102f291807c92864a2231e5e925d6cd64783bb59
pycompat: alias urlreq.unquote to unquote_to_bytes

Previously, urlreq.unquote aliased to urllib.parse.unquote,
which returned a str/unicode. We like bytes, so switch urlreq.unquote
to dispatch to urllib.parse.unquote_to_bytes.

This required a minor helper function to register an alias under a
different name from which it points. If this turns into a common
pattern, we could likely teach _registeralias to accept tuple
values defining the mapping. Until then, I didn't feel like
adding complexity to _registeralias.
Yuya Nishihara - March 22, 2017, 2:09 p.m.
On Tue, 21 Mar 2017 22:56:38 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1490160011 25200
> #      Tue Mar 21 22:20:11 2017 -0700
> # Node ID 285f48d5644ea070f717473af077e6728df6ea82
> # Parent  102f291807c92864a2231e5e925d6cd64783bb59
> pycompat: alias urlreq.unquote to unquote_to_bytes

Queued the series, many thanks.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -268,6 +268,10 @@  class _pycompatstub(object):
             (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
             for item in items)
 
+    def _registeralias(self, origin, attr, name):
+        """Alias ``origin``.``attr`` as ``name``"""
+        self._aliases[sysstr(name)] = (origin, sysstr(attr))
+
     def __getattr__(self, name):
         try:
             origin, item = self._aliases[name]
@@ -337,8 +341,8 @@  else:
         "splitpasswd",
         "splitport",
         "splituser",
-        "unquote",
     ))
+    urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote")
     import urllib.request
     urlreq._registeraliases(urllib.request, (
         "AbstractHTTPHandler",