Skip to content

Commit

Permalink
Bump to pyo3 0.21 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jun 22, 2024
1 parent d1fa965 commit 9a845fd
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 98 deletions.
2 changes: 1 addition & 1 deletion arro3-compute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ arrow-buffer = "52"
arrow-schema = "52"
arrow-select = "52"
arrow = { version = "52", features = ["ffi"] }
pyo3 = { version = "0.20.0", features = ["abi3-py38"] }
pyo3 = { version = "0.21.0", features = ["abi3-py38"] }
thiserror = "1"
arro3-internal = { path = "../arro3-internal" }
24 changes: 12 additions & 12 deletions arro3-core/Cargo.lock

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

2 changes: 1 addition & 1 deletion arro3-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ crate-type = ["cdylib"]

[dependencies]
arro3-internal = { path = "../arro3-internal" }
pyo3 = { version = "0.20.0", features = ["abi3-py38"] }
pyo3 = { version = "0.21.0", features = ["abi3-py38"] }
2 changes: 1 addition & 1 deletion arro3-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn ___version() -> &'static str {

/// A Python module implemented in Rust.
#[pymodule]
fn _rust(_py: Python, m: &PyModule) -> PyResult<()> {
fn _rust(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(___version))?;

m.add_class::<arro3_internal::array::PyArray>()?;
Expand Down
4 changes: 2 additions & 2 deletions arro3-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ arrow-array = "52"
arrow-buffer = "52"
arrow-schema = "52"
arrow = { version = "52", features = ["ffi"] }
pyo3 = { version = "0.20.0", features = ["abi3-py38"] }
numpy = { version = "0.20", features = ["half"] }
pyo3 = { version = "0.21.0", features = ["abi3-py38"] }
numpy = { version = "0.21", features = ["half"] }
thiserror = "1"

[lib]
Expand Down
18 changes: 9 additions & 9 deletions arro3-internal/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl PyArray {
}

pub fn to_python(&self, py: Python) -> PyArrowResult<PyObject> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let arro3_mod = py.import_bound(intern!(py, "arro3.core"))?;
let core_obj = arro3_mod.getattr(intern!(py, "Array"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
self.__arrow_c_array__(py, None)?,
Expand Down Expand Up @@ -65,17 +65,17 @@ impl PyArray {
&'py self,
py: Python<'py>,
requested_schema: Option<PyObject>,
) -> PyArrowResult<&'py PyTuple> {
) -> PyArrowResult<Bound<PyTuple>> {
let field = &self.field;
let ffi_schema = FFI_ArrowSchema::try_from(field)?;
let ffi_array = FFI_ArrowArray::new(&self.array.to_data());

let schema_capsule_name = CString::new("arrow_schema").unwrap();
let array_capsule_name = CString::new("arrow_array").unwrap();

let schema_capsule = PyCapsule::new(py, ffi_schema, Some(schema_capsule_name))?;
let array_capsule = PyCapsule::new(py, ffi_array, Some(array_capsule_name))?;
let tuple = PyTuple::new(py, vec![schema_capsule, array_capsule]);
let schema_capsule = PyCapsule::new_bound(py, ffi_schema, Some(schema_capsule_name))?;
let array_capsule = PyCapsule::new_bound(py, ffi_array, Some(array_capsule_name))?;
let tuple = PyTuple::new_bound(py, vec![schema_capsule, array_capsule]);

Ok(tuple)
}
Expand All @@ -96,16 +96,16 @@ impl PyArray {
/// Returns:
/// Self
#[classmethod]
pub fn from_arrow(_cls: &PyType, input: &PyAny) -> PyResult<Self> {
pub fn from_arrow(_cls: &Bound<PyType>, input: &Bound<PyAny>) -> PyResult<Self> {
input.extract()
}

/// Construct this object from a bare Arrow PyCapsule
#[classmethod]
pub fn from_arrow_pycapsule(
_cls: &PyType,
schema_capsule: &PyCapsule,
array_capsule: &PyCapsule,
_cls: &Bound<PyType>,
schema_capsule: &Bound<PyCapsule>,
array_capsule: &Bound<PyCapsule>,
) -> PyResult<Self> {
let (array, field) = import_array_pycapsules(schema_capsule, array_capsule)?;
Ok(Self::new(array, Arc::new(field)))
Expand Down
15 changes: 9 additions & 6 deletions arro3-internal/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl PyChunkedArray {
}

pub fn to_python(&self, py: Python) -> PyArrowResult<PyObject> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let arro3_mod = py.import_bound(intern!(py, "arro3.core"))?;
let core_obj = arro3_mod
.getattr(intern!(py, "ChunkedArray"))?
.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![self.__arrow_c_stream__(py, None)?]),
PyTuple::new_bound(py, vec![self.__arrow_c_stream__(py, None)?]),
)?;
Ok(core_obj.to_object(py))
}
Expand Down Expand Up @@ -77,14 +77,14 @@ impl PyChunkedArray {
&'py self,
py: Python<'py>,
requested_schema: Option<PyObject>,
) -> PyResult<&'py PyCapsule> {
) -> PyResult<Bound<'py, PyCapsule>> {
let field = self.field.clone();
let chunks = self.chunks.clone();

let array_reader = Box::new(ArrayIterator::new(chunks.into_iter().map(Ok), field));
let ffi_stream = new_stream(array_reader);
let stream_capsule_name = CString::new("arrow_array_stream").unwrap();
PyCapsule::new(py, ffi_stream, Some(stream_capsule_name))
PyCapsule::new_bound(py, ffi_stream, Some(stream_capsule_name))
}

