Reduce assumptions on stdout
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 15 Sep 2021 08:07:10 +0000 (10:07 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 15 Sep 2021 08:07:10 +0000 (10:07 +0200)
We might be running in an environment that captures stdout
by using some replacement class. Do not fail in that case
but just disble coloring

src/text_helpers.py

index 3b21141..cd33cf0 100644 (file)
@@ -198,7 +198,12 @@ _COLOR_TO_CODE = dict(zip([COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, \
 
 _ANSI_ESCAPE_SURROUND = '\x1b[{}m{}\x1b[0m'
 
-_STDOUT_IS_TTY = stdout.isatty()
+# only color output if we are writing output to a terminal (not a file or so)
+try:
+    _STDOUT_IS_TTY = stdout.isatty()
+except Exception:
+    # stdout might be some wrapper around the real thing to capture output
+    _STDOUT_IS_TTY = False
 
 
 def colored(text, foreground=None, background=None, style=None):