From 49439cda58447f920a04396b236986d86eddab99 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Wed, 15 Sep 2021 10:07:10 +0200 Subject: [PATCH] Reduce assumptions on stdout 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 | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-) diff --git a/src/text_helpers.py b/src/text_helpers.py index 3b21141..cd33cf0 100644 --- a/src/text_helpers.py +++ b/src/text_helpers.py @@ -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): -- 1.7.1