From 6bda1649c885dc886e89e34b67770b58e0740638 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Fri, 10 May 2024 10:04:07 -0400 Subject: [PATCH] Update example to PyO3 0.21. --- examples/python_rust_compiled_function/Cargo.toml | 2 +- examples/python_rust_compiled_function/src/ffi.rs | 14 +++++++------- examples/python_rust_compiled_function/src/lib.rs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/python_rust_compiled_function/Cargo.toml b/examples/python_rust_compiled_function/Cargo.toml index da8b5f37096a..94982fe498ef 100644 --- a/examples/python_rust_compiled_function/Cargo.toml +++ b/examples/python_rust_compiled_function/Cargo.toml @@ -14,4 +14,4 @@ polars = { path = "../../crates/polars" } pyo3 = { workspace = true, features = ["extension-module"] } [build-dependencies] -pyo3-build-config = "0.20" +pyo3-build-config = "0.21" diff --git a/examples/python_rust_compiled_function/src/ffi.rs b/examples/python_rust_compiled_function/src/ffi.rs index 16e4f09a440c..22222e8e20f8 100644 --- a/examples/python_rust_compiled_function/src/ffi.rs +++ b/examples/python_rust_compiled_function/src/ffi.rs @@ -7,7 +7,7 @@ use pyo3::{PyAny, PyObject, PyResult}; /// Take an arrow array from python and convert it to a rust arrow array. /// This operation does not copy data. -fn array_to_rust(arrow_array: &PyAny) -> PyResult { +fn array_to_rust(arrow_array: &Bound) -> PyResult { // prepare a pointer to receive the Array struct let array = Box::new(ffi::ArrowArray::empty()); let schema = Box::new(ffi::ArrowSchema::empty()); @@ -30,7 +30,7 @@ fn array_to_rust(arrow_array: &PyAny) -> PyResult { } /// Arrow array to Python. -pub(crate) fn to_py_array(py: Python, pyarrow: &PyModule, array: ArrayRef) -> PyResult { +pub(crate) fn to_py_array(py: Python, pyarrow: &Bound, array: ArrayRef) -> PyResult { let schema = Box::new(ffi::export_field_to_c(&ArrowField::new( "", array.data_type().clone(), @@ -49,7 +49,7 @@ pub(crate) fn to_py_array(py: Python, pyarrow: &PyModule, array: ArrayRef) -> Py Ok(array.to_object(py)) } -pub fn py_series_to_rust_series(series: &PyAny) -> PyResult { +pub fn py_series_to_rust_series(series: &Bound) -> PyResult { // rechunk series so that they have a single arrow array let series = series.call_method0("rechunk")?; @@ -59,7 +59,7 @@ pub fn py_series_to_rust_series(series: &PyAny) -> PyResult { let array = series.call_method0("to_arrow")?; // retrieve rust arrow array - let array = array_to_rust(array)?; + let array = array_to_rust(&array)?; Series::try_from((name.as_str(), array)).map_err(|e| PyValueError::new_err(format!("{}", e))) } @@ -71,13 +71,13 @@ pub fn rust_series_to_py_series(series: &Series) -> PyResult { Python::with_gil(|py| { // import pyarrow - let pyarrow = py.import("pyarrow")?; + let pyarrow = py.import_bound("pyarrow")?; // pyarrow array - let pyarrow_array = to_py_array(py, pyarrow, array)?; + let pyarrow_array = to_py_array(py, &pyarrow, array)?; // import polars - let polars = py.import("polars")?; + let polars = py.import_bound("polars")?; let out = polars.call_method1("from_arrow", (pyarrow_array,))?; Ok(out.to_object(py)) }) diff --git a/examples/python_rust_compiled_function/src/lib.rs b/examples/python_rust_compiled_function/src/lib.rs index 71708aa90475..f8c2caec2123 100644 --- a/examples/python_rust_compiled_function/src/lib.rs +++ b/examples/python_rust_compiled_function/src/lib.rs @@ -5,7 +5,7 @@ use pyo3::exceptions::PyValueError; use pyo3::prelude::*; #[pyfunction] -fn hamming_distance(series_a: &PyAny, series_b: &PyAny) -> PyResult { +fn hamming_distance(series_a: &Bound, series_b: &Bound) -> PyResult { let series_a = ffi::py_series_to_rust_series(series_a)?; let series_b = ffi::py_series_to_rust_series(series_b)?; @@ -44,7 +44,7 @@ fn hamming_distance_strs(a: Option<&str>, b: Option<&str>) -> Option { } #[pymodule] -fn my_polars_functions(_py: Python, m: &PyModule) -> PyResult<()> { +fn my_polars_functions(_py: Python, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(hamming_distance)).unwrap(); Ok(()) }