From eae176c21b1ef915227294e8a8a201b6f266031a Mon Sep 17 00:00:00 2001 From: Vrishabh Date: Thu, 21 Nov 2024 20:04:59 +0530 Subject: [PATCH] update to pyo3 0.23.0 (#6745) --- arrow-pyarrow-integration-testing/Cargo.toml | 2 +- arrow-pyarrow-integration-testing/src/lib.rs | 2 +- arrow/Cargo.toml | 2 +- arrow/src/pyarrow.rs | 34 ++++++++++++-------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/arrow-pyarrow-integration-testing/Cargo.toml b/arrow-pyarrow-integration-testing/Cargo.toml index 0834f2d13384..03d08df30959 100644 --- a/arrow-pyarrow-integration-testing/Cargo.toml +++ b/arrow-pyarrow-integration-testing/Cargo.toml @@ -34,4 +34,4 @@ crate-type = ["cdylib"] [dependencies] arrow = { path = "../arrow", features = ["pyarrow"] } -pyo3 = { version = "0.22", features = ["extension-module"] } +pyo3 = { version = "0.23", features = ["extension-module"] } diff --git a/arrow-pyarrow-integration-testing/src/lib.rs b/arrow-pyarrow-integration-testing/src/lib.rs index e12c1389e66f..d4908fff0897 100644 --- a/arrow-pyarrow-integration-testing/src/lib.rs +++ b/arrow-pyarrow-integration-testing/src/lib.rs @@ -43,7 +43,7 @@ fn to_py_err(err: ArrowError) -> PyErr { #[pyfunction] fn double(array: &Bound, py: Python) -> PyResult { // import - let array = make_array(ArrayData::from_pyarrow_bound(&array)?); + let array = make_array(ArrayData::from_pyarrow_bound(array)?); // perform some operation let array = array diff --git a/arrow/Cargo.toml b/arrow/Cargo.toml index a0fd96415a1d..732819cf0b5f 100644 --- a/arrow/Cargo.toml +++ b/arrow/Cargo.toml @@ -54,7 +54,7 @@ arrow-select = { workspace = true } arrow-string = { workspace = true } rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true } -pyo3 = { version = "0.22.2", default-features = false, optional = true } +pyo3 = { version = "0.23", default-features = false, optional = true } chrono = { workspace = true, optional = true } diff --git a/arrow/src/pyarrow.rs b/arrow/src/pyarrow.rs index 6effe1c03e01..4ccbd0541d3f 100644 --- a/arrow/src/pyarrow.rs +++ b/arrow/src/pyarrow.rs @@ -111,7 +111,7 @@ impl IntoPyArrow for T { } fn validate_class(expected: &str, value: &Bound) -> PyResult<()> { - let pyarrow = PyModule::import_bound(value.py(), "pyarrow")?; + let pyarrow = PyModule::import(value.py(), "pyarrow")?; let class = pyarrow.getattr(expected)?; if !value.is_instance(&class)? { let expected_module = class.getattr("__module__")?.extract::()?; @@ -177,7 +177,7 @@ impl ToPyArrow for DataType { fn to_pyarrow(&self, py: Python) -> PyResult { let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?; let c_schema_ptr = &c_schema as *const FFI_ArrowSchema; - let module = py.import_bound("pyarrow")?; + let module = py.import("pyarrow")?; let class = module.getattr("DataType")?; let dtype = class.call_method1("_import_from_c", (c_schema_ptr as Py_uintptr_t,))?; Ok(dtype.into()) @@ -213,7 +213,7 @@ impl ToPyArrow for Field { fn to_pyarrow(&self, py: Python) -> PyResult { let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?; let c_schema_ptr = &c_schema as *const FFI_ArrowSchema; - let module = py.import_bound("pyarrow")?; + let module = py.import("pyarrow")?; let class = module.getattr("Field")?; let dtype = class.call_method1("_import_from_c", (c_schema_ptr as Py_uintptr_t,))?; Ok(dtype.into()) @@ -249,7 +249,7 @@ impl ToPyArrow for Schema { fn to_pyarrow(&self, py: Python) -> PyResult { let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?; let c_schema_ptr = &c_schema as *const FFI_ArrowSchema; - let module = py.import_bound("pyarrow")?; + let module = py.import("pyarrow")?; let class = module.getattr("Schema")?; let schema = class.call_method1("_import_from_c", (c_schema_ptr as Py_uintptr_t,))?; Ok(schema.into()) @@ -309,7 +309,7 @@ impl ToPyArrow for ArrayData { let array = FFI_ArrowArray::new(self); let schema = FFI_ArrowSchema::try_from(self.data_type()).map_err(to_py_err)?; - let module = py.import_bound("pyarrow")?; + let module = py.import("pyarrow")?; let class = module.getattr("Array")?; let array = class.call_method1( "_import_from_c", @@ -318,7 +318,7 @@ impl ToPyArrow for ArrayData { addr_of!(schema) as Py_uintptr_t, ), )?; - Ok(array.to_object(py)) + Ok(array.unbind()) } } @@ -335,7 +335,7 @@ impl ToPyArrow for Vec { .iter() .map(|v| v.to_pyarrow(py)) .collect::>>()?; - Ok(values.to_object(py)) + Ok(PyList::new(py, values)?.unbind().into()) } } @@ -451,7 +451,7 @@ impl FromPyArrow for ArrowArrayStreamReader { // make the conversion through PyArrow's private API // this changes the pointer's memory and is thus unsafe. // In particular, `_export_to_c` can go out of bounds - let args = PyTuple::new_bound(value.py(), [stream_ptr as Py_uintptr_t]); + let args = PyTuple::new(value.py(), [stream_ptr as Py_uintptr_t])?; value.call_method1("_export_to_c", args)?; let stream_reader = ArrowArrayStreamReader::try_new(stream) @@ -469,9 +469,9 @@ impl IntoPyArrow for Box { let mut stream = FFI_ArrowArrayStream::new(self); let stream_ptr = (&mut stream) as *mut FFI_ArrowArrayStream; - let module = py.import_bound("pyarrow")?; + let module = py.import("pyarrow")?; let class = module.getattr("RecordBatchReader")?; - let args = PyTuple::new_bound(py, [stream_ptr as Py_uintptr_t]); + let args = PyTuple::new(py, [stream_ptr as Py_uintptr_t])?; let reader = class.call_method1("_import_from_c", args)?; Ok(PyObject::from(reader)) @@ -500,11 +500,17 @@ impl<'source, T: FromPyArrow> FromPyObject<'source> for PyArrowType { } } -impl IntoPy for PyArrowType { - fn into_py(self, py: Python) -> PyObject { +impl<'py, T: IntoPyArrow> IntoPyObject<'py> for PyArrowType { + type Target = PyAny; + + type Output = Bound<'py, Self::Target>; + + type Error = PyErr; + + fn into_pyobject(self, py: Python<'py>) -> Result { match self.0.into_pyarrow(py) { - Ok(obj) => obj, - Err(err) => err.to_object(py), + Ok(obj) => Result::Ok(obj.into_bound(py)), + Err(err) => Result::Err(err), } } }