fix: centralize UTF-8 file I/O for Windows compatibility#45
Draft
joshbouncesecurity wants to merge 2 commits intoknostic:masterfrom
Draft
fix: centralize UTF-8 file I/O for Windows compatibility#45joshbouncesecurity wants to merge 2 commits intoknostic:masterfrom
joshbouncesecurity wants to merge 2 commits intoknostic:masterfrom
Conversation
Bare open() calls use the system encoding (cp1252 on Windows), causing 'charmap codec can't decode byte ...' errors when parsing repositories containing non-ASCII characters such as curly quotes. Adds utilities/file_io.py with open_utf8, read_json, write_json, and run_utf8 helpers, and migrates ~190 bare open() call sites across libs/openant-core/ (core, parsers, utilities, openant CLI, top-level scripts) to specify encoding="utf-8" explicitly. Also sets encoding/errors on the docker_executor subprocess.run that captures container stdout/stderr as text. Includes a regression test that scans non-test code for any bare open() call without an encoding= argument and fails if a regression reappears. Addresses item 9 from #16.
… UTF-8 Round 1 review fixes for PR knostic#45: - application_context.py, ast_parser.py, dataset_enhancer.py, report/__main__.py, report/generator.py: pass encoding='utf-8' on every Path.read_text() / write_text() call. The previous migration only covered open() calls; pathlib's text helpers also default to the system locale on Windows (cp1252) and crash on non-ASCII source code. - parsers/{c,go,javascript,php,ruby}/test_pipeline.py: pass encoding='utf-8', errors='replace' on subprocess.run(text=True) invocations of parser binaries and CodeQL. Only docker_executor.py was migrated before; these other call sites had the same Windows cp1252 hazard. - tests/test_file_io.py: extend regression scan with two new asserts — Path.read_text/write_text without encoding=, and subprocess.run(text=True) without encoding=. Refactored the call-walking logic into a shared helper. All 14 file_io tests pass; full tests/ suite: 98 passed, 22 skipped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
Author
Manual verification
|
Contributor
Author
Local test resultsVerified the migration on Windows by static-grepping the worktree and round-tripping non-ASCII content through Commands run: Output: Outcome:
|
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
Bare
open()calls use the system encoding (cp1252 on Windows), causingcharmap codec can't decodeerrors on any target codebase containing non-ASCII characters (e.g. curly quotes U+2019, accented characters, CJK).Adds
utilities/file_io.pywith centralized UTF-8 helpers (open_utf8,read_json,write_json,run_utf8) and migrates ~190 bareopen()call sites acrosslibs/openant-core/(core, parsers, utilities, openant CLI, top-level scripts) to specifyencoding="utf-8"explicitly. Also setsencoding/errorson the docker_executorsubprocess.runthat captures container stdout/stderr as text.A regression test scans non-test Python files for any bare
open()call without anencoding=argument and fails if a regression reappears.Addresses item 9 from #16 (does not close the issue).
Test plan
open_utf8,read_json,write_json.run_utf8captures non-ASCII subprocess output (incl.universal_newlines=Truealias).run_utf8only injectsencoding/errorswhen caller asks for text mode; explicitencoding=is respected.open(in non-test code (test scanslibs/openant-core/excludingtests/and the helper).pytest tests/-> 96 passed, 22 skipped (env-dependent).charmaperrors.