From ec75658a0a8d90cc5bd735c80c8f75199022e9df Mon Sep 17 00:00:00 2001 From: llama Date: Sat, 1 Nov 2025 22:23:35 +0800 Subject: [PATCH] add PyFfi impl --- qt/launcher/src/platform/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/qt/launcher/src/platform/mod.rs b/qt/launcher/src/platform/mod.rs index 3fe322ea0..ee4501e1e 100644 --- a/qt/launcher/src/platform/mod.rs +++ b/qt/launcher/src/platform/mod.rs @@ -13,6 +13,7 @@ pub mod windows; use std::path::PathBuf; use anki_process::CommandExt; +use anyhow::ensure; use anyhow::Context; use anyhow::Result; @@ -153,3 +154,21 @@ struct PyFfi { PyRun_SimpleString: PyRunSimpleString, Py_FinalizeEx: PyFinalizeEx, } + +impl PyFfi { + fn run(self, preamble: impl AsRef) -> Result<()> { + (self.Py_InitializeEx)(1); + + let res = (self.Py_IsInitialized)(); + ensure!(res != 0, "failed to initialise"); + let res = (self.PyRun_SimpleString)(preamble.as_ref().as_ptr()); + ensure!(res == 0, "failed to run preamble"); + // test importing aqt first before falling back to usual launch + let res = (self.PyRun_SimpleString)(c"import aqt".as_ptr()); + ensure!(res == 0, "failed to import aqt"); + // from here on, don't fallback if we fail + let _ = (self.PyRun_SimpleString)(c"aqt.run()".as_ptr()); + + Ok(()) + } +}