改掉一点之前写的比较粗糙的地方

This commit is contained in:
shenjack-5600u 2025-04-10 22:28:49 +08:00
parent 546214f52f
commit d4999d1ab3
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F

View File

@ -11,9 +11,12 @@ use std::time::SystemTime;
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf};
use colored::Colorize; use colored::Colorize;
use pyo3::exceptions::PyTypeError; use pyo3::{
use pyo3::types::PyTuple; exceptions::PyTypeError,
use pyo3::{intern, prelude::*}; intern,
types::{PyAny, PyAnyMethods, PyModule, PyTracebackMethods, PyTuple},
Bound, Py, PyErr, PyResult, Python,
};
use tracing::{event, span, warn, Level}; use tracing::{event, span, warn, Level};
use crate::error::PyPluginError; use crate::error::PyPluginError;
@ -121,7 +124,7 @@ pub fn get_py_err_traceback(py_err: &PyErr) -> String {
pub struct PyPlugin { pub struct PyPlugin {
pub file_path: PathBuf, pub file_path: PathBuf,
pub changed_time: Option<SystemTime>, pub changed_time: Option<SystemTime>,
pub py_module: Py<PyAny>, pub py_module: Py<PyModule>,
pub enabled: bool, pub enabled: bool,
} }
@ -376,7 +379,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
Ok(PyPlugin { Ok(PyPlugin {
file_path: path, file_path: path,
changed_time, changed_time,
py_module: module.clone().into_any().unbind(), py_module: module.unbind(),
enabled: true, enabled: true,
}) })
} else if config.is_none() { } else if config.is_none() {
@ -384,7 +387,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
Ok(PyPlugin { Ok(PyPlugin {
file_path: path, file_path: path,
changed_time, changed_time,
py_module: module.clone().into_any().unbind(), py_module: module.unbind(),
enabled: true, enabled: true,
}) })
} else { } else {
@ -406,7 +409,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
Ok(PyPlugin { Ok(PyPlugin {
file_path: path, file_path: path,
changed_time, changed_time,
py_module: module.clone().into_any().unbind(), py_module: module.unbind(),
enabled: true, enabled: true,
}) })
} }
@ -471,9 +474,8 @@ pub fn py_module_from_code(content: &str, path: &Path) -> PyResult<Py<PyModule>>
.unwrap() .unwrap()
.as_c_str(), .as_c_str(),
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
) )?;
.map(|module| module.unbind()); Ok(module.unbind())
module
}) })
} }