1 from __future__ import unicode_literals
10 import urllib.request as compat_urllib_request
11 except ImportError: # Python 2
12 import urllib2 as compat_urllib_request
15 import urllib.error as compat_urllib_error
16 except ImportError: # Python 2
17 import urllib2 as compat_urllib_error
20 import urllib.parse as compat_urllib_parse
21 except ImportError: # Python 2
22 import urllib as compat_urllib_parse
25 from urllib.parse import urlparse as compat_urllib_parse_urlparse
26 except ImportError: # Python 2
27 from urlparse import urlparse as compat_urllib_parse_urlparse
30 import urllib.parse as compat_urlparse
31 except ImportError: # Python 2
32 import urlparse as compat_urlparse
35 import http.cookiejar as compat_cookiejar
36 except ImportError: # Python 2
37 import cookielib as compat_cookiejar
40 import html.entities as compat_html_entities
41 except ImportError: # Python 2
42 import htmlentitydefs as compat_html_entities
45 import html.parser as compat_html_parser
46 except ImportError: # Python 2
47 import HTMLParser as compat_html_parser
50 import http.client as compat_http_client
51 except ImportError: # Python 2
52 import httplib as compat_http_client
55 from urllib.error import HTTPError as compat_HTTPError
56 except ImportError: # Python 2
57 from urllib2 import HTTPError as compat_HTTPError
60 from urllib.request import urlretrieve as compat_urlretrieve
61 except ImportError: # Python 2
62 from urllib import urlretrieve as compat_urlretrieve
66 from subprocess import DEVNULL
67 compat_subprocess_get_DEVNULL = lambda: DEVNULL
69 compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w')
72 from urllib.parse import unquote as compat_urllib_parse_unquote
74 def compat_urllib_parse_unquote(string, encoding='utf-8', errors='replace'):
77 res = string.split('%')
84 # pct_sequence: contiguous sequence of percent-encoded bytes, decoded
91 pct_sequence += item[:2].decode('hex')
94 # This segment was just a single percent-encoded character.
95 # May be part of a sequence of code units, so delay decoding.
96 # (Stored in pct_sequence).
100 # Encountered non-percent-encoded characters. Flush the current
102 string += pct_sequence.decode(encoding, errors) + rest
105 # Flush the final pct_sequence
106 string += pct_sequence.decode(encoding, errors)
111 from urllib.parse import parse_qs as compat_parse_qs
112 except ImportError: # Python 2
113 # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
114 # Python 2's version is apparently totally broken
116 def _parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
117 encoding='utf-8', errors='replace'):
118 qs, _coerce_result = qs, unicode
119 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
121 for name_value in pairs:
122 if not name_value and not strict_parsing:
124 nv = name_value.split('=', 1)
127 raise ValueError("bad query field: %r" % (name_value,))
128 # Handle case of a control-name with no equal sign
129 if keep_blank_values:
133 if len(nv[1]) or keep_blank_values:
134 name = nv[0].replace('+', ' ')
135 name = compat_urllib_parse_unquote(
136 name, encoding=encoding, errors=errors)
137 name = _coerce_result(name)
138 value = nv[1].replace('+', ' ')
139 value = compat_urllib_parse_unquote(
140 value, encoding=encoding, errors=errors)
141 value = _coerce_result(value)
142 r.append((name, value))
145 def compat_parse_qs(qs, keep_blank_values=False, strict_parsing=False,
146 encoding='utf-8', errors='replace'):
148 pairs = _parse_qsl(qs, keep_blank_values, strict_parsing,
149 encoding=encoding, errors=errors)
150 for name, value in pairs:
151 if name in parsed_result:
152 parsed_result[name].append(value)
154 parsed_result[name] = [value]
158 compat_str = unicode # Python 2
163 compat_chr = unichr # Python 2
168 from xml.etree.ElementTree import ParseError as compat_xml_parse_error
169 except ImportError: # Python 2.6
170 from xml.parsers.expat import ExpatError as compat_xml_parse_error
173 from shlex import quote as shlex_quote
174 except ImportError: # Python < 3.3
176 return "'" + s.replace("'", "'\"'\"'") + "'"
180 if type(c) is int: return c
184 if sys.version_info >= (3, 0):
185 compat_getenv = os.getenv
186 compat_expanduser = os.path.expanduser
188 # Environment variables should be decoded with filesystem encoding.
189 # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
191 def compat_getenv(key, default=None):
192 from .utils import get_filesystem_encoding
193 env = os.getenv(key, default)
195 env = env.decode(get_filesystem_encoding())
198 # HACK: The default implementations of os.path.expanduser from cpython do not decode
199 # environment variables with filesystem encoding. We will work around this by
200 # providing adjusted implementations.
201 # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
202 # for different platforms with correct environment variables decoding.
204 if os.name == 'posix':
205 def compat_expanduser(path):
206 """Expand ~ and ~user constructions. If user or $HOME is unknown,
208 if not path.startswith('~'):
210 i = path.find('/', 1)
214 if 'HOME' not in os.environ:
216 userhome = pwd.getpwuid(os.getuid()).pw_dir
218 userhome = compat_getenv('HOME')
222 pwent = pwd.getpwnam(path[1:i])
225 userhome = pwent.pw_dir
226 userhome = userhome.rstrip('/')
227 return (userhome + path[i:]) or '/'
228 elif os.name == 'nt' or os.name == 'ce':
229 def compat_expanduser(path):
230 """Expand ~ and ~user constructs.
232 If user or $HOME is unknown, do nothing."""
236 while i < n and path[i] not in '/\\':
239 if 'HOME' in os.environ:
240 userhome = compat_getenv('HOME')
241 elif 'USERPROFILE' in os.environ:
242 userhome = compat_getenv('USERPROFILE')
243 elif not 'HOMEPATH' in os.environ:
247 drive = compat_getenv('HOMEDRIVE')
250 userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
253 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
255 return userhome + path[i:]
257 compat_expanduser = os.path.expanduser
260 if sys.version_info < (3, 0):
262 from .utils import preferredencoding
263 print(s.encode(preferredencoding(), 'xmlcharrefreplace'))
266 assert type(s) == type(u'')
271 subprocess_check_output = subprocess.check_output
272 except AttributeError:
273 def subprocess_check_output(*args, **kwargs):
274 assert 'input' not in kwargs
275 p = subprocess.Popen(*args, stdout=subprocess.PIPE, **kwargs)
276 output, _ = p.communicate()
279 raise subprocess.CalledProcessError(ret, p.args, output=output)
282 if sys.version_info < (3, 0) and sys.platform == 'win32':
283 def compat_getpass(prompt, *args, **kwargs):
284 if isinstance(prompt, compat_str):
285 from .utils import preferredencoding
286 prompt = prompt.encode(preferredencoding())
287 return getpass.getpass(prompt, *args, **kwargs)
289 compat_getpass = getpass.getpass
291 # Old 2.6 and 2.7 releases require kwargs to be bytes
293 (lambda x: x)(**{'x': 0})
295 def compat_kwargs(kwargs):
296 return dict((bytes(k), v) for k, v in kwargs.items())
298 compat_kwargs = lambda kwargs: kwargs
307 'compat_html_entities',
308 'compat_html_parser',
309 'compat_http_client',
315 'compat_subprocess_get_DEVNULL',
316 'compat_urllib_error',
317 'compat_urllib_parse',
318 'compat_urllib_parse_unquote',
319 'compat_urllib_parse_urlparse',
320 'compat_urllib_request',
322 'compat_urlretrieve',
323 'compat_xml_parse_error',
325 'subprocess_check_output',