| @@ -110,7 +110,10 @@ impl PyEvent { | |||
| pydict.insert("_cleanup", cleanup.into_py(py)); | |||
| } | |||
| Ok(pydict.into_py_dict_bound(py).unbind()) | |||
| Ok(pydict | |||
| .into_py_dict(py) | |||
| .context("Failed to create py_dict")? | |||
| .unbind()) | |||
| } | |||
| fn ty(event: &Event) -> &str { | |||
| @@ -218,7 +221,7 @@ pub fn metadata_to_pydict<'a>( | |||
| metadata: &'a Metadata, | |||
| py: Python<'a>, | |||
| ) -> Result<pyo3::Bound<'a, PyDict>> { | |||
| let dict = PyDict::new_bound(py); | |||
| let dict = PyDict::new(py); | |||
| for (k, v) in metadata.parameters.iter() { | |||
| match v { | |||
| Parameter::Bool(bool) => dict | |||
| @@ -10,6 +10,7 @@ use dora_node_api::{merged::MergedEvent, Event, Parameter}; | |||
| use dora_operator_api_python::PyEvent; | |||
| use dora_operator_api_types::DoraStatus; | |||
| use eyre::{bail, eyre, Context, Result}; | |||
| use pyo3::ffi::c_str; | |||
| use pyo3::{ | |||
| pyclass, | |||
| types::{IntoPyDict, PyAnyMethods, PyDict, PyDictMethods, PyTracebackMethods}, | |||
| @@ -75,9 +76,7 @@ pub fn run( | |||
| let parent_path = parent_path | |||
| .to_str() | |||
| .ok_or_else(|| eyre!("module path is not valid utf8"))?; | |||
| let sys = py | |||
| .import_bound("sys") | |||
| .wrap_err("failed to import `sys` module")?; | |||
| let sys = py.import("sys").wrap_err("failed to import `sys` module")?; | |||
| let sys_path = sys | |||
| .getattr("path") | |||
| .wrap_err("failed to import `sys.path` module")?; | |||
| @@ -89,14 +88,16 @@ pub fn run( | |||
| .wrap_err("failed to append module path to python search path")?; | |||
| } | |||
| let module = py.import_bound(module_name).map_err(traceback)?; | |||
| let module = py.import(module_name).map_err(traceback)?; | |||
| let operator_class = module | |||
| .getattr("Operator") | |||
| .wrap_err("no `Operator` class found in module")?; | |||
| let locals = [("Operator", operator_class)].into_py_dict_bound(py); | |||
| let locals = [("Operator", operator_class)] | |||
| .into_py_dict(py) | |||
| .context("Failed to create py_dict")?; | |||
| let operator = py | |||
| .eval_bound("Operator()", None, Some(&locals)) | |||
| .eval(c_str!("Operator()"), None, Some(&locals)) | |||
| .map_err(traceback)?; | |||
| operator.setattr( | |||
| "dataflow_descriptor", | |||
| @@ -141,11 +142,11 @@ pub fn run( | |||
| })?; | |||
| // Reload module | |||
| let module = py | |||
| .import_bound(module_name) | |||
| .import(module_name) | |||
| .map_err(traceback) | |||
| .wrap_err(format!("Could not retrieve {module_name} while reloading"))?; | |||
| let importlib = py | |||
| .import_bound("importlib") | |||
| .import("importlib") | |||
| .wrap_err("failed to import `importlib` module")?; | |||
| let module = importlib | |||
| .call_method("reload", (module,), None) | |||
| @@ -155,9 +156,11 @@ pub fn run( | |||
| .wrap_err("no `Operator` class found in module")?; | |||
| // Create a new reloaded operator | |||
| let locals = [("Operator", reloaded_operator_class)].into_py_dict_bound(py); | |||
| let locals = [("Operator", reloaded_operator_class)] | |||
| .into_py_dict(py) | |||
| .context("Failed to create py_dict")?; | |||
| let operator: Py<pyo3::PyAny> = py | |||
| .eval_bound("Operator()", None, Some(&locals)) | |||
| .eval(c_str!("Operator()"), None, Some(&locals)) | |||
| .map_err(traceback) | |||
| .wrap_err("Could not initialize reloaded operator")? | |||
| .into(); | |||
| @@ -60,7 +60,7 @@ impl Ros2Context { | |||
| pub fn new(ros_paths: Option<Vec<PathBuf>>) -> eyre::Result<Self> { | |||
| Python::with_gil(|py| -> Result<()> { | |||
| let warnings = py | |||
| .import_bound("warnings") | |||
| .import("warnings") | |||
| .wrap_err("failed to import `warnings` module")?; | |||
| warnings | |||
| .call_method1("warn", ("dora-rs ROS2 Bridge is unstable and may change at any point without it being considered a breaking change",)) | |||
| @@ -333,7 +333,7 @@ impl Ros2Publisher { | |||
| /// :rtype: None | |||
| /// | |||
| pub fn publish(&self, data: Bound<'_, PyAny>) -> eyre::Result<()> { | |||
| let pyarrow = PyModule::import_bound(data.py(), "pyarrow")?; | |||
| let pyarrow = PyModule::import(data.py(), "pyarrow")?; | |||
| let data = if data.is_instance_of::<PyDict>() { | |||
| // convert to arrow struct scalar | |||
| @@ -344,7 +344,7 @@ impl Ros2Publisher { | |||
| let data = if data.is_instance(&pyarrow.getattr("StructScalar")?)? { | |||
| // convert to arrow array | |||
| let list = PyList::new_bound(data.py(), [data]); | |||
| let list = PyList::new(data.py(), [data]).context("Failed to create Py::List")?; | |||
| pyarrow.getattr("array")?.call1((list,))? | |||
| } else { | |||
| data | |||
| @@ -37,6 +37,7 @@ mod tests { | |||
| use arrow::pyarrow::ToPyArrow; | |||
| use eyre::eyre; | |||
| use pyo3::ffi::c_str; | |||
| use pyo3::types::IntoPyDict; | |||
| use pyo3::types::PyAnyMethods; | |||
| use pyo3::types::PyDict; | |||
| @@ -65,13 +66,17 @@ mod tests { | |||
| let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); //.join("test_utils.py"); // Adjust this path as needed | |||
| // Add the Python module's directory to sys.path | |||
| py.run_bound( | |||
| "import sys; sys.path.append(str(path))", | |||
| Some(&[("path", path)].into_py_dict_bound(py)), | |||
| py.run( | |||
| c_str!("import sys; sys.path.append(str(path))"), | |||
| Some( | |||
| &[("path", path)] | |||
| .into_py_dict(py) | |||
| .context("Failed to create py_dict")?, | |||
| ), | |||
| None, | |||
| )?; | |||
| let my_module = PyModule::import_bound(py, "test_utils")?; | |||
| let my_module = PyModule::import(py, "test_utils")?; | |||
| let arrays = my_module.getattr("TEST_ARRAYS")?; | |||
| let arrays = arrays | |||
| @@ -108,16 +113,16 @@ mod tests { | |||
| let out_pyarrow = out_value.to_pyarrow(py)?; | |||
| let test_utils = PyModule::import_bound(py, "test_utils")?; | |||
| let context = PyDict::new_bound(py); | |||
| let test_utils = PyModule::import(py, "test_utils")?; | |||
| let context = PyDict::new(py); | |||
| context.set_item("test_utils", test_utils)?; | |||
| context.set_item("in_pyarrow", in_pyarrow)?; | |||
| context.set_item("out_pyarrow", out_pyarrow)?; | |||
| let _ = py | |||
| .eval_bound( | |||
| "test_utils.is_subset(in_pyarrow, out_pyarrow)", | |||
| .eval( | |||
| c_str!("test_utils.is_subset(in_pyarrow, out_pyarrow)"), | |||
| Some(&context), | |||
| None, | |||
| ) | |||