From 967e3db032e0fb4db5d1d5e219ad928619c01ddf Mon Sep 17 00:00:00 2001 From: danbugs Date: Sun, 17 May 2026 06:53:26 +0000 Subject: [PATCH] fix: enable Python UTF-8 mode to fix open() encoding default 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 --- examples/python-agent-driver/hl_pydriver.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/python-agent-driver/hl_pydriver.c b/examples/python-agent-driver/hl_pydriver.c index 0781702..c013c86 100644 --- a/examples/python-agent-driver/hl_pydriver.c +++ b/examples/python-agent-driver/hl_pydriver.c @@ -234,17 +234,12 @@ static void py_run_user_code(const uint8_t *fc, size_t fc_len) static void py_initialize_once(void) { + Py_UTF8Mode = 1; Py_Initialize(); - /* sys.argv so scripts that look at it don't crash. Also force - * stdout/stderr to UTF-8 — the guest has no locale configured, - * so Python defaults to ASCII and any non-ASCII char (em-dash, - * smart quotes, …) in a script's print() raises UnicodeEncodeError. */ PyRun_SimpleString( "import sys\n" - "sys.argv = ['hl_pydriver']\n" - "sys.stdout.reconfigure(encoding='utf-8')\n" - "sys.stderr.reconfigure(encoding='utf-8')\n"); + "sys.argv = ['hl_pydriver']\n"); /* Pre-import the python-agent stack so every subsequent call * through the v2 callback sees a warm sys.modules and pays only