tools: explicit UTF-8 encoding for file_read, file_write, file_edit

This commit is contained in:
Levi Neely 2026-07-18 16:15:10 +02:00
parent caf547c4af
commit be2f3b3487
3 changed files with 31 additions and 12 deletions

View File

@ -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")

View File

@ -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:

View File

@ -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")