]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/compat.py
[downloader/fragment] Fix bugs around resuming with Range (#2901)
[yt-dlp.git] / yt_dlp / compat.py
index b107b211421c5113cfb255803876ca13ce03a9d2..2bc6a6b7fad04392a3f04246df5d95b9dd9e8d39 100644 (file)
@@ -2,6 +2,7 @@
 
 import asyncio
 import base64
+import collections
 import ctypes
 import getpass
 import html
@@ -19,6 +20,7 @@
 import shutil
 import socket
 import struct
+import subprocess
 import sys
 import tokenize
 import urllib
@@ -132,6 +134,16 @@ def compat_asyncio_run(coro):
     asyncio.run = compat_asyncio_run
 
 
+try:  # >= 3.7
+    asyncio.tasks.all_tasks
+except AttributeError:
+    asyncio.tasks.all_tasks = asyncio.tasks.Task.all_tasks
+
+try:
+    import websockets as compat_websockets
+except ImportError:
+    compat_websockets = None
+
 # Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
 # See https://github.com/yt-dlp/yt-dlp/issues/792
 # https://docs.python.org/3/library/os.path.html#os.path.expanduser
@@ -159,24 +171,37 @@ def compat_expanduser(path):
         compat_pycrypto_AES = None
 
 
+WINDOWS_VT_MODE = False if compat_os_name == 'nt' else None
+
+
 def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.python.org/issue30075
     if compat_os_name != 'nt':
         return
-    os.system('')
+    global WINDOWS_VT_MODE
+    startupinfo = subprocess.STARTUPINFO()
+    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+    try:
+        subprocess.Popen('', shell=True, startupinfo=startupinfo)
+        WINDOWS_VT_MODE = True
+    except Exception:
+        pass
 
 
 #  Deprecated
 
 compat_basestring = str
 compat_chr = chr
+compat_filter = filter
 compat_input = input
 compat_integer_types = (int, )
 compat_kwargs = lambda kwargs: kwargs
+compat_map = map
 compat_numeric_types = (int, float, complex)
 compat_str = str
 compat_xpath = lambda xpath: xpath
 compat_zip = zip
 
+compat_collections_abc = collections.abc
 compat_HTMLParser = html.parser.HTMLParser
 compat_HTTPError = urllib.error.HTTPError
 compat_Struct = struct.Struct
@@ -223,6 +248,7 @@ def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.pytho
 # Set public objects
 
 __all__ = [
+    'WINDOWS_VT_MODE',
     'compat_HTMLParseError',
     'compat_HTMLParser',
     'compat_HTTPError',
@@ -233,6 +259,7 @@ def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.pytho
     'compat_b64decode',
     'compat_basestring',
     'compat_chr',
+    'compat_collections_abc',
     'compat_cookiejar',
     'compat_cookiejar_Cookie',
     'compat_cookies',
@@ -242,6 +269,7 @@ def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.pytho
     'compat_etree_fromstring',
     'compat_etree_register_namespace',
     'compat_expanduser',
+    'compat_filter',
     'compat_get_terminal_size',
     'compat_getenv',
     'compat_getpass',
@@ -253,6 +281,7 @@ def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.pytho
     'compat_integer_types',
     'compat_itertools_count',
     'compat_kwargs',
+    'compat_map',
     'compat_numeric_types',
     'compat_ord',
     'compat_os_name',
@@ -284,6 +313,7 @@ def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.pytho
     'compat_urllib_response',
     'compat_urlparse',
     'compat_urlretrieve',
+    'compat_websockets',
     'compat_xml_parse_error',
     'compat_xpath',
     'compat_zip',