Skip to content

Support running stubtest in non-UTF8 terminals #19085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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