pyo3_arrow/ffi/from_python/
array.rs1use 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};
8
9impl<'py> FromPyObject<'_, 'py> for PyArray {
10 type Error = PyErr;
11
12 fn extract(obj: Borrowed<'_, 'py, PyAny>) -> Result<Self, Self::Error> {
13 if obj.hasattr(intern!(obj.py(), "__arrow_c_array__"))? {
14 let (schema_capsule, array_capsule) = call_arrow_c_array(&obj)?;
15 Self::from_arrow_pycapsule(&schema_capsule, &array_capsule)
16 } else {
17 #[cfg(feature = "buffer_protocol")]
18 if let Ok(buf) = obj.extract::<AnyBufferProtocol>() {
19 return Ok(buf.try_into()?);
20 }
21
22 Err(PyValueError::new_err(
23 "Expected object with __arrow_c_array__ method or implementing buffer protocol.",
24 ))
25 }
26 }
27}