Skip to content

Commit

Permalink
Merge pull request #47 from tinythings/isbm-pymod-return-type
Browse files Browse the repository at this point in the history
Add return type
  • Loading branch information
isbm authored Nov 25, 2024
2 parents 302187a + 21c8f85 commit 89ade4c
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion libsysinspect/src/pylang/pylib/pysystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ pub mod syscore {
util::dataconv,
};
use rustpython_vm::PyResult;
use rustpython_vm::{builtins::PyList, convert::ToPyObject, pyclass, PyObjectRef, PyPayload, VirtualMachine};
use rustpython_vm::{
builtins::{PyDict, PyList},
common::lock::PyMutex,
convert::ToPyObject,
pyclass, PyObjectRef, PyPayload, PyRef, VirtualMachine,
};

#[derive(Debug, Clone)]
struct StrVec(Vec<String>);
Expand Down Expand Up @@ -63,4 +68,78 @@ pub mod syscore {
fn MinionTraits() -> PyResult<MinionTraits> {
Ok(MinionTraits::new())
}

#[derive(Debug)]
struct Inner {
retcode: usize,
warnings: Vec<String>,
message: String,
data: PyRef<PyDict>,
}

#[pyattr]
#[pyclass(module = "syscore", name = "__SysinspectReturn")]
#[derive(Debug, PyPayload)]
pub struct SysinspectReturn {
inner: PyMutex<Inner>,
}

#[pyclass]
impl SysinspectReturn {
fn new(_vm: &VirtualMachine) -> SysinspectReturn {
SysinspectReturn {
inner: PyMutex::new(Inner { retcode: 0, data: _vm.ctx.new_dict(), warnings: vec![], message: "".to_string() }),
}
}

#[pygetset]
fn retcode(&self) -> usize {
self.inner.lock().retcode
}

#[pymethod]
fn set_retcode(&self, retcode: usize, _vm: &VirtualMachine) -> PyObjectRef {
self.inner.lock().retcode = retcode;
_vm.ctx.none()
}

#[pygetset]
fn message(&self) -> String {
self.inner.lock().message.to_owned()
}

#[pymethod]
fn set_message(&self, message: String) {
self.inner.lock().message = message
}

#[pygetset]
fn warnings(&self, _vm: &VirtualMachine) -> PyObjectRef {
let list = self.inner.lock().warnings.iter().map(|e| _vm.new_pyobj(e)).collect();
PyList::new_ref(list, _vm.as_ref()).to_pyobject(_vm)
}

#[pymethod]
fn add_warning(&self, warn: String, _vm: &VirtualMachine) {
self.inner.lock().warnings.push(warn);
}

#[pygetset]
fn data(&self) -> PyRef<PyDict> {
self.inner.lock().data.to_owned()
}

#[pymethod]
fn set_data(&self, data: PyRef<PyDict>) {
self.inner.lock().data = data;
}
}

#[pyfunction]
#[allow(non_snake_case)]
/// This is a mimic of "MinionTraits" Python class,
/// which needs to be called for the init.
fn SysinspectReturn(_vm: &VirtualMachine) -> PyResult<SysinspectReturn> {
Ok(SysinspectReturn::new(_vm))
}
}

0 comments on commit 89ade4c

Please sign in to comment.