Skip to content

fix: use context managers and explicit encoding for file operations#14

Open
haosenwang1018 wants to merge 1 commit into1rgs:masterfrom
haosenwang1018:fix/file-handling-safety
Open

fix: use context managers and explicit encoding for file operations#14
haosenwang1018 wants to merge 1 commit into1rgs:masterfrom
haosenwang1018:fix/file-handling-safety

Conversation

@haosenwang1018
Copy link
Copy Markdown

Fixes file handling issues that cause crashes or resource leaks:

  1. read(): Used bare open() without with statement or encoding. Crashes on binary/non-UTF-8 files. Now uses context manager with encoding='utf-8', errors='replace'.

  2. edit(): Same bare open() issue — if the .read() or subsequent logic raises, the file handle leaks. Now uses context manager.

  3. grep(): Iterating over bare open(filepath) could crash on non-UTF-8 files (e.g. .pyc, images, compiled assets in the workspace). Now uses errors='replace' to skip bad bytes gracefully.

These are minimal changes that preserve the existing behavior while making file ops more robust.

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant