Skip to content

Commit 39e315a

Browse files
committed
Make it work with replaced stdout
1 parent cf35636 commit 39e315a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

mypy/stubtest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,11 +2107,12 @@ def safe_print(text: str) -> None:
21072107
# If `sys.stdout` encoding is not the same as out (usually UTF8) string,
21082108
# if may cause painful crashes. I don't want to reconfigure `sys.stdout`
21092109
# to do `errors = "replace"` as that sounds scary.
2110-
print(
2111-
text.encode(sys.stdout.encoding, errors="replace").decode(
2112-
sys.stdout.encoding, errors="replace"
2113-
)
2114-
)
2110+
out_encoding = sys.stdout.encoding
2111+
if out_encoding is not None:
2112+
# Can be None if stdout is replaced (including our own tests). This should be
2113+
# safe to omit if the actual stream doesn't care about encoding.
2114+
text = text.encode(out_encoding, errors="replace").decode(out_encoding, errors="replace")
2115+
print(text)
21152116

21162117

21172118
def parse_options(args: list[str]) -> _Arguments:

0 commit comments

Comments
 (0)