Skip to content

fix: enable Python UTF-8 mode to fix open() encoding default - #66

Merged
danbugs merged 1 commit into
mainfrom
fix/utf8-mode
May 17, 2026
Merged

fix: enable Python UTF-8 mode to fix open() encoding default#66
danbugs merged 1 commit into
mainfrom
fix/utf8-mode

Conversation

@danbugs

@danbugs danbugs commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Sets Py_UTF8Mode = 1 before Py_Initialize() in the Python agent driver
  • This makes all file I/O (including open(path, 'w')) default to UTF-8 encoding
  • Removes the now-redundant sys.stdout.reconfigure(encoding='utf-8') / sys.stderr.reconfigure(encoding='utf-8') calls since UTF-8 mode handles all streams globally

Previously, 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 raise UnicodeEncodeError.

Test plan

  • Verify existing pyhl tests still pass (no behavior change for ASCII content)
  • Confirm open('/host/file.txt', 'w').write('hello -- world') works without error
  • Confirm print() with non-ASCII still works (covered by UTF-8 mode globally)

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>
Copilot AI review requested due to automatic review settings May 17, 2026 06:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = 1 before Py_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.argv initialization for script compatibility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danbugs
danbugs merged commit 659686a into main May 17, 2026
80 checks passed
@danbugs
danbugs deleted the fix/utf8-mode branch May 17, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants