1 from __future__
import unicode_literals
17 import urllib
.request
as compat_urllib_request
18 except ImportError: # Python 2
19 import urllib2
as compat_urllib_request
22 import urllib
.error
as compat_urllib_error
23 except ImportError: # Python 2
24 import urllib2
as compat_urllib_error
27 import urllib
.parse
as compat_urllib_parse
28 except ImportError: # Python 2
29 import urllib
as compat_urllib_parse
32 from urllib
.parse
import urlparse
as compat_urllib_parse_urlparse
33 except ImportError: # Python 2
34 from urlparse
import urlparse
as compat_urllib_parse_urlparse
37 import urllib
.parse
as compat_urlparse
38 except ImportError: # Python 2
39 import urlparse
as compat_urlparse
42 import http
.cookiejar
as compat_cookiejar
43 except ImportError: # Python 2
44 import cookielib
as compat_cookiejar
47 import http
.cookies
as compat_cookies
48 except ImportError: # Python 2
49 import Cookie
as compat_cookies
52 import html
.entities
as compat_html_entities
53 except ImportError: # Python 2
54 import htmlentitydefs
as compat_html_entities
57 import http
.client
as compat_http_client
58 except ImportError: # Python 2
59 import httplib
as compat_http_client
62 from urllib
.error
import HTTPError
as compat_HTTPError
63 except ImportError: # Python 2
64 from urllib2
import HTTPError
as compat_HTTPError
67 from urllib
.request
import urlretrieve
as compat_urlretrieve
68 except ImportError: # Python 2
69 from urllib
import urlretrieve
as compat_urlretrieve
73 from subprocess
import DEVNULL
74 compat_subprocess_get_DEVNULL
= lambda: DEVNULL
76 compat_subprocess_get_DEVNULL
= lambda: open(os
.path
.devnull
, 'w')
79 import http
.server
as compat_http_server
81 import BaseHTTPServer
as compat_http_server
84 compat_str
= unicode # Python 2
89 from urllib
.parse
import unquote_to_bytes
as compat_urllib_parse_unquote_to_bytes
90 from urllib
.parse
import unquote
as compat_urllib_parse_unquote
91 from urllib
.parse
import unquote_plus
as compat_urllib_parse_unquote_plus
92 except ImportError: # Python 2
93 _asciire
= (compat_urllib_parse
._asciire
if hasattr(compat_urllib_parse
, '_asciire')
94 else re
.compile('([\x00-\x7f]+)'))
96 # HACK: The following are the correct unquote_to_bytes, unquote and unquote_plus
97 # implementations from cpython 3.4.3's stdlib. Python 2's version
98 # is apparently broken (see https://github.com/rg3/youtube-dl/pull/6244)
100 def compat_urllib_parse_unquote_to_bytes(string
):
101 """unquote_to_bytes('abc%20def') -> b'abc def'."""
102 # Note: strings are encoded as UTF-8. This is only an issue if it contains
103 # unescaped non-ASCII characters, which URIs should not.
105 # Is it a string-like object?
108 if isinstance(string
, compat_str
):
109 string
= string
.encode('utf-8')
110 bits
= string
.split(b
'%')
115 for item
in bits
[1:]:
117 append(compat_urllib_parse
._hextochr
[item
[:2]])
124 def compat_urllib_parse_unquote(string
, encoding
='utf-8', errors
='replace'):
125 """Replace %xx escapes by their single-character equivalent. The optional
126 encoding and errors parameters specify how to decode percent-encoded
127 sequences into Unicode characters, as accepted by the bytes.decode()
129 By default, percent-encoded sequences are decoded with UTF-8, and invalid
130 sequences are replaced by a placeholder character.
132 unquote('abc%20def') -> 'abc def'.
134 if '%' not in string
:
141 bits
= _asciire
.split(string
)
144 for i
in range(1, len(bits
), 2):
145 append(compat_urllib_parse_unquote_to_bytes(bits
[i
]).decode(encoding
, errors
))
149 def compat_urllib_parse_unquote_plus(string
, encoding
='utf-8', errors
='replace'):
150 """Like unquote(), but also replace plus signs by spaces, as required for
151 unquoting HTML form values.
153 unquote_plus('%7e/abc+def') -> '~/abc def'
155 string
= string
.replace('+', ' ')
156 return compat_urllib_parse_unquote(string
, encoding
, errors
)
159 compat_basestring
= basestring
# Python 2
161 compat_basestring
= str
164 compat_chr
= unichr # Python 2
169 from xml
.etree
.ElementTree
import ParseError
as compat_xml_parse_error
170 except ImportError: # Python 2.6
171 from xml
.parsers
.expat
import ExpatError
as compat_xml_parse_error
175 from urllib
.parse
import parse_qs
as compat_parse_qs
176 except ImportError: # Python 2
177 # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
178 # Python 2's version is apparently totally broken
180 def _parse_qsl(qs
, keep_blank_values
=False, strict_parsing
=False,
181 encoding
='utf-8', errors
='replace'):
182 qs
, _coerce_result
= qs
, compat_str
183 pairs
= [s2
for s1
in qs
.split('&') for s2
in s1
.split(';')]
185 for name_value
in pairs
:
186 if not name_value
and not strict_parsing
:
188 nv
= name_value
.split('=', 1)
191 raise ValueError("bad query field: %r" % (name_value
,))
192 # Handle case of a control-name with no equal sign
193 if keep_blank_values
:
197 if len(nv
[1]) or keep_blank_values
:
198 name
= nv
[0].replace('+', ' ')
199 name
= compat_urllib_parse_unquote(
200 name
, encoding
=encoding
, errors
=errors
)
201 name
= _coerce_result(name
)
202 value
= nv
[1].replace('+', ' ')
203 value
= compat_urllib_parse_unquote(
204 value
, encoding
=encoding
, errors
=errors
)
205 value
= _coerce_result(value
)
206 r
.append((name
, value
))
209 def compat_parse_qs(qs
, keep_blank_values
=False, strict_parsing
=False,
210 encoding
='utf-8', errors
='replace'):
212 pairs
= _parse_qsl(qs
, keep_blank_values
, strict_parsing
,
213 encoding
=encoding
, errors
=errors
)
214 for name
, value
in pairs
:
215 if name
in parsed_result
:
216 parsed_result
[name
].append(value
)
218 parsed_result
[name
] = [value
]
222 from shlex
import quote
as shlex_quote
223 except ImportError: # Python < 3.3
225 if re
.match(r
'^[-_\w./]+$', s
):
228 return "'" + s
.replace("'", "'\"'\"'") + "'"
231 if sys
.version_info
>= (2, 7, 3):
232 compat_shlex_split
= shlex
.split
234 # Working around shlex issue with unicode strings on some python 2
235 # versions (see http://bugs.python.org/issue1548891)
236 def compat_shlex_split(s
, comments
=False, posix
=True):
237 if isinstance(s
, compat_str
):
238 s
= s
.encode('utf-8')
239 return shlex
.split(s
, comments
, posix
)
249 if sys
.version_info
>= (3, 0):
250 compat_getenv
= os
.getenv
251 compat_expanduser
= os
.path
.expanduser
253 # Environment variables should be decoded with filesystem encoding.
254 # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
256 def compat_getenv(key
, default
=None):
257 from .utils
import get_filesystem_encoding
258 env
= os
.getenv(key
, default
)
260 env
= env
.decode(get_filesystem_encoding())
263 # HACK: The default implementations of os.path.expanduser from cpython do not decode
264 # environment variables with filesystem encoding. We will work around this by
265 # providing adjusted implementations.
266 # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
267 # for different platforms with correct environment variables decoding.
269 if os
.name
== 'posix':
270 def compat_expanduser(path
):
271 """Expand ~ and ~user constructions. If user or $HOME is unknown,
273 if not path
.startswith('~'):
275 i
= path
.find('/', 1)
279 if 'HOME' not in os
.environ
:
281 userhome
= pwd
.getpwuid(os
.getuid()).pw_dir
283 userhome
= compat_getenv('HOME')
287 pwent
= pwd
.getpwnam(path
[1:i
])
290 userhome
= pwent
.pw_dir
291 userhome
= userhome
.rstrip('/')
292 return (userhome
+ path
[i
:]) or '/'
293 elif os
.name
== 'nt' or os
.name
== 'ce':
294 def compat_expanduser(path
):
295 """Expand ~ and ~user constructs.
297 If user or $HOME is unknown, do nothing."""
301 while i
< n
and path
[i
] not in '/\\':
304 if 'HOME' in os
.environ
:
305 userhome
= compat_getenv('HOME')
306 elif 'USERPROFILE' in os
.environ
:
307 userhome
= compat_getenv('USERPROFILE')
308 elif 'HOMEPATH' not in os
.environ
:
312 drive
= compat_getenv('HOMEDRIVE')
315 userhome
= os
.path
.join(drive
, compat_getenv('HOMEPATH'))
318 userhome
= os
.path
.join(os
.path
.dirname(userhome
), path
[1:i
])
320 return userhome
+ path
[i
:]
322 compat_expanduser
= os
.path
.expanduser
325 if sys
.version_info
< (3, 0):
327 from .utils
import preferredencoding
328 print(s
.encode(preferredencoding(), 'xmlcharrefreplace'))
331 assert isinstance(s
, compat_str
)
336 subprocess_check_output
= subprocess
.check_output
337 except AttributeError:
338 def subprocess_check_output(*args
, **kwargs
):
339 assert 'input' not in kwargs
340 p
= subprocess
.Popen(*args
, stdout
=subprocess
.PIPE
, **kwargs
)
341 output
, _
= p
.communicate()
344 raise subprocess
.CalledProcessError(ret
, p
.args
, output
=output
)
347 if sys
.version_info
< (3, 0) and sys
.platform
== 'win32':
348 def compat_getpass(prompt
, *args
, **kwargs
):
349 if isinstance(prompt
, compat_str
):
350 from .utils
import preferredencoding
351 prompt
= prompt
.encode(preferredencoding())
352 return getpass
.getpass(prompt
, *args
, **kwargs
)
354 compat_getpass
= getpass
.getpass
356 # Old 2.6 and 2.7 releases require kwargs to be bytes
360 _testfunc(**{'x': 0}
)
362 def compat_kwargs(kwargs
):
363 return dict((bytes(k
), v
) for k
, v
in kwargs
.items())
365 compat_kwargs
= lambda kwargs
: kwargs
368 if sys
.version_info
< (2, 7):
369 def compat_socket_create_connection(address
, timeout
, source_address
=None):
372 for res
in socket
.getaddrinfo(host
, port
, 0, socket
.SOCK_STREAM
):
373 af
, socktype
, proto
, canonname
, sa
= res
376 sock
= socket
.socket(af
, socktype
, proto
)
377 sock
.settimeout(timeout
)
379 sock
.bind(source_address
)
382 except socket
.error
as _
:
389 raise socket
.error("getaddrinfo returns an empty list")
391 compat_socket_create_connection
= socket
.create_connection
394 # Fix https://github.com/rg3/youtube-dl/issues/4223
395 # See http://bugs.python.org/issue9161 for what is broken
396 def workaround_optparse_bug9161():
397 op
= optparse
.OptionParser()
398 og
= optparse
.OptionGroup(op
, 'foo')
402 real_add_option
= optparse
.OptionGroup
.add_option
404 def _compat_add_option(self
, *args
, **kwargs
):
406 v
.encode('ascii', 'replace') if isinstance(v
, compat_str
)
408 bargs
= [enc(a
) for a
in args
]
410 (k
, enc(v
)) for k
, v
in kwargs
.items())
411 return real_add_option(self
, *bargs
, **bkwargs
)
412 optparse
.OptionGroup
.add_option
= _compat_add_option
414 if hasattr(shutil
, 'get_terminal_size'): # Python >= 3.3
415 compat_get_terminal_size
= shutil
.get_terminal_size
417 _terminal_size
= collections
.namedtuple('terminal_size', ['columns', 'lines'])
419 def compat_get_terminal_size(fallback
=(80, 24)):
420 columns
= compat_getenv('COLUMNS')
422 columns
= int(columns
)
425 lines
= compat_getenv('LINES')
431 if columns
is None or lines
is None or columns
<= 0 or lines
<= 0:
433 sp
= subprocess
.Popen(
435 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
436 out
, err
= sp
.communicate()
437 _lines
, _columns
= map(int, out
.split())
439 _columns
, _lines
= _terminal_size(*fallback
)
441 if columns
is None or columns
<= 0:
443 if lines
is None or lines
<= 0:
445 return _terminal_size(columns
, lines
)
448 itertools
.count(start
=0, step
=1)
449 compat_itertools_count
= itertools
.count
450 except TypeError: # Python 2.6
451 def compat_itertools_count(start
=0, step
=1):
457 if sys
.version_info
>= (3, 0):
458 from tokenize
import tokenize
as compat_tokenize_tokenize
460 from tokenize
import generate_tokens
as compat_tokenize_tokenize
469 'compat_get_terminal_size',
472 'compat_html_entities',
473 'compat_http_client',
474 'compat_http_server',
475 'compat_itertools_count',
480 'compat_shlex_split',
481 'compat_socket_create_connection',
483 'compat_subprocess_get_DEVNULL',
484 'compat_tokenize_tokenize',
485 'compat_urllib_error',
486 'compat_urllib_parse',
487 'compat_urllib_parse_unquote',
488 'compat_urllib_parse_unquote_plus',
489 'compat_urllib_parse_unquote_to_bytes',
490 'compat_urllib_parse_urlparse',
491 'compat_urllib_request',
493 'compat_urlretrieve',
494 'compat_xml_parse_error',
496 'subprocess_check_output',
497 'workaround_optparse_bug9161',