diff --git a/tools/file_edit b/tools/file_edit index d7d871c..f0e70fe 100644 --- a/tools/file_edit +++ b/tools/file_edit @@ -101,8 +101,13 @@ if not os.path.exists(file_path): print(f"error: file not found: {file_path}") sys.exit(1) -with open(file_path, 'r', errors='replace') as f: - original = f.read() +try: + with open(file_path, 'r', encoding='utf-8') as f: + original = f.read() +except UnicodeDecodeError: + print(f"STATUS=error STATE_CHANGED=false RETRYABLE=false") + print(f"error: file is not valid UTF-8: {file_path}") + sys.exit(1) new_content, err, retryable = robust_replace(original, old_string, new_string, replace_all) if err: @@ -111,7 +116,7 @@ if err: sys.exit(1) try: - with open(file_path, 'w') as f: + with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) except OSError as e: print(f"STATUS=error STATE_CHANGED=false RETRYABLE=false") diff --git a/tools/file_read b/tools/file_read index 1cc84cf..bcada32 100644 --- a/tools/file_read +++ b/tools/file_read @@ -62,8 +62,12 @@ with open(file_path, 'rb') as f: sys.exit(0) # Count total lines -with open(file_path, 'r', errors='replace') as f: - total_lines = sum(1 for _ in f) +try: + with open(file_path, 'r', encoding='utf-8') as f: + total_lines = sum(1 for _ in f) +except UnicodeDecodeError: + with open(file_path, 'r', encoding='utf-8', errors='replace') as f: + total_lines = sum(1 for _ in f) start = max(1, start) if head: @@ -73,10 +77,16 @@ elif end is None: else: end = min(end, start + MAX_SEGMENT - 1) -with open(file_path, 'r', errors='replace') as f: - for _ in islice(f, start - 1): - pass - segment_lines = list(islice(f, end - start + 1)) +try: + with open(file_path, 'r', encoding='utf-8') as f: + for _ in islice(f, start - 1): + pass + segment_lines = list(islice(f, end - start + 1)) +except UnicodeDecodeError: + with open(file_path, 'r', encoding='utf-8', errors='replace') as f: + for _ in islice(f, start - 1): + pass + segment_lines = list(islice(f, end - start + 1)) if not segment_lines: if start == 1: diff --git a/tools/file_write b/tools/file_write index 6797d6b..76c54e2 100644 --- a/tools/file_write +++ b/tools/file_write @@ -34,11 +34,15 @@ if parent and not os.path.isdir(parent): old_content = "" if os.path.exists(file_path): - with open(file_path, 'r', errors='replace') as f: - old_content = f.read() + try: + with open(file_path, 'r', encoding='utf-8') as f: + old_content = f.read() + except UnicodeDecodeError: + with open(file_path, 'r', encoding='utf-8', errors='replace') as f: + old_content = f.read() try: - with open(file_path, 'w') as f: + with open(file_path, 'w', encoding='utf-8') as f: f.write(content) except OSError as e: print(f"STATUS=error STATE_CHANGED=false RETRYABLE=false")