]> gitweb @ CieloNegro.org - youtube-dl.git/blobdiff - youtube_dl/compat.py
Work around 2.7.0 deficencies (Fixes #4223)
[youtube-dl.git] / youtube_dl / compat.py
index b3752634a08a5982e52353be0ba272b6aba1f202..64a97548933eadec42aa578583ce878ce2cc1e9e 100644 (file)
@@ -1,6 +1,7 @@
 from __future__ import unicode_literals
 
 import getpass
+import optparse
 import os
 import subprocess
 import sys
@@ -288,6 +289,34 @@ 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():
+    try:
+        optparse.OptionGroup('foo').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 +328,7 @@ __all__ = [
     'compat_html_entities',
     'compat_html_parser',
     'compat_http_client',
+    'compat_kwargs',
     'compat_ord',
     'compat_parse_qs',
     'compat_print',
@@ -314,4 +344,5 @@ __all__ = [
     'compat_xml_parse_error',
     'shlex_quote',
     'subprocess_check_output',
+    'workaround_optparse_bug9161',
 ]