Repository navigation
fix(cli): write redirected output as UTF-8 so EvoSci does not crash on Windows - #663
Conversation
…dows On Windows, Python writes to a pipe, a file or the NUL device in the ANSI code page, so the first character outside it (the check mark after a start, a box-drawing character, a path) stopped the command with UnicodeEncodeError, in EvoSci deploy > deploy.log for example. main() now switches stdout and stderr to UTF-8 before anything is printed, unless the user set PYTHONIOENCODING. Elsewhere a terminal keeps its locale encoding; on Windows every stream is switched, since isatty() is true for NUL as well and a console gets UTF-8 already. Each stream keeps its error handler. Closes EvoScientist#660
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
X-iZhang
left a comment
There was a problem hiding this comment.
Thanks for the fix and for checking it on Windows with a file, a pipe and NUL. One suggestion on the tests, inline in tests/test_cli_output_encoding.py.
Description
Closes #660.
main()now switches stdout and stderr to UTF-8 before anything is printed, as the issue proposes. APYTHONIOENCODINGthe user set is kept (an empty one counts as unset, since Python ignores it), and each stream keeps its error handler:reconfigure(encoding=...)alone would reset stderr'sbackslashreplacetostrict.One change from the proposal: on Windows
isatty()is also true for the NUL device, which a child gets from Node'sstdio: "ignore", and NUL still gets the code page. So on Windows the switch does not depend onisatty(); a console there is UTF-8 already. Elsewhere a terminal keeps its locale encoding.As the issue notes, a reader that decodes with the code page (PowerShell's pipe, unless
[Console]::OutputEncodingis UTF-8) now gets UTF-8, so box-drawing and other non-ASCII characters can show garbled there. The install scripts keep theirPYTHONIOENCODING, since they can install older releases.Tested: the new
tests/test_cli_output_encoding.pyfails onmainand passes here. On Windows, a script that runsmain()with an app printing✓exits 1 with the issue'sUnicodeEncodeErroronmain, with stdout sent to a file, a pipe or NUL, and exits 0 with UTF-8 output on this branch.Type of change
Checklist
uv run ruff check .passesuv run pytestpasses (on my Windows machine, 13 tests that need symlink privilege or a git config fail the same way onmain)Summary by CodeRabbit