Repository navigation
fix(bash): decode Windows command output in Python's UTF-8 mode - #3897
Merged
Merged
Conversation
decode_output() falls back to the Windows code page when a command's output is not valid UTF-8, and looked that code page up with locale.getpreferredencoding(). In UTF-8 mode (PYTHONUTF8=1, -X utf8, and the default from Python 3.15) that always returns "utf-8", so the fallback was skipped and GBK output came back as mojibake. Use locale.getencoding() when it exists; it returns the code page regardless of UTF-8 mode. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
|
Thanks! UTF-8 mode quietly skipping the code-page fallback is a subtle one, and |
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.
What does this PR do?
decode_output()falls back to the Windows code page when a command's output is not valid UTF-8. It looks that code page up withlocale.getpreferredencoding(False). In Python's UTF-8 mode (PYTHONUTF8=1,-X utf8, and the default from Python 3.15) that always returns"utf-8", so the fallback is skipped and code-page output comes back as mojibake.On a cp932 system with
-X utf8,echo テストthrough cmd.exe decodes as'�e�X�g\r\n'instead of'テスト\r\n'. Without UTF-8 mode it decodes correctly. On Chinese Windows the same happens with cp936/GBK output.The fix uses
locale.getencoding()(Python 3.11+), which returns the code page regardless of UTF-8 mode, and keepsgetpreferredencoding()for older Pythons.tests/test_bash_decode_utf8_mode.pysimulates UTF-8 mode on a cp936 system. The GBK case fails without the change and passes with it. The existingtests/test_bash_*.pyfiles still pass (88 passed, 31 skipped on Windows, Python 3.11).Type of change
Checklist
I used Claude Code to write this change.