fix: write UTF-8 to stdout.buffer to avoid UnicodeEncodeError on non-UTF-8 systems#1790
Open
octo-patch wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: write UTF-8 to stdout.buffer to avoid UnicodeEncodeError on non-UTF-8 systems#1790octo-patch wants to merge 1 commit intomicrosoft:mainfrom
octo-patch wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…UTF-8 systems On Windows systems with a non-UTF-8 locale (e.g. GBK on Chinese Windows), running `markitdown file.pdf > output.md` raises: UnicodeEncodeError: 'gbk' codec can't encode character '\u2022' Two problems existed in the previous approach of encoding to sys.stdout.encoding with errors='replace': 1. sys.stdout.encoding can be None when stdout is a raw pipe, causing a TypeError. 2. Characters are silently replaced with '?' (lossy output), which is undesirable when redirecting to a file. Fix by writing UTF-8 encoded bytes directly to sys.stdout.buffer when available. This produces lossless UTF-8 output regardless of the system locale, matching the behaviour of the -o/--output flag. A safe fallback handles the rare case where stdout.buffer is absent. Fixes microsoft#1788
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.
Fixes #1788
Problem
On Windows systems with a non-UTF-8 locale (e.g. GBK on Chinese Windows), running:
raises a
UnicodeEncodeError:The previous approach of encoding to
sys.stdout.encodingwitherrors='replace'had two remaining issues:sys.stdout.encodingcan beNonewhen stdout is a raw pipe, causing aTypeErrorinstead of a graceful failure.'?'(lossy output), which is undesirable when redirecting to a file.Solution
Write UTF-8 encoded bytes directly to
sys.stdout.bufferwhen available. This produces lossless UTF-8 output regardless of the system locale encoding, consistent with the behavior of the-o/--outputflag (which already writes UTF-8 explicitly).A safe fallback handles the rare case where
stdout.bufferis not available (e.g. some embedded or wrapped stdout objects), using the locale encoding witherrors='replace'and guarding againstNoneencoding.Testing
sys.stdout.encoding is Nonecase without raisingTypeError> file.md) on systems with non-UTF-8 locale encoding