diff --git a/crates/polars-python/src/conversion/any_value.rs b/crates/polars-python/src/conversion/any_value.rs index 1ac41ce31bd1..699792f282ff 100644 --- a/crates/polars-python/src/conversion/any_value.rs +++ b/crates/polars-python/src/conversion/any_value.rs @@ -362,7 +362,7 @@ pub(crate) fn py_object_to_any_value<'py>( let py = ob.py(); let kwargs = PyDict::new(py); kwargs.set_item("strict", strict)?; - let s = pl_series(py).call_bound(py, (ob,), Some(&kwargs))?; + let s = pl_series(py).call(py, (ob,), Some(&kwargs))?; get_list_from_series(s.bind(py), strict) } @@ -374,10 +374,10 @@ pub(crate) fn py_object_to_any_value<'py>( } else if ob.is_instance_of::() | ob.is_instance_of::() { const INFER_SCHEMA_LENGTH: usize = 25; - let list = ob.downcast::().unwrap(); + let list = ob.downcast::()?; let mut avs = Vec::with_capacity(INFER_SCHEMA_LENGTH); - let mut iter = list.iter()?; + let mut iter = list.try_iter()?; let mut items = Vec::with_capacity(INFER_SCHEMA_LENGTH); for item in (&mut iter).take(INFER_SCHEMA_LENGTH) { items.push(item?); @@ -477,7 +477,7 @@ pub(crate) fn py_object_to_any_value<'py>( Ok(get_struct) } else { let ob_type = ob.get_type(); - let type_name = ob_type.qualname().unwrap().to_string(); + let type_name = ob_type.qualname()?.to_string(); match type_name.as_str() { // Can't use pyo3::types::PyDateTime with abi3-py37 feature, // so need this workaround instead of `isinstance(ob, datetime)`. @@ -496,10 +496,9 @@ pub(crate) fn py_object_to_any_value<'py>( } // Support custom subclasses of datetime/date. - let ancestors = ob_type.getattr(intern!(py, "__mro__")).unwrap(); + let ancestors = ob_type.getattr(intern!(py, "__mro__"))?; let ancestors_str_iter = ancestors - .iter() - .unwrap() + .try_iter()? .map(|b| b.unwrap().str().unwrap().to_string()); for c in ancestors_str_iter { match &*c { diff --git a/crates/polars-python/src/conversion/chunked_array.rs b/crates/polars-python/src/conversion/chunked_array.rs index 6cfa93664c47..de8ef187d4c4 100644 --- a/crates/polars-python/src/conversion/chunked_array.rs +++ b/crates/polars-python/src/conversion/chunked_array.rs @@ -136,7 +136,7 @@ impl<'py> IntoPyObject<'py> for &Wrap<&DecimalChunked> { type Output = Bound<'py, Self::Target>; type Error = PyErr; fn into_pyobject(self, py: Python<'py>) -> Result { - let iter = decimal_to_pyobject_iter(py, self.0); + let iter = decimal_to_pyobject_iter(py, self.0)?; PyList::new(py, iter) } } @@ -144,13 +144,13 @@ impl<'py> IntoPyObject<'py> for &Wrap<&DecimalChunked> { pub(crate) fn decimal_to_pyobject_iter<'py, 'a>( py: Python<'py>, ca: &'a DecimalChunked, -) -> impl ExactSizeIterator>> + use<'py, 'a> { +) -> PyResult>> + use<'py, 'a>> { let utils = pl_utils(py).bind(py); - let convert = utils.getattr(intern!(py, "to_py_decimal")).unwrap(); - let py_scale = (-(ca.scale() as i32)).to_object(py); + let convert = utils.getattr(intern!(py, "to_py_decimal"))?; + let py_scale = (-(ca.scale() as i32)).into_pyobject(py)?; // if we don't know precision, the only safe bet is to set it to 39 - let py_precision = ca.precision().unwrap_or(39).to_object(py); - ca.iter().map(move |opt_v| { + let py_precision = ca.precision().unwrap_or(39).into_pyobject(py)?; + Ok(ca.iter().map(move |opt_v| { opt_v.map(|v| { // TODO! use AnyValue so that we have a single impl. const N: usize = 3; @@ -162,10 +162,10 @@ pub(crate) fn decimal_to_pyobject_iter<'py, 'a>( N * size_of::(), ) }; - let digits = PyTuple::new_bound(py, buf.iter().take(n_digits)); + let digits = PyTuple::new(py, buf.iter().take(n_digits)).unwrap(); convert .call1((v.is_negative() as u8, digits, &py_precision, &py_scale)) .unwrap() }) - }) + })) } diff --git a/crates/polars-python/src/conversion/mod.rs b/crates/polars-python/src/conversion/mod.rs index e6c0ec297e49..8bc33aeced01 100644 --- a/crates/polars-python/src/conversion/mod.rs +++ b/crates/polars-python/src/conversion/mod.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - pub(crate) mod any_value; pub(crate) mod chunked_array; mod datetime; @@ -109,12 +107,10 @@ pub(crate) fn get_series(obj: &Bound<'_, PyAny>) -> PyResult { Ok(s.extract::()?.series) } -pub(crate) fn to_series(py: Python, s: PySeries) -> PyObject { +pub(crate) fn to_series(py: Python, s: PySeries) -> PyResult> { let series = pl_series(py).bind(py); - let constructor = series - .getattr(intern!(series.py(), "_from_pyseries")) - .unwrap(); - constructor.call1((s,)).unwrap().into_py(py) + let constructor = series.getattr(intern!(py, "_from_pyseries"))?; + constructor.call1((s,)) } impl<'a> FromPyObject<'a> for Wrap { @@ -287,7 +283,7 @@ impl<'py> IntoPyObject<'py> for &Wrap { let s = Series::from_arrow(PlSmallStr::from_static("category"), categories.to_boxed()) .map_err(PyPolarsErr::from)?; - let series = to_series(py, s.into()); + let series = to_series(py, s.into())?; class.call1((series,)) }, DataType::Time => pl.getattr(intern!(py, "Time")), @@ -686,10 +682,8 @@ impl From for ObjectValue { impl<'a> FromPyObject<'a> for ObjectValue { fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult { - Python::with_gil(|py| { - Ok(ObjectValue { - inner: ob.to_object(py), - }) + Ok(ObjectValue { + inner: ob.to_owned().unbind(), }) } } @@ -724,7 +718,7 @@ impl<'a, T: NativeType + FromPyObject<'a>> FromPyObject<'a> for Wrap> { fn extract_bound(obj: &Bound<'a, PyAny>) -> PyResult { let seq = obj.downcast::()?; let mut v = Vec::with_capacity(seq.len().unwrap_or(0)); - for item in seq.iter()? { + for item in seq.try_iter()? { v.push(item?.extract::()?); } Ok(Wrap(v)) diff --git a/crates/polars-python/src/dataframe/general.rs b/crates/polars-python/src/dataframe/general.rs index f78ef4e742da..00f7534086a2 100644 --- a/crates/polars-python/src/dataframe/general.rs +++ b/crates/polars-python/src/dataframe/general.rs @@ -10,6 +10,7 @@ use pyo3::exceptions::PyIndexError; use pyo3::prelude::*; use pyo3::pybacked::PyBackedStr; use pyo3::types::PyList; +use pyo3::IntoPyObjectExt; use self::row_encode::get_row_encoding_dictionary; use super::PyDataFrame; @@ -568,7 +569,7 @@ impl PyDataFrame { _ => return apply_lambda_unknown(df, py, lambda, inference_size), }; - Ok((PySeries::from(out).into_py(py), false)) + Ok((PySeries::from(out).into_py_any(py)?, false)) }) } diff --git a/crates/polars-python/src/dataframe/mod.rs b/crates/polars-python/src/dataframe/mod.rs index cdadb6298148..fbd514ab2c03 100644 --- a/crates/polars-python/src/dataframe/mod.rs +++ b/crates/polars-python/src/dataframe/mod.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - #[cfg(feature = "pymethods")] mod construction; #[cfg(feature = "pymethods")] diff --git a/crates/polars-python/src/expr/list.rs b/crates/polars-python/src/expr/list.rs index af3be10449b1..b8f10fc60c3e 100644 --- a/crates/polars-python/src/expr/list.rs +++ b/crates/polars-python/src/expr/list.rs @@ -244,7 +244,7 @@ impl PyExpr { .list() .to_struct(ListToStructArgs::FixedWidth( names - .iter()? + .try_iter()? .map(|x| Ok(x?.extract::>()?.0)) .collect::>>()?, )) diff --git a/crates/polars-python/src/expr/mod.rs b/crates/polars-python/src/expr/mod.rs index 56fdd2f001fd..93a00018a683 100644 --- a/crates/polars-python/src/expr/mod.rs +++ b/crates/polars-python/src/expr/mod.rs @@ -1,4 +1,3 @@ -#![allow(deprecated)] #[cfg(feature = "pymethods")] mod array; #[cfg(feature = "pymethods")] diff --git a/crates/polars-python/src/expr/serde.rs b/crates/polars-python/src/expr/serde.rs index 949005f08389..0900c40e0722 100644 --- a/crates/polars-python/src/expr/serde.rs +++ b/crates/polars-python/src/expr/serde.rs @@ -12,26 +12,22 @@ use crate::PyExpr; #[pymethods] impl PyExpr { - fn __getstate__(&self, py: Python) -> PyResult { + fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult> { // Used in pickle/pickling let mut writer: Vec = vec![]; ciborium::ser::into_writer(&self.inner, &mut writer) .map_err(|e| PyPolarsErr::Other(format!("{}", e)))?; - Ok(PyBytes::new(py, &writer).to_object(py)) + Ok(PyBytes::new(py, &writer)) } fn __setstate__(&mut self, state: &Bound) -> PyResult<()> { // Used in pickle/pickling - match state.extract::() { - Ok(s) => { - let cursor = Cursor::new(&*s); - self.inner = ciborium::de::from_reader(cursor) - .map_err(|e| PyPolarsErr::Other(format!("{}", e)))?; - Ok(()) - }, - Err(e) => Err(e), - } + let bytes = state.extract::()?; + let cursor = Cursor::new(&*bytes); + self.inner = + ciborium::de::from_reader(cursor).map_err(|e| PyPolarsErr::Other(format!("{}", e)))?; + Ok(()) } /// Serialize into binary data. diff --git a/crates/polars-python/src/functions/eager.rs b/crates/polars-python/src/functions/eager.rs index f271b818c8ce..5f68067824f1 100644 --- a/crates/polars-python/src/functions/eager.rs +++ b/crates/polars-python/src/functions/eager.rs @@ -11,7 +11,7 @@ pub fn concat_df(dfs: &Bound<'_, PyAny>, py: Python) -> PyResult { use polars_core::error::PolarsResult; use polars_core::utils::rayon::prelude::*; - let mut iter = dfs.iter()?; + let mut iter = dfs.try_iter()?; let first = iter.next().unwrap()?; let first_rdf = get_df(&first)?; @@ -49,7 +49,7 @@ pub fn concat_df(dfs: &Bound<'_, PyAny>, py: Python) -> PyResult { #[pyfunction] pub fn concat_series(series: &Bound<'_, PyAny>) -> PyResult { - let mut iter = series.iter()?; + let mut iter = series.try_iter()?; let first = iter.next().unwrap()?; let mut s = get_series(&first)?; @@ -64,7 +64,7 @@ pub fn concat_series(series: &Bound<'_, PyAny>) -> PyResult { #[pyfunction] pub fn concat_df_diagonal(dfs: &Bound<'_, PyAny>) -> PyResult { - let iter = dfs.iter()?; + let iter = dfs.try_iter()?; let dfs = iter .map(|item| { @@ -79,7 +79,7 @@ pub fn concat_df_diagonal(dfs: &Bound<'_, PyAny>) -> PyResult { #[pyfunction] pub fn concat_df_horizontal(dfs: &Bound<'_, PyAny>) -> PyResult { - let iter = dfs.iter()?; + let iter = dfs.try_iter()?; let dfs = iter .map(|item| { diff --git a/crates/polars-python/src/functions/io.rs b/crates/polars-python/src/functions/io.rs index 7beca957cac1..cc5f075ae621 100644 --- a/crates/polars-python/src/functions/io.rs +++ b/crates/polars-python/src/functions/io.rs @@ -15,7 +15,7 @@ use crate::prelude::ArrowDataType; #[cfg(feature = "ipc")] #[pyfunction] -pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult { +pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult> { use polars_core::export::arrow::io::ipc::read::read_file_metadata; let metadata = match get_either_file(py_f, false)? { EitherRustPythonFile::Rust(r) => { @@ -26,12 +26,12 @@ pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult { let dict = PyDict::new(py); fields_to_pydict(&metadata.schema, &dict)?; - Ok(dict.to_object(py)) + Ok(dict) } #[cfg(feature = "parquet")] #[pyfunction] -pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult { +pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult> { use polars_parquet::read::{infer_schema, read_metadata}; let metadata = match get_either_file(py_f, false)? { @@ -44,7 +44,7 @@ pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult { let dict = PyDict::new(py); fields_to_pydict(&arrow_schema, &dict)?; - Ok(dict.to_object(py)) + Ok(dict) } #[cfg(any(feature = "ipc", feature = "parquet"))] diff --git a/crates/polars-python/src/functions/lazy.rs b/crates/polars-python/src/functions/lazy.rs index 3a437b3281d3..4185fadff3f2 100644 --- a/crates/polars-python/src/functions/lazy.rs +++ b/crates/polars-python/src/functions/lazy.rs @@ -10,7 +10,7 @@ use crate::conversion::{get_lf, Wrap}; use crate::error::PyPolarsErr; use crate::expr::ToExprs; use crate::map::lazy::binary_lambda; -use crate::prelude::{vec_extract_wrapped, ObjectValue}; +use crate::prelude::vec_extract_wrapped; use crate::{map, PyDataFrame, PyExpr, PyLazyFrame, PySeries}; macro_rules! set_unwrapped_or_0 { @@ -151,7 +151,7 @@ pub fn collect_all_with_callback(lfs: Vec, lambda: PyObject) { }, Err(err) => { lambda - .call1(py, (PyErr::from(err).to_object(py),)) + .call1(py, (PyErr::from(err),)) .map_err(|err| err.restore(py)) .ok(); }, @@ -174,7 +174,7 @@ pub fn concat_lf( let len = seq.len()?; let mut lfs = Vec::with_capacity(len); - for res in seq.iter()? { + for res in seq.try_iter()? { let item = res?; let lf = get_lf(&item)?; lfs.push(lf); @@ -302,7 +302,7 @@ pub fn concat_lf_diagonal( parallel: bool, to_supertypes: bool, ) -> PyResult { - let iter = lfs.iter()?; + let iter = lfs.try_iter()?; let lfs = iter .map(|item| { @@ -326,7 +326,7 @@ pub fn concat_lf_diagonal( #[pyfunction] pub fn concat_lf_horizontal(lfs: &Bound<'_, PyAny>, parallel: bool) -> PyResult { - let iter = lfs.iter()?; + let iter = lfs.try_iter()?; let lfs = iter .map(|item| { @@ -437,8 +437,9 @@ pub fn nth(n: i64) -> PyExpr { #[pyfunction] pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyResult { + let py = value.py(); if value.is_instance_of::() { - let val = value.extract::().unwrap(); + let val = value.extract::()?; Ok(dsl::lit(val).into()) } else if let Ok(int) = value.downcast::() { let v = int @@ -447,7 +448,7 @@ pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyR .map_err(PyPolarsErr::from)?; Ok(Expr::Literal(LiteralValue::Int(v)).into()) } else if let Ok(float) = value.downcast::() { - let val = float.extract::().unwrap(); + let val = float.extract::()?; Ok(Expr::Literal(LiteralValue::Float(val)).into()) } else if let Ok(pystr) = value.downcast::() { Ok(dsl::lit(pystr.to_string()).into()) @@ -479,10 +480,7 @@ pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyR match av { #[cfg(feature = "object")] AnyValue::ObjectOwned(_) => { - let s = Python::with_gil(|py| { - PySeries::new_object(py, "", vec![ObjectValue::from(value.into_py(py))], false) - .series - }); + let s = PySeries::new_object(py, "", vec![value.extract()?], false).series; Ok(dsl::lit(s).into()) }, _ => Ok(Expr::Literal(LiteralValue::from(av)).into()), diff --git a/crates/polars-python/src/functions/mod.rs b/crates/polars-python/src/functions/mod.rs index 7242f3d83719..ddf58c7acde6 100644 --- a/crates/polars-python/src/functions/mod.rs +++ b/crates/polars-python/src/functions/mod.rs @@ -1,4 +1,3 @@ -#![allow(deprecated)] mod aggregation; mod business; mod eager; diff --git a/crates/polars-python/src/interop/numpy/to_numpy_df.rs b/crates/polars-python/src/interop/numpy/to_numpy_df.rs index 73f9fcafaf06..dc7f5b8cbfeb 100644 --- a/crates/polars-python/src/interop/numpy/to_numpy_df.rs +++ b/crates/polars-python/src/interop/numpy/to_numpy_df.rs @@ -187,7 +187,7 @@ where let first_slice = ca.data_views().next().unwrap(); let start_ptr = first_slice.as_ptr(); - let np_dtype = T::Native::get_dtype_bound(py); + let np_dtype = T::Native::get_dtype(py); let dims = [first_slice.len(), df.width()].into_dimension(); unsafe { diff --git a/crates/polars-python/src/interop/numpy/to_numpy_series.rs b/crates/polars-python/src/interop/numpy/to_numpy_series.rs index ea89bd7314e7..903a1f68e1e2 100644 --- a/crates/polars-python/src/interop/numpy/to_numpy_series.rs +++ b/crates/polars-python/src/interop/numpy/to_numpy_series.rs @@ -120,7 +120,7 @@ fn series_to_numpy_view_recursive(py: Python, s: Series, writable: bool) -> PyOb fn numeric_series_to_numpy_view(py: Python, s: Series, writable: bool) -> PyObject { let dims = [s.len()].into_dimension(); with_match_physical_numeric_polars_type!(s.dtype(), |$T| { - let np_dtype = <$T as PolarsNumericType>::Native::get_dtype_bound(py); + let np_dtype = <$T as PolarsNumericType>::Native::get_dtype(py); let ca: &ChunkedArray<$T> = s.unpack::<$T>().unwrap(); let flags = if writable { flags::NPY_ARRAY_FARRAY @@ -248,7 +248,9 @@ fn series_to_numpy_with_copy(py: Python, s: &Series, writable: bool) -> PyObject }, Decimal(_, _) => { let ca = s.decimal().unwrap(); - let values = decimal_to_pyobject_iter(py, ca).map(|v| v.into_py(py)); + let values = decimal_to_pyobject_iter(py, ca) + .unwrap() + .map(|v| v.into_py(py)); PyArray1::from_iter_bound(py, values).into_py(py) }, List(_) => list_series_to_numpy(py, s, writable), diff --git a/crates/polars-python/src/interop/numpy/utils.rs b/crates/polars-python/src/interop/numpy/utils.rs index dc0a47fcac26..df292e0f7fdf 100644 --- a/crates/polars-python/src/interop/numpy/utils.rs +++ b/crates/polars-python/src/interop/numpy/utils.rs @@ -105,23 +105,21 @@ pub(super) fn polars_dtype_to_np_temporal_dtype<'a>( use numpy::datetime::{units, Datetime, Timedelta}; match dtype { DataType::Datetime(TimeUnit::Milliseconds, _) => { - Datetime::::get_dtype_bound(py) + Datetime::::get_dtype(py) }, DataType::Datetime(TimeUnit::Microseconds, _) => { - Datetime::::get_dtype_bound(py) + Datetime::::get_dtype(py) }, DataType::Datetime(TimeUnit::Nanoseconds, _) => { - Datetime::::get_dtype_bound(py) + Datetime::::get_dtype(py) }, DataType::Duration(TimeUnit::Milliseconds) => { - Timedelta::::get_dtype_bound(py) + Timedelta::::get_dtype(py) }, DataType::Duration(TimeUnit::Microseconds) => { - Timedelta::::get_dtype_bound(py) - }, - DataType::Duration(TimeUnit::Nanoseconds) => { - Timedelta::::get_dtype_bound(py) + Timedelta::::get_dtype(py) }, + DataType::Duration(TimeUnit::Nanoseconds) => Timedelta::::get_dtype(py), _ => panic!("only Datetime/Duration inputs supported, got {}", dtype), } } diff --git a/crates/polars-python/src/lazyframe/general.rs b/crates/polars-python/src/lazyframe/general.rs index 263680f46485..95f723fed3f9 100644 --- a/crates/polars-python/src/lazyframe/general.rs +++ b/crates/polars-python/src/lazyframe/general.rs @@ -238,7 +238,7 @@ impl PyLazyFrame { let f = |schema: Schema| { let iter = schema.iter_names().map(|s| s.as_str()); Python::with_gil(|py| { - let names = PyList::new_bound(py, iter); + let names = PyList::new(py, iter).unwrap(); let out = lambda.call1(py, (names,)).expect("python function failed"); let new_names = out @@ -662,7 +662,7 @@ impl PyLazyFrame { }, Err(err) => { lambda - .call1(py, (PyErr::from(err).to_object(py),)) + .call1(py, (PyErr::from(err),)) .map_err(|err| err.restore(py)) .ok(); }, @@ -1294,7 +1294,7 @@ impl PyLazyFrame { self.ldf.clone().into() } - fn collect_schema(&mut self, py: Python) -> PyResult { + fn collect_schema<'py>(&mut self, py: Python<'py>) -> PyResult> { let schema = py .allow_threads(|| self.ldf.collect_schema()) .map_err(PyPolarsErr::from)?; @@ -1305,7 +1305,7 @@ impl PyLazyFrame { .set_item(fld.name().as_str(), &Wrap(fld.dtype().clone())) .unwrap() }); - Ok(schema_dict.to_object(py)) + Ok(schema_dict) } fn unnest(&self, columns: Vec) -> Self { diff --git a/crates/polars-python/src/lazyframe/mod.rs b/crates/polars-python/src/lazyframe/mod.rs index 7ad03295c041..85433332e415 100644 --- a/crates/polars-python/src/lazyframe/mod.rs +++ b/crates/polars-python/src/lazyframe/mod.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - mod exitable; #[cfg(feature = "pymethods")] mod general; diff --git a/crates/polars-python/src/lazyframe/serde.rs b/crates/polars-python/src/lazyframe/serde.rs index 126652df2298..82e2d4c87f5d 100644 --- a/crates/polars-python/src/lazyframe/serde.rs +++ b/crates/polars-python/src/lazyframe/serde.rs @@ -13,13 +13,13 @@ use crate::prelude::*; #[pymethods] #[allow(clippy::should_implement_trait)] impl PyLazyFrame { - fn __getstate__(&self, py: Python) -> PyResult { + fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult> { // Used in pickle/pickling let mut writer: Vec = vec![]; ciborium::ser::into_writer(&self.ldf.logical_plan, &mut writer) .map_err(|e| PyPolarsErr::Other(format!("{}", e)))?; - Ok(PyBytes::new(py, &writer).to_object(py)) + Ok(PyBytes::new(py, &writer)) } fn __setstate__(&mut self, py: Python, state: PyObject) -> PyResult<()> { diff --git a/crates/polars-python/src/lazyframe/visit.rs b/crates/polars-python/src/lazyframe/visit.rs index 37059e892e6d..4198b54c7ad7 100644 --- a/crates/polars-python/src/lazyframe/visit.rs +++ b/crates/polars-python/src/lazyframe/visit.rs @@ -89,37 +89,32 @@ impl NodeTraverser { this_node.copy_exprs(&mut self.expr_scratch); } - fn scratch_to_list(&mut self) -> PyObject { - Python::with_gil(|py| { - PyList::new_bound(py, self.scratch.drain(..).map(|node| node.0)).to_object(py) - }) + fn scratch_to_list<'py>(&mut self, py: Python<'py>) -> PyResult> { + PyList::new(py, self.scratch.drain(..).map(|node| node.0)) } - fn expr_to_list(&mut self) -> PyObject { - Python::with_gil(|py| { - PyList::new_bound( - py, - self.expr_scratch - .drain(..) - .map(|e| PyExprIR::from(e).into_py(py)), - ) - .to_object(py) - }) + fn expr_to_list<'py>(&mut self, py: Python<'py>) -> PyResult> { + PyList::new( + py, + self.expr_scratch + .drain(..) + .map(|e| PyExprIR::from(e).into_pyobject(py).unwrap()), + ) } } #[pymethods] impl NodeTraverser { /// Get expression nodes - fn get_exprs(&mut self) -> PyObject { + fn get_exprs<'py>(&mut self, py: Python<'py>) -> PyResult> { self.fill_expressions(); - self.expr_to_list() + self.expr_to_list(py) } /// Get input nodes - fn get_inputs(&mut self) -> PyObject { + fn get_inputs<'py>(&mut self, py: Python<'py>) -> PyResult> { self.fill_inputs(); - self.scratch_to_list() + self.scratch_to_list(py) } /// The current version of the IR diff --git a/crates/polars-python/src/lazyframe/visitor/mod.rs b/crates/polars-python/src/lazyframe/visitor/mod.rs index 39af9e064b73..8e4ea93de0f4 100644 --- a/crates/polars-python/src/lazyframe/visitor/mod.rs +++ b/crates/polars-python/src/lazyframe/visitor/mod.rs @@ -1,2 +1,3 @@ +#![allow(deprecated)] pub mod expr_nodes; pub mod nodes; diff --git a/crates/polars-python/src/map/dataframe.rs b/crates/polars-python/src/map/dataframe.rs index 313f7baff04d..3136472d30ee 100644 --- a/crates/polars-python/src/map/dataframe.rs +++ b/crates/polars-python/src/map/dataframe.rs @@ -4,6 +4,7 @@ use polars_core::series::SeriesIter; use pyo3::prelude::*; use pyo3::pybacked::PyBackedStr; use pyo3::types::{PyBool, PyFloat, PyInt, PyList, PyString, PyTuple}; +use pyo3::IntoPyObjectExt; use super::*; use crate::PyDataFrame; @@ -49,7 +50,7 @@ pub fn apply_lambda_unknown<'a>( apply_lambda_with_bool_out_type(df, py, lambda, null_count, first_value) .into_series(), ) - .into_py(py), + .into_py_any(py)?, false, )); } else if out.is_instance_of::() { @@ -66,7 +67,7 @@ pub fn apply_lambda_unknown<'a>( ) .into_series(), ) - .into_py(py), + .into_py_any(py)?, false, )); } else if out.is_instance_of::() { @@ -82,7 +83,7 @@ pub fn apply_lambda_unknown<'a>( ) .into_series(), ) - .into_py(py), + .into_py_any(py)?, false, )); } else if out.is_instance_of::() { @@ -92,7 +93,7 @@ pub fn apply_lambda_unknown<'a>( apply_lambda_with_string_out_type(df, py, lambda, null_count, first_value) .into_series(), ) - .into_py(py), + .into_py_any(py)?, false, )); } else if out.hasattr("_s")? { @@ -104,7 +105,7 @@ pub fn apply_lambda_unknown<'a>( apply_lambda_with_list_out_type(df, py, lambda, null_count, Some(&series), dt)? .into_series(), ) - .into_py(py), + .into_py_any(py)?, false, )); } else if out.extract::>>().is_ok() { @@ -121,7 +122,7 @@ pub fn apply_lambda_unknown<'a>( ) .map_err(PyPolarsErr::from)?, ) - .into_py(py), + .into_py_any(py)?, true, )); } else if out.is_instance_of::() || out.is_instance_of::() { diff --git a/crates/polars-python/src/map/mod.rs b/crates/polars-python/src/map/mod.rs index b4b0f1019696..9ffc74961302 100644 --- a/crates/polars-python/src/map/mod.rs +++ b/crates/polars-python/src/map/mod.rs @@ -1,4 +1,3 @@ -#![allow(deprecated)] pub mod dataframe; pub mod lazy; pub mod series; diff --git a/crates/polars-python/src/map/series.rs b/crates/polars-python/src/map/series.rs index 844fd5f5b3ce..478c44b22f51 100644 --- a/crates/polars-python/src/map/series.rs +++ b/crates/polars-python/src/map/series.rs @@ -245,7 +245,7 @@ impl<'a> ApplyLambda<'a> for BooleanChunked { let mut null_count = 0; for opt_v in self.into_iter() { if let Some(v) = opt_v { - let arg = PyTuple::new_bound(py, [v]); + let arg = PyTuple::new(py, [v])?; let out = lambda.call1(arg)?; if out.is_none() { null_count += 1; @@ -856,7 +856,7 @@ impl<'a> ApplyLambda<'a> for StringChunked { let mut null_count = 0; for opt_v in self.into_iter() { if let Some(v) = opt_v { - let arg = PyTuple::new_bound(py, [v]); + let arg = PyTuple::new(py, [v])?; let out = lambda.call1(arg)?; if out.is_none() { null_count += 1; diff --git a/crates/polars-python/src/on_startup.rs b/crates/polars-python/src/on_startup.rs index 09542a0d87c2..352f67ae1399 100644 --- a/crates/polars-python/src/on_startup.rs +++ b/crates/polars-python/src/on_startup.rs @@ -63,9 +63,6 @@ fn warning_function(msg: &str, warning: PolarsWarning) { .unwrap(); if let Err(e) = warn_fn.call1((msg, Wrap(warning).into_pyobject(py).unwrap())) { - // Force load lazy state of the exception so we don't deadlock in Display impl of PyErr - e.value(py); - eprintln!("{e}") } }); diff --git a/crates/polars-python/src/series/buffers.rs b/crates/polars-python/src/series/buffers.rs index a7bfc5ea6918..960b44949337 100644 --- a/crates/polars-python/src/series/buffers.rs +++ b/crates/polars-python/src/series/buffers.rs @@ -178,7 +178,6 @@ impl PySeries { /// Construct a PySeries from information about its underlying buffer. #[staticmethod] unsafe fn _from_buffer( - py: Python, dtype: Wrap, buffer_info: BufferInfo, owner: &Bound<'_, PyAny>, @@ -189,7 +188,7 @@ impl PySeries { offset, length, } = buffer_info; - let owner = owner.to_object(py); + let owner = owner.to_owned().unbind(); let arr_boxed = match dtype { dt if dt.is_numeric() => { diff --git a/crates/polars-python/src/series/construction.rs b/crates/polars-python/src/series/construction.rs index e9dbdf264d8c..5de101f325a4 100644 --- a/crates/polars-python/src/series/construction.rs +++ b/crates/polars-python/src/series/construction.rs @@ -23,13 +23,8 @@ macro_rules! init_method { #[pymethods] impl PySeries { #[staticmethod] - fn $name( - py: Python, - name: &str, - array: &Bound>, - _strict: bool, - ) -> Self { - mmap_numpy_array(py, name, array) + fn $name(name: &str, array: &Bound>, _strict: bool) -> Self { + mmap_numpy_array(name, array) } } }; @@ -44,14 +39,10 @@ init_method!(new_u16, u16); init_method!(new_u32, u32); init_method!(new_u64, u64); -fn mmap_numpy_array( - py: Python, - name: &str, - array: &Bound>, -) -> PySeries { +fn mmap_numpy_array(name: &str, array: &Bound>) -> PySeries { let vals = unsafe { array.as_slice().unwrap() }; - let arr = unsafe { arrow::ffi::mmap::slice_and_owner(vals, array.to_object(py)) }; + let arr = unsafe { arrow::ffi::mmap::slice_and_owner(vals, array.clone().unbind()) }; Series::from_arrow(name.into(), arr.to_boxed()) .unwrap() .into() @@ -78,7 +69,7 @@ impl PySeries { }); ca.with_name(name.into()).into_series().into() } else { - mmap_numpy_array(py, name, array) + mmap_numpy_array(name, array) } } @@ -94,7 +85,7 @@ impl PySeries { }); ca.with_name(name.into()).into_series().into() } else { - mmap_numpy_array(py, name, array) + mmap_numpy_array(name, array) } } } @@ -106,7 +97,7 @@ impl PySeries { let len = values.len()?; let mut builder = BooleanChunkedBuilder::new(name.into(), len); - for res in values.iter()? { + for res in values.try_iter()? { let value = res?; if value.is_none() { builder.append_null() @@ -131,7 +122,7 @@ where let len = values.len()?; let mut builder = PrimitiveChunkedBuilder::::new(name.into(), len); - for res in values.iter()? { + for res in values.try_iter()? { let value = res?; if value.is_none() { builder.append_null() @@ -176,7 +167,7 @@ fn convert_to_avs<'a>( allow_object: bool, ) -> PyResult>> { values - .iter()? + .try_iter()? .map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict, allow_object)) .collect() } @@ -186,7 +177,7 @@ impl PySeries { #[staticmethod] fn new_from_any_values(name: &str, values: &Bound, strict: bool) -> PyResult { let any_values_result = values - .iter()? + .try_iter()? .map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict, true)) .collect::>>(); let result = any_values_result.and_then(|avs| { @@ -202,13 +193,8 @@ impl PySeries { if !strict && result.is_err() { return Python::with_gil(|py| { let objects = values - .iter()? - .map(|v| { - let obj = ObjectValue { - inner: v?.to_object(py), - }; - Ok(obj) - }) + .try_iter()? + .map(|v| v?.extract()) .collect::>>()?; Ok(Self::new_object(py, name, objects, strict)) }); @@ -239,7 +225,7 @@ impl PySeries { let len = values.len()?; let mut builder = StringChunkedBuilder::new(name.into(), len); - for res in values.iter()? { + for res in values.try_iter()? { let value = res?; if value.is_none() { builder.append_null() @@ -259,7 +245,7 @@ impl PySeries { let len = values.len()?; let mut builder = BinaryChunkedBuilder::new(name.into(), len); - for res in values.iter()? { + for res in values.try_iter()? { let value = res?; if value.is_none() { builder.append_null() diff --git a/crates/polars-python/src/series/general.rs b/crates/polars-python/src/series/general.rs index c557d85c4121..25a9473b3f0f 100644 --- a/crates/polars-python/src/series/general.rs +++ b/crates/polars-python/src/series/general.rs @@ -390,7 +390,7 @@ impl PySeries { } #[cfg(feature = "ipc_streaming")] - fn __getstate__(&self, py: Python) -> PyResult { + fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult> { // Used in pickle/pickling let mut buf: Vec = vec![]; // IPC only support DataFrames so we need to convert it @@ -399,7 +399,7 @@ impl PySeries { .with_compat_level(CompatLevel::newest()) .finish(&mut df) .expect("ipc writer"); - Ok(PyBytes::new(py, &buf).to_object(py)) + Ok(PyBytes::new(py, &buf)) } #[cfg(feature = "ipc_streaming")] diff --git a/crates/polars-python/src/series/map.rs b/crates/polars-python/src/series/map.rs index 279ba155a5c2..be2a4e664fa7 100644 --- a/crates/polars-python/src/series/map.rs +++ b/crates/polars-python/src/series/map.rs @@ -226,7 +226,7 @@ impl PySeries { }, Some(DataType::List(inner)) => { // Make sure the function returns a Series of the correct data type. - let function_owned = function.to_object(py); + let function_owned = function.clone().unbind(); let dtype_py = Wrap((*inner).clone()); let function_wrapped = PyCFunction::new_closure(py, None, None, move |args, _kwargs| { @@ -235,7 +235,8 @@ impl PySeries { pl_series(py).call1(py, ("", out, &dtype_py)) }) })? - .to_object(py); + .into_any() + .unbind(); let ca = dispatch_apply!( series, diff --git a/crates/polars-python/src/series/mod.rs b/crates/polars-python/src/series/mod.rs index ee8206f81666..0c1ecbc40b1c 100644 --- a/crates/polars-python/src/series/mod.rs +++ b/crates/polars-python/src/series/mod.rs @@ -1,4 +1,3 @@ -#![allow(deprecated)] #[cfg(feature = "pymethods")] mod aggregation; #[cfg(feature = "pymethods")] diff --git a/crates/polars-python/src/series/numpy_ufunc.rs b/crates/polars-python/src/series/numpy_ufunc.rs index d5b04715240c..471e0e19c89e 100644 --- a/crates/polars-python/src/series/numpy_ufunc.rs +++ b/crates/polars-python/src/series/numpy_ufunc.rs @@ -35,7 +35,7 @@ unsafe fn aligned_array( let ptr = PY_ARRAY_API.PyArray_NewFromDescr( py, PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type), - T::get_dtype_bound(py).into_dtype_ptr(), + T::get_dtype(py).into_dtype_ptr(), dims.ndim_cint(), dims.as_dims_ptr(), strides.as_ptr() as *mut _, // strides @@ -83,7 +83,7 @@ macro_rules! impl_ufuncs { if !allocate_out { // We're not going to allocate the output array. // Instead, we'll let the ufunc do it. - let result = lambda.call1((PyNone::get_bound(py),))?; + let result = lambda.call1((PyNone::get(py),))?; let series_factory = crate::py_modules::pl_series(py).bind(py); return series_factory .call((self.name(), result), None)? @@ -97,7 +97,7 @@ macro_rules! impl_ufuncs { debug_assert_eq!(get_refcnt(&out_array), 1); // inserting it in a tuple increase the reference count by 1. - let args = PyTuple::new_bound(py, &[out_array.clone()]); + let args = PyTuple::new(py, &[out_array.clone()])?; debug_assert_eq!(get_refcnt(&out_array), 2); // whatever the result, we must take the leaked memory ownership back