Skip to content

fix(cli): prevent UnicodeEncodeError in doctor on GBK consoles - #11731

Closed
konsisumer wants to merge 1 commit into
NousResearch:mainfrom
konsisumer:fix/cli-doctor-unicode-encoding-7537
Closed

konsisumer wants to merge 1 commit into
NousResearch:mainfrom
konsisumer:fix/cli-doctor-unicode-encoding-7537

Conversation

@konsisumer

Copy link
Copy Markdown
Contributor

Summary

  • hermes doctor prints emoji (🩺, 🎉) and box-drawing chars on every run. On Windows consoles whose default encoding isn't UTF-8 (GBK on zh-CN, cp1252 elsewhere), the very first emoji raises UnicodeEncodeError: 'gbk' codec can't encode character '\U0001fa7a' and the command aborts before any check runs.
  • Fix: at the top of run_doctor, reconfigure sys.stdout/sys.stderr with errors='replace' — and switch to UTF-8 when the current encoding can't handle emoji. Unencodable characters become ? instead of crashing, so diagnostic output still works on legacy Windows consoles.
  • Streams without reconfigure (e.g. a redirected StringIO) and reconfigure failures are silently ignored, so the helper is safe under capture.

Fixes #7537.

Test plan

  • New TestStdoutUnicodeSafety unit tests cover: non-UTF-8 stream is reconfigured to utf-8 + errors='replace'; UTF-8 stream only gets errors='replace'; streams without reconfigure are skipped; reconfigure failures are swallowed.
  • All 25 tests in tests/hermes_cli/test_doctor.py pass (21 existing + 4 new), validated in a Docker container (python:3.12-slim).
  • Manual reproduction on a Windows GBK shell (requires a Windows box — not available here).

…GBK consoles

`hermes doctor` prints emoji (🩺, 🎉) and box-drawing characters on every
run. On Windows consoles whose default encoding is not UTF-8 (GBK on
zh-CN systems, cp1252 elsewhere), emitting 🩺 raises UnicodeEncodeError
and the command aborts before running any check.

Reconfigure sys.stdout/sys.stderr with errors='replace' (and switch to
UTF-8 where the current encoding can't handle emoji) at the top of
run_doctor. Unencodable chars become '?' instead of crashing, so the
diagnostic output still works on legacy Windows consoles.

Fixes NousResearch#7537.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Windows GBK encoding causes UnicodeEncodeError in doctor command

1 participant