]> jfr.im git - yt-dlp.git/commitdiff
[utils] Fix bug in 0b9c08b47bb5e95c21b067044ace4e824d19a9c2
authorpukkandan <redacted>
Thu, 19 May 2022 21:32:25 +0000 (03:02 +0530)
committerpukkandan <redacted>
Fri, 20 May 2022 00:31:09 +0000 (06:01 +0530)
* Cache of `supports_terminal_sequences` must be reset after enabling VT mode
* and move `windows_enable_vt_mode` to utils to avoid cyclic imports

yt_dlp/YoutubeDL.py
yt_dlp/compat/__init__.py
yt_dlp/compat/_legacy.py
yt_dlp/utils.py

index 749cf9402a5d731b7f0c50007eab1e828907859c..3dc11463c372007c630e137998d3dd4e61b7f2d0 100644 (file)
@@ -33,7 +33,6 @@
     compat_str,
     compat_urllib_error,
     compat_urllib_request,
-    windows_enable_vt_mode,
 )
 from .cookies import load_cookies
 from .downloader import FFmpegFD, get_suitable_downloader, shorten_protocol_name
     url_basename,
     variadic,
     version_tuple,
+    windows_enable_vt_mode,
     write_json_file,
     write_string,
 )
@@ -3605,7 +3605,7 @@ def print_debug_header(self):
         def get_encoding(stream):
             ret = str(getattr(stream, 'encoding', 'missing (%s)' % type(stream).__name__))
             if not supports_terminal_sequences(stream):
-                from .compat import WINDOWS_VT_MODE  # Must be imported locally
+                from .utils import WINDOWS_VT_MODE  # Must be imported locally
                 ret += ' (No VT)' if WINDOWS_VT_MODE is False else ' (No ANSI)'
             return ret
 
index a0cd621104a555a9ab8caa346ee39f4c6ba8ff37..c02e843d49d19d074ba751ec7ab319ab1c39c43d 100644 (file)
@@ -1,6 +1,4 @@
-import contextlib
 import os
-import subprocess
 import sys
 import warnings
 import xml.etree.ElementTree as etree
@@ -74,17 +72,3 @@ def compat_expanduser(path):
         return userhome + path[i:]
 else:
     compat_expanduser = os.path.expanduser
-
-
-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
-    global WINDOWS_VT_MODE
-    startupinfo = subprocess.STARTUPINFO()
-    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
-    with contextlib.suppress(Exception):
-        subprocess.Popen('', shell=True, startupinfo=startupinfo).wait()
-        WINDOWS_VT_MODE = True
index ce24760e5fd811d4d4f62b9250c063c77f1c94a5..c4d95e1fbd1f02b16641fc7e696dbf6bc3c957be 100644 (file)
@@ -55,3 +55,10 @@ def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
 compat_xpath = lambda xpath: xpath
 compat_zip = zip
 workaround_optparse_bug9161 = lambda: None
+
+
+def __getattr__(name):
+    if name in ('WINDOWS_VT_MODE', 'windows_enable_vt_mode'):
+        from .. import utils
+        return getattr(utils, name)
+    raise AttributeError(name)
index 0274e330d3fb6287e1329843154f55a91c2fe98d..78789b1c5b05af0d8ca5a6ac3e9d18f573922dd3 100644 (file)
@@ -5094,10 +5094,12 @@ def jwt_decode_hs256(jwt):
     return payload_data
 
 
+WINDOWS_VT_MODE = False if compat_os_name == 'nt' else None
+
+
 @functools.cache
 def supports_terminal_sequences(stream):
     if compat_os_name == 'nt':
-        from .compat import WINDOWS_VT_MODE  # Must be imported locally
         if not WINDOWS_VT_MODE or get_windows_version() < (10, 0, 10586):
             return False
     elif not os.getenv('TERM'):
@@ -5108,6 +5110,21 @@ def supports_terminal_sequences(stream):
         return False
 
 
+def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.python.org/issue30075
+    if compat_os_name != 'nt':
+        return
+    global WINDOWS_VT_MODE
+    startupinfo = subprocess.STARTUPINFO()
+    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+    try:
+        subprocess.Popen('', shell=True, startupinfo=startupinfo).wait()
+    except Exception:
+        return
+
+    WINDOWS_VT_MODE = True
+    supports_terminal_sequences.cache_clear()
+
+
 _terminal_sequences_re = re.compile('\033\\[[^m]+m')