From d246913b4cbd3fae2e1acc4a45004b3ab108c1dc Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 25 Feb 2024 16:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=AA=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/py/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 5d3c911..3e858b4 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -141,6 +141,20 @@ pub fn get_change_time(path: &PathBuf) -> Option { path.metadata().ok()?.modified().ok() } +pub fn py_module_from_code(content: &str, path: &str) -> PyResult> { + Python::with_gil(|py| -> PyResult> { + let module: PyResult> = PyModule::from_code( + py, + &content, + &path, + &path, + // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 + ) + .map(|module| module.into()); + module + }) +} + /// 传入文件路径 /// 返回 hash 和 文件内容 pub fn load_py_file(path: &PathBuf) -> std::io::Result<(Option, String)> {