[PATCH 1 of 7] pycompat: add util.urlerr util.urlreq classes for py3 compat

timeless timeless at mozdev.org
Mon Apr 11 18:48:55 EDT 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1459987548 0
#      Thu Apr 07 00:05:48 2016 +0000
# Node ID b70fa10db0f545ac167ab1408065ea58fb2de831
# Parent  86db5cb55d46db3984e94600f3902f47a16437ae
pycompat: add util.urlerr util.urlreq classes for py3 compat

python3 url.request and url.error are mapped as util.urlreq/util.urlerr
python2 equivalents from urllib/urllib2 are mapped according to the py3
hierarchy

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -25,6 +25,97 @@
 empty = _queue.Empty
 queue = _queue.Queue
 
+class _pycompatstub(object):
+    pass
+
+def _alias(alias, origin, items):
+    """ populate a _pycompatstub
+
+    copies items from origin to alias
+    """
+    def hgcase(item):
+        return item.replace('_', '').lower()
+    for item in items:
+        try:
+            setattr(alias, hgcase(item), getattr(origin, item))
+        except AttributeError:
+            pass
+
+urlreq = _pycompatstub()
+urlerr = _pycompatstub()
+try:
+    import urllib2
+    import urllib
+    _alias(urlreq, urllib, (
+        "addclosehook",
+        "addinfourl",
+        "ftpwrapper",
+        "pathname2url",
+        "quote",
+        "splitattr",
+        "splitpasswd",
+        "splitport",
+        "splituser",
+        "unquote",
+        "url2pathname",
+        "urlencode",
+        "urlencode",
+    ))
+    _alias(urlreq, urllib2, (
+        "AbstractHTTPHandler",
+        "BaseHandler",
+        "build_opener",
+        "FileHandler",
+        "FTPHandler",
+        "HTTPBasicAuthHandler",
+        "HTTPDigestAuthHandler",
+        "HTTPHandler",
+        "HTTPPasswordMgrWithDefaultRealm",
+        "HTTPSHandler",
+        "install_opener",
+        "ProxyHandler",
+        "Request",
+        "urlopen",
+    ))
+    _alias(urlerr, urllib2, (
+        "HTTPError",
+        "URLError",
+    ))
+
+except ImportError:
+    import urllib.request
+    _alias(urlreq, urllib.request, (
+        "AbstractHTTPHandler",
+        "addclosehook",
+        "addinfourl",
+        "BaseHandler",
+        "build_opener",
+        "FileHandler",
+        "FTPHandler",
+        "ftpwrapper",
+        "HTTPHandler",
+        "HTTPSHandler",
+        "install_opener",
+        "pathname2url",
+        "HTTPBasicAuthHandler",
+        "HTTPDigestAuthHandler",
+        "ProxyHandler",
+        "quote",
+        "Request",
+        "splitattr",
+        "splitpasswd",
+        "splitport",
+        "splituser",
+        "unquote",
+        "url2pathname",
+        "urlopen",
+    ))
+    import urllib.error
+    _alias(urlerr, urllib.error, (
+        "HTTPError",
+        "URLError",
+    ))
+
 try:
     xrange
 except NameError:
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -49,6 +49,8 @@
 for attr in (
     'empty',
     'queue',
+    'urlerr',
+    'urlreq',
     'stringio',
 ):
     globals()[attr] = getattr(pycompat, attr)


More information about the Mercurial-devel mailing list