1 from __future__
import unicode_literals
13 import urllib
.request
as compat_urllib_request
14 except ImportError: # Python 2
15 import urllib2
as compat_urllib_request
18 import urllib
.error
as compat_urllib_error
19 except ImportError: # Python 2
20 import urllib2
as compat_urllib_error
23 import urllib
.parse
as compat_urllib_parse
24 except ImportError: # Python 2
25 import urllib
as compat_urllib_parse
28 from urllib
.parse
import urlparse
as compat_urllib_parse_urlparse
29 except ImportError: # Python 2
30 from urlparse
import urlparse
as compat_urllib_parse_urlparse
33 import urllib
.parse
as compat_urlparse
34 except ImportError: # Python 2
35 import urlparse
as compat_urlparse
38 import http
.cookiejar
as compat_cookiejar
39 except ImportError: # Python 2
40 import cookielib
as compat_cookiejar
43 import html
.entities
as compat_html_entities
44 except ImportError: # Python 2
45 import htmlentitydefs
as compat_html_entities
48 import html
.parser
as compat_html_parser
49 except ImportError: # Python 2
50 import HTMLParser
as compat_html_parser
53 import http
.client
as compat_http_client
54 except ImportError: # Python 2
55 import httplib
as compat_http_client
58 from urllib
.error
import HTTPError
as compat_HTTPError
59 except ImportError: # Python 2
60 from urllib2
import HTTPError
as compat_HTTPError
63 from urllib
.request
import urlretrieve
as compat_urlretrieve
64 except ImportError: # Python 2
65 from urllib
import urlretrieve
as compat_urlretrieve
69 from subprocess
import DEVNULL
70 compat_subprocess_get_DEVNULL
= lambda: DEVNULL
72 compat_subprocess_get_DEVNULL
= lambda: open(os
.path
.devnull
, 'w')
75 import http
.server
as compat_http_server
77 import BaseHTTPServer
as compat_http_server
80 from urllib
.parse
import unquote
as compat_urllib_parse_unquote
82 def compat_urllib_parse_unquote(string
, encoding
='utf-8', errors
='replace'):
85 res
= string
.split('%')
92 # pct_sequence: contiguous sequence of percent-encoded bytes, decoded
99 pct_sequence
+= item
[:2].decode('hex')
102 # This segment was just a single percent-encoded character.
103 # May be part of a sequence of code units, so delay decoding.
104 # (Stored in pct_sequence).
108 # Encountered non-percent-encoded characters. Flush the current
110 string
+= pct_sequence
.decode(encoding
, errors
) + rest
113 # Flush the final pct_sequence
114 string
+= pct_sequence
.decode(encoding
, errors
)
119 from urllib
.parse
import parse_qs
as compat_parse_qs
120 except ImportError: # Python 2
121 # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
122 # Python 2's version is apparently totally broken
124 def _parse_qsl(qs
, keep_blank_values
=False, strict_parsing
=False,
125 encoding
='utf-8', errors
='replace'):
126 qs
, _coerce_result
= qs
, unicode
127 pairs
= [s2
for s1
in qs
.split('&') for s2
in s1
.split(';')]
129 for name_value
in pairs
:
130 if not name_value
and not strict_parsing
:
132 nv
= name_value
.split('=', 1)
135 raise ValueError("bad query field: %r" % (name_value
,))
136 # Handle case of a control-name with no equal sign
137 if keep_blank_values
:
141 if len(nv
[1]) or keep_blank_values
:
142 name
= nv
[0].replace('+', ' ')
143 name
= compat_urllib_parse_unquote(
144 name
, encoding
=encoding
, errors
=errors
)
145 name
= _coerce_result(name
)
146 value
= nv
[1].replace('+', ' ')
147 value
= compat_urllib_parse_unquote(
148 value
, encoding
=encoding
, errors
=errors
)
149 value
= _coerce_result(value
)
150 r
.append((name
, value
))
153 def compat_parse_qs(qs
, keep_blank_values
=False, strict_parsing
=False,
154 encoding
='utf-8', errors
='replace'):
156 pairs
= _parse_qsl(qs
, keep_blank_values
, strict_parsing
,
157 encoding
=encoding
, errors
=errors
)
158 for name
, value
in pairs
:
159 if name
in parsed_result
:
160 parsed_result
[name
].append(value
)
162 parsed_result
[name
] = [value
]
166 compat_str
= unicode # Python 2
171 compat_chr
= unichr # Python 2
176 from xml
.etree
.ElementTree
import ParseError
as compat_xml_parse_error
177 except ImportError: # Python 2.6
178 from xml
.parsers
.expat
import ExpatError
as compat_xml_parse_error
181 from shlex
import quote
as shlex_quote
182 except ImportError: # Python < 3.3
184 if re
.match(r
'^[-_\w./]+$', s
):
187 return "'" + s
.replace("'", "'\"'\"'") + "'"
197 if sys
.version_info
>= (3, 0):
198 compat_getenv
= os
.getenv
199 compat_expanduser
= os
.path
.expanduser
201 # Environment variables should be decoded with filesystem encoding.
202 # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
204 def compat_getenv(key
, default
=None):
205 from .utils
import get_filesystem_encoding
206 env
= os
.getenv(key
, default
)
208 env
= env
.decode(get_filesystem_encoding())
211 # HACK: The default implementations of os.path.expanduser from cpython do not decode
212 # environment variables with filesystem encoding. We will work around this by
213 # providing adjusted implementations.
214 # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
215 # for different platforms with correct environment variables decoding.
217 if os
.name
== 'posix':
218 def compat_expanduser(path
):
219 """Expand ~ and ~user constructions. If user or $HOME is unknown,
221 if not path
.startswith('~'):
223 i
= path
.find('/', 1)
227 if 'HOME' not in os
.environ
:
229 userhome
= pwd
.getpwuid(os
.getuid()).pw_dir
231 userhome
= compat_getenv('HOME')
235 pwent
= pwd
.getpwnam(path
[1:i
])
238 userhome
= pwent
.pw_dir
239 userhome
= userhome
.rstrip('/')
240 return (userhome
+ path
[i
:]) or '/'
241 elif os
.name
== 'nt' or os
.name
== 'ce':
242 def compat_expanduser(path
):
243 """Expand ~ and ~user constructs.
245 If user or $HOME is unknown, do nothing."""
249 while i
< n
and path
[i
] not in '/\\':
252 if 'HOME' in os
.environ
:
253 userhome
= compat_getenv('HOME')
254 elif 'USERPROFILE' in os
.environ
:
255 userhome
= compat_getenv('USERPROFILE')
256 elif 'HOMEPATH' not in os
.environ
:
260 drive
= compat_getenv('HOMEDRIVE')
263 userhome
= os
.path
.join(drive
, compat_getenv('HOMEPATH'))
266 userhome
= os
.path
.join(os
.path
.dirname(userhome
), path
[1:i
])
268 return userhome
+ path
[i
:]
270 compat_expanduser
= os
.path
.expanduser
273 if sys
.version_info
< (3, 0):
275 from .utils
import preferredencoding
276 print(s
.encode(preferredencoding(), 'xmlcharrefreplace'))
279 assert isinstance(s
, compat_str
)
284 subprocess_check_output
= subprocess
.check_output
285 except AttributeError:
286 def subprocess_check_output(*args
, **kwargs
):
287 assert 'input' not in kwargs
288 p
= subprocess
.Popen(*args
, stdout
=subprocess
.PIPE
, **kwargs
)
289 output
, _
= p
.communicate()
292 raise subprocess
.CalledProcessError(ret
, p
.args
, output
=output
)
295 if sys
.version_info
< (3, 0) and sys
.platform
== 'win32':
296 def compat_getpass(prompt
, *args
, **kwargs
):
297 if isinstance(prompt
, compat_str
):
298 from .utils
import preferredencoding
299 prompt
= prompt
.encode(preferredencoding())
300 return getpass
.getpass(prompt
, *args
, **kwargs
)
302 compat_getpass
= getpass
.getpass
304 # Old 2.6 and 2.7 releases require kwargs to be bytes
308 _testfunc(**{'x': 0}
)
310 def compat_kwargs(kwargs
):
311 return dict((bytes(k
), v
) for k
, v
in kwargs
.items())
313 compat_kwargs
= lambda kwargs
: kwargs
316 if sys
.version_info
< (2, 7):
317 def compat_socket_create_connection(address
, timeout
, source_address
=None):
320 for res
in socket
.getaddrinfo(host
, port
, 0, socket
.SOCK_STREAM
):
321 af
, socktype
, proto
, canonname
, sa
= res
324 sock
= socket
.socket(af
, socktype
, proto
)
325 sock
.settimeout(timeout
)
327 sock
.bind(source_address
)
330 except socket
.error
as _
:
337 raise socket
.error("getaddrinfo returns an empty list")
339 compat_socket_create_connection
= socket
.create_connection
342 # Fix https://github.com/rg3/youtube-dl/issues/4223
343 # See http://bugs.python.org/issue9161 for what is broken
344 def workaround_optparse_bug9161():
345 op
= optparse
.OptionParser()
346 og
= optparse
.OptionGroup(op
, 'foo')
350 real_add_option
= optparse
.OptionGroup
.add_option
352 def _compat_add_option(self
, *args
, **kwargs
):
354 v
.encode('ascii', 'replace') if isinstance(v
, compat_str
)
356 bargs
= [enc(a
) for a
in args
]
358 (k
, enc(v
)) for k
, v
in kwargs
.items())
359 return real_add_option(self
, *bargs
, **bkwargs
)
360 optparse
.OptionGroup
.add_option
= _compat_add_option
370 'compat_html_entities',
371 'compat_html_parser',
372 'compat_http_client',
373 'compat_http_server',
378 'compat_socket_create_connection',
380 'compat_subprocess_get_DEVNULL',
381 'compat_urllib_error',
382 'compat_urllib_parse',
383 'compat_urllib_parse_unquote',
384 'compat_urllib_parse_urlparse',
385 'compat_urllib_request',
387 'compat_urlretrieve',
388 'compat_xml_parse_error',
390 'subprocess_check_output',
391 'workaround_optparse_bug9161',