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.
( )
Alphare |
hg-reviewers |
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.
Automatic diff as part of commit; lint not applicable. |
Automatic diff as part of commit; unit tests not applicable. |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/urllibcompat.py (288 lines) |
# urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3 | # urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3 | ||||
# | # | ||||
# Copyright 2017 Google, Inc. | # Copyright 2017 Google, Inc. | ||||
# | # | ||||
# This software may be used and distributed according to the terms of the | # This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | # 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 .pycompat import getattr | ||||
from . import pycompat | from . import pycompat | ||||
_sysstr = pycompat.sysstr | _sysstr = pycompat.sysstr | ||||
class _pycompatstub(object): | class _pycompatstub(object): | ||||
def __init__(self): | def __init__(self): | ||||
self.__dict__[name] = obj = getattr(origin, item) | self.__dict__[name] = obj = getattr(origin, item) | ||||
return obj | return obj | ||||
httpserver = _pycompatstub() | httpserver = _pycompatstub() | ||||
urlreq = _pycompatstub() | urlreq = _pycompatstub() | ||||
urlerr = _pycompatstub() | urlerr = _pycompatstub() | ||||
if pycompat.ispy3: | |||||
import urllib.parse | |||||
urlreq._registeraliases( | urlreq._registeraliases( | ||||
urllib.parse, | urllib.parse, | ||||
( | ( | ||||
b"splitattr", | b"splitattr", | ||||
b"splitpasswd", | b"splitpasswd", | ||||
b"splitport", | b"splitport", | ||||
b"splituser", | b"splituser", | ||||
b"urlparse", | b"urlparse", | ||||
b"urlunparse", | b"urlunparse", | ||||
), | ), | ||||
) | ) | ||||
urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs") | urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs") | ||||
urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl") | urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl") | ||||
urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote") | urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote") | ||||
import urllib.request | |||||
urlreq._registeraliases( | urlreq._registeraliases( | ||||
urllib.request, | urllib.request, | ||||
( | ( | ||||
b"AbstractHTTPHandler", | b"AbstractHTTPHandler", | ||||
b"BaseHandler", | b"BaseHandler", | ||||
b"build_opener", | b"build_opener", | ||||
b"FileHandler", | b"FileHandler", | ||||
b"FTPHandler", | b"FTPHandler", | ||||
b"ftpwrapper", | b"ftpwrapper", | ||||
b"HTTPHandler", | b"HTTPHandler", | ||||
b"HTTPSHandler", | b"HTTPSHandler", | ||||
b"install_opener", | b"install_opener", | ||||
b"pathname2url", | b"pathname2url", | ||||
b"HTTPBasicAuthHandler", | b"HTTPBasicAuthHandler", | ||||
b"HTTPDigestAuthHandler", | b"HTTPDigestAuthHandler", | ||||
b"HTTPPasswordMgrWithDefaultRealm", | b"HTTPPasswordMgrWithDefaultRealm", | ||||
b"ProxyHandler", | b"ProxyHandler", | ||||
b"Request", | b"Request", | ||||
b"url2pathname", | b"url2pathname", | ||||
b"urlopen", | b"urlopen", | ||||
), | ), | ||||
) | ) | ||||
import urllib.response | |||||
urlreq._registeraliases( | urlreq._registeraliases( | ||||
urllib.response, | urllib.response, | ||||
( | ( | ||||
b"addclosehook", | b"addclosehook", | ||||
b"addinfourl", | b"addinfourl", | ||||
), | ), | ||||
) | ) | ||||
import urllib.error | |||||
urlerr._registeraliases( | urlerr._registeraliases( | ||||
urllib.error, | urllib.error, | ||||
( | ( | ||||
b"HTTPError", | b"HTTPError", | ||||
b"URLError", | b"URLError", | ||||
), | ), | ||||
) | ) | ||||
import http.server | |||||
httpserver._registeraliases( | httpserver._registeraliases( | ||||
http.server, | http.server, | ||||
( | ( | ||||
b"HTTPServer", | b"HTTPServer", | ||||
b"BaseHTTPRequestHandler", | b"BaseHTTPRequestHandler", | ||||
b"SimpleHTTPRequestHandler", | b"SimpleHTTPRequestHandler", | ||||
b"CGIHTTPRequestHandler", | b"CGIHTTPRequestHandler", | ||||
), | ), | ||||
) | ) | ||||
# urllib.parse.quote() accepts both str and bytes, decodes bytes | # urllib.parse.quote() accepts both str and bytes, decodes bytes | ||||
# (if necessary), and returns str. This is wonky. We provide a custom | # (if necessary), and returns str. This is wonky. We provide a custom | ||||
# implementation that only accepts bytes and emits bytes. | # implementation that only accepts bytes and emits bytes. | ||||
def quote(s, safe='/'): | def quote(s, safe='/'): | ||||
# bytestr has an __iter__ that emits characters. quote_from_bytes() | # bytestr has an __iter__ that emits characters. quote_from_bytes() | ||||
# does an iteration and expects ints. We coerce to bytes to appease it. | # does an iteration and expects ints. We coerce to bytes to appease it. | ||||
if isinstance(s, pycompat.bytestr): | if isinstance(s, pycompat.bytestr): | ||||
s = bytes(s) | s = bytes(s) | ||||
s = urllib.parse.quote_from_bytes(s, safe=safe) | s = urllib.parse.quote_from_bytes(s, safe=safe) | ||||
return s.encode('ascii', 'strict') | return s.encode('ascii', 'strict') | ||||
# urllib.parse.urlencode() returns str. We use this function to make | # urllib.parse.urlencode() returns str. We use this function to make | ||||
# sure we return bytes. | # sure we return bytes. | ||||
def urlencode(query, doseq=False): | def urlencode(query, doseq=False): | ||||
s = urllib.parse.urlencode(query, doseq=doseq) | s = urllib.parse.urlencode(query, doseq=doseq) | ||||
return s.encode('ascii') | return s.encode('ascii') | ||||
urlreq.quote = quote | urlreq.quote = quote | ||||
urlreq.urlencode = urlencode | urlreq.urlencode = urlencode | ||||
def getfullurl(req): | def getfullurl(req): | ||||
return req.full_url | return req.full_url | ||||
def gethost(req): | def gethost(req): | ||||
return req.host | return req.host | ||||
def getselector(req): | def getselector(req): | ||||
return req.selector | return req.selector | ||||
def getdata(req): | def getdata(req): | ||||
return req.data | return req.data | ||||
def hasdata(req): | |||||
return req.data is not None | |||||
else: | |||||
# pytype: disable=import-error | |||||
import BaseHTTPServer | |||||
import CGIHTTPServer | |||||
import SimpleHTTPServer | |||||
import urllib2 | |||||
import urllib | |||||
import urlparse | |||||
# pytype: enable=import-error | |||||
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",)) | |||||
def gethost(req): | |||||
return req.get_host() | |||||
def getselector(req): | |||||
return req.get_selector() | |||||
def getfullurl(req): | |||||
return req.get_full_url() | |||||
def getdata(req): | |||||
return req.get_data() | |||||
def hasdata(req): | def hasdata(req): | ||||
return req.has_data() | return req.data is not None |