fix(cli): force UTF-8 stdout/stderr on Windows to prevent UnicodeEncodeError#2817
Merged
mnriem merged 1 commit intoJun 3, 2026
Merged
Conversation
…deError On Windows, when stdout/stderr are not a UTF-8 TTY (output piped, redirected to a file, or running under a legacy code page such as cp1252), Rich cannot encode the banner and box-drawing glyphs, so the CLI aborts with a UnicodeEncodeError traceback instead of printing. This breaks basic commands like `specify --help` and `specify version` whenever their output is captured rather than written to an interactive terminal. Reconfigure sys.stdout/sys.stderr to UTF-8 with errors="replace" at the main() entry point on win32 so output degrades gracefully instead of crashing. The change is a no-op on POSIX, is guarded by try/except so it can never make stream setup worse, and lives at the CLI entry point only -- importing specify_cli as a library does not touch global streams. Verified on Windows 11 (cp1252): `specify --help` piped and `specify version` redirected to a file both render correctly and exit 0 without setting PYTHONUTF8 / PYTHONIOENCODING. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents UnicodeEncodeError crashes on Windows when specify output is redirected/piped or when the console uses a non‑UTF‑8 code page by reconfiguring sys.stdout/sys.stderr to UTF‑8 at the CLI entry point.
Changes:
- On Windows (
sys.platform == "win32"), callreconfigure(encoding="utf-8", errors="replace")onsys.stdoutandsys.stderrbefore invoking the Typer app. - Guard stream reconfiguration with
try/exceptso unusual stream setups don’t introduce new failures.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/__init__.py |
Reconfigures stdout/stderr to UTF‑8 on Windows at main() startup to avoid UnicodeEncodeError when printing Rich UI output. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
mnriem
approved these changes
Jun 3, 2026
Collaborator
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On Windows,
specifycrashes withUnicodeEncodeErrorwhenever stdout/stderrare not a UTF-8 TTY — i.e. when output is piped, redirected to a file, or the
console uses a legacy code page (e.g. cp1252). Rich renders the ASCII-art
banner and box-drawing characters, and the default Windows text encoding can't
encode them, so even
specify --helpandspecify versionabort with atraceback instead of printing.
Repro (Windows, default code page):
Fix: at the
main()entry point, onwin32only, reconfiguresys.stdout/sys.stderrto UTF-8 witherrors="replace"so output degradesgracefully instead of crashing.
try/except (AttributeError, ValueError, OSError)so unusualstream setups (already-wrapped/detached streams) can never make things worse.
specify_clias a library doesnot mutate global streams.
Testing
uv run specify --helpuv sync && uv run pytestOn Windows 11, cp1252 console, without
PYTHONUTF8/PYTHONIOENCODING:specify --helppiped → renders banner + option boxes, exit 0 (was:UnicodeEncodeError).specify version > out.txt→ renders correctly, exit 0 (was: traceback).Full suite: 3217 passed, 119 skipped. The 11 failures are pre-existing and
unrelated to this change —
os.symlink()calls in test setup raiseOSError [WinError 1314]because the Windows host lacks symlink-createprivilege; they pass on POSIX / elevated Windows.
AI Disclosure
Diagnosed, implemented, and verified with Claude (Claude Code); reviewed before
submission. Reflected in the commit's
Co-Authored-Bytrailer.