Fix UnicodeEncodeError on Windows (cp1252 default)#8
Open
cnrdfrncs-droid wants to merge 1 commit into
Open
Conversation
watch.py prints common Unicode chars (arrow U+2192, em-dash) directly to
stdout/stderr. On Windows, both streams default to cp1252, which crashes
with UnicodeEncodeError when the script generates its final report or
focus-range header.
Repro on Windows:
python scripts/watch.py <url> --start 0 --end 10
=> UnicodeEncodeError: 'charmap' codec can't encode character '→'
Force UTF-8 on stdout/stderr at startup. No-op on macOS/Linux (already
UTF-8) and on streams without .reconfigure() (the hasattr guard).
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.
Summary
scripts/watch.pycrashes on Windows when it prints common Unicode characters (the arrowU+2192, em-dashes, ellipses) to stdout/stderr. Both streams default to cp1252 on Windows, which doesn't include those code points.Repro
On a fresh Windows install with Python 3.13:
Crashes at the focus-range header line:
The pipeline itself (download, captions, frame extraction) all succeeds — only the final markdown report fails to render.
Fix
Force UTF-8 on
sys.stdout/sys.stderrat script startup via.reconfigure(encoding="utf-8"). Thehasattrguard makes it a no-op on streams without that method (e.g. when stdout is replaced by something non-standard). On macOS/Linux this is also a no-op since both streams are already UTF-8.Alternatives considered:
PYTHONUTF8=1env var — works but pushes the burden onto every user. Not discoverable.→→->, em-dash →--) — invasive, easy to miss occurrences, andwhisper.py/ other scripts have the same chars.sys.stdoutwith aTextIOWrapper— equivalent but more code.reconfigureis the smallest, most contained fix.Test plan
python scripts/watch.py <url> --start 0 --end 10 --no-whisperruns to completion withoutPYTHONUTF8set, prints the→character cleanly in the focus-range header and final report.