Skip to content

Don't crash on characters the output can't encode (Windows pipes and files) - #140

Merged
markbac merged 1 commit into
mainfrom
fix-console-encoding
Oct 17, 2026
Merged

markbac merged 1 commit into
mainfrom
fix-console-encoding

Conversation

@markbac

@markbac markbac commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Summary

On Windows, a pipe or a file gets the ANSI code page (cp1252 and the like), not UTF-8. When a lint finding quoted an emoji or an arrow from a document, py-doc lint ... > report.txt stopped partway with UnicodeEncodeError and exit status 1. This happened on markbac.github.io's glossary and blog.

Every console command (py-doc and the old py-doc2docx, py-doc2slides, py-doclint and py-c4-exporter) now calls safe_console() first. It sets errors="replace" on stdout and stderr when they aren't UTF-8, so such a character prints as ?. UTF-8 output (Linux, macOS, CI) and Windows console windows are unchanged.

Testing

  • New tests/test_console.py: a cp1252 stream gets ? instead of an exception, and a UTF-8 stream is left strict.
  • The repro: py-doc lint content/glossary > file on Windows crashed before and now exits 0.
  • Full suite: 1030 passed, 13 skipped. ruff check: clean.

🤖 Generated with Claude Code

On Windows, output sent to a pipe or a file uses the ANSI code page, so
an emoji or an arrow quoted from a document raised UnicodeEncodeError
partway through `py-doc lint` (and any other command) when its output was
redirected. Every console command now sets errors="replace" on stdout
and stderr when they are not UTF-8, so such a character prints as "?".
UTF-8 output and console windows are left as they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@markbac
markbac merged commit 8f9c7fb into main Oct 17, 2026
6 checks passed
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.

1 participant