pub fn __eq__(&self, other: &PyChunkedArray) -> bool {
Expand All @@ -101,13 +101,16 @@ impl PyChunkedArray {
}

#[classmethod]
pub fn from_arrow(_cls: &PyType, input: &PyAny) -> PyResult<Self> {
pub fn from_arrow(_cls: &Bound<PyType>, input: &Bound<PyAny>) -> PyResult<Self> {
input.extract()
}

/// Construct this object from a bare Arrow PyCapsule
#[classmethod]
pub fn from_arrow_pycapsule(_cls: &PyType, capsule: &PyCapsule) -> PyResult<Self> {
pub fn from_arrow_pycapsule(
_cls: &Bound<PyType>,
capsule: &Bound<PyCapsule>,
) -> PyResult<Self> {
let stream = import_stream_pycapsule(capsule)?;

let stream_reader = ArrowArrayStreamReader::try_new(stream)
Expand Down
8 changes: 6 additions & 2 deletions arro3-internal/src/ffi/from_python/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyArray {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let (schema_capsule, array_capsule) = call_arrow_c_array(ob)?;
Python::with_gil(|py| {
Self::from_arrow_pycapsule(py.get_type::<PyArray>(), schema_capsule, array_capsule)
Self::from_arrow_pycapsule(
&py.get_type_bound::<PyArray>(),
&schema_capsule,
&array_capsule,
)
})
}
}
6 changes: 4 additions & 2 deletions arro3-internal/src/ffi/from_python/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyChunkedArray {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let capsule = call_arrow_c_stream(ob)?;
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyChunkedArray>(), capsule))
Python::with_gil(|py| {
Self::from_arrow_pycapsule(&py.get_type_bound::<PyChunkedArray>(), &capsule)
})
}
}
4 changes: 2 additions & 2 deletions arro3-internal/src/ffi/from_python/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyField {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let capsule = call_arrow_c_schema(ob)?;
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyField>(), capsule))
Python::with_gil(|py| Self::from_arrow_pycapsule(&py.get_type_bound::<PyField>(), &capsule))
}
}
8 changes: 4 additions & 4 deletions arro3-internal/src/ffi/from_python/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyRecordBatch {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let (schema_capsule, array_capsule) = call_arrow_c_array(ob)?;
Python::with_gil(|py| {
Self::from_arrow_pycapsule(
py.get_type::<PyRecordBatch>(),
schema_capsule,
array_capsule,
&py.get_type_bound::<PyRecordBatch>(),
&schema_capsule,
&array_capsule,
)
})
}
Expand Down
4 changes: 2 additions & 2 deletions arro3-internal/src/ffi/from_python/record_batch_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyRecordBatchReader {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let capsule = call_arrow_c_stream(ob)?;
Python::with_gil(|py| {
Self::from_arrow_pycapsule(py.get_type::<PyRecordBatchReader>(), capsule)
Self::from_arrow_pycapsule(&py.get_type_bound::<PyRecordBatchReader>(), &capsule)
})
}
}
6 changes: 4 additions & 2 deletions arro3-internal/src/ffi/from_python/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PySchema {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let schema_ptr = call_arrow_c_schema(ob)?;
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PySchema>(), schema_ptr))
Python::with_gil(|py| {
Self::from_arrow_pycapsule(&py.get_type_bound::<PySchema>(), &schema_ptr)
})
}
}
4 changes: 2 additions & 2 deletions arro3-internal/src/ffi/from_python/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PyTable {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
let capsule = call_arrow_c_stream(ob)?;
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyTable>(), capsule))
Python::with_gil(|py| Self::from_arrow_pycapsule(&py.get_type_bound::<PyTable>(), &capsule))
}
}
32 changes: 18 additions & 14 deletions arro3-internal/src/ffi/from_python/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pyo3::types::{PyCapsule, PyTuple};
use pyo3::{PyAny, PyResult};

