Skip to content

Commit 54fcc73

Browse files
committed
Be more conservative on patching stdout and stderr
1 parent 15ea7b9 commit 54fcc73

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

mondrian/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
# Patch standard output/error if it's not supporting unicode
2222
# See: https://stackoverflow.com/questions/27347772/print-unicode-string-in-python-regardless-of-environment
23-
if sys.stdout.encoding is None or sys.stdout.encoding == "ANSI_X3.4-1968":
24-
sys.stdout = codecs.getwriter("UTF-8")(sys.stdout.buffer, errors="replace")
25-
sys.stderr = codecs.getwriter("UTF-8")(sys.stderr.buffer, errors="replace")
23+
if getattr(sys.stdout, "encoding", None) is None or sys.stdout.encoding == "ANSI_X3.4-1968":
24+
if hasattr(sys.stdout, "buffer"):
25+
sys.stdout = codecs.getwriter("UTF-8")(sys.stdout.buffer, errors="replace")
26+
if hasattr(sys.err, "buffer"):
27+
sys.stderr = codecs.getwriter("UTF-8")(sys.stderr.buffer, errors="replace")
2628

2729

2830
def setup_excepthook():

0 commit comments

Comments
 (0)