Skip to content

Commit 3e43240

Browse files
Write rendered files as UTF-8 to fix Windows UnicodeEncodeError
store_response_files opened files with the platform default text codec, which on Windows is cp1252 and cannot encode characters like 📍 (U+1F4CD). LLM-generated content containing such characters crashed the render with UnicodeEncodeError. Specify encoding="utf-8" explicitly, matching how the rest of file_utils reads text. Add a regression test covering unicode content. Fixes CODEPLAIN-P
1 parent 0cb870e commit 3e43240

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def store_response_files(target_folder, response_files, existing_files):
157157

158158
os.makedirs(os.path.dirname(full_file_name), exist_ok=True)
159159

160-
with open(full_file_name, "w") as f:
160+
with open(full_file_name, "w", encoding="utf-8") as f:
161161
f.write(response_files[file_name])
162162

163163
if file_name not in existing_files:

tests/test_file_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from file_utils import load_linked_resources
6+
from file_utils import load_linked_resources, store_response_files
77
from plain2code_exceptions import UnsupportedResourceType
88

99

@@ -39,3 +39,14 @@ def test_load_linked_resources_binary_file_raises_unsupported_resource_type(temp
3939
def test_load_linked_resources_missing_file_raises_file_not_found(template_dir):
4040
with pytest.raises(FileNotFoundError):
4141
load_linked_resources([template_dir], [{"text": "Missing", "target": "missing.md"}], "my_thing")
42+
43+
44+
def test_store_response_files_writes_unicode_as_utf8(template_dir):
45+
# Content with a non-cp1252 character (📍 U+1F4CD) must be written as UTF-8
46+
# regardless of the platform's default text encoding (e.g. cp1252 on Windows).
47+
content = "Location 📍 marker"
48+
store_response_files(template_dir, {"notes.md": content}, [])
49+
50+
file_path = os.path.join(template_dir, "notes.md")
51+
with open(file_path, "rb") as f:
52+
assert f.read().decode("utf-8") == content

0 commit comments

Comments
 (0)