Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ def warning_callback(msg: str) -> None:
if args.generate_allowlist:
generated_allowlist.add(error.object_desc)
continue
print(error.get_description(concise=args.concise))
safe_print(error.get_description(concise=args.concise))
error_count += 1

# Print unused allowlist entries
Expand Down Expand Up @@ -2102,6 +2102,19 @@ def warning_callback(msg: str) -> None:
return exit_code


def safe_print(text: str) -> None:
"""Print a text replacing chars not representable in stdout encoding."""
# If `sys.stdout` encoding is not the same as out (usually UTF8) string,
# if may cause painful crashes. I don't want to reconfigure `sys.stdout`
# to do `errors = "replace"` as that sounds scary.
out_encoding = sys.stdout.encoding
if out_encoding is not None:
# Can be None if stdout is replaced (including our own tests). This should be
# safe to omit if the actual stream doesn't care about encoding.
text = text.encode(out_encoding, errors="replace").decode(out_encoding, errors="replace")
print(text)


def parse_options(args: list[str]) -> _Arguments:
parser = argparse.ArgumentParser(
description="Compares stubs to objects introspected from the runtime."
Expand Down