From: Christian Herdtweck Date: Wed, 15 Sep 2021 08:07:10 +0000 (+0200) Subject: Reduce assumptions on stdout X-Git-Tag: v1.6.7~6 X-Git-Url: http://developer.intra2net.com/git/?p=pyi2ncommon;a=commitdiff_plain;h=49439cda58447f920a04396b236986d86eddab99 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 --- 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):