Skip to content

Commit 3353fc3

Browse files
authored
Support any data type in Rust->Python. #19 (#67)
Support any data type in Rust->Python.
1 parent 0e18f1c commit 3353fc3

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

src/ops/py_factory.rs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use pyo3::{
99
types::{IntoPyDict, PyAnyMethods, PyString, PyTuple},
1010
Bound, IntoPyObjectExt, Py, PyAny, PyResult, Python,
1111
};
12+
use pythonize::pythonize;
1213

1314
use crate::{
1415
base::{schema, value},
@@ -21,46 +22,6 @@ use super::sdk::{
2122
ExecutorFuture, FlowInstanceContext, SimpleFunctionExecutor, SimpleFunctionFactory,
2223
};
2324

24-
fn basic_value_to_py_object<'py>(
25-
py: Python<'py>,
26-
v: &value::BasicValue,
27-
) -> PyResult<Bound<'py, PyAny>> {
28-
let result = match v {
29-
value::BasicValue::Bytes(v) => v.into_bound_py_any(py)?,
30-
value::BasicValue::Str(v) => v.into_bound_py_any(py)?,
31-
value::BasicValue::Bool(v) => v.into_bound_py_any(py)?,
32-
value::BasicValue::Int64(v) => v.into_bound_py_any(py)?,
33-
value::BasicValue::Float32(v) => v.into_bound_py_any(py)?,
34-
value::BasicValue::Float64(v) => v.into_bound_py_any(py)?,
35-
value::BasicValue::Vector(v) => v
36-
.iter()
37-
.map(|v| basic_value_to_py_object(py, v))
38-
.collect::<PyResult<Vec<_>>>()?
39-
.into_bound_py_any(py)?,
40-
_ => {
41-
return Err(PyException::new_err(format!(
42-
"unsupported value type: {}",
43-
v.kind()
44-
)))
45-
}
46-
};
47-
Ok(result)
48-
}
49-
50-
fn value_to_py_object<'py>(py: Python<'py>, v: &value::Value) -> PyResult<Bound<'py, PyAny>> {
51-
let result = match v {
52-
value::Value::Null => py.None().into_bound(py),
53-
value::Value::Basic(v) => basic_value_to_py_object(py, v)?,
54-
_ => {
55-
return Err(PyException::new_err(format!(
56-
"unsupported value type: {}",
57-
v.kind()
58-
)))
59-
}
60-
};
61-
Ok(result)
62-
}
63-
6425
fn basic_value_from_py_object<'py>(
6526
typ: &schema::BasicValueType,
6627
v: &Bound<'py, PyAny>,
@@ -212,7 +173,7 @@ impl SimpleFunctionExecutor for Arc<PyFunctionExecutor> {
212173
Python::with_gil(|py| -> Result<_> {
213174
let mut args = Vec::with_capacity(self.num_positional_args);
214175
for v in input[0..self.num_positional_args].iter() {
215-
args.push(value_to_py_object(py, v)?);
176+
args.push(pythonize(py, v)?);
216177
}
217178

218179
let kwargs = if self.kw_args_names.is_empty() {
@@ -224,7 +185,7 @@ impl SimpleFunctionExecutor for Arc<PyFunctionExecutor> {
224185
.iter()
225186
.zip(input[self.num_positional_args..].iter())
226187
{
227-
kwargs.push((name.bind(py), value_to_py_object(py, v)?));
188+
kwargs.push((name.bind(py), pythonize(py, v)?));
228189
}
229190
Some(kwargs)
230191
};
@@ -272,7 +233,7 @@ impl SimpleFunctionFactory for PyFunctionFactory {
272233
)> {
273234
let (result_type, executor, kw_args_names, num_positional_args) =
274235
Python::with_gil(|py| -> anyhow::Result<_> {
275-
let mut args = vec![crate::py::Pythonized(spec).into_py_any(py)?];
236+
let mut args = vec![pythonize(py, &spec)?];
276237
let mut kwargs = vec![];
277238
let mut num_positional_args = 0;
278239
for arg in input_schema.into_iter() {
@@ -285,7 +246,7 @@ impl SimpleFunctionFactory for PyFunctionFactory {
285246
kwargs.push((name.clone(), py_arg_schema));
286247
}
287248
None => {
288-
args.push(py_arg_schema.into_py_any(py)?);
249+
args.push(py_arg_schema.into_bound_py_any(py)?);
289250
num_positional_args += 1;
290251
}
291252
}

0 commit comments

Comments
 (0)