forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio_utf8.py
More file actions
23 lines (20 loc) · 899 Bytes
/
Copy pathstdio_utf8.py
File metadata and controls
23 lines (20 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Force UTF-8 stdio regardless of the OS locale.
Windows embedded Python defaults stdout/stderr/stdin to the active console
codepage (cp1252, cp932, ...). Any print() containing non-ASCII characters
(e.g. "→", "…") then crashes with UnicodeEncodeError, and reading UTF-8
output under a legacy codepage can kill pipe reader threads. Reconfiguring
all three streams to UTF-8 makes Modly and extension workers agree with the
UTF-8 pipe readers used on the Electron side.
"""
import sys
def ensure_utf8_stdio() -> None:
"""Reconfigure stdin/stdout/stderr to UTF-8 when the streams support it."""
for stream in (sys.stdin, sys.stdout, sys.stderr):
reconfigure = getattr(stream, "reconfigure", None)
if reconfigure is None:
continue
try:
reconfigure(encoding="utf-8", errors="replace")
except (ValueError, OSError):
pass