From 73ec83f0febdf5015e18199abe42ee092b6e67ab Mon Sep 17 00:00:00 2001 From: Raul Tambre Date: Sat, 14 Sep 2019 08:22:52 +0000 Subject: [PATCH] setup_color: Don't output an error if GetConsoleMode fails Turns out GetConsoleMode fails if the user's console isn't native. This is sometimes the case for Git Bash. Also moved the checking code out of the if block so it applies in generic console cases too. This went unnoticed, because setup_color.py is currently only really used in Git scripts, which are piped. Bug: 1001187 Change-Id: I93357e479f84122f759e419d15c02500809657d3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1785605 Reviewed-by: Robbie Iannucci Reviewed-by: Edward Lesmes Reviewed-by: Marc-Antoine Ruel Commit-Queue: Raul Tambre Auto-Submit: Raul Tambre --- setup_color.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/setup_color.py b/setup_color.py index dfc60ec27..e866a6f48 100644 --- a/setup_color.py +++ b/setup_color.py @@ -25,15 +25,18 @@ def enable_native_ansi(): ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04 out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE) + + # GetConsoleMode fails if the terminal isn't native. mode = ctypes.wintypes.DWORD() if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0: - print('kernel32.GetConsoleMode failed') return False if not (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING): if kernel32.SetConsoleMode( out_handle, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0: - print('kernel32.SetConsoleMode to enable ANSI sequences failed') + print( + 'kernel32.SetConsoleMode to enable ANSI sequences failed', + file=sys.stderr) return False return True @@ -110,14 +113,15 @@ def init(): else: # A normal file, or an unknown file type. pass - - # Enable native ANSI color codes on Windows 10. - if IS_TTY and platform.release() == '10': - should_wrap = not enable_native_ansi() else: # This is non-windows, so we trust isatty. OUT_TYPE = 'pipe or file' + # Enable native ANSI color codes on Windows 10. + if IS_TTY and platform.release() == '10': + if enable_native_ansi(): + should_wrap = False + colorama.init(wrap=should_wrap) if __name__ == '__main__':