From 87f9dec5078c50b2fc75753c67be868b53ec2bf9 Mon Sep 17 00:00:00 2001 From: Brad Beattie <79596181+bbeattie-phxlabs@users.noreply.github.com> Date: Tue, 22 Mar 2022 01:06:42 -0400 Subject: [PATCH] Handle monkey-patched stdout (#404) * Handle monkey-patched stdout In some Python environments (e.g. Maya 2022's mayapy.exe), sys.stdout has been monkey-patched to something other than the expected stdout. As such, it doesn't actually have an `isatty` method and raises an AttributeError in response. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/structlog/_config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/structlog/_config.py b/src/structlog/_config.py index 536494b1..2f072c72 100644 --- a/src/structlog/_config.py +++ b/src/structlog/_config.py @@ -40,7 +40,10 @@ set_exc_info, TimeStamper(fmt="%Y-%m-%d %H:%M.%S", utc=False), ConsoleRenderer( - colors=_use_colors and sys.stdout is not None and sys.stdout.isatty() + colors=_use_colors + and sys.stdout is not None + and hasattr(sys.stdout, "isatty") + and sys.stdout.isatty() ), ] _BUILTIN_DEFAULT_CONTEXT_CLASS = cast(Type[Context], dict)