Repository navigation
FIX: Use UTF-8 for redirected CLI input and output - #486
Open
Zephyr4772 wants to merge 1 commit into
Open
Zephyr4772 wants to merge 1 commit into
Zephyr4772 wants to merge 1 commit into
Conversation
The CLI reads files as UTF-8, but Python uses the locale encoding (e.g. cp1252 on Windows) for standard streams that are not a terminal. So `markdown-it doc.md > doc.html` raised UnicodeEncodeError for characters outside it, and `--stdin` decoded UTF-8 input as cp1252. When converting files or stdin, redirected stdout now writes UTF-8 and redirected stdin is read as UTF-8, ignoring undecodable bytes like files. PYTHONIOENCODING is kept and a terminal is left as is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
On Windows,
markdown-it doc.md > doc.htmlfails withUnicodeEncodeError: 'charmap' codec can't encode characterswhen the Markdown contains characters outside cp1252 (e.g. Gujarati,ʤ,✓). The CLI reads files as UTF-8, but Python writes a redirected or piped stdout in the locale encoding.--stdinhas the matching problem on input: piped UTF-8 is decoded as cp1252, so the parser sees mojibake, which only round-trips back to the right bytes on output.When converting files or stdin,
main()now reconfigures a redirected stdout to UTF-8, and a redirected stdin to UTF-8 witherrors="ignore", the same asconvert_filereads files. An encoding set withPYTHONIOENCODINGis kept, and interactive and terminal use is unchanged.Tests use stand-ins for cp1252 streams. Three of the new tests (file to stdout, stdin, undecodable stdin bytes) fail on
master, and one checksPYTHONIOENCODINGis kept. On Windows (Python 3.12) the suite passes except two existingpytest-regressionsfixture tests that fail withoutPYTHONUTF8=1onmastertoo. Ruff 0.17.4 and mypy 2.4.1 pass.I used Claude Code to help write this change; I reviewed and tested it.