##// END OF EJS Templates
urllibcompat: remove Python 2 support code...
Gregory Szorc -
r49751:4286ec1d default
parent child Browse files
Show More
@@ -5,6 +5,12 b''
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
7
8 import http.server
9 import urllib.error
10 import urllib.parse
11 import urllib.request
12 import urllib.response
13
8 from .pycompat import getattr
14 from .pycompat import getattr
9 from . import pycompat
15 from . import pycompat
10
16
@@ -39,199 +45,109 b' 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,
44
50 (
45 urlreq._registeraliases(
51 b"splitattr",
46 urllib.parse,
52 b"splitpasswd",
47 (
53 b"splitport",
48 b"splitattr",
54 b"splituser",
49 b"splitpasswd",
55 b"urlparse",
50 b"splitport",
56 b"urlunparse",
51 b"splituser",
57 ),
52 b"urlparse",
58 )
53 b"urlunparse",
59 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
54 ),
60 urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
55 )
61 urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
56 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
57 urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
58 urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
59 import urllib.request
60
61 urlreq._registeraliases(
62 urllib.request,
63 (
64 b"AbstractHTTPHandler",
65 b"BaseHandler",
66 b"build_opener",
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
62
94 urlerr._registeraliases(
63 urlreq._registeraliases(
95 urllib.error,
64 urllib.request,
96 (
65 (
97 b"HTTPError",
66 b"AbstractHTTPHandler",
98 b"URLError",
67 b"BaseHandler",
99 ),
68 b"build_opener",
100 )
69 b"FileHandler",
101 import http.server
70 b"FTPHandler",
102
71 b"ftpwrapper",
103 httpserver._registeraliases(
72 b"HTTPHandler",
104 http.server,
73 b"HTTPSHandler",
105 (
74 b"install_opener",
106 b"HTTPServer",
75 b"pathname2url",
107 b"BaseHTTPRequestHandler",
76 b"HTTPBasicAuthHandler",
108 b"SimpleHTTPRequestHandler",
77 b"HTTPDigestAuthHandler",
109 b"CGIHTTPRequestHandler",
78 b"HTTPPasswordMgrWithDefaultRealm",
110 ),
79 b"ProxyHandler",
111 )
80 b"Request",
112
81 b"url2pathname",
113 # urllib.parse.quote() accepts both str and bytes, decodes bytes
82 b"urlopen",
114 # (if necessary), and returns str. This is wonky. We provide a custom
83 ),
115 # implementation that only accepts bytes and emits bytes.
84 )
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 )
102
103 httpserver._registeraliases(
104 http.server,
105 (
106 b"HTTPServer",
107 b"BaseHTTPRequestHandler",
108 b"SimpleHTTPRequestHandler",
109 b"CGIHTTPRequestHandler",
110 ),
111 )
159
112
160 urlreq._registeraliases(
113 # urllib.parse.quote() accepts both str and bytes, decodes bytes
161 urllib,
114 # (if necessary), and returns str. This is wonky. We provide a custom
162 (
115 # implementation that only accepts bytes and emits bytes.
163 b"addclosehook",
116 def quote(s, safe='/'):
164 b"addinfourl",
117 # bytestr has an __iter__ that emits characters. quote_from_bytes()
165 b"ftpwrapper",
118 # does an iteration and expects ints. We coerce to bytes to appease it.
166 b"pathname2url",
119 if isinstance(s, pycompat.bytestr):
167 b"quote",
120 s = bytes(s)
168 b"splitattr",
121 s = urllib.parse.quote_from_bytes(s, safe=safe)
169 b"splitpasswd",
122 return s.encode('ascii', 'strict')
170 b"splitport",
123
171 b"splituser",
124
172 b"unquote",
125 # urllib.parse.urlencode() returns str. We use this function to make
173 b"url2pathname",
126 # sure we return bytes.
174 b"urlencode",
127 def urlencode(query, doseq=False):
175 ),
128 s = urllib.parse.urlencode(query, doseq=doseq)
176 )
129 return s.encode('ascii')
177 urlreq._registeraliases(
130
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
131
224 def gethost(req):
132 urlreq.quote = quote
225 return req.get_host()
133 urlreq.urlencode = urlencode
134
226
135
227 def getselector(req):
136 def getfullurl(req):
228 return req.get_selector()
137 return req.full_url
138
139
140 def gethost(req):
141 return req.host
229
142
230 def getfullurl(req):
143
231 return req.get_full_url()
144 def getselector(req):
145 return req.selector
146
232
147
233 def getdata(req):
148 def getdata(req):
234 return req.get_data()
149 return req.data
235
150
236 def hasdata(req):
151
237 return req.has_data()
152 def hasdata(req):
153 return req.data is not None
General Comments 0
You need to be logged in to leave comments. Login now