feat: update pyo3 (#308)

* feat: update pyo3

* update workflows

* python version

* update minimum

* update pyproject

* fix pipeline
This commit is contained in:
stefan-gorules
2025-02-07 13:12:46 +01:00
committed by GitHub
parent b992e7ccef
commit be305a4fba
13 changed files with 145 additions and 111 deletions
+28 -15
View File
@@ -1,35 +1,48 @@
use pyo3::{PyObject, Python, ToPyObject};
use pyo3::prelude::{PyDictMethods, PyListMethods};
use pyo3::types::{PyDict, PyList};
use pyo3::{Bound, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python};
use serde_json::Value;
use std::collections::HashMap;
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct PyValue(pub Value);
pub fn value_to_object(py: Python<'_>, val: &Value) -> PyObject {
pub fn value_to_object<'py>(py: Python<'py>, val: &Value) -> PyResult<Bound<'py, PyAny>> {
match val {
Value::Null => py.None(),
Value::Bool(b) => b.to_object(py),
Value::Null => py.None().into_bound_py_any(py),
Value::Bool(b) => b.into_bound_py_any(py),
Value::Number(n) => {
let oi64 = n.as_i64().map(|i| i.to_object(py));
let ou64 = n.as_u64().map(|i| i.to_object(py));
let of64 = n.as_f64().map(|i| i.to_object(py));
let oi64 = n.as_i64().map(|i| i.into_bound_py_any(py));
let ou64 = n.as_u64().map(|i| i.into_bound_py_any(py));
let of64 = n.as_f64().map(|i| i.into_bound_py_any(py));
oi64.or(ou64).or(of64).expect("number too large")
}
Value::String(s) => s.to_object(py),
Value::String(s) => s.into_bound_py_any(py),
Value::Array(v) => {
let inner: Vec<_> = v.iter().map(|x| value_to_object(py, x)).collect();
inner.to_object(py)
let list = PyList::empty(py);
for item in v {
list.append(value_to_object(py, item)?)?;
}
list.into_bound_py_any(py)
}
Value::Object(m) => {
let inner: HashMap<_, _> = m.iter().map(|(k, v)| (k, value_to_object(py, v))).collect();
inner.to_object(py)
let dict = PyDict::new(py);
for (key, value) in m {
dict.set_item(key, value_to_object(py, value)?)?;
}
dict.into_bound_py_any(py)
}
}
}
impl ToPyObject for PyValue {
fn to_object(&self, py: Python<'_>) -> PyObject {
impl<'py> IntoPyObject<'py> for PyValue {
type Target = PyAny;
type Output = Bound<'py, PyAny>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
value_to_object(py, &self.0)
}
}