urllibcompat: remove Python 2 support code
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 21 Feb 2022 10:35:20 -0700
changeset 48896 4286ec1d9842
parent 48895 62baa61efe8f
child 48897 10839394f39b
urllibcompat: remove Python 2 support code We had to move the `import` statements to appease the import checker. This whole module could probably be deleted as its point in life is to pave over Python 2/3 differences. But that's for a different commit. Differential Revision: https://phab.mercurial-scm.org/D12299
mercurial/urllibcompat.py
--- a/mercurial/urllibcompat.py	Mon Feb 21 10:32:45 2022 -0700
+++ b/mercurial/urllibcompat.py	Mon Feb 21 10:35:20 2022 -0700
@@ -5,6 +5,12 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import http.server
+import urllib.error
+import urllib.parse
+import urllib.request
+import urllib.response
+
 from .pycompat import getattr
 from . import pycompat
 
@@ -39,199 +45,109 @@
 urlreq = _pycompatstub()
 urlerr = _pycompatstub()
 
-if pycompat.ispy3:
-    import urllib.parse
-
-    urlreq._registeraliases(
-        urllib.parse,
-        (
-            b"splitattr",
-            b"splitpasswd",
-            b"splitport",
-            b"splituser",
-            b"urlparse",
-            b"urlunparse",
-        ),
-    )
-    urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
-    urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
-    urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
-    import urllib.request
-
-    urlreq._registeraliases(
-        urllib.request,
-        (
-            b"AbstractHTTPHandler",
-            b"BaseHandler",
-            b"build_opener",
-            b"FileHandler",
-            b"FTPHandler",
-            b"ftpwrapper",
-            b"HTTPHandler",
-            b"HTTPSHandler",
-            b"install_opener",
-            b"pathname2url",
-            b"HTTPBasicAuthHandler",
-            b"HTTPDigestAuthHandler",
-            b"HTTPPasswordMgrWithDefaultRealm",
-            b"ProxyHandler",
-            b"Request",
-            b"url2pathname",
-            b"urlopen",
-        ),
-    )
-    import urllib.response
-
-    urlreq._registeraliases(
-        urllib.response,
-        (
-            b"addclosehook",
-            b"addinfourl",
-        ),
-    )
-    import urllib.error
+urlreq._registeraliases(
+    urllib.parse,
+    (
+        b"splitattr",
+        b"splitpasswd",
+        b"splitport",
+        b"splituser",
+        b"urlparse",
+        b"urlunparse",
+    ),
+)
+urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
+urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
+urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
 
-    urlerr._registeraliases(
-        urllib.error,
-        (
-            b"HTTPError",
-            b"URLError",
-        ),
-    )
-    import http.server
-
-    httpserver._registeraliases(
-        http.server,
-        (
-            b"HTTPServer",
-            b"BaseHTTPRequestHandler",
-            b"SimpleHTTPRequestHandler",
-            b"CGIHTTPRequestHandler",
-        ),
-    )
-
-    # urllib.parse.quote() accepts both str and bytes, decodes bytes
-    # (if necessary), and returns str. This is wonky. We provide a custom
-    # implementation that only accepts bytes and emits bytes.
-    def quote(s, safe='/'):
-        # bytestr has an __iter__ that emits characters. quote_from_bytes()
-        # does an iteration and expects ints. We coerce to bytes to appease it.
-        if isinstance(s, pycompat.bytestr):
-            s = bytes(s)
-        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):
-        s = urllib.parse.urlencode(query, doseq=doseq)
-        return s.encode('ascii')
-
-    urlreq.quote = quote
-    urlreq.urlencode = urlencode
-
-    def getfullurl(req):
-        return req.full_url
-
-    def gethost(req):
-        return req.host
-
-    def getselector(req):
-        return req.selector
-
-    def getdata(req):
-        return req.data
-
-    def hasdata(req):
-        return req.data is not None
+urlreq._registeraliases(
+    urllib.request,
+    (
+        b"AbstractHTTPHandler",
+        b"BaseHandler",
+        b"build_opener",
+        b"FileHandler",
+        b"FTPHandler",
+        b"ftpwrapper",
+        b"HTTPHandler",
+        b"HTTPSHandler",
+        b"install_opener",
+        b"pathname2url",
+        b"HTTPBasicAuthHandler",
+        b"HTTPDigestAuthHandler",
+        b"HTTPPasswordMgrWithDefaultRealm",
+        b"ProxyHandler",
+        b"Request",
+        b"url2pathname",
+        b"urlopen",
+    ),
+)
 
 
-else:
-    # pytype: disable=import-error
-    import BaseHTTPServer
-    import CGIHTTPServer
-    import SimpleHTTPServer
-    import urllib2
-    import urllib
-    import urlparse
+urlreq._registeraliases(
+    urllib.response,
+    (
+        b"addclosehook",
+        b"addinfourl",
+    ),
+)
 
