(
ob: &'a Bound<'py, PyAny>,
check: unsafe fn(Python<'py>, *mut ffi::PyObject) -> c_int,
)
| 142 | |
| 143 | impl<T: Element, D: Dimension> PyArray<T, D> { |
| 144 | fn extract<'a, 'py, E>( |
| 145 | ob: &'a Bound<'py, PyAny>, |
| 146 | check: unsafe fn(Python<'py>, *mut ffi::PyObject) -> c_int, |
| 147 | ) -> Result<&'a Bound<'py, Self>, E> |
| 148 | where |
| 149 | E: From<CastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'py>>, |
| 150 | { |
| 151 | // Check if the object is an array. |
| 152 | let array = unsafe { |
| 153 | if check(ob.py(), ob.as_ptr()) == 0 { |
| 154 | return Err(CastError::new( |
| 155 | ob.as_borrowed(), |
| 156 | <Self as PyTypeCheck>::classinfo_object(ob.py()), |
| 157 | ) |
| 158 | .into()); |
| 159 | } |
| 160 | ob.cast_unchecked::<Self>() |
| 161 | }; |
| 162 | |
| 163 | // Check if the dimensionality matches `D`. |
| 164 | let src_ndim = array.ndim(); |
| 165 | if let Some(dst_ndim) = D::NDIM { |
| 166 | if src_ndim != dst_ndim { |
| 167 | return Err(DimensionalityError::new(src_ndim, dst_ndim).into()); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Check if the element type matches `T`. |
| 172 | let src_dtype = array.dtype(); |
| 173 | let dst_dtype = T::get_dtype(ob.py()); |
| 174 | if !src_dtype.is_equiv_to(&dst_dtype) { |
| 175 | return Err(TypeError::new(src_dtype, dst_dtype).into()); |
| 176 | } |
| 177 | |
| 178 | Ok(array) |
| 179 | } |
| 180 | |
| 181 | /// Creates a new uninitialized NumPy array. |
| 182 | /// |
no test coverage detected