Skip to content

Commit 03a1a97

Browse files
erkinalpopenhands-agentryanhoangt
authored
Fix ASCII encoding issue when updating files with non-ASCII characters (#116)
* Fix ASCII encoding issue when updating files with non-ASCII characters * move test to intg * bump to 0.2.12 --------- Co-authored-by: openhands <openhands@all-hands.dev> Co-authored-by: Hoang Tran <descience.thh10@gmail.com>
1 parent f0c73a0 commit 03a1a97

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

openhands_aci/editor/encoding.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def detect_encoding(self, path: Path) -> str:
4848
# Get the best match if any exists
4949
if results and results['confidence'] > self.confidence_threshold:
5050
encoding = results['encoding']
51+
# Always use utf-8 instead of ascii for text files to support non-ASCII characters
52+
# This ensures files initially containing only ASCII can later accept non-ASCII content
53+
if encoding.lower() == 'ascii':
54+
encoding = self.default_encoding
5155
else:
5256
encoding = self.default_encoding
5357

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openhands-aci"
3-
version = "0.2.11"
3+
version = "0.2.12"
44
description = "An Agent-Computer Interface (ACI) designed for software development agents OpenHands."
55
authors = ["OpenHands"]
66
license = "MIT"

tests/integration/test_oh_editor.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,26 @@ def test_insert_with_empty_string(editor):
356356
assert len(content) == 3 # Original 2 lines plus empty line
357357

358358

359+
def test_insert_chinese_text_into_english_file(editor):
360+
editor, test_file = editor
361+
result = editor(
362+
command='insert',
363+
path=str(test_file),
364+
insert_line=0,
365+
new_str='中文文本',
366+
)
367+
assert isinstance(result, CLIResult)
368+
assert '中文文本' in test_file.read_text()
369+
assert (
370+
result.output
371+
== f"""The file {test_file} has been edited. Here's the result of running `cat -n` on a snippet of the edited file:
372+
1\t中文文本
373+
2\tThis is a test file.
374+
3\tThis file is for testing purposes.
375+
Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."""
376+
)
377+
378+
359379
def test_insert_with_none_new_str(editor):
360380
editor, test_file = editor
361381
with pytest.raises(EditorToolParameterMissingError) as exc_info:

0 commit comments

Comments
 (0)