瀏覽代碼

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 <iannucci@chromium.org>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Marc-Antoine Ruel <maruel@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
Auto-Submit: Raul Tambre <raul@tambre.ee>
Raul Tambre 6 年之前
父節點
當前提交
73ec83f0fe
共有 1 個文件被更改,包括 10 次插入6 次删除
  1. 10 6
      setup_color.py

+ 10 - 6
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__':