X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=youtube_dl%2Fcompat.py;h=9d33a8ec5fbd80a75a5d382e8621f6dc616c7aa0;hb=dfd5313afdf00250d9b8a4e103bd7647713f1af3;hp=b3752634a08a5982e52353be0ba272b6aba1f202;hpb=451948b28c4c520e3146d36b6ffb3079449f22e1;p=youtube-dl.git diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index b3752634a..9d33a8ec5 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import getpass +import optparse import os import subprocess import sys @@ -288,6 +289,36 @@ if sys.version_info < (3, 0) and sys.platform == 'win32': else: compat_getpass = getpass.getpass +# Old 2.6 and 2.7 releases require kwargs to be bytes +try: + (lambda x: x)(**{'x': 0}) +except TypeError: + def compat_kwargs(kwargs): + return dict((bytes(k), v) for k, v in kwargs.items()) +else: + compat_kwargs = lambda kwargs: kwargs + + +# Fix https://github.com/rg3/youtube-dl/issues/4223 +# See http://bugs.python.org/issue9161 for what is broken +def workaround_optparse_bug9161(): + op = optparse.OptionParser() + og = optparse.OptionGroup(op, 'foo') + try: + og.add_option('-t') + except TypeError: + real_add_option = optparse.OptionGroup.add_option + + def _compat_add_option(self, *args, **kwargs): + enc = lambda v: ( + v.encode('ascii', 'replace') if isinstance(v, compat_str) + else v) + bargs = [enc(a) for a in args] + bkwargs = dict( + (k, enc(v)) for k, v in kwargs.items()) + return real_add_option(self, *bargs, **bkwargs) + optparse.OptionGroup.add_option = _compat_add_option + __all__ = [ 'compat_HTTPError', @@ -299,6 +330,7 @@ __all__ = [ 'compat_html_entities', 'compat_html_parser', 'compat_http_client', + 'compat_kwargs', 'compat_ord', 'compat_parse_qs', 'compat_print', @@ -314,4 +346,5 @@ __all__ = [ 'compat_xml_parse_error', 'shlex_quote', 'subprocess_check_output', + 'workaround_optparse_bug9161', ]