- fix some pickling issues

This commit is contained in:
Wim Pomp
2025-05-06 15:37:04 +02:00
parent cbb135e13e
commit f1add2f8ad
3 changed files with 10 additions and 38 deletions

View File

@@ -1,4 +1,3 @@
use anyhow::anyhow;
use crate::Ome;
use crate::ome::{
Convert, UnitsElectricPotential, UnitsFrequency, UnitsLength, UnitsPower,
@@ -6,39 +5,7 @@ use crate::ome::{
};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;
/// helper class to be used for unpickling
#[pyclass(module = "ome_metadata.ome_metadata_rs")]
struct Constructor;
#[pymethods]
impl Constructor {
#[new]
fn new() -> Self {
Self
}
fn __getstate__(&self) -> (u8,) {
(0,)
}
fn __setstate__(&self, _state: (u8,)) {}
#[staticmethod]
fn __call__(py: Python, class: String, variant: String) -> PyResult<Bound<PyAny>> {
match class.as_str() {
"ElectricPotential" => ElectricPotential::new(&variant)?.into_bound_py_any(py),
"Frequency" => Frequency::new(&variant)?.into_bound_py_any(py),
"Length" => Length::new(&variant)?.into_bound_py_any(py),
"Power" => Power::new(&variant)?.into_bound_py_any(py),
"Pressure" => Pressure::new(&variant)?.into_bound_py_any(py),
"Temperature" => Temperature::new(&variant)?.into_bound_py_any(py),
"Time" => Time::new(&variant)?.into_bound_py_any(py),
_ => Err(anyhow!("cannot parse class {}", class).into())
}
}
}
macro_rules! impl_enum_into_py_object {
($($s:ident: $t:ty $(,)?)*) => {
@@ -79,9 +46,9 @@ macro_rules! impl_enum_into_py_object {
fn __str__(&self) -> String {
format!("{:?}", self.inner)
}
fn __reduce__(&self) -> PyResult<(Constructor, (String, String))> {
Ok((Constructor, (stringify!($s).to_string(), format!("{:?}", self.inner))))
fn __getnewargs__(&self) -> (String,) {
(format!("{:?}", self.inner),)
}
}
@@ -110,7 +77,6 @@ impl_enum_into_py_object! {
#[pymodule]
#[pyo3(name = "ome_metadata_rs")]
fn ome_metadata_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Constructor>()?;
m.add_class::<ElectricPotential>()?;
m.add_class::<Frequency>()?;
m.add_class::<Length>()?;