fix: add explicit encoding to open() calls and improve error handling#5428
Open
ashishgowdamj wants to merge 1 commit into
Open
fix: add explicit encoding to open() calls and improve error handling#5428ashishgowdamj wants to merge 1 commit into
ashishgowdamj wants to merge 1 commit into
Conversation
Add explicit encoding='utf-8' to open() calls across the codebase to prevent UnicodeDecodeError on Windows where default encoding may differ. Fixes: - editor.py: add try/except around temp file read + encoding - watch.py: add encoding + errors='replace' for gitignore files (fixes Aider-AI#2888) - main.py: encoding for config, credentials, and installs files - models.py, analytics.py, history.py: encoding for file I/O Adds tests: - editor: read failure and UnicodeDecodeError graceful fallback - watch: non-UTF8 gitignore handling, BOM handling, None/empty input
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
Add explicit
encoding='utf-8'toopen()calls across the codebase to preventUnicodeDecodeErroron Windows where the default encoding (e.g. cp1252) differs from UTF-8.Changes
Production code
aider/watch.py: Fixes Uncaught UnicodeDecodeError in cp1252.py line 23 #2888 - addedencoding='utf-8', errors='replace'when reading .gitignore files to handle non-UTF8 content gracefullyaider/editor.py: Addedencoding='utf-8'+ try/except around temp file read to handle missing/unreadable files after editor closesaider/main.py: Added encoding to config file read, Streamlit credentials write, and installs file read/writeaider/models.py: Added encoding to model settings YAML readaider/analytics.py: Added encoding to analytics log writeaider/history.py: Added encoding to history file readTests
test_editor.py: Addedtest_pipe_editor_read_failureandtest_pipe_editor_unicode_decode_errorto verify graceful fallbacktest_watch.py: Addedtest_load_gitignores_handles_non_utf8,test_load_gitignores_handles_utf8_bom,test_load_gitignores_none_pathsTesting
All existing tests pass (109 passed, excluding 1 pre-existing failure unrelated to this change).