fix: enable Python UTF-8 mode to fix open() encoding default - #66
Merged
Conversation
The guest has no locale configured, so CPython defaults all I/O to ASCII. This causes UnicodeEncodeError when writing non-ASCII characters via open(path, 'w'). Setting Py_UTF8Mode = 1 before Py_Initialize() enables UTF-8 mode globally, making stdout/stderr/open() all default to UTF-8 without needing per-stream reconfigure calls. Signed-off-by: danbugs <danilochiarlone@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the hl_pydriver embedded-CPython initialization to force CPython’s UTF-8 mode, ensuring that default text encoding for file I/O (e.g., open(path, "w")) is UTF-8 even when the guest has no locale configured.
Changes:
- Sets
Py_UTF8Mode = 1beforePy_Initialize()to make Python’s default text encoding UTF-8 in the guest. - Removes Python-level
sys.stdout.reconfigure(...)/sys.stderr.reconfigure(...)since UTF-8 mode should cover standard streams globally. - Keeps
sys.argvinitialization for script compatibility.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Linux Benchmarks
Details
| Benchmark suite | Current: 967e3db | Previous: 2c92788 | Ratio |
|---|---|---|---|
hello_world (median) |
20 ms |
20 ms |
1 |
pandas (median) |
110 ms |
110 ms |
1 |
density (per VM) |
7 MB |
7 MB |
1 |
snapshot (disk) |
385 MiB |
385 MiB |
1 |
This comment was automatically generated by workflow using github-action-benchmark.
Contributor
There was a problem hiding this comment.
Windows Benchmarks
Details
| Benchmark suite | Current: 967e3db | Previous: 2c92788 | Ratio |
|---|---|---|---|
hello_world (median) |
261 ms |
187 ms |
1.40 |
pandas (median) |
810 ms |
545 ms |
1.49 |
density (per VM) |
6 MB |
6 MB |
1 |
snapshot (disk) |
392 MiB |
392 MiB |
1 |
This comment was automatically generated by workflow using github-action-benchmark.
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.
Summary
Py_UTF8Mode = 1beforePy_Initialize()in the Python agent driveropen(path, 'w')) default to UTF-8 encodingsys.stdout.reconfigure(encoding='utf-8')/sys.stderr.reconfigure(encoding='utf-8')calls since UTF-8 mode handles all streams globallyPreviously, only stdout/stderr were reconfigured to UTF-8 via Python-level calls. File operations like
open('/host/file.txt', 'w')still defaulted to ASCII because the guest has no locale configured. Writing non-ASCII characters (em-dashes, curly quotes, Unicode symbols) would raiseUnicodeEncodeError.Test plan
open('/host/file.txt', 'w').write('hello -- world')works without errorprint()with non-ASCII still works (covered by UTF-8 mode globally)