From f079514957401f49db30ec4cd25f8c8246b0c1de Mon Sep 17 00:00:00 2001 From: pukkandan Date: Tue, 3 Jan 2023 11:23:34 +0530 Subject: [PATCH] [utils] `windows_enable_vt_mode`: Better error handling Closes #5927 --- yt_dlp/YoutubeDL.py | 7 ++++++- yt_dlp/utils.py | 11 ++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 37964169f..1fb44e7f9 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -586,7 +586,6 @@ class YoutubeDL: self._playlist_urls = set() self.cache = Cache(self) - windows_enable_vt_mode() stdout = sys.stderr if self.params.get('logtostderr') else sys.stdout self._out_files = Namespace( out=stdout, @@ -595,6 +594,12 @@ class YoutubeDL: console=None if compat_os_name == 'nt' else next( filter(supports_terminal_sequences, (sys.stderr, sys.stdout)), None) ) + + try: + windows_enable_vt_mode() + except Exception as e: + self.write_debug(f'Failed to enable VT mode: {e}') + self._allow_colors = Namespace(**{ type_: not self.params.get('no_color') and supports_terminal_sequences(stream) for type_, stream in self._out_files.items_ if type_ != 'console' diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index a0ae12aea..0180954ef 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -5659,7 +5659,6 @@ def windows_enable_vt_mode(): dll = ctypes.WinDLL('kernel32', use_last_error=False) handle = os.open('CONOUT$', os.O_RDWR) - try: h_out = ctypes.wintypes.HANDLE(msvcrt.get_osfhandle(handle)) dw_original_mode = ctypes.wintypes.DWORD() @@ -5671,15 +5670,13 @@ def windows_enable_vt_mode(): dw_original_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) if not success: raise Exception('SetConsoleMode failed') - except Exception as e: - write_string(f'WARNING: Cannot enable VT mode - {e}') - else: - global WINDOWS_VT_MODE - WINDOWS_VT_MODE = True - supports_terminal_sequences.cache_clear() finally: os.close(handle) + global WINDOWS_VT_MODE + WINDOWS_VT_MODE = True + supports_terminal_sequences.cache_clear() + _terminal_sequences_re = re.compile('\033\\[[^m]+m')