comparison mercurial/urllibcompat.py @ 48896:4286ec1d9842

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
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 21 Feb 2022 10:35:20 -0700
parents 6000f5b25c9b
children 642e31cb55f0
comparison
equal deleted inserted replaced
48895:62baa61efe8f 48896:4286ec1d9842
2 # 2 #
3 # Copyright 2017 Google, Inc. 3 # Copyright 2017 Google, Inc.
4 # 4 #
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7
8 import http.server
9 import urllib.error
10 import urllib.parse
11 import urllib.request
12 import urllib.response
7 13
8 from .pycompat import getattr 14 from .pycompat import getattr
9 from . import pycompat 15 from . import pycompat
10 16
11 _sysstr = pycompat.sysstr 17 _sysstr = pycompat.sysstr
37 43
38 httpserver = _pycompatstub() 44 httpserver = _pycompatstub()
39 urlreq = _pycompatstub() 45 urlreq = _pycompatstub()
40 urlerr = _pycompatstub() 46 urlerr = _pycompatstub()
41 47
42 if pycompat.ispy3: 48 urlreq._registeraliases(
43 import urllib.parse 49 urllib.parse,
50 (
51 b"splitattr",
52 b"splitpasswd",
53 b"splitport",
54 b"splituser",
55 b"urlparse",
56 b"urlunparse",
57 ),
58 )
59 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
60 urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
61 urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
44 62
45 urlreq._registeraliases( 63 urlreq._registeraliases(
46 urllib.parse, 64 urllib.request,
47 ( 65 (
48 b"splitattr", 66 b"AbstractHTTPHandler",
49 b"splitpasswd", 67 b"BaseHandler",
50 b"splitport", 68 b"build_opener",
51 b"splituser", 69 b"FileHandler",
52 b"urlparse", 70 b"FTPHandler",
53 b"urlunparse", 71 b"ftpwrapper",
54 ), 72 b"HTTPHandler",
55 ) 73 b"HTTPSHandler",
56 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs") 74 b"install_opener",
57 urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl") 75 b"pathname2url",
58 urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote") 76 b"HTTPBasicAuthHandler",
59 import urllib.request 77 b"HTTPDigestAuthHandler",
60 78 b"HTTPPasswordMgrWithDefaultRealm",
61 urlreq._registeraliases( 79 b"ProxyHandler",
62 urllib.request, 80 b"Request",
63 ( 81 b"url2pathname",
64 b"AbstractHTTPHandler", 82 b"urlopen",
65 b"BaseHandler", 83 ),
66 b"build_opener", 84 )
67 b"FileHandler",
68 b"FTPHandler",
69 b"ftpwrapper",
70 b"HTTPHandler",
71 b"HTTPSHandler",
72 b"install_opener",
73 b"pathname2url",
74 b"HTTPBasicAuthHandler",
75 b"HTTPDigestAuthHandler",
76 b"HTTPPasswordMgrWithDefaultRealm",
77 b"ProxyHandler",
78 b"Request",
79 b"url2pathname",
80 b"urlopen",
81 ),
82 )
83 import urllib.response
84
85 urlreq._registeraliases(
86 urllib.response,
87 (
88 b"addclosehook",
89 b"addinfourl",
90 ),
91 )
92 import urllib.error
93
94 urlerr._registeraliases(
95 urllib.error,
96 (
97 b"HTTPError",
98 b"URLError",
99 ),
100 )
101 import http.server
102
103 httpserver._registeraliases(
104 http.server,
105 (
106 b"HTTPServer",
107 b"BaseHTTPRequestHandler",
108 b"SimpleHTTPRequestHandler",
109 b"CGIHTTPRequestHandler",
110 ),
111 )
112
113 # urllib.parse.quote() accepts both str and bytes, decodes bytes
114 # (if necessary), and returns str. This is wonky. We provide a custom
115 # implementation that only accepts bytes and emits bytes.
116 def quote(s, safe='/'):
117 # bytestr has an __iter__ that emits characters. quote_from_bytes()
118 # does an iteration and expects ints. We coerce to bytes to appease it.
119 if isinstance(s, pycompat.bytestr):
120 s = bytes(s)
121 s = urllib.parse.quote_from_bytes(s, safe=safe)
122 return s.encode('ascii', 'strict')
123
124 # urllib.parse.urlencode() returns str. We use this function to make
125 # sure we return bytes.
126 def urlencode(query, doseq=False):
127 s = urllib.parse.urlencode(query, doseq=doseq)
128 return s.encode('ascii')
129
130 urlreq.quote = quote
131 urlreq.urlencode = urlencode
132
133 def getfullurl(req):
134 return req.full_url
135
136 def gethost(req):
137 return req.host
138
139 def getselector(req):
140 return req.selector
141
142 def getdata(req):
143 return req.data
144
145 def hasdata(req):
146 return req.data is not None
147 85
148 86
149 else: 87 urlreq._registeraliases(
150 # pytype: disable=import-error 88 urllib.response,
151 import BaseHTTPServer 89 (
152 import CGIHTTPServer 90 b"addclosehook",
153 import SimpleHTTPServer 91 b"addinfourl",
154 import urllib2 92 ),
155 import urllib 93 )
156 import urlparse
157 94
158 # pytype: enable=import-error 95 urlerr._registeraliases(
96 urllib.error,
97 (
98 b"HTTPError",
99 b"URLError",
100 ),
101 )
159 102
160 urlreq._registeraliases( 103 httpserver._registeraliases(
161 urllib, 104 http.server,
162 ( 105 (
163 b"addclosehook", 106 b"HTTPServer",
164 b"addinfourl", 107 b"BaseHTTPRequestHandler",
165 b"ftpwrapper", 108 b"SimpleHTTPRequestHandler",
166 b"pathname2url", 109 b"CGIHTTPRequestHandler",
167 b"quote", 110 ),
168 b"splitattr", 111 )
169 b"splitpasswd",
170 b"splitport",
171 b"splituser",
172 b"unquote",
173 b"url2pathname",
174 b"urlencode",
175 ),
176 )
177 urlreq._registeraliases(
178 urllib2,
179 (
180 b"AbstractHTTPHandler",
181 b"BaseHandler",
182 b"build_opener",
183 b"FileHandler",
184 b"FTPHandler",
185 b"HTTPBasicAuthHandler",
186 b"HTTPDigestAuthHandler",
187 b"HTTPHandler",
188 b"HTTPPasswordMgrWithDefaultRealm",
189 b"HTTPSHandler",
190 b"install_opener",
191 b"ProxyHandler",
192 b"Request",
193 b"urlopen",
194 ),
195 )
196 urlreq._registeraliases(
197 urlparse,
198 (
199 b"urlparse",
200 b"urlunparse",
201 ),
202 )
203 urlreq._registeralias(urlparse, b"parse_qs", b"parseqs")
204 urlreq._registeralias(urlparse, b"parse_qsl", b"parseqsl")
205 urlerr._registeraliases(
206 urllib2,
207 (
208 b"HTTPError",
209 b"URLError",
210 ),
211 )
212 httpserver._registeraliases(
213 BaseHTTPServer,
214 (
215 b"HTTPServer",
216 b"BaseHTTPRequestHandler",
217 ),
218 )
219 httpserver._registeraliases(
220 SimpleHTTPServer, (b"SimpleHTTPRequestHandler",)
221 )
222 httpserver._registeraliases(CGIHTTPServer, (b"CGIHTTPRequestHandler",))
223 112
224 def gethost(req): 113 # urllib.parse.quote() accepts both str and bytes, decodes bytes
225 return req.get_host() 114 # (if necessary), and returns str. This is wonky. We provide a custom
115 # implementation that only accepts bytes and emits bytes.
116 def quote(s, safe='/'):
117 # bytestr has an __iter__ that emits characters. quote_from_bytes()
118 # does an iteration and expects ints. We coerce to bytes to appease it.
119 if isinstance(s, pycompat.bytestr):
120 s = bytes(s)
121 s = urllib.parse.quote_from_bytes(s, safe=safe)
122 return s.encode('ascii', 'strict')
226 123
227 def getselector(req):
228 return req.get_selector()
229 124
230 def getfullurl(req): 125 # urllib.parse.urlencode() returns str. We use this function to make
231 return req.get_full_url() 126 # sure we return bytes.
127 def urlencode(query, doseq=False):
128 s = urllib.parse.urlencode(query, doseq=doseq)
129 return s.encode('ascii')
232 130
233 def getdata(req):
234 return req.get_data()
235 131
236 def hasdata(req): 132 urlreq.quote = quote
237 return req.has_data() 133 urlreq.urlencode = urlencode
134
135
136 def getfullurl(req):
137 return req.full_url
138
139
140 def gethost(req):
141 return req.host
142
143
144 def getselector(req):
145 return req.selector
146
147
148 def getdata(req):
149 return req.data
150
151
152 def hasdata(req):
153 return req.data is not None