Skip to content

Commit cf35636

Browse files
committed
Support running stubtest in non-UTF8 terminals.
1 parent 81f6285 commit cf35636

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mypy/stubtest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ def warning_callback(msg: str) -> None:
20622062
if args.generate_allowlist:
20632063
generated_allowlist.add(error.object_desc)
20642064
continue
2065-
print(error.get_description(concise=args.concise))
2065+
safe_print(error.get_description(concise=args.concise))
20662066
error_count += 1
20672067

20682068
# Print unused allowlist entries
@@ -2102,6 +2102,18 @@ def warning_callback(msg: str) -> None:
21022102
return exit_code
21032103

21042104

2105+
def safe_print(text: str) -> None:
2106+
"""Print a text replacing chars not representable in stdout encoding."""
2107+
# If `sys.stdout` encoding is not the same as out (usually UTF8) string,
2108+
# if may cause painful crashes. I don't want to reconfigure `sys.stdout`
2109+
# 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+
)
2115+
2116+
21052117
def parse_options(args: list[str]) -> _Arguments:
21062118
parser = argparse.ArgumentParser(
21072119
description="Compares stubs to objects introspected from the runtime."

0 commit comments

Comments
 (0)