Skip to content

Commit

Permalink
logger: fix UnicodeEncodeError with non-utf8 stdout in frozen envs (#617
Browse files Browse the repository at this point in the history
)

logger: fix UnicodeEncodeError with non-utf8 stdout in frozen envs

Co-authored-by: glwnd <glwnd2030@gmail.com>
  • Loading branch information
glowinthedark and glwnd authored Jan 10, 2025
1 parent 1d19700 commit 4a6556a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pyglossary/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ def emit(self, record: logging.LogRecord) -> None:
print(f"fp=None, levelname={record.levelname}") # noqa: T201
print(msg) # noqa: T201
return
fp.write(msg + "\n")
encoding = getattr(fp, "encoding", "utf-8") or "utf-8"
try:
fp.write(msg + "\n")
except UnicodeEncodeError:
fp.write(
(msg + "\n")
.encode(encoding, errors="xmlcharrefreplace")
.decode(encoding)
)
fp.flush()


Expand Down

0 comments on commit 4a6556a

Please sign in to comment.