I have a problem in my code with the current version (0.23.0 of PYO3) and I don't know why, can someone please help me here? #4848
Answered
by
LilyAcorn
AriBermeki
asked this question in
Questions
-
use std::collections::HashMap;
use std::sync::Arc;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyDict};
#[pyclass]
#[derive(Debug)]
pub struct FunctionInfo {
#[pyo3(get, set)]
pub handler: Py<PyAny>,
#[pyo3(get, set)]
pub is_async: bool,
#[pyo3(get, set)]
pub number_of_params: u8,
#[pyo3(get, set)]
pub args: Py<PyDict>,
#[pyo3(get, set)]
pub kwargs: Py<PyDict>,
}
#[pymethods]
impl FunctionInfo {
#[new]
pub fn new(
handler: Py<PyAny>,
is_async: bool,
number_of_params: u8,
args: Py<PyDict>,
kwargs: Py<PyDict>,
) -> Self {
Self {
handler,
is_async,
number_of_params,
args,
kwargs,
}
}
}
impl Clone for FunctionInfo {
fn clone(&self) -> Self {
Python::with_gil(|py| {
Self {
handler: self.handler.clone_ref(py),
is_async: self.is_async,
number_of_params: self.number_of_params,
args: self.args.clone_ref(py),
kwargs: self.kwargs.clone_ref(py),
}
})
}
}
#[inline]
#[allow(dead_code)]
fn get_py_callable_output<'a, T>(
function: &'a FunctionInfo,
py: Python<'a>,
function_args: T,
) -> Result<&'a PyAny, PyErr>
where
T:IntoPyObject<'a>,
{
let cloned_info = function.handler.clone_ref(py);
let handler = cloned_info.clone_ref(py);
let kwargs = function.kwargs.into_bound(py).downcast::<PyDict>().unwrap();
let result = match function.number_of_params {
0 => handler.call0(py)?,
1 => {
if kwargs.get_item("global_dependencies").is_ok()
|| kwargs.get_item("system_dependencies").is_ok()
{
handler.call(py, (), Some(kwargs))?
} else {
handler.call1(py, (function_args,))?
}
}
_ => handler.call(py, (function_args,), Some(kwargs))?,
};
} |
Beta Was this translation helpful? Give feedback.
Answered by
LilyAcorn
Jan 10, 2025
Replies: 1 comment
-
Your second screenshot is closer, but you should also change the return signature from |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AriBermeki
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your second screenshot is closer, but you should also change the return signature from
Result<&'a PyAny, PyErr>
toResult<Py<PyAny>, PyErr>
.