pyo3_arrow/ffi/from_python/
array.rs

1use crate::array::*;
2#[cfg(feature = "buffer_protocol")]
3use crate::buffer::AnyBufferProtocol;
4use crate::ffi::from_python::utils::call_arrow_c_array;
5use pyo3::exceptions::PyValueError;
6use pyo3::prelude::*;
7use pyo3::{intern, PyAny, PyResult};
8
9impl<'a> FromPyObject<'a> for PyArray {
10    fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
11        if ob.hasattr(intern!(ob.py(), "__arrow_c_array__"))? {
12            let (schema_capsule, array_capsule) = call_arrow_c_array(ob)?;
13            Self::from_arrow_pycapsule(&schema_capsule, &array_capsule)
14        } else {
15            #[cfg(feature = "buffer_protocol")]
16            if let Ok(buf) = ob.extract::<AnyBufferProtocol>() {
17                return Ok(buf.try_into()?);
18            }
19
20            Err(PyValueError::new_err(
21                "Expected object with __arrow_c_array__ method or implementing buffer protocol.",
22            ))
23        }
24    }
25}