-    # pytype: enable=import-error
+urlerr._registeraliases(
+    urllib.error,
+    (
+        b"HTTPError",
+        b"URLError",
+    ),
+)
+
+httpserver._registeraliases(
+    http.server,
+    (
+        b"HTTPServer",
+        b"BaseHTTPRequestHandler",
+        b"SimpleHTTPRequestHandler",
+        b"CGIHTTPRequestHandler",
+    ),
+)
 
-    urlreq._registeraliases(
-        urllib,
-        (
-            b"addclosehook",
-            b"addinfourl",
-            b"ftpwrapper",
-            b"pathname2url",
-            b"quote",
-            b"splitattr",
-            b"splitpasswd",
-            b"splitport",
-            b"splituser",
-            b"unquote",
-            b"url2pathname",
-            b"urlencode",
-        ),
-    )
-    urlreq._registeraliases(
-        urllib2,
-        (
-            b"AbstractHTTPHandler",
-            b"BaseHandler",
-            b"build_opener",
-            b"FileHandler",
-            b"FTPHandler",
-            b"HTTPBasicAuthHandler",
-            b"HTTPDigestAuthHandler",
-            b"HTTPHandler",
-            b"HTTPPasswordMgrWithDefaultRealm",
-            b"HTTPSHandler",
-            b"install_opener",
-            b"ProxyHandler",
-            b"Request",
-            b"urlopen",
-        ),
-    )
-    urlreq._registeraliases(
-        urlparse,
-        (
-            b"urlparse",
-            b"urlunparse",
-        ),
-    )
-    urlreq._registeralias(urlparse, b"parse_qs", b"parseqs")
-    urlreq._registeralias(urlparse, b"parse_qsl", b"parseqsl")
-    urlerr._registeraliases(
-        urllib2,
-        (
-            b"HTTPError",
-            b"URLError",
-        ),
-    )
-    httpserver._registeraliases(
-        BaseHTTPServer,
-        (
-            b"HTTPServer",
-            b"BaseHTTPRequestHandler",
-        ),
-    )
-    httpserver._registeraliases(
-        SimpleHTTPServer, (b"SimpleHTTPRequestHandler",)
-    )
-    httpserver._registeraliases(CGIHTTPServer, (b"CGIHTTPRequestHandler",))
+# urllib.parse.quote() accepts both str and bytes, decodes bytes
+# (if necessary), and returns str. This is wonky. We provide a custom
+# implementation that only accepts bytes and emits bytes.
+def quote(s, safe='/'):
+    # bytestr has an __iter__ that emits characters. quote_from_bytes()
+    # does an iteration and expects ints. We coerce to bytes to appease it.
+    if isinstance(s, pycompat.bytestr):
+        s = bytes(s)
+    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):
+    s = urllib.parse.urlencode(query, doseq=doseq)
+    return s.encode('ascii')
+
 
-    def gethost(req):
-        return req.get_host()
+urlreq.quote = quote
+urlreq.urlencode = urlencode
+
 
-    def getselector(req):
-        return req.get_selector()
+def getfullurl(req):
+    return req.full_url
+
+
+def gethost(req):
+    return req.host
 
-    def getfullurl(req):
-        return req.get_full_url()
+
+def getselector(req):
+    return req.selector
+
 
-    def getdata(req):
-        return req.get_data()
+def getdata(req):
+    return req.data
 
-    def hasdata(req):
-        return req.has_data()
+
+def hasdata(req):
+    return req.data is not None