fix: use context managers and explicit encoding for file operations#14
Open
haosenwang1018 wants to merge 1 commit into1rgs:masterfrom
Open
fix: use context managers and explicit encoding for file operations#14haosenwang1018 wants to merge 1 commit into1rgs:masterfrom
haosenwang1018 wants to merge 1 commit into1rgs:masterfrom
Conversation
- read(): Use 'with' statement and utf-8 encoding with error replacement to handle binary files gracefully instead of crashing - edit(): Use 'with' statement and utf-8 encoding to prevent resource leaks on exception paths - grep(): Add utf-8 encoding with error replacement so searching doesn't crash on files with non-UTF-8 content
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.
Fixes file handling issues that cause crashes or resource leaks:
read(): Used bareopen()withoutwithstatement or encoding. Crashes on binary/non-UTF-8 files. Now uses context manager withencoding='utf-8', errors='replace'.edit(): Same bareopen()issue — if the.read()or subsequent logic raises, the file handle leaks. Now uses context manager.grep(): Iterating over bareopen(filepath)could crash on non-UTF-8 files (e.g..pyc, images, compiled assets in the workspace). Now useserrors='replace'to skip bad bytes gracefully.These are minimal changes that preserve the existing behavior while making file ops more robust.