A feature-rich command-line audio/video downloader
[utils] Fix bug in 0b9c08b47bb5e95c21b067044ace4e824d19a9c2
pukkandan53973b4d2cb3
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex 749cf9402a..3dc11463c3 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -33,7 +33,6 @@ from .compat import (compat_str,compat_urllib_error,compat_urllib_request,- windows_enable_vt_mode,)from .cookies import load_cookiesfrom .downloader import FFmpegFD, get_suitable_downloader, shorten_protocol_name@@ -142,6 +141,7 @@ from .utils import (url_basename,variadic,version_tuple,+ windows_enable_vt_mode,write_json_file,write_string,)@@ -3605,7 +3605,7 @@ class YoutubeDL: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 locallyret += ' (No VT)' if WINDOWS_VT_MODE is False else ' (No ANSI)'return retdiff --git a/yt_dlp/compat/__init__.py b/yt_dlp/compat/__init__.pyindex a0cd621104..c02e843d49 100644--- a/yt_dlp/compat/__init__.py+++ b/yt_dlp/compat/__init__.py@@ -1,6 +1,4 @@-import contextlibimport os-import subprocessimport sysimport warningsimport xml.etree.ElementTree as etree@@ -74,17 +72,3 @@ if compat_os_name in ('nt', 'ce'):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 = Truediff --git a/yt_dlp/compat/_legacy.py b/yt_dlp/compat/_legacy.pyindex ce24760e5f..c4d95e1fbd 100644--- a/yt_dlp/compat/_legacy.py+++ b/yt_dlp/compat/_legacy.py@@ -55,3 +55,10 @@ compat_xml_parse_error = etree.ParseErrorcompat_xpath = lambda xpath: xpathcompat_zip = zipworkaround_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)diff --git a/yt_dlp/utils.py b/yt_dlp/utils.pyindex 0274e330d3..78789b1c5b 100644--- a/yt_dlp/utils.py+++ b/yt_dlp/utils.py@@ -5094,10 +5094,12 @@ def jwt_decode_hs256(jwt):return payload_data+WINDOWS_VT_MODE = False if compat_os_name == 'nt' else None++@functools.cachedef supports_terminal_sequences(stream):if compat_os_name == 'nt':- from .compat import WINDOWS_VT_MODE # Must be imported locallyif not WINDOWS_VT_MODE or get_windows_version() < (10, 0, 10586):return Falseelif 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')