X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=youtube_dl%2Fcompat.py;h=497ca52de14e59aabbc5587a0c6238e02dd37a48;hb=ec4161a57d66c92ea7460ac44dda13aadfb85f21;hp=cd46693b34f4746284e74b36af3bddd7ba8155ee;hpb=16040f46d64bad8dcc5f948288ef469dd787d3d3;p=youtube-dl.git diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index cd46693b3..497ca52de 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -1,11 +1,10 @@ from __future__ import unicode_literals -import ctypes import getpass import optparse import os -import platform import re +import socket import subprocess import sys @@ -72,6 +71,11 @@ try: except ImportError: compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w') +try: + import http.server as compat_http_server +except ImportError: + import BaseHTTPServer as compat_http_server + try: from urllib.parse import unquote as compat_urllib_parse_unquote except ImportError: @@ -299,7 +303,9 @@ else: # Old 2.6 and 2.7 releases require kwargs to be bytes try: - (lambda x: x)(**{'x': 0}) + def _testfunc(x): + pass + _testfunc(**{'x': 0}) except TypeError: def compat_kwargs(kwargs): return dict((bytes(k), v) for k, v in kwargs.items()) @@ -307,6 +313,32 @@ else: compat_kwargs = lambda kwargs: kwargs +if sys.version_info < (2, 7): + def compat_socket_create_connection(address, timeout, source_address=None): + host, port = address + err = None + for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): + af, socktype, proto, canonname, sa = res + sock = None + try: + sock = socket.socket(af, socktype, proto) + sock.settimeout(timeout) + if source_address: + sock.bind(source_address) + sock.connect(sa) + return sock + except socket.error as _: + err = _ + if sock is not None: + sock.close() + if err is not None: + raise err + else: + raise socket.error("getaddrinfo returns an empty list") +else: + compat_socket_create_connection = socket.create_connection + + # Fix https://github.com/rg3/youtube-dl/issues/4223 # See http://bugs.python.org/issue9161 for what is broken def workaround_optparse_bug9161(): @@ -328,22 +360,6 @@ def workaround_optparse_bug9161(): optparse.OptionGroup.add_option = _compat_add_option -if platform.python_implementation() == 'PyPy': - # PyPy expects byte strings as Windows function names - # https://github.com/rg3/youtube-dl/pull/4392 - def compat_WINFUNCTYPE(*args, **kwargs): - real = ctypes.WINFUNCTYPE(*args, **kwargs) - - def resf(tpl, *args, **kwargs): - funcname, dll = tpl - return real((str(funcname), dll), *args, **kwargs) - - return resf -else: - def compat_WINFUNCTYPE(*args, **kwargs): - return ctypes.WINFUNCTYPE(*args, **kwargs) - - __all__ = [ 'compat_HTTPError', 'compat_chr', @@ -354,10 +370,12 @@ __all__ = [ 'compat_html_entities', 'compat_html_parser', 'compat_http_client', + 'compat_http_server', 'compat_kwargs', 'compat_ord', 'compat_parse_qs', 'compat_print', + 'compat_socket_create_connection', 'compat_str', 'compat_subprocess_get_DEVNULL', 'compat_urllib_error', @@ -367,7 +385,6 @@ __all__ = [ 'compat_urllib_request', 'compat_urlparse', 'compat_urlretrieve', - 'compat_WINFUNCTYPE', 'compat_xml_parse_error', 'shlex_quote', 'subprocess_check_output',