/// Validate PyCapsule has provided name
pub fn validate_pycapsule_name(capsule: &PyCapsule, expected_name: &str) -> PyResult<()> {
pub fn validate_pycapsule_name(capsule: &Bound<PyCapsule>, expected_name: &str) -> PyResult<()> {
let capsule_name = capsule.name()?;
if let Some(capsule_name) = capsule_name {
let capsule_name = capsule_name.to_str()?;
Expand All @@ -28,27 +28,29 @@ pub fn validate_pycapsule_name(capsule: &PyCapsule, expected_name: &str) -> PyRe
}

/// Import `__arrow_c_schema__` across Python boundary
pub(crate) fn call_arrow_c_schema(ob: &PyAny) -> PyResult<&PyCapsule> {
pub(crate) fn call_arrow_c_schema<'py>(ob: &'py Bound<PyAny>) -> PyResult<Bound<'py, PyCapsule>> {
if !ob.hasattr("__arrow_c_schema__")? {
return Err(PyValueError::new_err(
"Expected an object with dunder __arrow_c_schema__",
));
}

Ok(PyTryInto::try_into(
ob.getattr("__arrow_c_schema__")?.call0()?,
)?)
Ok(ob.getattr("__arrow_c_schema__")?.call0()?.downcast_into()?)
}

pub(crate) fn import_schema_pycapsule(capsule: &PyCapsule) -> PyResult<&FFI_ArrowSchema> {
pub(crate) fn import_schema_pycapsule<'py>(
capsule: &'py Bound<PyCapsule>,
) -> PyResult<&'py FFI_ArrowSchema> {
validate_pycapsule_name(capsule, "arrow_schema")?;

let schema_ptr = unsafe { capsule.reference::<FFI_ArrowSchema>() };
Ok(schema_ptr)
}

/// Import `__arrow_c_array__` across Python boundary
pub(crate) fn call_arrow_c_array(ob: &PyAny) -> PyResult<(&PyCapsule, &PyCapsule)> {
pub(crate) fn call_arrow_c_array<'py>(
ob: &'py Bound<PyAny>,
) -> PyResult<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)> {
if !ob.hasattr("__arrow_c_array__")? {
return Err(PyValueError::new_err(
"Expected an object with dunder __arrow_c_array__",
Expand All @@ -62,14 +64,14 @@ pub(crate) fn call_arrow_c_array(ob: &PyAny) -> PyResult<(&PyCapsule, &PyCapsule
));
}

let schema_capsule = PyTryInto::try_into(tuple.get_item(0)?)?;
let array_capsule = PyTryInto::try_into(tuple.get_item(1)?)?;
let schema_capsule = tuple.get_item(0)?.downcast_into()?;
let array_capsule = tuple.get_item(1)?.downcast_into()?;
Ok((schema_capsule, array_capsule))
}

pub(crate) fn import_array_pycapsules(
schema_capsule: &PyCapsule,
array_capsule: &PyCapsule,
schema_capsule: &Bound<PyCapsule>,
array_capsule: &Bound<PyCapsule>,
) -> PyResult<(ArrayRef, Field)> {
validate_pycapsule_name(schema_capsule, "arrow_schema")?;
validate_pycapsule_name(array_capsule, "arrow_array")?;
Expand All @@ -84,18 +86,20 @@ pub(crate) fn import_array_pycapsules(
}

/// Import `__arrow_c_stream__` across Python boundary.
pub(crate) fn call_arrow_c_stream(ob: &PyAny) -> PyResult<&PyCapsule> {
pub(crate) fn call_arrow_c_stream<'py>(ob: &'py Bound<PyAny>) -> PyResult<Bound<'py, PyCapsule>> {
if !ob.hasattr("__arrow_c_stream__")? {
return Err(PyValueError::new_err(
"Expected an object with dunder __arrow_c_stream__",
));
}

let capsule = PyTryInto::try_into(ob.getattr("__arrow_c_stream__")?.call0()?)?;
let capsule = ob.getattr("__arrow_c_stream__")?.call0()?.downcast_into()?;
Ok(capsule)
}

pub(crate) fn import_stream_pycapsule(capsule: &PyCapsule) -> PyResult<FFI_ArrowArrayStream> {
pub(crate) fn import_stream_pycapsule(
capsule: &Bound<PyCapsule>,
) -> PyResult<FFI_ArrowArrayStream> {
validate_pycapsule_name(capsule, "arrow_array_stream")?;

let stream = unsafe { FFI_ArrowArrayStream::from_raw(capsule.pointer() as _) };
Expand Down
Loading

0 comments on commit 9a845fd

Please sign in to comment.