fix(cli): force UTF-8 stdout/stderr at startup, no PYTHONUTF8 needed (closes #179)#183
Merged
Merged
Conversation
…loses #179) Windows without PYTHONUTF8=1 crashes with UnicodeEncodeError when commands emit Unicode chars like '\u2192' in trace paths (e.g. 'sidepanel.ts \u2192 auth/google-auth-cache.ts'). Default Windows stdout encoding (cp1252) cannot represent those chars. Fix: scripts/codelens.py wraps sys.stdout/sys.stderr as UTF-8 TextIOWrapper at import time, before any user-code imports. Behaviour: - Linux/macOS: no-op (encoding already utf-8) - pytest capsys: no-op (encoding is utf-8) - Windows without PYTHONUTF8=1: wrap with errors='replace' so unencodable bytes become '?' instead of crashing - Streams without .buffer (IDLE REPL, custom capture): skip gracefully - Old stream's buffer is detach()-ed before new wrapper is built, so GC of old wrapper does not close the underlying buffer Tests: tests/test_codelens.py::TestForceUtf8Stdio -- 7 regression tests covering all 4 scenarios + end-to-end test for the exact trace pattern from the issue.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
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.



Closes #179
Sebelum / Sesudah
Sebelum: Windows users running
codelens trace(or any command emitting Unicode chars like→) withoutPYTHONUTF8=1crashed with:Root cause: Windows default stdout encoding (
cp1252or similar) cannot represent Unicode chars used in trace path strings likesidepanel.ts → auth/google-auth-cache.ts.Sesudah:
scripts/codelens.pynow wrapssys.stdout/sys.stderras UTF-8TextIOWrapperat import time, before any user-code imports. Windows users no longer needPYTHONUTF8=1to get working output.Changes
scripts/codelens.py(+64 lines)Added
_force_utf8_stdio()at the top of the file (after the module docstring, before any other imports). Behaviour:sys.stdout.encodingis alreadyutf-8).capsys: no-op (capture's encoding is alsoutf-8).PYTHONUTF8=1: wraps bothstdoutandstderrasio.TextIOWrapper(buffer, encoding='utf-8', errors='replace'). Unencodable bytes become?instead of crashing..buffer(IDLE REPL, custom capture objects): skipped gracefully.detach()-ed before the new wrapper is built, so garbage-collection of the oldTextIOWrapperdoes not close the underlying buffer (which would otherwise make the new wrapper unusable withValueError: I/O operation on closed file— caught by the regression test).The fix runs unconditionally at module import time, before any user-code imports that might emit Unicode output (warnings, deprecation notices, etc.).
tests/test_codelens.py(+195 lines)Added
TestForceUtf8Stdioclass with 7 regression tests:test_function_is_callable— sanity check that_force_utf8_stdiois exposed.test_wraps_non_utf8_stdout— cp1252 stream is rewrapped as UTF-8; arrow writes succeed; underlying buffer receives UTF-8 bytes.test_noop_when_stream_already_utf8— UTF-8 stream is left untouched (same object).test_noop_for_stream_without_buffer— bufferless streams (IDLE REPL, custom capture) are skipped, not crashed.test_handles_stream_with_none_encoding— streams withencoding=Noneare wrapped.test_writes_unicode_arrow_to_replaced_stream— end-to-end test for the exactsidepanel.ts → auth/google-auth-cache.tstrace pattern from the issue.test_both_stdout_and_stderr_wrapped— verifies stderr is also handled (error messages like[CodeLens] Warning: ...would also crash on Windows if they contained Unicode).Tests
Pre-existing failures in
test_adr.py,test_codelensignore.py,test_confidence.py,test_formatters_phase2.py,test_universal_grammar_loader.pyverified to also fail onorigin/main(run withgit stash && git checkout main && pytest ... && git checkout - && git stash pop). They are environment-specific (missingtree_sittermodule, stale fixtures, schema_version field not yet implemented) and not regressions from this PR.Definition of Done check
PYTHONUTF8=1—sys.stdoutis forced to UTF-8 at import time.→and other Unicode chars in trace paths render correctly or are replaced with ASCII fallback (?viaerrors='replace') without crashing.sys.stdout.encodingis alreadyutf-8.Findings
tests/test_cli.py::TestArgparseFormatConflictRegression::test_scan_with_format_long_does_not_crashhas a hardcodedtimeout=60in itssubprocess.runcall. On this Linux sandbox it takes ~55-59s to scan the CodeLens repo itself, which is borderline. On my branch it ran in 55-56s on retry (faster than main's 58.98s), so no real regression — but the test will be flaky on any slow CI runner. Out of scope for this issue; flagging for awareness.