fix(chat): read and write export/interpolate files as UTF-8 - #438
Merged
Merged
Conversation
Both opened files without an explicit encoding, so a non-UTF-8 locale made export() raise and interpolate_file() return mojibake. Fixes posit-dev#437
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Fixes locale-dependent encoding by explicitly using UTF-8 for chat exports and prompt interpolation.
Changes:
- Uses UTF-8 for reading and writing affected files.
- Adds non-ASCII regression tests.
- Documents the fix in the changelog.
| File | Description |
|---|---|
tests/test_interpolate.py |
Tests UTF-8 prompt reading. |
tests/test_chat.py |
Tests UTF-8 transcript writing. |
chatlas/_interpolate.py |
Reads prompt files as UTF-8. |
chatlas/_chat.py |
Writes exports as UTF-8. |
CHANGELOG.md |
Documents the fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #437.
On a non-UTF-8 locale,
open()without an explicitencoding=falls back to the platform's preferred encoding.Chat.export()andinterpolate_file()both did this, soexport()raisedUnicodeEncodeErroron non-ASCII content andinterpolate_file()silently returned a mojibake prompt instead of raising. Both now passencoding="utf-8"explicitly.Adds regression tests for
interpolate_file()andChat.export()that monkeypatchopen()to fall back to latin-1 (simulating a non-UTF-8 locale default) when no encoding is given, and assert round-trip correctness with non-ASCII content.Chat.export_eval()andBatchJob's state file read/write open files the same way and could get the same fix in a follow-up, if maintainers want it.