From c10435d58c5da6333a1020c4430c05d07f38d290 Mon Sep 17 00:00:00 2001 From: danbugs Date: Fri, 22 May 2026 18:10:06 +0000 Subject: [PATCH] fix: zipfile.ZipInfo monkey-patch NameError after init cleanup The _safe_zi wrapper referenced _orig_zi as a module-level global, but `del _orig_zi` at the end of the init block destroyed it before any call to ZipInfo() could use it. This broke all libraries that use zip-based formats (python-docx, openpyxl, etc.) with: NameError: name '_orig_zi' is not defined Wrap the patch in a function so _orig becomes a proper closure cell that survives the namespace cleanup. Adds a regression test using python-docx's Document() constructor. Signed-off-by: danbugs --- examples/python-agent-driver/hl_pydriver.c | 16 +++++++++------- host/tests/pyhl_runtime.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/python-agent-driver/hl_pydriver.c b/examples/python-agent-driver/hl_pydriver.c index 9668938..16c8266 100644 --- a/examples/python-agent-driver/hl_pydriver.c +++ b/examples/python-agent-driver/hl_pydriver.c @@ -265,13 +265,15 @@ static void py_initialize_once(void) * writer (XLSX is a ZIP container). */ PyRun_SimpleString( "import zipfile as _zf\n" - "_orig_zi = _zf.ZipInfo.__init__\n" - "def _safe_zi(self, filename='NoName', date_time=(1980,1,1,0,0,0)):\n" - " if date_time[0] < 1980:\n" - " date_time = (1980,1,1,0,0,0)\n" - " _orig_zi(self, filename, date_time)\n" - "_zf.ZipInfo.__init__ = _safe_zi\n" - "del _zf, _orig_zi, _safe_zi\n"); + "def _patch_zipinfo():\n" + " _orig = _zf.ZipInfo.__init__\n" + " def _safe(self, filename='NoName', date_time=(1980,1,1,0,0,0)):\n" + " if date_time[0] < 1980:\n" + " date_time = (1980,1,1,0,0,0)\n" + " _orig(self, filename, date_time)\n" + " _zf.ZipInfo.__init__ = _safe\n" + "_patch_zipinfo()\n" + "del _patch_zipinfo, _zf\n"); /* Monkey-patch time.sleep to call the host via /dev/hcall. * Unikraft's cooperative scheduler on Hyperlight has no timer diff --git a/host/tests/pyhl_runtime.rs b/host/tests/pyhl_runtime.rs index 20b371a..15e9fb6 100644 --- a/host/tests/pyhl_runtime.rs +++ b/host/tests/pyhl_runtime.rs @@ -117,6 +117,21 @@ fn runtime_pandas_import() { assert_eq!(timing.exit_code, 0); } +// --------------------------------------------------------------------------- +// zipfile.ZipInfo monkey-patch (pre-1980 date clamping) +// --------------------------------------------------------------------------- + +#[test] +fn runtime_zipinfo_patch_survives_cleanup() { + let Some((_home, mut rt)) = setup() else { + return; + }; + let timing = rt + .run_code("from docx import Document\ndoc = Document()\nprint('ok')") + .unwrap(); + assert_eq!(timing.exit_code, 0); +} + // --------------------------------------------------------------------------- // Exit code propagation // ---------------------------------------------------------------------------