Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(python): More PyO3 0.21 Bound<> APIs, and finally disable gil-refs backwards compat feature on pyo3 crate #16143

Merged
merged 9 commits into from
May 14, 2024
9 changes: 5 additions & 4 deletions crates/polars-plan/src/dsl/python_udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use polars_core::error::*;
use polars_core::frame::DataFrame;
use polars_core::prelude::Series;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::PyBytes;
#[cfg(feature = "serde")]
use serde::ser::Error;
Expand Down Expand Up @@ -67,9 +68,9 @@ impl Serialize for PythonFunction {
let dumped = pickle
.call1((python_function,))
.map_err(|s| S::Error::custom(format!("cannot pickle {s}")))?;
let dumped = dumped.extract::<&PyBytes>().unwrap();
let dumped = dumped.extract::<PyBackedBytes>().unwrap();

serializer.serialize_bytes(dumped.as_bytes())
serializer.serialize_bytes(&dumped)
})
}
}
Expand Down Expand Up @@ -192,8 +193,8 @@ impl SeriesUdf for PythonUdfExpression {
let dumped = pickle
.call1((self.python_function.clone(),))
.map_err(from_pyerr)?;
let dumped = dumped.extract::<&PyBytes>().unwrap();
buf.extend_from_slice(dumped.as_bytes());
let dumped = dumped.extract::<PyBackedBytes>().unwrap();
buf.extend_from_slice(&dumped);
Ok(())
})
}
Expand Down
2 changes: 1 addition & 1 deletion py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ndarray = { workspace = true }
num-traits = { workspace = true }
numpy = { version = "0.21", default-features = false }
once_cell = { workspace = true }
pyo3 = { workspace = true, features = ["abi3-py38", "extension-module", "multiple-pymethods", "gil-refs"] }
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
pyo3 = { workspace = true, features = ["abi3-py38", "extension-module", "multiple-pymethods"] }
pyo3-built = { version = "0.5", optional = true }
recursive = { workspace = true }
serde_json = { workspace = true, optional = true }
Expand Down
5 changes: 3 additions & 2 deletions py-polars/src/conversion/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use polars_core::utils::any_values_to_supertype_and_n_dtypes;
use pyo3::exceptions::{PyOverflowError, PyTypeError};
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::{PyBool, PyBytes, PyDict, PyFloat, PyInt, PyList, PySequence, PyString, PyTuple};

use super::{decimal_to_digits, struct_dict, ObjectValue, Wrap};
Expand Down Expand Up @@ -171,8 +172,8 @@ pub(crate) fn py_object_to_any_value<'py>(
}

fn get_bytes<'py>(ob: &Bound<'py, PyAny>, _strict: bool) -> PyResult<AnyValue<'py>> {
let value = ob.extract::<&'py [u8]>().unwrap();
Ok(AnyValue::Binary(value))
let value = ob.extract::<PyBackedBytes>().unwrap();
itamarst marked this conversation as resolved.
Show resolved Hide resolved
Ok(AnyValue::BinaryOwned(value.to_vec()))
}

fn get_date(ob: &Bound<'_, PyAny>, _strict: bool) -> PyResult<AnyValue<'static>> {
Expand Down