Browse Source

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 years ago
parent
commit
73ec83f0fe
1 changed files with 10 additions and 6 deletions
  1. 10 6
      setup_color.py

+ 10 - 6
setup_color.py

@@ -25,15 +25,18 @@ def enable_native_ansi():
   ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
   ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
 
 
   out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE)
   out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE)
+
+  # GetConsoleMode fails if the terminal isn't native.
   mode = ctypes.wintypes.DWORD()
   mode = ctypes.wintypes.DWORD()
   if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
   if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
-    print('kernel32.GetConsoleMode failed')
     return False
     return False
 
 
   if not (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING):
   if not (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING):
     if kernel32.SetConsoleMode(
     if kernel32.SetConsoleMode(
         out_handle, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
         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 False
 
 
   return True
   return True
@@ -110,14 +113,15 @@ def init():
     else:
     else:
       # A normal file, or an unknown file type.
       # A normal file, or an unknown file type.
       pass
       pass
-
-    # Enable native ANSI color codes on Windows 10.
-    if IS_TTY and platform.release() == '10':
-      should_wrap = not enable_native_ansi()
   else:
   else:
     # This is non-windows, so we trust isatty.
     # This is non-windows, so we trust isatty.
     OUT_TYPE = 'pipe or file'
     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)
   colorama.init(wrap=should_wrap)
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':