Skip to content

Commit 3ab1b98

Browse files
anhnh2002claude
andcommitted
fix(str_replace_editor): handle non-UTF-8 subprocess output on Windows
Subprocess output decoded with bare .decode() crashed on Windows when the system code page produced non-UTF-8 bytes (e.g. GBK-encoded Chinese filenames or flake8 messages). Decode explicitly with errors="replace" in the flake8() and directory-view paths. Fixes #58 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 69b2610 commit 3ab1b98

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎codewiki/src/be/agent_tools/str_replace_editor.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ def flake8(file_path: str) -> str:
215215
cmd = "flake8 --isolated --select=F821,F822,F831,E111,E112,E113,E999,E902 {file_path}"
216216
# don't use capture_output because it's not compatible with python3.6
217217
out = subprocess.run(cmd.format(file_path=file_path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
218-
return out.stdout.decode()
218+
# Use errors="replace" so non-UTF-8 bytes (e.g. GBK-encoded paths on Windows) don't crash decoding.
219+
return out.stdout.decode("utf-8", errors="replace")
219220

220221

221222
class Filemap:
@@ -492,8 +493,9 @@ def view(self, path: Path, view_range: Optional[List[int]] = None):
492493
stdout=subprocess.PIPE,
493494
stderr=subprocess.PIPE,
494495
)
495-
stdout = out.stdout.decode()
496-
stderr = out.stderr.decode()
496+
# Use errors="replace" so non-UTF-8 bytes (e.g. GBK-encoded filenames on Windows) don't crash decoding.
497+
stdout = out.stdout.decode("utf-8", errors="replace")
498+
stderr = out.stderr.decode("utf-8", errors="replace")
497499

498500
if not stderr:
499501
stdout = stdout.replace(str(path), self._get_display_path(path))

0 commit comments

Comments
 (0)