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

build(python): Update pyo3 and numpy crates to version 0.23 #20111

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ memmap = { package = "memmap2", version = "0.9" }
multiversion = "0.7"
ndarray = { version = "0.16", default-features = false }
num-traits = "0.2"
numpy = "0.22"
numpy = "0.23"
object_store = { version = "0.11", default-features = false }
once_cell = "1"
parking_lot = "0.12"
percent-encoding = "2.3"
pin-project-lite = "0.2"
pyo3 = "0.22"
pyo3 = "0.23"
rand = "0.8"
rand_distr = "0.4"
raw-cpuid = "11"
Expand Down
7 changes: 7 additions & 0 deletions crates/polars-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod warning;

use std::borrow::Cow;
use std::collections::TryReserveError;
use std::convert::Infallible;
use std::error::Error;
use std::fmt::{self, Display, Formatter, Write};
use std::ops::Deref;
Expand Down Expand Up @@ -168,6 +169,12 @@ impl From<TryReserveError> for PolarsError {
}
}

impl From<Infallible> for PolarsError {
fn from(_: Infallible) -> Self {
unreachable!()
}
}

pub type PolarsResult<T> = Result<T, PolarsError>;

impl PolarsError {
Expand Down
16 changes: 8 additions & 8 deletions crates/polars-mem-engine/src/executors/scan/python_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use polars_core::error::to_compute_err;
use polars_core::utils::accumulate_dataframes_vertical;
use pyo3::exceptions::PyStopIteration;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::{intern, PyTypeInfo};
use pyo3::types::{PyBytes, PyNone};
use pyo3::{intern, IntoPyObjectExt, PyTypeInfo};

use super::*;

Expand Down Expand Up @@ -41,7 +41,7 @@ impl Executor for PythonScanExec {
let with_columns = self.options.with_columns.take();
let n_rows = self.options.n_rows.take();
Python::with_gil(|py| {
let pl = PyModule::import_bound(py, intern!(py, "polars")).unwrap();
let pl = PyModule::import(py, intern!(py, "polars")).unwrap();
let utils = pl.getattr(intern!(py, "_utils")).unwrap();
let callable = utils.getattr(intern!(py, "_execute_from_rust")).unwrap();

Expand All @@ -50,14 +50,14 @@ impl Executor for PythonScanExec {
let with_columns = with_columns.map(|cols| cols.iter().cloned().collect::<Vec<_>>());

let predicate = match &self.options.predicate {
PythonPredicate::PyArrow(s) => s.into_py(py),
PythonPredicate::None => None::<()>.into_py(py),
PythonPredicate::PyArrow(s) => s.into_bound_py_any(py).unwrap(),
PythonPredicate::None => None::<()>.into_bound_py_any(py).unwrap(),
PythonPredicate::Polars(_) => {
assert!(self.predicate.is_some(), "should be set");

match &self.predicate_serialized {
None => None::<()>.into_py(py),
Some(buf) => PyBytes::new_bound(py, buf).to_object(py),
None => PyNone::get(py).to_owned().into_any(),
Some(buf) => PyBytes::new(py, buf).into_any(),
}
},
};
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Executor for PythonScanExec {
}
chunks.push(df)
},
Err(err) if err.matches(py, PyStopIteration::type_object_bound(py)) => break,
Err(err) if err.matches(py, PyStopIteration::type_object(py))? => break,
Err(err) => {
polars_bail!(ComputeError: "caught exception during execution of a Python source, exception: {}", err)
},
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-plan/src/dsl/python_udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ impl PythonUdfExpression {

// Load UDF
Python::with_gil(|py| {
let pickle = PyModule::import_bound(py, "pickle")
let pickle = PyModule::import(py, "pickle")
.expect("unable to import 'pickle'")
.getattr("loads")
.unwrap();
let arg = (PyBytes::new_bound(py, remainder),);
let arg = (PyBytes::new(py, remainder),);
let python_function = pickle.call1(arg).map_err(from_pyerr)?;
Ok(Arc::new(Self::new(
python_function.into(),
Expand Down Expand Up @@ -136,15 +136,15 @@ impl ColumnsUdf for PythonUdfExpression {

Python::with_gil(|py| {
// Try pickle to serialize the UDF, otherwise fall back to cloudpickle.
let pickle = PyModule::import_bound(py, "pickle")
let pickle = PyModule::import(py, "pickle")
.expect("unable to import 'pickle'")
.getattr("dumps")
.unwrap();
let pickle_result = pickle.call1((self.python_function.clone_ref(py),));
let (dumped, use_cloudpickle) = match pickle_result {
Ok(dumped) => (dumped, false),
Err(_) => {
let cloudpickle = PyModule::import_bound(py, "cloudpickle")
let cloudpickle = PyModule::import(py, "cloudpickle")
.map_err(from_pyerr)?
.getattr("dumps")
.unwrap();
Expand Down
12 changes: 6 additions & 6 deletions crates/polars-python/src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use crate::lazyframe::visit::NodeTraverser;
use crate::{PyDataFrame, PyLazyFrame};

#[pyfunction]
pub fn prepare_cloud_plan(lf: PyLazyFrame, py: Python) -> PyResult<PyObject> {
pub fn prepare_cloud_plan(lf: PyLazyFrame, py: Python<'_>) -> PyResult<Bound<'_, PyBytes>> {
let plan = lf.ldf.logical_plan;
let bytes = polars::prelude::prepare_cloud_plan(plan).map_err(PyPolarsErr::from)?;

Ok(PyBytes::new_bound(py, &bytes).to_object(py))
Ok(PyBytes::new(py, &bytes))
}

/// Take a serialized `IRPlan` and execute it on the GPU engine.
Expand Down Expand Up @@ -62,13 +62,13 @@ fn gpu_post_opt(
expr_arena: &mut Arena<AExpr>,
) -> PolarsResult<()> {
// Get cuDF Python function.
let cudf = PyModule::import_bound(py, intern!(py, "cudf_polars")).unwrap();
let cudf = PyModule::import(py, intern!(py, "cudf_polars")).unwrap();
let lambda = cudf.getattr(intern!(py, "execute_with_cudf")).unwrap();

// Define cuDF config.
let polars = PyModule::import_bound(py, intern!(py, "polars")).unwrap();
let polars = PyModule::import(py, intern!(py, "polars")).unwrap();
let engine = polars.getattr(intern!(py, "GPUEngine")).unwrap();
let kwargs = [("raise_on_fail", true)].into_py_dict_bound(py);
let kwargs = [("raise_on_fail", true)].into_py_dict(py).unwrap();
let engine = engine.call((), Some(&kwargs)).unwrap();

// Define node traverser.
Expand All @@ -79,7 +79,7 @@ fn gpu_post_opt(

// Pass the node visitor which allows the Python callback to replace parts of the query plan.
// Remove "cuda" or specify better once we have multiple post-opt callbacks.
let kwargs = [("config", engine)].into_py_dict_bound(py);
let kwargs = [("config", engine)].into_py_dict(py).unwrap();
lambda
.call((nt,), Some(&kwargs))
.map_err(|e| polars_err!(ComputeError: "'cuda' conversion failed: {}", e))?;
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-python/src/conversion/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub(crate) fn any_value_into_py_object(av: AnyValue, py: Python) -> PyObject {
let object = v.0.as_any().downcast_ref::<ObjectValue>().unwrap();
object.inner.clone_ref(py)
},
AnyValue::Binary(v) => PyBytes::new_bound(py, v).into_py(py),
AnyValue::BinaryOwned(v) => PyBytes::new_bound(py, &v).into_py(py),
AnyValue::Binary(v) => PyBytes::new(py, v).into_py(py),
AnyValue::BinaryOwned(v) => PyBytes::new(py, &v).into_py(py),
AnyValue::Decimal(v, scale) => {
let convert = utils.getattr(intern!(py, "to_py_decimal")).unwrap();
const N: usize = 3;
Expand Down Expand Up @@ -351,7 +351,7 @@ pub(crate) fn py_object_to_any_value<'py>(
// This constructor is able to go via dedicated type constructors
// so it can be much faster.
let py = ob.py();
let kwargs = PyDict::new_bound(py);
let kwargs = PyDict::new(py);
kwargs.set_item("strict", strict)?;
let s = SERIES.call_bound(py, (ob,), Some(&kwargs))?;
get_list_from_series(s.bind(py), strict)
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/conversion/chunked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ToPyObject for Wrap<&BinaryChunked> {
let iter = self
.0
.iter()
.map(|opt_bytes| opt_bytes.map(|bytes| PyBytes::new_bound(py, bytes)));
.map(|opt_bytes| opt_bytes.map(|bytes| PyBytes::new(py, bytes)));
PyList::new_bound(py, iter).into_py(py)
}
}
Expand Down
12 changes: 8 additions & 4 deletions crates/polars-python/src/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub(crate) mod any_value;
pub(crate) mod chunked_array;
mod datetime;
Expand Down Expand Up @@ -149,9 +151,10 @@ fn struct_dict<'a>(
vals: impl Iterator<Item = AnyValue<'a>>,
flds: &[Field],
) -> PyObject {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
for (fld, val) in flds.iter().zip(vals) {
dict.set_item(fld.name().as_str(), Wrap(val)).unwrap()
dict.set_item(fld.name().as_str(), Wrap(val).into_py(py))
.unwrap()
}
dict.into_py(py)
}
Expand Down Expand Up @@ -596,9 +599,10 @@ impl<'py> FromPyObject<'py> for Wrap<ScanSources> {

impl IntoPy<PyObject> for Wrap<&Schema> {
fn into_py(self, py: Python<'_>) -> PyObject {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
for (k, v) in self.0.iter() {
dict.set_item(k.as_str(), Wrap(v.clone())).unwrap();
dict.set_item(k.as_str(), Wrap(v.clone()).to_object(py))
.unwrap();
}
dict.into_py(py)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn dicts_to_rows<'a>(
) -> PyResult<Vec<Row<'a>>> {
let len = data.len()?;
let mut rows = Vec::with_capacity(len);
for d in data.iter()? {
for d in data.try_iter()? {
let d = d?;
let d = d.downcast::<PyDict>()?;

Expand Down Expand Up @@ -189,7 +189,7 @@ fn infer_schema_names_from_data(
.unwrap_or(data_len);

let mut names = PlIndexSet::new();
for d in data.iter()?.take(infer_schema_length) {
for d in data.try_iter()?.take(infer_schema_length) {
let d = d?;
let d = d.downcast::<PyDict>()?;
let keys = d.keys();
Expand Down
Loading
Loading