Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Fix(tools): Encoding issues

Kilian Lieretd5baa2da8719
diff --git a/tools/edit_anthropic/bin/str_replace_editor b/tools/edit_anthropic/bin/str_replace_editor
index 756feb08..d28f1bfc 100644
--- a/tools/edit_anthropic/bin/str_replace_editor
+++ b/tools/edit_anthropic/bin/str_replace_editor
@@ -44,6 +44,7 @@ class EditTool:
def __init__(self):
super().__init__()
+ self._encoding = None
@property
def _file_history(self):
@@ -265,16 +266,28 @@ class EditTool:
def read_file(self, path: Path):
"""Read the content of a file from a given path; raise a ToolError if an error occurs."""
- try:
- return path.read_text()
- except Exception as e:
- print(f"Ran into {e} while trying to read {path}")
+ encodings = [
+ None,
+ "utf-8",
+ "latin-1",
+ ]
+ exception = None
+ for self._encoding in encodings:
+ try:
+ text = path.read_text(encoding=self._encoding)
+ except UnicodeDecodeError as e:
+ exception = e
+ else:
+ break
+ else:
+ print(f"Ran into UnicodeDecodeError {exception} while trying to read {path}")
sys.exit(19)
+ return text
def write_file(self, path: Path, file: str):
"""Write the content of a file to a given path; raise a ToolError if an error occurs."""
try:
- path.write_text(file)
+ path.write_text(file, encoding=self._encoding)
except Exception as e:
print(f"Ran into {e} while trying to write to {path}")
sys.exit(20)